< Zurück | Inhalt | Weiter >

OBJECT_LINEAR mode


The OBJECT_LINEAR texture coordinate generation mode calculates texture coordinates based on the relative positions of vertices. The TexCoordTest example creates a simulated landscape that has contours automatically mapped onto the landscape Everything above the y = 0 plane is texture−mapped green, while everything below is texture−mapped blue.


Figure 14.7 illustrates the texture image used in the TexCoordTest example for dynamic texture mapping. The texture image is 64 x 64 pixels and merely contains a single row of pixels that is of interest—the rest of the image is ignored. The bottom row of the image (t = 0) defines the colors to be dynamically applied to the landscape. The midpoint of the row (s = 0.5) defines the elevation = 0 (sea level) contour, while everything to the left of the midpoint is used for elevations below sea level, and everything to the right is used for elevations above sea level. Different colored pixels for contours are evenly spaced from the midpoint.


image


Figure 14.7 The texture image used to perform dynamic mapping of texture coordinates in the TexCoordTest

example application


To map contours onto the landscape we merely need to define a mapping from the y coordinate of the landscape to the s coordinate of the texture image. That is, we are defining a 1D−to−1D mapping from vertex coordinates to texture coordinates.

A vertex’s position is defined using three dimensions (x,y,z), while a texture coordinate can potentially be expressed in three dimensions (although typically only s and t are used).


We define a plane for each of the texture coordinates (s, t, and sometimes r). The s texture coordinate is given by a vertex’s position relative to the s plane, the t coordinate is given by a vertex’s position relative to the t plane, and so on. Planes are defined by specifying the direction of the vector perpendicular (normal) to the plane.


For example, to create our mapping from y vertex coordinate to s texture coordinate:


TexCoordGeneration texGen =

new TexCoordGeneration( TexCoordGeneration.OBJECT_LINEAR,

TexCoordGeneration.TEXTURE_COORDINATE_2, new Vector4f( 0,

(float)

(1.0/(2 * yMaxHeight)), 0,

0.5f ),

new Vector4f( 0,0,0,0 ), new Vector4f( 0,0,0,0 ) );


The parameters to the TexCoordGeneration constructor do the following:


1. Specify the texture coordinate generation mode that we are using, in this case, OBJECT_LINEAR.

2. Specify that we are generating 2D texture coordinates (s and t).

3. Define the mapping from vertex coordinate to s coordinate.

4. Define the mapping from vertex coordinate to t coordinate.

5. Define the mapping from vertex coordinate to r coordinate (which is unused when 2D texture coordinates are used).


The mapping from vertex coordinates to s coordinates we defined was: (0, (float) (1.0/ (2 * yMaxHeight)), 0, 0.5f)

This equates to:


s texture coordinate = (0.0 * vertex x) + (1.0/ 2 * yMaxHeight * vertex y) + (0.0 * vertex z) + 0.5;


image

Figure 14.8 Using OBJECT_LINEAR texture coordinate generation


That is, the t texture coordinate is equal to a scaled version of the y vertex coordinate plus an offset of 0.5. We use an offset of 0.5 because we defined the midpoint of the texture image as the elevation = 0 contour.


From the equation you can see that: Vertex y = yMaxHeight, s = 1.0 Vertex y = 0.0, s = 0.5

Vertex y = – yMaxHeight, s = 0.0


That is, we have successfully defined a mapping from vertex y coordinate in the range ± yMaxHeight to s texture coordinates in the range 0.0 to 1.0. The texture coordinate is independent of a vertex’s x and z coordinates.


Using the OBJECT_LINEAR mode, the landscape has texture coordinates automatically calculated, coloring areas above sea level green (light gray) and areas below sea level blue (dark gray). As the landscape as a whole is rotated and translated, the texture coordinates are unaffected. The vertex coordinates in the local coordinate system of the landscape are unchanged, despite the origin of the landscape’s coordinate being shifted.