Microsoft DirectX 8.1 (Visual Basic)

Texture Coordinate Transformations

Microsoft® Direct3D® devices can transform the texture coordinates for vertices by applying a 4×4 matrix. The system applies transformations to texture coordinates in the same manner as geometry, and any transformations that can be communicated in a 4×4 matrix—scales, rotations, translations, projections, shears, or any combination of these—can be applied.

Note  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 (TnLHAL 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

The following code sets a matrix to apply to the texture coordinates for texture stage 0.

' For this example, the D3DDevice variable contains a
' valid reference to a Direct3DDevice8 object, and
' dx contains a valid reference to a DirectX8 object.

Dim matTrans As D3DMATRIX

Call D3DXMatrixIdentity( matTrans )

' 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 CONST_D3DTEXTURETRANSFORMFLAGS enumeration.

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 example disables them.

' For this example, the D3DDevice variable contains a
' valid reference to a Direct3DDevice8 object.

Call D3DDevice.SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, _
                                    D3DTTFF_DISABLE)

The other values defined in CONST_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, the D3DDevice variable contains a 
' valid reference to a Direct3DDevice8 object.

Call 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, the D3DDevice variable contains a 
' valid pointer to an IDirect3DDevice8 interface.

Call 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 2-D texture coordinates needed to address the texture.