Depth Value VS Z coordinate? I'm confused...

Started by
2 comments, last by Koen 5 years, 2 months ago

Hello!

I'm currently learning how the depth testing works in OpenGL from these tutorials and the tutorial says that


By default the depth function GL_LESS is used that discards all the fragments that have a depth value higher than or equal to the current depth buffer's value.

If i guess that the depth value it the z coordinate that I pass through the vertex data, then the above statement should not be true. Fragments with small Z values should be discarded because the depth is towards the -Z axis not fragments with higher z value. Does the depth values are created somehow else by using the z coordinate of the fragment? So the depth value is a number from 0...N so lets say a fragment has a depth value of 5 and the one that is behind it has 10, the 5 will pass the test?


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

Advertisement

Depth values are between 0 and 1. The z coordinates get mapped to this range with a formula, so that 0 is closest and 1 is furthest away. When comparison happens with GL_LESS, a fragment that is in front (closer, has a lower depth value) will pass and be rendered, and a fragment that is in the back (further, has a higher depth value) will be discarded, so it won't get rendered. In short, fragments that are behind other fragments will be skipped.

If you set up the projection matrix the "default way", it will transform the near plane to -1 (or 0 for Direct3D) and the far plane to 1 (for both apis). If you'd prefer the other way around, you can do that too, but you'll have to properly build your projection matrix for your convention.

edit: reversing might even be a good idea: see this blog post

This topic is closed to new replies.

Advertisement