9.9. Texture Application
The built-in texture functions read values from texture memory. The values read from texture memory are used in a variety of ways. OpenGL fixed functionality includes support for texture application formulas enabled with the symbolic constants GL_REPLACE, GL_MODULATE, GL_DECAL, GL_BLEND, and GL_ADD. These modes operate differently, depending on the format of the texture being accessed. The following code illustrates the case in which an RGBA texture is accessed with the sampler tex0. The variable color is initialized to be gl_Color and then modified as needed so that it contains the color value that results from texture application.
GL_REPLACE is the simplest texture application mode of all. It simply replaces the current fragment color with the value read from texture memory. See Listing 9.24.
Listing 9.24. GL_REPLACE computation
GL_MODULATE causes the incoming fragment color to be multiplied by the value retrieved from texture memory. This is a good texture function to use if lighting is computed before texturing (e.g., the vertex shader performs the lighting computation, and the fragment shader does the texturing). White can be used as the base color for an object rendered with this technique, and the texture then provides the diffuse color. This technique is illustrated with the OpenGL shader code in Listing 9.25.
Listing 9.25. GL_MODULATE computation
GL_DECAL is useful for applying an opaque image to a portion of an object. For instance, you might want to apply a number and company logos to the surfaces of a race car or tattoos to the skin of a character in a game. When an RGBA texture is accessed, the alpha value at each texel linearly interpolates between the incoming fragment's RGB value and the texture's RGB value. The incoming fragment's alpha value is used as is. The code for implementing this mode is in Listing 9.26.
Listing 9.26. GL_DECAL computation
GL_BLEND is the only texture application mode that takes the current texture environment color into account. The RGB values read from the texture linearly interpolate between the RGB values of the incoming fragment and the texture environment color. We compute the new alpha value by multiplying the alpha of the incoming fragment by the alpha read from the texture. The OpenGL shader code is shown in Listing 9.27.
Listing 9.27. GL_BLEND computation
GL_ADD computes the sum of the incoming fragment color and the value read from the texture. The two alpha values are multiplied together to compute the new alpha value. This is the only traditional texture application mode for which the resulting values can exceed the range [0,1], so we clamp the final result (see Listing 9.28).
Listing 9.28. GL_ADD computation
The texture-combine environment mode that was added in OpenGL 1.3 and extended in OpenGL 1.4 defines a large number of additional simple ways to perform texture application. A variety of new formulas, source values, and operands were defined. The mapping of these additional modes into OpenGL shader code is straightforward but tiresome, so it is omitted here.