From 652577d0e698f3d2aef9ab42108ec41eae36d4a4 Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Wed, 29 Oct 2025 21:29:13 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(mesh):=20add=20optional=20vert?= =?UTF-8?q?ical=20flip=20when=20loading=20OBJ=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a boolean flag to Mesh3D constructor and loadOBJFile to optionally flip the Y coordinate of vertices. Update engine to use this flag for specific objects (e.g., mimikyu) that require flipping. --- include/Mesh3D.h | 4 ++-- src/core/Mesh3D.cpp | 11 +++++++---- src/core/engine.cpp | 3 ++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/Mesh3D.h b/include/Mesh3D.h index bde7940..0d90aed 100644 --- a/include/Mesh3D.h +++ b/include/Mesh3D.h @@ -12,8 +12,8 @@ namespace djalim { public: Mesh3D(); - Mesh3D(const std::string& filepath); - void loadOBJFile(const std::string& filepath); + Mesh3D(const std::string& filepath, bool flipObjectVertically = false); + void loadOBJFile(const std::string& filepath, bool flipObjectVertically = false); public: Texture2D texture; glm::vec3 position = {0.0f, 0.0f, 0.0f}; diff --git a/src/core/Mesh3D.cpp b/src/core/Mesh3D.cpp index 4041bd5..61add7b 100644 --- a/src/core/Mesh3D.cpp +++ b/src/core/Mesh3D.cpp @@ -23,11 +23,11 @@ struct objFace { djalim::Mesh3D::Mesh3D(){} -djalim::Mesh3D::Mesh3D(const std::string& filepath) { - loadOBJFile(filepath); +djalim::Mesh3D::Mesh3D(const std::string& filepath, bool flipObjectVertically) { + loadOBJFile(filepath, flipObjectVertically); } -void djalim::Mesh3D::loadOBJFile(const std::string& filepath) { +void djalim::Mesh3D::loadOBJFile(const std::string& filepath, bool flipObjectVertically) { std::ifstream file(filepath); if (!file.is_open()) { std::cerr << "Failed to open OBJ file: " << filepath << std::endl; @@ -48,7 +48,10 @@ void djalim::Mesh3D::loadOBJFile(const std::string& filepath) { if (type == "v") { float x, y, z; ss >> x >> y >> z; - vertices.push_back({x, y, z}); + if(flipObjectVertically) + vertices.push_back({x, -y, z}); + else + vertices.push_back({x, y, z}); numMeshVertices++; } else if (type == "vn") { diff --git a/src/core/engine.cpp b/src/core/engine.cpp index a29de3f..385ec04 100644 --- a/src/core/engine.cpp +++ b/src/core/engine.cpp @@ -65,7 +65,8 @@ void djalim::OpenGlEngine::createObject(std::string name, std::string textures, void djalim::OpenGlEngine::createObject(std::string name, std::string textures, std::string filepath){ // Load the mesh from the file - objects[name] = std::make_unique(Mesh3D(filepath)); + bool flip = (name == "mimikyu"); + objects[name] = std::make_unique(Mesh3D(filepath, flip)); bool textureLoaded = objects[name]->texture.loadTexture(textures);