OpenSceneGraph实现的NeHe OpenGL教程 - 第四课_附录1

  • 简介

在第四课中我们使用节点更新回调的方式实现了对场景中节点的旋转,在OSG中还有更简便的方法实现这种效果,下面用另一种方式实现第四课的内容。我们使用OSG中自带的AnimationPathCallback实现旋转的效果


  • 实现

要理解osg::AnimationPathCallback我们首先需要知道什么是AnimationPath?OSG通过AnimationPath记录某些时刻节点的位置和姿态信息(称为ControlPoint),并通过插值得出给定时刻节点的位置和姿态信息,从而完成动画的效果。AnimationPathCallback提供了一个构造函数来实现简单的旋转动画,正好可以实现我们需要的效果。

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. AnimationPathCallback(const osg::Vec3d& pivot,const osg::Vec3d& axis,float angularVelocity);  

首先构造对三角形进行旋转的Callback,代码如下:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. osg::MatrixTransform *triangleMT = new osg::MatrixTransform;  
  2. triangleMT->setMatrix(osg::Matrix::translate(-1.5, 0.0, -6.0));  
  3.   
  4. osg::AnimationPathCallback *triangleAnimationCallback =   
  5.     new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Y_AXIS, osg::RadiansToDegrees(0.2));  
  6. osg::MatrixTransform *triangleRotMT = new osg::MatrixTransform;  
  7. triangleRotMT->setUpdateCallback(triangleAnimationCallback);  

需要注意的是AnimationPathCallback会一直修改它节点MatrixTransform的变换矩阵,因此我们单独使用一个旋转空间变换节点来实现旋转的效果从而保证平移的效果一直存在,因此三角形Geode需要挂接到RotMT下面

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. triangleGeode->addDrawable(triangleGeometry);  
  2. triangleRotMT->addChild(triangleGeode);  
  3. triangleMT->addChild(triangleRotMT);  

以下四边形的代码与上面类似,不再赘述

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. osg::MatrixTransform *quadMT = new osg::MatrixTransform;  
  2. quadMT->setMatrix(osg::Matrix::translate(1.5, 0.0, -6.0));  
  3.   
  4. osg::AnimationPathCallback *quadAnimationCallback =   
  5.     new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::X_AXIS, osg::RadiansToDegrees(0.15));  
  6. osg::MatrixTransform *quadRotMT = new osg::MatrixTransform;  
  7. quadRotMT->setUpdateCallback(quadAnimationCallback);  

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. quadGeode->addDrawable(quadGeometry);  
  2. quadRotMT->addChild(quadGeode);  
  3. quadMT->addChild(quadRotMT);  

编译并运行程序,得到和第四课中类似的效果

附:本课源码(源码中可能存在错误和不足,仅供参考)

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "../osgNeHe.h"  
  2.   
  3. #include <QtCore/QTimer>  
  4. #include <QtGui/QApplication>  
  5. #include <QtGui/QVBoxLayout>  
  6.   
  7. #include <osgViewer/Viewer>  
  8. #include <osgDB/ReadFile>  
  9. #include <osgQt/GraphicsWindowQt>  
  10.   
  11. #include <osg/MatrixTransform>  
  12. #include <osg/NodeVisitor>  
  13. #include <osg/AnimationPath>  
  14.   
  15.   
  16. class ViewerWidget : public QWidget, public osgViewer::Viewer  
  17. {  
  18. public:  
  19.     ViewerWidget(osg::Node *scene = NULL)  
  20.     {  
  21.         QWidget* renderWidget = getRenderWidget( createGraphicsWindow(0,0,100,100), scene);  
  22.   
  23.         QVBoxLayout* layout = new QVBoxLayout;  
  24.         layout->addWidget(renderWidget);  
  25.         layout->setContentsMargins(0, 0, 0, 1);  
  26.         setLayout( layout );  
  27.   
  28.         connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );  
  29.         _timer.start( 10 );  
  30.     }  
  31.   
  32.     QWidget* getRenderWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene )  
  33.     {  
  34.         osg::Camera* camera = this->getCamera();  
  35.         camera->setGraphicsContext( gw );  
  36.   
  37.         const osg::GraphicsContext::Traits* traits = gw->getTraits();  
  38.   
  39.         camera->setClearColor( osg::Vec4(0.0, 0.0, 0.0, 1.0) );  
  40.         camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );  
  41.         camera->setProjectionMatrixAsPerspective(45.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 0.1f, 100.0f );  
  42.         camera->setViewMatrixAsLookAt(osg::Vec3d(0, 0, 1), osg::Vec3d(0, 0, 0), osg::Vec3d(0, 1, 0));  
  43.   
  44.         this->setSceneData( scene );  
  45.   
  46.         return gw->getGLWidget();  
  47.     }  
  48.   
  49.     osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name=""bool windowDecoration=false )  
  50.     {  
  51.         osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();  
  52.         osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;  
  53.         traits->windowName = name;  
  54.         traits->windowDecoration = windowDecoration;  
  55.         traits->x = x;  
  56.         traits->y = y;  
  57.         traits->width = w;  
  58.         traits->height = h;  
  59.         traits->doubleBuffer = true;  
  60.         traits->alpha = ds->getMinimumNumAlphaBits();  
  61.         traits->stencil = ds->getMinimumNumStencilBits();  
  62.         traits->sampleBuffers = ds->getMultiSamples();  
  63.         traits->samples = ds->getNumMultiSamples();  
  64.   
  65.         return new osgQt::GraphicsWindowQt(traits.get());  
  66.     }  
  67.   
  68.     virtual void paintEvent( QPaintEvent* event )  
  69.     {   
  70.         frame();   
  71.     }  
  72.   
  73. protected:  
  74.   
  75.     QTimer _timer;  
  76. };  
  77.   
  78.   
  79.   
  80. //  
  81.   
  82.   
  83.   
  84. osg::Node*  buildScene()  
  85. {  
  86.     osg::Group *root = new osg::Group;  
  87.   
  88.     osg::MatrixTransform *triangleMT = new osg::MatrixTransform;  
  89.     triangleMT->setMatrix(osg::Matrix::translate(-1.5, 0.0, -6.0));  
  90.   
  91.     osg::AnimationPathCallback *triangleAnimationCallback =   
  92.         new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Y_AXIS, osg::RadiansToDegrees(0.2));  
  93.     osg::MatrixTransform *triangleRotMT = new osg::MatrixTransform;  
  94.     triangleRotMT->setUpdateCallback(triangleAnimationCallback);  
  95.   
  96.     osg::Geometry *triangleGeometry = new osg::Geometry;  
  97.     osg::Vec3Array *triangleVertexArray = new osg::Vec3Array;  
  98.     triangleVertexArray->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));  
  99.     triangleVertexArray->push_back(osg::Vec3(-1.0f,-1.0f, 0.0f));  
  100.     triangleVertexArray->push_back(osg::Vec3(1.0f,-1.0f, 0.0f));  
  101.     triangleGeometry->setVertexArray(triangleVertexArray);  
  102.   
  103.     osg::Vec4Array *triangleColorArray = new osg::Vec4Array;  
  104.     triangleColorArray->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f));  
  105.     triangleColorArray->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f));  
  106.     triangleColorArray->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));  
  107.     triangleGeometry->setColorArray(triangleColorArray);  
  108.     triangleGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);  
  109.   
  110.     triangleGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, 3));  
  111.     osg::Geode *triangleGeode = new osg::Geode;  
  112.   
  113.     triangleGeode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  
  114.     triangleGeode->addDrawable(triangleGeometry);  
  115.     triangleRotMT->addChild(triangleGeode);  
  116.     triangleMT->addChild(triangleRotMT);  
  117.   
  118.     //四边形部分  
  119.     osg::MatrixTransform *quadMT = new osg::MatrixTransform;  
  120.     quadMT->setMatrix(osg::Matrix::translate(1.5, 0.0, -6.0));  
  121.   
  122.     osg::AnimationPathCallback *quadAnimationCallback =   
  123.         new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::X_AXIS, osg::RadiansToDegrees(0.15));  
  124.     osg::MatrixTransform *quadRotMT = new osg::MatrixTransform;  
  125.     quadRotMT->setUpdateCallback(quadAnimationCallback);  
  126.   
  127.     osg::Geometry *quadGeometry = new osg::Geometry;  
  128.     osg::Vec3Array *quadVertexArray = new osg::Vec3Array;  
  129.     quadVertexArray->push_back(osg::Vec3(-1.0f, 1.0f, 0.0f));  
  130.     quadVertexArray->push_back(osg::Vec3(1.0f, 1.0f, 0.0f));  
  131.     quadVertexArray->push_back(osg::Vec3(1.0f,-1.0f, 0.0f));  
  132.     quadVertexArray->push_back(osg::Vec3(-1.0f,-1.0f, 0.0f));  
  133.     quadGeometry->setVertexArray(quadVertexArray);  
  134.   
  135.     osg::Vec4Array *quadColorArray = new osg::Vec4Array;  
  136.     quadColorArray->push_back(osg::Vec4(0.5f,0.5f,1.0f,1.0f));  
  137.     quadGeometry->setColorArray(quadColorArray);  
  138.     quadGeometry->setColorBinding(osg::Geometry::BIND_OVERALL);  
  139.   
  140.     osg::DrawElementsUByte *vertexIndices = new osg::DrawElementsUByte(osg::PrimitiveSet::QUADS, 4);  
  141.     vertexIndices->push_back(0);  
  142.     vertexIndices->push_back(1);  
  143.     vertexIndices->push_back(2);  
  144.     vertexIndices->push_back(3);  
  145.     quadGeometry->addPrimitiveSet(vertexIndices);  
  146.     osg::Geode *quadGeode = new osg::Geode;  
  147.     quadGeode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);  
  148.     quadGeode->addDrawable(quadGeometry);  
  149.     quadRotMT->addChild(quadGeode);  
  150.     quadMT->addChild(quadRotMT);  
  151.   
  152.     root->addChild(triangleMT);  
  153.     root->addChild(quadMT);  
  154.   
  155.     return root;  
  156. }  
  157.   
  158.   
  159.   
  160. int main( int argc, char** argv )  
  161. {  
  162.     QApplication app(argc, argv);  
  163.     ViewerWidget* viewWidget = new ViewerWidget(buildScene());  
  164.     viewWidget->setGeometry( 100, 100, 640, 480 );  
  165.     viewWidget->show();  
  166.     return app.exec();  
  167. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值