Texture mapping

Texture mapping is technique that maps a 2D image onto polygonal surfaces.

The mapping between the texture image and a polygon is performed by associating a texture coordinate with each vertex of the polygon. The coordinates of the rectangular texture image are 0 to 1 in both the x and y directions, regardless of its aspect ratio.

In the following example, we will map a decorative pattern:

onto a rectangular surface:

The texture map image is specified as a rectangular bitmap. Both dimensions must be powers of 2, i.e. width = 2^n, height = 2^m. The minimum bitmap size is 64x64.

We need to execute the following steps:

to perform the texture mapping.

  unsigned char *texture;                  /* texture data */
  int width, height;                       /* size of texture data */

  /* specify the texture */

  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);   /* use byte alignment */

  glTexImage2D(GL_TEXTURE_2D, 0, 3,        /* specify the texture */
               _width, _height, 0,
               GL_RGB, GL_UNSIGNED_BYTE,
               _texture);

  /* specify the mapping mode */

  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

  /* enable texture mapping */

  glEnable(GL_TEXTURE_2D);                 /* enable texture mapping */

  /* draw the scene with geometric and texture coordinates */

  glBegin(GL_QUADS);                       /* draw a rectangle */

  /* assign texture coordinates to each vertex */
  glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -0.5);
  glTexCoord2f(1.0, 0.0); glVertex2f( 2.0, -0.5);
  glTexCoord2f(1.0, 1.0); glVertex2f( 2.0,  1.0);
  glTexCoord2f(0.0, 1.0); glVertex2f(-1.0,  1.0);

  glEnd();

The entire texture image is mapped to the full rectangle. Since the aspect ratios are not the same, there is some distortion caused by the mapping.

Note that as we rotate the surface, the mapped texture image follows the perspective foreshortening.

Although the coordinates for the texture image run from 0 to 1, we can specify texture coordinates greater than one. Typically, this is done to repeat the texture pattern.

  ...

  /* assign texture coordinates to each vertex */
  glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -0.5);
  glTexCoord2f(3.0, 0.0); glVertex2f( 2.0, -0.5);
  glTexCoord2f(3.0, 2.0); glVertex2f( 2.0,  1.0);
  glTexCoord2f(0.0, 2.0); glVertex2f(-1.0,  1.0);

  ...

This repeats the pattern 3 times in the X direction and 2 times in the Y direction.

The texture mapping can occur in two modes: decal mode, in which the texture image completely obscures the surface appearance of the polygonal surface, in other words, ignoring the effects of color and the lighting model; and modulate mode, in which the final appearance of the surface is a composite of the texture image and the original surface properties.


Back to input (keyboard)...
Back to the 13.016 overview...