OSG动画与声音-路径动画之导出与导入(2)

news2024/9/27 15:31:11

路径的导出示例

        路径的导出示例的代码如程序清单10-2所示。

1.	// 创建路径  
2.	osg::ref_ptr<osg::AnimationPath> createAnimationPath(osg::Vec3 ¢er,  
3.	    float radius, float looptime)  
4.	{  
5.	    // 创建一个Path对象  
6.	    osg::ref_ptr<osg::AnimationPath> animationPath = new osg::AnimationPath();  
7.	  
8.	    // 设置动画模式为循环(LOOP),LOOP,循环,SWING;单摆,NO_LOOPING,不循环  
9.	    animationPath->setLoopMode(osg::AnimationPath::LOOP);  
10.	      
11.	    // 关键点数  
12.	    int numPoint = 60;  
13.	  
14.	    // 每次偏移角度  
15.	    float yaw = 0.0;  
16.	    float yaw_delta = 2.0f * osg::PI / (numPoint - 1.0);  
17.	  
18.	    // 倾斜角度  
19.	    float roll = osg::inDegrees(45.0);  
20.	  
21.	    // 时间偏移  
22.	    float time = 0.0;  
23.	    float time_delta = looptime/(float(numPoint));  
24.	  
25.	    for (int i = 0; i < numPoint; ++i)  
26.	    {  
27.	        // 关键点位置  
28.	        osg::Vec3 position(center + osg::Vec3(sinf(yaw) * radius, cosf(yaw) * radius, 0.0f));  
29.	  
30.	        // 关键点角度  
31.	        osg::Quat rotation(osg::Quat(roll, osg::Vec3(0.0, 1.0, 0.0)) * osg::Quat(-(yaw + osg::inDegrees(90.0)),osg::Vec3(0.0,0.0,1.0)));  
32.	  
33.	        // 插入Path,把关键点与时间压入星辰Path  
34.	        animationPath->insert(time, osg::AnimationPath::ControlPoint(position, rotation));  
35.	  
36.	        yaw += yaw_delta;  
37.	        time += time_delta;  
38.	    }  
39.	  
40.	    // 返回Path  
41.	    return animationPath.get();  
42.	}  
43.	// 导出动画路径  
44.	void outAnimationPath_10_2(const string &strDataFolder)  
45.	{  
46.	    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();  
47.	    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;  
48.	    traits->x = 40;  
49.	    traits->y = 40;  
50.	    traits->width = 600;  
51.	    traits->height = 480;  
52.	    traits->windowDecoration = true;  
53.	    traits->doubleBuffer = true;  
54.	    traits->sharedContext = 0;  
55.	  
56.	    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());  
57.	  
58.	    osg::ref_ptr<osg::Camera> camera = viewer->getCamera();  
59.	    camera->setGraphicsContext(gc.get());  
60.	    camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));  
61.	    GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;  
62.	    camera->setDrawBuffer(buffer);  
63.	    camera->setReadBuffer(buffer);  
64.	  
65.	    osg::ref_ptr<osg::Group> root = new osg::Group();  
66.	  
67.	    // 读取飞机模型  
68.	    string strDataPath = strDataFolder + "cessna.osg";  
69.	    osg::ref_ptr<osg::Node> cessna = osgDB::readNodeFile(strDataPath);  
70.	  
71.	    strDataPath = strDataFolder + "lz.osg";  
72.	    osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(strDataPath);  
73.	  
74.	    // 得到包围盒,来确定动画旋转中心  
75.	    const osg::BoundingSphere &bs = cessna->getBound();  
76.	    osg::Vec3 position = bs.center() + osg::Vec3(0.0, 0.0, 200.0);  
77.	  
78.	    // 缩放比例,如果比例不当,模型会看不见  
79.	    float size = 100.0 / bs.radius() * 0.3;  
80.	  
81.	    // 创建路径  
82.	    osg::ref_ptr<osg::AnimationPath> animationPath = new osg::AnimationPath();  
83.	    animationPath = createAnimationPath(position, 200.0, 10.0);  
84.	  
85.	    string fileName = strDataFolder + "animation_10_2.path";  
86.	    ofstream out(fileName.c_str());  
87.	    animationPath->write(out);  
88.	  
89.	    osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform();  
90.	  
91.	    // OSG确保只有STATIC数据可以进行图形渲染  
92.	    mt->setDataVariance(osg::Object::STATIC);  
93.	  
94.	    // 进行适当的变换(平移、缩放以及旋转)  
95.	    mt->setMatrix(osg::Matrix::translate(-bs.center()) * osg::Matrix::scale(size, size, size) *  
96.	        osg::Matrix::rotate(osg::inDegrees(-180.0), 0.0, 0.0, 1.0));  
97.	    mt->addChild(cessna.get());  
98.	  
99.	    osg::ref_ptr<osg::PositionAttitudeTransform> pat = new osg::PositionAttitudeTransform();  
100.	  
101.	    // 设置更新回调  
102.	    pat->setUpdateCallback(new osg::AnimationPathCallback(animationPath.get(), 0.0, 1.0));  
103.	    pat->addChild(mt.get());  
104.	  
105.	    root->addChild(pat.get());  
106.	    root->addChild(node.get());  
107.	  
108.	    // 优化场景数据  
109.	    osgUtil::Optimizer optimizer;  
110.	    optimizer.optimize(root.get());  
111.	  
112.	    viewer->setSceneData(root.get());  
113.	    viewer->addEventHandler(new AnimationEventHander(*(viewer.get())));  
114.	    viewer->realize();  
115.	    viewer->run();  
116.	}  

        运行程序,截图如图10-4所示。

