OSG中距离转像素公式(PIXEL_SIZE_ON_SCREEN)

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;
    }
};

 

 

posted @ 2018-10-16 15:48  Mouming_Ning  阅读(2707)  评论(0编辑  收藏  举报