DirectX SDK

Configuring Bump Mapping Parameters

When your application has created a bump map and set the contents of each pixel, you can prepare for rendering by configuring bump mapping parameters. Bump mapping parameters include setting the required textures and their blending operations, as well as the transformation and luminance controls for the bump map itself.

[C++]

To configure bump mapping parameters in C++:

  1. Set the base texture map (if used), bump map, and environment map textures into texture blending stages.
  2. Set the color and alpha blending operations for each texture stage.
  3. Set the bump map transformation matrix.
  4. Set the luminance scale and offset values (as needed).

The following code sets three textures—the base texture map, the bump map, and a specular environment map—to the appropriate texture blending stages.

    // Set the three textures.
    g_pd3dDevice->SetTexture( 0, lpd3dBaseTexture);
    g_pd3dDevice->SetTexture( 1, lpd3dBumpMap);
    g_pd3dDevice->SetTexture( 2, lpd3dEnvMap);

After setting the textures to their blending stages, the following code prepares the blending operations and arguments for each stage.

    //
    // Set the color operations and arguments to prepare for bump mapping.
    //

    // Stage 0: the base texture
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); 
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 1 );

    // Stage 1: the bump map 
    g_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1 );
    // Use luminance for this example.
    g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAPLUMINANCE);
    g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    g_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );

    // Stage 2: a specular environment map
    g_pd3dDevice->SetTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 0 );
    g_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_ADD );
    g_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    g_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG2, D3DTA_CURRENT );

Once the blending operations and arguments are set, the following code sets the 2×2 bump mapping matrix to the identity matrix, by setting the D3DTSS_BUMPENVMAT00 and D3DTSS_BUMPENVMAT11 texture stage states to 1.0. Setting the matrix to the identity causes the system to use the delta-values in the bump map unmodified, but this is not a requirement.

    // Set the bump mapping matrix. 
    //
    // NOTE:
    //    These calls rely on the following inline shortcut function:
    //       inline DWORD F2DW( FLOAT f ) { return *((DWORD*)&f); }
    g_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT00, F2DW(1.0f) );
    g_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT01, F2DW(0.0f) );
    g_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT10, F2DW(0.0f) );
    g_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT11, F2DW(1.0f) );

If you set the bump mapping operation to include luminance (D3DTOP_BUMPENVMAPLUMINANCE), you must set the luminance controls. The luminance controls configure how the system computes luminance before modulating the color from the texture in the next stage. (For details, see Bump Mapping Formulas.)

    // Set luminance controls. This is only needed when using a bump map 
    // that contains luminance, and when the D3DTOP_BUMPENVMAPLUMINANCE 
    // texture blending operation is being used.
    //
    // NOTE:
    //    These calls rely on the following inline shortcut function:
    //       inline DWORD F2DW( FLOAT f ) { return *((DWORD*)&f); }
    g_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVLSCALE, F2DW(0.5f) );
    g_pd3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVLOFFSET, F2DW(0.0f) );

After your application configures bump mapping parameters, it can render as it normally would, and the rendered polygons will receive bump mapping effects.

[Visual Basic]

To configure bump mapping parameters in Visual Basic:

  1. Set the base texture map (if used), bump map, and environment map textures into texture blending stages.
  2. Set the color and alpha blending operations for each texture stage.
  3. Set the bump map transformation matrix.
  4. Set the luminance scale and offset values (as needed).

The following code sets three textures—the base texture map, the bump map, and a specular environment map—to the appropriate texture blending stages.

    ' Set the three textures.
    Call g_d3dDevice.SetTexture(0, d3dBaseTexture)
    Call g_d3dDevice.SetTexture(1, d3dBumpMap)
    Call g_d3dDevice.SetTexture(2, d3dEnvMap)

After setting the textures to their blending stages, the following code prepares the blending operations and arguments for each stage.

  ' Create the bump map texture.
    
    '
    ' Set the color operations and arguments to prepare for bump mapping.
    '

    ' Stage 0: the base texture
    Call g_d3dDevice.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE)
    Call g_d3dDevice.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE)
    Call g_d3dDevice.SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE)
    Call g_d3dDevice.SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1)
    Call g_d3dDevice.SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE)
    Call g_d3dDevice.SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 1)

    ' Stage 1: the bump map
    Call g_d3dDevice.SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1)
    ' Use luminance for this example.
    Call g_d3dDevice.SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAPLUMINANCE)
    Call g_d3dDevice.SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE)
    Call g_d3dDevice.SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT)

    ' Stage 2: a specular environment map
    Call g_d3dDevice.SetTextureStageState(2, D3DTSS_TEXCOORDINDEX, 0)
    Call g_d3dDevice.SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_ADD)
    Call g_d3dDevice.SetTextureStageState(2, D3DTSS_COLORARG1, D3DTA_TEXTURE)
    Call g_d3dDevice.SetTextureStageState(2, D3DTSS_COLORARG2, D3DTA_CURRENT)

Once the blending operations and arguments are set, the following code sets the 2×2 bump mapping matrix to the identity matrix, by setting the D3DTSS_BUMPENVMAT00 and D3DTSS_BUMPENVMAT11 texture stage states to 1.0. Setting the matrix to the identity causes the system to use the delta-values in the bump map unmodified, but this is not a requirement.

    ' Set the bump mapping matrix.
    Call g_d3dDevice.SetTextureStageStateSingle(1, D3DTSS_BUMPENVMAT00, 1#)
    Call g_d3dDevice.SetTextureStageStateSingle(1, D3DTSS_BUMPENVMAT01, 0#)
    Call g_d3dDevice.SetTextureStageStateSingle(1, D3DTSS_BUMPENVMAT10, 0#)
    Call g_d3dDevice.SetTextureStageStateSingle(1, D3DTSS_BUMPENVMAT11, 1#)

If you set the bump mapping operation to include luminance (D3DTOP_BUMPENVMAPLUMINANCE), you must set the luminance controls. The luminance controls configure how the system computes luminance before modulating the color from the texture in the next stage. (For details, see Bump Mapping Formulas.)

    ' Set luminance controls. This is only needed when using a bump map
    ' that contains luminance, and when the D3DTOP_BUMPENVMAPLUMINANCE
    ' texture blending operation is being used.
    Call g_d3dDevice.SetTextureStageStateSingle(1, D3DTSS_BUMPENVLSCALE, 0.5)
    Call g_d3dDevice.SetTextureStageStateSingle(1, D3DTSS_BUMPENVLOFFSET, 0#)

After your application configures bump mapping parameters, it can render as it normally would, and the rendered polygons will receive bump mapping effects.

Note:  The preceding example shows parameters set for specular environment mapping. When performing diffuse light mapping, applications would set the texture blending operation for the last stage to D3DTOP_MODULATE. For more information, see Diffuse Light Maps.