Open GL Super Bible

Previous Table of Contents Next


Drawing Textured Polygons

Once you have defined a texture, you still have to enable texturing. To enable 1D texturing, you’d use the following:

glDisable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_1D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

The glEnable call enables 1D texturing. If you forget to enable texturing, none of your polygons will be textured! The glTexEnvi function sets texturing to “decal” mode, meaning that images are overlaid directly upon the polygons.

Other texturing modes are listed in Table 12-2.

Table 12-2 Texture Modes for GL_TEXTURE_ENV_MODE

Mode Description

GL_MODULATE Texture pixels “filter” existing pixel colors on the screen.
GL_DECAL Texture pixels replace existing pixels on the screen.
GL_BLEND Texture pixels “filter” existing pixels colors and are combined with a constant color.

The GL_MODULATE texture mode multiplies the current texture color (or luminance) by the color on the screen. For one-component (luminance) textures, this translates into a brightness filter that will vary the brightness of the screen image based upon the texture image. For three-component (RGB) textures, you can generate “colored lens filter” effects.

Unlike GL_MODULATE texturing, GL_BLEND texturing allows you to blend a constant color into the scene based upon the texture image. You’d use GL_BLEND texturing for things like clouds; the constant color would be off-white, and the texture image would be of a cloud.

Once you have defined the texturing mode to use, you can then proceed with the drawing of your polygons. Listing 12-3 shows how to draw the rainbow in Figure 12-1.

Listing 12-3 Drawing a 1D textured rainbow

glEnable(GL_TEXTURE_1D);
glCallList(RainbowTexture);
glBegin(GL_QUAD_STRIP);
  for (th = 0.0; th <= M_PI; th += (0.03125 * M_PI))
  {
   /*
    * Bottom edge of rainbow…
    */

    x = cos(th) * 50.0;
    y= sin(th) * 50.0;
    z = -50.0;
    glTexCoord1f(0.0);
    glVertex3f(x, y, z);

   /*
    * Top edge of rainbow…
    */

    x = cos(th) * 55.0;
    y = sin(th) * 55.0;
    z = -50.0;
    glTexCoord1f(1.0);
    glVertex3f(x, y, z);
};
glEnd();

To position the ROY-G-BIV texture on the rainbow, you call glTexCoord. For 1D textures, you call one of the glTexCoord1f, glTexCoord1d, glTexCoord1s, or glTexCoord1i functions. A value of 0.0 represents the leftmost pixel in the image, and 1.0 represents the rightmost pixel. Values outside this range are handled differently depending on the value of the GL_TEXTURE_WRAP_S parameter. If GL_TEXTURE_WRAP_S is set to GL_CLAMP (the default), then texture coordinates are restricted to a range of 0.0 to 1.0, inclusive. When a polygon strays from the texture image, it is drawn using the color(s) along the texture image’s edges (see Figure 12-3) or the texture image border colors, if defined. Texture coordinates are traditionally referred to as S and T, or (s,t) instead of X and Y.


Figure 12-3  GL_CLAMP textures

If you use GL_REPEAT instead, the texture image is tiled over the polygon. Texture coordinates are used modulo 1.0—that is, the texture image repeats at regular intervals. GL_REPEAT texturing can be used to reduce the size of texture images on repetitive surfaces. The challenge with these kinds of textures is to make the edges of each tile blend into the next.


Automatically Generating Texture Coordinates:  
Generating texture coordinates can be a tedious task. Later in this chapter you’ll learn about the glTexGen functions that can generate these coordinates automatically for you.


Previous Table of Contents Next