4.5. Positional and Directional Lights
Positional light sources
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
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
In either case, always specify the light position before specifying any geometry that the light illuminates. |
4.6. Debugging Lights
Several things could cause lighting not to work properly, and there are
4.6.1. Debugging a Blank WindowIf OpenGL appears to have rendered nothing, try the following:
4.6.2. NormalsWhen first learning to use OpenGL, many programmers fail to supply correct normals. This type of error causes odd shading artifacts on lit surfaces. As a good rule of thumb, supply a unit-length normal for every vertex. If the application is specifying unit-length normals, yet odd lighting artifacts persist, it's possible that scale transformations in the model-view matrix are distorting the normals. If necessary, enable GL_RESCALE_NORMAL or GL_NORMALIZE to resolve this issue. 4.6.3. Incorrect Face Culling
If your application enables
GL_CULL_FACE
but has the vertex winding order
4.6.4. Debugging Position and Direction
If your lights appear in the wrong position or
Be sure to specify the GL_POSITION before rendering geometry illuminated by that light. If you use positional lights, first try a directional light instead. This will help verify that you've specified the vertex normals correctly. 4.6.5. Debugging Light Colors
If you're using colored lights and not getting the results you expect, initially use white lights to verify that you've specified the correct material colors. Remember, for example, that a pure-red object lit by a pure-blue light will appear black, because red objects reflect only red light and
4.6.6. Per-Vertex Lighting Artifacts
The fact that OpenGL calculates lighting at each vertex could create lighting artifacts on
Figure 4-3. The sphere on the left has insufficient resolution for OpenGL to render the specular highlight accurately. This is not an issue for the sphere on the right, due to its higher resolution. Light and material parameters are identical for both spheres.
Low-resolution geometry lighting artifacts are
Geometry must have sufficient vertices for per-vertex lighting and Gouraud shading to produce acceptable results. Increasing geometric resolution could adversely affect performance, however, so try to find a good balance between lighting effects and number of vertices. You might also try specifying a smaller GL_SHININESS specular exponent to create a larger specular highlight that affects more vertices.
If
4.6.7. Missing Specular HighlightsSpecular highlights might not appear for several reasons. Check to ensure that the incident light and viewing angles are correct. The specular highlight won't appear unless the viewing angle coincides with the reflection of the incident light. Incorrect surface normals could cause OpenGL to calculate the reflection vector incorrectly, so check that your application is sending correct unit-length normals. Check your setting for the specular material color and specular light color. Remember that only GL_LIGHT0 defaults to a full-intensity specular color; other lights default to zero intensity. Check your GL_SHININESS value. Large values may create specular highlights too small to appear on low-resolution geometry. Temporarily set a much lower value, such as 1.0. If the specular highlight appears, you'll need to increase the resolution of your geometry, as discussed earlier in this chapter, or set GL_SHININESS to a smaller value.
If you're using texture mapping with
GL_MODULATE
texture environment mode, you'll need to use secondary color or
4.6.8. Line and Point Colors
OpenGL is a state machine. When your application enables lighting, OpenGL lights all
|