osgEarth示例分析——osgearth_horizon

前言

osgearth_horizon示例,在地球上绘制了一大一小两个白色半球。随着转动地球,右下角会提示:白色球的可见状态,以及当前地图图层加载到第几层级。

执行命令:

osgearth_horizond.exe earth_image\world.earth --activity

执行效果

右下角被logo遮挡了。。。

 代码分析

#include <osgViewer/Viewer>
#include <osgEarth/Notify>
#include <osgEarth/GeoTransform>
#include <osgEarth/MapNode>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/ExampleResources>
#include <osgEarthAnnotation/AnnotationUtils>
#include <osgEarth/Horizon>
#include <osgEarth/Registry>
#include <osgEarthUtil/ActivityMonitorTool>

#include <osg/Shape>
#include <osg/ShapeDrawable>
#include <osg/NodeCallback>

#define LC "[viewer] "

using namespace osgEarth;
using namespace osgEarth::Util;
using namespace osgEarth::Annotation;

int
usage(const char* name)
{
    OE_NOTICE 
        << "\nUsage: " << name << " file.earth --activity" << std::endl
        << MapNodeHelper().usage() << std::endl;

    return 0;
}

// 定义两个球的半径
#define RADIUS  500000.0f
#define RADIUS2 200000.0f

// 计算自定义包围球回调方法
struct MyComputeBoundCallback : public osg::Node::ComputeBoundingSphereCallback
{
    double _radius;
    MyComputeBoundCallback(float radius) : _radius(radius) { }
    osg::BoundingSphere computeBound(const osg::Node&) const
    {
		// 根据传入的半径重新计算包围球
        return osg::BoundingSphere(osg::Vec3f(0,0,0), _radius);
    }
};

// 安装第一个球
osg::Node*
installGeometry1(const SpatialReference* srs)
{
    osg::Geode* geode = new osg::Geode();
	// 添加包围球回调方法
    geode->setComputeBoundingSphereCallback( new MyComputeBoundCallback(RADIUS) );
	// 绘制包围球
    geode->addDrawable( new osg::ShapeDrawable( new osg::Sphere(osg::Vec3f(0,0,0), RADIUS) ) );
	// 获取到包围球中心
    osg::Vec3f center = geode->getBound().center();
	// 创建地球矩阵,将叶子节点加入到此矩阵
    GeoTransform* xform = new GeoTransform();
    xform->setPosition( GeoPoint(srs, 0.0, 0.0, 0.0, ALTMODE_ABSOLUTE) );
    xform->addChild( geode );
    return xform;
}

// 安装第二个球
osg::Node*
installGeometry2(const SpatialReference* srs)
{
    osg::Geode* geode = new osg::Geode();
    geode->setComputeBoundingSphereCallback( new MyComputeBoundCallback(RADIUS2) );
    geode->addDrawable( new osg::ShapeDrawable( new osg::Sphere(osg::Vec3f(0,0,0), RADIUS2) ) );
    osg::Vec3f center = geode->getBound().center();
    GeoTransform* xform = new GeoTransform();
    xform->setPosition( GeoPoint(srs, 180.0, 0.0, 0.0, ALTMODE_ABSOLUTE) );
    xform->addChild( geode );
    return xform;
}

int
main(int argc, char** argv)
{
    osg::ArgumentParser arguments(&argc,argv);

    // help?
    if ( arguments.read("--help") )
        return usage(argv[0]);

	// 执行命令时,必须输入 --activity 参数,否则地平线判断无法用
    if (arguments.find("--activity") < 0)
        return usage(argv[0]);

    // create a viewer:
    osgViewer::Viewer viewer(arguments);

    // Tell the database pager to not modify the unref settings
    viewer.getDatabasePager()->setUnrefImageDataAfterApplyPolicy( false, false );

    // install our default manipulator (do this before calling load)
    viewer.setCameraManipulator( new EarthManipulator(arguments) );

    // load an earth file, and support all or our example command-line options
    // and earth file <external> tags    
    osg::Node* node = MapNodeHelper().load( arguments, &viewer );
    if ( node )
    {
        osg::Group* root = new osg::Group();
        viewer.setSceneData( root );
        root->addChild( node );

        MapNode* mapNode = MapNode::get(node);
        const SpatialReference* srs = mapNode->getMapSRS();

		// 添加两个球
        osg::Node* item1 = installGeometry1(srs);
        mapNode->addChild( item1 );

        osg::Node* item2 = installGeometry2(srs);
        mapNode->addChild( item2 );

        // Culls the second item based on its horizon visibility
		// 为第二个item设置地平线可见回调
        HorizonCullCallback* callback = new HorizonCullCallback();
        //item2->addCullCallback( callback ); // 源码中,此处为item2设置地平线回调
		item1->addCullCallback(callback);	// 根据while内的判断,改为item1
        
        // This horizon object we are just using to print out the results;
        // it's not actually part of the culling cullback!
		// 此地平线对象,用于打印结果,并不作为回调方法的一部分
        osg::ref_ptr<Horizon> horizon = new Horizon(srs);        

        while (!viewer.done())
        {
            viewer.frame();

			// 获取相机视点信息
            osg::Vec3d eye, center, up;
            viewer.getCamera()->getViewMatrixAsLookAt(eye, center, up);
            horizon->setEye( eye );// 视点设置给地平线对象

            if ( horizon->isVisible( item1->getBound() ) )
            {
                Registry::instance()->endActivity( "large sphere" );
                Registry::instance()->startActivity( "large sphere", "VISIBLE" );
            }
            else
            {
                Registry::instance()->endActivity( "large sphere" );
                Registry::instance()->startActivity( "large sphere", "occluded" );
            }
        }
    }
    else
    {
        return usage(argv[0]);
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值