Lab 7: The Z-Buffer Algorithm
Will Moss & Andrew Cantino

This is lab 7 of Computer Graphics.

Sections:

The Z-Buffer Algorithm

The Z-Buffer algorithm is very simple in concept. Along with the image that we are drawing to, we keep a z-buffer, which is simply a matrix of doubles that stores the closest z value that has been drawn to the screen at any given (x, y) position. Hence, the z-buffer has the same dimensions as the image itself. As we draw to the image, at every pixel we compare our z value to that in the z-buffer, and only draw if our z value is nearer to the viewer than the value in the z-buffer. If we do draw, we update the z-buffer with the new z value.

Our specific implementation of the z-buffer algorithm is a modification to our polygon scanline fill algorithm. Now polygonRef3Ds know how to render themselves to an image, assuming the image has an initialized z-buffer. Our new scanline fill interpolates z across each scanline and column (actually, 1/z to preserve linearity in the perspective projection.) For each point on each scanline, we check our z value against that in the z-buffer, and only draw and update the z-buffer if our's is closer.

Images

Here are our required images:


Required Image One

Required Image One with Depth Shading

Questions and Answers
  1. How can you/did you connect your Z-buffer algorithm with the rest of your 3D modeling system?
    Because we made out modeling system output a list of polygon references and a list of points in the last lab, integrating our z-buffer with our hierarchical modeling system was quite easy. We store our current z-buffer in our image class, and then render all of the polygon list using that z-buffer. To speed up the z-buffer algorithm, we sort the polygon list from closest to farthest z, using the first point in the polygon for the comparison.

  2. We know z varies inversely with x and y for perspective projection. How does z vary relative to x and y for parallel projection?
    Z varies linearly with x and y in parallel projection. In other words, given two z values, we can interpolate z between them linearly, unlike in perspective projection.

Extensions

Integration of Z-Buffer and Modeling System
We integrated our Z-Buffer and modeling system, as described above.

Cool Animation Sequences and pyramid3D
We made a cool animation of two square pyramids rotating and orbiting eachother. Notice the aliasing issues on the first animation. This results from the fact that we did not have our polygons share points. Noting this, we designed a subclass of modelPrimative called pyramid3D that takes a top and an arbitrary base and generates a pyramid with polygons that share points as needed. This was only possible because we now render using a separate point list and polygon list.


Animation with Aliasing Issues

Animation with Shared Points (no aliasing issues)

Additional Resources

Here are some useful links relating to Z-buffers:

[Back to Lab Index]