图10-4路径的导出示例截图

​​​​​​​路径的导入示例

        路径的导入示例的代码如程序清单10-3所示

 

1.	// 创建路径  
2.	osg::ref_ptr<osg::AnimationPath> createAnimationPath(osg::Vec3 ¢er,  
3.	    float radius, float looptime)  
4.	{  
5.	    // 创建一个Path对象  
6.	    osg::ref_ptr<osg::AnimationPath> animationPath = new osg::AnimationPath();  
7.	  
8.	    // 设置动画模式为循环(LOOP),LOOP,循环,SWING;单摆,NO_LOOPING,不循环  
9.	    animationPath->setLoopMode(osg::AnimationPath::LOOP);  
10.	      
11.	    // 关键点数  
12.	    int numPoint = 60;  
13.	  
14.	    // 每次偏移角度  
15.	    float yaw = 0.0;  
16.	    float yaw_delta = 2.0f * osg::PI / (numPoint - 1.0);  
17.	  
18.	    // 倾斜角度  
19.	    float roll = osg::inDegrees(45.0);  
20.	  
21.	    // 时间偏移  
22.	    float time = 0.0;  
23.	    float time_delta = looptime/(float(numPoint));  
24.	  
25.	    for (int i = 0; i < numPoint; ++i)  
26.	    {  
27.	        // 关键点位置  
28.	        osg::Vec3 position(center + osg::Vec3(sinf(yaw) * radius, cosf(yaw) * radius, 0.0f));  
29.	  
30.	        // 关键点角度  
31.	        osg::Quat rotation(osg::Quat(roll, osg::Vec3(0.0, 1.0, 0.0)) * osg::Quat(-(yaw + osg::inDegrees(90.0)),osg::Vec3(0.0,0.0,1.0)));  
32.	  
33.	        // 插入Path,把关键点与时间压入星辰Path  
34.	        animationPath->insert(time, osg::AnimationPath::ControlPoint(position, rotation));  
35.	  
36.	        yaw += yaw_delta;  
37.	        time += time_delta;  
38.	    }  
39.	  
40.	    // 返回Path  
41.	    return animationPath.get();  
42.	}  
43.	// 导入动画路径  
44.	void inputAnimationPath_10_3(const string &strDataFolder)  
45.	{  
46.	    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();  
47.	    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;  
48.	    traits->x = 40;  
49.	    traits->y = 40;  
50.	    traits->width = 600;  
51.	    traits->height = 480;  
52.	    traits->windowDecoration = true;  
53.	    traits->doubleBuffer = true;  
54.	    traits->sharedContext = 0;  
55.	  
56.	    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());  
57.	  
58.	    osg::ref_ptr<osg::Camera> camera = viewer->getCamera();  
59.	    camera->setGraphicsContext(gc.get());  
60.	    camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));  
61.	    GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;  
62.	    camera->setDrawBuffer(buffer);  
63.	    camera->setReadBuffer(buffer);  
64.	  
65.	    osg::ref_ptr<osg::Group> root = new osg::Group();  
66.	  
67.	    // 读取飞机模型  
68.	    string strDataPath = strDataFolder + "cessna.osg";  
69.	    osg::ref_ptr<osg::Node> cessna = osgDB::readNodeFile(strDataPath);  
70.	  
71.	    strDataPath = strDataFolder + "lz.osg";  
72.	    osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(strDataPath);  
73.	  
74.	    // 得到包围盒,来确定动画旋转中心  
75.	    const osg::BoundingSphere &bs = cessna->getBound();  
76.	    osg::Vec3 position = bs.center() + osg::Vec3(0.0, 0.0, 200.0);  
77.	  
78.	    // 缩放比例,如果比例不当,模型会看不见  
79.	    float size = 100.0 / bs.radius() * 0.3;  
80.	  
81.	    // 创建路径  
82.	    string fileName = strDataFolder + "animation_10_2.path";  
83.	    ifstream fin(fileName.c_str());  
84.	  
85.	    osg::ref_ptr<osg::AnimationPath> animationPath = new osg::AnimationPath();  
86.	    animationPath->read(fin);  
87.	    fin.close();  
88.	  
89.	    osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform();  
90.	  
91.	    // OSG确保只有STATIC数据可以进行图形渲染  
92.	    mt->setDataVariance(osg::Object::STATIC);  
93.	  
94.	    // 进行适当的变换(平移、缩放以及旋转)  
95.	    mt->setMatrix(osg::Matrix::translate(-bs.center()) * osg::Matrix::scale(size, size, size) *  
96.	        osg::Matrix::rotate(osg::inDegrees(-180.0), 0.0, 0.0, 1.0));  
97.	    mt->addChild(cessna.get());  
98.	  
99.	    osg::ref_ptr<osg::PositionAttitudeTransform> pat = new osg::PositionAttitudeTransform();  
100.	  
101.	    // 设置更新回调  
102.	    pat->setUpdateCallback(new osg::AnimationPathCallback(animationPath.get(), 0.0, 1.0));  
103.	    pat->addChild(mt.get());  
104.	  
105.	    root->addChild(pat.get());  
106.	    root->addChild(node.get());  
107.	  
108.	    // 优化场景数据  
109.	    osgUtil::Optimizer optimizer;  
110.	    optimizer.optimize(root.get());  
111.	  
112.	    viewer->setSceneData(root.get());  
113.	    viewer->addEventHandler(new AnimationEventHander(*(viewer.get())));  
114.	    viewer->realize();  
115.	    viewer->run();  
116.	}  

        运行程序,截图如图10-5所示

