Texture Coordinate Transformations (Direct3D 9)
Direct3D devices can transform the texture coordinates for vertices by applying a 4x4 matrix. The system applies transformations to texture coordinates in the same manner as geometry. Any transformation (scale, rotation, translation, projection, shear, or any combination of these) can be done with a 4x4 matrix.
Direct3D does not modify transformed and lit vertices. As a result, an application using transformed and lit vertices cannot use Direct3D to transform the texture coordinates of the vertices.
Devices that support hardware-accelerated transformation and lighting operations (T&L HAL Device) also accelerate the transformation of texture coordinates. When hardware acceleration of transformations isn't available, platform-specific optimizations in the Direct3D geometry pipeline apply to texture coordinate transformations.
Texture coordinate transformations are useful for producing special effects while avoiding the need to directly modify the texture coordinates of your geometry. You could use simple translation or rotation matrices to animate textures on an object, or you can transform texture coordinates that are automatically generated by Direct3D to simplify and perhaps accelerate advanced effects such as projected textures and dynamic light-mapping. Additionally, you might use texture coordinate transforms to reuse a single set of texture coordinates for multiple purposes, in multiple texture stages.
Setting and Retrieving Texture Coordinate Transformations
Like the matrices that your application uses for geometry, you set and retrieve texture coordinate transformations by calling the IDirect3DDevice9::SetTransform and IDirect3DDevice9::GetTransform methods. These methods accept the D3DTS_TEXTURE0 through D3DTS_TEXTURE7 members of the D3DTRANSFORMSTATETYPE enumerated type to identify the transformation matrices for texture stages 0 through 7, respectively.
The following code sets a matrix to apply to the texture coordinates for texture stage 0.
// For this example, assume the d3dDevice variable contains a // valid pointer to an IDirect3DDevice9 interface. D3DMATRIX matTrans = D3DXMatrixIdentity( NULL ); // Set up the matrix for the desired transformation. d3dDevice->SetTransform( D3DTS_TEXTURE0, &matTrans );
Enabling Texture Coordinate Transformations
The D3DTSS_TEXTURETRANSFORMFLAGS texture stage state controls the application of texture coordinate transformations. Values for this texture stage state are defined by the D3DTEXTURETRANSFORMFLAGS enumerated type.
Texture coordinate transformations are disabled when D3DTSS_TEXTURETRANSFORMFLAGS is set to D3DTTFF_DISABLE (the default value). Assuming that texture coordinate transformations were enabled for stage 0, the following code disables them.
// For this example, assume the d3dDevice variable contains a // valid pointer to an IDirect3DDevice9 interface. d3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
The other values defined in D3DTEXTURETRANSFORMFLAGS are used to enable texture coordinate transformations, and to control how many resulting texture coordinate elements are passed to the rasterizer. For example, take the following code.
// For this example, assume the d3dDevice variable contains a // valid pointer to an IDirect3DDevice9 interface. d3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
The D3DTTFF_COUNT2 value instructs the system to apply the transformation matrix set for texture stage 0, and then pass the first two elements of the modified texture coordinates to the rasterizer.
The D3DTTFF_PROJECTED texture transformation flag indicates coordinates for a projected texture. When this flag is specified, the rasterizer divides the elements passed-in by the last element. Take the following code, for example.
// For this example, assume the d3dDevice variable contains a // valid pointer to an IDirect3DDevice9 interface. d3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3 | D3DTTFF_PROJECTED );
This example informs the system to pass three texture coordinate elements to the rasterizer. The rasterizer divides the first two elements by the third, producing the 2D texture coordinates needed to address the texture.