Computer Graphics (CS4300) 2011S: Assignment 5

DUE: 12pm, Wed Mar 30

Worth: 5% of your final grade

Goals

Assignment

This assignment builds on the final program you built for Part III of HW4 by implementing two methods to fix incorrect rendering when one thing is behind another (occlusion). For example, in the left image below, the duck is rendered with shading and backface cull, but it still does not appear correct. The problem is that some of the triangles in the tail are not backfacing (and so are not culled), but they happen to be drawn after the triangles in the head. Unless you implemented extra credit, your code from HW4 probably has this problem. The image on the right shows correct rendering.

The first method you will explore is depth sorting, also known as the painter’s algorithm, because the idea is to “paint” all triangles in order from back to front. This method works reasonably well but still has some problems. The second method, z-buffering, works much better. The idea there is to store a second frame buffer that does not hold the RGB color of the last-drawn pixel, but rather the interpolated (depth) coordinate of the last pixel that was drawn there. Each time you draw a pixel you now check the current value in the z buffer; if the pixel you are about to draw is further away from the viewer, you don’t draw it.

Policy on using HW4 solution code for this assignment: You will likely find it easiest to solve this assignment by extending the code you developed for HW4. If you are not satisfied with your solution to HW4, then you have a few options:

  1. you can contact the course staff for help to fix your code
  2. you can write an entirely new program
  3. you may base your solution to this homework on someone else’s solution to HW4, provided that
    1. that person did not already implement either depth sorting or z-buffering in the code you use
    2. you have permission from that person, and
    3. you clearly document the original author of the HW4 solution you used in your README.txt for this assignment

If you think carefully, and the HW4 solution you use is clean enough, it is entirely possible to add depth sorting and z-buffering with only about 10 additional or changed lines of code for each (and this assumes no use of OpenGL or similar libraries).

Policy on using libraries for this assignment: For this assignment, you are again permitted to use OpenGL or other 3D graphics libraries. These commonly provide their own implementation of z-buffering, but not depth sorting. If you would like to use the z-buffering feature of such a library, that is acceptable for Part II this assignment. However, for Part I, you must implement your own depth sorting algorithm, even if such an algorithm happens to be available in whatever library you are using. (This may mean that you have to perform vertex coordinate transformations in your own code instead of asking the library to do them for you.)

Part I: Depth Sorting

Write a program that satisfies all requirements of HW4 Part III, but that also implements depth sorting. You should be able to turn depth sorting on and off, so that you can produce renderings that look like both the left and right duck images above. Your depth sorting program should also have the same failure modes as shown in the example images below for the overlapping and intersecting input files.

Part II: Z-Buffering

Write a program that satisfies all requirements of HW4 Part III, but that also implements z-buffering. You should be able to turn z-buffering on and off, so that you can produce renderings that look like both the left and right example images below.

How to implement a z-buffer without using a library like OpenGL? Assuming your objects have already been transformed to view coordinates (i.e. you have already applied any rotation and scale), and that the view coordinate frame has the axis pointing towards the viewer,

  1. Allocate an array of floats that is the same size () as your canvas (if your canvas can change size, you will need to take care to re-allocate this array as needed).
  2. Before rendering each frame, “clear” the z buffer by setting all depth values to (remember this is a special IEEE754 bit pattern).
  3. Within the inner loop for rendering each triangle using barycentric interpolation, also apply barycentric interpolation to the coordinates of the three vertices, resulting in an interpolated value at that pixel within the triangle.
  4. If your calculated value is greater than what is currently in the depth buffer at this pixel, then write it to the depth buffer, and also write the pixel color to the canvas, as usual. If your calculated value is less than what is currently in the depth buffer, just skip drawing this pixel.

Example Files

All the example files from HW4 should also be useful in this assignment. Here are two additional files that specifically demonstrate some problems with the painter’s algorithm that are solved with z-buffering. For each, a snapshot of failed rendering with the painter’s algorithm is shown on the left, and correct rendering with a z-buffer is shown on the right.

Turn In

Follow the instructions on the assignments page.

For this assignment, it is acceptable to write a single program that implements both depth sorting and z-buffering. However, it must be possible to turn each of those on and off.

Grading

Out of 100 total possible points, 60 will be assigned based on the organization, functionality, clarity, and documentation of your code, and the remaining 40 will be assigned based on the graphic output of your program.