Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::vector<Model> ObjManager::Import(std::string path)
- {
- HANDLE file = CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- if (FAILED(file))
- throw std::runtime_error("ObjManager::Import CreateFileA error");
- DWORD fileSize = GetFileSize(file, 0);
- char* buffer = new char[fileSize];
- if (!ReadFile(file, buffer, fileSize, 0, 0))
- throw std::runtime_error("ObjManager::Import ReadFile error");
- if (!CloseHandle(file))
- throw std::runtime_error("ObjManager::Import CloseHandle error");
- std::istringstream ss(buffer);
- std::string line;
- Mesh mesh;
- std::vector<Vertex2> vertices;
- std::vector<int> indices;
- float vertex[3];
- int x = 0;
- while (std::getline(ss, line))
- {
- if (line[0] == 'v') // vertex
- {
- std::string str = line.erase(0, 3);
- char* result = &str[0];
- char* vert = strtok(result, " ");
- vertex[0] = (float)atof(vert);
- vert = strtok(0, " ");
- vertex[1] = (float)atof(vert);
- vert = strtok(0, " ");
- vertex[2] = (float)atof(vert);
- vertices.push_back(Vertex2(Vector3(vertex[0], vertex[1], vertex[2]), Vector2<float>()));
- }
- else if (line[0] == 'g') // object name
- {
- std::string str = line.erase(0, 1);
- }
- else if (line[0] == 'f') // face
- {
- std::string str = line.erase(0, 2);
- char* index = strtok(&str[0], " ");
- indices.push_back(atoi(index) - 1);
- index = strtok(0, " ");
- indices.push_back(atoi(index) - 1);
- index = strtok(0, " ");
- indices.push_back(atoi(index) - 1);
- }
- else if (line.substr(0, 7) == "mtllib ") // material file
- {
- // TODO: get material
- }
- x++;
- }
- mesh.vertices = vertices;
- mesh.indices = indices;
- std::vector<Model> meshes;
- Model model;
- model.mesh = mesh;
- meshes.push_back(model);
- return meshes;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement