• 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();
    }
    结果图:



  • 相关阅读:
    实验2四则运算结对
    作业5 四则运算 测试与封装 5.1
    0909我对编译的看法
    P2602 [ZJOI2010]数字计数(递推+数位dp写法)
    模数的世界[数论]
    P2312[秦九韶+读入取模+哈希解方程]
    第三章 Python 的容器: 列表、元组、字典与集合
    第二章 Python 基本元素:数字、字符串、变量
    第一章 Python 之初探
    第四章 Python 外壳 :代码结构
  • 原文地址:https://www.cnblogs.com/huahai/p/7270941.html
Copyright © 2020-2023  润新知