图10-5路的导入示例截图

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1243208.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【史上最细教程】服务器MySQL数据库完成主从复制

文章目录 MySQL完成主从复制教程准备&#xff1a;原理&#xff1a;步骤&#xff1a; 推荐文章 MySQL完成主从复制教程 主从复制&#xff08;也称 AB 复制&#xff09;就是将一个服务器&#xff08;主服务器&#xff09;的数据复制到一个或多个MySQL数据库服务器&#xff08;从…

云安全之盾:ZStack 云主机安全防护解决方案全方位保护云环境

随着云计算的蓬勃发展&#xff0c;网络威胁愈发复杂&#xff0c;涵盖了从勒索病毒到APT攻击的各种威胁类型。在这一风云变幻的网络安全环境下&#xff0c;云主机安全不再仅仅是一个选项&#xff0c;它是信息系统安全的核心要素。云轴科技ZStack 云主机安全防护解决方案是为了满…

java项目之品牌银饰售卖平台(ssm+vue)

项目简介 主要功能包括首页、个人中心、用户管理、促销活动管理、饰品管理、我的收藏管理、系统管理、订单管理等。管理员模块: 管理员可以查询、编辑、管理每个用户的信息和系统管理员自己的信息&#xff0c;同时还可以编辑、修改、查询用户账户和密码&#xff0c;以及对系统…

