From d0a3b5e9a0bfc1dce35bbfe7cff87fcead1b2891 Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Wed, 13 Jan 2021 21:32:19 +0100 Subject: [PATCH] Window stuff, sleep helper function and cleanups --- src/Controller.cpp | 44 +++++---------------------- src/Controller.h | 7 ++--- src/Window.cpp | 76 +++++++++++++++++++++++++++++++--------------- src/Window.h | 5 +-- src/World.cpp | 4 +-- src/World.h | 11 ++++--- src/helper.h | 21 +++++++++++++ 7 files changed, 94 insertions(+), 74 deletions(-) create mode 100644 src/helper.h diff --git a/src/Controller.cpp b/src/Controller.cpp index 05a6679..92d4bf7 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -12,13 +12,7 @@ #include #endif -#ifdef __linux__ -#include -#endif -#ifdef _WIN32 -#include -#endif - +#include "helper.h" #include "Controller.h" #include "VertexArray.h" #include "Texture.h" @@ -31,14 +25,6 @@ Controller::Controller() { - if (!glfwInit()) { - exit(-1); - } - - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - gameWindow = new Window(); gameEventHandler = new EventHandler(gameWindow->getGLFWwindow()); @@ -47,14 +33,10 @@ Controller::Controller() JsonParser shaderParser("res/shaderPrograms.json"); shaderPrograms = shaderParser.getShaderPrograms(); - pp_framebuffer = new Framebuffer(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT, getShaderProgramByName("postProcessingProgram")); + pp_framebuffer = new Framebuffer(gameWindow->getWindowWidth(), gameWindow->getWindowHeight(), getShaderProgramByName("postProcessingProgram")); menu = new Menu(pp_framebuffer, getShaderProgramByName("menuProgram")); #ifdef _DEBUG - glfwWindowHint(GLFW_MAXIMIZED, GL_TRUE); - glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); - glfwSetErrorCallback(error_callback); - // Setup Dear ImGui context IMGUI_CHECKVERSION(); ImGui::CreateContext(); @@ -85,7 +67,6 @@ Controller::~Controller() delete pp_framebuffer; delete gameEventHandler; delete gameWindow; - glfwTerminate(); } void Controller::run() @@ -169,8 +150,9 @@ void Controller::run() glfwSwapBuffers(gameWindow->getGLFWwindow()); // Update window size - if (gameWindow->checkWindowWasResized()) { - updateWindowSize(); + if (gameWindow->isWindowResized()) { + gameWindow->updateWindowDimensions(); + updateWindowDimensions(); } // --- Check events, handle input --- @@ -194,12 +176,7 @@ void Controller::limit_framerate() double frameTime = 1 / (double)MAX_FPS; if (frameTime > lastTime) { -#ifdef __linux__ - usleep((frameTime - lastTime) * 1000000); -#endif -#ifdef _WIN32 - Sleep((frameTime - lastTime) * 1000); -#endif + Helper::sleep((frameTime - lastTime) * 1000000); } deltaTime = glfwGetTime() - startingTime; @@ -207,14 +184,7 @@ void Controller::limit_framerate() startingTime = glfwGetTime(); } -// GLFW error function -void Controller::error_callback(int error, const char *description) -{ - (void)error; - fprintf(stderr, "Error: %s\n", description); -} - -void Controller::updateWindowSize() +void Controller::updateWindowDimensions() { camera->updateAspectRatio(gameWindow->getWindowAspectRatio()); gameEventHandler->setFirstMouseInput(1); diff --git a/src/Controller.h b/src/Controller.h index a621881..4552089 100644 --- a/src/Controller.h +++ b/src/Controller.h @@ -22,15 +22,14 @@ public: void run(); - static ShaderProgram* getShaderProgramByName(std::vector shaderPrograms, const char *name); - static void error_callback(int error, const char *description); - void setMaxFps(uint16_t fps); + static ShaderProgram* getShaderProgramByName(std::vector shaderPrograms, const char *name); + private: void limit_framerate(); - void updateWindowSize(); + void updateWindowDimensions(); void updateExposure(ShaderProgram *shaderProgram); ShaderProgram* getShaderProgramByName(const char *name); diff --git a/src/Window.cpp b/src/Window.cpp index be73c00..aa7c69d 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -4,19 +4,41 @@ #include "Window.h" #include "eventActions.h" #include "defines.h" +#include "helper.h" Window::Window() { + // Initialize GLFW + if (!glfwInit()) { + exit(-1); + } + width = INIT_WINDOW_WIDTH; height = INIT_WINDOW_HEIGHT; + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + +#ifdef _DEBUG + glfwSetErrorCallback(error_callback); + glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); +#else + // Maximize in release build + glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE); +#endif + window = glfwCreateWindow(width, height, "OpenGL", NULL, NULL); if (!window) { std::cout << "Failed to create window" << std::endl; } + // Wait for window to maximize (in case) + Helper::sleep(1000); + glfwGetWindowPos(window, &posX, &posY); + glfwGetWindowSize(window, &width, &height); // Create OpenGL context glfwMakeContextCurrent(window); @@ -27,6 +49,16 @@ Window::Window() exit(-1); } +#ifdef _DEBUG + // Disable mouse cursor + mouseCatched = false; + + std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl; + glEnable(GL_DEBUG_OUTPUT); + glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); + glDebugMessageCallback(openGLDebugCallback, NULL); +#endif + // Enable z buffer glEnable(GL_DEPTH_TEST); @@ -37,23 +69,10 @@ Window::Window() // Enable multisampling (a bit redundant because most graphics drivers do this automatically) glEnable(GL_MULTISAMPLE); - + // Disable VSync since my sleep function handles this glfwSwapInterval(0); -#ifdef _DEBUG - // Disable mouse cursor - mouseCatched = false; - - std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl; - glEnable(GL_DEBUG_OUTPUT); - glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); - glDebugMessageCallback(openGLDebugCallback, NULL); -#else - // Maximize in release build - glfwMaximizeWindow(window); -#endif - setCatchedCursor(mouseCatched); glViewport(0, 0, width, height); @@ -65,26 +84,27 @@ Window::Window() Window::~Window() { glfwDestroyWindow(window); + glfwTerminate(); } -bool Window::checkWindowWasResized() +bool Window::isWindowResized() { int new_width, new_height, new_posx, new_posy; glfwGetFramebufferSize(window, &new_width, &new_height); glfwGetWindowPos(window, &new_posx, &new_posy); - if (new_width == width && new_height == height && new_posx == posX && new_posy == posY) { - return 0; - } - width = new_width; - height = new_height; - posX = new_posx; - posY = new_posy; - glViewport(0, 0, width, height); - - return 1; + return !(new_width == width && new_height == height && new_posx == posX && new_posy == posY); } +void Window::updateWindowDimensions() +{ + glfwGetFramebufferSize(window, &width, &height); + glfwGetWindowPos(window, &posX, &posY); + + glViewport(0, 0, width, height); +} + + void Window::setCatchedCursor(bool value) { if (value) { @@ -113,6 +133,12 @@ void Window::handleActionRegister(bool *windowActionRegister) } } +// GLFW error function +void Window::error_callback(int error, const char *description) +{ + fprintf(stderr, "[Error] GLFW Error %d: %s\n", error, description); +} + // This function is called when the window gets resized (currently not used) void Window::framebuffer_size_callback(GLFWwindow *window, int width, int height) { diff --git a/src/Window.h b/src/Window.h index d3d41b2..7a2d49c 100644 --- a/src/Window.h +++ b/src/Window.h @@ -15,12 +15,13 @@ public: float getWindowAspectRatio(); bool getMouseIsCatched(); - // side effect! - bool checkWindowWasResized(); + bool isWindowResized(); + void updateWindowDimensions(); void handleActionRegister(bool *windowActionRegister); private: + static void error_callback(int error, const char *description); static void framebuffer_size_callback(GLFWwindow *window, int width, int height); static void openGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam); void setCatchedCursor(bool value); diff --git a/src/World.cpp b/src/World.cpp index 0e73ba8..66cd3c4 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -163,7 +163,7 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg pointShaderProgram->setUniform(("u_shadowMatrices[" + std::to_string(i) + "]").c_str(), viewProjMatrices[i]); } - pointShaderProgram->setUniform("pointShadowDepthMapFarPlane", far); + pointShaderProgram->setUniform("pointShadowDepthMapFarPlane", far_plane_point); pointShaderProgram->setUniform("v_lightPos", lightPos); // Draw scene from light perspective @@ -176,7 +176,7 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg shaderProgram->bind(); - shaderProgram->setUniform("pointShadowDepthMapFarPlane", far); + shaderProgram->setUniform("pointShadowDepthMapFarPlane", far_plane_point); textureUnit = TEXTURE_TYPE_NUM_ITEMS * 2 + i + 1; shaderProgram->setUniform("u_texture_pointShadowMap0", (int)textureUnit); diff --git a/src/World.h b/src/World.h index 558af74..e74bc96 100644 --- a/src/World.h +++ b/src/World.h @@ -48,8 +48,11 @@ private: DepthMap depthMapDirectionalFBO; std::vector depthMapPointFBO; // Shadow projection matrices - float near_plane = 1.0f, far_plane = 15.0f; - glm::mat4 directionalLightProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane, far_plane); - float aspect = 1.0f, near = 1.0f, far = 25.0f; - glm::mat4 pointLightProjection = glm::perspective(glm::radians(90.0f), aspect, near, far); + const float near_plane_directional = 1.0f; + const float far_plane_directional = 15.0f; + glm::mat4 directionalLightProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane_directional, far_plane_directional); + const float aspect_ratio_point = 1.0f; + const float near_plane_point = 1.0f; + const float far_plane_point = 25.0f; + glm::mat4 pointLightProjection = glm::perspective(glm::radians(90.0f), aspect_ratio_point, near_plane_point, far_plane_point); }; diff --git a/src/helper.h b/src/helper.h new file mode 100644 index 0000000..5ec5274 --- /dev/null +++ b/src/helper.h @@ -0,0 +1,21 @@ +#pragma once +#include + +#ifdef __linux__ +#include +#endif +#ifdef _WIN32 +#include +#endif + +namespace Helper +{ + static void sleep(uint32_t us) { +#ifdef __linux__ + usleep(us); +#endif +#ifdef _WIN32 + Sleep(us/1000); +#endif + } +};