dhpoware logo
home products source demos contact us
navbar navbar
box top left corner box top right corner
Direct3D 9 HLSL Pixel Lighting Demo.

This is the second demo in the Direct3D 9 HLSL (High Level Shading Language) demo series. This demo implements per-pixel lighting.

The purpose of this demo is to implement per-pixel lighting by evaluating the Direct3D fixed function lighting model per-pixel rather than per-vertex as demonstrated in the previous demo in the series. The shader presented in this demo contains fundamental techniques that are the basis for the more advanced per-pixel shaders such as normal mapping.

A single D3DX Effect file (.fx) is provided containing 3 techniques. There is a technique for each of the three fixed function lighting models: per-pixel directional lighting, per-pixel point lighting, and per-pixel spotlight lighting.

The spotlight lighting model used in this demo is a simplified version of the fixed function Direct3D spot lighting model. The fixed function model is more complex to implement and consumes more instruction slots in the pixel shader. We use a popular simplification that seems to produce a more visually appealing result compared to a direct implementation of the fixed function model.

This demo consists of a single textured cube centered at the world origin. The light source is fixed and located directly in front of the cube where the virtual camera is located in the scene. The mouse can be used to rotate the cube and to zoom in and out of the scene. Note that the cube has a very low polygon count to emphasize the fact that realistic per-pixel lighting is independent of the geometry's level of tessellation.

This demo was built using the June 2010 update of the Microsoft DirectX SDK. The required end-user runtimes can be downloaded here.

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.


zipfile Download executable, source, and Visual C++ 2010 solution files.

Change History:

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.

4 January 2008.
blinn_phong.fx: Changed vector outputs from the vertex shaders to float3.

main.cpp: Fixed dollying so that moving the mouse middle button or wheel towards the user will move the cube closer to the user. Moving the mouse and wheel away from the user will move the cube away from the user.

26 December 2007.
Renamed existing pixel_lighting.fx file to blinn_phong.fx.
Renamed existing decal_map.dds file to color_map.dds.

blinn_phong.fx: Updated comments at the top of the file. Removed attenuation member of the Light structure. Added light radius member to the Light structure. Replaced existing attenuation model with a simpler one based on a light radius. Uses the formula: attenuation factor = 1 - ((distance * distance)/(radius * radius)). Fixed a bug in the way the directional lighting vertex shader calculates the half angle vector. Reordered vertex layout.

main.cpp: Refactored code to match the structure of the more recent Direct3D demos. Updated the Light structure to match the changes made to the blinn_phong.fx shader. This is primarily the replacement of the attenuation member with the radius member. With this update we've replaced the old attenuation model that uses the constant, linear, and quadratic attenuation terms to control the light attenuation with a much simpler and more intuitive light radius model.

2 January 2007.
main.cpp: Fixed a possible resource leak in LoadShader() function where the ID3DXBuffer pointer was not being released. The executable has been rebuilt using Visual C++ 2005 with Visual Studio 2005 Service Pack 1 applied.

1 January 2007.
main.cpp: Added event handler for the WM_SIZE message. Fixes an issue with the aspect ratio being incorrect after a full screen toggle.

box bottom left corner content box box bottom right corner
logo logo