Partager via


Light Mapping with Textures

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

For an application to realistically render a 3-D scene, it must take into account the effect that light sources have on the appearance of the scene. Although techniques such as flat and Gouraud shading are valuable tools in this respect, they can be insufficient for your needs. Microsoft® Direct3D® Mobile supports multipass and multiple texture blending. These capabilities enable your application to render scenes with a more realistic appearance than scenes rendered with shading techniques alone. By applying one or more light maps, your application can map areas of light and shadow onto its primitives.

A light map is a texture or group of textures that contains information about lighting in a 3-D scene. You can store the lighting information in the alpha values of the light map, in the color values, or in both.

If you implement light mapping using multipass texture blending, your application should render the light map onto its primitives on the first pass. It should use a second pass to render the base texture. The exception to this is specular light mapping. In that case, render the base texture first; then add the light map.

Multiple texture blending enables your application to render the light map and the base texture in one pass. If the user's hardware provides for multiple texture blending, your application should take advantage of it when performing light mapping. This significantly improves your application's performance.

Using light maps, a Direct3D Mobile application can achieve a variety of lighting effects when it renders primitives. It can map not only monochrome and colored lights in a scene, but it can also add details such as specular highlights and diffuse lighting.

Monochrome Light Maps

Some older 3-D accelerator boards do not support texture blending using the alpha value of the destination pixel. For more information, see Alpha Texture Blending. These adapters also generally do not support multiple texture blending. If your application is running on an adapter such as this, it can use multipass texture blending to perform monochrome light mapping.

To perform monochrome light mapping, an application stores the lighting information in the alpha data of its light map textures. The application uses the texture filtering capabilities of Direct3D Mobile to perform a mapping from each pixel in the primitive's image to a corresponding texel in the light map. It sets the source blending factor to the alpha value of the corresponding texel.

The following code example shows how an application can use a texture as a monochrome light map.

// This example assumes that d3dmDevice is a valid pointer to an
// IDirect3DMobileDevice interface and that lptexLightMap is a valid
// pointer to a texture that contains monochrome light map data.

// Set the light map texture as the current texture.
d3dmDevice->SetTexture(0, lptexLightMap);

// Set the color operation.
d3dmDevice->SetTextureStageState(0, D3DMTSS_COLOROP, D3DMTOP_SELECTARG1);

// Set argument 1 to the color operation.
d3dmDevice->SetTextureStageState(0, D3DMTSS_COLORARG1,
       D3DMTA_TEXTURE | D3DMTA_ALPHAREPLICATE);

Because display adapters that do not support destination alpha blending usually do not support multiple texture blending, this example sets the light map as the first texture, which is available on all 3-D accelerator cards. The sample code sets the color operation for the texture's blending stage to blend the texture data with the primitive's existing color. It then selects the first texture and the primitive's existing color as the input data.

Color Light Maps

Your application will usually render 3-D scenes more realistically if it uses colored light maps. A colored light map uses the RGB data in the light map for its lighting information.

The following code example shows light mapping with RGB color data.

// This example assumes that d3dmDevice is a valid pointer to an
// IDirect3DMobileDevice interface and that lptexLightMap is a valid
// pointer to a texture that contains RGB light map data.
 
// Set the light map texture as the first texture.
d3dmDevice->SetTexture(0, lptexLightMap);

d3dmDevice->SetTextureStageState( 0,D3DMTSS_COLOROP, D3DMTOP_MODULATE );
d3dmDevice->SetTextureStageState( 0,D3DMTSS_COLORARG1, D3DMTA_TEXTURE );
d3dmDevice->SetTextureStageState( 0,D3DMTSS_COLORARG2, D3DMTA_DIFFUSE );

This example sets the light map as the first texture. It then sets the state of the first blending stage to modulate the incoming texture data. It uses the first texture and the current color of the primitive as the arguments to the modulate operation.

Specular Light Maps

When illuminated by a light source, shiny objects — those that use highly reflective materials — receive specular highlights. In some cases, the specular highlights produced by the lighting module is not accurate. To produce a more appealing highlight, Direct3D Mobile applications can 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 example shows this process.

// This example assumes that d3dmDevice is a valid pointer to an
// IDirect3DMobileDevice 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.
d3dmDevice->SetTexture(0, lptexBaseTexture );

// Set the base texture operation and arguments.
d3dmDevice->SetTextureStageState(0, D3DMTSS_COLOROP, D3DMTOP_MODULATE );
d3dmDevice->SetTextureStageState(0, D3DMTSS_COLORARG1, D3DMTA_TEXTURE );
d3dmDevice->SetTextureStageState(0, D3DMTSS_COLORARG2, D3DMTA_DIFFUSE );

// Set the specular light map.
d3dmDevice->SetTexture(1, lptexSpecLightMap);

// Set the specular light map operation and args.
d3dmDevice->SetTextureStageState(1, D3DMTSS_COLOROP, D3DMTOP_ADD );
d3dmDevice->SetTextureStageState(1, D3DMTSS_COLORARG1, D3DMTA_TEXTURE );
d3dmDevice->SetTextureStageState(1, D3DMTSS_COLORARG2, D3DMTA_CURRENT );
 
// Set the RGB light map.
d3dmDevice->SetTexture(2, lptexLightMap);
 
// Set the RGB light map operation and arguments.
d3dmDevice->SetTextureStageState(2,D3DMTSS_COLOROP, D3DMTOP_MODULATE);
d3dmDevice->SetTextureStageState(2,D3DMTSS_COLORARG1, D3DMTA_TEXTURE );
d3dmDevice->SetTextureStageState(2,D3DMTSS_COLORARG2, D3DMTA_CURRENT );

Diffuse Light Maps

When illuminated by a light source, matte surfaces display diffuse light reflection. The brightness of diffuse light depends on the distance from the light source and the angle between the surface normal and the light source direction vector. The diffuse lighting effects simulated by lighting calculations produce only general effects.

Your application can simulate more complex diffuse lighting with texture light maps. Do this by adding the diffuse light map to the base texture, as shown in the following code example.

// This example assumes that d3dmDevice is a valid pointer to an
// IDirect3DMobileDevice interface.
// lptexBaseTexture is a valid pointer to a texture.
// lptexDiffuseLightMap is a valid pointer to a texture that contains 
// RGB diffuse light map data.
 
// Set the base texture.
d3dmDevice->SetTexture(0,lptexBaseTexture );
 
// Set the base texture operation and args.
d3dmDevice->SetTextureStageState(0,D3DMTSS_COLOROP,
       D3DMTOP_MODULATE );
d3dmDevice->SetTextureStageState(0,D3DMTSS_COLORARG1, D3DMTA_TEXTURE );
d3dmDevice->SetTextureStageState(0,D3DMTSS_COLORARG2, D3DMTA_DIFFUSE );
 
// Set the diffuse light map.
d3dmDevice->SetTexture(1,lptexDiffuseLightMap );
 
// Set the blend stage.
d3dmDevice->SetTextureStageState(1, D3DMTSS_COLOROP, D3DMTOP_MODULATE );
d3dmDevice->SetTextureStageState(1, D3DMTSS_COLORARG1, D3DMTA_TEXTURE );
d3dmDevice->SetTextureStageState(1, D3DMTSS_COLORARG2, D3DMTA_CURRENT );

See Also

Concepts

Texture Blending