#include "engine.h" #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::vector& vertices){ objects[name]; bool textureLoaded = objects[name].texture.loadTexture(textures); if (!textureLoaded) { std::cerr << "Failed to load " << name << " texture!" << std::endl; exit(1); } //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); std::cout << "Loading 3D objects with "<< vertices.size() << " vertices"<< std::endl; glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vertices.size(), vertices.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); } }