#include #include #include #include #include #include "Loader.h" #include "ObjLoader.h" /* * Grotendeels van deze functies zijn gemaakt door: * https://github.com/Hopson97/ThinMatrix-OpenGL-Engine/blob/master/Source/Render_Engine/OBJLoader.cpp */ static void split(const std::string& s, char delim, std::vector& elems) { std::stringstream ss; ss.str(s); std::string item; while (getline(ss, item, delim)) { elems.push_back(item); } } static std::vector split(const std::string& s, char delim) { std::vector elems; split(s, delim, elems); return elems; } static void processVertex(const std::vector& vertexData, const std::vector& normals, const std::vector& textures, std::vector& indices, std::vector& textureArray, std::vector& normalArray) { GLuint currentVertexPointer = std::stoi(vertexData.at(0)) - 1; indices.push_back(currentVertexPointer); glm::vec2 currentTexture = textures.at(std::stoi(vertexData.at(1)) - 1); textureArray[(currentVertexPointer * 2) % textureArray.size()] = currentTexture.x; textureArray[(currentVertexPointer * 2 + 1) % textureArray.size()] = 1 - currentTexture.y; glm::vec3 currentNorm = normals.at(std::stoi(vertexData.at(2)) - 1); normalArray[currentVertexPointer * 3] = currentNorm.x; normalArray[currentVertexPointer * 3 + 1] = currentNorm.y; normalArray[currentVertexPointer * 3 + 2] = currentNorm.z; } models::RawModel LoadObjModel(std::string fileName) { std::ifstream inFile (fileName); if ( !inFile.is_open() ) { throw std::runtime_error ( "Could not open model file " + fileName + ".obj!" ); } std::vector vertices; std::vector normals; std::vector textures; std::vector indices; std::vector vertexArray; std::vector normalArray; std::vector textureArray; std::string line; try { while (std::getline(inFile, line)) { std::vector splitline = split(line, ' '); if (splitline.at(0) == "v") { glm::vec3 vertex; vertex.x = std::stof(splitline.at(1)); vertex.y = std::stof(splitline.at(2)); vertex.z = std::stof(splitline.at(3)); vertices.push_back(vertex); } else if (splitline.at(0) == "vt") { glm::vec2 texture; texture.x = std::stof(splitline.at(1)); texture.y = std::stof(splitline.at(2)); textures.push_back(texture); } else if (splitline.at(0) == "vn") { glm::vec3 normal; normal.x = std::stof(splitline.at(1)); normal.y = std::stof(splitline.at(2)); normal.z = std::stof(splitline.at(3)); normals.push_back(normal); } else if (splitline.at(0) == "f") { normalArray = std::vector(vertices.size() * 3); textureArray = std::vector(textures.size() * 2); break; } } while (true) { std::vector splitline = split(line, ' '); std::vector vertex1 = split(splitline.at(1), '/'); std::vector vertex2 = split(splitline.at(2), '/'); std::vector vertex3 = split(splitline.at(3), '/'); processVertex(vertex1, normals, textures, indices, textureArray, normalArray); processVertex(vertex2, normals, textures, indices, textureArray, normalArray); processVertex(vertex3, normals, textures, indices, textureArray, normalArray); if (!std::getline(inFile, line)) { break; } } } catch (const std::exception& e) { // Always go in here } inFile.close(); vertexArray = std::vector( vertices.size() * 3 ); int p = 0; for ( auto& vertex : vertices ) { vertexArray[p++] = vertex.x; vertexArray[p++] = vertex.y; vertexArray[p++] = vertex.z; } return renderEngine::loader::LoadToVAO( vertexArray, textureArray, indices); }