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

StackOverflow https://stackoverflow.com/questions/15996986

  •  03-04-2022
  •  | 
  •  

문제

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;
}
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top