Textures

Texturing is used to shade primitives by looking up color values from a 2D table instead of just linearly interpolating between vertex colors.

Turning on texture mapping

Texture mapping is turned on with glEnable(GL_TEXTURE_2D) and disabled with glDisable(GL_TEXTURE_2D).

Specifying the current texture

OpenGL has a concept of a current texture. The current texture is specified with the glTexture2D command:
glTexture2D(GLenum target,
	    GLint level,
            GLint components,
            GLsizei width,
            GLsizei height,
            GLint border,
            GLenum format,
            GLenum type,
            const GLvoid *pixels)
  • target : GL_TEXTURE_2D
  • level : Used for mipmapping. Use 0 if you aren't doing mipmapping.
  • components : How many data elements there are in each texture element. Use 3 for RGB, 4 for RGBA, and look at the manual for what other numbers mean.
  • width: The width in texture elements of the image. This must be a power of 2.
  • height: The height of the texture map. Must also be a power of 2.
  • border: Set it to 0 if you are not using texture borders. If you do use texture borders, the texture width and height may be a power of 2 plus 2 pixels.
  • format: The format of the pixel data (how the data is packed). You would usually use GL_RGB or GL_RGBA for this, but there are many other possible formats.
  • type: The data type of the texture data (e.g. float, byte, etc.) The usual desired value for this is GL_UNSIGNED_BYTE (0-255 for each component).
  • pixels: The actual texture data.
  • Texture coordinates

    Once you have specified the current texture, you must give texture coordinates for each vertex (remember that there is always a current texture coordinate, so if you do not specify a texture coordinate for a vertex or a set of vertices, they will all get the last texture coordinates specified). Texture coordinate specification should be done before the vertex you want it to affect. It is done through the glTexCoord[1234][dfis]() call. Under most circumstances, you would choose glTexCoord2[dfis].

    Changing texture mapping parameters

    The glTexEnv() call allows you to change how rasterization is performed while using texture maps. Look at the manual for all possible modes, but some of the more interesting ones are the following:

    Assuming you used 3 for the number of components in your glTexture2D call:

    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE) The final RGB of a pixel will be the RGB it would have without texture mapping, multiplied by the RGB of the texture map. This allows the original primitive color to modulate the texture.

    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL) The final RGB of a pixel will be exactly that of the texture.

    Assuming you used 4 for the number of components in your glTexture2D call:

    glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL) The final RGB of the pixel will be an alpha blend (using the texture alpha) of the original RGB with the texture RGB.

    The glTexParameter*() calls allow for further modification of texture behavior. The following are some of the more interesting parameters (see manual for complete list):

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) When the pixel being textured is larger than one texture element, use the texture element closest to the center of the pixel.

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) When the pixel being textured is larger than one texture element, use a weighted average of the four texture elements closest to the center of the pixel.

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) When the pixel being textured is smaller than one texture element, use the texture element closest to the center of the pixel.

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) When the pixel being textured is smaller than one texture element, use a weighted average of the four texture elements closest to the center of the pixel.