CS 351: Assignment #9


Due midnight 25 November 2008 (before Thanksgiving break)

For this assignment you will implement Gouraud shading for polygons with ambient and point light sources. The end result will be a complete 3D z-buffer rendering system with lighting and shading. If you decide to implement Phong shading as an extension, you may skip the Gouraud shading step and just go straight to Phong shading.


To implement shading, you will create two new functions Polygon_shadeFill and Module_shade by first copying and then modifying Polygon_drawFill and Module_draw, respectively. That way you will always have a working system for testing the geometry of your scenes. The definitions for the functions are given in the complete graphics spec.

We'll also finally implement the Color class, which consists of three floats (r, g, b) and is described fully in the graphics spec. The light source objects and functions will complete the new functions.

The major steps are as follows.

  1. Create the light source data type and supporting functions
  2. Create the Vector functions, if you haven't already done so.
  3. Create the Color_calcShading function that calculates the appearance of a surface given the relevant parameters.
  4. Create the new Polygon_shadeFill algorithm and test it on a hand-created polygon to see if the colors interpolate cleanly. Give all the vertices the same z value to avoid problems with perspective project. Calculate the colors using an ambient and a point light source and the shading function defined in the prior step.
  5. Add the functions to the Module class for handling body and surface colors and the surface reflection coefficient. These additions will also involve changes to the Element_init function.
  6. Create the Module_shade function by first copying the Module_draw function and then modifying it to handle the body reflection, surface reflection, surface coefficient cases. These will behave just like the way color is currently handled.
  7. Implement Gouraud shading for polygons, which means you need to calculate shading at the vertices of the polygon. The simplest way to do this is to place the light sources in world coordinates and make the color calculations after multiplying the polygon by the LTM and GTM matrices. Therefore, your polygon case will end up looking something like the following.

    case ObjPolygon:
      Copy the polygon data to a temporary polygon P
      Transform P by the LTM
      Transform P by the GTM
      Calculate the color at each vertex using P
      Transform P by the VTM
      Homogenize the X and Y coordinates
      Call Polygon_shadeFill with P and the vertex colors
  8. Test your system on this simple image of a single cube. Here are the shading outputs for the point light source when the program is run with no arguments and the case where the program gets an argument of 30.
  9. You can also test your system using the cubismShade.c program. The result should be similar to last week, but with variations in shading as the cubes rotate.
  10. Run the example program test.ply using the command:
  11. ./testply starfury.ply 135

    Note that you will need the three files below. The first is the test program, the second goes in your library and enables your system to read in .ply files, and the third is the .ply file itself. Note, you may have to modify the read function depending upon the specific fields in your polygon structure. Also, the program makes a system call to convert, so if your system does not have the ImageMagick package installed, either install it or reduce the size of the image from 2000x2000 to 500x500 using a different image reduction tool.

    Feel free to create your own .ply models. Several of the Stanford models are available in that format. To obtain the models, you can peruse the Stanford 3D Scanning Repository. Also, 3D modeling programs like Blender3D (which is freely downloadable), can save models in the .ply format.

  12. The above images are for testing only, although you are welcome to make animated sequences with the star furies. The one required image for this assignment is to create your own 3D model and place at least three copies of it into a scene with at least one point light source, an ambient light source, and shading. Each person should create their own scene.



All writeups should suggest the format of a lab report. They do not have to be long, but need to provide enough information that I have some idea you knew what you were doing.

  1. Abstract: 200 word description (at most) of what you did and a picture to go along with it.
  2. Description of the task, in your own words. Be brief, but write it as though explaining it to a fellow student not in the course.
  3. Description of how you solved the task, including any key equations or algorithms. You should also include algorithms or descriptions of what you did for any extensions. Include pictures here.
  4. More pictures. Please put a caption on each picture explaining something about it. If you have nothing else, give it a name and indicate whose picture it is.
  5. Summary of what you learned.


Put your code files in your handin folder. Send the prof an email with the URL for your writeup (wiki or standard web page) when it's done. In either case, please edit the assignment handin page on the wiki so there is a link to your assignment.