Microsoft DirectX 8.0 (C++) |
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.
To configure bump mapping parameters in C++
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.
// Set the color operations and arguments to prepare for // bump mapping. // Stage 0: the base texture. d3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE ); d3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); d3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE ); d3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); d3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); d3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 1 ); // Stage 1: the bump map - Use luminance for this example. d3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 1 ); d3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_BUMPENVMAPLUMINANCE); d3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE ); d3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT ); // Stage 2: a specular environment map. d3dDevice->SetTextureStageState( 2, D3DTSS_TEXCOORDINDEX, 0 ); d3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_ADD ); d3dDevice->SetTextureStageState( 2, D3DTSS_COLORARG1, D3DTA_TEXTURE ); 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. // // NOTE // These calls rely on the following inline shortcut function: // inline DWORD F2DW( FLOAT f ) { return *((DWORD*)&f); } d3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT00, F2DW(1.0f) ); d3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT01, F2DW(0.0f) ); d3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVMAT10, F2DW(0.0f) ); d3dDevice->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); } d3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVLSCALE, F2DW(0.5f) ); d3dDevice->SetTextureStageState( 1, D3DTSS_BUMPENVLOFFSET, F2DW(0.0f) );
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.