Question

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;
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top