Texture and Environment Mapping Sample Programs

Supporting Files

The following programs all read JPEG images using the C routines read_jpeg.c and the associated header file read_jpeg.h.

Sample Programs


    

The program block.cc shows how to use texture mapping. In OpenGL 1.1 and later, you can load more than one texture at a time and then bind one as the active texture when needed. Note that texture dimensions must be powers of 2. While it is possible to scale images in the program, this particular example does not, so any texture files supplied must have dimensions that are powers of 2. The texture file is Ambrosil.JPG from http://textures.forrest.cz.

The following keyboard commands may be used:

left mouserotate object
middle mousezoom object
right mousetranslate object
0toggle light 0
1toggle light 1
ttoggle texture mapping
qquit

    

The program texfilter.cc shows how mipmapping can be used to reduce aliasing effects that occur when textures are mapped to various sized regions in a scene. Note in how the mipmap filtering modes improve the appearance dramatically. The default texture used is checkerboard1.jpg.

Pressing the right mouse button will display a pop-menu that lets you select the filter option used to assign the fragment colors from the texture data. Press 'q' to quit the program.


    

Using textures on rectangular objects and polygons is fairly straightforward. The coke.cc program demonstrates how to apply textures to a curved surface (albeit one composed of rectangles) and a flat surface composed of triangles meeting at a point. It uses the texture files coke_1024x512.jpg (scanned from the label off of 2 liter bottle rather than a can) and top_256x256.jpg.

The following keyboard commands may be used:

left mouserotate object
middle mousezoom object
right mousetranslate object
ttoggle texture mapping
qquit

    

OpenGL provides a mechanism to generate texture coordinates automatically. This is useful when applying textures to irregular surfaces such as the ground in a flight simulator program. In the teapot_texture.cc program the texture coordinates are computed as distances from planes perpendicular to the x and y axes. The coordinates can be fixed to the object or to the viewpoint (press either 'o' or 'e' while the program is running). The texture file is checkerboard1.jpg.

The following keyboard commands may be used:

left mouserotate object
middle mousezoom object
right mousetranslate object
etexture fixed to eye coordinates
otexture fixed to object coordinates
qquit

    

Environment mapping can be done in OpenGL using either sphere mapping or cube mapping. The teapot_sphere.cc program provides a demo of the first of these. This program is a clone of the sphere_map program written by Rob Bateman which can be found (along with many other great examples) in the OpenGL section of his website at http://www.robthebloke.org. The default sphere map texture is sphere_map.jpg. Many others can be found on the web; two of them are spheremap.jpg and Streetscene_env.jpg.

The following keyboard commands may be used:

left mouserotate object
middle mousezoom object
right mousetranslate object
qquit