DirectX SDK |
When illuminated by a light source, "shiny" objects (those that use highly reflective materials) will receive specular highlights. In some cases, the specular highlights produced by the lighting module will not be accurate. To produce a more appealing highlight, many Direct3D applications apply specular light maps to primitives.
To perform specular light mapping, first modulate the specular light map with the primitive's existing texture. Then add the monochrome or RGB light map.
The following code fragment illustrates this process in C++:
// This example assumes that lpD3DDev is a valid pointer to an // IDirect3DDevice7 interface. // lptexBaseTexture is a valid pointer to a texture. // lptexSpecLightMap is a valid pointer to a texture that contains RGB // specular light map data. // lptexLightMap is a valid pointer to a texture that contains RGB // light map data. // Set the base texture. lpD3DDev->SetTexture(0,lptexBaseTexture ); // Set the base texture operation and args lpD3DDev->SetTextureStageState(0,D3DTSS_COLOROP, D3DTOP_MODULATE ); lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG1, D3DTA_TEXTURE ); lpD3DDev->SetTextureStageState(0,D3DTSS_COLORARG2, D3DTA_DIFFUSE ); // Set the specular light map. lpD3DDev->SetTexture(1,lptexSpecLightMap ); // Set the specular light map operation and args lpD3DDev->SetTextureStageState(1,D3DTSS_COLOROP, D3DTOP_ADD ); lpD3DDev->SetTextureStageState(1,D3DTSS_COLORARG1, D3DTA_TEXTURE ); lpD3DDev->SetTextureStageState(1,D3DTSS_COLORARG2, D3DTA_CURRENT ); // Set the RGB light map. lpD3DDev->SetTexture(2,lptexLightMap); // Set the RGB light map operation and args lpD3DDev->SetTextureStageState(2,D3DTSS_COLOROP, D3DTOP_MODULATE); lpD3DDev->SetTextureStageState(2,D3DTSS_COLORARG1, D3DTA_TEXTURE ); lpD3DDev->SetTextureStageState(2,D3DTSS_COLORARG2, D3DTA_CURRENT );
The following Visual Basic code fragment illustrates this process:
' This example assumes that d3dDev is a valid reference to an ' Direct3DDevice7 object. ' texBaseTexture is a valid reference to a texture. ' texSpecLightMap is a valid reference to a texture that contains RGB ' specular light map data. ' texLightMap is a valid reference to a texture that contains RGB ' light map data. ' Set the base texture. Call d3dDev.SetTexture(0, texBaseTexture) ' Set the base texture operation and args Call d3dDev.SetTextureStageState(0, D3DTSS_COLOROP, _ D3DTOP_MODULATE) Call d3dDev.SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE) Call d3dDev.SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE) ' Set the specular light map. Call d3dDev.SetTexture(1, texSpecLightMap) ' Set the specular light map operation and args Call d3dDev.SetTextureStageState(1, D3DTSS_COLOROP, _ D3DTOP_ADD) Call d3dDev.SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE) Call d3dDev.SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT) ' Set the RGB light map. Call d3dDev.SetTexture(2, texLightMap) ' Set the RGB light map operation and args Call d3dDev.SetTextureStageState(2, D3DTSS_COLOROP, D3DTOP_MODULATE) Call d3dDev.SetTextureStageState(2, D3DTSS_COLORARG1, D3DTA_TEXTURE) Call d3dDev.SetTextureStageState(2, D3DTSS_COLORARG2, D3DTA_CURRENT)