s The s Lecture 22 Sections 8.8, 8.9, 8.10 Hampden-Sydney College Wed, Oct 28, 2009
Outline s The 1 2 3 4 5 The 6 7 8
Outline s The 1 2 3 4 5 The 6 7 8
Creating Images s The To create a texture image 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 a Images s The Example (Creating a Image) GLubyte image[64][64][3]; void makeimage() { // Make a 64 x 64 black and white checkerboard for (int i = 0; i < 64; i++) for (int j = 0; j < 64; j++) if ((i & 0x8) ˆ (j & 0x8)) for (int k = 0; k < 3; k++) image[i][j][k] = 0; else for (int k = 0; k < 3; k++) image[i][j][k] = 255; return; }
Creating Images s The A checkerboard texture
Storing s in s The The function glpixelstore*() tells how texels are stored in texture memory. 1 = on byte boundaries. 2 = on even-numbered byte boundaries. 4 = on word boundaries. 8 = on double-word boundaries. We will use glpixelstorei(gl_unpack_alignment, 1);
Storing s in s The The function glteximage2d() stores the image in texture memory. glteximage2d(gl_texture_2d, level, internal format, width, height, border, image format, image type, ptr to image);
s The level is used if we are supplying multiple levels of detail, 0 otherwise. internal format is usually GL_RGB or GL_RGBA. 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 array of texels.
s The A typical function call is glteximage2d(gl_texture_2d, 0, GL_RGB, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
Outline s The 1 2 3 4 5 The 6 7 8
s The 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 0 0 1 s
Mapping s to Vertices s The 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(1.0, 0.0); glvertex3f(4.0, -3.0, 0.0); gltexcoord2f(1.0, 1.0); glvertex3f(4.0, 3.0, 0.0); gltexcoord2f(0.0, 1.0); glvertex3f(-4.0, 3.0, 0.0); glend();
Mapping s to Vertices s 1 t 3 y x The 0 0 s 1-3 -4 Mapping textures to vertices 4
Outline s The 1 2 3 4 5 The 6 7 8
s The The function gltexparameter*() is used to set other texture attributes. We can set whether to tile the texture. 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].
Using GL_REPEAT s The Example (Using GL_REPEAT) glbegin(gl_quads); gltexcoord2f(0.0, 0.0); glvertex3f(-4.0, -3.0, 0.0); gltexcoord2f(4.0, 0.0); glvertex3f(4.0, -3.0, 0.0); gltexcoord2f(4.0, 3.0); glvertex3f(4.0, 3.0, 0.0); gltexcoord2f(0.0, 3.0); glvertex3f(-4.0, 3.0, 0.0); glend();
Applying s s 1 t 3 y The 0 0 1-1 s -3-4 -2 Using GL_REPEAT 4 x
s The We can set whether to interpolate texels. gltexparameteri(gl_texture_2d, GL_TEXTURE_MAG_FILTER, GL_NEAREST); We can also use GL_TEXTURE_MIN_FILTER for minification. GL_LINEAR for interpolation.
Outline s The 1 2 3 4 5 The 6 7 8
Creating s The A texture object consists of An array of texels. The various texture parameters. memory may contain several texture objects (as many as fit). Each object has its own texel array and parameters.
Creating s The A texture object is accessed through a reference number. OpenGL will give us a reference number to the object which we will use to access it.
Creating s The To get a reference number for a single texture, write unsigned int texname; glgens(1, &texname); To get a reference numbers for a several textures, write unsigned int texname[numofs]; glgens(numofs, texname);
Creating s The To create a texture object, use the glbind() function. is used to glbind(gl_texture_2d, texname[i]); The first time this function is called with the texture name texname[i], the texture object is initialized and made the active texture. In all subsequent such calls, texname[i] becomes the active texture.
Outline s The 1 2 3 4 5 The 6 7 8
The s The The texture environment contains a number of texture state variables that apply to all textures. We use the function gltexenv*() to specify the values of the environment variables. We will use the form gltexenvf(gl_texture_env, GL_TEXTURE_ENV_MODE, parameter);
The s The The parameter may take one of the following five values. GL_REPLACE Replace the object color with the texture color. GL_DECAL Replace the object color with the texture color. GL_MODULATE Multiply the object color by the texture color. GL_BLEND Multiply the object color by the reverse of the texture color. GL_ADD Add the texture color to the object color.
Applying s s Finally, textures may be enabled and disabled. glenable(gl_texture_2d); gldisable(gl_texture_2d); The
Outline s The 1 2 3 4 5 The 6 7 8
Brick s Example (Brick ) The code. The executable. The
Multiple s s Example (Multiple s) The code. The executable. The
Outline s The 1 2 3 4 5 The 6 7 8
s The A bitmap file (.bmp) is a file that contains the color of each pixel of a rectangular region. Among other information, the file contains the height and width of the region. See http://en.wikipedia.org/wiki/windows_bitmap
Reading s The The Bitmap class has the following public member functions. readbitmap(filename) - Read the bitmap info from the specified file. imagewidth() - Get the width of the image, in pixels. imageheight() - Get the height of the image, in pixels. imagedata() - Get a pointer to the array of RGB values.
Reading s The We will use the function Bitmap image; image.readbitmap(filename); to 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 s The We will use the function the other three member function in the glteximade2d() function. to read the information from the bitmap file and store the relevant information in a Bitmap object. glteximage2d(gl_texture_2d, 0, GL_RGB, image.imagewidth(), image.imageheight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.imagedata());
Bitmap s s Example (Bitmap s) The code. The executable. The
Outline s The 1 2 3 4 5 The 6 7 8
Homework s The Homework Read Section 8.8 textures in OpenGL. Read Section 8.9 texture generation. Read Section 8.10 environment maps.