Open GL Super Bible

Previous Table of Contents Next

Chapter 12
Texture Mapping

What you’ll learn in this chapter:

How to… Functions You’ll Use

Drape images onto polygons (texture mapping) glTexImage1D/glTexImage2D
Use .BMP files as textures TextureLoadBitmap/TextureLoadMipmapy
Use automatic texture coordinate generation glTexGen

Texture mapping is probably the most significant advance in computer graphics in the last ten years. OpenGL provides texture image mapping functions that fit images onto polygons in your scene. How those images are put onto the polygons is up to you.

Texture mapping is used in games, including DOOM, for realistic images of rooms and monsters. Unlike OpenGL, these games use a texturing method called raycasting to map texture images onto polygons. Though raycasting is much faster on standard graphics cards than the texture mapping provided by OpenGL, it is also limited to flat surfaces in a 2D plane. That is, you can’t look up or down. Texture mapping in OpenGL doesn’t have this limitation, but you can expect it to work more slowly on standard graphics cards.

The good news is that some newer, affordable 3D graphics cards support OpenGL and hardware texturing. When a board supports hardware texture mapping, your CPU doesn’t have to do all the texture mapping calculations and preparation—the graphics card does it for you.

The examples in this chapter will run on any Windows-compatible graphics card. If your graphics card supports 16- or 24-bit “true color” displays, you’ll want to use them. Besides better-looking scenes, you’ll find that the 16- and 24-bit modes are actually faster.

The Basics of Texture Mapping

Texture mapping in OpenGL is fairly straightforward. To begin with, every texture is an image of some sort.

A 1D texture is an image with width but no height, or vise versa; 1D textures are a single pixel wide or high. You might think that 1D textures aren’t very useful, but in fact they can take the place of more conventional color-shading techniques and accelerate rendering in the process! Figure 12-1 shows a 1D “ROY-G-BIV” (Red, Orange, Yellow - Green - Blue, Indigo, Violet) texture to display a rainbow. The texture image is a line of pixels (color values) covering the color spectrum seen in a rainbow. The equivalent nontextured scene would contain seven times the polygons of the textured one and require much more rendering time.

Figure 12-1  A 1D textured rainbow

A 2D texture is an image that is more than 1 pixel wide and high and is generally loaded from a Windows .BMP file. Two-dimensional textures are commonly used to replace complex surface geometry (lots of polygons) on buildings, trees, and so forth. These 2D textures can also be used to add realistic background details, like the clouds in the sky in Figure 12-2.

Figure 12-2  A 2D sky texture and the resulting scene

The 1D and 2D textures you’ve seen so far are composed of RGB color values. Textures can also be composed of color indices or luminance (gray) levels, and can include alpha (transparency) values. The latter is useful for defining natural objects such as trees, because the alpha value can be used to make the tree visible but let the background show through. You’ll learn more about this in Chapter 16.

Some hardware also supports 3D (volume) textures with OpenGL. Volume textures are used for viewing CAT, MRI, and other 3D “scans.” Unfortunately, even a small 256 x 256 x 256 grayscale texture image will need a whopping 16 MB of memory. Currently an extension to OpenGL, 3D texturing may be included as a required feature in the OpenGL 1.1 specification.

Defining Texture Images

Naturally, you must define a texture image before you can draw textured polygons in OpenGL. Texture images follow the same storage rules as bitmaps (discussed in Chapter 11).

A Note About Texture Images:  
The OpenGL standard requires that texture images’ dimensions must be a power of 2. Texture images can also have 1 or 2 border pixels around their edges to define the color of polygons that fall outside the texture image.

Previous Table of Contents Next