Very Basic...Steps to get depth buffer values

Hi Guys,

I am trying to learn openGl programing. I am just trying to do a simple program to calculate the depth values for a given tgriangle. Needless to say it was an epic failure. I am sure I am not making the right calls because after the glReadPixels event the values in the z-buffer do not change. It maybe that my triangle here is just not visible from my camera???

If anyone could please help me and let me know what steps I need to code to get the z-values, i would be real greatful.

The triangle is defined by the following verts
The vertices are at (0, 10000.0, -100.01) , (-10000,-10000,-100.01),(10000,-10000,-100.01)
some other info:
near = 100, far = 100000, left = -100, right = 100, top = 100, bottom = 100.
The camera is at (0,0,-1000).

directly from my app’s code


GLfloat get_gl_depth(int x, int y)
{
  float depth_z = 0.0f;
  
  glReadBuffer(GL_FRONT);
  glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth_z);
  return depth_z;
}

You gave it a good shot, but didn’t fully specify all of what’s needed if someone were to crunch through this. You’ve got all the glFrustum params (PROJECTION transform), but not enough for anyone to build a VIEWING transform. They only know where the eye is. Not where it’s looking or what the view “up” vector is. We’re also missing the viewport (for the VIEWPORT transform).

At any rate, to get the depth, you need to first figure out where your poly is ending up on the screen. To do so, you need to take your vertices (which I presume are OBJECT space) all the way up to SCREEN space. To do so, multiply them by MODELING, VIEWING, PROJECTION, and VIEWPORT transforms. See gluProject for a quick-n-dirty helper function – just pass in the right values.

Once you’ve got a screen (pixel) location you want to sample the depth at, use the above-provided code to pull the depth value out of the framebuffer. Keep in mind that GPU readbacks are slow and should generally be avoided. You need to read from the buffer you rendered to, whether it be FRONT (single-buffer) or BACK (double-buffer).

What you get is a not-too-useful 0…1 value. You then presumably want to map this “depth in screen space” back to object space or world space units where it makes some sense to you. So you just do the inverse of the above transform-sequence (use inverse transforms and apply them in reverse order). See gluUnProject for a quick-n-dirty helper for that.

The up vector in this case is -z axis looking at (0,0,0)

Dark Phono, Is this the sequence (step by step) you are saying to use
glMatrixMode(GL_MODELVIEW);
glLookAt(…);
glFrustrum(…);
glViewPort(…);
gluProject(…);
glReadPixels(…);
gluUnProject(…);

Close. glFrustum should be applied to the PROJECTION matrix though. And you render your scene right after the glViewport.