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