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.
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.