OSG程序设计之更新回调

博客介绍了更新回调(Update Callback)涉及的osg::NodeCallback类,该类重载了函数调用操作符,回调动作发生时执行其内容。若节点绑定更新回调函数,系统每帧遍历到该节点时都会调用回调函数,还给出了相关例子链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  更新回调(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();
}

 

转载于:https://www.cnblogs.com/gattaca/p/4562885.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值