Microsoft DirectX 8.1 (Visual Basic)

Using Bump Mapping

Detecting Support for Bump Mapping

A Microsoft® Direct3D® device can perform bump mapping if it supports either the D3DTOP_BUMPENVMAP or D3DTOP_BUMPENVMAPLUMINANCE texture blending operation. Additionally, applications should check the device capabilities to make sure the device supports an appropriate number of blending stages, usually at least three, and exposes at least one bump-mapping pixel format.

The following code example checks device capabilities to detect support for bump mapping in the current device, using the given criteria.

Function SupportsBumpMapping As Bool
{
    Dim d3dCaps As D3DCAPS8

    m_D3DDevice.GetDeviceCaps d3dCaps

    // Does this device support the two bump mapping blend operations?
    If ( d3dCaps.TextureOpsCaps And _
         ( D3DTEXOPCAPS_BUMPENVMAP Or D3DTEXOPCAPS_BUMPENVMAPLUMINANCE ) = 0) Then
         SupportBumpMapping = 0
    End If

    // Does this device support up to three blending stages?
    If d3dCaps.MaxTextureBlendStages < 3 Then
        SupportBumpMapping = 0
    End If

    SupportBumpMapping = 1
}

Creating a Bump Map Texture

You create a bump map texture like any other texture. Your application verifies support for bump mapping and retrieves a valid pixel format, as discussed in Detecting Support for Bump Mapping.

After the surface is created, you can load each pixel with the appropriate delta values, and luminance, if the surface format includes luminance. The values for each pixel component are described in Bump Map Pixel Formats.

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.

  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 example 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.
d3dDevice.SetTexture(0, d3dBaseTexture)
d3dDevice.SetTexture(1, d3dBumpMap)
d3dDevice.SetTexture(2, d3dEnvMap)

After setting the textures to their blending stages, the following code example 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 d3dDevice.SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE)
Call d3dDevice.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE)
Call d3dDevice.SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE)
Call d3dDevice.SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1)
Call d3dDevice.SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE)
Call d3dDevice.SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 1)

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

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

Once the blending operations and arguments are set, the following code example 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 d3dDevice.SetTextureStageStateSingle(1, D3DTSS_BUMPENVMAT00, 1#)
Call d3dDevice.SetTextureStageStateSingle(1, D3DTSS_BUMPENVMAT01, 0#)
Call d3dDevice.SetTextureStageStateSingle(1, D3DTSS_BUMPENVMAT10, 0#)
Call 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 d3dDevice.SetTextureStageStateSingle(1, D3DTSS_BUMPENVLSCALE, 0.5)
Call d3dDevice.SetTextureStageStateSingle(1, D3DTSS_BUMPENVLOFFSET, 0#)

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

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