osgearth_computerangecallback.cpp 中
下面的代碼假設:range模式是PIXEL_SIZE_ON_SCREEN,根據距視點的距離計算在螢幕中的像素大小。
像素大小轉距離可以根據此代碼中的公式逆推。
struct MyComputeRangeCallback : public osgEarth::ComputeRangeCallback
{
virtual float operator()(osg::Node* node, osg::NodeVisitor& nv)
{
osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(&nv);
if (cv)
{
// This code assumes that the range_mode is PIXEL_SIZE_ON_SCREEN and computes the pixel size on screen
// without just using the vertical field of view.
float distance = nv.getDistanceToViewPoint(node->getBound().center(),true);
float radius = node->getBound().radius();
double fov, ar, near, far;
cv->getCurrentCamera()->getProjectionMatrixAsPerspective(fov, ar, near,far);
osg::Viewport* viewPort = cv->getCurrentCamera()->getViewport();
double angularSize = osg::RadiansToDegrees( 2.0*atan(radius/distance) );
double dpp = osg::maximum(fov, 1.0e-17) / viewPort->height();
float pixelSize = angularSize / dpp;
return pixelSize;
}
// Fall back to the base calculation.
return -1.0;
}
};