271018426 发表于 2021-10-8 17:09:48

MD2模型不能读取

代码如下:
    #include<osgDB/ReadFile>
#include<osgViewer/Viewer>
#include<osg/Switch>
#include<osgGA/GUIEventHandler>
#include<osgDB/FileUtils>

class DisplayMD2:public osgGA::GUIEventHandler
{
public:
        DisplayMD2(osg::Switch* tempSd1,osg::Switch* tempWp1)      //构造函数
        {
                currentPos=0;
                maxsd1Pos=0;
                maxwp1Pos=0;
                sd1=tempSd1;
                wp1=tempWp1;
                std::cout<<"这里有输出111"<<std::endl;
                if(sd1&&wp1)
                {
                        std::cout<<"这里有输出"<<std::endl;
                        maxsd1Pos=sd1->getNumChildren();   //赋值:sd1的孩子的最大数量
                        maxwp1Pos=wp1->getNumChildren();   //赋值:wp1的孩子的最大数量
                }
        }
        bool handle(const osgGA::GUIEventAdapter &ea,osgGA::GUIActionAdapter &aa)
        {
                if(sd1&&wp1)
                {
                        if(ea.getEventType()==osgGA::GUIEventAdapter::KEYDOWN)
                        {
                                if(ea.getKey()==osgGA::GUIEventAdapter::KEY_Right)
                                {
                                        currentPos++;
                                        sd1->setAllChildrenOff();    //将士兵所以孩子关闭
                                        sd1->setSingleChildOn(currentPos% (maxsd1Pos-1));   //按顺序打开士兵一个单独的孩子。
                                        wp1->setAllChildrenOff();   //武器所有孩子关闭
                                        if(currentPos%(maxsd1Pos-1)<=(maxwp1Pos-1))            //如果当前的动作小于武器动作库的最大数量则进入语句
                                        {                                                         //用余数就是要弄个循环,currentPos可以一直往上加,不用重置了。
                                                wp1->setSingleChildOn(currentPos% (maxsd1Pos-1));          //按顺序打开武器单独孩子
                                        }
                                }
                        }
                }
                return false;
        }
private:
        int currentPos;
        int maxsd1Pos;
        int maxwp1Pos;
        osg::Switch* sd1;
        osg::Switch* wp1;
};


int main()
{
        osg::ref_ptr<osgViewer::Viewer>viewer=new osgViewer::Viewer;
        osgDB::FilePathList f1=osgDB::getDataFilePathList();
        //push_back:在Vector最后添加一个元素
        f1.push_back(std::string(getenv("OSG_FILE_PATH"))+"\\s0\\");      //获取字符串,从而获得环境变量
        osgDB::setDataFilePathList(f1);

          
        osg::ref_ptr<osg::Switch> sd1=dynamic_cast<osg::Switch* >(osgDB::readNodeFile("s0/tris.MD2"));
        osg::ref_ptr<osg::Switch> wp1=dynamic_cast<osg::Switch* >(osgDB::readNodeFile("s0/weapon.MD2"));
        osg::ref_ptr<osg::Group>root=new osg::Group;
        root->addChild(sd1);
        root->addChild(wp1);
        viewer->addEventHandler(new DisplayMD2(sd1.get(),wp1.get()));
        viewer->setSceneData(root);
        return viewer->run();

}

       本程序是参照《三维视景仿真技术开发详解》里的示例3-10.主要就是读取MD2模型然后响应鼠标事件,进行动画播放,但是我不能将MD2模型数据读取进来,不知道是哪里出现问题了。其次对osgDB::FilePathList这种获取模型数据的方式不太了解。

       运行画面没有模型,且cout只有一句话,说明那个if语句没有进去,怀疑sd1和wp1是空的,没有读取到MD2数据。
   
页: [1]
查看完整版本: MD2模型不能读取