Contents / Previous / Next


The Position of a Light Source

You can choose whether to have a light source that's treated as though it's located infinitely far away from the scene or one that's nearer to the scene.

The first type is referred to as a directional light source; the effect of an infinite location is that the rays of light can be considered parallel by the time they reach an object. An example of a real-world directional light source is the sun.

The second type is called a positional light source, since its exact position within the scene determines the effect it has on a scene and, specifically, the direction from which the light rays come. A desk lamp is an example of a positional light source. Example:

GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; // direction light
  or 
GLfloat light_position[] = { 1.0, 1.0, 1.0, 1.0 }; // positional light

glLightfv(GL_LIGHT0, GL_POSITION, light_position); // Set position
You supply a vector of four values (x, y, z, w) for the GL_POSITION parameter.
If the last value, w, is zero, the corresponding light source is a directional one, and the (x, y, z) values describe its direction. This direction is transformed by the modelview matrix just as it would be if it described a normal vector. By default, GL_POSITION is (0, 0, 1, 0), which defines a directional light that points along the negative z-axis. (Note that nothing prevents you from creating a directional light with the direction of (0, 0, 0), but such a light won't help you much.)

If the w value is nonzero, the light is positional, and the (x, y, z) values specify the location of the light in homogeneous object coordinates. This location is transformed by the modelview matrix and stored in eye coordinates.
By default, a positional light radiates in all directions, but you can restrict it to producing a cone of illumination by defining the light as a spotlight.