参考源码:osg的官方例子:osganimationviewer

首先制作一个带骨骼动画的模型  demo.FBX

这里面我们做了两个骨骼动画:1.open   2.close

下面开始在osg中使用这个动画。

我们用几种代码从简到繁来演示加载播放等过程:

1.最简单的示例代码

 #include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgAnimation/BasicAnimationManager> int main(int argc, char* argv[])
{
osgViewer::Viewer viewer; //读取带动画的节点
osg::Node *animationNode = osgDB::readNodeFile("demo.FBX");
//获得节点的动画列表
osgAnimation::BasicAnimationManager* anim =
dynamic_cast<osgAnimation::BasicAnimationManager*>(animationNode->getUpdateCallback());
const osgAnimation::AnimationList& list = anim->getAnimationList();
//从动画列表中选择一个动画,播放
anim->playAnimation(list[].get()); viewer.setSceneData(animationNode);
return viewer.run();
}

2.通过自定义AnimationManagerFinder加载

本段代码,我没有测试,但是大体是这样。

#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osgAnimation/BasicAnimationManager> struct AnimationManagerFinder : public osg::NodeVisitor
{
osg::ref_ptr<osgAnimation::BasicAnimationManager> _am;
AnimationManagerFinder() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
void apply(osg::Node& node) {
if (_am.valid())
return;
if (node.getUpdateCallback()) {
osgAnimation::AnimationManagerBase* b = dynamic_cast<osgAnimation::AnimationManagerBase*>(node.getUpdateCallback());
if (b) {
_am = new osgAnimation::BasicAnimationManager(*b);
return;
}
}
traverse(node);
}
}; int main(int argc, char* argv[])
{
osgViewer::Viewer viewer; //读取带动画的节点
osg::Node *animationNode = osgDB::readNodeFile("demo.FBX"); AnimationManagerFinder m_cFinder;
//获得节点的动画列表
animationNode ->accept(*m_cFinder);
if (m_cFinder->_am.valid())
{
animationNode ->setUpdateCallback(m_cFinder->_am.get());
} for (osgAnimation::AnimationList::const_iterator it = m_cFinder->_am->getAnimationList().begin(); it != m_cFinder->_am->getAnimationList().end(); it++)
{
std::string animationName = (*it)->getName();
osgAnimation::Animation::PlayMode playMode = osgAnimation::Animation::ONCE;
(*it)->setPlayMode(playMode);//设置播放模式
(*it)->setDuration(5.0);//设置播放时间
} //从动画列表中选择一个动画,播放
m_cFinder->_am->->playAnimation(*m_cFinder->_am->getAnimationList().begin()); viewer.setSceneData(animationNode);
return viewer.run();
}

最新文章

  1. 【Win 10 应用开发】应用预启动
  2. ListView滑动位置精准记忆
  3. js中关于原型的几个方法
  4. SQL从入门到基础 - 04 SQLServer基础2(数据删除、数据检索、数据汇总、数据排序、通配符过滤、空值处理、多值匹配)
  5. JAVA泛型-自动包装机制不能应用于泛型数据的测试
  6. InnoDB引擎Myslq数据库数据恢复
  7. unlinking
  8. javascript:void(0) 含义
  9. nodejs多版本管理
  10. javaweb开发.常用的第三方包
  11. Maven整理
  12. Docker容器使用jenkins部署web项目--总结(二)
  13. oc门
  14. java 操作 RabbitMQ 发送、接受消息
  15. tomcat和servlet关系
  16. 安装tftp服务器进行文件传输
  17. maven 启动 tomcat 及 跳过 test 安装
  18. 基于DCT系数的实时监控中运动目标检测
  19. Mybatis插入数据返回自增主键
  20. 多线程设计模式(三):Master-Worker模式

热门文章

  1. Spark SQL 编程初级实践
  2. GBDT总结
  3. 用python脚本获取运行环境中的module 列表
  4. 运维自动化之系统部署 PXE(二)
  5. DS18B20初上电显示85℃问题
  6. TCP 服务端接收数据解析工具类
  7. MyEclipse的JPA实现集成EasyJF+Spring
  8. 原生js获取 一个dom元素距离页面可视区域的位置值 -- getBoundingClientRect
  9. Go语言文件操作
  10. Visual Studio 2010 集成 SP1 补丁 制作 Visual Studio 2010 Service Pack 1 完整版安装光盘的方法