#include "engine.h" #include "Mesh3D.h" #include #include #include #include #include #include #include #include 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(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); }