From f5430f4dd7a15991b7096d55f3ddb2349e6c5eed Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Wed, 15 Oct 2025 10:03:25 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20Mesh3D=20class=20for=20loadin?= =?UTF-8?q?g=20OBJ=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/Mesh3D.h | 19 +++++++ src/core/Mesh3D.cpp | 123 ++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 8 ++- 3 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 include/Mesh3D.h create mode 100644 src/core/Mesh3D.cpp diff --git a/include/Mesh3D.h b/include/Mesh3D.h new file mode 100644 index 0000000..c3da6a1 --- /dev/null +++ b/include/Mesh3D.h @@ -0,0 +1,19 @@ +#ifndef _MESH3D +#define _MESH3D + +#include +#include + +namespace djalim { + + class Mesh3D { + public: + // Constructor + Mesh3D(const std::string& filepath); + std::vector meshVertices; + void loadOBJFile(const std::string& filepath); + }; +} + +#endif // !_MESH3D + diff --git a/src/core/Mesh3D.cpp b/src/core/Mesh3D.cpp new file mode 100644 index 0000000..6544d38 --- /dev/null +++ b/src/core/Mesh3D.cpp @@ -0,0 +1,123 @@ +#include "Mesh3D.h" +#include +#include +#include +#include +#include +#include + + +struct objVertex { + float x, y, z; +}; + +struct objVertexTexture{ + float u,v; +}; + +struct objFaceIndice{ + int v,vt,vn; +}; + +struct objFace { + objFaceIndice vertices[3]; +}; + + + +djalim::Mesh3D::Mesh3D(const std::string& filepath) { + loadOBJFile(filepath); +} + + +void djalim::Mesh3D::loadOBJFile(const std::string& filepath){ + + std::ifstream file(filepath); + if (!file.is_open()) { + std::cerr << "Failed to open OBJ file: " << filepath << std::endl; + exit(1); + } + std::string line; + std::vector vertices; + std::vector vertexNormal; + std::vector vertexTexture; + std::vector faces; + + while (std::getline(file, line)) { + std::stringstream ss(line); + std::string type; + ss >> type; + + if (type == "v"){ + float x, y, z; + ss >> x >> y >> z; + //std::cout << "found vertice: (" << x << ", " << y << ", " << z << ")" << std::endl; + objVertex v = {x, y, z}; + vertices.push_back(v); + } + else if (type == "vn"){ + float x, y, z; + ss >> x >> y >> z; + //std::cout << "found normal: (" << x << ", " << y << ", " << z << ")" << std::endl; + objVertex v = {x, y, z}; + vertexNormal.push_back(v); + } + else if (type == "vt"){ + float u, v; + ss >> u >> v; + //std::cout << "found texture: (" << u << ", " << v << ")" << std::endl; + objVertexTexture vt = {u, v}; + vertexTexture.push_back(vt); + } + else if (type == "f"){ + std::cout << "found face" << std::endl; + std::cout << ss.str() << std::endl; + objFace face; + // TODO Find a more elegant way to do this + for (int i = 0; i < 3; i++){ + int a, b, c; + char slash; + ss >> a >> slash >> b >> slash >> c; + //std::cout << "vertex " << i << " :(" << a << ", " << b << ", " << c << ")" << std::endl; + objFaceIndice f = {a-1, b-1, c-1}; + face.vertices[i] = f; + } + faces.push_back(face); + } + } + // Pour chaque face + for(int i = 0; i < faces.size() ; i++ ){ + objFace currentFace = faces[i]; + // pour chaque vertex de cette face + for (int j = 0; j < 3; j++){ + + int verticeIndex = currentFace.vertices[j].v; + + if(verticeIndex >= vertices.size()){ + std::cerr << "Error: Vertex index out of range" << std::endl; + exit(1); + } + + objVertex currentVertice = vertices[verticeIndex]; + + meshVertices.push_back(currentVertice.x); + meshVertices.push_back(currentVertice.y); + meshVertices.push_back(currentVertice.z); + + //uv + + int verticeTextureIndex = currentFace.vertices[j].vt; + + if(verticeTextureIndex >= vertexTexture.size()){ + std::cerr << "Error: Texture index out of range" << std::endl; + exit(1); + } + + objVertexTexture currentTexture = vertexTexture[verticeTextureIndex]; + + meshVertices.push_back(currentTexture.u); + meshVertices.push_back(currentTexture.v); + } + } +} + diff --git a/src/main.cpp b/src/main.cpp index a1ec38f..a0f318e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,11 @@ +#define DEBUG true + #include "engine.h" +#include "Mesh3D.h" int main() { - djalim::OpenGlEngine engine = djalim::OpenGlEngine("Mon app", 800, 600); - engine.start(); + //djalim::OpenGlEngine engine = djalim::OpenGlEngine("Mon app", 800, 600); + //engine.start(); + djalim::Mesh3D mesh = djalim::Mesh3D("../assets/models/UTAH_BLEND.obj"); return 0; }