osg::Node* createBase(const osg::Vec3& center,float radius)
{ int numTilesX = ;
int numTilesY = ; float width = *radius;
float height = *radius; osg::Vec3 v000(center - osg::Vec3(width*0.5f,height*0.5f,0.0f));
osg::Vec3 dx(osg::Vec3(width/((float)numTilesX),0.0,0.0f));
osg::Vec3 dy(osg::Vec3(0.0f,height/((float)numTilesY),0.0f)); // fill in vertices for grid, note numTilesX+1 * numTilesY+1...
osg::Vec3Array* coords = new osg::Vec3Array;
int iy;
for(iy=;iy<=numTilesY;++iy)
{
for(int ix=;ix<=numTilesX;++ix)
{
coords->push_back(v000+dx*(float)ix+dy*(float)iy);
}
} //Just two colours - black and white.
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); // white
colors->push_back(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); // black osg::ref_ptr<osg::DrawElementsUShort> whitePrimitives = new osg::DrawElementsUShort(GL_QUADS);
osg::ref_ptr<osg::DrawElementsUShort> blackPrimitives = new osg::DrawElementsUShort(GL_QUADS); int numIndicesPerRow=numTilesX+;
for(iy=;iy<numTilesY;++iy)
{
for(int ix=;ix<numTilesX;++ix)
{
osg::DrawElementsUShort* primitives = ((iy+ix)%==) ? whitePrimitives.get() : blackPrimitives.get();
primitives->push_back(ix +(iy+)*numIndicesPerRow);
primitives->push_back(ix +iy*numIndicesPerRow);
primitives->push_back((ix+)+iy*numIndicesPerRow);
primitives->push_back((ix+)+(iy+)*numIndicesPerRow);
}
} // set up a single normal
osg::Vec3Array* normals = new osg::Vec3Array;
normals->push_back(osg::Vec3(0.0f,0.0f,1.0f)); osg::Geometry* geom = new osg::Geometry;
geom->setVertexArray(coords); geom->setColorArray(colors, osg::Array::BIND_PER_PRIMITIVE_SET); geom->setNormalArray(normals, osg::Array::BIND_OVERALL); geom->addPrimitiveSet(whitePrimitives.get());
geom->addPrimitiveSet(blackPrimitives.get()); osg::Geode* geode = new osg::Geode;
geode->addDrawable(geom); return geode;
}

createBase函数用于建立模型模拟时的地板,作为飞机飞行的参照。

23行中,循环向变量coords中添加坐标点,作为地板的控制点。

36-46行中,交替取出白色面元和黑色面元,并向其中每次添加四个顶点(索引值),指示面元的角点坐标(按照索引值从coords数组中取出)。

随后设置所有面元的法向量,并将黑白面元分别添加进绘制几何节点中,并将绘制几何节点添加到集合节点中,由函数返回。

接下来分析第三个函数:

 osg::Node* createMovingModel(const osg::Vec3& center, float radius)
{
float animationLength = 10.0f; osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength); osg::ref_ptr<osg::Group> model = new osg::Group; osg::ref_ptr<osg::Node> glider = osgDB::readRefNodeFile("glider.osgt");
if (glider)
{
const osg::BoundingSphere& bs = glider->getBound(); float size = radius/bs.radius()*0.3f;
osg::MatrixTransform* positioned = new osg::MatrixTransform;
positioned->setDataVariance(osg::Object::STATIC);
positioned ->setMatrix(osg::Matrix::translate(-bs.center())*
osg::Matrix::scale(size,size,size)*
osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f)); positioned->addChild(glider); osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0));
xform->addChild(positioned); model->addChild(xform);
} osg::ref_ptr<osg::Node> cessna = osgDB::readRefNodeFile("cessna.osgt");
if (cessna)
{
const osg::BoundingSphere& bs = cessna->getBound(); float size = radius/bs.radius()*0.3f;
osg::MatrixTransform* positioned = new osg::MatrixTransform;
positioned->setDataVariance(osg::Object::STATIC);
positioned->setMatrix(osg::Matrix::translate(-bs.center())*
osg::Matrix::scale(size,size,size)*
osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f)); positioned->addChild(cessna); osg::ref_ptr<osg::MatrixTransform> xform = new osg::MatrixTransform;
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0));
xform->addChild(positioned); model->addChild(xform);
} #ifndef OSG_GLES2_AVAILABLE
model->getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
#endif return model.release();
}

这个函数的作用是创建运动模型。

  首先利用上一节介绍的createAnimationPath 函数创建模拟路径,并以此为基础加载模型,并控制模型的运动。

  动态更新的核心就是设置数据变度属性DataVariance,它决定了OSG在多线程渲染的过程中的执行策略:只有所有DYNAMIC属性的对象被渲染 完毕之后,OSG才会开始执行下一帧的用户更新操作;这样有效地可以避免数据的过快更新造成当前的渲染动作出错,以致系统崩溃。

  将模型进行平移,缩放,并绕z轴旋转180度。创建转换函数,并设置其更新回调函数。AnimationPathCallback的构造函数为:

osg::AnimationPathCallback::AnimationPathCallback     (     AnimationPath *      ap,
double timeOffset = 0.0,
double timeMultiplier = 1.0
)

