mad1231999

Untitled

Jan 30th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. GLuint model::loadOBJ(const char *path, std::vector<glm::vec3> &outVertices, std::vector<glm::vec2> &outUVs, std::vector<glm::vec3> &outNormals) {
  2.     std::cout << "Loading OBJ file " << path << std::endl;
  3.     std::vector<float> vertexIndices;
  4.     std::vector<float> uvIndices;
  5.     std::vector<float> normalIndices;
  6.     std::vector<glm::vec3> tempVertices;
  7.     std::vector<glm::vec2> tempUVs;
  8.     std::vector<glm::vec3> tempNormals;
  9.  
  10.     std::ifstream file(path);
  11.  
  12.     if(!file.is_open()) {
  13.         std::cout << "Unable to open OBJ file " << path << std::endl;
  14.         return -1;
  15.     }
  16.  
  17.     char line[256];
  18.  
  19.     while(file.getline(line, 256)) {
  20.         char lineHeader[128];
  21.  
  22.         int res = sscanf(line, "%s", lineHeader);
  23.  
  24.         if(res == EOF)
  25.             break;
  26.  
  27.         if(strcmp(lineHeader, "v") == 0) {
  28.  
  29.             glm::vec3 vertex;
  30.             sscanf(line, "v %f %f %f\n", &(vertex.x), &(vertex.y), &(vertex.z));
  31.             std::cout << "Adding vertex... (" << vertex.x << ", " << vertex.y << ", " << vertex.y << ")" << std::endl;
  32.             tempVertices.push_back(vertex);
  33.  
  34.         } else if(strcmp(lineHeader, "vt") == 0) {
  35.  
  36.             std::cout << line << std::endl;
  37.             glm::vec2 uv;
  38.             sscanf(line, "vt %f %f\n", &(uv.x), &(uv.y));
  39.             tempUVs.push_back(uv);
  40.  
  41.         } else if(strcmp(lineHeader, "vn") == 0) {
  42.  
  43.             std::cout << line << std::endl;
  44.             glm::vec3 normal;
  45.             sscanf(line, "vn %f %f %f\n", &(normal.x), &(normal.y), &(normal.z));
  46.             tempNormals.push_back(normal);
  47.  
  48.         } else if(strcmp(lineHeader, "f") == 0) {
  49.  
  50.             std::string vertex1, vertex2, vertex3;
  51.             unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
  52.  
  53.             std::cout << line << std::endl;
  54.  
  55.             int matches = sscanf(line, "f %d//%d %d//%d %d//%d\n", &vertexIndex[0], &normalIndex[0],
  56.                                  &vertexIndex[1], &normalIndex[1], &vertexIndex[2], &normalIndex[2]);
  57.  
  58.             if(matches != 6) {
  59.                 std::cout << "Unable to parse OBJ file " << path << std::endl << "Matches: " << matches << std::endl;
  60.                 return -1;
  61.             }
  62.  
  63.             vertexIndices.push_back(vertexIndex[0]);
  64.             vertexIndices.push_back(vertexIndex[1]);
  65.             vertexIndices.push_back(vertexIndex[2]);
  66.             uvIndices.push_back(uvIndex[0]);
  67.             uvIndices.push_back(uvIndex[1]);
  68.             uvIndices.push_back(uvIndex[2]);
  69.             normalIndices.push_back(normalIndex[0]);
  70.             normalIndices.push_back(normalIndex[1]);
  71.             normalIndices.push_back(normalIndex[2]);
  72.         }
  73.     }
  74.  
  75.     for(unsigned int i = 0; i < vertexIndices.size(); i++) {
  76.         unsigned int vertexIndex = vertexIndices[i];
  77.  
  78.         glm::vec3 vertex = tempVertices[vertexIndex - 1];
  79.         outVertices.push_back(vertex);
  80.     }
  81.     std::cout << "Finished vertex stuff" << std::endl;
  82.  
  83.     /*
  84.     for(unsigned int i = 0; i < uvIndices.size(); i++) {
  85.         unsigned int uvIndex = uvIndices[i];
  86.  
  87.         glm::vec2 uv = tempUVs[uvIndex - 1];
  88.         outUVs.push_back(uv);
  89.     } */
  90.     std::cout << "Finished UV stuff" << std::endl;
  91.  
  92.     for(unsigned int i = 0; i < normalIndices.size(); i++) {
  93.         unsigned int normalIndex = normalIndices[i];
  94.  
  95.         glm::vec3 normal = tempNormals[normalIndex - 1];
  96.         outNormals.push_back(normal);
  97.     }
  98.     std::cout << "Finished normal stuff" << std::endl;
  99.  
  100.     std::cout << "Loaded OBJ file " << path << std::endl;
  101.  
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment