Results 1 to 5 of 5

Thread: Very Basic...Steps to get depth buffer values

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2009
    Posts
    7

    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).

  2. #2
    Member Contributor
    Join Date
    Feb 2010
    Posts
    66

    Re: Very Basic...Steps to get depth buffer values

    directly from my app's code

    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;
    }


  3. #3
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,649

    Re: Very Basic...Steps to get depth buffer values

    Quote Originally Posted by asimov01
    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).
    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.

  4. #4
    Junior Member Newbie
    Join Date
    Oct 2009
    Posts
    7

    Re: Very Basic...Steps to get depth buffer values

    Quote Originally Posted by Dark Photon
    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).
    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(..);


  5. #5
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,649

    Re: Very Basic...Steps to get depth buffer values

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

Similar Threads

  1. Converting World Values to Depth Buffer Values
    By Tokage in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-22-2004, 02:04 AM
  2. Modifying Depth Buffer Values
    By Whilibarj in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 09-09-2004, 04:56 PM
  3. depth buffer values (again)
    By thelamberto in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-24-2002, 08:53 AM
  4. depth buffer values
    By thelamberto in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 07-24-2002, 04:45 AM
  5. depth buffer values
    By hoshi55 in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 05-19-2001, 10:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean