在第四课中我们使用节点更新回调的方式实现了对场景中节点的旋转,在OSG中还有更简便的方法实现这种效果,下面用另一种方式实现第四课的内容。我们使用OSG中自带的AnimationPathCallback实现旋转的效果
要理解osg::AnimationPathCallback我们首先需要知道什么是AnimationPath?OSG通过AnimationPath记录某些时刻节点的位置和姿态信息(称为ControlPoint),并通过插值得出给定时刻节点的位置和姿态信息,从而完成动画的效果。AnimationPathCallback提供了一个构造函数来实现简单的旋转动画,正好可以实现我们需要的效果。
AnimationPathCallback(const osg::Vec3d& pivot,const osg::Vec3d& axis,float angularVelocity);
osg::MatrixTransform *triangleMT = new osg::MatrixTransform; triangleMT->setMatrix(osg::Matrix::translate(-1.5, 0.0, -6.0)); osg::AnimationPathCallback *triangleAnimationCallback = new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Y_AXIS, osg::RadiansToDegrees(0.2)); osg::MatrixTransform *triangleRotMT = new osg::MatrixTransform; triangleRotMT->setUpdateCallback(triangleAnimationCallback);
triangleGeode->addDrawable(triangleGeometry); triangleRotMT->addChild(triangleGeode); triangleMT->addChild(triangleRotMT);
osg::MatrixTransform *quadMT = new osg::MatrixTransform; quadMT->setMatrix(osg::Matrix::translate(1.5, 0.0, -6.0)); osg::AnimationPathCallback *quadAnimationCallback = new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::X_AXIS, osg::RadiansToDegrees(0.15)); osg::MatrixTransform *quadRotMT = new osg::MatrixTransform; quadRotMT->setUpdateCallback(quadAnimationCallback);
quadGeode->addDrawable(quadGeometry); quadRotMT->addChild(quadGeode); quadMT->addChild(quadRotMT);
附:本课源码(源码中可能存在错误和不足,仅供参考)
#include "../osgNeHe.h" #include <QtCore/QTimer> #include <QtGui/QApplication> #include <QtGui/QVBoxLayout> #include <osgViewer/Viewer> #include <osgDB/ReadFile> #include <osgQt/GraphicsWindowQt> #include <osg/MatrixTransform> #include <osg/NodeVisitor> #include <osg/AnimationPath> class ViewerWidget : public QWidget, public osgViewer::Viewer { public: ViewerWidget(osg::Node *scene = NULL) { QWidget* renderWidget = getRenderWidget( createGraphicsWindow(0,0,100,100), scene); QVBoxLayout* layout = new QVBoxLayout; layout->addWidget(renderWidget); layout->setContentsMargins(0, 0, 0, 1); setLayout( layout ); connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) ); _timer.start( 10 ); } QWidget* getRenderWidget( osgQt::GraphicsWindowQt* gw, osg::Node* scene ) { osg::Camera* camera = this->getCamera(); camera->setGraphicsContext( gw ); const osg::GraphicsContext::Traits* traits = gw->getTraits(); camera->setClearColor( osg::Vec4(0.0, 0.0, 0.0, 1.0) ); camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) ); camera->setProjectionMatrixAsPerspective(45.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 0.1f, 100.0f ); camera->setViewMatrixAsLookAt(osg::Vec3d(0, 0, 1), osg::Vec3d(0, 0, 0), osg::Vec3d(0, 1, 0)); this->setSceneData( scene ); return gw->getGLWidget(); } osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false ) { osg::DisplaySettings* ds = osg::DisplaySettings::instance().get(); osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; traits->windowName = name; traits->windowDecoration = windowDecoration; traits->x = x; traits->y = y; traits->width = w; traits->height = h; traits->doubleBuffer = true; traits->alpha = ds->getMinimumNumAlphaBits(); traits->stencil = ds->getMinimumNumStencilBits(); traits->sampleBuffers = ds->getMultiSamples(); traits->samples = ds->getNumMultiSamples(); return new osgQt::GraphicsWindowQt(traits.get()); } virtual void paintEvent( QPaintEvent* event ) { frame(); } protected: QTimer _timer; }; ////////////////////////////////////////////////////////////////////////// osg::Node* buildScene() { osg::Group *root = new osg::Group; osg::MatrixTransform *triangleMT = new osg::MatrixTransform; triangleMT->setMatrix(osg::Matrix::translate(-1.5, 0.0, -6.0)); osg::AnimationPathCallback *triangleAnimationCallback = new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::Y_AXIS, osg::RadiansToDegrees(0.2)); osg::MatrixTransform *triangleRotMT = new osg::MatrixTransform; triangleRotMT->setUpdateCallback(triangleAnimationCallback); osg::Geometry *triangleGeometry = new osg::Geometry; osg::Vec3Array *triangleVertexArray = new osg::Vec3Array; triangleVertexArray->push_back(osg::Vec3(0.0f, 1.0f, 0.0f)); triangleVertexArray->push_back(osg::Vec3(-1.0f,-1.0f, 0.0f)); triangleVertexArray->push_back(osg::Vec3(1.0f,-1.0f, 0.0f)); triangleGeometry->setVertexArray(triangleVertexArray); osg::Vec4Array *triangleColorArray = new osg::Vec4Array; triangleColorArray->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f)); triangleColorArray->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f)); triangleColorArray->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f)); triangleGeometry->setColorArray(triangleColorArray); triangleGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); triangleGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, 3)); osg::Geode *triangleGeode = new osg::Geode; triangleGeode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); triangleGeode->addDrawable(triangleGeometry); triangleRotMT->addChild(triangleGeode); triangleMT->addChild(triangleRotMT); //四边形部分 osg::MatrixTransform *quadMT = new osg::MatrixTransform; quadMT->setMatrix(osg::Matrix::translate(1.5, 0.0, -6.0)); osg::AnimationPathCallback *quadAnimationCallback = new osg::AnimationPathCallback(osg::Vec3d(0, 0, 0), osg::X_AXIS, osg::RadiansToDegrees(0.15)); osg::MatrixTransform *quadRotMT = new osg::MatrixTransform; quadRotMT->setUpdateCallback(quadAnimationCallback); osg::Geometry *quadGeometry = new osg::Geometry; osg::Vec3Array *quadVertexArray = new osg::Vec3Array; quadVertexArray->push_back(osg::Vec3(-1.0f, 1.0f, 0.0f)); quadVertexArray->push_back(osg::Vec3(1.0f, 1.0f, 0.0f)); quadVertexArray->push_back(osg::Vec3(1.0f,-1.0f, 0.0f)); quadVertexArray->push_back(osg::Vec3(-1.0f,-1.0f, 0.0f)); quadGeometry->setVertexArray(quadVertexArray); osg::Vec4Array *quadColorArray = new osg::Vec4Array; quadColorArray->push_back(osg::Vec4(0.5f,0.5f,1.0f,1.0f)); quadGeometry->setColorArray(quadColorArray); quadGeometry->setColorBinding(osg::Geometry::BIND_OVERALL); osg::DrawElementsUByte *vertexIndices = new osg::DrawElementsUByte(osg::PrimitiveSet::QUADS, 4); vertexIndices->push_back(0); vertexIndices->push_back(1); vertexIndices->push_back(2); vertexIndices->push_back(3); quadGeometry->addPrimitiveSet(vertexIndices); osg::Geode *quadGeode = new osg::Geode; quadGeode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); quadGeode->addDrawable(quadGeometry); quadRotMT->addChild(quadGeode); quadMT->addChild(quadRotMT); root->addChild(triangleMT); root->addChild(quadMT); return root; } int main( int argc, char** argv ) { QApplication app(argc, argv); ViewerWidget* viewWidget = new ViewerWidget(buildScene()); viewWidget->setGeometry( 100, 100, 640, 480 ); viewWidget->show(); return app.exec(); }