|
@@ -18,7 +18,132 @@
|
|
|
|
|
|
#include "AssetManager.hpp"
|
|
|
|
|
|
-void AssetManager::loadOBJ(const std::string &path,
|
|
|
+#include "System.hpp"
|
|
|
+
|
|
|
+#include <vector>
|
|
|
+#include <stdio.h>
|
|
|
+
|
|
|
+//#define TINYOBJLOADER_IMPLEMENTATION
|
|
|
+//#include <tiny_obj_loader.h>
|
|
|
+#include <glm/glm.hpp>
|
|
|
+#include <assimp/Importer.hpp>
|
|
|
+#include <assimp/scene.h>
|
|
|
+#include <assimp/postprocess.h>
|
|
|
+
|
|
|
+/*void AssetManager::loadOBJ(const std::string &path,
|
|
|
const std::string &name)
|
|
|
{
|
|
|
+ std::vector<unsigned int> vertexIndices,
|
|
|
+ uvIndices, normalIndices;
|
|
|
+ std::vector<glm::vec3> temp_vertices;
|
|
|
+ std::vector<glm::vec2> temp_uvs;
|
|
|
+ std::vector<glm::vec3> temp_normals;
|
|
|
+
|
|
|
+ FILE *file = fopen(path.c_str(), "r");
|
|
|
+ if(not file)
|
|
|
+ {
|
|
|
+ System::logger->writeError("Failed to open file.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ while(true)
|
|
|
+ {
|
|
|
+ char lineHeader[128];
|
|
|
+ int res = fscanf(file, "%s", lineHeader);
|
|
|
+ if(res == EOF)
|
|
|
+ break;
|
|
|
+
|
|
|
+ if(strcmp(lineHeader, "v") == 0)
|
|
|
+ {
|
|
|
+ glm::vec3 vertex;
|
|
|
+ fscanf(file, "%f %f %f\n",
|
|
|
+ &vertex.x, &vertex.y, &vertex.z);
|
|
|
+ temp_vertices.push_back(vertex);
|
|
|
+ }
|
|
|
+ else if(strcmp(lineHeader, "vt") == 0)
|
|
|
+ {
|
|
|
+ glm::vec2 uv;
|
|
|
+ fscanf(file, "%f %f\n", &uv.x, &uv.y);
|
|
|
+ temp_uvs.push_back(uv);
|
|
|
+ }
|
|
|
+ else if(strcmp(lineHeader, "vn") == 0)
|
|
|
+ {
|
|
|
+ glm::vec3 normal;
|
|
|
+ fscanf(file, "%f %f %f\n",
|
|
|
+ &normal.x, &normal.y, &normal.z);
|
|
|
+ temp_normals.push_back(normal);
|
|
|
+ }
|
|
|
+ else if(strcmp(lineHeader, "f") == 0)
|
|
|
+ {
|
|
|
+ std::string v1, v2, v3;
|
|
|
+ unsigned int vIndex[3], uvIndex[3], normIndex[3];
|
|
|
+ int matches = fscanf(file,
|
|
|
+ "%d/%d/%d %d/%d/%d %d/%d/%d\n",
|
|
|
+ &vIndex[0], &uvIndex[0], &normIndex[0],
|
|
|
+ &vIndex[1], &uvIndex[1], &normIndex[1],
|
|
|
+ &vIndex[2], &uvIndex[2], &normIndex[2]);
|
|
|
+ if(matches not_eq 9)
|
|
|
+ {
|
|
|
+ System::logger->writeError(
|
|
|
+ "OBJ file is too complicated.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ for(auto &i : vIndex)
|
|
|
+ vertexIndices.push_back(i);
|
|
|
+ for(auto &i : uvIndex)
|
|
|
+ uvIndices.push_back(i);
|
|
|
+ for(auto &i : normIndex)
|
|
|
+ normalIndices.push_back(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}*/
|
|
|
+
|
|
|
+void AssetManager::loadModel(const std::string &path,
|
|
|
+ const std::string &name)
|
|
|
+{
|
|
|
+ Assimp::Importer importer;
|
|
|
+ const aiScene *scene = importer.ReadFile(path,
|
|
|
+ aiProcess_Triangulate);
|
|
|
+ if(not scene)
|
|
|
+ {
|
|
|
+ System::logger->writeError(importer.GetErrorString());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const aiMesh *mesh = scene->mMeshes[0];
|
|
|
+
|
|
|
+ std::vector<struct Vertex> vertices;
|
|
|
+ std::vector<unsigned int> indices;
|
|
|
+
|
|
|
+ vertices.reserve(mesh->mNumVertices);
|
|
|
+ for(unsigned int i = 0; i < mesh->mNumVertices; ++i)
|
|
|
+ {
|
|
|
+ aiVector3D pos = mesh->mVertices[i];
|
|
|
+ aiVector3D norm = mesh->mNormals[i];
|
|
|
+
|
|
|
+ vertices.push_back({
|
|
|
+ glm::vec3(pos.x, pos.y, pos.z),
|
|
|
+ glm::vec3(norm.x, norm.y, norm.z)});
|
|
|
+ }
|
|
|
+
|
|
|
+ indices.reserve(3 * mesh->mNumFaces);
|
|
|
+ for(unsigned int i = 0; i < mesh->mNumFaces; ++i)
|
|
|
+ {
|
|
|
+ for(unsigned int j = 0; j < 3; ++j)
|
|
|
+ indices.push_back(mesh->mFaces[i].mIndices[j]);
|
|
|
+ }
|
|
|
+
|
|
|
+ models.insert({ name,
|
|
|
+ std::make_shared<Model>(
|
|
|
+ vertices, indices)});
|
|
|
+}
|
|
|
+
|
|
|
+std::shared_ptr<Model> AssetManager::getModel(
|
|
|
+ const std::string &name)
|
|
|
+{
|
|
|
+ return models.at(name);
|
|
|
+}
|
|
|
+
|
|
|
+void AssetManager::unloadModel(const std::string &name) {
|
|
|
+ models.erase(name);
|
|
|
}
|