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