0
\$\begingroup\$

Why is it that the Z-buffer algorithm does not require polygons to be depth sorted to function correctly?

\$\endgroup\$
  • 5
    \$\begingroup\$ Can you walk us through your current understanding of the z-buffer algorithm, so we can spot where there might be gaps we need to address in answers? \$\endgroup\$ – DMGregory Dec 17 '18 at 12:45
1
\$\begingroup\$

The Z-Buffer simply saves the Z value of each pixel as you draw them. That means that when you try to draw a new pixel, you compare its Z value with the one saved on the Z-Buffer.

That means that once the whole scene is drawn, and the game decided to draw one last polygon on the very far background, the value of the pixels on the Z-Buffer corresponding to that polygon will be higher than the Z value of the pixels belonging to the polygon, so they will not be drawn at all.

This guarantees that the scene will be rendered correctly, and no polygon will be drawn on top of another where it shouldn't. However the negative is the overdraw. Although there is no guarantee which polygon will be drawn first (depends on engine/framework used), there's a high chance a pixel on the screen will be drawn more than once.

As mentioned in the comments, for performance reasons, it's best to draw polygons from front to back, so that most polygons that are obscured won't be rendered at all.

Also an exception to this are semi-transparent polygons, which have to be Z-sorted and drawn after all opaque polygons are drawn.

For more information, see Z-buffering on Wikipedia

\$\endgroup\$
  • \$\begingroup\$ This is true of opaque polygons. But semi-transparent polygons still need to be depth sorted because the semi-transparent blend operation requires the colour of what is behind the polygon to already be in the framebuffer. \$\endgroup\$ – Wyck Dec 17 '18 at 14:08
  • \$\begingroup\$ Also for performance reasons, it's better to draw a z-buffered scene from front to back, thus ensuring that most rasterized pixels will fail the Z test and can skip the fragment shader entirely. If you draw back to front with Z-buffering, then every pixel will PASS the Z test and it will be a waste of time to compute the fragment shader when the result is just going to be overwritten by the next polygon anyway. With no sorting, we expect average performance, and it will be likely be view orientation dependent based on the order of the geometry primitives within the models. \$\endgroup\$ – Wyck Dec 17 '18 at 14:10
  • \$\begingroup\$ @Wyck Thanks a lot, I added both recommendations in my answer for completeness :) \$\endgroup\$ – TomTsagk Dec 17 '18 at 14:25
  • \$\begingroup\$ For absolute completeness and technical correctness, this also all depends on your depth clear value and the depth test comparison used. \$\endgroup\$ – Maximus Minimus Dec 17 '18 at 14:41
  • \$\begingroup\$ @MaximusMinimus Do you mean as in, some libraries/frameworks out there use their own algorithms so it might not matter at the end? Also I'm not sure if I should include more information, as the question seems to be more beginner-friendly. I added a link to wikipedia, for people wanting to learn more. \$\endgroup\$ – TomTsagk Dec 17 '18 at 14:56

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.