Results 1 to 2 of 2

Thread: why the z-buffer value is always 1 when i use glReadPixels() to read the z-buffer?

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    3

    why the z-buffer value is always 1 when i use glReadPixels() to read the z-buffer?

    in openinventor ,i use the callback node to read the z-buffer value and want to get the z coordinate in the real world. I use the glReadPixels() to read the z-buffer, but all the depth
    value i reading is 1. I do not know why i can not get it. my programme
    is following.


    //project the elbowCenter to the screen
    elbowCenter.setValue(1.0,-3.0,0.0);
    SbViewVolume myViewVolume=myCamera->getViewVolume();
    SbVec3f pointScreen;
    int x,y;
    myViewVolume. projectToScreen(SbVec3f(elbowCenter[0], elbowCenter[1],
    elbowCenter[2]),pointScreen);
    x=pointScreen[0]*size[0];
    y=pointScreen[1]*size[1];

    glEnable (GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    //get the real world coordinate --posz
    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX, winY, winZ;
    GLdouble posX, posY, posZ;

    glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
    glGetDoublev( GL_PROJECTION_MATRIX, projection );
    glGetIntegerv( GL_VIEWPORT, viewport );

    winX = x;
    winY = y;
    glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ
    );
    gluUnProject( winX, winY, winZ, modelview, projection, viewport,
    &posX, &posY, &posZ);

  2. #2
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,450

    Re: why the z-buffer value is always 1 when i use glReadPixels() to read the z-buffer?

    That's the depth clear value. Make sure you pick at the correct place!
    Windows coordinates are top left orign, OpenGL pixel coordinates are bottom left.
    y needs to be inverted if you get it from Windows. Use this formula:
    y = windowClientHeight - 1 - yMousePosition;

    Example: You clicked on the bottom line in a 640*480 window.
    windowClientHeight == 480, yMouse is 479, y == 0.
    glReadPixels from there. Voila!

Similar Threads

  1. glReadPixels fails to read the content of the color buffer
    By salous in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 06-18-2012, 07:33 PM
  2. read stencil buffer
    By Cucus in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 07-23-2011, 02:51 AM
  3. glReadPixels read the front buffer or the back buffer?
    By s.hu in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 09-16-2004, 11:15 AM
  4. How to read Depth Buffer
    By Heema in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 10-30-2003, 03:25 AM
  5. how to read from back buffer
    By ae97004 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-02-2002, 12:52 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