我正在尝试使用z缓冲区来测量距离,但我发现了一个意想不到的问题。
我知道z-buffer通过我的代码返回一个0..1浮点数:
zImageData = new osg::Image;
zImageData->allocateImage(720, 576, 1, GL_DEPTH_COMPONENT ,GL_FLOAT);
osgCam->setProjectionMatrixAsPerspective(45.0, 1.0, nearplane, farplane);
osgCam->setViewMatrixAsLookAt(osg::Vec3(0,0,camera_high), osg::Vec3(0,0,0),osg::Vec3(0,1,0) );
osgCam->attach(osg::Camera::DEPTH_BUFFER, zImageData);
//osgCam is my master camera.
然后我使用回调函数对z-buffer值进行后处理:
z = ((float*)sonar->zImageData->data())[1];
float true_distance = farplane*nearplane/(farplane - z*(farplane-nearplane));
一切正常,直到我达到 24 距离单位,如下所示。
我有3个变量, nearplane (视口), farplane 和 camera_high 。 最后一个是相机与地面的距离。它正在看一架飞机(它的意思是地面)。 这是程序输出:
const float nearplane = 0.2; const float farplane = 200;
const float camera_high = 5;
z value : 0.960961
z distance value : 5
*** Exited normally ***
const float camera_high = 10;
z value : 0.980981
z distance value : 10
*** Exited normally ***
const float camera_high = 20;
z value : 0.990991
z distance value : 20
*** Exited normally ***
const float camera_high = 23;
z value : 0.992297
z distance value : 22.9999
*** Exited normally ***
const float camera_high = 25;
z value : 1
z distance value : 200.003
*** Exited normally ***
const float nearplane = 20;
const float camera_high = 25;
z value : 1
z distance value : 200
*** Exited normally ***
const float camera_high = 23;
z value : 0.144928
z distance value : 23
*** Exited normally ***
const float nearplane = 24;
const float camera_high = 25;
z value : 1
z distance value : 200
*** Exited normally ***
除了 nearplane 和 farplane 的位置,我才能在 24 之外的z缓冲区中获得任何差异。< / p>