0

how to parsing file in this form my file is looks like this

v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
f 2//1 3//1 1//1
f 4//2 7//2 3//2

and my parsing code here:

ifstream fin;
string  reader;     
while(fin>>reader)

  {       
     if(reader == "v"){
      vec3 vertex;
      fin>>vertex.x>>vertex.y>>vertex.z;
      out_vertices.push_back(vertex);

     } else if(reader == "vn"){
     vec3 normal;
     fin>>normal.x>>normal.y>>normal.z;
     out_normals.push_back(normal);
     }else if(reader== "f"){
       vec2 facet;
       vec3 _normal,_vertex;
       fin>>_vertex.x>>_vertex.y>>_vertex.z >>_normal.x>>_normal.y>>_normal.z;
       facet.normal = _normal;
       facet.vetex = _vertex;
       out_facets.push_back(facet);
     }

 }

how ever it can't reading the line start with f? is there any good way to do that ?

genpfault
  • 51,148
  • 11
  • 85
  • 139
hahahaha
  • 11
  • 6
  • That file format looks familiar. Are you writing your own file format? – Asesh Apr 30 '18 at 04:00
  • 2
    @Asesh It's most probably an .obj file for loading models – Jay Wai Tan Apr 30 '18 at 04:07
  • @JayWaiTan That's what I was thinking too but not sure if comments are allowed – Asesh Apr 30 '18 at 04:08
  • 1
    http://www.cplusplus.com/reference/istream/istream/peek/ Read to see if after parsing number and /, if next is not / do not parse num – 138 Apr 30 '18 at 04:13
  • For a triangle, the format is f v/vt/vn v/vt/vn v/vt/vn, f 2//1 means the triangle will be using the vertex position at index 2, texture coordinates is ignored, vertex normal at index 1 (for the first vertex). – Jay Wai Tan Apr 30 '18 at 04:13
  • 1
    @138 is correct, you are parsing in the '/' into floats. Plus, you are supposed to read it as ints and use it as indices. – Jay Wai Tan Apr 30 '18 at 04:16
  • @138 still not get it – hahahaha Apr 30 '18 at 10:16
  • or try reading each line into a stringstream (ss), then read each component into a string using getline(ss, str, '/') the convert str to integer – 138 Apr 30 '18 at 15:55

0 Answers0