Beispiel #1
0
ObjLoader::ObjLoader(std::string filepath)
{
        std::ifstream file;
        file.open(filepath.c_str(), std::ios::in);


       for(std::string line; std::getline(file, line);)
       {
               std::stringstream ss(line);
               std::string identifier;

               ss >> identifier;

               if(identifier.compare("#") ==0)
               {
                       std::cout << "Found comment" << std::endl;
               }
               else if(identifier.compare("v") == 0)
               {
                        std::vector<float> vec3 = loadNumbers(ss);
                        vertices.push_back(Vertex(vec3[0], vec3[1], vec3[2]));
               }
               else if(identifier.compare("vn") == 0)
               {
                               std::vector<float> vec3 = loadNumbers(ss);
                                normals.push_back(triplet<float, float, float>(vec3[0], vec3[1], vec3[2]));
               }
               else if(identifier.compare("vt") == 0)
               {
                       //std::cout << "tex coord found" << std::endl;
                       std::pair<float, float> texCoord = loadTexCoords(ss);
                       texCoords.push_back(texCoord);
               }
               else if(identifier.compare("f") == 0)
               {
                       std::string face;

                       ss >> face;

                       Face newFace;
                       while(!ss.fail())
                       {
                               std::vector<std::pair<int, bool> > vec3 = loadFace(face);
                               newFace.addVertex(vec3[0].first);
                               if(vec3[1].second)
                               {
                                       newFace.addTexCoord(vec3[1].first);
                               }
                               if(vec3[2].second)
                               {
                                       newFace.addNormal(vec3[2].first);
                               }
                               ss >> face;
                       }

                       for(int i = 0; i < 3; i ++)
                       {
                               int vertexIdx = newFace.getVertex(i);
                               Vertex vert = vertices[vertexIdx];
                               triplet<float, float, float> normal = normals[newFace.getNormal(i)];
                               vert.addNormal(normal.first, normal.second, normal.third);
                               vertices[vertexIdx] = vert;
                       }

                       faces.push_back(newFace);
               }
       }