osgCallback的实现方法

来源:互联网 发布:hbase不直接删除数据 编辑:程序博客网 时间:2024/05/01 10:32
//更新遍历,重载操作符operator,当场景更新时,operator()的内容被调用,注意//需要添加traverse()以实现节点的继续遍历。可以参考src/osg/stateset.cpp,runUpdateCallBack()中的调用class UpdateCallback : public osg::NodeCallback{        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)        {             std::cout<<"update callback - pre traverse"<<node<<std::endl;            traverse(node,nv);            std::cout<<"update callback - post traverse"<<node<<std::endl;        }};//拣选遍历,重载操作符operator(),当场景执行拣选优化时,operator()被调用,注意//需要添加traverse()以实现节点的继续遍历,可以参考include/osgUtil/CullVisitor,//handle_cull_callbacks_and_traverse()中的调用过程。class CullCallback : public osg::NodeCallback{        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)        {             std::cout<<"cull callback - pre traverse"<<node<<std::endl;            traverse(node,nv);            std::cout<<"cull callback - post traverse"<<node<<std::endl;        }};//几何体绘制遍历,重载drawImplementation(),当几何体进行绘制时,函数的内容被调用,否则//将无法画出当前的几何形状。可以参考include/osg/Drawable,draw()中的调用过程class DrawableDrawCallback : public osg::Drawable::DrawCallback{        virtual void drawImplementation(osg::RenderInfo& renderInfo,const osg::Drawable* drawable) const        {            std::cout<<"draw call back - pre drawImplementation"<<drawable<<std::endl;            drawable->drawImplementation(renderInfo);            std::cout<<"draw call back - post drawImplementation"<<drawable<<std::endl;        }};//几何体更新遍历,重载函数update().当几何体更新时,其函数被调用。可以参考//include/osgUtil/UpdateVisitor, handle_geode_callback()中的调用过程。struct DrawableUpdateCallback : public osg::Drawable::UpdateCallback{    virtual void update(osg::NodeVisitor*, osg::Drawable* drawable)    {        std::cout<<"Drawable update callback "<<drawable<<std::endl;    }};//几何体拣选遍历,重载函数cull。当几何体进行拣选优化时,函数的内容被调用。可以参考//src/osgutil/cullVisitor, apply(Geode&)函数中的调用过程struct DrawableCullCallback : public osg::Drawable::CullCallback{    /** do customized cull code.*/    virtual bool cull(osg::NodeVisitor*, osg::Drawable* drawable, osg::State* /*state*/) const    {        std::cout<<"Drawable cull callback "<<drawable<<std::endl;        return false;    }};class InsertCallbacksVisitor : public osg::NodeVisitor{   public:           InsertCallbacksVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)        {        }                virtual void apply(osg::Node& node)        {             node.setUpdateCallback(new UpdateCallback());             node.setCullCallback(new CullCallback());             traverse(node);        }        virtual void apply(osg::Geode& geode)        {            geode.setUpdateCallback(new UpdateCallback());                        //note, it makes no sense to attach a cull callback to the node            //at there are no nodes to traverse below the geode, only            //drawables, and as such the Cull node callbacks is ignored.            //If you wish to control the culling of drawables            //then use a drawable cullback...            for(unsigned int i=0;i<geode.getNumDrawables();++i)            {                geode.getDrawable(i)->setUpdateCallback(new DrawableUpdateCallback());                geode.getDrawable(i)->setCullCallback(new DrawableCullCallback());                geode.getDrawable(i)->setDrawCallback(new DrawableDrawCallback());            }        }                virtual void apply(osg::Transform& node)        {            apply((osg::Node&)node);        }};class MyReadFileCallback : public osgDB::Registry::ReadFileCallback{public:    virtual osgDB::ReaderWriter::ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* options)    {        std::cout<<"before readNode"<<std::endl;        // note when calling the Registry to do the read you have to call readNodeImplementation NOT readNode, as this will        // cause on infinite recusive loop.        osgDB::ReaderWriter::ReadResult result = osgDB::Registry::instance()->readNodeImplementation(fileName,options);        std::cout<<"after readNode"<<std::endl;        return result;    }};class CameraUpdateCallback : public osg::NodeCallback{    virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)    {         std::cout<<"Camera update callback - pre traverse"<<node<<std::endl;        traverse(node,nv);        std::cout<<"Camera update callback - post traverse"<<node<<std::endl;    }};class CameraEventCallback : public osg::NodeCallback{    virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)    {         std::cout<<"Camera event callback - pre traverse"<<node<<std::endl;        traverse(node,nv);        std::cout<<"Camera event callback - post traverse"<<node<<std::endl;    }};int main( int argc, char **argv ){    // use an ArgumentParser object to manage the program arguments.    osg::ArgumentParser arguments(&argc,argv);       // set the osgDB::Registy read file callback to catch all requests for reading files.    osgDB::Registry::instance()->setReadFileCallback(new MyReadFileCallback());       // initialize the viewer.    osgViewer::Viewer viewer;    // load the nodes from the commandline arguments.    osg::Node* rootnode = osgDB::readNodeFiles(arguments);    // if not loaded assume no arguments passed in, try use default mode instead.    if (!rootnode) rootnode = osgDB::readNodeFile("cow.osgt");    if (!rootnode)    {        osg::notify(osg::NOTICE)<<"Please specify a file on the command line"<<std::endl;        return 1;    }        // run optimization over the scene graph    osgUtil::Optimizer optimzer;    optimzer.optimize(rootnode);         // insert all the callbacks    InsertCallbacksVisitor icv;    rootnode->accept(icv);    viewer.getCamera()->setUpdateCallback(new CameraUpdateCallback());    viewer.getCamera()->setEventCallback(new CameraEventCallback());    // set the scene to render    viewer.setSceneData(rootnode);    return viewer.run();}

  综述,可以得出回调的编写方法:

