Section 4.5. Positional and Directional Lights


4.5. Positional and Directional Lights

Positional light sources shine light in all directions. Use a positional light source to simulate local light sources, such as a streetlight or an exposed light bulb. Specify the position as a homogenous xyzw coordinate. x, y, and z specify the light position in object coordinates, and the w value must be 1.0.

The following code makes GL_LIGHT1 a positional light at (10, 4, 4) in object coordinates:

 const GLfloat pos[4] = { 10.f, 4.f, -4.f, 1.f }; glLightfv( GL_LIGHT1, GL_POSITION, pos ); 


Directional light sources are always at an infinite distance from your geometry and shine light in a single direction. Use a directional light to simulate nonlocal light sources with effectively parallel light rays, such as the Sun. To specify a directional light, again use a homogenous coordinate, but store a vector pointing toward the light in the x, y, and z values (the light shines in the opposite direction), and set the w coordinate to 0.0.

The following code makes GL_LIGHT1 a directional light at positive x, with light shining along the negative x axis (in object coordinates):

 const GLfloat pos[4] = { 1.f, 0.f, 0.f, 0.f }; glLightfv( GL_LIGHT1, GL_POSITION, pos ); 


To summarize the above:

  • For positional lights, GL_POSITION is an xyz location with a w value of 1.0.

  • For directional lights, GL_POSITION is an xyz vector pointing toward the light with a w value of 0.0. The light direction is (xyz).

For both positional and directional lights, GL_POSITION is an object-coordinate value. When you call glLightfv() to specify the position, OpenGL multiplies the GL_POSITION value by the current model-view matrix to transform it into eye coordinates, where OpenGL performs lighting calculations. Usually, an application needs to manage only two light-positioning scenarios:

  • Headlights The light position stays fixed relative to the camera, regardless of the camera position. Applications commonly create this effect by specifying the light position in eye coordinates. Because the camera is at the origin in eye coordinates, specifying the light position relative to the camera position is simple. The following code places GL_LIGHT1 directly above the camera:

     glMatrixModel( GL_MODELVIEW ); glPushMatrix(); glLoadIdentity(); const GLfloat pos[] = { 0., 1., 0., 1. }; glLightfv( GL_LIGHT1, GL_POSITION, pos ); glPopMatrix(); 

  • Scene lights Architectural applications commonly place positional light sources in the scene to simulate a light fixture or table lamp. To create this effect, the model-view matrix must contain the current view transformation, as well as the light modeling transformation (if any), when you specify the light position. You'll need to specify the light position again if the camera changes position or if the light moves (to simulate repositioning a desk lamp, for example).

In either case, always specify the light position before specifying any geometry that the light illuminates.




OpenGL Distilled
OpenGL Distilled
ISBN: 0321336798
EAN: 2147483647
Year: 2007
Pages: 123
Authors: Paul Martz

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net