Lab 8: Ugly Textures

CSC385 - Computer Graphics

Goal: Learn the basics of texture mapping in OpenGL. You can find more information online.

Action:

To start, download the source and image files from Blackboard. Create a visual studio project and insert the source files. Put the image files where where visual studio can find them: inside the folder with your .sln file should be a folder with the name of the project. Your images go in this folder. Build the project and run the executable. You should see two triangles with textures attached. Use the mouse to manipulate the camera. Notice that the textures remain in place. Texture mapping is essentially the "gluing" of an image onto a polygonal surface. To texture map in OpenGL you need to do the following:

1. Read in the texture image.

The enclosed demo has a simple method to read bmp files. This procedure only works with 24 bit image (8 bits for each of three channels). If you have a file in another format you can convert it to the prescribed format in gimp, photoshop, etc. Note: OpenGL requires that texture images have a width and height that are powers of 2! The texture does not have to be square. If you have problems with texture mapping this is the first thing to check.

2. Create a texture object.

This is done in the demo in initializeTexture(). There are various parameters that you can change to create various effects but the chosen settings should work for most applications. Note: The texture should be read and initialized ONCE when the application is started. Do not re-intialize each time you want to use the texture. The texture handle -- named htexture in the demo -- is used as the application runs to identify the texture. You can create more than one texture at a time; each will have a unique texture handle.

3. Draw the triangle with texturing enabled, the texture bound, and texture coordinates provided.

The display function in the demo shows how to draw a triangle with a texture map. You need to enable texturing and bind the appropriate texture before starting to draw the triangle, provide a texture coordinate for each vertex during drawing, then disable texturing when done. The texture coordinate tells OpenGL the point in the image that should be glued to the triangle vertex. The image itself will be stretched or shrunk to fit. Texture coordinates of (0,0) and (1,1) correspond, respectively, to the lower left corner and upper right corners of the image. You can use texture coordinate outside the range of [0,1]; OpenGL simply drops the integer part. (Another way to think of this is that the plane is tessellated with 1x1 squares of your image. This is what the GL_REPEAT means in the initialization code.) Play around with the texture coordinates in the demo to be sure you understand how they work.

4. Your turn!

Modify the program to make an ugly house. Your house should be a rectangle with a triangle for the roof. The roof should be textured with texture.bmp. The square should be textured with checkerboard.bmp.

5. Make your house 3D. You can decide how to texture it. (Maybe find a new texture???)

6. Tryout different texture options:

7. Try out an advanced technique of your choice from the "red book."

8. (optional) Challenge: A billboard is a texture mapped surface that is oriented toward the viewer. A demo is available on here. Add a billboard to your scene.