LerpworksAcademy
On this page

Graphics › The frame

Depth buffer: what is closer to the camera

Know first: Shader: a function for every pixel, Spaces: where a point is

A graphics card draws triangles one at a time, in whatever order the engine sends them. So how does it know that a wall hides a tree and not the other way around? The answer is the depth buffer. This lesson shows how it settles the question for every pixel, why depth is stored unevenly, and where the flicker of coinciding surfaces comes from. It also covers how an early test saves the pixel shader’s work, and how the same depth, read inside a material, gives water a soft edge. This is the third lesson of the course “How an engine draws a frame.”

Why this matters

The shader lesson ended a triangle’s journey with one line: “depth test: if something closer to the camera is already drawn at that spot, the result is thrown away.” Behind that line is a mechanism that an environment artist runs into all the time, whether or not they know its name:

  • a decal on a wall or road markings in the distance shimmer in stripes;
  • distant mountains start to flicker after the camera is set up to get right up against walls;
  • water, fog and smoke slice the ground along a hard line where they touch it;
  • grass and foliage cost more than their pixel count says they should;
  • particles vanish behind a window, as if the glass were solid.

All of this is one topic: how the graphics card decides what is closer to the camera, and what else depth is good for.

Further in the article

  1. The painter’s algorithm
  2. The depth buffer: deciding per pixel
  3. What the buffer holds: not meters
  4. Precision and flicker
  5. Flicker where surfaces coincide
  6. Early-Z: don’t shade what is hidden
  7. Depth as a texture
  8. Where it breaks
  9. Connections: the same idea in other fields
  10. Key points
  11. Test yourself