mad1231999

OPENGL PROBLAM

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