Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. std::vector<Model> ObjManager::Import(std::string path)
  2. {
  3.     HANDLE file = CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  4.  
  5.     if (FAILED(file))
  6.         throw std::runtime_error("ObjManager::Import CreateFileA error");
  7.  
  8.     DWORD fileSize = GetFileSize(file, 0);
  9.  
  10.     char* buffer = new char[fileSize];
  11.  
  12.     if (!ReadFile(file, buffer, fileSize, 0, 0))
  13.         throw std::runtime_error("ObjManager::Import ReadFile error");
  14.  
  15.     if (!CloseHandle(file))
  16.         throw std::runtime_error("ObjManager::Import CloseHandle error");
  17.  
  18.     std::istringstream ss(buffer);
  19.     std::string line;
  20.  
  21.     Mesh mesh;
  22.  
  23.     std::vector<Vertex2> vertices;
  24.     std::vector<int> indices;
  25.     float vertex[3];
  26.  
  27.     int x = 0;
  28.  
  29.     while (std::getline(ss, line))
  30.     {
  31.         if (line[0] == 'v') // vertex
  32.         {
  33.             std::string str = line.erase(0, 3);
  34.             char* result = &str[0];
  35.  
  36.             char* vert = strtok(result, " ");
  37.             vertex[0] = (float)atof(vert);
  38.  
  39.             vert = strtok(0, " ");
  40.             vertex[1] = (float)atof(vert);
  41.  
  42.             vert = strtok(0, " ");
  43.             vertex[2] = (float)atof(vert);
  44.  
  45.             vertices.push_back(Vertex2(Vector3(vertex[0], vertex[1], vertex[2]), Vector2<float>()));
  46.         }
  47.         else if (line[0] == 'g') // object name
  48.         {
  49.             std::string str = line.erase(0, 1);
  50.  
  51.  
  52.         }
  53.         else if (line[0] == 'f') // face
  54.         {
  55.             std::string str = line.erase(0, 2);
  56.  
  57.             char* index = strtok(&str[0], " ");
  58.             indices.push_back(atoi(index) - 1);
  59.  
  60.             index = strtok(0, " ");
  61.             indices.push_back(atoi(index) - 1);
  62.  
  63.             index = strtok(0, " ");
  64.             indices.push_back(atoi(index) - 1);
  65.         }
  66.         else if (line.substr(0, 7) == "mtllib ") // material file
  67.         {
  68.             // TODO: get material
  69.         }
  70.  
  71.         x++;
  72.     }
  73.  
  74.     mesh.vertices = vertices;
  75.     mesh.indices = indices;
  76.  
  77.     std::vector<Model> meshes;
  78.  
  79.     Model model;
  80.     model.mesh = mesh;
  81.  
  82.     meshes.push_back(model);
  83.  
  84.     return meshes;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement