CS40: Computer Graphics, Lab 8

Z-Buffering

By Jeff Kaufman

In this lab I extended the graphics library I started in lab 2 by adding support for Z-buffering.

How Z-buffering works

If we want to draw objects where one is in front of the other we can't just draw them in any order. If we draw the front one first and the back one second we'll get a confusing effect where there appears to be a hole in the closer object through which we can see the farther one. The simplest solution seems to be to sort the objects by their z-coordinate and draw them in that order, from farthest to nearest. I used a manual version of this in previous labs for the few cases where I wanted something in front of something else.

This is not a great solution, though. Not only does it require a full sort of all the polygons but it can't deal with several cases. If a polygon goes through another polygon it must be broken so the two parts can be sorted independently. There are also cases where there is no intersection, but a cut must still be made. If there are three triangles, each overlaping one and being overlapped by the other, as in the image below, breaks are needed and the calculation is tricky.
A nicer solution than ordering is to make an image-sized array, the z-buffer, to hold z-values. When setting a point, if the z-value for that point is less than that in the z-buffer, draw onto the image and set the corresponding point in the z-buffer to the z-value. If the z-value in the z-buffer is greater, then do nothing because this is a point that is behind another one we've already drawn.

Getting z-values is not that hard; we can interpolate them over our polygons and along our lines. This interpolation is not linear, however, as we are drawing a perspective projection and points that are farther away are drawn closer together. We reached this projection by dividing our x and y coordinates by their corresponding z coordinate. This means that if we interpolate 1/z instead of z it will be linear.

Interpolating 1/z and then inverting to compare with the z-buffer would be a waste of time, so we instead just use 1/z values for the z-buffer and compare with those.

Required Images

Cubism - with ordering

Demonstration Images

Solid Intersecting Cubes and a Sphere

In lab 7 there were transparent gaps between adjoining polygons. This was a result of my antialiasing algoithm trying to blend with the background at polygon edges. The algorithm was not well tuned from the beginning, and once we begin Z-buffering it can no longer blend properly, as it does not know what objects are behind. To do this properly in three dimensions I would implement alpha-buffering and make polygons and lines draw with fuzzily transparent edges. Not having done or having the time to do this, I have removed my antialiasing code from my line and polygon drawing routines.

Solid Cubes Intersecting a Wire Sphere

Note that while the Z-buffering mostly works, there are some lines in the above movie that flicker between two colors. This is because my sphere creation algorithm has placed two otherwise identical lines with different colors. To fix this I modified the sphere creation algorithm to shrink each of its triangles by 0.00001% when it was in wireframe mode. The movie below shows the revised version:
The Z-buffering of the lines is somewhat silly: the buffering implementation is floating point, very similar to the one for polygons. The rest of the line drawing algorithm is integerized, though. It is possible that we get speedups from being able to use both the ALU and the FPU at once, but if I had more time I would want to write a fully integerized line Z-buffering algorithm.

Lab Questions

  1. In parallel projection objects do not become smaller as they get farther away; the ratios between the z coordinate and the other two coordniates remain constant. This means z would interpolate linearly if we wanted to z-buffer in parallel projection.
  2. The only extentions I did for this assignment were the movies of the cubes and sphere rotating.


Jeff Kaufman : 2006
cbr at sccs dot swarthmore dot spam edu. Remove spam.

main page