Lab 9: Shading
Alex Benn and Andrew Frampton
Quick Summary
The first step in creating our shading algorithm was defining a light
source. Each light source stores a light type, a color, and, depending on the
type, one or more of a direction, position, and spotlight angle. We then
coded a number of vector manipulation functions, useful for a number of
things. Next, we implemented body illumination and surface illumination for
each type of light source. Here's how it works:
- For ambient lighting, the intensity is simply the body color times
the light intensity.
- For point and directional lighting, there are three steps:
- Body illumination: first, we take the dot product of the light vector
(the unit vector pointing from the vertex toward the light source) and
the normal vector to determine the intensity of the body illumination;
this value is then multiplied by the body color times the light color
to get the illumination color.
- Surface illumination: first, find the viewer vector, which is the
unit vector pointing from the vertex toward the viewer. Next, find the
unit light vector, described above, and add it to the viewer vector.
The resulting vector can be normalized to get the halfway vector, the
vector which bisects the angle between the light vector and the viewer
vector. This halfway vector is then dotted with the normal, and the
resulting dot product is multiplied by the surface color times the light
color.
- Final color is the sum of the body illumination and the surface
illumination.
- The contributions of all of the light sources are summed; this value is
then clipped to fit in the range of [0.0, 1.0] per channel.
Images

Figure 1: Test of Color Interpolation Algorithm

Figure 2: Output of 3D Shading Test Code
Super-Excellent Update! Although the
sphere-drawing code demonstrated in Figure 3 below was developed after this
lab was due, it demonstrates Gouraud shading quite well.

Figure 3: Gouraud Shading of Sphere
Questions
- The Module_sphere() function was added to the Module code in order to
demonstrate Gouraud shading. To generate the normals of the polygons, we
used the true sphere normal at each point, rather than using the normal to
the current polygon, as is used elsewhere. This generates a smooth gradient
across the sphere which looks quite nice.
Back to Alex and Andrew's other labs