♻️ (engine): refactor object storage to unique_ptr and improve mesh handling
- Add face counter in Mesh3D - Switch objects map to std::unique_ptr<Mesh3D> - Update VAO/VBO binding syntax accordingly - Adjust cube rendering logic and camera position - Simplify vertex data, load external model instead of hardcoded vertices - Change main window size to 1000x1000 - Clean up includes and debug prints
This commit is contained in:
		
							parent
							
								
									430b1fb364
								
							
						
					
					
						commit
						0ffaffc83e
					
				| @ -86,6 +86,7 @@ void djalim::Mesh3D::loadOBJFile(const std::string& filepath){ | |||||||
|         face.vertices[i] = f;  |         face.vertices[i] = f;  | ||||||
|       } |       } | ||||||
|       faces.push_back(face); |       faces.push_back(face); | ||||||
|  |       numFaces++; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|   // Pour chaque face
 |   // Pour chaque face
 | ||||||
|  | |||||||
| @ -2,6 +2,8 @@ | |||||||
| #include "Mesh3D.h" | #include "Mesh3D.h" | ||||||
| #include <cstdlib> | #include <cstdlib> | ||||||
| #include <iostream> | #include <iostream> | ||||||
|  | #include <iterator> | ||||||
|  | #include <memory> | ||||||
| #include <vector> | #include <vector> | ||||||
| 
 | 
 | ||||||
| djalim::OpenGlEngine::OpenGlEngine(const char* title, int width, int height) { | djalim::OpenGlEngine::OpenGlEngine(const char* title, int width, int height) { | ||||||
| @ -44,32 +46,33 @@ djalim::OpenGlEngine::OpenGlEngine(const char* title, int width, int height) { | |||||||
| 
 | 
 | ||||||
| void djalim::OpenGlEngine::createObject(std::string name, std::string textures, std::string filepath){ | void djalim::OpenGlEngine::createObject(std::string name, std::string textures, std::string filepath){ | ||||||
|    |    | ||||||
|    |   // Load the mesh from the file
 | ||||||
|   Mesh3D obj = Mesh3D(filepath); |   objects[name] = std::make_unique<Mesh3D>(Mesh3D(filepath)); | ||||||
|    |  | ||||||
|   objects[name] = obj; |  | ||||||
| 
 | 
 | ||||||
|   bool textureLoaded = obj.texture.loadTexture(textures); |   bool textureLoaded = objects[name]->texture.loadTexture(textures); | ||||||
|  |    | ||||||
|  |   std::cout << &(objects[name]) << std::endl;  | ||||||
|    |    | ||||||
|   if (!textureLoaded) { |   if (!textureLoaded) { | ||||||
|     std::cerr << "Failed to load " << name << " texture!" << std::endl; |     std::cerr << "Failed to load " << name << " texture!" << std::endl; | ||||||
|     exit(1); |     exit(1); | ||||||
|   } |   } | ||||||
|  |   std::cout << "texture loaded" << std::endl; | ||||||
| 
 | 
 | ||||||
|   //glGenVertexArrays(1, &VAO);
 |   //glGenVertexArrays(1, &VAO);
 | ||||||
|   glGenVertexArrays(1, &(objects[name].VAO)); |   glGenVertexArrays(1, &(objects[name]->VAO)); | ||||||
| 
 | 
 | ||||||
|   //glGenBuffers(1, &VBO);
 |   //glGenBuffers(1, &VBO);
 | ||||||
|   glGenBuffers(1, &(objects[name].VBO)); |   glGenBuffers(1, &((objects[name])->VBO)); | ||||||
|    |    | ||||||
|   //glBindVertexArray(VAO);
 |   //glBindVertexArray(VAO);
 | ||||||
|   glBindVertexArray(objects[name].VAO); |   glBindVertexArray((objects[name])->VAO); | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|   //glBindBuffer(GL_ARRAY_BUFFER, VBO);
 |   //glBindBuffer(GL_ARRAY_BUFFER, VBO);
 | ||||||
|   glBindBuffer(GL_ARRAY_BUFFER, objects[name].VBO); |   glBindBuffer(GL_ARRAY_BUFFER, (objects[name])->VBO); | ||||||
|    |    | ||||||
|   glBufferData(GL_ARRAY_BUFFER, sizeof(float)*obj.meshVertices.size(), obj.meshVertices.data(), GL_STATIC_DRAW); |   glBufferData(GL_ARRAY_BUFFER, sizeof(float)*(objects[name])->meshVertices.size(), (objects[name])->meshVertices.data(), GL_STATIC_DRAW); | ||||||
|    |    | ||||||
|   // Attribut de position
 |   // Attribut de position
 | ||||||
|   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); |   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); | ||||||
|  | |||||||
| @ -9,39 +9,15 @@ | |||||||
| #include <glm/gtc/type_ptr.hpp> | #include <glm/gtc/type_ptr.hpp> | ||||||
| #include <iostream> | #include <iostream> | ||||||
| 
 | 
 | ||||||
| // void djalim::cube(GLFWwindow* window, djalim::ShaderProgram shader, GLuint VAO, std::map<std::string, Texture2D>& textures){
 |  | ||||||
| //
 |  | ||||||
| //     shader.use();
 |  | ||||||
| //     shader.setUniform("ourTexture", 0);
 |  | ||||||
| //     textures["cube"].bind();
 |  | ||||||
| //
 |  | ||||||
| //     // Matrice de Projection
 |  | ||||||
| //     glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f);
 |  | ||||||
| //     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::vec3(0.0f, 0.0f, 0.0f), // Cible
 |  | ||||||
| //         glm::vec3(0.0f, 1.0f, 0.0f)); // Axe Haut
 |  | ||||||
| //     shader.setUniform("view", view);
 |  | ||||||
| //
 |  | ||||||
| //     // Matrice Modèle
 |  | ||||||
| //     glm::mat4 model = glm::mat4(1.0f);
 |  | ||||||
| //     model = glm::rotate(model, (float)glfwGetTime() * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
 |  | ||||||
| //     shader.setUniform("model", model);
 |  | ||||||
| //
 |  | ||||||
| //     // Dessiner le cube
 |  | ||||||
| //     glBindVertexArray(VAO);
 |  | ||||||
| //     glDrawArrays(GL_TRIANGLES, 0, 36);
 |  | ||||||
| //
 |  | ||||||
| // }
 |  | ||||||
| 
 | 
 | ||||||
| void djalim::cube(GLFWwindow* window, djalim::ShaderProgram shader, Objects3D& objects){ | void djalim::cube(GLFWwindow* window, djalim::ShaderProgram shader, Objects3D& objects){ | ||||||
| 
 | 
 | ||||||
|     shader.use(); |     shader.use(); | ||||||
|     shader.setUniform("ourTexture", 0); |     shader.setUniform("ourTexture", 0); | ||||||
|      |      | ||||||
|     objects["cube"].texture.bind(); |     (objects["cube"])->texture.bind(); | ||||||
|  |      | ||||||
|  |     //std::cout << &(objects["cube"].texture) << std::endl;
 | ||||||
|     //textures["cube"].bind();
 |     //textures["cube"].bind();
 | ||||||
| 
 | 
 | ||||||
|     // Matrice de Projection
 |     // Matrice de Projection
 | ||||||
| @ -49,19 +25,19 @@ void djalim::cube(GLFWwindow* window, djalim::ShaderProgram shader, Objects3D& o | |||||||
|     shader.setUniform("projection", projection); // Assurez-vous que setUniform pour mat4 est implémenté
 |     shader.setUniform("projection", projection); // Assurez-vous que setUniform pour mat4 est implémenté
 | ||||||
| 
 | 
 | ||||||
|     // Matrice de Vue
 |     // Matrice de Vue
 | ||||||
|     glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 9.0f), // Position caméra
 |     glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 11.0f), // Position caméra
 | ||||||
|         glm::vec3(0.0f, 0.0f, 0.0f), // Cible
 |         glm::vec3(0.0f, 2.0f, 0.0f), // Cible
 | ||||||
|         glm::vec3(0.0f, 1.0f, 0.0f)); // Axe Haut
 |         glm::vec3(0.0f, 1.0f, 0.0f)); // Axe Haut
 | ||||||
|     shader.setUniform("view", view); |     shader.setUniform("view", view); | ||||||
| 
 | 
 | ||||||
|     // Matrice Modèle
 |     // Matrice Modèle
 | ||||||
|     glm::mat4 model = glm::mat4(1.0f); |     glm::mat4 model = glm::mat4(1.0f); | ||||||
|     model = glm::rotate(model, (float)glfwGetTime() * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f)); |     ///model = glm::rotate(model, (float)glfwGetTime() * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
 | ||||||
|     shader.setUniform("model", model); |     shader.setUniform("model", model); | ||||||
| 
 | 
 | ||||||
|     // Dessiner le cube
 |     // Dessiner le cube
 | ||||||
|     glBindVertexArray(objects["cube"].VAO); |     glBindVertexArray((objects["cube"])->VAO); | ||||||
|     glDrawArrays(GL_TRIANGLES, 0, objects["cube"].numMeshVertices*3); |     glDrawArrays(GL_TRIANGLES, 0, (objects["cube"])->numFaces * 3); | ||||||
| 
 | 
 | ||||||
|     //std::cout << objects["cube"].VAO << std::endl;
 |     //std::cout << objects["cube"].VAO << std::endl;
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,10 +1,7 @@ | |||||||
| #define DEBUG true |  | ||||||
| 
 |  | ||||||
| #include "engine.h" | #include "engine.h" | ||||||
| #include "Mesh3D.h" |  | ||||||
| 
 | 
 | ||||||
| int main() { | int main() { | ||||||
|   djalim::OpenGlEngine engine = djalim::OpenGlEngine("Mon app", 800, 600); |   djalim::OpenGlEngine engine = djalim::OpenGlEngine("Mon app", 1000, 1000); | ||||||
|   engine.start(); |   engine.start(); | ||||||
|   return 0; |   return 0; | ||||||
| } | } | ||||||
|  | |||||||
| @ -2,53 +2,10 @@ | |||||||
| #include "Texture2D.h" | #include "Texture2D.h" | ||||||
| 
 | 
 | ||||||
| void djalim::OpenGlEngine::onCreate(){ | void djalim::OpenGlEngine::onCreate(){ | ||||||
|     glEnable(GL_DEPTH_TEST); |     glEnable(GL_DEPTH_TEST);  | ||||||
|     std::vector<float> vertices { |  | ||||||
|         -0.5f, -0.5f, -0.5f,  0.0f, 0.0f, |  | ||||||
|          0.5f, -0.5f, -0.5f,  1.0f, 0.0f, |  | ||||||
|          0.5f,  0.5f, -0.5f,  1.0f, 1.0f, |  | ||||||
|          0.5f,  0.5f, -0.5f,  1.0f, 1.0f, |  | ||||||
|         -0.5f,  0.5f, -0.5f,  0.0f, 1.0f, |  | ||||||
|         -0.5f, -0.5f, -0.5f,  0.0f, 0.0f, |  | ||||||
| 
 |  | ||||||
|         -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, |  | ||||||
|          0.5f, -0.5f,  0.5f,  1.0f, 0.0f, |  | ||||||
|          0.5f,  0.5f,  0.5f,  1.0f, 1.0f, |  | ||||||
|          0.5f,  0.5f,  0.5f,  1.0f, 1.0f, |  | ||||||
|         -0.5f,  0.5f,  0.5f,  0.0f, 1.0f, |  | ||||||
|         -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, |  | ||||||
| 
 |  | ||||||
|         -0.5f,  0.5f,  0.5f,  1.0f, 0.0f, |  | ||||||
|         -0.5f,  0.5f, -0.5f,  1.0f, 1.0f, |  | ||||||
|         -0.5f, -0.5f, -0.5f,  0.0f, 1.0f, |  | ||||||
|         -0.5f, -0.5f, -0.5f,  0.0f, 1.0f, |  | ||||||
|         -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, |  | ||||||
|         -0.5f,  0.5f,  0.5f,  1.0f, 0.0f, |  | ||||||
| 
 |  | ||||||
|          0.5f,  0.5f,  0.5f,  1.0f, 0.0f, |  | ||||||
|          0.5f,  0.5f, -0.5f,  1.0f, 1.0f, |  | ||||||
|          0.5f, -0.5f, -0.5f,  0.0f, 1.0f, |  | ||||||
|          0.5f, -0.5f, -0.5f,  0.0f, 1.0f, |  | ||||||
|          0.5f, -0.5f,  0.5f,  0.0f, 0.0f, |  | ||||||
|          0.5f,  0.5f,  0.5f,  1.0f, 0.0f, |  | ||||||
| 
 |  | ||||||
|         -0.5f, -0.5f, -0.5f,  0.0f, 1.0f, |  | ||||||
|          0.5f, -0.5f, -0.5f,  1.0f, 1.0f, |  | ||||||
|          0.5f, -0.5f,  0.5f,  1.0f, 0.0f, |  | ||||||
|          0.5f, -0.5f,  0.5f,  1.0f, 0.0f, |  | ||||||
|         -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, |  | ||||||
|         -0.5f, -0.5f, -0.5f,  0.0f, 1.0f, |  | ||||||
| 
 |  | ||||||
|         -0.5f,  0.5f, -0.5f,  0.0f, 1.0f, |  | ||||||
|          0.5f,  0.5f, -0.5f,  1.0f, 1.0f, |  | ||||||
|          0.5f,  0.5f,  0.5f,  1.0f, 0.0f, |  | ||||||
|          0.5f,  0.5f,  0.5f,  1.0f, 0.0f, |  | ||||||
|         -0.5f,  0.5f,  0.5f,  0.0f, 0.0f, |  | ||||||
|         -0.5f,  0.5f, -0.5f,  0.0f, 1.0f |  | ||||||
|     }; |  | ||||||
| 
 | 
 | ||||||
|     stbi_set_flip_vertically_on_load(true); |     stbi_set_flip_vertically_on_load(true); | ||||||
|     createObject("cube", "../assets/textures/prof.png", "../assets/models/UTAH_BLEND.obj"); |     createObject("cube", "../assets/textures/robot_diffuse.jpg", "../assets/models/robot.obj"); | ||||||
|      |      | ||||||
|     // textures["cube"] = Texture2D();
 |     // textures["cube"] = Texture2D();
 | ||||||
|     // bool textureLoaded = textures["cube"].loadTexture("../assets/textures/prof.png");
 |     // bool textureLoaded = textures["cube"].loadTexture("../assets/textures/prof.png");
 | ||||||
|  | |||||||
| @ -1,10 +1,10 @@ | |||||||
| #include "engine.h" | #include "engine.h" | ||||||
| #include <GL/gl.h> | #include <GL/gl.h> | ||||||
| #include "loops.h" | #include "loops.h" | ||||||
|  | #include <iostream> | ||||||
| 
 | 
 | ||||||
| void djalim::OpenGlEngine::onUpdate(){ | void djalim::OpenGlEngine::onUpdate(){ | ||||||
|   showFps(); |   showFps();  | ||||||
|    |  | ||||||
|   cube(window,shaderProgram,objects); |   cube(window,shaderProgram,objects); | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user