The Z-Buffer Algorithm


The Z-buffer algorithm is a convenient algorithm for rendering images properly according to depth. To begin with, a buffer containing the closest depth at each pixel location is created parallel to the image buffer. Each location in this depth buffer is initialized to negative infinity. Now, the zIntersect and dzPerScanline fields are added to each edge record in the polyfill algorithm. For each point that is to be rendered, the depth of the point against the depth of the point at the desired pixel location. If the point depth is greater than the depth at the current pixel, the pixel is colored with the new color and the depth buffer is updated. Otherwise, the point is not rendered because it is behind another object.

Depth Conversion of 3D Triangles

The changes described above were made to our polyfill algorithm. The image below was created to demonstrate the operation of the improved system. The blue triangle has corners at (75, 75, 10), (75, 25, 10), and (75, 50, 20). The red triangle has corners at points (25, 75, 10), (25, 25, 15), and (75, 50, 30).


Two extensions were performed for this assignment. First, we integrated the z-buffer code into our 3D modeling system. A few changes had to be made to accomodate the z-buffer code. Points were converted from 2D to 3D. In addition, a depth buffer was added to the image structure. Then polyFIll was changed to take into account the depth of the pixel before drawing it. It would be fairly simple to add this depth buffer check to the other primitives. If we end up changing other primitives to work with the Z-buffer, it will probably make more sense to test the pixel's depth within SetPixel.

For the second extension, we created an animated gif of one cube passing through another. This animation integrates a shift of the viewpoint with the z-buffer algorithm. The image was fairly simple to create given our 3D modeling system.


  1. How can you/did you connect your Z-buffer algorithm with the rest of your 3D modeling system?
    See above (Extensions section) for the answer to this question.
  2. Is there any problem with using a linear representation of depth across a polygon? When does it matter?
    There is no problem if the polygons are restricted to triangles, since 3 points define a plane. With polygons with more vertices, however, the points do not all have to lie in a plane, resulting in a non-linear interpolation across the polygon.
  3. What extensions did you do for this assignment, how did you do them, and how well did they work?
    See above for more detail on this. We basically added the z-buffer algorithm to our hierarchical modeling system. It worked well. We also created a neat gif image to show off our cool system.