9.7. Texture Coordinate GenerationOpenGL can be set up to compute texture coordinates automatically, based only on the incoming vertex positions. Five methods are defined, and each can be useful for certain purposes. The texture generation mode specified by GL_OBJECT_LINEAR is useful for cases in which a texture is to remain fixed to a geometric model, such as in a terrain modeling application. GL_EYE_LINEAR is useful for producing dynamic contour lines on an object. Examples of this usage include a scientist studying isosurfaces or a geologist interpreting seismic data. GL_SPHERE_MAP can generate texture coordinates for simple environment mapping. GL_REFLECTION_MAP and GL_NORMAL_MAP can work in conjunction with cube map textures. GL_REFLECTION_MAP passes the reflection vector as the texture coordinate. GL_NORMAL_MAP simply passes the computed eye space normal as the texture coordinate. A function that generates sphere map coordinates according to the OpenGL specification is shown in Listing 9.20. Listing 9.20. GL_SPHERE_MAP computation
A function that generates reflection map coordinates according to the OpenGL specification looks almost identical to the function shown in Listing 9.20. The difference is that it returns the reflection vector as its result (see Listing 9.21). Listing 9.21. GL_REFLECTION_MAP computation
Listing 9.22 shows the code for selecting between the five texture generation methods and computing the appropriate texture coordinate values. Listing 9.22. Texture coordinate generation computation
In this code, we assume that each texture unit less than NumEnabledTexture-Units is enabled. If this value is 0, the whole loop is skipped. Otherwise, each texture coordinate that is needed is computed in the loop. Because the sphere map and reflection computations do not depend on any of the texture unit state, they can be performed once and the result is used for all texture units. For the GL_OBJECT_LINEAR and GL_EYE_LINEAR methods, there is a plane equation for each component of each set of texture coordinates. For the former case, we generate the components of gl_TexCoord[0] by multiplying the plane equation coefficients for the specified component by the incoming vertex position. For the latter case, we compute the components of gl_TexCoord[0] by multiplying the plane equation coefficients by the eye coordinate position of the vertex. Depending on what type of texture access is done during fragment processing, it may not be necessary to compute the t, p, or q texture component,[1] so these computations could be eliminated.
|