Lab 9: Lighting and Shading
In this lab, we created a data structure for Light objects and developed a model to compute the light color reflected by a surface in a scene. We expanded the Polygon data structure to record a surface normal for each vertex for use in this computation. We implemented Gouraud shading by performing a lighting calculation at every vertex of a polygon and interpolating the colors linearly. We integrated world-coordinatized lighting and shading into our hierarchical modeling system.
Procedure
Our graphics environment continues to follow Prof. Maxwell's Graphics Environment Specification.
To calculate the visible energy reflected from a given point on a surface, one must specify the properties of that surface and of the light sources that illuminate it. We assume that all surfaces are Lambertian, or composed of a translucent binder containing particles of pigment, and we model the following surface properties.
- The surface normal is necessary to compute angles of incidence and reflection.
- The body color is the pigmentation of the surface. Pigment diffuses incident light uniformly, so the intensity of body reflection decreases with greater angle between the surface and light source, but does not vary with angle to the viewer.
- The surface color is the tint of the binder. Surface reflection is glare. It decreases in intensity with greater angle between the surface and the light source. It also decreases in intensity with increasing distance between the viewer and the angle of incidence.
- The surface coefficient specifies the roughness of the surface. A rougher surface has less surface reflection.
Every light source has an RGB color value. Reflected energy can never be more intense in any channel than the value of its source. We define four types of light sources.
- An ambient light is simply a color value. It illuminates all surfaces to the scaled product of the light color and the body color.
- A direct light is situated at infinite distance in a specified direction. Thus, it strikes all surfaces from that direction.
- A point light is situated at a finite location in space. The angle from which it strikes a surface must be computed from that surface's location.
- A spot light is a direct light that diminishes nonlinearly with the angle between it and the normal of the illuminated surface. If that angle is less than the spot angle, the incident light is full intensity; if not, it declines exponentially with the excess angle.
The reflected energy from all sources is summed to compute the color value at the point. In Gouraud shading, this energy is computed at each vertex of a triangle; the energy at each pixel in the triangle is then found by linear interpolation of the inverses of the vertex colors.
To model a shaded scene hierarchically, we define a list of lights; the positions of point lights are specified in worldspace. We define new module elements that change the pen body color, surface color, and surface coefficient just as we did to change the pen color in lab 5. We populate a module with elements in the familiar fashion, taking care to specify surface normals for the polygons. To draw a module, we traverse it, transform every polygon to worldspace, compute the reflected energy at each vertex, and fill it using Gouraud interpolation.
Results
Group Images
Image 1. A demonstration of color interpolation across a polygon.
We generated the following image using Prof. Maxwell's test code. He developed the star fury model in Blender, exported it in the Stanford PLY format, and supplied a library to read in the polygons, colors, and surface normals.
Image 2. Star furies.
Allison's Portfolio Images
Image 1. Allison doesn't care much for captions.
David's Portfolio Images
Image 1. The mobile of Lab 8, with lighting.
Conclusions and Future Work
The basic lighting and shading system developed in this lab is functional, but it lacks some realism. As has been shown, Gouraud shading is susceptible to aliasing because true color values are only computed at polygon vertices. Phong shading, in which surface normals are interpolated and shading is calculated at every pixel, resolves this issue and is a possible future extension. Light sources are not themselves visible in the present modeling system, nor can they be included in modules or subjected to transformations. Future work may remedy this by introducing Light elements into modules, which would then require one rendering pass to world-coordinatize the light sources and another to shade polygons accordingly. Finally, no objects cast shadows under the present lighting model. Shadows can be calculated by rendering the scene from the perspective of every light source, saving the Z-buffer, and applying light from that source only to objects at the depth stored in the buffer.Appendices
Our code is available with AES256 encryption. The passphrase is only available to Dr. Maxwell.
The entire project was pair-programmed. David wrote this writeup.