LesDingeriesDeDjalimSurOpenGL/src/core/engine.cpp
Djalim Simaila 7e36811c91 ♻️ refactor engine to use Mesh3D with transform support
- Add position, rotation and scale fields to `Mesh3D` and expose them publicly.
- Remove the old `Object3D` struct; `Objects3D` now stores `unique_ptr<Mesh3D>`.
- Implement a new `createObject(name,textures,filepath,position,rotation,scale)` overload that sets transforms after loading the mesh.
- Add a `draw(Mesh3D*)` helper that binds the texture, builds a model matrix and issues `glDrawArrays`.
- Update the rendering loop to use `draw()` instead of the old cube routine.
- Simplify `onCreate`: enable depth test, flip textures, create two objects with proper transforms, set clear color.
- Enable window resizing (`GLFW_RESIZABLE`), change default size to 800×600 in `main.cpp`.
- Clean up includes and remove unused `cube.cpp`; refactor OBJ parsing for robustness.

These changes unify object handling, allow per‑object transformations, simplify the rendering pipeline, improve maintainability, and make the window resizable.
2025-10-25 19:46:49 +02:00

139 lines
3.6 KiB
C++

#include "engine.h"
#include "Mesh3D.h"
#include <cstdlib>
#include <glm/ext/vector_float3.hpp>
#include <iostream>
#include <memory>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
djalim::OpenGlEngine::OpenGlEngine(const char* title, int width, int height) {
// initialize GLFW
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
exit(1);
}
loadHints();
this->window = glfwCreateWindow(width, height, title, NULL, NULL);
if (!this->window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
exit(1);
}
glfwMakeContextCurrent(this->window);
// initialize GLEW
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW" << std::endl;
exit(1);
}
loadCallbacks();
bool loaded = shaderProgram.loadShaders("../assets/shaders/vertex.glsl", "../assets/shaders/fragment.glsl");
if (!loaded) {
std::cerr << "Failed to load shaders correctly" << std::endl;
exit(1);
}
onCreate();
}
void djalim::OpenGlEngine::createObject(std::string name, std::string textures, std::string filepath, glm::vec3 position, glm::vec3 rotation, glm::vec3 scale){
createObject(name,textures,filepath);
(objects[name])->position = position;
(objects[name])->rotation = rotation;
(objects[name])->scale = scale;
}
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));
bool textureLoaded = objects[name]->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));
//glGenBuffers(1, &VBO);
glGenBuffers(1, &((objects[name])->VBO));
//glBindVertexArray(VAO);
glBindVertexArray((objects[name])->VAO);
//glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ARRAY_BUFFER, (objects[name])->VBO);
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);
glEnableVertexAttribArray(0);
// Attribut de coordonnée de texture
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
}
djalim::OpenGlEngine::~OpenGlEngine() {
onDestroy();
glfwDestroyWindow(window);
glfwTerminate();
}
void djalim::OpenGlEngine::start(){
// Main loop
double currentSeconds;
while (!glfwWindowShouldClose(this->window)) {
currentSeconds = glfwGetTime();
elapsedSeconds = currentSeconds-previousSeconds;
previousSeconds = currentSeconds;
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClearColor(0, 0.5, 0.5, 1.0f);
onUpdate();
glfwSwapBuffers(this->window);
}
}
void djalim::OpenGlEngine::draw(djalim::Mesh3D* object){
object->texture.bind();
glm::mat4 model = glm::mat4(1.0f);
if (object->rotation != glm::vec3(.0,.0,.0)){
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(50.0f), object->rotation);
}
model = glm::translate(model, object->position);
model = glm::scale(model, object->scale);
shaderProgram.setUniform("model", model);
glBindVertexArray(object->VAO);
glDrawArrays(GL_TRIANGLES, 0, object->numFaces * 3);
}