♻️ (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;  | ||||
|       } | ||||
|       faces.push_back(face); | ||||
|       numFaces++; | ||||
|     } | ||||
|   } | ||||
|   // Pour chaque face
 | ||||
|  | ||||
| @ -2,6 +2,8 @@ | ||||
| #include "Mesh3D.h" | ||||
| #include <cstdlib> | ||||
| #include <iostream> | ||||
| #include <iterator> | ||||
| #include <memory> | ||||
| #include <vector> | ||||
| 
 | ||||
| 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){ | ||||
|    | ||||
|   // Load the mesh from the file
 | ||||
|   objects[name] = std::make_unique<Mesh3D>(Mesh3D(filepath)); | ||||
| 
 | ||||
|   Mesh3D obj = Mesh3D(filepath); | ||||
|   bool textureLoaded = objects[name]->texture.loadTexture(textures); | ||||
|    | ||||
|   objects[name] = obj; | ||||
| 
 | ||||
|   bool textureLoaded = obj.texture.loadTexture(textures); | ||||
|   std::cout << &(objects[name]) << std::endl;  | ||||
|    | ||||
|   if (!textureLoaded) { | ||||
|     std::cerr << "Failed to load " << name << " texture!" << std::endl; | ||||
|     exit(1); | ||||
|   } | ||||
|   std::cout << "texture loaded" << std::endl; | ||||
| 
 | ||||
|   //glGenVertexArrays(1, &VAO);
 | ||||
|   glGenVertexArrays(1, &(objects[name].VAO)); | ||||
|   glGenVertexArrays(1, &(objects[name]->VAO)); | ||||
| 
 | ||||
|   //glGenBuffers(1, &VBO);
 | ||||
|   glGenBuffers(1, &(objects[name].VBO)); | ||||
|   glGenBuffers(1, &((objects[name])->VBO)); | ||||
|    | ||||
|   //glBindVertexArray(VAO);
 | ||||
|   glBindVertexArray(objects[name].VAO); | ||||
|   glBindVertexArray((objects[name])->VAO); | ||||
| 
 | ||||
| 
 | ||||
|   //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
 | ||||
|   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); | ||||
|  | ||||
| @ -9,39 +9,15 @@ | ||||
| #include <glm/gtc/type_ptr.hpp> | ||||
| #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){ | ||||
| 
 | ||||
|     shader.use(); | ||||
|     shader.setUniform("ourTexture", 0); | ||||
|      | ||||
|     objects["cube"].texture.bind(); | ||||
|     (objects["cube"])->texture.bind(); | ||||
|      | ||||
|     //std::cout << &(objects["cube"].texture) << std::endl;
 | ||||
|     //textures["cube"].bind();
 | ||||
| 
 | ||||
|     // 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é
 | ||||
| 
 | ||||
|     // Matrice de Vue
 | ||||
|     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::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 11.0f), // Position caméra
 | ||||
|         glm::vec3(0.0f, 2.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)); | ||||
|     ///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(objects["cube"].VAO); | ||||
|     glDrawArrays(GL_TRIANGLES, 0, objects["cube"].numMeshVertices*3); | ||||
|     glBindVertexArray((objects["cube"])->VAO); | ||||
|     glDrawArrays(GL_TRIANGLES, 0, (objects["cube"])->numFaces * 3); | ||||
| 
 | ||||
|     //std::cout << objects["cube"].VAO << std::endl;
 | ||||
| 
 | ||||
|  | ||||
| @ -1,10 +1,7 @@ | ||||
| #define DEBUG true | ||||
| 
 | ||||
| #include "engine.h" | ||||
| #include "Mesh3D.h" | ||||
| 
 | ||||
| int main() { | ||||
|   djalim::OpenGlEngine engine = djalim::OpenGlEngine("Mon app", 800, 600); | ||||
|   djalim::OpenGlEngine engine = djalim::OpenGlEngine("Mon app", 1000, 1000); | ||||
|   engine.start(); | ||||
|   return 0; | ||||
| } | ||||
|  | ||||
| @ -3,52 +3,9 @@ | ||||
| 
 | ||||
| void djalim::OpenGlEngine::onCreate(){ | ||||
|     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); | ||||
|     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();
 | ||||
|     // bool textureLoaded = textures["cube"].loadTexture("../assets/textures/prof.png");
 | ||||
|  | ||||
| @ -1,10 +1,10 @@ | ||||
| #include "engine.h" | ||||
| #include <GL/gl.h> | ||||
| #include "loops.h" | ||||
| #include <iostream> | ||||
| 
 | ||||
| void djalim::OpenGlEngine::onUpdate(){ | ||||
|   showFps();  | ||||
|    | ||||
|   cube(window,shaderProgram,objects); | ||||
| 
 | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user