diff --git a/headers/renderer.hpp b/headers/renderer.hpp index 3f7afa9..64ce566 100644 --- a/headers/renderer.hpp +++ b/headers/renderer.hpp @@ -84,7 +84,7 @@ namespace Renderer { class Scene { public: - float deltaTime = 0.0f; + float deltaTime = 0.0f; // Seconds Scene(Window* win); Scene(Window* win, std::vector ROs); diff --git a/headers/window.hpp b/headers/window.hpp index cbc11e5..fdb0fa4 100644 --- a/headers/window.hpp +++ b/headers/window.hpp @@ -1,7 +1,7 @@ #pragma once -// #include "renderer.hpp" #include #include "GLFW/glfw3.h" +#include // Default window dimensions #define D_WINDOW_TITLE "Euclid Engine" @@ -11,23 +11,31 @@ class Window { public: - GLFWwindow* win; - // Renderer::Camera cam; - Window(const char* title); Window(const char* title, unsigned int w, unsigned int h); + ~Window(); + void spawn(); - unsigned int width() { return _width; } // getters and dumb and - unsigned int height() { return _height; } // cpp should have readonly fields... + unsigned int getWidth() { return _width; } // getters and dumb and + unsigned int getHeight() { return _height; } // cpp should have readonly fields... + GLFWwindow* getWindow() { return _win; } void updateSize(int w, int h); + void makeCurrent(); + void swapBuffers(); + bool shouldClose(); private: + GLFWwindow* _win; + static std::map windowMap; + const char* _title = D_WINDOW_TITLE; unsigned int _width = D_WINDOW_WIDTH; unsigned int _height = D_WINDOW_HEIGHT; void setWidth(unsigned int w) { _width = w; } void setHeight(unsigned int h) { _height = h; } + + static void framebufferSizeCallback(GLFWwindow* win, int width, int height); }; diff --git a/src/controller.cpp b/src/controller.cpp index c19b231..d0f36eb 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -10,15 +10,15 @@ void Controller::processMouseInput(float deltaTime) { double x, y; - glfwGetCursorPos(window->win, &x, &y); + glfwGetCursorPos(window->getWindow(), &x, &y); if (firstMouseInput) { lastX = x; lastY = y; firstMouseInput = false; - } + } - float xOffset = x - lastX; + float xOffset = x - lastX; float yOffset = y - lastY; lastX = x; lastY = y; @@ -49,24 +49,24 @@ void Controller::processMouseInput(float deltaTime) { void Controller::processInput(float deltaTime) { processMouseInput(deltaTime); - float cameraSpeed = deltaTime * 2.5f; + float cameraSpeed = deltaTime * 2.5f; - if (glfwGetKey(window->win, GLFW_KEY_W) == GLFW_PRESS) { + if (glfwGetKey(window->getWindow(), GLFW_KEY_W) == GLFW_PRESS) { translate(cameraSpeed * front); } - if (glfwGetKey(window->win, GLFW_KEY_S) == GLFW_PRESS) { + if (glfwGetKey(window->getWindow(), GLFW_KEY_S) == GLFW_PRESS) { translate(-cameraSpeed * front); } - if (glfwGetKey(window->win, GLFW_KEY_A) == GLFW_PRESS) { + if (glfwGetKey(window->getWindow(), GLFW_KEY_A) == GLFW_PRESS) { translate(-glm::normalize(glm::cross(front, up)) * cameraSpeed); } - if (glfwGetKey(window->win, GLFW_KEY_D) == GLFW_PRESS) { + if (glfwGetKey(window->getWindow(), GLFW_KEY_D) == GLFW_PRESS) { translate(glm::normalize(glm::cross(front, up)) * cameraSpeed); } - if (glfwGetKey(window->win, GLFW_KEY_SPACE) == GLFW_PRESS) { + if (glfwGetKey(window->getWindow(), GLFW_KEY_SPACE) == GLFW_PRESS) { translate(cameraSpeed * up); } - if (glfwGetKey(window->win, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { + if (glfwGetKey(window->getWindow(), GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { translate(cameraSpeed * -up); } } diff --git a/src/main.cpp b/src/main.cpp index 63f45ba..25a341e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,19 +15,19 @@ std::vector verts({ -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, - 0.5f, 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.0f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f }); // Vert struc: x y z r g b tx ty -std::vector indices({ - 0, 1, 3, +std::vector indices({ + 0, 1, 3, 1, 2, 3, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, 11, 12, 13, }); // Window for the game -Window win("Euclid Engine: Demo"); -Renderer::Scene scene(&win); - -void framebuffer_size_callback(GLFWwindow* glfwWindow, int w, int h) { - win.updateSize(w, h); -} - void processInput(GLFWwindow *win) { int action = glfwGetKey(win, GLFW_KEY_ESCAPE); if (action == GLFW_PRESS) { @@ -120,6 +113,9 @@ void processInput(GLFWwindow *win) { int main() { // Spawn window + Window win("Euclid Engine: Demo"); + Renderer::Scene scene(&win); + win.spawn(); if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) { @@ -127,13 +123,10 @@ int main() { return 1; } - glViewport(0, 0, win.width(), win.height()); - glfwSetFramebufferSizeCallback(win.win, framebuffer_size_callback); // Framebuffer - - glEnable(GL_DEPTH_TEST); + glEnable(GL_DEPTH_TEST); // Input - glfwSetInputMode(win.win, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Disable cursor + glfwSetInputMode(win.getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Disable cursor float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); @@ -152,22 +145,23 @@ int main() { Controller player(&win, glm::vec3(0.0f, 0.0f, 8.0f)); scene.setCamera(&player); - while (!glfwWindowShouldClose(win.win)) { + while (!win.shouldClose()) { // Handle input player.processInput(scene.deltaTime); - processInput(win.win); + processInput(win.getWindow()); ro.translate(glm::vec3(0.0f, 0.0f, 0.001f)); - // ro2.setPosition(glm::vec3(0.0f, 0.0f, -1000.0f)); + // ro2.translate(glm::vec3(0.0f, -0.01f, 0.01f)); + // ro2.translate(glm::vec3(0.0f, 0.0f, -0.001f)); + ro2.rotate(glm::vec3(1.01f, 1.0f, 1.0f)); // Render new frame scene.render(); // glfw - glfwSwapBuffers(win.win); + win.swapBuffers(); glfwPollEvents(); } - glfwTerminate(); return 0; } diff --git a/src/renderer.cpp b/src/renderer.cpp index efca49e..d1008d2 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "GLFW/glfw3.h" #include "glm/ext/matrix_transform.hpp" @@ -72,7 +73,7 @@ namespace Renderer { // Scene Scene::Scene(Window* win) { - window = win; + window = win; } Scene::Scene(Window* win, std::vector ROs) : Scene(win) { @@ -92,6 +93,8 @@ namespace Renderer { float curFrame = glfwGetTime(); deltaTime = curFrame - lastFrame; + printf("\rdeltaTime=%f FPS=%f", deltaTime, 1/(deltaTime+0.0001f)); + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -117,7 +120,7 @@ namespace Renderer { } glm::mat4 Camera::getProjection() { - projection = glm::perspective(glm::radians(FOV), (float)window->width() / (float)window->height(), NEAR_PLANE, FAR_PLANE); + projection = glm::perspective(glm::radians(FOV), (float)window->getWidth() / (float)window->getHeight(), NEAR_PLANE, FAR_PLANE); return projection; } diff --git a/src/textures.cpp b/src/textures.cpp index 8841ed5..64551ce 100644 --- a/src/textures.cpp +++ b/src/textures.cpp @@ -1,5 +1,5 @@ #include -#include "../headers/textures.hpp" +#include "textures.hpp" #include #define STB_IMAGE_IMPLEMENTATION @@ -30,7 +30,7 @@ namespace Textures { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); unsigned char* data = stbi_load(texture_src, &width, &height, &nrChannels, 0); - + if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); diff --git a/src/window.cpp b/src/window.cpp index 1166734..576de07 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -4,6 +4,8 @@ #include #include +std::map Window::windowMap; + Window::Window(const char* title) { this->_title = title; } @@ -13,21 +15,30 @@ Window::Window(const char* title, unsigned int w, unsigned int h) : Window(title this->_height = h; } +Window::~Window() { + glfwDestroyWindow(_win); + windowMap.erase(_win); +} + void Window::spawn() { - glfwInit(); + glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_FLOATING, GL_TRUE); - win = glfwCreateWindow(_width, _height, _title, NULL, NULL); - if (win == NULL) { + _win = glfwCreateWindow(_width, _height, _title, NULL, NULL); + if (_win == NULL) { printf("[ERROR] Failed to create a window.\n"); glfwTerminate(); exit(1); } - glfwMakeContextCurrent(win); + // Register window in the std::map + windowMap[_win] = this; + + glfwSetFramebufferSizeCallback(_win, framebufferSizeCallback); + glfwMakeContextCurrent(_win); } void Window::updateSize(int w, int h) { @@ -35,3 +46,20 @@ void Window::updateSize(int w, int h) { _height = h; glViewport(0, 0, w, h); } + +void Window::makeCurrent() { + glfwMakeContextCurrent(_win); +} + +void Window::swapBuffers() { + glfwSwapBuffers(_win); +} + +bool Window::shouldClose() { + return glfwWindowShouldClose(_win); +} + +// Framebuffer Size Callback, called each time the window "updates" +void Window::framebufferSizeCallback(GLFWwindow* win, int width, int height) { + windowMap[win]->updateSize(width, height); +}