Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

beginning opengl game programming 2004 phần 7 docx
Nội dung xem thử
Mô tả chi tiết
for you. This works in situations where the texture coordinates can be determined using
well-defined mathematical steps. Examples include reflections, contouring, and projective
texturing. We’ll be discussing a couple of specific examples here.
Texture coordinate generation is controlled independently for each coordinate. To use it,
you must first enable it by passing GL_TEXTURE_GEN_S, GL_TEXTURE_GEN_T, GL_TEXTURE_GEN_R, or
GL_TEXTURE_GEN_Q (each corresponding to the indicated coordinate) to glEnable().
To specify how texture coordinates are generated, you use one of the following:
void glTexGen{ifd}(GLenum coord, GLenum pname, TYPE param);
void glTexGen{ifd}v(GLenum coord, GLenum pname, const TYPE *params);
coord indicates which texture coordinate to apply the parameter to. Valid values are GL_S,
GL_T, GL_R, and GL_Q, corresponding to the s, t, r, and q texture coordinates. The accepted
values of pname and the param or params associated with them are listed in Table 9.1.
If the texture generation mode is GL_OBJECT_LINEAR, then the texture coordinates are generated using the following equation:
texcoord = p1 * xo + p2 * yo + p3 * zo + p4 * wo
xo, yo, zo, and wo are the object-space coordinates of the vertex the texture coordinate is
being generated for. p1, p2, p3, and p4 are the four parameters provided via GL_OBJECT_PLANE.
These are used to pass the A, B, C, and D coefficients of a plane, so this equation is in effect
calculating the distance from the plane and using that as the texture coordinate.
The GL_EYE_LINEAR texture generation mode uses a similar equation, except that eye-space
vertex coordinates are used, and the inverse modelview matrix is applied to the plane
parameters when they are specified.
Texture Coordinate Generation 203
Table 9.1 Texture Generation Parameters
Parameter Meaning
GL_TEXTURE_GEN_MODE param specifies the texture generation mode, which must be GL_OBJECT_LINEAR,
GL_EYE_LINEAR, GL_SPHERE_MAP, GL_REFLECTION_MAP*, or GL_NORMAL_MAP*.
GL_OBJECT_PLANE params is a pointer to a four-element array of values that are used as
parameters for the texture coordinate generation function. Used in conjunction
with GL_OBJECT_LINEAR.
GL_EYE_PLANE Same as above, but used with GL_EYE_LINEAR.
* Available only via the ARB_texture_cube_map extension under Windows.
09 BOGL_GP CH09 3/1/04 10:04 AM Page 203
TLFeBOOK
When using the GL_NORMAL_MAP mode, the (s, t, r) texture coordinates are generated by using
the vertex’s normal, transformed into eye-space. These are intended to be used with cube
maps.
The remaining two texture generation modes will be covered in the next section, but for
now, let’s look at an example of generating texture coordinates based on the distance from
a plane.
When rendering terrain, you can create a visually appealing effect by varying the color based
on the height of the terrain. Areas close to sea level appear as sand; slightly higher areas
appear as grass, and then dirt or rock, and finally snow on the highest peaks. One way to
achieve this is to create a one-dimensional texture like the one shown in Figure 9.1. Then,
you would enable texture generation based on the height above sea level by passing the coefficients for the sea level plane to glTexGen() and enable texture generation for the s coordinate. Later in this chapter, in the “Multitexturing” section, you’ll see a demo that does exactly
that, so let’s look at the code that sets up and uses texture coordinate generation.
First, during initialization, texture coordinate generation is enabled, GL_OBJECT_LINEAR
mode is selected, and the reference plane is passed via GL_OBJECT_PLANE:
glEnable(GL_TEXTURE_GEN_S);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
GLfloat waterPlane[] = { 0.0, 1.0, 0.0, -WATER_HEIGHT };
glTexGenfv(GL_S, GL_OBJECT_PLANE, waterPlane);
Then, when actually rendering the terrain, the only thing that needs to be done is to scale
the generated texture coordinate. This is because the generated coordinate is the distance
from the plane, which could be any value. We want the texture coordinates to fall in the
range [0, 1] so that the lowest points correspond to sand and the highest points correspond to snow. This is simply a matter of dividing the texture coordinate by the maximum
terrain height. Using the texture matrix you learned about in the last section, this is done
as follows:
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glScalef(1.0f/MAX_HEIGHT, 1.0, 1.0);
204 Chapter 9 ■ More on Texture Mapping
Figure 9.1 A 1D texture used to color terrain.
09 BOGL_GP CH09 3/1/04 10:04 AM Page 204
TLFeBOOK