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

代码如下:

#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();
}

结果图:

OSG学习:转动的小汽车示例的更多相关文章

  1. OSG学习:阴影代码示例

    效果图: 代码示例: #include <osgViewer/Viewer> #include <osg/Node> #include <osg/Geode> #i ...

  2. OSG学习:自动对齐节点示例

    /********************************************************** *Write by FlySky *zzuxp@163.com http://w ...

  3. OSG学习:矩阵变换节点示例

    #include<osgViewer\Viewer> #include<osg\Node> #include<osg\Geode> #include<osg\ ...

  4. osg学习示例之遇到问题四骨骼动画编译osgCal

    osg学习示例之遇到问题四骨骼动画编译osgCal 转自:http://blog.csdn.net/wuwangrun/article/details/8239451 今天学到书<OpenSce ...

  5. Angular 快速学习笔记(1) -- 官方示例要点

    创建组件 ng generate component heroes {{ hero.name }} {{}}语法绑定数据 管道pipe 格式化数据 <h2>{{ hero.name | u ...

  6. SQL 数据库 学习 007 通过一个示例简单介绍什么是字段、属性、列、元组、记录、表、主键、外键 (上)

    SQL 数据库 学习 007 通过一个示例简单介绍什么是字段.属性.列.元组.记录.表.主键.外键 (上) 我们来介绍一下:数据库是如何存储数据的. 数据库是如何存储数据的 来看一个小例子 scott ...

  7. WebGPU学习(七):学习“twoCubes”和“instancedCube”示例

    大家好,本文学习Chrome->webgpu-samplers->twoCubes和instancedCube示例. 这两个示例都与"rotatingCube"示例差不 ...

  8. OSG学习:响应键盘鼠标示例

    示例功能:示例里面有两个模型,一个是牛,另一个是飞机.鼠标右键时牛和飞机都隐藏,鼠标左键双击时牛和飞机都显示,按键盘上面的LEFT键,显示牛,按键盘上面的RIGHT键显示飞机.其中显示与隐藏节点使用的 ...

  9. OSG学习:使用已有回调示例

    回调的类型有很多种,一般很容易就想到的是UpdateCallBack,或者EventCallBack,回调的意思就是说,你可以规定在某件事情发生时启动一个函数,这个函数可能做一些事情.这个函数就叫做回 ...

随机推荐

  1. 「PHP」策略模式

    引言   所属:行为型模式,常用设计模式之一     学习资料: <大话设计模式>程杰   模式概述   分享一篇关于策略模式讲的很好的文章,大家可以参考一下:https://www.cn ...

  2. python__PIP : 安装第三方库

    pipy国内镜像目前有: http://pypi.douban.com/  豆瓣 http://pypi.hustunique.com/  华中理工大学 http://pypi.sdutlinux.o ...

  3. day 24 内置模块re

    1.正则表达式,匹配字符串 正则表达式是对字符串操作的一种逻辑公式.我们一般使用正则表达式对字符串镜子那个匹配和过滤,使用正则的优缺点: 优点: 灵活,功能性强,逻辑性强 缺点: 上手难.一旦上手,会 ...

  4. ELK的端口以及加入x-pack的密码问题

    ElasticSearch的端口: http://localhost:9200 http://localhost:9200/_plugin/head Kibana的端口: http://localho ...

  5. Spring quantz--定时任务调度工具

    1.在xml中交给spring管理的一些类 <bean id="cancelOrderJobDetail" class="org.springframework.s ...

  6. ruby中的return方法及class实例方法的initialize方法

    return是函数的返回值 class Mtring def initialize(str) @name = str end def aa ary = @name.split(/:/) return ...

  7. AtCoder Grant Contest 10.F 博弈

    题意:给定一棵树,每个节点有个权值,Alice和Bob轮流进行操作,给定游戏起点指针指向节点C.不断进行下述操作. 1.将当前节点权值-1,然后将指针从该节点移动到相邻节点,出现一方不能移动(即指针指 ...

  8. HDU 5212 莫比乌斯反演

    Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  9. 20145209 2016-2017-2 《Java程序设计》第4周学习总结

    20145209 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 继承是面向对象最显著的一个特性.继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性 ...

  10. CSS布局遇到的问题小结

    clear属性的作用 指定某个元素的一侧不能出现浮动元素.它是通过为这个元素在上边距之外增加空间,从而使得这个元素的顶部和浮动元素的底部对齐.这里作用的仅仅是同一个bfc下的浮动元素. This pr ...