Programming Textures Lecture 15 Fri, Sep 28, 2007
Creating Texture Objects To create a texture object internally, Create a 3-dimensional array of unsigned bytes (chars) First dimension = row number Second dimension = column number Third dimension = RGB level Assign to each byte a value from 0 to 255.
Creating Texture Objects GLubyte image[64][64][3]; void makeImage() { // Make black and white checkerboard for (int i = 0; i < 64; i++) for (int j = 0; j < 64; j++) if ((i & 0x8) == (j & 0x8)) image[i][j][0] = 0; image[i][j][1] = 0; image[i][j][2] = 0; } : return;
Creating Texture Objects The function glPixelStore*() tells how texels are stored. 1 = on byte boundaries. 2 = on word boundaries. 4 = on long-word boundaries. We will use glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Creating Texture Objects We need to create a texture object. A texture object has an internal structure determined by OpenGL. OpenGL will give us a reference number to the object which we will use to access it. Declare an array of unsigned integers to hold the reference numbers. unsigned int texName[number];
Creating Texture Objects The function call glGenTextures(number, texName); will return the specified number of integers to represent texture objects. These integers are stored in the array texName.
Creating Texture Objects The function call glBindTexture(GL_TEXTURE_2D, texName[i]); is used to Initialize a texture object. Make a specified texture object the active texture. The first time texName[i] is used, the object is initialized and made active. In all subsequent uses, texName[i] becomes the active texture.
Creating Texture Objects The function glTexImage2D() defines the image of the current texture object. glTexImage2D(GL_TEXTURE_2D, level, internal format, width, height, border, image format, image type, ptr to image); level is used if we are supplying multiple levels of detail, 0 otherwise. internal format is usually GL_RGB or GL_RGBA.
Creating Texture Objects width and height must be powers of two. border is 0 if there is no border, 1 otherwise. image format is the format of the data in the external image array, usually GL_RGB or GL_RGBA. image type is the data type of each value in the image array, usually unsigned byte. ptr to image is a pointer to the image array.
Creating Texture Objects A typical function call is glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
Texture Coordinates Each texture has its own coordinate system. s is the horizontal axis; t is the vertical axis. The texture coordinates are always 0 s 1, 0 t 1. 1 t s 1
Mapping Textures To apply a texture to a polygon, we must map a pair of texture coordinates (s, t) to each vertex (x, y, z) of the polygon. glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3f(-4.0, -3.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(4.0, -3.0, 0.0); : glEnd();
Applying Textures We have several choices concerning how the texture is applied to the polygon Replace, blend, decal, or modulate. Repeat or clamp. Use nearest texel or interpolate texels. The most common combination of choices is replace, repeat, and interpolate.
Applying Textures We use the function glTexEnv*() to specify whether to replace, blend, decal, or modulate the face color with the texture color. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); Other choices for the third parameter are GL_BLEND, GL_DECAL, and GL_MODULATE.
Applying Textures The function glTexParameter*() is used to make the other choices. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); We can also use GL_TEXTURE_WRAP_T for the t direction. GL_CLAMP to clamp s or t to [0, 1].
Applying Textures glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); We can also use GL_TEXTURE_MIN_FILTER for minification. GL_LINEAR for interpolation.
Applying Textures Finally, textures must be enabled. glEnable(GL_TEXTURE_2D); Textures can also be disabled. glDisable(GL_TEXTURE_2D);
Example void init() { makeImage(); // Create texture object glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(3, texName); glBindTexture(GL_TEXTURE_2D, texName[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); // Set mode of texture application glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glEnable(GL_TEXTURE_2D); : }
Brick Wall Texture Example Read Run
Bitmaps A bitmap file (.bmp) is a file that contains the color of each pixel of a rectangular region. A bitmap file also contains some header information. See http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
Format of a Bitmap File A bitmap file has the following format. Bitmap file header 2 chars – ‘B’, ‘M’ for “bitmap” int – Total file size of file, in bytes short int – 0 int – offset to pixel data, in bytes Bitmap info header int – size of header, in bytes int – width of image, in pixels
Format of a Bitmap File int – height of image, in pixels short int – number of planes (1) short int – bits per pixel 1 = 2 colors (color table) 4 = 16 colors (color table) 8 = 256 colors (color table) 24 = millions of colors (RGB triples) int – compression (0 = no compression) int – image size in bytes int – pixels per meter horizontally (0)
Format of a Bitmap File The pixel data int – pixels per meter vertically (0) int – number of colors used in color table, or 0 int – number of important colors (0 = all) The pixel data A linear array of RGB triples, padded so that the number of bytes in each row is a multiple of 4.
Reading Bitmaps We will use a function readBitmap(filename); that will read the information from the bitmap file and store the relevant information in a Bitmap object. The Bitmap class assumes that the bitmap file uses 24-bit color. It will not handle other formats.
Reading Bitmaps The Bitmap class has the following public member functions. readBitmap(filename) – read the bitmap info from the specified file. imageWidth() – the width of the image, in pixels. imageHeight() – the height of the image, in pixels. imageData() – a pointer to an array of RGB values.
Bitmap Texture Read Run