zoukankan      html  css  js  c++  java
  • OSG学习:转动的小汽车示例

     由于只是简单的示例,所以小汽车的模型也比较简单,是由简单的几何体组成。

    代码如下:

    #include <osgShapeDrawable>
    #include <osgAnimationPath>
    #include <osgMatrixTransform>
    #include<osgDBReadFile>
    #include<osgViewerViewer>
    
    osg::MatrixTransform* createTransformNode(osg::Drawable* shape, const osg::Matrix& matrix)
    {
    	osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    	geode->addDrawable(shape);
    
    	osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform;
    	trans->addChild(geode.get());
    	trans->setMatrix(matrix);
    	return trans.release();
    }
    
    osg::AnimationPathCallback* createWheelAnimation(const osg::Vec3& base)
    {
    	osg::ref_ptr<osg::AnimationPath> wheelPath = new osg::AnimationPath;
    	wheelPath->setLoopMode(osg::AnimationPath::LOOP);
    	wheelPath->insert(0.0, osg::AnimationPath::ControlPoint(base, osg::Quat()));
    	wheelPath->insert(0.01, osg::AnimationPath::ControlPoint(base + osg::Vec3(0.0f, 0.02f, 0.0f), osg::Quat(osg::PI_2, osg::Z_AXIS)));
    	wheelPath->insert(0.02, osg::AnimationPath::ControlPoint(base + osg::Vec3(0.0f, -0.02f, 0.0f), osg::Quat(osg::PI, osg::Z_AXIS)));
    	
    	osg::ref_ptr<osg::AnimationPathCallback> apcb = new osg::AnimationPathCallback;
    	apcb->setAnimationPath(wheelPath.get());
    	return apcb.release();
    }
    
    int main(int argc, char** argv)
    {
    	osg::ref_ptr<osg::ShapeDrawable> mainRodShape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(), 0.4f, 10.0f));
    	osg::ref_ptr<osg::ShapeDrawable> wheelRodShape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(), 0.4f, 8.0f));
    	osg::ref_ptr<osg::ShapeDrawable> wheelShape = new osg::ShapeDrawable(new osg::Cylinder(osg::Vec3(), 2.0f, 1.0f));
    	osg::ref_ptr<osg::ShapeDrawable> bodyShape = new osg::ShapeDrawable(new osg::Box(osg::Vec3(), 6.0f, 4.0f, 14.0f));
    
    	osg::MatrixTransform* wheel1 = createTransformNode(wheelShape.get(), osg::Matrix::translate(0.0f, 0.0f, -4.0f));
    	wheel1->setUpdateCallback(createWheelAnimation(osg::Vec3(0.0f, 0.0f, -4.0f)));
    	osg::MatrixTransform* wheel2 = createTransformNode(wheelShape.get(), osg::Matrix::translate(0.0f, 0.0f, 4.0f));
    	wheel2->setUpdateCallback(createWheelAnimation(osg::Vec3(0.0f, 0.0f, 4.0f)));
    
    	osg::MatrixTransform* wheelRod1 = createTransformNode(wheelRodShape.get(),
    		osg::Matrix::rotate(osg::Z_AXIS, osg::X_AXIS)*
    		osg::Matrix::translate(0.0f, 0.0f, -5.0f));
    	wheelRod1->addChild(wheel1);
    	wheelRod1->addChild(wheel2);
    
    	osg::MatrixTransform* wheelRod2 = static_cast<osg::MatrixTransform*>(wheelRod1->clone(osg::CopyOp::SHALLOW_COPY));
    	wheelRod2->setMatrix(osg::Matrix::rotate(osg::Z_AXIS, osg::X_AXIS)*osg::Matrix::translate(0.0f, 0.0f, 5.0f));
    
    	osg::MatrixTransform* body = createTransformNode(bodyShape.get(), osg::Matrix::translate(0.0f, 2.2f, 0.0f));
    
    
    	osg::MatrixTransform* mainRod = createTransformNode(mainRodShape.get(), osg::Matrix::identity());
    	mainRod->addChild(wheelRod1);
    	mainRod->addChild(wheelRod2);
    	mainRod->addChild(body);
    
    	osg::ref_ptr<osg::Group> root = new osg::Group;
    	root->addChild(mainRod);
    
    	osgViewer::Viewer viewer;
    	viewer.setSceneData(root.get());
    	return viewer.run();
    }
    结果图:



  • 相关阅读:
    Centos7安装Docker
    [LeetCode] 651. 四键键盘 ☆☆☆(动态规划)
    一行代码就能解决的算法题
    博弈问题--石头游戏(动态规划)
    [LeetCode] 322. 零钱兑换 ☆☆☆(动态规划)
    java趣题
    [LeetCode] 516. 最长回文子序列 ☆☆☆(动态规划)
    [LeetCode] 337. 打家劫舍III ☆☆☆(动态规划)
    算法基础--贪心算法
    [LeetCode] 42. 接雨水 ☆☆☆☆☆(按列、动态规划、双指针)
  • 原文地址:https://www.cnblogs.com/huahai/p/7270941.html
Copyright © 2011-2022 走看看