Normal mapped screen space "diffuse" occlusion, August 2009, by Nicolas Vizerie
===============================================================================

Lucy model if from of the Standford University mesh repository.

About :
==========

An extension of the SSAO technique (Screen Space Ambient Occlusion) popularized by the game Crysis, 
that shows how to add high frequency details to the ambient occlusion term. 
The technique uses 3 color components to store the occlusion over each third of 
the hemisphere for each sampling position (whereas standard SSAO samples over a 'whole' 
hemisphere for each pixel in screen space). Each component is then blurred using a so-called 'bilateral filter'. 
In the end, the normal map is read, and occlusion is computed by using a weighting of the normal with respect with each sampling direction. 
This is very similar to the source shading of half-life 2, but applied to screen space. The executable also has additionnal features such as diffuse bleeding.
The technique can somewhat enhance environments where baking of the lighting is not possible (dynamic or/and huge worlds ...). 
The aim of the sample is to demonstates the visual enhancement that normal maps brings, but speed-wise it can certainly be improved :).
This sample requires DirectX 9.0c and a Shader 3.0 capable GPU. It was tested on a nVidia 8700M GT GPU only. Shaders are included in the archive. 
They do not use the DX effect framework but can be easily adapted.

The occlusion sampling uses a cosine weighted distribution. Morevover, sampling effort is done for those fragement that are near 
the current sampling location (because nearest occluder contributes to a greater extent to the occlusion)
The sampling texture is 8 x 128 (for a 8x8 repeating pattern with 16 rays, a 8x8x16 3D texture could have been used instead and
perhaps is it faster..). A repeating pattern of size 8 gave me the best results. (gaussian blur is over 17 texels)

The random ray distribution was computed as follow :
(Distribution formulae come's from the global illumination compendium : http://www.cs.kuleuven.be/~phil/GI/TotalCompendium.pdf)

Color result;                   // a rgba8 color
float r1 = frand();             // (Note : frand returns a uniform random value in the [0, 1] range)
float r2 = 0.05f + 0.95f * frand();
float fx = cosf(2.f * (float) PI *  r1) * sqrtf(1 - r2));
float fy = sinf(2.f * (float) PI *  r1) * sqrtf(1 - r2));
float fz = sqrtf(r2);           // sqrt added for cosine weighted distribution
float fw = powf(frand(), 3.f);  // -> more sampling importance for the occluders that are close to the sampling location
                                // increasing this value will outline creases
// pack for usage with a RGBA8 format
result.set((uint8) (fx * 127.f + 128.f),
           (uint8) (fy * 127.f + 128.f), 
           (uint8) (fz * 127.f + 128.f), 
           (uint8) (4.f + 251.f * fw));
return result;

Please note that the vector length is encoded as a seprate component to preserve direction accuracy (the target texture is a rgba8 one)




Controls : 
==========
Use arrow keys to move the camera.
Use alt-enter to toggle to fullscreen mode.
Click and hold left button in the scene to look around.                                 

Options :
=========
Please edit scene_view_config.lua to change option such a fullscreen resolution, multi sampling ...

Hardware requirement
====================
A GPU supporting pixel shaders 3.0 is required (GeForce 6 or more)

Version history
===============
1.0 : Initial release

Contact
=======
Nicolas Vizerie : nicovize@club-internet.fr
www.vizerie3d.net

License
=======
This demo is freeware. You may redistribute freely it as long as the whole 
package is left unmodified and complete (including this file). You may not 
sale this software. You may not misrepresent the origin of this software. By using this software, you agree that the author should 
not be liable or responsible for any damages, injuries or liabilities caused 
directly or indirectly from the use of the software, including but not limited
to incidental, consequential or special damages.
The additionnal shader files in the datas folder (ps and vs files) are subject the LGPL license . Please see the header of each file.
If you use those in one of your project, it would be nice to give credit, though.
