From 430b1fb36411163f846ddd3ecc2460ec160c2350 Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Fri, 24 Oct 2025 07:58:20 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(engine,=20Mesh3D):=20refa?= =?UTF-8?q?ctor=203D=20object=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Introduce Mesh3D class with vertices, texture, VBO/VAO. - Replace Object3D struct and textures map in engine with Mesh3D objects. - Update createObject to load from file path instead of vertex array. - Adjust cube rendering to use new vertex count. - Remove debug prints from Texture2D loading. - Toggle fragment shader color vs texture usage. --- assets/shaders/fragment.glsl | 4 ++-- include/Mesh3D.h | 7 ++++++- include/engine.h | 7 ++++--- src/core/Mesh3D.cpp | 7 +++++-- src/core/Texture2D.cpp | 4 ++-- src/core/engine.cpp | 16 +++++++++------- src/loops/cube.cpp | 4 ++-- src/main.cpp | 5 ++--- src/onCreate.cpp | 2 +- 9 files changed, 33 insertions(+), 23 deletions(-) diff --git a/assets/shaders/fragment.glsl b/assets/shaders/fragment.glsl index 5c910e4..bd0b499 100644 --- a/assets/shaders/fragment.glsl +++ b/assets/shaders/fragment.glsl @@ -6,6 +6,6 @@ uniform sampler2D ourTexture; void main() { - //FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); - FragColor = texture(ourTexture, TexCoord); + FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); + //FragColor = texture(ourTexture, TexCoord); } diff --git a/include/Mesh3D.h b/include/Mesh3D.h index c3da6a1..2a6946e 100644 --- a/include/Mesh3D.h +++ b/include/Mesh3D.h @@ -1,6 +1,7 @@ #ifndef _MESH3D #define _MESH3D +#include "Texture2D.h" #include #include @@ -8,10 +9,14 @@ namespace djalim { class Mesh3D { public: - // Constructor + Mesh3D(); Mesh3D(const std::string& filepath); std::vector meshVertices; + int numMeshVertices = 0; void loadOBJFile(const std::string& filepath); + Texture2D texture; + GLuint VBO; + GLuint VAO; }; } diff --git a/include/engine.h b/include/engine.h index 2c2f6f6..98f351a 100644 --- a/include/engine.h +++ b/include/engine.h @@ -6,8 +6,10 @@ #include "ShaderProgram.h" #include "Texture2D.h" #include +#include #include #include +#include "Mesh3D.h" #define NBCALLBACKS 1 @@ -19,7 +21,7 @@ struct Object3D { GLuint VAO; }; -typedef std::map Objects3D; +typedef std::map Objects3D; static bool gWireframe = false; @@ -37,7 +39,6 @@ namespace djalim { double previousSeconds = 0.0; double elapsedSeconds; int frameCount = 0; - std::map textures; ShaderProgram shaderProgram; Objects3D objects; @@ -49,7 +50,7 @@ namespace djalim { void onUpdate(); void loadHints(); - void createObject(std::string name, std::string textures, std::vector& vertices); + void createObject(std::string name, std::string textures, std::string filepath); void loadCallbacks(); void showFps(); void onCreate(); diff --git a/src/core/Mesh3D.cpp b/src/core/Mesh3D.cpp index 6544d38..fe5ad61 100644 --- a/src/core/Mesh3D.cpp +++ b/src/core/Mesh3D.cpp @@ -25,6 +25,8 @@ struct objFace { +djalim::Mesh3D::Mesh3D(){} + djalim::Mesh3D::Mesh3D(const std::string& filepath) { loadOBJFile(filepath); } @@ -54,6 +56,7 @@ void djalim::Mesh3D::loadOBJFile(const std::string& filepath){ //std::cout << "found vertice: (" << x << ", " << y << ", " << z << ")" << std::endl; objVertex v = {x, y, z}; vertices.push_back(v); + numMeshVertices++; } else if (type == "vn"){ float x, y, z; @@ -70,8 +73,8 @@ void djalim::Mesh3D::loadOBJFile(const std::string& filepath){ vertexTexture.push_back(vt); } else if (type == "f"){ - std::cout << "found face" << std::endl; - std::cout << ss.str() << std::endl; + // 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++){ diff --git a/src/core/Texture2D.cpp b/src/core/Texture2D.cpp index d6e8ac9..0fcc19d 100644 --- a/src/core/Texture2D.cpp +++ b/src/core/Texture2D.cpp @@ -21,8 +21,8 @@ bool Texture2D::loadTexture(const std::string& filename, bool generateMipmaps) { return false; } - std::cout << "Texture loaded successfully: " << filename << std::endl; - std::cout << "Width: " << width << ", Height: " << height << ", Components: " << components << std::endl; + //std::cout << "Texture loaded successfully: " << filename << std::endl; + //std::cout << "Width: " << width << ", Height: " << height << ", Components: " << components << std::endl; glGenTextures(1, &mTexture); glBindTexture(GL_TEXTURE_2D, mTexture); diff --git a/src/core/engine.cpp b/src/core/engine.cpp index c7444bd..093689c 100644 --- a/src/core/engine.cpp +++ b/src/core/engine.cpp @@ -1,4 +1,5 @@ #include "engine.h" +#include "Mesh3D.h" #include #include #include @@ -41,11 +42,14 @@ djalim::OpenGlEngine::OpenGlEngine(const char* title, int width, int height) { } -void djalim::OpenGlEngine::createObject(std::string name, std::string textures, std::vector& vertices){ +void djalim::OpenGlEngine::createObject(std::string name, std::string textures, std::string filepath){ - objects[name]; + + Mesh3D obj = Mesh3D(filepath); + + objects[name] = obj; - bool textureLoaded = objects[name].texture.loadTexture(textures); + bool textureLoaded = obj.texture.loadTexture(textures); if (!textureLoaded) { std::cerr << "Failed to load " << name << " texture!" << std::endl; @@ -64,10 +68,8 @@ void djalim::OpenGlEngine::createObject(std::string name, std::string textures, //glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, objects[name].VBO); - - - std::cout << "Loading 3D objects with "<< vertices.size() << " vertices"<< std::endl; - glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vertices.size(), vertices.data(), GL_STATIC_DRAW); + + glBufferData(GL_ARRAY_BUFFER, sizeof(float)*obj.meshVertices.size(), obj.meshVertices.data(), GL_STATIC_DRAW); // Attribut de position glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); diff --git a/src/loops/cube.cpp b/src/loops/cube.cpp index c6c0572..fb9df93 100644 --- a/src/loops/cube.cpp +++ b/src/loops/cube.cpp @@ -49,7 +49,7 @@ void djalim::cube(GLFWwindow* window, djalim::ShaderProgram shader, Objects3D& o shader.setUniform("projection", projection); // Assurez-vous que setUniform pour mat4 est implémenté // Matrice de Vue - glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f), // Position caméra + glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 9.0f), // Position caméra glm::vec3(0.0f, 0.0f, 0.0f), // Cible glm::vec3(0.0f, 1.0f, 0.0f)); // Axe Haut shader.setUniform("view", view); @@ -61,7 +61,7 @@ void djalim::cube(GLFWwindow* window, djalim::ShaderProgram shader, Objects3D& o // Dessiner le cube glBindVertexArray(objects["cube"].VAO); - glDrawArrays(GL_TRIANGLES, 0, 36); + glDrawArrays(GL_TRIANGLES, 0, objects["cube"].numMeshVertices*3); //std::cout << objects["cube"].VAO << std::endl; diff --git a/src/main.cpp b/src/main.cpp index a0f318e..fc22cd8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,8 +4,7 @@ #include "Mesh3D.h" int main() { - //djalim::OpenGlEngine engine = djalim::OpenGlEngine("Mon app", 800, 600); - //engine.start(); - djalim::Mesh3D mesh = djalim::Mesh3D("../assets/models/UTAH_BLEND.obj"); + djalim::OpenGlEngine engine = djalim::OpenGlEngine("Mon app", 800, 600); + engine.start(); return 0; } diff --git a/src/onCreate.cpp b/src/onCreate.cpp index 5207619..2cfecbd 100644 --- a/src/onCreate.cpp +++ b/src/onCreate.cpp @@ -48,7 +48,7 @@ void djalim::OpenGlEngine::onCreate(){ }; stbi_set_flip_vertically_on_load(true); - createObject("cube", "../assets/textures/prof.png", vertices); + createObject("cube", "../assets/textures/prof.png", "../assets/models/UTAH_BLEND.obj"); // textures["cube"] = Texture2D(); // bool textureLoaded = textures["cube"].loadTexture("../assets/textures/prof.png");