Depth Test

The sphere's depth is less than the far side of the torus, so red is written to the color buffer.

The sphere's depth is greater than the near side of the torus, so the yellow remains.