osgEarth示例分析——osgearth_sequencecontrol

前言

osgearth_sequencecontrol示例,应该是会实现每一帧更新一个东西,但是尝试了很多earth文件,都报错:Your earth file does not contain any sequenced layers...bye! 如果哪位大佬有可以运行的earth文件,烦请告知!

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

会输出:current layer name: GlobeImage

代码分析

//在osgearth_scenegraphcallbacks示例中,调用:
osgEarth::LayerVector layers;// 获取图层列表
mapNode->getMap()->getLayers(layers);

//在osgearth_sequencecontrol示例中,调用:
osgEarth::ImageLayerVector layers;// 获取图层列表
mapNode->getMap()->getLayers( layers );

关于layer的继承关系:

osgEarth示例分析——osgearth_sequencecontrol_第1张图片

#include 
#include 
#include 
#include 
#include 
#include 

namespace ui = osgEarth::Util::Controls;


int
usage(char** argv, const char* msg)
{
    OE_WARN << argv[0] << ": " << msg << std::endl;
    return -1;
}


// Updates the timestamp label once per frame.
// 一帧更新一次时间戳标签
struct UpdateLabel : public osg::Operation
{
    osgEarth::SequenceControl* _sc;
    ui::LabelControl*          _label;
    unsigned                   _prevIndex;

    UpdateLabel(osgEarth::SequenceControl* sc, ui::LabelControl* label)
        : osg::Operation("updatelabel", true), _sc(sc), _label(label), _prevIndex(INT_MAX) { }

    void operator()(osg::Object* obj)
    {
        osgViewer::View* view = dynamic_cast(obj);
        unsigned index = _sc->getCurrentSequenceFrameIndex(view->getFrameStamp());
        if ( index != _prevIndex )
        {
            const std::vector& frames = _sc->getSequenceFrameInfo();
            _label->setText( frames[index].timeIdentifier );// 标签上的内容被更新
            _prevIndex = index;
        }
    }
};


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

    // load an earth file from the command line.
    osg::Node* node = osgEarth::Util::MapNodeHelper().load( arguments, &viewer );
    osgEarth::MapNode* mapNode = osgEarth::MapNode::get(node);
    if ( !mapNode )
        return usage(argv, "Unable to load an earth file.");
    viewer.setSceneData( node );

    // find the first layer with sequence control.
	// 找到具有顺序控制的第一层。
    bool found = false;
    osgEarth::ImageLayerVector layers;
    mapNode->getMap()->getLayers( layers );
    for( osgEarth::ImageLayerVector::const_iterator i = layers.begin(); i != layers.end(); ++i )
    {
        // get the sequence control interface for the layer, if there is one.
		// 获取到图层 顺序控制接口
        osgEarth::ImageLayer*      layer = i->get();
        osgEarth::SequenceControl* sc = layer->getSequenceControl();

		std::string str = layer->getName();
		std::cout << "current layer name: "<setFontSize( 24.0f );
            label->setBackColor ( 0, 0, 0, 0.5 );
            label->setHorizAlign( ui::Control::ALIGN_CENTER );
            label->setVertAlign ( ui::Control::ALIGN_TOP );
            ui::ControlCanvas::getOrCreate( &viewer )->addControl( label );

            // make sure the sequence is playing:
            sc->playSequence();

            // add a callback to update the label with the sequence time id
            viewer.addUpdateOperation( new UpdateLabel(sc, label) );
            found = true;
            break;// 找到第一个图层即可
        }
    }

    if (!found)
    {
        return usage(argv, "Your earth file does not contain any sequenced layers...bye!");
    }

    return viewer.run();
}

你可能感兴趣的:(osgEarth,c++)