♻️ (engine, Mesh3D): refactor 3D object handling

- 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.
This commit is contained in:
Djalim Simaila 2025-10-24 07:58:20 +02:00
parent f3cf6c3798
commit 430b1fb364
9 changed files with 33 additions and 23 deletions

View File

@ -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);
}

View File

@ -1,6 +1,7 @@
#ifndef _MESH3D
#define _MESH3D
#include "Texture2D.h"
#include <vector>
#include <string>
@ -8,10 +9,14 @@ namespace djalim {
class Mesh3D {
public:
// Constructor
Mesh3D();
Mesh3D(const std::string& filepath);
std::vector<float> meshVertices;
int numMeshVertices = 0;
void loadOBJFile(const std::string& filepath);
Texture2D texture;
GLuint VBO;
GLuint VAO;
};
}

View File

@ -6,8 +6,10 @@
#include "ShaderProgram.h"
#include "Texture2D.h"
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include "Mesh3D.h"
#define NBCALLBACKS 1
@ -19,7 +21,7 @@ struct Object3D {
GLuint VAO;
};
typedef std::map<std::string,Object3D> Objects3D;
typedef std::map<std::string,djalim::Mesh3D> Objects3D;
static bool gWireframe = false;
@ -37,7 +39,6 @@ namespace djalim {
double previousSeconds = 0.0;
double elapsedSeconds;
int frameCount = 0;
std::map<std::string,Texture2D> 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<float>& vertices);
void createObject(std::string name, std::string textures, std::string filepath);
void loadCallbacks();
void showFps();
void onCreate();

View File

@ -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++){

View File

@ -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);

View File

@ -1,4 +1,5 @@
#include "engine.h"
#include "Mesh3D.h"
#include <cstdlib>
#include <iostream>
#include <vector>
@ -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<float>& 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);

View File

@ -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;

View File

@ -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;
}

View File

@ -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");