Advertisement
Guest User

.obj loader

a guest
Sep 20th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. void load(char* fileName, vector<glm::vec3> &vertices, vector<glm::vec2> &texCoords, vector<glm::vec3> &normals) {
  2.     vector<unsigned int> vertexIndices, uvIndices, normalIndices;
  3.     vector<glm::vec3> temp_vertices;
  4.     vector<glm::vec2> temp_uvs;
  5.     vector<glm::vec3> temp_normals;
  6.  
  7.  
  8.     FILE * file = fopen(fileName, "r");
  9.     if( file == NULL ){
  10.         cout << "Nie mozna otworzyc pliku!" << endl;
  11.         getchar();
  12.     }
  13.  
  14.     while( 1 ){
  15.  
  16.         char lineHeader[128];
  17.         int res = fscanf(file, "%s", lineHeader);
  18.         if (res == EOF)
  19.             break;
  20.        
  21.         if (strcmp( lineHeader, "v" ) == 0 ){
  22.             glm::vec3 vertex;
  23.             fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z );
  24.             temp_vertices.push_back(vertex);
  25.         } else if (strcmp( lineHeader, "vt" ) == 0 ) {
  26.             glm::vec2 uv;
  27.             fscanf(file, "%f %f\n", &uv.x, &uv.y );
  28.             uv.y = -uv.y;
  29.             temp_uvs.push_back(uv);
  30.         } else if (strcmp( lineHeader, "vn" ) == 0 ) {
  31.             glm::vec3 normal;
  32.             fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z );
  33.             temp_normals.push_back(normal);
  34.         } else if ( strcmp( lineHeader, "f" ) == 0 ) {
  35.             string vertex1, vertex2, vertex3;
  36.             unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
  37.             int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2] );
  38.             if (matches != 9){
  39.                 cout << "Nie mozna odczytac pliku!" << endl;
  40.         }
  41.             vertexIndices.push_back(vertexIndex[0]);
  42.             vertexIndices.push_back(vertexIndex[1]);
  43.             vertexIndices.push_back(vertexIndex[2]);
  44.             uvIndices    .push_back(uvIndex[0]);
  45.             uvIndices    .push_back(uvIndex[1]);
  46.             uvIndices    .push_back(uvIndex[2]);
  47.             normalIndices.push_back(normalIndex[0]);
  48.             normalIndices.push_back(normalIndex[1]);
  49.             normalIndices.push_back(normalIndex[2]);
  50.         } else {
  51.             char stupidBuffer[1000];
  52.             fgets(stupidBuffer, 1000, file);
  53.         }
  54.  
  55.     }
  56.  
  57.     for (unsigned int i=0; i<vertexIndices.size(); i++) {
  58.  
  59.         unsigned int vertexIndex = vertexIndices[i];
  60.         unsigned int uvIndex = uvIndices[i];
  61.         unsigned int normalIndex = normalIndices[i];
  62.        
  63.         glm::vec3 vertex = temp_vertices[ vertexIndex-1 ];
  64.         glm::vec2 uv = temp_uvs[ uvIndex-1 ];
  65.         glm::vec3 normal = temp_normals[ normalIndex-1 ];
  66.        
  67.         vertices.push_back(vertex);
  68.         texCoords.push_back(uv);
  69.         normals.push_back(normal);
  70.    
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement