Skip to main content

Z-buffer (depth buffer) method

Short answer

Z-buffer stores nearest depth per pixel; depth is computed as z = (−Ax−By−D)/C. Incrementally along a scan line, depth updates as z' = z − A/C; if new depth is less than stored, the pixel is replaced in the frame buffer.

Explain the Z-buffer (depth buffer) method of visible surface detection, including how the depth of pixels is calculated.

The Z-buffer (depth-buffer) method is an image-space method. It compares the depth (z value) of surfaces at each pixel position (x, y) of the projection plane and keeps the nearest one.

Two buffers

  • Depth buffer zbuf(x, y): depth of the nearest surface found so far at each pixel.
  • Frame (refresh) buffer frame(x, y): colour of the nearest surface at each pixel.

Viewing is along the z-axis, with 0 ≤ depth ≤ 1 (0 = front plane, 1 = back plane).

Algorithm

1. Initialise zbuf(x,y) = 1 (maximum depth), frame(x,y) = background colour for all x,y.
2. For each polygon in the scene:
     for each pixel (x,y) of its projection:
         compute depth z of the polygon at (x,y)
         if z < zbuf(x,y):
             zbuf(x,y)  = z
             frame(x,y) = surface colour at (x,y)
3. After all polygons are done, the frame buffer holds the visible image.

Depth calculation. For a plane Ax + By + Cz + D = 0:

z = (−Ax − By − D) / C

Incremental calculation (faster): when we move one pixel to the right along a scan line, x becomes x + 1, so

z' = (−A(x+1) − By − D)/C = z − A/C

(A/C is a constant for the polygon). Moving down to the next scan line along an edge with slope m (x' = x − 1/m, y' = y − 1):

z' = z + (A/m + B)/C

For a vertical edge, z' = z + B/C.

Features: the method is simple, handles any shape, needs no sorting, and polygons may be processed in any order. However, it needs extra memory for the depth buffer, cannot render transparent surfaces, and suffers from aliasing.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Graphics chapter 7 questions →