MULTIMEDIA

Programming with DirectX : Textures in Direct3D 10 (part 1) - Textures Coordinates

1/15/2011 9:46:58 AM

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.

Figure 1. A square displaying vertices.


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).

Figure 2. Texture coordinates for a surface’s vertices.


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.

Figure 3. Changing the texture coordinates alters the output.


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.


Other  
  •  Programming with DirectX : Shading and Surfaces - Types of Textures
  •  iPhone 3D Programming : Adding Shaders to ModelViewer (part 2)
  •  iPhone 3D Programming : Adding Shaders to ModelViewer (part 1) - New Rendering Engine
  •  iPhone 3D Programming : Adding Depth and Realism - Shaders Demystified
  •  Programming with DirectX : Transformation Demo
  •  Programming with DirectX : View Transformations
  •  Programming with DirectX : World Transformations
  •  Programming with DirectX : Projection Transformations
  •  iPhone 3D Programming : Adding Depth and Realism - Lighting Up (part 2)
  •  iPhone 3D Programming : Adding Depth and Realism - Lighting Up (part 1)
  •  iPhone 3D Programming : Adding Depth and Realism - Surface Normals (part 2)
  •  iPhone 3D Programming : Adding Depth and Realism - Surface Normals (part 1)
  •  iPhone 3D Programming : Adding Depth and Realism - Filling the Wireframe with Triangles
  •  iPhone 3D Programming : Adding Depth and Realism - Creating and Using the Depth Buffer
  •  iPhone 3D Programming : Adding Depth and Realism - Examining the Depth Buffer
  •  iPhone 3D Programming : HelloCone with Fixed Function
  •  iPhone 3D Programming : Vector Beautification with C++
  •  jQuery 1.3 : An image carousel
  •  jQuery 1.3 : Headline rotator
  •  Silverlight : Print a Document
  •  
    Most View
    HTC One X+ Review - Highlighting Almost All Power Of HTC One X (Part 2)
    Skype For Windows 8 - A New Way Of Using Skype
    ASP.NET 4 : Data Source Controls (part 2) - Parameterized Commands
    Ten Stellar Keyboard Shortcuts
    Windows Phone 8 Group Test – June 2013 (Part 1) : HTC Windows Phone 8S, HTC Windows Phone 8X
    Microsoft Surface With Windows RT Review (Part 1)
    Improve Your Mac (Part 1) - Import Pictures into iPhoto
    Apple iPod Nano – The Trendy Compactness
    Sony Vaio Tap 20 Mobile Desktop - Meet The Laptablet
    Affordable Strong-Bass Denon Headphones
    Top 10
    The NZXT Kraken X40 Compact Liquid Cooler Review (Part 3)
    The NZXT Kraken X40 Compact Liquid Cooler Review (Part 2)
    T-Mobile’s Samsung Galaxy Note II Review (Part 6)
    T-Mobile’s Samsung Galaxy Note II Review (Part 5)
    T-Mobile’s Samsung Galaxy Note II Review (Part 4)
    T-Mobile’s Samsung Galaxy Note II Review (Part 3)
    T-Mobile’s Samsung Galaxy Note II Review (Part 2)
    T-Mobile’s Samsung Galaxy Note II Review (Part 1)
    Sony Cybershot DSC-TF1 - Affordable Water-Resistant Camera
    Buffalo MiniStation Slim 500GB External Hard Drive