OpenGL Distilled
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 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:
In either case, always specify the light position before specifying any geometry that the light illuminates. |
Категории