项目管理套路:看这一篇绝对够用❤️

写论文必不可少的&#xff0c;就是创建代码并进行实验。好的项目管理可以让实验进行得更加顺利。本篇博客以一次项目实践为例&#xff0c;介绍项目管理的方法&#xff0c;以及可能遇到的问题&#xff0c;并提供一些可行的解决方案。 目录 项目管理工具开始第一步版本管理十分关…

微信小程序:This Mini Program cannot be opened as your Weixin version is out-of-date.

项目场景&#xff1a; 问题描述 升级基础库3.2.0&#xff0c;然后PC端整个小程序都打不开了&#xff0c;点击小程序提示”This Mini Program cannot be opened as your Weixin version is out-of-date. Update Weixin to the latest version.“&#xff0c;并且点击Update Wei…

成为AI产品经理——模型构建流程(下)

目录 1.模型训练 2.模型验证 3.模型融合 4.模型部署 上节课我们讲了模型设计、特征工程&#xff0c;这节课我们来讲模型构建剩下的三个部分&#xff1a;模型训练、模型验证和模型融合。 1.模型训练 模型训练就是要不断地训练、验证、调优直至让模型达到最优。 那么怎么达…

Linux系统装宝塔面板提示磁盘空间不足如何清理,检测到当前磁盘超过80%,空间不足如何清理

Linux系统装宝塔面板提示磁盘空间不足如何清理&#xff0c;检测到当前磁盘超过80%&#xff0c;空间不足如何清理 今天登陆服务器查看到磁盘空间满&#xff0c;如题这部分属于运维问题&#xff0c;如果直接点击 如何直接点立即清理会跳转企业版购买&#xff0c;1599CNY对普通用…

如何做一个简单的深度集成学习框架

使用同一个框架&#xff0c;独立在一个数据集上面&#xff0c;分别训练多次&#xff0c;每个单独模型训练超参数可以一样&#xff0c;也可以不一样&#xff0c;最后若干个训练好的独立模型在测试集数据上面做最后集中决策。 实例代码如下&#xff1a; class MyEnsemble(nn.Modu…

CVE-2023-6099:优卡特脸爱云一脸通智慧管理平台SystemMng.ashx接口未授权漏洞复现

文章目录 优卡特脸爱云一脸通智慧管理平台未授权SystemMng.ashx接口漏洞复现&#xff08;CVE-2023-6099&#xff09; [附POC]0x01 前言0x02 漏洞描述0x03 影响版本0x04 漏洞环境0x05 漏洞复现1.访问漏洞环境2.构造POC3.复现 0x06 修复建议 优卡特脸爱云一脸通智慧管理平台未授权…

eNSP-远程登陆实验

实验拓扑&#xff1a; 实验需求&#xff1a; 1. 如图所示&#xff0c;配置设备名称和IP地址 2. 配置R2的远程登录vty接口0-4&#xff0c;完成以下需求&#xff1a; 1&#xff09;采用接口password的验证方式&#xff0c;口令为"spoto"&#xff08;不含引号&#xf…

为何越来越多的程序员纷纷转行网络安全?

