osg 加载 fbx文件

#ifdef _WIN32
#include <Windows.h>
#endif // _WIN32

#include <osg/Group>
#include <osg/Camera>
#include <osg/Node>
#include <osg/Geometry>

#include <osg/Image>
#include <osg/ShapeDrawable>
#include <osg/Texture2D>
#include <osg/MatrixTransform>

#include <osg/AnimationPath>
#include <osg/ArgumentParser>
#include <osg/NodeVisitor>

#include <osgDB/FileNameUtils>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

#include <osgGA/DriveManipulator>
#include <osgGA/GUIEventHandler>
#include <osgGA/GUIEventAdapter>
#include <osgGA/GUIActionAdapter>

#include <osgGA/AnimationPathManipulator>
#include <osgGA/KeySwitchMatrixManipulator>

#include <osgUtil/LineSegmentIntersector>
#include <osgAnimation/BasicAnimationManager>

#include <iostream>
using namespace std;

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)
{
    //osg::ArgumentParser arguments(&argc, argv);
    osg::ref_ptr<osgViewer::Viewer> viewer1 = new osgViewer::Viewer;
    osg::ref_ptr<osg::Group> group1 = new osg::Group;
    osg::ref_ptr<osg::MatrixTransform> matrixTransform1 = new osg::MatrixTransform;

    osg::ref_ptr<osg::Node> node1 = osgDB::readNodeFile("I:\\BIM\\fbx\\Worker201907.fbx");
    //osg::ref_ptr<osgAnimation::BasicAnimationManager> basicAnimationManager1 = dynamic_cast<osgAnimation::BasicAnimationManager*>(node1->getUpdateCallback());
    /*
    osgAnimation::BasicAnimationManager* basicAnimationManager1 = dynamic_cast<osgAnimation::BasicAnimationManager*>(node1->getUpdateCallback());
    osgAnimation::AnimationList animationList1 = basicAnimationManager1->getAnimationList();
    osgAnimation::AnimationList::iterator animationList_iter;
    for (animationList_iter = animationList1.begin();animationList_iter != animationList1.end();++animationList_iter)
    {
        std::string name = (*animationList_iter)->getName();
        std::cout << name << std::endl;
        basicAnimationManager1->playAnimation(*animationList_iter);
    }
*/
/*
    for (unsigned int i = 0; i<animationList1.size(); ++i)
    {
        const std::string& name = animationList1[i]->getName();
        basicAnimationManager1->playAnimation(animationList1[i].get());
        if (true)
        {
            std::cout << name << std::endl;
        }
    }
*/
    AnimationManagerFinder animationManagerFinder1;
    group1->accept(animationManagerFinder1);

    if (animationManagerFinder1._am.valid())
    {
        std::string playModeOpt;
        osgAnimation::Animation::PlayMode playMode = osgAnimation::Animation::LOOP;
        
        if (osgDB::equalCaseInsensitive(playModeOpt, "ONCE"))
        {
            playMode = osgAnimation::Animation::ONCE;
        }
        else if (osgDB::equalCaseInsensitive(playModeOpt, "STAY"))
        {
            playMode = osgAnimation::Animation::STAY;
        }
        else if (osgDB::equalCaseInsensitive(playModeOpt, "LOOP"))
        {
            playMode = osgAnimation::Animation::LOOP;
        }
        else if (osgDB::equalCaseInsensitive(playModeOpt, "PPONG"))
        {
            playMode = osgAnimation::Animation::PPONG;
        }

        for (osgAnimation::AnimationList::const_iterator animIter = animationManagerFinder1._am->getAnimationList().begin();
            animIter != animationManagerFinder1._am->getAnimationList().end(); 
            ++animIter)
        {
            (*animIter)->setPlayMode(playMode);
        }

    }

    matrixTransform1->setMatrix(osg::Matrix::translate(0.0, 0.0, 0.0));
    matrixTransform1->addChild(node1);
    group1->addChild(matrixTransform1);
    viewer1->setSceneData(group1);

    viewer1->setUpViewInWindow(200, 200, 800, 600, 0);
    return viewer1->run();
}

 

猜你喜欢

转载自www.cnblogs.com/herd/p/11137065.html