OSG学习:转动的小汽车示例
由于只是简单的示例,所以小汽车的模型也比较简单,是由简单的几何体组成。
代码如下:
#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学习:转动的小汽车示例的更多相关文章
- OSG学习:阴影代码示例
效果图: 代码示例: #include <osgViewer/Viewer> #include <osg/Node> #include <osg/Geode> #i ...
- OSG学习:自动对齐节点示例
/********************************************************** *Write by FlySky *zzuxp@163.com http://w ...
- OSG学习:矩阵变换节点示例
#include<osgViewer\Viewer> #include<osg\Node> #include<osg\Geode> #include<osg\ ...
- osg学习示例之遇到问题四骨骼动画编译osgCal
osg学习示例之遇到问题四骨骼动画编译osgCal 转自:http://blog.csdn.net/wuwangrun/article/details/8239451 今天学到书<OpenSce ...
- Angular 快速学习笔记(1) -- 官方示例要点
创建组件 ng generate component heroes {{ hero.name }} {{}}语法绑定数据 管道pipe 格式化数据 <h2>{{ hero.name | u ...
- SQL 数据库 学习 007 通过一个示例简单介绍什么是字段、属性、列、元组、记录、表、主键、外键 (上)
SQL 数据库 学习 007 通过一个示例简单介绍什么是字段.属性.列.元组.记录.表.主键.外键 (上) 我们来介绍一下:数据库是如何存储数据的. 数据库是如何存储数据的 来看一个小例子 scott ...
- WebGPU学习(七):学习“twoCubes”和“instancedCube”示例
大家好,本文学习Chrome->webgpu-samplers->twoCubes和instancedCube示例. 这两个示例都与"rotatingCube"示例差不 ...
- OSG学习:响应键盘鼠标示例
示例功能:示例里面有两个模型,一个是牛,另一个是飞机.鼠标右键时牛和飞机都隐藏,鼠标左键双击时牛和飞机都显示,按键盘上面的LEFT键,显示牛,按键盘上面的RIGHT键显示飞机.其中显示与隐藏节点使用的 ...
- OSG学习:使用已有回调示例
回调的类型有很多种,一般很容易就想到的是UpdateCallBack,或者EventCallBack,回调的意思就是说,你可以规定在某件事情发生时启动一个函数,这个函数可能做一些事情.这个函数就叫做回 ...
随机推荐
- 【 es搜索】
地图搜索实现: ①参数: 左下角经纬度和右上角经纬度 图层数(zoom) 关键字等各种数据库中的字段 排序方式 具体的坐标点+距离 ②实现 a.用es作为关系库,首先先mapping所有的字段,然后用 ...
- Spark 加载数据库mysql表中数据进行分析
1.工程maven依赖包 <properties> <spark_version>2.3.1</spark_version> <!-- elasticsear ...
- 『Python基础-14』匿名函数 `lambda`
匿名函数和关键字lambda 匿名函数就是没有名称的函数,也就是不再使用def语句定义的函数 在Python中,如果要声匿名函数,则需要使用lambda关键字 使用lambda声明的匿名函数能接收任何 ...
- Java学习笔记二十二:Java的方法重写
Java的方法重写 一:什么是方法的重写: 如果子类对继承父类的方法不满意,是可以重写父类继承的方法的,当调用方法时会优先调用子类的方法. 语法规则 返回值类型.方法名.参数类型及个数都要与父类继承的 ...
- linux线程篇 (二) 线程的基本操作
线程 进程 标识符 pthread_t pid_t 获取ID pthread_self() getpid() 创建 pthread_create() fork 销毁 pthread_exit() ...
- sublime解决gbk中文乱码包括Package Control: Install Package 无法使用
最近喜欢上了sublime,打算抛弃notepad,但是发现sublime居然不支持gbk编码,再上网查找资料之后,总结了一套解决方法,目前为止是行之有效的. 日期:2019年3月14日 第一步:到G ...
- ant-design form
表单配置 示例代码 import { Form } from 'antd'; const FormItem = Form.Item; class NormalLoginForm extends Rea ...
- springboot之redis的应用
1.redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted se ...
- substr是不安全的
今天遇到一个问题,数据库中保存的内容通过php在页面无法显示,如果将内容换行或加个空格或者随便加点其他内容就能正常显示. 非常的诡异,显示的内容是通过截取得到的.代码非常简单 substr($pMar ...
- linux 安装 node.js
wget http://nodejs.org/dist/v0.10.26/node-v0.10.26.tar.gztar zxvf node-v0.10.26.tar.gzcd node-v0.10. ...