osgEarth示例分析——osgearth_scenegraphcallbacks

前言

osgearth_scenegraphcallbacks示例,定义了一个图层的回调方法,继承自 osgEarth::SceneGraphCallback. 获取场景事件通知的回调方法。并没有看懂此回调方法是如何使用的。也没有打印输出。

执行命令:osgearth_scenegraphcallbacksd.exe earth_image\china-simple.earth

// 当前的earth文件中,有4个图层
layer-name: GlobeHeightfiled
layer-name: GlobeImage
layer-name: world_boundaries
layer-name: china_boundaries

效果

 代码分析

#include <osgViewer/Viewer>
#include <osgEarth/Notify>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/ExampleResources>
#include <osgEarth/MapNode>
#include <osgEarth/SceneGraphCallback>

#include <osgEarthAnnotation/PlaceNode>

#define LC "[scenegraphcallbacks] "

using namespace osgEarth;
using namespace osgEarth::Util;

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

    return 0;
}

// SceneGraphCallback: 获取场景图事件通知的回调。
// 并非所有方法都可以在所有情况下调用。
// 这取决于实现在需要时调用。
class MyCallback : public SceneGraphCallback
{
public:
	// 在将节点添加到实时场景图之前调用
    void onPreMergeNode(osg::Node* node, osg::Object* sender)
    {
        Layer* layer = static_cast<Layer*>(sender);
        OE_NOTICE << "Layer " << layer->getName() << " pre-merge node " << node->getName() << std::endl;
    }

	// 在主/更新线程中 将节点添加到 活动 场景图后调用。
    void onPostMergeNode(osg::Node* node, osg::Object* sender)
    {
        Layer* layer = static_cast<Layer*>(sender);
        OE_NOTICE << "Layer " << layer->getName() << " post-merge node " << node->getName() << std::endl;
    }

	// 在主/更新线程中 从 活动 场景图中 移除节点后调用。
    void onRemoveNode(osg::Node* node, osg::Object* sender)
    {
        Layer* layer = static_cast<Layer*>(sender);
        OE_NOTICE << "Layer " << layer->getName() << " remove node " << node->getName() << std::endl;
    }
};

int
main(int argc, char** argv)
{
    osg::ArgumentParser arguments(&argc,argv);
    osgViewer::Viewer viewer(arguments);
    viewer.setCameraManipulator( new EarthManipulator(arguments) );

    osg::Node* node = MapNodeHelper().load(arguments, &viewer);
    if ( node )
    {
        MapNode* mapNode = MapNode::get(node);
        if (!mapNode)
            return -1;

        MyCallback* myCallback = new MyCallback();

        // Install a callback on each loaded layer.
        LayerVector layers;// 图层列表
        mapNode->getMap()->getLayers(layers);
        for (LayerVector::iterator layer = layers.begin(); layer != layers.end(); ++layer)
        {
			std::cout <<"layer-name: "<< layer->get()->getName()<< std::endl;;
			// 为每一个图层添加回调
            layer->get()->getSceneGraphCallbacks()->add( myCallback );
        }

		// 测试添加一个node节点
		// 设置style 贴地
		Style placeStyle;
		placeStyle.getOrCreate<AltitudeSymbol>()->clamping() = AltitudeSymbol::CLAMP_RELATIVE_TO_TERRAIN;

		// A lat/long SRS for specifying points. 获取地理坐标系
		const SpatialReference* geoSRS = mapNode->getMapSRS()->getGeographicSRS();
		double centerLat = 39.840866;
		double centerLon = 119.769846;
		osg::ref_ptr<osg::Image> pin = osgDB::readRefImageFile("../data/placemark32.png");
		osgEarth::Annotation::PlaceNode* place = new osgEarth::Annotation::PlaceNode("PlaceMark", placeStyle, pin.get());
		place->setMapNode(mapNode);
		place->setPosition(GeoPoint(geoSRS, centerLon, centerLat));
		place->setOcclusionCulling(true);

		osg::Group* root = new osg::Group();
		root->addChild(place);

		root->addChild(node);
        viewer.setSceneData(root);
        return viewer.run();
    }
    else
    {
        return usage(argv[0]);
    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值