I’m currently trying to save the depth image (pixel values = depth ) of 3D models. The aim is to simulate a kinect sensor taking depth pictures of a 3D object.
After looking at some websites, I read there are three ways to achieve this: using the zbuffer, using the wbuffer or using ray casting.
Which would you advice ? What are the pros and cons ?
For now, the only thing I barely managed to do is get information from the zbuffer. But I guess I’m doing something wrong as the values I get are really strange.
Also, you should read your depth buffer back in the format that it’s stored in. Which means that unless you did something special when creating it, it is likely stored in 24_8 format (where the 8 bits are stencil bits). You should read them back as unsigned integers.
Here is my display function, in case the problem is there :
readDepth();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to red
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the colour buffer (more buffers later on)
glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations
glTranslatef(0.0f, 0.0f, -3.0f);
glutSolidTeapot(1.0f); // Render a teapot
glutSwapBuffers();
Where the readDepth() function read the zbuffer and write it to a file.
This reads from outside of the screen. If you want to read the entire screen, the first two values should be 0, 0. You’re starting at the height of the screen, and then reading another screen height on top of that. That’s not going to work.
Using GL_DEPTH_COMPONENT24 did not raise any exception.
With V-man’s solution, the zeros are replace by a full load of 4294967295.
But it is still as if my teapot doesn’t exist.
After some testing I get results. I think the problem is because I want to save the depth buffer to a file at the beginning of the program.
I suppose the object is not rendered when I try to read the zbuffer so it is empty.
The question now is, when should I attempt to read the z buffer in order to be sure there will be usefull information in it ?