250 likes | 421 Views
Textures in OpenGL. Lecture 32 Mon, Dec 1, 2003. Creating an Image Internally. To create a 64 x 64 black and white checkerboard, declare the array unsigned char checkerboard[64][64][3]; Then, for each i and j, assign checkboard[i][j][0] = 0; checkboard[i][j][1] = 0;
E N D
Textures in OpenGL Lecture 32 Mon, Dec 1, 2003
Creating an Image Internally • To create a 64 x 64 black and white checkerboard, declare the array unsigned char checkerboard[64][64][3]; • Then, for each i and j, assign checkboard[i][j][0] = 0; checkboard[i][j][1] = 0; checkboard[i][j][2] = 0; to set the color to black, or
Creating an Image Internally checkboard[i][j][0] = 255; checkboard[i][j][1] = 255; checkboard[i][j][2] = 255; to set the color to white. • A clever way to set the color is to set each component to white if and only if ((i & 8) ^ (j & 8)) == 0.
Reading an Image from a File • The RgbImage class has a constructor with prototype RgbImage(char* filename); • filename should be the name of a .bmp file. • This constructor will read the RGB values from the file. • Note that they are RGB, not RGBA.
OpenGL Texture Functions • The OpenGL texture functions typically set the values of various state variables. • Therefore, they can be called in any order, just so all the relevant variables have been set by the time the scene is rendered.
Defining the Current Texture • To define the current texture, write glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, ptr);
Defining the Current Texture • The first 0 means mipmap level 0. • The first GL_RGB is the format to be used in the texture. • The second 0 means no border. • The second GL_RGB is the format used in the image. • ptr is a pointer to the texture. • Specifically, a pointer to an unsigned byte.
Mapping a Texture to a Polygon • Consider the simpler case of mapping a texture to a rectangle. • Use the function glTexCoord2f(s, t). • The specified texture coordinates (s, t) will be mapped to all vertices drawn, until the texture coordinates are changed.
Example • To map the texture coordinate (0, 0) to the vertex with coordinates (0, 1, 1), write glTexCoord2f(0.0, 0.0); glVertex3f(0.0, 1.0, 1.0);
Mapping a Texture to a Polygon • To map the entire texture to the rectangle with corners at (0, 0, 0), (4, 0, 0), (4, 4, 0), and (0, 4, 0), write • glBegin(GL_QUADS); • glTexCoord2f(0, 0); glVertex(0, 0, 0); • glTexCoord2f(1, 0); glVertex(4, 0, 0); • glTexCoord2f(1, 1); glVertex(4, 4, 0); • glTexCoord2f(0, 1); glVertex(0, 4, 0); • glEnd();
Wrapping a Texture • What should OpenGL do if the texture is “smaller” than the polygon? • (How can that be?) • In the last example, we could have mapped • (0, 0) to (0, 0, 0) • (2, 0) to (4, 0, 0) • (2, 2) to (4, 4, 0) • (0, 2) to (0, 4, 0)
Wrapping a Texture 2 ? ? ? 0 Polygon 0 2
Wrapping a Texture • To tell OpenGl to repeat (tile) the texture in each block, we should write glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Example: Using GL_REPEAT 2 0 Polygon 0 2
Wrapping a Texture • Another choice, instead of GL_REPEAT, is GL_CLAMP. • This causes only the edge pixels to be repeated.
Example: Using GL_CLAMP 2 0 Polygon 0 2
Pasting a Texture onto a Polygon • There are several modes we can use when pasting the texture onto the polygon. • GL_DECAL. • GL_REPLACE. • GL_MODULATE. • GL_BLEND.
Pasting a Texture onto a Polygon • We will use GL_REPLACE. • That will replace the color of the surface with the color of the texture. • If we use GL_BLEND, it will mix the surface color with the texture. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
Magnification and Minification Filters • The term “filter” comes from signal theory, which provides a mathematical model of the problems faced in discrete sampling. • We should tell OpenGL which kind of magnification and which kind of minification we want it to do.
Magnification Filter • When a texture is magnified, the two choices are • Use the nearest texel. • Use a weighted average of the nearest 2 2 array of texels (interpolation).
Magnification Filter • To set the magnification filter, we should write one of the following. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // or glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Minification Filter • If we are not using mipmaps, then the minification choices are similar. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // or glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);