Resolve two overlapping triangles by keeping the smaller depth at each covered pixel.

Example

Choose the nearest triangle at each overlapping pixel.

highlighted = computed this step

Step 1 — Set up

Set up the exact input values.

red and blue triangles(((1, 1), (4, 1), (2, 4)), ((1, 2), (4, 2), (3, 4)))\begin{array}{c|c}\text{red and blue triangles}&\text{(((1, 1), (4, 1), (2, 4)), ((1, 2), (4, 2), (3, 4)))}\end{array}
Z-buffer winning pixelsAt each covered pixel, the smaller depth keeps its color.0123401234

Step 2 — Pixel A

Compute the highlighted value.

row, col, red z, blue z, winner(2, 2, 3, 2, blue)\begin{array}{c|c}\text{row, col, red z, blue z, winner}&\hlmath{\text{(2, 2, 3, 2, blue)}}\end{array}
Z-buffer winning pixelsAt each covered pixel, the smaller depth keeps its color.0123401234B

Step 3 — Pixel B

Compute the highlighted value.

row, col, red z, blue z, winner(2, 3, 1, 4, red)\begin{array}{c|c}\text{row, col, red z, blue z, winner}&\hlmath{\text{(2, 3, 1, 4, red)}}\end{array}
Z-buffer winning pixelsAt each covered pixel, the smaller depth keeps its color.0123401234BR

Step 4 — Result

State the final pixel set.

z-buffer pixels((2, 2, blue), (2, 3, red))\begin{array}{c|c}\text{z-buffer pixels}&\hlmath{\text{((2, 2, blue), (2, 3, red))}}\end{array}
Z-buffer winning pixelsAt each covered pixel, the smaller depth keeps its color.0123401234BR
z-buffer-depth A z-buffer stores the nearest depth seen so far at each pixel, so a later fragment only wins when its z value is smaller.