Shader Learn

10 Other fragment outputs

gl_FragDepth: a card pretending to be a sphere

The green “sphere” is really a flat card facing you. The fragment computes the sphere’s depth and writes it to gl_FragDepth, so the box intersects it correctly.

0.51.3

Runs once per pixelDrag numbers, or edit the code

Rasterization gives every fragment a depth; writing gl_FragDepth replaces it. That is how a flat card can intersect a box like a real sphere.

Try it

  • Turn off “Write gl_FragDepth”: how do the box and the card intersect now?
  • Increase the radius for a rounder bulge
  • Replace N.z with 0.0: the depth goes flat again