2
\$\begingroup\$

Suppose I use glReadPixels() to read the depth of a pixel from the depth buffer. What are the units of this? Is it the distance from the camera? Or the distance from the near clip plane? And is the distance normalized to clip space, or in real units?

\$\endgroup\$
1

1 Answer 1

2
\$\begingroup\$

Whatever you want them to be. From the documentation:

Each component is converted to floating point such that the minimum depth value maps to 0 and the maximum value maps to 1. Each component is then multiplied by GL_DEPTH_SCALE, added to GL_DEPTH_BIAS, and finally clamped to the range 0 1.

Beyond the given ranges, it is up to you to interpret the data as whatever "units" you see fit, depending on the data you wrote into the depth buffer in the first place and what it means to you.

\$\endgroup\$
6
  • \$\begingroup\$ So would a value of 0 be at the near clip plane? And a value of 1 be at the far clip plane? \$\endgroup\$ Commented Jul 29, 2015 at 16:45
  • 1
    \$\begingroup\$ @Karnivaurus Yes, 0 is near, and 1 is far. \$\endgroup\$ Commented Jul 29, 2015 at 17:05
  • 1
    \$\begingroup\$ @Karnivaurus Note though, the depth in the depth-buffer is not linear. \$\endgroup\$ Commented Jul 29, 2015 at 17:28
  • \$\begingroup\$ Oh, so its not just (depth - near_plane) / (far_plane - near_plane)? \$\endgroup\$ Commented Jul 29, 2015 at 17:30
  • \$\begingroup\$ No, i don't know the exact equation (its the perspective-matrix which performs it i guess) but its a equation which allows near z-values to be more exact and further values to be less exact. So the first 20% of depth from z-near to z-far already take ~80% of the buffer (values 0.0 to 0.8) to prevent z-fighting and wrong results in near objects, at the expense of inaccurate depth-values for further objects. \$\endgroup\$ Commented Jul 29, 2015 at 17:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.