本文主要介绍通过osg读取fbx 3d模型,并播放模型所带的动画。
所使用到的类为AnimationManagerBase。
下面是完整代码:
//#include "AnimtkViewer.h"
//#include "AnimtkViewerKeyHandler.h"
#include
#include
#include
#include
#include
#include
struct AnimationManagerFinder : public osg::NodeVisitor
{
osg::ref_ptr
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
if (b) {
_am = new osgAnimation::BasicAnimationManager(*b);
return;
}
}
traverse(*node);
}
};
int main(int argc, char ** argv)
{
osgViewer::Viewer viewer;
//读取带动画的节点
osg::Node *animationNode = osgDB::readNodeFile("./Naruto/Naruto.fbx");
//获得节点的动画列表
AnimationManagerFinder *m_cFinder = new AnimationManagerFinder();
m_cFinder->apply(animationNode);
animationNode->accept(*m_cFinder);
if (m_cFinder->_am.valid())
{
animationNode->setUpdateCallback(m_cFinder->_am.get());
}
osgAnimation::AnimationList::const_iterator it = m_cFinder->_am->getAnimationList().begin();
it++;
it++;
it++;
std::string animationName = (*it)->getName();
osgAnimation::Animation::PlayMode playMode = osgAnimation::Animation::LOOP;
(*it)->setPlayMode(playMode);//设置播放模式
//(*it)->setDuration(5.0);//设置播放时间
//从动画列表中选择一个动画,播放
m_cFinder->_am->playAnimation(*it);
viewer.setSceneData(animationNode);
return viewer.run();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
最终效果(用手机拍的效果不是很好):
模型下载地址:https://download.csdn.net/download/github_39611196/11182576
————————————————