1、编写用户结构体,继承OSG中响应的虚函数结构图,如osg::NodeCallback;

2、重载回调执行函数,根据回调种类的不同,执行函数的名称也不同,可以参考osgCallBack中的设置

3、注意在回调执行的过程中,有一些必要的系统操作需要交由用户来完成,例如readNodeImplementation,Drawable::drawImplementation,traverse等,否则系统本身的渲染工作可能会不正常。

编译运行osgcallback之后,可以看到控制台不断输出各种回调的执行结果,用户可以根据自己的需要在不同的时间段进入不同的回调来完成所需的工作。    

参考文章地址:http://www.360doc.com/content/14/0619/23/12278894_388198735.shtml


OSG源码中回调函数的执行过程

//当你为你的节点设置了回调函数node.setUpdateCallback(new UpdateCallback());void ViewerBase::frame(double simulationTime){    updateTraversal();  /帧循环,更新回调}//回调函数的执行过程void Viewer::updateTraversal(){         .........    _scene->updateSceneGraph(*_updateVisitor);//_updateVisitor为osgUtil::UpdateVisitor,用来负责场景更新遍历         .........}void Scene::updateSceneGraph(osg::NodeVisitor& updateVisitor){     if (getSceneData())    {        updateVisitor.setImageRequestHandler(getImagePager());        getSceneData()->accept(updateVisitor);    }}void Node::accept(osg::NodeVisitor& nv) {    if (nv.validNodeMask(*this))   {      nv.pushOntoNodePath(this); nv.apply(*this); nv.popFromNodePath();    }     } void UpdateVisitor::apply(osg::Group& node){ handle_callbacks_and_traverse(node); }inline void handle_callbacks_and_traverse(osg::Node& node){     handle_callbacks(node.getStateSet());     osg::NodeCallback* callback = node.getUpdateCallback();     if (callback) (*callback)(&node,this);      else if (node.getNumChildrenRequiringUpdateTraversal()>0) traverse(node);}  <strong></strong>


0 0
原创粉丝点击