Window stuff, sleep helper function and cleanups

This commit is contained in:
2021-01-13 21:32:19 +01:00
parent 37542ab831
commit d0a3b5e9a0
7 changed files with 94 additions and 74 deletions

View File

@@ -12,13 +12,7 @@
#include <imgui_impl_opengl3.h>
#endif
#ifdef __linux__
#include <unistd.h>
#endif
#ifdef _WIN32
#include <windows.h>
#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);

View File

@@ -22,15 +22,14 @@ public:
void run();
static ShaderProgram* getShaderProgramByName(std::vector<ShaderProgram*> shaderPrograms, const char *name);
static void error_callback(int error, const char *description);
void setMaxFps(uint16_t fps);
static ShaderProgram* getShaderProgramByName(std::vector<ShaderProgram*> shaderPrograms, const char *name);
private:
void limit_framerate();
void updateWindowSize();
void updateWindowDimensions();
void updateExposure(ShaderProgram *shaderProgram);
ShaderProgram* getShaderProgramByName(const char *name);

View File

@@ -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)
{

View File

@@ -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);

View File

@@ -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);

View File

@@ -48,8 +48,11 @@ private:
DepthMap depthMapDirectionalFBO;
std::vector<DepthMap *> 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);
};

21
src/helper.h Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include <stdint.h>
#ifdef __linux__
#include <unistd.h>
#endif
#ifdef _WIN32
#include <windows.h>
#endif
namespace Helper
{
static void sleep(uint32_t us) {
#ifdef __linux__
usleep(us);
#endif
#ifdef _WIN32
Sleep(us/1000);
#endif
}
};