Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

4.5 Hidden Surface Removal - Z-Buffer

Lesson 31 of 32 in the free Computer Graphics notes on Siksha Sarovar, written by Rohit Jangra.

Why Hidden Surface Removal

Multiple polygons may project onto the same pixel; the closest one to the viewer wins. The z-buffer algorithm (Catmull, 1974) is the most common solution and is built into every modern GPU.

Algorithm

  1. Allocate two buffers same size as the framebuffer:
  • Color buffer (the framebuffer).
  • Depth buffer (z-buffer), initialized to +infinity (or 1.0 in NDC).
  1. For each polygon, rasterize. For each pixel (x, y) covered:
  • Compute fragment depth z (interpolated barycentrically from vertex z values).
  • If z < depthBuf[x, y]:
  • colorBuf[x, y] = fragmentColor
  • depthBuf[x, y] = z
  • Else discard.
  1. After all polygons processed, the framebuffer holds the visible image.

Properties

  • Independent of polygon order -> can render in any order; trivially parallelizable.
  • Constant memory cost = WHdepth_precision (e.g., 1080p at 24-bit = ~6 MB).
  • Handles intersecting polygons correctly at fragment level.
  • Per-pixel test -> fast on GPU (early-Z, hierarchical Z optimizations).

Z Precision and Z-Fighting

The depth value after the perspective divide is non-linear: z_ndc = (f + n)/(f - n) - 2fn/((f - n)*z_view). Most precision is concentrated near the near plane. Two coplanar surfaces at large z may yield identical z_buf values -> z-fighting (flickering pixels). Mitigations:

  • Push the near plane farther.
  • Use a 32-bit float depth buffer.
  • Reverse-Z: store depth as 1 - z and use float, distributing precision more evenly.
  • Polygon offset for decals.

Numerical Example

Three triangles overlap at pixel (300, 200) with fragment z values 0.5, 0.3, 0.7. After processing in any order:

  • First write: z = 0.5 -> stored.
  • Next: z = 0.3 < 0.5 -> stored.
  • Next: z = 0.7 > 0.3 -> discarded.
  • Final color is from the z = 0.3 fragment, regardless of arrival order.

Memory Cost Table

Resolution24-bit depth (MB)32-bit float (MB)
720p2.63.5
1080p5.97.9
1440p10.514.0
4K23.731.6

Pros and Cons

  • Pros: Simple, parallel, handles arbitrary scenes including intersections and self-occlusion.
  • Cons: Memory; z-fighting at far ranges; transparency requires sorting (or order-independent transparency techniques).

Modern Optimizations

  • Hierarchical Z-buffer: store min/max z in a quadtree for fast occlusion culling.
  • Early-Z test: hardware skips fragment shader if depth test fails before shading.
  • Reverse-Z + float depth: industry-standard precision improvement.