zoukankan      html  css  js  c++  java
  • OSG程序设计之更新回调

      更新回调(Update Callback)涉及到一个类:osg::NodeCallback。这个类重载了函数调用操作符。当回调动作发生时,将会执行这一操作符的内容。

      如果节点绑定了更新回调函数,那么在每一帧系统遍历到此节点时,回调函数都会被调用。

      下面给出一个例子:

    #include <osg/io_utils>
    #include <osg/PositionAttitudeTransform>
    #include <osgDB/ReadFile>
    #include <osgViewer/Viewer>
    #include <iostream>
    
    class RotateCallback: public osg::NodeCallback
    {
    public:
        RotateCallback():_rotateZ(0.0){}
    
        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
        {
            osg::PositionAttitudeTransform *pat = dynamic_cast<osg::PositionAttitudeTransform*>(node);
            if (pat)
            {
                osg::Quat quat(osg::DegreesToRadians(_rotateZ), osg::Z_AXIS);
                pat->setAttitude(quat);
                _rotateZ += 0.5;
            }
            traverse(node, nv);
        }
    protected:
        double _rotateZ;
    };
    
    class InfoCallback: public osg::NodeCallback
    {
    public:
    
        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
        {
            osg::PositionAttitudeTransform *pat = dynamic_cast<osg::PositionAttitudeTransform*>(node);
            if (pat)
            {
                double angle = 0.0;
                osg::Vec3 axis;
                pat->getAttitude().getRotate(angle, axis);
                std::cout<<"Node is rotating around the("<<axis<<")axis,"
                    <<osg::RadiansToDegrees(angle)<<" degrees"<<std::endl;
            }
            traverse(node, nv);
        }
    
    };
    
    int main(int argc, char **argv)
    {
        osg::ArgumentParser arguments(&argc, argv);
        osg::Node *model = osgDB::readNodeFiles(arguments);
        if(!model)
            model = osgDB::readNodeFile("cow.osg");
        osg::ref_ptr<osg::PositionAttitudeTransform> pat = new osg::PositionAttitudeTransform;
        pat->addChild(model);
        pat->setUpdateCallback(new RotateCallback);
        pat->addUpdateCallback(new InfoCallback);
        osgViewer::Viewer viewer;
        viewer.setSceneData(pat.get());
        return viewer.run();
    }
  • 相关阅读:
    jQuery 集合 搜索操作(父辈元素搜索、同辈元素搜索、子元素搜索)
    struts <s:form action=""> 和 <s:submit action=""> 的区别
    654. 最大二叉树
    701. 二叉搜索树中的插入操作
    617. 合并二叉树
    98. 验证二叉搜索树
    236. 二叉树的最近公共祖先
    700. 二叉搜索树中的搜索
    235. 二叉搜索树的最近公共祖先
    105. 从前序与中序遍历序列构造二叉树
  • 原文地址:https://www.cnblogs.com/gattaca/p/4562885.html
Copyright © 2011-2022 走看看