-1

Why the z-buffer value is always 0 when I use glReadPixels() to read the z-buffer?

int main()
{
    glReadPixels(0.0,0.0,width,height,GL_DEPTH_COMPONENT,GL_FLOAT,depth_data);
    for(int i=0;i<10;i++)
        for (int j=0;j<10;j++)
            cout<<depth_data[i][j]<<endl;

    return 0;
}
Fox32
  • 13,126
  • 9
  • 50
  • 71
  • 1
    Welcome to Stack Overflow. Simply posting a blob of code, with no explanation beyond the title, is not appropriate for this site. – Nicol Bolas Apr 14 '13 at 08:12
  • Why would you expect it to be anything else--you haven't drawn anything (or even created a GL context)? – Drew Hall Apr 14 '13 at 11:32

1 Answers1

2

I guess you simply didn't setup the context properly for your needs. What needs to be done is writing to the depth buffer needs to be enabled (glDepthMask(GL_TRUE);). Initially this is the case anyway. Also OpenGL requires depth testing to be enabled for anything to be written to the depth buffer. If you don't want the depth test to be performed add a call to glDepthFunc to always write the depth value.

glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_ALWAYS); // Change this to whatever kind of depth testing you want
glDepthRange(0.0f, 1.0f);

You might also want to check for any OpenGL error after reading the pixel data. Maybe you don't even have a depth buffer?

I hope this helps.

SIGKILL
  • 390
  • 1
  • 9