Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;OBJ文件解析器_C++_Opengl - Fatal编程技术网

C++ C++;OBJ文件解析器

C++ C++;OBJ文件解析器,c++,opengl,C++,Opengl,我正在为OBJ文件做一个文件解析器,所有的东西都放在正确的位置,但由于某种原因,它直到文件结束时才运行 void loader::readIn() { //!takes in the all the data and //!puts in string first. std::string line; while(!myFile.eof()) { linetype = unknown;//enum set to uknown

我正在为OBJ文件做一个文件解析器,所有的东西都放在正确的位置,但由于某种原因,它直到文件结束时才运行

    void loader::readIn()
{
    //!takes in the all the data and 
    //!puts in string first.
    std::string line;

    while(!myFile.eof())
    {

        linetype = unknown;//enum set to uknown
        line.clear(); // clear line
        ss.clear();  // clear string stream
        std::getline(myFile,line); //intake line , to string line

        //found = line.find("v "); //enum to check the line type i,e Face ,vertex
        if(line[0] == 'v') //! check to see if the first char is v
           {
             linetype = vertex;   

           }

    //  found = line.find("f ");
        if(line[0] == 'f') //! checkl to see if the first char is f
        {
            linetype = face;

        }

    //  found = line.find("vn ");
        if(line[0] == 'vn') //! checkl to see if the first char is vn 
        {

            linetype = vertexNormal;

        }
        //  found = line.find("vt ")
        if(line[0] == 'vt') //! checkl to see if the first char is vt
        {
            linetype = vertexTexture;

        }

            if(line[0] == ' ' || '#') // if the start of the line is empty or a #
        {
            line.clear();   //clear line
                std::getline(myFile,line); // intake the next line
        }



        switch(linetype)
        { 
        case vertex:     //!stores the verrtex floats in vert.

            ss >> vertexFloat[0] >> vertexFloat[1] >> vertexFloat[2];
            verts.push_back(new coordinate(vertexFloat[0],vertexFloat[1],vertexFloat[2])); //creates new coord
            linetype = unknown;
            break;

        case face:
            int n; // these are the counters for the float arrays
            int m;
            int b;
            n = 0;
            m = 0;
            b = 0;
            int faces[3];   //temperary float array
            int faceText[3];
            int faceNorm[3];
            ss.str(line);  //string stream  intake line
            ss.ignore(1); 
            while( !ss.eof())
            {

                ss >> faces[n]; // intake first umber
                n++;

                 if(ss.peek() == '/')
                 {
                     ss.ignore(1);

                     if(ss.peek() != '/')
                    { 
                      ss >> faceText[m];
                      m++;
                     }
                 }

                ss.ignore(1);
                ss >> faceNorm[b];
                b++;

             }


            for( int i = 0; i < 3 ; ++i)
            {
            totalFaces.push_back(faces[i]);  // push back all the ints on the correct
            faceTexture.push_back(faceText[i]); // vector
            faceNormal.push_back(faceNorm[i]);
            }
            break;

是我的obj文件。

在循环条件中检查
eof()
是不正确的。这不是预测,它表明前一次读取由于EOF而失败。例如,即使是空文件也不会以
.eof
true开头

此外,“vn”既不是一个字符,也不是两个字符<代码>行[0]肯定是一个字符,显然不能等于两个字符“vn”

您不必使用eof()。看看我的代码:

void Mesh::LoadObjModel(const char *filename)
{
  std::ifstream in(filename, std::ios::in);
  if (!in)
    {
        std::cerr << "Cannot open " << filename << std::endl;
        exit(1);

    }
  std::string line;
  while (std::getline(in, line))
  {
    //check v for vertices
     if (line.substr(0,2)=="v "){
        std::istringstream v(line.substr(2));
        glm::vec3 vert;
        double x,y,z;
        v>>x;v>>y;v>>z;
        vert=glm::vec3(x,y,z);
        vertices.push_back(vert);
  }
  //check for texture co-ordinate
  else if(line.substr(0,2)=="vt"){

      std::istringstream v(line.substr(3));
      glm::vec2 tex;
      int U,V;
      v>>U;v>>V;
      tex=glm::vec2(U,V);
      texture.push_back(tex);

  }
  //check for faces
  else if(line.substr(0,2)=="f "){
    int a,b,c; //to store mesh index
    int A,B,C; //to store texture index
    //std::istringstream v;
  //v.str(line.substr(2));
  const char* chh=line.c_str();
    sscanf (chh, "f %i/%i %i/%i %i/%i",&a,&A,&b,&B,&c,&C); //here it read the line start with f and store the corresponding values in the variables

    //v>>a;v>>b;v>>c;
    a--;b--;c--;
    A--;B--;C--;
    //std::cout<<a<<b<<c<<A<<B<<C;
    faceIndex.push_back(a);textureIndex.push_back(A);
    faceIndex.push_back(b);textureIndex.push_back(B);
    faceIndex.push_back(c);textureIndex.push_back(C);
  }

}
//the mesh data is finally calculated here
for(unsigned int i=0;i<faceIndex.size();i++)
{
    glm::vec3 meshData;
    glm::vec2 texData;
    meshData=glm::vec3(vertices[faceIndex[i]].x,vertices[faceIndex[i]].y,vertices[faceIndex[i]].z);
    texData=glm::vec2(texture[textureIndex[i]].x,texture[textureIndex[i]].y);
    meshVertices.push_back(meshData);
    texCoord.push_back(texData);
}

}
void Mesh::LoadObjModel(常量字符*文件名)
{
std::ifstream-in(文件名,std::ios::in);
如果(!in)
{
标准:cerr>y;v>>z;
vert=glm::vec3(x,y,z);
顶点。向后推(顶点);
}
//检查纹理坐标
否则,如果(行substr(0,2)=“vt”){
std::istringstream v(第3行);
glm::vec2-tex;
INTU,V;
v> >U;v>>v;
tex=glm::vec2(U,V);
纹理。推回(tex);
}
//检查人脸
否则,如果(第行substr(0,2)=“f”){
int a,b,c;//用于存储网格索引
int A,B,C;//用于存储纹理索引
//std::istringstream v;
//v、 str(品系substr(2));
const char*chh=line.c_str();
sscanf(chh,“f%i/%i%i/%i%i/%i”、&a、&b、&b、&c、&c);//在这里,它读取以f开头的行,并将相应的值存储在变量中
//v> >a;v>>b;v>>c;
a-;b-;c-;
A-;B-;C-;

//std::coutw我可以使用什么替代方法将eof()读取到文件的末尾。只需尝试读取,然后检查流的结果。如果仍然良好,则读取成功,字符串正常。如果没有,则完成。这很简单。为了使其真正方便,
std::getline(stream,line)
返回您可以直接测试的流-流在仍然正常时转换为
true
。您可以使用while(std::getline(myFile,line)){..此处的代码..}为什么要递减
a
b
c
a
b
c
@Ask,在obj文件索引中,从1开始,而不是从零开始,这是我递减的方式。
void Mesh::LoadObjModel(const char *filename)
{
  std::ifstream in(filename, std::ios::in);
  if (!in)
    {
        std::cerr << "Cannot open " << filename << std::endl;
        exit(1);

    }
  std::string line;
  while (std::getline(in, line))
  {
    //check v for vertices
     if (line.substr(0,2)=="v "){
        std::istringstream v(line.substr(2));
        glm::vec3 vert;
        double x,y,z;
        v>>x;v>>y;v>>z;
        vert=glm::vec3(x,y,z);
        vertices.push_back(vert);
  }
  //check for texture co-ordinate
  else if(line.substr(0,2)=="vt"){

      std::istringstream v(line.substr(3));
      glm::vec2 tex;
      int U,V;
      v>>U;v>>V;
      tex=glm::vec2(U,V);
      texture.push_back(tex);

  }
  //check for faces
  else if(line.substr(0,2)=="f "){
    int a,b,c; //to store mesh index
    int A,B,C; //to store texture index
    //std::istringstream v;
  //v.str(line.substr(2));
  const char* chh=line.c_str();
    sscanf (chh, "f %i/%i %i/%i %i/%i",&a,&A,&b,&B,&c,&C); //here it read the line start with f and store the corresponding values in the variables

    //v>>a;v>>b;v>>c;
    a--;b--;c--;
    A--;B--;C--;
    //std::cout<<a<<b<<c<<A<<B<<C;
    faceIndex.push_back(a);textureIndex.push_back(A);
    faceIndex.push_back(b);textureIndex.push_back(B);
    faceIndex.push_back(c);textureIndex.push_back(C);
  }

}
//the mesh data is finally calculated here
for(unsigned int i=0;i<faceIndex.size();i++)
{
    glm::vec3 meshData;
    glm::vec2 texData;
    meshData=glm::vec3(vertices[faceIndex[i]].x,vertices[faceIndex[i]].y,vertices[faceIndex[i]].z);
    texData=glm::vec2(texture[textureIndex[i]].x,texture[textureIndex[i]].y);
    meshVertices.push_back(meshData);
    texCoord.push_back(texData);
}

}