View Full Version : QGLViewer - Depthbuffer z distance

03-09-2012, 11:46 AM
Hi, Iím to use Depthbuffer to retrieve the Z distance of objets in my OpenGl scene using QglViewer.
Iím using the following code: glReadPixels(nStartX, nStartY, nWidth, nHeight, GL_DEPTH_COMPONENT, GL_FLOAT, f_Depth);
This retrieves the Depth Component.
BUT Iím reading on http://www.opengl.org/resources/faq/technical/depthbuffer.htm
ďThe depth buffer precision in eye coordinates is strongly affected by the ratio of zFar to zNear, the zFar clipping plane, and how far an object is from the zNear clipping plane.Ē
So, this make that my Depth Value are not linear. So I canít use theses values the have a real Z matrix of the scene.
This cause many problem since I canít set a ďunitĒ to these value.

My usage is to use these Depth Matrix to use the technology SLAM
So I need real distance in the matrix, not perspective value that mean nothing for me.

If anyone have any idea to retrieve the good Z value of the pixel, I would be so happy.

The first link I gave explain in some way how the calcul is done, but Iím not able to understand the functions given and how to retrieve the REAL Z distanceÖ


03-09-2012, 02:06 PM
I finally found the answers after a couple of hours of googling:
bufferToCamera[zb_, near_, far_] := far * near / (zb * (far - near) - far);
where zb = z from buffer
bufferToCamera= z from camera