Texture Coordinates

[Previous] [Next]

A Direct3D texture is nothing more than a bitmap—a two-dimensional array of color values. Each entry in the array is an individual color value called a texel. Each texel has a unique address in the texture that is basically the column and row position, defined as u (column) and v (row). This address is called a texture coordinate and is represented in the texture's own coordinate space. A position in texture space is specified in relation to the texture's origin (0, 0).

When you apply a texture to a 3D primitive, Direct3D needs to map the texel addresses into object coordinates and then translate those addresses into screen coordinates (pixel locations). Direct3D maps texels from texture space directly into pixels in screen space. Direct3D also determines the texel or texels needed to color each pixel in screen space. The texture color of the point on the polygon that maps to the 2D pixel location is determined in a process known as texture filtering, which is described in detail later in this chapter. You can access each texel in a texture by specifying its texel coordinates. However, Direct3D needs a uniform address range for all the texels in a texture so that it can map them onto a primitive. To do this, Direct 3D uses a normalized addressing scheme in which texture addresses consist of texel coordinates that map to the address range 0.0 to 1.0. The texture addresses are defined using (u, v) values, which are analogous to (x, y) coordinates in screen space. Be aware that mapping from texture space to screen space means that texels from differently sized textures can have identical texture addresses but can be mapped to different screen coordinates. In short, by using normalized coordinates for textures, Direct3D allows you to deal with texture coordinates without worrying about the dimensions of the texture map you're using.

Assigning Texture Coordinates

You can assign texture coordinates to the vertices (see the D3DVERTEX structure) that define the primitives in your 3D world. This way, you can control which part of the texture is mapped onto the primitive. Also, when multitexturing (texture blending), you can use flexible vertex formats to specify a (u, v) pair for each texture stage. (You'll learn more about texture stages later in this chapter.) For example, if you have a rectangular primitive that represents a wall segment and that has the same aspect ratio (the ratio of width to height) as a texture, you can assign the texture coordinates (0.0, 0.0), (1.0, 0.0), (1.0, 1.0), and (0.0, 1.0) to the primitive's vertices, causing Direct3D to stretch the texture over the entire rectangle.

If you instead choose to apply the texture to a rectangle that's half as wide, you have to decide how to apply it. The first option is to apply the entire texture to the rectangle, which requires you to change the texture's aspect ratio (squash the texture) so that it fits the narrower wall. When you scale the texture, the texture-filtering method you choose will impact the quality of the rendered image.

The second option is to apply the left or right half of the texture to the wall. If you choose to apply the left side of the texture, assign the texture coordinates (0.0, 0.0), (0.5, 0.0), (0.5, 1.0), and (0.0, 1.0) to the vertices. If you've designed textures with edges that blend well together, this approach might be problematic for some applications because it effectively cuts the texture in half, preventing the edges from matching colorwise.

The third option is to change the texture-addressing mode to allow techniques such as wrapping, clamping, and mirroring (which you'll learn about later in the chapter).



Inside Direct3D
Inside Direct3D (Dv-Mps Inside)
ISBN: 0735606137
EAN: 2147483647
Year: 1999
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net