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();
}
时间: 2024-08-29 06:07:14

OSG程序设计之更新回调的相关文章

OSG使用更新回调来更改模型

OSG使用更新回调来更改模型 转自:http://blog.sina.com.cn/s/blog_668aae7801017gl7.html 使用回调类实现对场景图形节点的更新.本节将讲解如何使用回调来实现在每帧的更新遍历(update traversal)中进行节点的更新. 回调概览用户可以使用回调来实现与场景图形的交互.回调可以被理解成是一种用户自定义的函数,根据遍历方式的不同(更新update,拣选cull,绘制draw),回调函数将自动地执行.回调可以与个别的节点或者选定类型(及子类型)

OSG程序设计之Hello World

对于从未接触过OSG的我来说,首先需要一个入门教程.在OSG论坛逛了半天,再加上google,最终决定使用<OSG程序设计>这本书. 下面就贴出书中的第一个例子:Hello World. 使用vs2010新建一个win32控制台应用程序,然后配置一下osg环境,接下来就可以上代码了. //库文件:osgDBd.lib.osgViewerd.lib #include <osgDB/ReadFile> #include <osgViewer/Viewer> void mai

vuex的dom更新回调问题

https://segmentfault.com/q/1010000007359564 根据vue的响应式原理,多次的数据操作之后进行一次的dom更新,所以可以使用$nextTick在dom更新后做些什么. 但是今天我使用了vuex来管理应用状态,我在组件中发了一个dispatch来触发某个action,action又触发mutations来改变状态,但是在vuex中没有nextTick这个东西,我无法在dom更新后做些什么 在组件写的nextTick也不是在vuex更新状态后触发的,而是在组件

OSG程序设计之osg::NodeVisitor

本文所有内容来自<OpenSceneGraph三维渲染引擎设计与实践>一书. 本文主要讨论的是OSG中节点的访问. 对于节点的访问是从节点接收一个访问器开始的,用户执行某个节点的accept()函数,将一个具体的访问器对象传递给节点. 第二步,节点反过来执行访问器的apply()函数,并将自身传入访问器. 这两步的实现过程可以用一行十分简单的函数代码来表达: void Node::accept(NodeVisitor& nv) { nv.apply(*this); } 下面是一个具体的

OSG程序设计之Hello World 4.0

代码如下: //需要添加两个库:osgUtild.lib.osgTextd.lib #include <osgDB/ReadFile> #include <osgUtil/Optimizer> #include <osg/CoordinateSystemNode> #include <osg/Switch> #include <osgText/Text> #include <osgViewer/Viewer> #include <

OSG程序设计之osg::Group

以下是一个简单的模型读取程序: #include <osgDB/ReadFile> #include <osgViewer/Viewer> #include <osg/Node> void main() { osgViewer::Viewer viewer; osg::Group *root = new osg::Group(); root->addChild(osgDB::readNodeFile("glider.osg")); root-&

OSG程序设计之Hello World 2.0

现在为Hello World添加一些键盘响应事件. //需要多添加两个库:osgGAd.lib.osgd.lib 代码如下: #include <osgDB/ReadFile> #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #include <osgGA/StateSetManipulator> void main() { osgViewer::Viewer view

OSG节点更新与事件回调

OSG中的节点主要使用回调(CallBack)来完成用户临时.需要每帧执行的工作.根据回调功能被调用的时机划分为更新回调(Update CallBack)和人机交互时间回调(Event CallBack).前者在每一帧中系统遍历到当前节点时调用,后者则由交互事件触发,如操作键盘.鼠标.关闭窗口.改变窗口大小等动作.回调类基类是osg::NodeCallBack(),主要函数如下: //虚函数,回调函数主要操作在此函数中,子类应当重写,已完成相应操作 void operator()(Node* n

[osg]osgcallback各种回调使用的例子介绍

观察MyReadFileCallback结构体的内容,可以发现它继承自osgDB::Registry::ReadFileCallback,并重载了一个函数readNode,分析源代码可知,该函数在osgDB::readNodeFile函数中被调用,即,在加载模型文件时,即会调用 MyReadFileCallback结构体所重载的readNode函数并执行相应的内容.示例程序中该函数的内容如下:class MyReadFileCallback : public osgDB::Registry::R