该函数的第三个参数决定了节点旋转的速度。因此cessna模型的旋转速度是glider的二倍。这两个模型的加载和回调更新函数的设定过程相同,只是参数有所不同。

Learning OSG programing---osgAnimation(2)的更多相关文章

  1. Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现(转)

    Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现 zouxy09@qq.com http://blog.csdn.net/zouxy09          自己平时看了一些论文, ...

  2. Deep Learning论文笔记之(八)Deep Learning最新综述

    Deep Learning论文笔记之(八)Deep Learning最新综述 zouxy09@qq.com http://blog.csdn.net/zouxy09 自己平时看了一些论文,但老感觉看完 ...

  3. Deep Learning论文笔记之(六)Multi-Stage多级架构分析

    Deep Learning论文笔记之(六)Multi-Stage多级架构分析 zouxy09@qq.com http://blog.csdn.net/zouxy09          自己平时看了一些 ...

  4. Learning Cocos2d-x for WP8(2)——深入刨析Hello World

    原文:Learning Cocos2d-x for WP8(2)--深入刨析Hello World cocos2d-x框架 在兄弟篇Learning Cocos2d-x for XNA(1)——小窥c ...

  5. Learning Cocos2d-x for WP8(1)——创建首个项目

    原文:Learning Cocos2d-x for WP8(1)--创建首个项目 Cocos2d-x for WP8开发语言是C++,系列文章将参考兄弟篇Learning Cocos2d-x for ...

  6. Learning Cocos2d-x for WP8(9)——Sprite到哪,我做主

    原文:Learning Cocos2d-x for WP8(9)--Sprite到哪,我做主 工程文件TouchesTest.h和TouchesTest.cpp 相关素材文件 事件驱动同样适用于coc ...

  7. Learning Cocos2d-x for WP8(8)——动作Action

    原文:Learning Cocos2d-x for WP8(8)--动作Action 游戏很大程度上是由动作画面支撑起来的. 动作分为两大类:瞬间动作和延时动作. 瞬间动作基本等同于设置节点的属性,延 ...

  8. Learning Cocos2d-x for WP8(7)——让Sprite动起来

    原文:Learning Cocos2d-x for WP8(7)--让Sprite动起来 C#(wp7)兄弟篇Learning Cocos2d-x for XNA(7)——让Sprite动起来 本讲将 ...

  9. Learning Cocos2d-x for WP8(6)——场景切换和场景过渡效果

    原文:Learning Cocos2d-x for WP8(6)--场景切换和场景过渡效果 C#(wp7)兄弟篇 Learning Cocos2d-x for XNA(6)——场景切换和场景过渡效果 ...

  10. Learning Cocos2d-x for WP8(5)——详解Menu菜单

    原文:Learning Cocos2d-x for WP8(5)--详解Menu菜单 C#(wp7)兄弟篇Learning Cocos2d-x for XNA(5)——详解Menu菜单 菜单是游戏必不 ...

随机推荐

  1. JSP学习(2)

    JSP学习(2) JSP简介 Java Server Page,其根本是一个简单Servlet设计. 常用的动态网站开发技术 JSP:安全性高,适合开发大型的,企业级或分布式的Web应用程序. Asp ...

  2. apache重写规则简单理解

    1.前提:开启apache重写,并把httpd.conf里的相关的AllowOverride denied改为AllowOverride all 2.重写规则可写在项目根目录的.htaccess文件或 ...

  3. (NOIP)CSP-S 2019前计划

    前言 无 1.NOIP原题板刷 NOIP原题板刷 这是一篇咕了的blog 2.牛客 & ACwing & 洛谷 网课学习 收获还是蛮大的,不过我没有写博客 3.codeforces专项 ...

  4. SpringBoot自定义Jackson配置

    为了在SpringBoot工程中集中解决long类型转成json时JS丢失精度问题和统一设置常见日期类型序列化格式,我们可以自定义Jackson配置类,具体如下: import com.fasterx ...

  5. Dubbo学习-1-基础知识

    分布式基础理论 1.什么是分布式系统: 分布式系统是若干独立计算机的集合,这些计算机对于用户来说就像是单个相关系统.分布式系统是建立在网络之上的软件系统 随着互联网发展,网站应用规模的不断扩大,常规的 ...

  6. php substr_replace()函数 语法

    php substr_replace()函数 语法 作用:替换字符串中某串为另一个字符串大理石平台价格 语法:substr_replace(string,replacement,start,lengt ...

  7. html+js(swiper.js)+css左右滑动切换页面效果,适配移动端

    demo: 截图: 结构:1.swiper-progress.html2.css文件夹 -swiper.css -swiper.min.css 3.js文件夹 -swiper.min.js -swip ...

  8. (转)Hyper-v 安装CentOS 7 (其他虚拟机一样参考)

    转:https://www.cnblogs.com/dunitian/p/4976077.html 平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunit ...

  9. oracle12.2 CDB PDB基本管理操作

    容器间切换 切换到对应的PDBSSQL> alter session set container=pdb1;Session altered.SQL> alter database open ...

  10. 1208C Magic Grid

    题目大意 给你一个n 让你用0~n^2-1的数填满一个n*n的正方形 满足每个数值出现一次且每行每列的异或值相等 输出任意一种方案 分析 我们发现对于4*4的正方形 0  1  2  3 4  5  ...