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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM