查看: 1547|回复: 0

MD2模型不能读取

[复制链接]

该用户从未签到

发表于 2021-10-8 17:09:48 | 显示全部楼层 |阅读模式
代码如下:
    #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数据。
   

只cout一句话

只cout一句话

运行画面

运行画面
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表