CodePlexProject Hosting for Open Source Software

A scene without indirect illumination looks extremely unrealistic. Objects in shadow could look completely black, something unlikely in every scene.

Applying a uniform color that does not depend of the local lights, geometry or camera position is the simplest model of indirect illumination. This simple solution improves significantly the scene realism at a really small cost. However, even if the ambient light is by definition low frequency diffuse information, a uniform color is not enough to produce a photo-realistic look.

A better alternative is image based ambient illumination. This method samples an ambient map that contains the diffuse illumination information of far light sources. To access this map the surface normals are used instead of the reflection vector, for this reason there is no need to use any local illumination parameter. To obtain the correct ambient contribution the map should be sampled in all directions of the hemisphere centered on the surface normal and modulated using the Lambert cosine law.

There are a number of alternatives to store ambient maps that varies on efficient, practicality and efficiency. The most used mediums are cube maps, spherical harmonics and Valve's ambient cubes. Other methods like wavelets (better for a little higher frequency information) and zonal harmonics (easy to rotate) are other good alternatives but that are normally used in other global illumination techniques.

Spherical harmonics is a mathematical system analog to the Fourier transform, but it's defined in the sphere surface. Intuitively, given a direction it returns a value that is an approximation of the original spherical function. The approximation is more accurate if more base functions are used, but given the properties of the bases used and the fact that the ambient light only requires low frequency information, then with just few bases it is possible to reconstruct an approximation of the original map.

Second order spherical harmonics are used in the engine and that means that only 9 base functions per color channel are needed. The 27 base functions (9 per channel) are stored directly in the shader algorithm, only 27 coefficients need to be sent to the GPU (and store them in registers) and these coefficients could be calculated automatically by the engine itself if a non-convoluted cube map is supplied.

The algorithm to sample these maps is very simple:

float3 normalSquared = worldNormal * worldNormal; int3 isNegative = ( worldNormal < 0.0 ); float3 linearColor; linearColor = normalSquared.x * cAmbientCube[isNegative.x] + normalSquared.y * cAmbientCube[isNegative.y+2] + normalSquared.z * cAmbientCube[isNegative.z+4];

They are less accurate than second order spherical harmonics and they lack of some useful properties but still they are simple, fast and useful for very low frequency information.

Last edited Feb 5, 2013 at 9:39 PM by jischneider, version 30