OSG程式設計之更新回調

來源:互聯網
上載者:User

標籤:

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

 

OSG程式設計之更新回調

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.