Lab Assignment 5 -- Demo and writeup due Thursday, May 4
CS 5721, Spring Semester, 2006
20 Points

Topic: Rendering: Simple ray-tracing and the Z-buffer algorithm

The assignment: Consists of two parts: In Part A, you will add code to the lab5a.c program to do simple (non-recursive) ray-tracing, or more accurately "ray-casting", for a scene with spheres in it. In Part B, you will add code to the lab5b.c program to implement the Z-buffer algorithm (for the same scene).

Part A: Ray-tracing

  1. Copy all the files in the lab5a subdirectory of the class account to your account. Then make and run the lab5a program. It will display a black window (any keystroke or button press will exit the program).
  2. First, add another sphere to the scene or more than one, if you like to the array of sphere data ( SphereObject S[] ) near the top of the program.
  3. Second modify the program where the extensive comments are after the computation of the discriminant within the nested loops in the main program. There are two things to do:
    (a) Test the discriminant for being >= 0.0
       if so, find the solution, t_solution, that has the smallest value
       (hint: use "-" in front of the sqrt) 
         if that solution is smaller than the smallest value of t so far,
           replace it with t_solution _and_ 
           update the number of the closest sphere to the value k
       If the discriminant < 0.0, there is no solution, so
         do nothing
    
    (b) After testing all the spheres for intersection,
      set the color of the pixel at (i,j) as follows:
       if there was a closest sphere, i.e. if the "closest" variable is >= 0,
          set the color to the diffuse color components (O_dR, O_dG, O_dB)
    	of that sphere, (in the next part, we will replace this with values
            from illumination equations)
       otherwise 
          set the color to black: SRGP_setColor( SRGP_BLACK ) ;
    
    At the end of this part, you should just see "flat", constantly shaded circles.
    Hint: Get this part running before attempting the next part.
  4. Finally, shade the visible points. Your task for this part is to replace the colors on the spheres in the previous part with colors determined by the illumination equations 16.9 (page 726) of the textbook that take into account ambient and diffuse lighting (actually, the equations only show the red component, but green and blue equations are the same except that each subscript "R" is replaced by "G" and "B" respectively). The intensities from the three illumination equations, I_R, I_G, and I_B, determine the pixel's color: SetColor(I_R, I_G, I_B);

    Assumptions: (1) there is no light-source attenuation -- i.e. f_att = 1.0, and
    (2) the point light source is "at infinity", i.e. L is constant.

Hints: To find the surface normal vector, N, you need the coordinates of the intersection, which are given by: x = x_0 + t*del_x, y = y_0 + t*del_y, and z = z_0 + t*del_z, then the normal is given by: N = ( (x-a)/r, (y-b)/r, (z-c)/r ) where a, b, c, and r are the center and radius of the closest sphere.

It is reasonable that the light source(s) should not illuminate the back sides of the spheres, so that the dot product of N and L should be clamped to 0.0 if it is negative (this sets the diffuse term to 0). I had neglected to do this when I first tried to write a solution for the lab, so that the back sides of the spheres were darker than they should have been. The sample solution, lab5asample, is correctly shaded (but it only has 3 spheres).

Suggestion: Once you get your program running correctly, you may want to try setting the SRGP window to a larger size to see a bigger view of your spheres.

Part A extra credit features:

  1. (1 point) Add additional light sources.
  2. (1 point) Add specular reflection terms (must do Item 1 first).
  3. (1 point) Read in all the data for the scene from a data file.
  4. (3 points) Allow for convex polyhedra in addition to spheres.
  5. (4 points) Do recursive ray tracing, showing reflection, transparency, and shadows (see Section 16.12 of the textbook).

Part B: The Z-buffer algorithm

  1. Copy all the files in the lab5b subdirectory of the class account to your account. Then make and run the lab5b program. It will display a white window (any keystroke or button press will exit the program).
  2. First, add another sphere to the scene or more than one, if you like to the array of sphere data ( SphereObject S[] ) near the top of the program -- but be careful to make sure that they are completely visible in the window.
  3. Initialize the Z-buffer to some large negative value as indicated in the comments after the call to SRGP_begin(). In this lab, we will just use the window rather than the frame buffer (which we can't access directly); so you don't have to worry about intializing the frame buffer (but you can do it indirectly if you want with a call to SRGP_rectangleCoord() for instance).
  4. Compute the z-values. Inside the second "if" that guards against taking the square root of a negative number, compute the "closest" (i.e. most positive) z value on the sphere from the equation:
    (x-a)^2 + (y-b)^2 + (z-c)^2 = r^2. Here x and y are the world coordinate values corresponding to the integer SRGP window coordinates i and j.
    Hint: see comments in lab5b.c
  5. Inside the last "if" (the innermost "if" in the Z-buffer pseudocode in the texts), update the Z-buffer and compute the color intensities       I_R, I_G, and I_B at x, y, z (which can be exactly the same as in Part A), then set the color and draw a point at (i,j)
Notes:
  1. It is necessary to guard against taking the square root of negative quantities which occur when solving quadratic equations -- I have taken care of this for you!
  2. There is a sample program, lab5bsample, in the lab5b directory that shows the final result (but with only 3 spheres).
Suggestions:
  1. Once you get your program running correctly, you may want to try setting the SRGP window to a larger size to see a bigger view of your spheres.
  2. Put all the spheres in z-order (by their centers) from front to back.
  3. Do timings of program runs before and after 2. and compare with Lab 5a. Using a larger window (as in 1.) or more spheres may emphasize differences.

Part B extra credit features:

  1. (1 point) Add additional light sources.
  2. (1 point) Add specular reflection terms (must do Item 1. first).
  3. (1 point) Read in all the data for the scene from a data file.
  4. (1 point) Allow spheres to overlap the window, so that you may see only parts of some spheres. This should include extent-checking to "trivially reject" any sphere that is completely outside the window.
  5. (3 points) Allow for convex polyhedra in addition to spheres.

Note: You can earn 15 points for doing just one part, A or B.

What to turn in or demo:
Since this lab has visual aspects that can most easily be observed and explained in a live demo, you should do a demonstration of your program. Turn in or do the following items from the Computer Science Lab Report Format:
1. (1 point) The Basic Information (your name, class, section, TA's name, assignment number, and date) can be on a separate cover sheet or as (highlighted) comments at the top of your main program file.
2. (2 point) The Problem Statement is a brief description of The assignment: above. Note: the problem statement can (and should) be in a comment at the top of your main program files.
9. (If done.)
11. (5 points) Include the Program Listing(s) and data files (if used). These listings should show good style, be appropriately commented, have the new parts highlighted that you added to the original lab5a.c and lab5b.c code, and include handwritten explanations if it helps the reader's understanding of the code.
13. & 14. (12 points) Do "demos" of your programs. Also, do demos of any extra credit items.
15. Include a description of any Known Bugs if needed. You may be able to gain back lost points by a careful analysis of what went wrong and possible fixes.
16. This is optional (and no points usually), but it is useful to think about Possible Improvements.
17. Comments on the lab assignment are also optional (and again, no points usually), but appreciated!

Important Note: You may discuss algorithms with other students, but the program and lab report should be your own work.


Page URL: http://www.d.umn.edu /~ddunham/cs5721s06/assignments/lab5/assignment.html
Page Author: Doug Dunham
Last Modified: Wednesday, 26-Apr-2006 19:36:23 CDT
Comments to: ddunham@d.umn.edu