Osg-Osg利用NodeCallback实现对象旋转(Qt5.14.2+osgE3.6.5+win10)-No27-NodeCallback
相关资料:
https://www.cnblogs.com/k5bg/p/11465921.html
实例代码:
.pro
1 QT += core gui widgets 2 QT += opengl 3 TARGET = TestOsgQt 4 TEMPLATE = app 5 DEFINES += QT_DEPRECATED_WARNINGS 6 CONFIG += c++11 7 8 SOURCES += \ 9 main.cpp 10 11 HEADERS += 12 13 OsgDir = D:\\Gitee\\osg365R 14 CONFIG(release, debug|release) { 15 LIBS += -L$${OsgDir}/lib/ -losgDB -losgViewer -lOpenThreads -losgAnimation -losg \ 16 -losgEarth -losgEarthAnnotation -losgEarthFeatures -losgEarthSymbology -losgEarthUtil \ 17 -losgQOpenGL -losgUtil -losgText -losgTerrain -losgSim \ 18 -losgShadow -losgParticle -losgManipulator -losgGA -losgFX \ 19 -losgWidget 20 } else { 21 LIBS += -L$${OsgDir}/debug/lib/ -losgDBd -losgViewerd -lOpenThreadsd -losgAnimationd -losgd \ 22 -losgEarthd -losgEarthAnnotationd -losgEarthFeaturesd -losgEarthSymbologyd -losgEarthUtild \ 23 -losgQOpenGLd -losgUtild -losgTextd -losgTerraind -losgSimd \ 24 -losgShadowd -losgParticled -losgManipulatord -losgGAd -losgFXd \ 25 } 26 27 LIBS += -lOpenGL32 28 LIBS += -lGlU32 29 30 INCLUDEPATH += $${OsgDir}/include 31 DEPENDPATH += $${OsgDir}/include
main.cpp
1 #include <osgViewer/Viewer> 2 3 #include <osg/Node> 4 #include <osg/Geode> 5 #include <osg/Group> 6 #include <osg/NodeCallback> 7 #include <osg/MatrixTransform> 8 9 #include <osgDB/ReadFile> 10 #include <osgDB/WriteFile> 11 12 #include <osgUtil/Optimizer> 13 14 //从 osg::NodeCallback 继承一个新类,实现 spaceship 的回调 15 class SpaceShipCallback : public osg::NodeCallback 16 { 17 public: 18 SpaceShipCallback():angle(0) 19 { 20 } 21 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) 22 { 23 //创建矩阵变换节点 24 osg::ref_ptr<osg::MatrixTransform> mtCow = dynamic_cast<osg::MatrixTransform*>(node); 25 //创建矩阵 26 osg::Matrix mr; 27 //旋转绕Z轴 每次旋转angle角度 28 mr.makeRotate(angle, osg::Vec3(0.0f, 0.0f, 1.0f)); 29 //设置矩阵 30 mtCow->setMatrix(mr); 31 angle += 0.01; 32 //继续传递参数,执行其他设置回调的节点 33 traverse(node, nv); 34 } 35 public: 36 //旋转角度 37 double angle; 38 }; 39 40 int main() 41 { 42 osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(); 43 osg::ref_ptr<osg::Group> root = new osg::Group(); 44 //读取奶牛模型 45 osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("D:\\Gitee\\data\\cow.osg"); 46 //创建矩阵变换节点 47 osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform(); 48 mt->addChild(node.get()); 49 //设置更新回调 50 mt->setUpdateCallback(new SpaceShipCallback()); 51 root->addChild(mt.get()); 52 //优化场景数据 53 osgUtil::Optimizer optimizer; 54 optimizer.optimize(root.get()); 55 viewer->setSceneData(root.get()); 56 viewer->setUpViewInWindow(50,50,500,400); 57 viewer->realize(); 58 viewer->run(); 59 60 return 0; 61 }
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我