目前&#xff0c;我国IT行业的人才结构不断升级&#xff0c;公司对程序员的要求越来越高&#xff0c;出现了大量的裁员现象&#xff0c;导致很多的程序员纷纷想转行的想法。 可能对于早期的程序员而言&#xff0c;学好编程语言就能找到比较好的工作。而现在伴随着互联网的不断发…

美国大学陪读签证申请条件是什么?

美国大学陪读签证是留学生的家庭成员在美国陪同留学生就读期间的签证类型。申请该签证需要满足一定的条件&#xff0c;这些条件通常包括&#xff1a; 1.申请人身份关系证明&#xff1a;申请人必须是留学生的家庭成员&#xff0c;如配偶或子女&#xff0c;需要提供证明文件&…

Lua脚本解决redis实现的分布式锁多条命令原子性问题

线程1现在持有锁之后&#xff0c;在执行业务逻辑过程中&#xff0c;他正准备删除锁&#xff0c;而且已经走到了条件判断的过程中&#xff0c;比如他已经拿到了当前这把锁确实是属于他自己的&#xff0c;正准备删除锁&#xff0c;但是此时他的锁到期了&#xff0c;那么此时线程2…

【深度学习实验】图像处理(一):Python Imaging Library(PIL)库:图像读取、写入、复制、粘贴、几何变换、图像增强、图像滤波

文章目录 一、实验介绍二、实验环境1. 配置虚拟环境2. 库版本介绍 三、实验内容0. 安装 PIL 库1. 图像读取和写入a. 图像读取b. 图像写入c. 构建新图像 2. 图像复制粘贴a. 图像复制b. 图像局部复制c. 图像粘贴 3. 几何变换a. 图像调整大小b. 图像旋转c. 图像翻转 4. 图像增强a.…

QGIS之二十五两个面图层数据中选择图形完全一致的数据

效果 步骤 1、准备数据 2、按位置选择 在Qgis工具箱中搜索"按位置选择"工具 选择要素和比较要素根据实际选择 运行 3、结果

FreeRTOS学习之路,以STM32F103C8T6为实验MCU(2-3:任务切换)

学习之路主要为FreeRTOS操作系统在STM32F103&#xff08;STM32F103C8T6&#xff09;上的运用&#xff0c;采用的是标准库编程的方式&#xff0c;使用的IDE为KEIL5。 注意&#xff01;&#xff01;&#xff01;本学习之路可以通过购买STM32最小系统板以及部分配件的方式进行学习…

WorkPlus解放企业应用开发,实现跨平台无缝切换

在移动应用的快速发展时代&#xff0c;企业对于自主开发和应用平台替换的需求日益增长。WorkPlus作为一个完整的移动数字化APP底座&#xff0c;为企业提供了独立自主的应用开发环境&#xff0c;实现了平台无关性和应用解耦。今天&#xff0c;我们将深入探讨WorkPlus在企业应用开…

基于 Flink SQL 和 Paimon 构建流式湖仓新方案

本文整理自阿里云智能开源表存储负责人&#xff0c;Founder of Paimon&#xff0c;Flink PMC 成员李劲松在云栖大会开源大数据专场的分享。本篇内容主要分为四部分&#xff1a; 数据分析架构演进介绍 Apache PaimonFlink Paimon 流式湖仓流式湖仓Demo演示 数据分析架构演进 …

【C语言:深入理解指针二】

文章目录 1. 二级指针2. 指针数组3. 字符指针变量4. 数组指针变量5. 二维数组传参的本质6. 函数指针变量7. 函数指针数组8. 转移表9. 回调函数10. qsort函数的使用与模拟实现 1. 二级指针 我们知道&#xff0c;指针变量也是变量&#xff0c;它也有自己的地址&#xff0c;使用什…

TypeError: Cannot read property ‘sendpost‘ of undefined

箭头函数指向问题&#xff0c;定义let that this 解决