Microsoft DirectX 8.0 (Visual Basic)

Texture Coordinates for Cubic Environment Maps

Texture coordinates that index a cubic-environment map aren't simple u, v style coordinates, as used when standard textures are applied. In fact, cubic environment maps don't use texture coordinates at all. In place of a set of texture coordinates, cubic environment maps require a 3-D vector. You must take care to specify a proper vertex format. In addition to telling the system how many sets of texture coordinates your application uses, you must provide information about how many elements are in each set. Microsoft® Direct3D® offers the CONST_D3DFVFTEXTUREFORMATS enumerated type which can be used to set up 3-D vectors. The members of this enumeration can be combined within a flexible vertex format description by using the OR operator.

'
' Create a flexible vertex format descriptor for a vertex that 
' contains a position, normal, and one set of 3-D texture 
' coordinates.
'/
Const D3DFVF CUSTOMVERTEX = (D3DFVF_XYZ Or D3DFVF_NORMAL Or _
                             D3DFVF_TEXCOORDSIZE3_0)
)

In some cases, such as diffuse light mapping, you use the camera-space vertex normal for the vector. In other cases, like specular environment mapping, you use a reflection vector. Because transformed vertex normals are widely understood, the information here concentrates on computing a reflection vector.

Computing a reflection vector on your own requires understanding of the position of each vertex, and of a vector from the viewpoint to that vertex. Direct3D can automatically compute the reflection vectors for your geometry. Using this feature saves memory because you don't need to include texture coordinates for the environment map. It also reduces bandwidth and, in the case of a TnLHAL Device, it can be significantly faster than the computations your application can make on its own. To use this feature, in the texture stage that contains the cubic-environment map, set the D3DTSS_TEXCOORDINDEX texture stage state to a combination of the D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR flag and the index of a texture coordinate set. In some situations, like diffuse light mapping, you might use the D3DTSS_TCI_CAMERASPACENORMAL flag, which causes the system to use the transformed, camera-space, vertex normal as the addressing vector for the texture. The index is only used by the system to determine the wrapping mode for the texture.

The following code example shows how this value is used.

'
' The m_D3DDevice variable is a valid Direct3DDevice8 object.
'
' Automatically generate texture coordinates for stage 2.
' This assumes that stage 2 is assigned a cube map.
' Use the wrap mode from the texture coordinate set at index 1.
'
m_D3DDevice.SetTextureStageState 2, D3DTSS_TEXCOORDINDEX, _
                                 (D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR Or 1) 

When you enable automatic texture coordinate generation, the system uses one of two formulas to compute the reflection vector for each vertex. When the D3DRS_LOCALVIEWER render state is set to TRUE, the formula used is the following:

In this formula, R is the reflection vector being computed, E is the normalized position-to-eye vector, and N is the camera-space vertex normal.

When the D3DRS_LOCALVIEWER render state is set to FALSE, the system uses the following formula.

The R and N elements in this formula are identical to the previous formula. The Nz element is the world-space z of the vertex normal, and I is the vector (0,0,1) of an infinitely distant viewpoint. The system uses the reflection vector from either formula to select and address the appropriate face of the cube map.

Note  In most cases, applications should enable automatic normalization of vertex normals. To do this, set D3DRS_NORMALIZENORMALS to TRUE. If you do not enable this render state, the appearance of the environment map will be drastically different than you might expect.