1 / 23

Textures in OpenGL

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;

Download Presentation

Textures in OpenGL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Textures in OpenGL Lecture 32 Mon, Dec 1, 2003

  2. 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

  3. 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.

  4. 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.

  5. 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.

  6. 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);

  7. 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.

  8. 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.

  9. 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);

  10. 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();

  11. Mapping a Texture to a Polygon

  12. 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)

  13. Wrapping a Texture 2 ? ? ? 0 Polygon 0 2

  14. 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);

  15. Example: Using GL_REPEAT 2 0 Polygon 0 2

  16. Wrapping a Texture • Another choice, instead of GL_REPEAT, is GL_CLAMP. • This causes only the edge pixels to be repeated.

  17. Example: Using GL_CLAMP 2 0 Polygon 0 2

  18. 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.

  19. 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);

  20. 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.

  21. 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).

  22. 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);

  23. 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);

More Related