Lab 8: Z-Buffer Algorithm

Alex Benn and Andrew Frampton

Quick Summary

In this lab, we added a z Buffer to our graphics library by adding the appropriate flags and functions to the Image, Line, Polygon, Polyline, and Scanfill files. For the Image file we added functions to both access and manipulate the z value for a given pixel. In Line we added a flag to indicate when to deal with the z buffer, and then augmented the Line_draw() function to appropriately draw and clip in the 3D space. The inverse z-value is first calculated for each pixel along the line; a pixel is only drawn to the output image if its z-value is closer to the viewer than the previous z-value for that pixel. Finally, we changed the Scanfill algorithm to draw polygons using the Z-buffer. This is done in a manner very similar to the Line algorithm: each pixel's z-value is calculated, then compared against the z-buffer. Finally, we changed the brightness of each pixel according to its z-value, so that farther objects appear darker. This was done both because resulting images look nifty and because this feature helped with debugging the z-buffer algorithm.


Figure 1: Test Code Animation (cubism.c) with Wireframe


Figure 2: Test Code Animation with Fill

Super-Awesome Update! Although the sphere-drawing code demonstrated in Figure 3 below was developed long after this lab was due, it demonstrates the Z-buffering techniques on a level unparalleled on this plane of existence. Check it out.


Figure 3: Intersection of Sphere and Cube

Questions

  1. In parallel projection, all coordinates are linked linearly. This means that travelling along a line in any one dimension results in a corresponding linear change in both of the other dimensions. This means that the z-coordinate varies directly with the x- and y-coordinates. This is not the case in perspective projection, where the x- and y-coordinates vary with the inverse of the z-coordinate.
  2. Extensions: Figure 2 above uses a quick-and-dirty supersampling anti-aliasing scheme. Images are rendered at 4 times as many pixels in both dimensions, and are then scaled down using the convert command.

Back to Alex and Andrew's other labs