Special Effects (Direct3D 9)

This topic contains examples of special effects accomplished with texture coordinate processing.

Animating textures (by translation or rotation) on a model

  • Define 2D texture coordinates in your vertex format.

    // Use a single texture, with 2D texture coordinates. This
    // bit-pattern should be expanded to include position, normal, 
    // and color information as needed.
    DWORD dwFVFTex = D3FVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0);
    
  • Configure the rasterizer to use 2D texture coordinates.

    SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
    
  • Define and set an appropriate texture coordinate transformation matrix.

    // M is a D3DMATRIX being set to translate texture
    // coordinates in the U and V directions.
    //      1   0  0  0
    //      0   1  0  0
    //      du dv  1  0 (du and dv change each frame)
    //      0   0  0  1
    
    D3DMATRIX M = D3DXMatrixIdentity(); // declared in d3dutil.h
    M._31 = du; 
    M._32 = dv; 
    

Creating texture coordinates as a linear function of a model's camera-space position

  • Use the D3DTSS_TCI_CAMERASPACEPOSITION flag to instruct the system to pass the vertex position, in camera space, as input to a texture transformation.

    // The input vertices have no texture coordinates, saving 
    // bandwidth. Three texture coordinates are generated by 
    // using vertex position in camera space (x, y, z).
    SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);
    
  • Instruct the rasterizer to expect 2D texture coordinates.

    // Two output coordinates are used.
    SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
    
  • Define and set a matrix that applies a linear function.

    // Generate texture coordinates as linear functions 
    // so that:
    //      u = Ux*x + Uy*y + Uz*z + Uw 
    //      v = Vx*x + Vy*y + Vz*z + Vw
    // The matrix M for this case is:
    //      Ux  Vx  0  0 
    //      Uy  Vy  0  0 
    //      Uz  Vz  0  0 
    //      Uw  Vw  0  0 
    
    SetTransform(D3DTS_TEXTURE0, &M);
    

Performing environment mapping with a cubic environment map

  • Use the D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR flag to instruct the system to automatically generate texture coordinates as reflection vectors for cubic mapping.

    SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR);
    
  • Instruct the rasterizer to expect texture coordinates with three elements.

    SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3);
    

Performing projective texturing

  • Use the D3DTSS_TCI_CAMERASPACEPOSITION flag to instruct the system to pass the vertex position as input to a texture transformation matrix.

    SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION);
    
  • Create and apply the texture projection matrix. This is beyond the scope of this documentation, and is the topic of several industry articles.

  • Instruct the rasterizer to expect three-element projected texture coordinates.

    // Two output coordinates are used.
    
    SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTF_PROJECTED | D3DTTFF_COUNT3);
    

Texture Coordinate Processing