SHIrradianceEnvMap Sample

Description
This sample shows how to use spherical harmonics to handle any number of lights in a single pass with a simple shader.



Path
Source: DX90SDK\Samples\C++\Direct3D\SHIrradianceEnvMap
Executable: DX90SDK\Samples\C++\Direct3D\Bin\SHIrradianceEnvMap.exe


Why is this sample interesting?
Irradiance Environment Maps are a technique for lighting diffuse objects in an arbitrary lighting environment without shadows in a single pass. This technique was first presented at SIGGRAPH 2001 by Ravi Ramamoorthi and Pat Hanrahan in the paper "An Efficient Representation for Irradiance Environment Maps". Using spherical harmonics (SH), an arbitrary lighting environment can almost be perfectly reconstructed with only 9 coefficients. Because of this, the shader complexity is independent of the complexity of the lighting environment. Also note that unlike PRT this technique does not require any precomputation.

How does this sample work?
The sample loads the mesh, the texture for the mesh, and an effect as usual. The interesting part of the sample is in CMyD3DApplication::RenderMeshWithIrradianceEnvMap(). This function calls D3DXSHEvalDirectionalLight() for each directional light to get 9 spherical harmonics coefficients per channel per light. It then sums these coefficients to a single set of 9 coefficients per channel using D3DXSHAdd(). Note that instead of D3DXSHEvalDirectionalLight(), it's possible to call any of the D3DXSHEval*() functions or D3DXSHProjectCubeMap(). This simple sample just allows up to 3 directional lights.

To compute the exit radiance for a given normal, you need to evaluate the lighting convolved with the clamped cosine kernel (represented in SH) in the direction of the normal. The coefficients of the SH basis functions are combined into shader constants with the convolution coefficients in a form that is efficient to evaluate in the shader "IrradianceEnvironmentMapVS". This is described more fully in the article by Peter-Pike Sloan titled "Efficient Evaluation of Irradiance Environment Maps" in the book "ShaderX 2 - Shader Programming Tips and Tricks" by Wolfgang F. Engel. If the lighting environment is grayscale this shader could be simplified.

Typical scenarios for this technique
A typical engine using this technique might do some number of lights normally with shadows, phong, etc but would use this technique to render any additional lights in a single pass. Care has to be taken so popping doesn't occur as lights are moved out of the diffuse only SH set and into the set that models shadows/more complex materials.



Copyright (c) Microsoft Corporation. All rights reserved.