Skip to content
Part IA Michaelmas Term

Z-Buffer Algorithm

Hidden Surface Removal

Multiple triangles may cover the same pixel. We need to determine which surface is closest to the camera.

Multiple triangles overlapping the same screen region

Z-Buffer

The Z-buffer (depth buffer) stores the depth value for each pixel.

Algorithm

Initialise:
    colour(x, y) = background_colour
    depth(x, y) = z_max  // far clipping plane

For each triangle:
    For each fragment (x, y):
        Calculate z for current (x, y)
        if (z < depth(x, y)) and (z > z_min):
            depth(x, y) = z
            colour(x, y) = fragment_colour(x, y)

Z-buffer showing depth values being tested and updated

Z-Buffer Precision

Typically 24 or 32 bits. Often stores 1/z1/z instead of zz for better precision distribution.

Why 1/z1/z?

Perspective transformation makes zz non-linear in screen space. Interpolating 1/z1/z gives correct results.

zscreen=znearzfarzeyez_{\text{screen}} = \frac{z_{\text{near}} \cdot z_{\text{far}}}{z_{\text{eye}}}

Z-buffer precision showing non-linear depth distribution

Z-Fighting

When two surfaces are nearly coplanar, limited precision causes Z-fighting: pixels from both surfaces appear randomly.

Solutions:

  • Increase depth buffer precision
  • Move surfaces slightly apart
  • Use polygon offset

Z-fighting artefact showing flickering between two surfaces

Summary

  • Z-buffer stores depth for each pixel
  • Update pixel only if new fragment is closer
  • Store 1/z1/z for better precision distribution
  • Z-fighting occurs when surfaces are nearly coplanar

Past Paper Questions

2025 Paper 3 Question 4(b)(i): In rasterisation, what information does the Z-buffer store? Why is this information needed, and how is it computed? [3 marks]

2024 Paper 3 Question 3: Explain the Z-buffer algorithm. What happens when two surfaces are at the same depth?