[原]osg模型動畫|骨骼動畫


參考源碼:osg的官方例子:osganimationviewer

首先制作一個帶骨骼動畫的模型  demo.FBX

這里面我們做了兩個骨骼動畫:1.open   2.close  

下面開始在osg中使用這個動畫。

 

我們用幾種代碼從簡到繁來演示加載播放等過程:

1.最簡單的示例代碼

 1 #include <osgViewer/Viewer>
 2 #include <osgDB/ReadFile>
 3 #include <osgAnimation/BasicAnimationManager>
 4 
 5 int main(int argc, char* argv[])
 6 {
 7     osgViewer::Viewer viewer;
 8 
 9     //讀取帶動畫的節點
10     osg::Node *animationNode = osgDB::readNodeFile("demo.FBX");
11     //獲得節點的動畫列表
12     osgAnimation::BasicAnimationManager* anim =
13         dynamic_cast<osgAnimation::BasicAnimationManager*>(animationNode->getUpdateCallback());
14     const osgAnimation::AnimationList& list = anim->getAnimationList();
15     //從動畫列表中選擇一個動畫,播放
16     anim->playAnimation(list[0].get());
17 
18     viewer.setSceneData(animationNode);
19     return viewer.run();
20 }

 

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();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM