Lab 8. Illumination and Shading
M. Stone and A. Pshenichkin. 12.26.04.
(This lab is much belated.)
Vertices
Illumination requires a significant amount of additional object data, specifically surface normals. While simpler algorithms such as flat shading assume that objects have uniform surface normals, Phong shading attaches a specific surface normal to each polygon vertex and interpolates to find the intervening values. This means that the normals for a polygon correspond more closely to the object it represents (such as part of as sphere, for example) rather than the polygon itself. Thus, since we cannot use the polygon vertices alone to calculate normals, we must store their values in our structure. To this end, the Vertex and VertexBuffer classes were created. Each Vertex stores vectors representing not only the position of a point, but also the color values and a normal vector for that location. In the parser, each vertex is expressed as a point, a normal, and, optionally, a color (which can be used to, say, make a gradient): v { p { px py pz } n { nx ny nz } c { cr cg cb ca } }. Thus, the polygons in our model files now look like this:
Model cube { Polygon front-face { vertex-list { v { p { 0 0 0 } n { 0 0 -1 } } v { p { 1 0 0 } n { 0 0 -1 } } v { p { 1 1 0 } n { 0 0 -1 } } v { p { 0 1 0 } n { 0 0 -1 } } } } ... }
EdgeRecords now store Vertex data, with a vertexIntersect and a dVertexPerScan, which govern the interpolation of colors and normals in addition to edges.
Illumination
Reflection is a complex phenomenon based on the physical and chemical structure of specific materials in addition to the general physical laws governing light.
Body reflection occurs when an object absorbs light and re-emits it at specific wavelengths in random directions. The energy of this diffuse colored light coming from a body is proportional to the energy it takes in, which varies with the angle of incidence. This is called the Lambertian reflection, and can be calculated using the equation
where the L and N vectors are the light source direction and surface normal, respectively, L is the light source intensity, and k is a diffuse reflection coefficient.
We also have to consider surface reflection, however. This is light bouncing off of the surface rather than being absorbed and emitted. The color of illumination is generally unaffected by the material, but there is another factor: the reflection depends upon the viewing angle as well as the angle of incidence:
If V is the surface normal and R is the perfect specular direction, V * R is the specular reflection vector. The n above denotes the specularity coefficient.
Integration: Shaders
The program reads in and parses a list of lights, which can be point light sources or directional lights. The program then takes a list of shaders, with a shading algorithm and a list of applicable light sources specified for each shader. Each shader consists of a set of child shaders and a list of algorithms to run after executing the child shaders. So, for example, we could add a bump shader that gets run before the diffuse and specularity shaders. This framework, therefore, enables the use of arbitrary shading algorithms. Note that lights are separate from all the objects and specified in world coordinates.
Sample Images
Unfortunately, the program currently generates a black screen.
Equation images from Professor Maxwell's pages at http://palantir.swarthmore.edu/maxwell/classes/e26/F04/labs/lab08/.
M. Stone and A. Pshenichkin.