dhpoware logo
navbar
home products source demos contact us
navbar navbar
box top left corner box top right corner
 
 
OpenGL 2.0 Shading Language Attenuation Demo.

This Windows OpenGL application demonstrates per fragment light attenuation for a single point light source. Two OpenGL Shading Language shaders are provided: a per fragment Blinn-Phong lighting shader, and a per fragment normal mapping shader.

OpenGL attenuates a light source by multiplying the contribution of that light source by an attenuation factor. The attenuation factor is controlled using these three terms for the attenuation factor: GL_CONSTANT_ATTENUATION, GL_LINEAR_ATTENUATION, and GL_QUADRATIC_ATTENUATION.

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.

To learn more about per fragment lighting in OpenGL check out the OpenGL 2.0 Shading Language Fragment Lighting demo.
To learn more about normal mapping in OpenGL check out the OpenGL 2.0 Shading Language Normal Mapping demo.

Note:
1. This is an OpenGL 2.0 demo. This demo will not run on OpenGL 1.x hardware.
2. This demo requires the Visual C++ 2010 Library Runtimes. Download instructions can be found here.

screen shot

zip file 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.

1 April 2008.
gl_font.cpp: Fixed bug in drawTextEnd() method where the vertex buffer was not being unmapped when there's no text to draw. Thanks goes to Ari Ohvo for spotting this bug.

2 February 2008.
Updated solution to Microsoft Visual Studio 2008.

11 January 2008.
main.cpp: Added LIGHT_LAUNCH_ANGLE constant. Modified InitApp() to always set phi to the LIGHT_LAUNCH_ANGLE. Fixed a bug in the RandomAngle() function where srand() was being passed a 64-bit time value that was being truncated to 32-bit. This was causing the same seed value to be passed into srand(). What this means is that the light was always moving in the same direction each time the demo was launched when it should have been moving in a random direction.

25 December 2007.
main.cpp: Updated the runtime_error exception thrown from the InitApp() function to include the shader infoLog when shader loading fails.

blinn_phong_point.glsl: Moved lightRadius uniform variable to the vertex shader. Removed the division in the attenuation formula by simplifying the calculation to 1 - dot(l, l). Added lightDir and viewDir varying variables. These are calculated in the vertex shader. The lightDir calculation includes the lightRadius scaling. Clamped the attenuation factor to prevent negative values.

normal_mapping_point.glsl: Moved lightRadius uniform variable to the vertex shader. Removed the division in the attenuation formula by simplifying the calculation to 1 - dot(l, l). Moved the light vector scaling by the lightRadius to the vertex shader. Clamped the attenuation factor to prevent negative values.

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