OSG学习:转动的小汽车示例
来源:互联网 发布:大数据框架hadoop 编辑:程序博客网 时间:2024/04/29 15:24
由于只是简单的示例,所以小汽车的模型也比较简单,是由简单的几何体组成。
代码如下:
#include <osg\ShapeDrawable>#include <osg\AnimationPath>#include <osg\MatrixTransform>#include<osgDB\ReadFile>#include<osgViewer\Viewer>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();}结果图:
阅读全文
0 0
- OSG学习:转动的小汽车示例
- OSG学习:使用已有回调示例
- OSG学习:阴影代码示例
- 1070: 小汽车的位置
- OSG学习:位置变换节点示例
- OSG学习:矩阵变换节点示例
- OSG学习:自动对齐节点示例
- OSG学习:基本几何体绘制示例
- OSG学习:响应键盘鼠标示例
- OSG学习笔记01:OSG的安装
- 【OSG】运行OSG示例出现的奶牛不完整问题
- 【Unity学习系列之一】 转动的Cube
- OSG学习之示例一遇到的错误:丢失osg80-osgDB.dll
- OSG学习之示例遇到的问题二图形不能显示网格模式
- OSG学习之示例遇到的问题三无法打开文件osgGA/MatrixManipulator错误
- OSG学习之示例遇到的问题三无法打开文件osgGA/MatrixManipulator错误
- 用正确的小汽车对象学习和熟悉类的概念
- 用正确的小汽车对象学习和熟悉类的概念
- 导弹拦截系统(附详细讲解,误喷)
- POJ 1286 A
- tomcat最大最小内存设置
- 迅为6818开发板-Cortex-A5架构丨支持4G全网通丨GPS丨WIFI丨另有丰富的扩展模块
- 四种常见的 POST 提交数据方式
- OSG学习:转动的小汽车示例
- Sophix更新补丁后崩溃
- codeforces 444D DZY Loves Strings
- 引入工程javax包编译报错
- Relu函数作用
- 为什么我选择使用原型工具来代替纸原型
- 欢迎使用CSDN-markdown编辑器
- C#初1(语言介绍&工具熟悉)
- Ubuntu系统 ADB无法识别数据线连接的设备 adb connection error eof