|Direct3D 9 Attenuation Demo.
This Windows Direct3D 9 application demonstrates per pixel light attenuation for a single point light source. Three Direct3D Effect (.fx) files are provided: a per pixel Blinn-Phong lighting effect file, and a per pixel normal mapping effect file, and an ambient lighting effect file. Per pixel light attenuation is implemented in the Blinn-Phong and the normal mapping effect files. The ambient lighting effect file is used by the application to render the light mesh and the circles around the light mesh representing the light's radius.
Fixed function Direct3D attenuates a light source by multiplying the contribution of that light source by an attenuation factor. The attenuation factor is controlled using these three constants: a constant attenuation constant, a linear attenuation constant, and a quadratic attenuation constant. These constants are defined in the D3DLIGHT9 structure as: Attenuation0, Attenuation1, and Attenuation2. It is these three constants that control how the light intensity changes over distance.
Often a more intuitive approach is to define a point light based on a light radius. The center of the sphere defined by this light radius is where the point light is at its brightest. The light intensity drops off to zero as we approach the edges of this sphere.
This demo contains a single room with a point light that is bouncing off the walls, ceiling, and floor. The light radius is adjustable at runtime. The light is initially given a light radius that is large enough to contain the entire room. As the light radius decreases less of the room is illuminated.
This demo was built using the June 2010 update of the Microsoft DirectX SDK. The required end-user runtimes can be downloaded here.
To learn more about per pixel lighting in Direct3D check out the Direct3D 9 HLSL Pixel Lighting Demo.
To learn more about normal mapping in Direct3D check out the Direct3D 9 Normal Mapping Demo.
1. This demo requires shader model 2.0 or higher support.
2. This demo requires the Visual C++ 2010 Library Runtimes. Download instructions can be found here
Download executable, source, and Visual C++ 2010 solution files.
10 July 2010.
Added missing .vcxproj.filters file to the project.
13 June 2010.
Updated solution to Microsoft Visual Studio 2010.
10 April 2008.
blinn_phong.fx: Fixed a bug in the PS_PointLighting() and PS_SpotLighting() functions where the computed light attenuation was not being clamped.
25 March 2008.
Fixed memory leak in CreateNullTexture() function where 'pSurface' was not being released.
2 February 2008.
Updated solution to Microsoft Visual Studio 2008.
11 January 2008.
main.cpp: Fixed bug in RandomAngle() function where srand() was being passed a 64-bit time value truncated to 32-bit. This was causing srand() to be seeded with the same 32-bit value each time the application launches.
4 January 2008.
blinn_phong.fx: Changed vector outputs from the vertex shaders to float3 types. Updated the pixel shaders accordingly. Moved some of the diffuse and specular color calculations to the vertex shaders.
normal_mapping.fx: Changed all vector outputs from the vertex shaders to float3. Added diffuse and specular outputs for the vertex shaders. Fixed bug in the vertex shaders where the worldInverseTransposeMatrix was not being cast to a float3x3. Updated pixel shaders to use the float3 vector outputs from the vertex shaders. Updated pixel shaders to use the diffuse and specular outputs from the vertex shaders.