Textures Coordinates
To properly display
textures on surfaces, we use texture coordinates. A texture coordinate
is an attribute of the vertex in the same way the position is an
attribute that is used to specify how textures are to be mapped onto
surfaces. When performing texture mapping, we must specify, along with
the positions of each vertex, the texture coordinates.
In Figure 1,
four vertices form the shape of the square. The position property of
the vertex specifies how the shape appears in the 3D virtual world. The
texture coordinates for each vertex, on the other hand, specify how the
texture image is displayed on the rendered surface.
A
1D texture has a single value for the texture coordinate of a vertex, a
2D texture uses two values (one for the width and one for the height),
and a 3D texture uses three values. Cube maps use three values since the
six images make up a single cube texture map whereas a cube map is made
up of six 2D textures, and sphere maps use two since sphere maps are
just 2D images.
A texture coordinate
is essentially a percentage and is defined using floating-point data
types. Using 2D texture coordinates as an example, the first value in
the texture coordinate is the percentage from 0.0 to 1.0 (in other
words, 0% to 100%) of how far along the width this vertex is mapped onto
the image, and the second value is the percentage for the height. These
are the S and T texture coordinates for 2D textures. They are also
known as the U and V or the TU and TV texture coordinates. For 3D
textures, you have S, T, and Q or TU, TV, and TW. The value of a texture
coordinate can go below 0.0 or above 1.0, which can be useful for
tiling a texture so that it appears across a surface repeatedly.
In Figure 2
the upper-left vertex point has a texture coordinate of 0.0 for the S
and 0.0 for the T. This tells graphics APIs such as Direct3D where in
the image the mapping should be for that vertex. The upper-right vertex
has 1.0 and 0.0 for the coordinates; that is, the upper-right vertex
should have the upper-right portion of the textured image mapped to it
(i.e., 100% of the width but 0% of the height).
When a primitive is mapped,
the relationship of all the vertices of the shape determines how the
object will look. So using the example in Figure 2, the image is displayed on the surface as if it was opened up normally in an image editor. However, in Figure 3,
you can see that changing the texture coordinates will alter how the
image is mapped unto the surface. How the image appears mapped on the
surface is solely dependent on the relationship of all the vertices.
In
Direct3D, 0.0 for the S (TU) texture coordinate represents the
left-most part of the image, and 1.0 is the right-most part. For the T
(TV) coordinate 0.0 is the top-most part and 1.0 is the bottom-most
part. Any value between 0.0 and 1.0 falls within that range.
Up to this point we’ve
specified vertices using three floating-point values for the position.
From here on, whenever texture mapping is used, we will need five
floating-point values for 2D textures, where the first three are for the
position and the last two values are the S and T texture coordinates.
Throughout the remainder of this book, we will refer to the texture
coordinates for a 2D texture as the TU and TV pair since many books use
that terminology.