From f816f5f565c8a278dc96c1cd7b7e2d128b587301 Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Sun, 4 Jul 2021 13:12:34 +0200 Subject: [PATCH] Format code with clang-format. --- src/Camera.cpp | 6 +- src/Camera.h | 6 +- src/Controller.cpp | 45 +++++++------ src/Controller.h | 21 +++--- src/Entity.cpp | 16 ++--- src/Entity.h | 2 +- src/EventHandler.cpp | 19 +++--- src/Framebuffer.cpp | 10 ++- src/Framebuffer.h | 10 ++- src/Helper.cpp | 145 +++++++++++++++++++++--------------------- src/Helper.h | 17 ++--- src/JsonParser.cpp | 107 ++++++++++++++++--------------- src/JsonParser.h | 18 +++--- src/Light.cpp | 19 ++---- src/Light.h | 3 +- src/Menu.cpp | 31 ++++----- src/Menu.h | 12 ++-- src/Mesh.cpp | 12 ++-- src/Mesh.h | 2 +- src/Model.cpp | 33 +++++----- src/Model.h | 9 +-- src/Screen.cpp | 25 ++++---- src/Screen.h | 23 +++---- src/ShaderProgram.cpp | 36 ++++++----- src/ShaderProgram.h | 23 +++---- src/Texture.cpp | 55 ++++++++-------- src/Texture.h | 13 +++- src/VertexArray.cpp | 23 +++---- src/VertexArray.h | 2 +- src/Widget.cpp | 41 ++++-------- src/Widget.h | 12 ++-- src/Window.cpp | 15 ++--- src/World.cpp | 69 +++++++++++--------- src/World.h | 30 +++++---- src/defines.h | 3 +- src/eventActions.h | 12 ++-- src/main.cpp | 10 +-- 37 files changed, 470 insertions(+), 465 deletions(-) diff --git a/src/Camera.cpp b/src/Camera.cpp index 0e7bae1..5ce40a4 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -1,8 +1,8 @@ #include "Camera.h" #include "eventActions.h" -#include #include +#include Camera::Camera(float fov, float aspectRatio) { @@ -19,7 +19,7 @@ void Camera::updateVPM() void Camera::updateAspectRatio(float aspectRatio) { - //projectionMatrix = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.f, 100.0f); + // projectionMatrix = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.f, 100.0f); projectionMatrix = glm::perspective(fov / 2.0f, aspectRatio, 0.1f, 1000.0f); updateVPM(); } @@ -79,7 +79,7 @@ void Camera::updateDirectionFromMouseInput(double *cameraMouseActionRegister) pitch += cameraMouseActionRegister[cameraMouseDeltaY]; if (pitch > 89.0f) { - pitch = 89.0f; + pitch = 89.0f; } if (pitch < -89.0f) { pitch = -89.0f; diff --git a/src/Camera.h b/src/Camera.h index 3b830b5..9503ec4 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -1,7 +1,7 @@ #pragma once -#include #include "ShaderProgram.h" +#include class Camera { @@ -34,7 +34,7 @@ private: glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f); glm::vec3 frontVec = glm::vec3(0.0f, 0.0f, -1.0f); - glm::vec3 upVec = glm::vec3(0.0f, 1.0f, 0.0f); + glm::vec3 upVec = glm::vec3(0.0f, 1.0f, 0.0f); float pitch = 0.0f; float yaw = -90.0f; @@ -43,5 +43,3 @@ private: float fov; }; - - diff --git a/src/Controller.cpp b/src/Controller.cpp index 094b059..bd18b02 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -12,16 +12,16 @@ #include #endif -#include "Helper.h" #include "Controller.h" -#include "VertexArray.h" -#include "Texture.h" -#include "Model.h" #include "Entity.h" -#include "World.h" -#include "Widget.h" -#include "Screen.h" +#include "Helper.h" #include "JsonParser.h" +#include "Model.h" +#include "Screen.h" +#include "Texture.h" +#include "VertexArray.h" +#include "Widget.h" +#include "World.h" Controller::Controller() { @@ -33,7 +33,8 @@ Controller::Controller() JsonParser shaderParser("data/shaderPrograms.json"); shaderPrograms = shaderParser.getShaderPrograms(); - pp_framebuffer = new Framebuffer(gameWindow->getWindowWidth(), gameWindow->getWindowHeight(), getShaderProgramByName("postProcessingProgram")); + pp_framebuffer = new Framebuffer(gameWindow->getWindowWidth(), gameWindow->getWindowHeight(), + getShaderProgramByName("postProcessingProgram")); menu = new Menu(pp_framebuffer, getShaderProgramByName("menuProgram")); @@ -119,7 +120,7 @@ void Controller::run() getShaderProgramByName("lightProgram")->unbind(); // --- Render and buffer swap --- - + // Calc shadows static bool drawShadows = false; static bool firstRun = true; @@ -128,13 +129,14 @@ void Controller::run() getShaderProgramByName("defaultProgram")->unbind(); if (drawShadows || firstRun) { firstRun = false; - world->calculateShadows(getShaderProgramByName("directionalShadowDepthProgram"), getShaderProgramByName("pointShadowDepthProgram")); + world->calculateShadows(getShaderProgramByName("directionalShadowDepthProgram"), + getShaderProgramByName("pointShadowDepthProgram")); } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); auto activeScreen = menu->getActiveScreen(); - if(activeScreen) { + if (activeScreen) { activeScreen->draw(); } else { pp_framebuffer->bind(); @@ -150,7 +152,8 @@ void Controller::run() pp_framebuffer->render(); #ifdef _DEBUG - renderImGui(world, &lightColor, &rotateEntity, &rotateLightSource, getShaderProgramByName("postProcessingProgram"), &intensity, &drawShadows); + renderImGui(world, &lightColor, &rotateEntity, &rotateLightSource, + getShaderProgramByName("postProcessingProgram"), &intensity, &drawShadows); #endif } glfwSwapBuffers(gameWindow->getGLFWwindow()); @@ -168,7 +171,7 @@ void Controller::run() if (gameWindow->getMouseIsCatched()) { camera->updateDirectionFromMouseInput(gameEventHandler->getCursorDelta()); } - + menu->writeWindowActions(gameEventHandler->getWindowActionRegister()); gameWindow->handleWindowActionRegister(gameEventHandler->getWindowActionRegister()); @@ -210,10 +213,10 @@ void Controller::updateExposure(ShaderProgram *shaderProgram) shaderProgram->unbind(); } -ShaderProgram* Controller::getShaderProgramByName(const std::string& name) +ShaderProgram *Controller::getShaderProgramByName(const std::string &name) { for (auto it = shaderPrograms.begin(); it != shaderPrograms.end(); it++) { - if((*it)->getUniqueName() == name) { + if ((*it)->getUniqueName() == name) { return *it; } } @@ -221,10 +224,10 @@ ShaderProgram* Controller::getShaderProgramByName(const std::string& name) return nullptr; } -ShaderProgram* Controller::getShaderProgramByName(std::vector shaderPrograms, const std::string& name) +ShaderProgram *Controller::getShaderProgramByName(std::vector shaderPrograms, const std::string &name) { for (auto it = shaderPrograms.begin(); it != shaderPrograms.end(); it++) { - if((*it)->getUniqueName() == name) { + if ((*it)->getUniqueName() == name) { return *it; } } @@ -238,13 +241,13 @@ void Controller::setMaxFps(uint16_t fps) } #ifdef _DEBUG -void Controller::renderImGui(World *world, glm::vec3 *lightColor, bool *rotateEntity, bool *rotateLightSource, ShaderProgram *postProcessingProgram, float *intensity, bool *drawShadows) +void Controller::renderImGui(World *world, glm::vec3 *lightColor, bool *rotateEntity, bool *rotateLightSource, + ShaderProgram *postProcessingProgram, float *intensity, bool *drawShadows) { ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); - // render your GUI ImGui::Begin("Debug Utils"); ImGui::Text("Object"); @@ -283,8 +286,8 @@ void Controller::renderImGui(World *world, glm::vec3 *lightColor, bool *rotateEn ImGui::Checkbox("Draw Shadows", drawShadows); ImGui::Checkbox("Rotate Lightsource", rotateLightSource); - ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", - 1000.0 / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0 / ImGui::GetIO().Framerate, + ImGui::GetIO().Framerate); ImGui::End(); diff --git a/src/Controller.h b/src/Controller.h index fb11ca3..63ba0c2 100644 --- a/src/Controller.h +++ b/src/Controller.h @@ -2,17 +2,16 @@ #include -#include "Window.h" -#include "EventHandler.h" #include "Camera.h" -#include "ShaderProgram.h" #include "Entity.h" -#include "defines.h" -#include "Light.h" +#include "EventHandler.h" #include "Framebuffer.h" +#include "Light.h" #include "Menu.h" +#include "ShaderProgram.h" +#include "Window.h" #include "World.h" - +#include "defines.h" class Controller { @@ -24,7 +23,7 @@ public: void setMaxFps(uint16_t fps); - static ShaderProgram* getShaderProgramByName(std::vector shaderPrograms, const std::string& name); + static ShaderProgram *getShaderProgramByName(std::vector shaderPrograms, const std::string &name); private: void limit_framerate(); @@ -32,9 +31,10 @@ private: void updateWindowDimensions(); void updateExposure(ShaderProgram *shaderProgram); - ShaderProgram* getShaderProgramByName(const std::string& name); + ShaderProgram *getShaderProgramByName(const std::string &name); - void renderImGui(World *world, glm::vec3 *lightColor, bool *rotateEntity, bool *rotateLightSource, ShaderProgram *postProcessingProgram, float *intensity, bool *drawShadows); + void renderImGui(World *world, glm::vec3 *lightColor, bool *rotateEntity, bool *rotateLightSource, + ShaderProgram *postProcessingProgram, float *intensity, bool *drawShadows); private: Window *gameWindow; @@ -45,11 +45,10 @@ private: Camera *camera; Menu *menu; - std::vector shaderPrograms; + std::vector shaderPrograms; Framebuffer *pp_framebuffer; - uint16_t MAX_FPS = 60; double deltaTime; diff --git a/src/Entity.cpp b/src/Entity.cpp index ded4547..4068b8a 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -1,14 +1,12 @@ #include "Entity.h" -#include #include +#include uint32_t Entity::id_counter = 0; -Entity::Entity(std::string name, Model *model, ShaderProgram *shaderProgram) : - unique_name(name), - model(model), - shaderProgram(shaderProgram) +Entity::Entity(std::string name, Model *model, ShaderProgram *shaderProgram) + : unique_name(name), model(model), shaderProgram(shaderProgram) { id = id_counter++; } @@ -147,11 +145,9 @@ bool Entity::getIsLightSource() return isLightSource; } -Skybox::Skybox(Model *cubeModel, ShaderProgram *shaderProgram, const char *texturePseudoPath) : - cubeModel(cubeModel), - shaderProgram(shaderProgram), - cubeMap(texturePseudoPath), - vertexArray(cubeModel->getMesh(0)->getVertexArray()) +Skybox::Skybox(Model *cubeModel, ShaderProgram *shaderProgram, const char *texturePseudoPath) + : cubeModel(cubeModel), shaderProgram(shaderProgram), cubeMap(texturePseudoPath), + vertexArray(cubeModel->getMesh(0)->getVertexArray()) { // Empty } diff --git a/src/Entity.h b/src/Entity.h index 9a3772f..094bb00 100644 --- a/src/Entity.h +++ b/src/Entity.h @@ -1,8 +1,8 @@ #pragma once #include "Model.h" -#include "Texture.h" #include "ShaderProgram.h" +#include "Texture.h" #include #include diff --git a/src/EventHandler.cpp b/src/EventHandler.cpp index 008f187..2109727 100644 --- a/src/EventHandler.cpp +++ b/src/EventHandler.cpp @@ -10,9 +10,7 @@ bool EventHandler::windowActionRegister[WINDOW_ACTION_NUM_ITEMS] = {0}; bool EventHandler::firstMouseInput = 1; float EventHandler::mouseSensitivity = 0.15f; - -EventHandler::EventHandler(GLFWwindow *p_window) : - window(p_window) +EventHandler::EventHandler(GLFWwindow *p_window) : window(p_window) { glfwSetKeyCallback(window, key_callback); glfwSetCursorPosCallback(window, mouse_callback); @@ -27,7 +25,7 @@ void EventHandler::handleEvents() void EventHandler::clearActionRegisters() { - //std::fill_n(cameraActionRegister, CAMERA_ACTION_NUM_ITEMS, 0); + // std::fill_n(cameraActionRegister, CAMERA_ACTION_NUM_ITEMS, 0); std::fill_n(cameraMouseActionRegister, CAMERA_MOUSE_ACTION_NUM_ITEMS, 0.0); std::fill_n(windowActionRegister, WINDOW_ACTION_NUM_ITEMS, 0); std::fill_n(mouseButtonActionRegister, MOUSE_BUTTON_ACTION_NUM_ITEMS, 0); @@ -120,9 +118,10 @@ void EventHandler::mouse_callback(GLFWwindow *window, double xpos, double ypos) cameraMouseActionRegister[cameraMouseActions::cameraMouseDeltaY] += deltaCursorPosY; } -void EventHandler::mouse_button_callback(GLFWwindow* window, int button, int action, int mods) +void EventHandler::mouse_button_callback(GLFWwindow *window, int button, int action, int mods) { - (void) window; (void) mods; + (void)window; + (void)mods; if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) mouseButtonActionRegister[mouseButtonActions::leftClicked] = true; @@ -132,22 +131,22 @@ void EventHandler::mouse_button_callback(GLFWwindow* window, int button, int act mouseButtonActionRegister[mouseButtonActions::middleClicked] = true; } -bool * EventHandler::getCameraActionRegister() +bool *EventHandler::getCameraActionRegister() { return cameraActionRegister; } -bool * EventHandler::getWindowActionRegister() +bool *EventHandler::getWindowActionRegister() { return windowActionRegister; } -bool * EventHandler::getMouseButtonActionRegister() +bool *EventHandler::getMouseButtonActionRegister() { return mouseButtonActionRegister; } -double * EventHandler::getCursorDelta() +double *EventHandler::getCursorDelta() { return cameraMouseActionRegister; } diff --git a/src/Framebuffer.cpp b/src/Framebuffer.cpp index 0017ce2..7dedd7b 100644 --- a/src/Framebuffer.cpp +++ b/src/Framebuffer.cpp @@ -3,8 +3,7 @@ #include #include -Framebuffer::Framebuffer(uint32_t width, uint32_t height, ShaderProgram *shaderProgram) : - shaderProgram(shaderProgram) +Framebuffer::Framebuffer(uint32_t width, uint32_t height, ShaderProgram *shaderProgram) : shaderProgram(shaderProgram) { glGenFramebuffers(1, &FBO); @@ -90,7 +89,6 @@ void Framebuffer::generateTextures(uint32_t width, uint32_t height) unbind(); } - void Framebuffer::setExposureCorrection(bool exposureCorrection) { shaderProgram->bind(); @@ -112,7 +110,8 @@ DepthMap::DepthMap(int TYPE, int RESOLUTION) glGenTextures(1, &depthMap); glBindTexture(GL_TEXTURE_2D, depthMap); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, RESOLUTION, RESOLUTION, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, RESOLUTION, RESOLUTION, 0, GL_DEPTH_COMPONENT, GL_FLOAT, + NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -140,11 +139,10 @@ DepthMap::DepthMap(int TYPE, int RESOLUTION) DepthMap::~DepthMap() { - if(cubeMap) + if (cubeMap) delete cubeMap; } - void DepthMap::bind() { glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); diff --git a/src/Framebuffer.h b/src/Framebuffer.h index 824e8f9..b7594c1 100644 --- a/src/Framebuffer.h +++ b/src/Framebuffer.h @@ -1,13 +1,13 @@ #pragma once -#include #include "ShaderProgram.h" #include "Texture.h" +#include class Framebuffer { public: - Framebuffer(uint32_t width, uint32_t height, ShaderProgram* shaderProgram); + Framebuffer(uint32_t width, uint32_t height, ShaderProgram *shaderProgram); ~Framebuffer(); void bind(); @@ -31,7 +31,11 @@ private: ShaderProgram *shaderProgram; }; -enum depthMapType {DEPTHMAP_NORMAL, DEPTHMAP_CUBEMAP}; +enum depthMapType +{ + DEPTHMAP_NORMAL, + DEPTHMAP_CUBEMAP +}; // Framebuffer without color buffer. (Shadows) class DepthMap diff --git a/src/Helper.cpp b/src/Helper.cpp index ee550c3..e04ea30 100644 --- a/src/Helper.cpp +++ b/src/Helper.cpp @@ -1,7 +1,7 @@ #include "Helper.h" -#include #include +#include #ifdef __linux__ #include @@ -21,116 +21,117 @@ void Helper::sleep(uint32_t us) #endif } -void Helper::gl_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam) +void Helper::gl_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, + const GLchar *message, const void *userParam) { (void)length; (void)userParam; - const char* _source; - const char* _type; - const char* _severity; + const char *_source; + const char *_type; + const char *_severity; // Remove unwanted newline characters from message string std::string _message = message; _message.erase(std::remove(_message.begin(), _message.end(), '\n'), _message.end()); switch (source) { - case GL_DEBUG_SOURCE_API: - _source = "API"; - break; + case GL_DEBUG_SOURCE_API: + _source = "API"; + break; - case GL_DEBUG_SOURCE_WINDOW_SYSTEM: - _source = "WINDOW SYSTEM"; - break; + case GL_DEBUG_SOURCE_WINDOW_SYSTEM: + _source = "WINDOW SYSTEM"; + break; - case GL_DEBUG_SOURCE_SHADER_COMPILER: - _source = "SHADER COMPILER"; - break; + case GL_DEBUG_SOURCE_SHADER_COMPILER: + _source = "SHADER COMPILER"; + break; - case GL_DEBUG_SOURCE_THIRD_PARTY: - _source = "THIRD PARTY"; - break; + case GL_DEBUG_SOURCE_THIRD_PARTY: + _source = "THIRD PARTY"; + break; - case GL_DEBUG_SOURCE_APPLICATION: - _source = "APPLICATION"; - break; + case GL_DEBUG_SOURCE_APPLICATION: + _source = "APPLICATION"; + break; - case GL_DEBUG_SOURCE_OTHER: - _source = "UNKNOWN"; - break; + case GL_DEBUG_SOURCE_OTHER: + _source = "UNKNOWN"; + break; - default: - _source = "UNKNOWN"; - break; + default: + _source = "UNKNOWN"; + break; } switch (type) { - case GL_DEBUG_TYPE_ERROR: - _type = "ERROR"; - break; + case GL_DEBUG_TYPE_ERROR: + _type = "ERROR"; + break; - case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: - _type = "DEPRECATED BEHAVIOR"; - break; + case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: + _type = "DEPRECATED BEHAVIOR"; + break; - case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: - _type = "UDEFINED BEHAVIOR"; - break; + case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: + _type = "UDEFINED BEHAVIOR"; + break; - case GL_DEBUG_TYPE_PORTABILITY: - _type = "PORTABILITY"; - break; + case GL_DEBUG_TYPE_PORTABILITY: + _type = "PORTABILITY"; + break; - case GL_DEBUG_TYPE_PERFORMANCE: - _type = "PERFORMANCE"; - break; + case GL_DEBUG_TYPE_PERFORMANCE: + _type = "PERFORMANCE"; + break; - case GL_DEBUG_TYPE_OTHER: - _type = "OTHER"; - break; + case GL_DEBUG_TYPE_OTHER: + _type = "OTHER"; + break; - case GL_DEBUG_TYPE_MARKER: - _type = "MARKER"; - break; + case GL_DEBUG_TYPE_MARKER: + _type = "MARKER"; + break; - default: - _type = "UNKNOWN"; - break; + default: + _type = "UNKNOWN"; + break; } switch (severity) { - case GL_DEBUG_SEVERITY_HIGH: - _severity = "HIGH"; - break; + case GL_DEBUG_SEVERITY_HIGH: + _severity = "HIGH"; + break; - case GL_DEBUG_SEVERITY_MEDIUM: - _severity = "MEDIUM"; - break; + case GL_DEBUG_SEVERITY_MEDIUM: + _severity = "MEDIUM"; + break; - case GL_DEBUG_SEVERITY_LOW: - _severity = "LOW"; - break; + case GL_DEBUG_SEVERITY_LOW: + _severity = "LOW"; + break; - case GL_DEBUG_SEVERITY_NOTIFICATION: - _severity = "NOTIFICATION"; - break; + case GL_DEBUG_SEVERITY_NOTIFICATION: + _severity = "NOTIFICATION"; + break; - default: - _severity = "UNKNOWN"; - break; + default: + _severity = "UNKNOWN"; + break; } if (severity == GL_DEBUG_SEVERITY_HIGH || severity == GL_DEBUG_SEVERITY_MEDIUM) std::cout << "[OpenGL Debug Message]" << std::endl - << "Message: " << _message << std::endl - << "Source: " << _source << std::endl - << "Type: " << _type << std::endl - << "ID: " << id << std::endl - << "Severity: " << _severity << std::endl - << std::endl; + << "Message: " << _message << std::endl + << "Source: " << _source << std::endl + << "Type: " << _type << std::endl + << "ID: " << id << std::endl + << "Severity: " << _severity << std::endl + << std::endl; } -Helper::Timer::Timer(const std::string& name) : name(name) +Helper::Timer::Timer(const std::string &name) : name(name) { start = std::chrono::high_resolution_clock::now(); } diff --git a/src/Helper.h b/src/Helper.h index 8e1ebf4..06a37e1 100644 --- a/src/Helper.h +++ b/src/Helper.h @@ -1,19 +1,20 @@ #pragma once +#include +#include #include #include -#include -#include -namespace Helper -{ +namespace Helper { void sleep(uint32_t us); -void gl_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam); +void gl_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, + const void *userParam); -class Timer { +class Timer +{ public: - Timer(const std::string& name); + Timer(const std::string &name); ~Timer(); private: @@ -22,4 +23,4 @@ private: std::chrono::duration duration; }; -} +} // namespace Helper diff --git a/src/JsonParser.cpp b/src/JsonParser.cpp index 801a1d0..11f7116 100644 --- a/src/JsonParser.cpp +++ b/src/JsonParser.cpp @@ -1,10 +1,10 @@ #include "JsonParser.h" #include -#include #include +#include -JsonParser::JsonParser(const std::string& path) +JsonParser::JsonParser(const std::string &path) { std::ifstream file(path.c_str(), std::ifstream::binary); @@ -24,17 +24,16 @@ JsonParser::JsonParser(const std::string& path) } JsonParser::~JsonParser() -{ +{} -} - -std::vector JsonParser::getModels() +std::vector JsonParser::getModels() { - std::vector temp_models; + std::vector temp_models; const Json::Value modelsJson = root["models"]; - struct ModelSkeleton { + struct ModelSkeleton + { std::string model_name; std::string model_path; }; @@ -51,26 +50,27 @@ std::vector JsonParser::getModels() std::vector> futures; std::mutex mutex; - auto* temp_models_ptr = &temp_models; - for (const auto& model_skeleton : model_skeletons) { + auto *temp_models_ptr = &temp_models; + for (const auto &model_skeleton : model_skeletons) { auto loadModel = [&]() { Model *current_model = new Model(model_skeleton.model_name, model_skeleton.model_path); - if(current_model) { + if (current_model) { std::lock_guard lock(mutex); temp_models_ptr->push_back(current_model); - std::cout << "Loaded Model \"" << model_skeleton.model_name << "\" from \"" << model_skeleton.model_path << "\"" << std::endl; + std::cout << "Loaded Model \"" << model_skeleton.model_name << "\" from \"" << model_skeleton.model_path + << "\"" << std::endl; } }; futures.push_back(std::async(std::launch::async, loadModel)); - } + } return temp_models; } -std::vector JsonParser::getEntities(std::vector &models, std::vector shaderPrograms) +std::vector JsonParser::getEntities(std::vector &models, std::vector shaderPrograms) { - std::vector temp_entities; + std::vector temp_entities; const Json::Value entitiesJson = root["entities"]; @@ -83,20 +83,21 @@ std::vector JsonParser::getEntities(std::vector &models, std::v ShaderProgram *shaderProgram = nullptr; for (auto it = shaderPrograms.begin(); it != shaderPrograms.end(); it++) { - if((*it)->getUniqueName() == entity_shaderProgram) { + if ((*it)->getUniqueName() == entity_shaderProgram) { shaderProgram = *it; } } - if(!shaderProgram) - std::cout << "[Warning] ShaderProgram could not be found by name \"" << entity_shaderProgram << "\"" << std::endl; + if (!shaderProgram) + std::cout << "[Warning] ShaderProgram could not be found by name \"" << entity_shaderProgram << "\"" + << std::endl; Model *current_model = nullptr; for (auto it = models.begin(); it != models.end(); it++) { - if((*it)->getUniqueName() == entity_model) { + if ((*it)->getUniqueName() == entity_model) { current_model = *it; } } - if(!current_model) { + if (!current_model) { // Apply fallback model (first model in vector) current_model = models[0]; std::cout << "[Warning] Model could not be found by unique name \"" << entity_model << "\"" << std::endl; @@ -105,17 +106,17 @@ std::vector JsonParser::getEntities(std::vector &models, std::v const Json::Value positionJson = entitiesJson[index]["position"]; const Json::Value rotationJson = entitiesJson[index]["rotation"]; const Json::Value scaleJson = entitiesJson[index]["scale"]; - if(!positionJson.empty()) { + if (!positionJson.empty()) { entitiy_position.x = positionJson[0].asFloat(); entitiy_position.y = positionJson[1].asFloat(); entitiy_position.z = positionJson[2].asFloat(); } - if(!rotationJson.empty()) { + if (!rotationJson.empty()) { entity_rotation.s = rotationJson[0].asFloat(); entity_rotation.t = rotationJson[1].asFloat(); entity_rotation.p = rotationJson[2].asFloat(); } - if(!scaleJson.empty()) { + if (!scaleJson.empty()) { entity_scale = scaleJson.asFloat(); } @@ -132,7 +133,7 @@ std::vector JsonParser::getEntities(std::vector &models, std::v std::vector JsonParser::getShaderPrograms() { - std::vector temp_shaderPrograms; + std::vector temp_shaderPrograms; const Json::Value shaderProgramsJson = root["shaderPrograms"]; @@ -143,10 +144,12 @@ std::vector JsonParser::getShaderPrograms() std::string shaderProgram_geometryPath = shaderProgramsJson[index]["geometryPath"].asString(); ShaderProgram *current_shaderProgram; - if(shaderProgram_geometryPath.empty()) { - current_shaderProgram = new ShaderProgram(shaderProgram_name, shaderProgram_vertexPath, shaderProgram_fragmentPath); + if (shaderProgram_geometryPath.empty()) { + current_shaderProgram = + new ShaderProgram(shaderProgram_name, shaderProgram_vertexPath, shaderProgram_fragmentPath); } else { - current_shaderProgram = new ShaderProgram(shaderProgram_name, shaderProgram_vertexPath, shaderProgram_geometryPath, shaderProgram_fragmentPath); + current_shaderProgram = new ShaderProgram(shaderProgram_name, shaderProgram_vertexPath, + shaderProgram_geometryPath, shaderProgram_fragmentPath); } temp_shaderPrograms.push_back(current_shaderProgram); std::cout << "Loaded ShaderProgram \"" << shaderProgram_name << "\"" << std::endl; @@ -155,9 +158,9 @@ std::vector JsonParser::getShaderPrograms() return temp_shaderPrograms; } -std::vector JsonParser::getLights(ShaderProgram* shaderProgram) +std::vector JsonParser::getLights(ShaderProgram *shaderProgram) { - std::vector temp_lights; + std::vector temp_lights; glm::vec3 light_direction = {1.0f, 0.0f, 0.0f}; glm::vec3 light_position = {}; glm::vec3 light_color = {1.0f, 1.0f, 1.0f}; @@ -169,21 +172,22 @@ std::vector JsonParser::getLights(ShaderProgram* shaderProgram) Json::Value colorJson = directionalLightsJson["color"]; Json::Value intensityJson = directionalLightsJson["intensity"]; - if(!intensityJson.empty()) { + if (!intensityJson.empty()) { light_intensity = intensityJson.asFloat(); } - if(!directionJson.empty()) { + if (!directionJson.empty()) { light_direction.x = directionJson[0].asFloat(); light_direction.y = directionJson[1].asFloat(); light_direction.z = directionJson[2].asFloat(); } - if(!colorJson.empty()) { + if (!colorJson.empty()) { light_color.x = colorJson[0].asFloat(); light_color.y = colorJson[1].asFloat(); light_color.z = colorJson[2].asFloat(); } - DirectionalLight *current_directionalLight = new DirectionalLight(light_direction, light_color, light_intensity, shaderProgram); + DirectionalLight *current_directionalLight = + new DirectionalLight(light_direction, light_color, light_intensity, shaderProgram); current_directionalLight->setActive(true); temp_lights.push_back(current_directionalLight); @@ -198,15 +202,15 @@ std::vector JsonParser::getLights(ShaderProgram* shaderProgram) colorJson = pointLightsJson[index]["color"]; intensityJson = pointLightsJson[index]["intensity"]; - if(!intensityJson.empty()) { + if (!intensityJson.empty()) { light_intensity = intensityJson.asFloat(); } - if(!positionJson.empty()) { + if (!positionJson.empty()) { light_position.x = positionJson[0].asFloat(); light_position.y = positionJson[1].asFloat(); light_position.z = positionJson[2].asFloat(); } - if(!colorJson.empty()) { + if (!colorJson.empty()) { light_color.x = colorJson[0].asFloat(); light_color.y = colorJson[1].asFloat(); light_color.z = colorJson[2].asFloat(); @@ -218,11 +222,12 @@ std::vector JsonParser::getLights(ShaderProgram* shaderProgram) } // In case there aren't enough PointLights defined in the Json file - for(; NUM_POINT_LIGHTS - index > 0; index++) { + for (; NUM_POINT_LIGHTS - index > 0; index++) { const glm::vec3 default_position(0.0f); const glm::vec3 default_color(1.0f); const float default_intensity = 10.0f; - PointLight *current_pointLight = new PointLight(default_position, default_color, default_intensity, shaderProgram); + PointLight *current_pointLight = + new PointLight(default_position, default_color, default_intensity, shaderProgram); current_pointLight->setActive(false); temp_lights.push_back(current_pointLight); } @@ -230,9 +235,9 @@ std::vector JsonParser::getLights(ShaderProgram* shaderProgram) return temp_lights; } -std::vector JsonParser::getScreens(ShaderProgram *menuProgram, Framebuffer *framebuffer) +std::vector JsonParser::getScreens(ShaderProgram *menuProgram, Framebuffer *framebuffer) { - std::vector temp_screens; + std::vector temp_screens; const Json::Value loadingScreenJson = root["loadingScreen"]; const Json::Value mainMenuScreenJson = root["mainMenuScreen"]; @@ -251,9 +256,9 @@ std::vector JsonParser::getScreens(ShaderProgram *menuProgram, Framebuf return temp_screens; } -std::vector JsonParser::getWidgetsFromScreen(const Json::Value &screenJson) +std::vector JsonParser::getWidgetsFromScreen(const Json::Value &screenJson) { - std::vector temp_widgets; + std::vector temp_widgets; // Iterate over widgets unsigned int index = 0; @@ -263,16 +268,12 @@ std::vector JsonParser::getWidgetsFromScreen(const Json::Value &screenJ const Json::Value currentWidgetPosition = currentWidgetJson["position"]; const Json::Value currentWidgetDimensions = currentWidgetJson["dimensions"]; std::string name = currentWidgetJson["unique_name"].asString(); - Texture *currentWidgetTexture = new Texture(currentWidgetTextureJson.asString().c_str(), textureType::texture_diffuse); - Widget *currentWidget = new Widget( - name, - currentWidgetTexture, - currentWidgetPosition[0].asFloat(), - currentWidgetPosition[1].asFloat(), - currentWidgetDimensions[0].asFloat(), - currentWidgetDimensions[1].asFloat(), - currentWidgetJson["callbackId"].asUInt() - ); + Texture *currentWidgetTexture = + new Texture(currentWidgetTextureJson.asString().c_str(), textureType::texture_diffuse); + Widget *currentWidget = + new Widget(name, currentWidgetTexture, currentWidgetPosition[0].asFloat(), + currentWidgetPosition[1].asFloat(), currentWidgetDimensions[0].asFloat(), + currentWidgetDimensions[1].asFloat(), currentWidgetJson["callbackId"].asUInt()); temp_widgets.push_back(currentWidget); } return temp_widgets; @@ -280,7 +281,7 @@ std::vector JsonParser::getWidgetsFromScreen(const Json::Value &screenJ Skybox *JsonParser::getSkybox(Model *cubeModel, ShaderProgram *skyboxProgram) { - Skybox* temp_skybox; + Skybox *temp_skybox; const Json::Value shaderProgramsJson = root["skybox"]; diff --git a/src/JsonParser.h b/src/JsonParser.h index f0a3959..f2133c5 100644 --- a/src/JsonParser.h +++ b/src/JsonParser.h @@ -1,10 +1,10 @@ #pragma once -#include "Model.h" #include "Entity.h" -#include "ShaderProgram.h" #include "Light.h" +#include "Model.h" #include "Screen.h" +#include "ShaderProgram.h" #include #include @@ -13,19 +13,19 @@ class JsonParser { public: - JsonParser(const std::string& path); + JsonParser(const std::string &path); ~JsonParser(); - std::vector getModels(); - std::vector getEntities(std::vector &models, std::vector shaderPrograms); - std::vector getLights(ShaderProgram *shaderProgram); - std::vector getScreens(ShaderProgram *menuProgram, Framebuffer *framebuffer); + std::vector getModels(); + std::vector getEntities(std::vector &models, std::vector shaderPrograms); + std::vector getLights(ShaderProgram *shaderProgram); + std::vector getScreens(ShaderProgram *menuProgram, Framebuffer *framebuffer); Skybox *getSkybox(Model *cubeModel, ShaderProgram *skyboxProgram); - std::vector getShaderPrograms(); + std::vector getShaderPrograms(); private: - std::vector getWidgetsFromScreen(const Json::Value &screenJson); + std::vector getWidgetsFromScreen(const Json::Value &screenJson); private: Json::Value root; diff --git a/src/Light.cpp b/src/Light.cpp index 97e8190..e7edd9d 100644 --- a/src/Light.cpp +++ b/src/Light.cpp @@ -6,21 +6,19 @@ uint32_t Light::id_counter = 0; // Light -Light::Light(glm::vec3 color, float intensity, ShaderProgram* shaderProgram) : - shaderProgram(shaderProgram), - intensity(intensity) +Light::Light(glm::vec3 color, float intensity, ShaderProgram *shaderProgram) + : shaderProgram(shaderProgram), intensity(intensity) { id = id_counter++; lightColor = color * intensity; } - glm::vec3 Light::getColor() { return lightColor; } -void Light::setShaderProgram(ShaderProgram* shaderProgram) +void Light::setShaderProgram(ShaderProgram *shaderProgram) { this->shaderProgram = shaderProgram; update(); @@ -37,7 +35,6 @@ void Light::setIntensity(float intensity) this->intensity = intensity; } - void Light::setActive(bool active) { isActive = active; @@ -46,9 +43,8 @@ void Light::setActive(bool active) // PointLight -PointLight::PointLight(glm::vec3 position, glm::vec3 color, float intensity, ShaderProgram *shaderProgram) : - Light(color, intensity, shaderProgram), - position(position) +PointLight::PointLight(glm::vec3 position, glm::vec3 color, float intensity, ShaderProgram *shaderProgram) + : Light(color, intensity, shaderProgram), position(position) { // Empty } @@ -84,9 +80,8 @@ void PointLight::setPosition(glm::vec3 position) // DirectionalLight -DirectionalLight::DirectionalLight(glm::vec3 direction, glm::vec3 color, float intensity, ShaderProgram *shaderProgram) : - Light(color, intensity, shaderProgram), - direction(direction) +DirectionalLight::DirectionalLight(glm::vec3 direction, glm::vec3 color, float intensity, ShaderProgram *shaderProgram) + : Light(color, intensity, shaderProgram), direction(direction) { // Empty } diff --git a/src/Light.h b/src/Light.h index 15667dd..d6b90b2 100644 --- a/src/Light.h +++ b/src/Light.h @@ -9,7 +9,8 @@ class Light { public: - virtual ~Light() {} + virtual ~Light() + {} virtual void update() = 0; diff --git a/src/Menu.cpp b/src/Menu.cpp index 9b3a8fb..8dd97c4 100644 --- a/src/Menu.cpp +++ b/src/Menu.cpp @@ -1,12 +1,12 @@ #include "Menu.h" +#include "Helper.h" #include "JsonParser.h" #include "eventActions.h" -#include "Helper.h" #include -Menu::Menu(Framebuffer *p_framebuffer, ShaderProgram *p_shaderProgram) : - framebuffer(p_framebuffer), shaderProgram(p_shaderProgram) +Menu::Menu(Framebuffer *p_framebuffer, ShaderProgram *p_shaderProgram) + : framebuffer(p_framebuffer), shaderProgram(p_shaderProgram) { JsonParser screenParser("data/screens.json"); screens = screenParser.getScreens(shaderProgram, framebuffer); @@ -20,13 +20,13 @@ Menu::~Menu() } } -Screen *Menu::getScreenByName(const std::string& name) const +Screen *Menu::getScreenByName(const std::string &name) const { if (m_screen_name_cache.find(name) != m_screen_name_cache.end()) return m_screen_name_cache[name]; for (auto it : screens) { - if(it->getUniqueName() == name) { + if (it->getUniqueName() == name) { m_screen_name_cache[name] = it; return it; } @@ -40,11 +40,12 @@ Screen *Menu::getActiveScreen() const return activeScreen; } -void Menu::showScreenByName(const std::string& name) +void Menu::showScreenByName(const std::string &name) { - Screen* screen = getScreenByName(name); + Screen *screen = getScreenByName(name); - if (!screen) return; + if (!screen) + return; screen->draw(); activeScreen = screen; @@ -55,16 +56,16 @@ void Menu::resetActiveScreen() activeScreen = nullptr; } -void Menu::handleMouseButtonActionRegister(bool *mouseButtonActionRegister, Window* window) +void Menu::handleMouseButtonActionRegister(bool *mouseButtonActionRegister, Window *window) { if (mouseButtonActionRegister[mouseButtonActions::leftClicked]) { auto widgets = activeScreen->getWidgets(); for (auto it = widgets.begin(); it != widgets.end(); it++) { if ((*it)->isHovered(window)) { // std::cout << (*it)->getUniqueName() << " clicked!" << std::endl; - if((*it)->getCallbackId() == 1) + if ((*it)->getCallbackId() == 1) resetActiveScreen(); - if((*it)->getCallbackId() == 2) + if ((*it)->getCallbackId() == 2) shouldExit = true; } } @@ -78,11 +79,7 @@ void Menu::writeWindowActions(bool *windowActionRegister) } void Menu::onPlayPressed() -{ - -} +{} void Menu::onExitPressed() -{ - -} +{} diff --git a/src/Menu.h b/src/Menu.h index 077484e..264c36a 100644 --- a/src/Menu.h +++ b/src/Menu.h @@ -3,9 +3,9 @@ #include #include -#include "Screen.h" #include "Framebuffer.h" #include "JsonParser.h" +#include "Screen.h" #include "eventActions.h" class Menu @@ -14,15 +14,15 @@ public: Menu(Framebuffer *p_framebuffer, ShaderProgram *p_shaderProgram); ~Menu(); - Screen *getScreenByName(const std::string& name) const; - void showScreenByName(const std::string& name); + Screen *getScreenByName(const std::string &name) const; + void showScreenByName(const std::string &name); Screen *getActiveScreen() const; void writeWindowActions(bool *windowActionRegister); void resetActiveScreen(); - void handleMouseButtonActionRegister(bool *mouseButtonActionRegister, Window* window); + void handleMouseButtonActionRegister(bool *mouseButtonActionRegister, Window *window); void onPlayPressed(); void onExitPressed(); @@ -33,12 +33,12 @@ private: bool shouldExit = false; - std::vector screens; + std::vector screens; Screen *activeScreen; /*Screen *loadingScreen; Screen *mainMenuScreen; Screen *optionMenuScreen; Screen *pauseMenuScreen;*/ - mutable std::unordered_map m_screen_name_cache; + mutable std::unordered_map m_screen_name_cache; }; diff --git a/src/Mesh.cpp b/src/Mesh.cpp index ef754c9..6256c13 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -1,16 +1,16 @@ #include "Mesh.h" -Mesh::Mesh(std::vector vertices, std::vector indices, std::vector textures) : - numElements(indices.size()), - textures(textures), - vertexArray(static_cast(vertices.data()), static_cast(indices.data()), vertices.size(), indices.size()) +Mesh::Mesh(std::vector vertices, std::vector indices, std::vector textures) + : numElements(indices.size()), textures(textures), + vertexArray(static_cast(vertices.data()), static_cast(indices.data()), vertices.size(), + indices.size()) { // Empty... } void Mesh::draw(ShaderProgram *shaderProgram) { - uint8_t typeNumberCount[TEXTURE_TYPE_NUM_ITEMS] {0}; + uint8_t typeNumberCount[TEXTURE_TYPE_NUM_ITEMS]{0}; glBindTexture(GL_TEXTURE_2D, 0); // Bind all textures in order to its texture unit for (auto it = textures.begin(); it != textures.end(); it++) { @@ -41,7 +41,7 @@ void Mesh::drawWithoutTextures() vertexArray.unbind(); } -VertexArray* Mesh::getVertexArray() +VertexArray *Mesh::getVertexArray() { return &vertexArray; } diff --git a/src/Mesh.h b/src/Mesh.h index b5fa53b..0025dda 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -3,8 +3,8 @@ #include #include "ShaderProgram.h" -#include "VertexArray.h" #include "Texture.h" +#include "VertexArray.h" #include "defines.h" class Mesh diff --git a/src/Model.cpp b/src/Model.cpp index 5afd3f7..c9781c6 100644 --- a/src/Model.cpp +++ b/src/Model.cpp @@ -1,12 +1,11 @@ #include "Model.h" -#include #include +#include uint32_t Model::id_counter = 0; -Model::Model(const std::string &modelName, const std::string &modelPath) : - unique_name(modelName) +Model::Model(const std::string &modelName, const std::string &modelPath) : unique_name(modelName) { directory = modelPath.substr(0, modelPath.find_last_of('/')); @@ -58,12 +57,12 @@ void Model::loadModel(const std::string &pathToModel) } uint32_t numTextures; - input.read((char *) &numTextures, sizeof(uint32_t)); + input.read((char *)&numTextures, sizeof(uint32_t)); std::vector textureTypes; for (unsigned int i = 0; i < numTextures; i++) { uint32_t currentTextureType; - input.read((char *) ¤tTextureType, sizeof(uint32_t)); + input.read((char *)¤tTextureType, sizeof(uint32_t)); textureTypes.push_back(currentTextureType); } @@ -72,7 +71,7 @@ void Model::loadModel(const std::string &pathToModel) std::string currentTextureSource; for (unsigned int i = 0; i < 128; i++) { uint8_t currentChar; - input.read((char *) ¤tChar, sizeof(uint8_t)); + input.read((char *)¤tChar, sizeof(uint8_t)); if (currentChar) { currentTextureSource.push_back(currentChar); @@ -81,7 +80,6 @@ void Model::loadModel(const std::string &pathToModel) textureSources.push_back(currentTextureSource); } - for (unsigned int i = 0; i < numTextures; i++) { TexturePrototype texture_prototype; std::string texturePath = directory + '/' + textureSources[i].c_str(); @@ -110,7 +108,7 @@ void Model::loadModel(const std::string &pathToModel) // Here starts the first mesh uint32_t numMeshes; - input.read((char *) &numMeshes, sizeof(uint32_t)); + input.read((char *)&numMeshes, sizeof(uint32_t)); for (unsigned int j = 0; j < numMeshes; j++) { @@ -118,9 +116,9 @@ void Model::loadModel(const std::string &pathToModel) uint32_t numMeshVertices, numMeshIndices, numMeshTextureIds; - input.read((char *) &numMeshVertices, sizeof(uint32_t)); - input.read((char *) &numMeshIndices, sizeof(uint32_t)); - input.read((char *) &numMeshTextureIds, sizeof(uint32_t)); + input.read((char *)&numMeshVertices, sizeof(uint32_t)); + input.read((char *)&numMeshIndices, sizeof(uint32_t)); + input.read((char *)&numMeshTextureIds, sizeof(uint32_t)); uint32_t vertexBlockSize = numMeshVertices * sizeof(Vertex); uint32_t indexBlockSize = numMeshIndices * sizeof(uint32_t); @@ -129,17 +127,17 @@ void Model::loadModel(const std::string &pathToModel) std::vector meshVertices; meshVertices.resize(numMeshVertices); - input.read((char *) meshVertices.data(), vertexBlockSize); + input.read((char *)meshVertices.data(), vertexBlockSize); mesh_prototype.meshVertices = std::move(meshVertices); std::vector meshIndices; meshIndices.resize(numMeshIndices); - input.read((char *) meshIndices.data(), indexBlockSize); + input.read((char *)meshIndices.data(), indexBlockSize); mesh_prototype.meshIndices = std::move(meshIndices); for (unsigned int i = 0; i < numMeshTextureIds; i++) { uint32_t currentTextureId; - input.read((char *) ¤tTextureId, sizeof(uint32_t)); + input.read((char *)¤tTextureId, sizeof(uint32_t)); mesh_prototype.textureIds.push_back(currentTextureId); } @@ -159,13 +157,13 @@ void Model::prepareModel() model_prepared = true; // Create textures on GPU - for (auto& it : modelTexturePrototypes) { + for (auto &it : modelTexturePrototypes) { Texture *newTex = new Texture(it.texturePath.c_str(), it.textureType); loadedTextures.push_back(newTex); } // Create meshes on GPU - for (const auto& it : modelMeshPrototypes) { + for (const auto &it : modelMeshPrototypes) { std::vector meshTextures; for (const auto it2 : it.textureIds) { meshTextures.push_back(loadedTextures[it2]); @@ -176,8 +174,7 @@ void Model::prepareModel() } } - -Mesh* Model::getMesh(unsigned int index) +Mesh *Model::getMesh(unsigned int index) { return meshes[index]; } diff --git a/src/Model.h b/src/Model.h index b94ffc9..788d0fd 100644 --- a/src/Model.h +++ b/src/Model.h @@ -5,12 +5,14 @@ #include "Mesh.h" -struct TexturePrototype { +struct TexturePrototype +{ uint32_t textureType; std::string texturePath; }; -struct MeshPrototype { +struct MeshPrototype +{ std::vector textureIds; std::vector meshVertices; std::vector meshIndices; @@ -19,7 +21,7 @@ struct MeshPrototype { class Model { public: - Model(const std::string& modelName, const std::string& pathToModel); + Model(const std::string &modelName, const std::string &pathToModel); ~Model(); void prepareModel(); @@ -47,5 +49,4 @@ private: static uint32_t id_counter; uint32_t id; std::string unique_name; - }; diff --git a/src/Screen.cpp b/src/Screen.cpp index fe7fbb2..2e038b5 100644 --- a/src/Screen.cpp +++ b/src/Screen.cpp @@ -3,11 +3,9 @@ uint32_t Screen::id_counter = 0; -Screen::Screen(const std::string &name, std::vector widgets, Framebuffer *framebuffer, ShaderProgram *shaderProgram) : - unique_name(name), - framebuffer(framebuffer), - shaderProgram(shaderProgram), - widgets(widgets) +Screen::Screen(const std::string &name, std::vector widgets, Framebuffer *framebuffer, + ShaderProgram *shaderProgram) + : unique_name(name), framebuffer(framebuffer), shaderProgram(shaderProgram), widgets(widgets) { id = id_counter++; } @@ -15,31 +13,31 @@ Screen::Screen(const std::string &name, std::vector widgets, Framebuffe Screen::~Screen() { // Iterate over Widgets and Textures to delete all of them - for(auto it = widgets.begin(); it != widgets.end(); it++) { + for (auto it = widgets.begin(); it != widgets.end(); it++) { delete *it; } - for(auto it = textures.begin(); it != textures.end(); it++) { + for (auto it = textures.begin(); it != textures.end(); it++) { delete *it; } } -const std::string& Screen::getUniqueName() const +const std::string &Screen::getUniqueName() const { return unique_name; } -std::vector Screen::getWidgets() const +std::vector Screen::getWidgets() const { return widgets; } -Widget *Screen::getWidgetByName(const std::string& name) const +Widget *Screen::getWidgetByName(const std::string &name) const { if (m_widget_name_cache.find(name) != m_widget_name_cache.end()) return m_widget_name_cache[name]; for (auto it : widgets) { - if(it->getUniqueName() == name) { + if (it->getUniqueName() == name) { m_widget_name_cache[name] = it; return it; } @@ -56,8 +54,8 @@ void Screen::draw() const { framebuffer->setExposureCorrection(false); framebuffer->bind(); - - for(auto it = widgets.begin(); it != widgets.end(); it++) { + + for (auto it = widgets.begin(); it != widgets.end(); it++) { (*it)->draw(shaderProgram); } @@ -65,4 +63,3 @@ void Screen::draw() const framebuffer->render(); framebuffer->setExposureCorrection(true); } - diff --git a/src/Screen.h b/src/Screen.h index d304b75..4091fb5 100644 --- a/src/Screen.h +++ b/src/Screen.h @@ -2,8 +2,8 @@ #include -#include "ShaderProgram.h" #include "Framebuffer.h" +#include "ShaderProgram.h" #include "Widget.h" class Menu; @@ -11,16 +11,17 @@ class Menu; class Screen { public: - Screen(const std::string& name, std::vector widgets, Framebuffer* framebuffer, ShaderProgram* shaderProgram); + Screen(const std::string &name, std::vector widgets, Framebuffer *framebuffer, + ShaderProgram *shaderProgram); ~Screen(); - + void addWidget(Widget *widget); void draw() const; - const std::string& getUniqueName() const; - std::vector getWidgets() const; - Widget *getWidgetByName(const std::string& name) const; - + const std::string &getUniqueName() const; + std::vector getWidgets() const; + Widget *getWidgetByName(const std::string &name) const; + private: uint32_t id; static uint32_t id_counter; @@ -28,9 +29,9 @@ private: Framebuffer *framebuffer; ShaderProgram *shaderProgram; - - std::vector textures; - std::vector widgets; - mutable std::unordered_map m_widget_name_cache; + std::vector textures; + std::vector widgets; + + mutable std::unordered_map m_widget_name_cache; }; diff --git a/src/ShaderProgram.cpp b/src/ShaderProgram.cpp index 827fde1..9bf70b9 100644 --- a/src/ShaderProgram.cpp +++ b/src/ShaderProgram.cpp @@ -1,13 +1,14 @@ -#include #include -#include #include #include +#include +#include #include "ShaderProgram.h" -ShaderProgram::ShaderProgram(const std::string &name, const std::string &vertexShaderPath, const std::string &fragmentShaderPath) : - unique_name(name) +ShaderProgram::ShaderProgram(const std::string &name, const std::string &vertexShaderPath, + const std::string &fragmentShaderPath) + : unique_name(name) { std::string vertexShaderSource = parse(vertexShaderPath.c_str()); std::string fragmentShaderSource = parse(fragmentShaderPath.c_str()); @@ -36,8 +37,9 @@ ShaderProgram::ShaderProgram(const std::string &name, const std::string &vertexS #endif } -ShaderProgram::ShaderProgram(const std::string &name, const std::string &vertexShaderPath, const std::string &geometryShaderPath, const std::string &fragmentShaderPath) : - unique_name(name) +ShaderProgram::ShaderProgram(const std::string &name, const std::string &vertexShaderPath, + const std::string &geometryShaderPath, const std::string &fragmentShaderPath) + : unique_name(name) { std::string vertexShaderSource = parse(vertexShaderPath.c_str()); std::string geometryShaderSource = parse(geometryShaderPath.c_str()); @@ -57,7 +59,8 @@ ShaderProgram::ShaderProgram(const std::string &name, const std::string &vertexS GLint isLinked = 0; glGetProgramiv(shaderProgramId, GL_LINK_STATUS, &isLinked); if (!isLinked) { - std::cout << "Failed to link shaderProgram: " << vertexShaderPath << ", " << geometryShaderPath << ", " << fragmentShaderPath << std::endl; + std::cout << "Failed to link shaderProgram: " << vertexShaderPath << ", " << geometryShaderPath << ", " + << fragmentShaderPath << std::endl; } #ifdef _RELEASE @@ -86,7 +89,7 @@ void ShaderProgram::unbind() glUseProgram(0); } -std::string ShaderProgram::parse(const std::string& filename) +std::string ShaderProgram::parse(const std::string &filename) { std::fstream shaderfile; shaderfile.open(filename, std::ios::in); @@ -101,7 +104,7 @@ std::string ShaderProgram::parse(const std::string& filename) return contents; } -GLuint ShaderProgram::compile(const std::string& shaderSource, GLenum type) +GLuint ShaderProgram::compile(const std::string &shaderSource, GLenum type) { GLuint shaderId = glCreateShader(type); const char *src = shaderSource.c_str(); @@ -122,7 +125,8 @@ GLuint ShaderProgram::compile(const std::string& shaderSource, GLenum type) return shaderId; } -GLint ShaderProgram::retrieveUniformLocation(const std::string& name) const { +GLint ShaderProgram::retrieveUniformLocation(const std::string &name) const +{ if (uniformLocationCache.find(name) != uniformLocationCache.end()) return uniformLocationCache[name]; @@ -132,37 +136,37 @@ GLint ShaderProgram::retrieveUniformLocation(const std::string& name) const { return location; } -void ShaderProgram::setUniform(const std::string& name, bool value) const +void ShaderProgram::setUniform(const std::string &name, bool value) const { GLint location = retrieveUniformLocation(name); glUniform1i(location, (int)value); } -void ShaderProgram::setUniform(const std::string& name, int value) const +void ShaderProgram::setUniform(const std::string &name, int value) const { GLint location = retrieveUniformLocation(name); glUniform1i(location, value); } -void ShaderProgram::setUniform(const std::string& name, float value) const +void ShaderProgram::setUniform(const std::string &name, float value) const { GLint location = retrieveUniformLocation(name); glUniform1f(location, value); } -void ShaderProgram::setUniform(const std::string& name, glm::vec3 vector) const +void ShaderProgram::setUniform(const std::string &name, glm::vec3 vector) const { GLint location = retrieveUniformLocation(name); glUniform3f(location, vector.x, vector.y, vector.z); } -void ShaderProgram::setUniform(const std::string& name, glm::mat3 matrix) const +void ShaderProgram::setUniform(const std::string &name, glm::mat3 matrix) const { GLint location = retrieveUniformLocation(name); glUniformMatrix3fv(location, 1, GL_FALSE, glm::value_ptr(matrix)); } -void ShaderProgram::setUniform(const std::string& name, glm::mat4 matrix) const +void ShaderProgram::setUniform(const std::string &name, glm::mat4 matrix) const { GLint location = retrieveUniformLocation(name); glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(matrix)); diff --git a/src/ShaderProgram.h b/src/ShaderProgram.h index 5b01922..7b7aa23 100644 --- a/src/ShaderProgram.h +++ b/src/ShaderProgram.h @@ -2,35 +2,36 @@ #include #include -#include #include +#include class ShaderProgram { public: ShaderProgram(const std::string &name, const std::string &vertexShaderPath, const std::string &fragmentShaderPath); - ShaderProgram(const std::string &name, const std::string &vertexShaderPath, const std::string &geometryShaderPath, const std::string &fragmentShaderPath); + ShaderProgram(const std::string &name, const std::string &vertexShaderPath, const std::string &geometryShaderPath, + const std::string &fragmentShaderPath); ~ShaderProgram(); void bind(); void unbind(); - GLint retrieveUniformLocation(const std::string& name) const; + GLint retrieveUniformLocation(const std::string &name) const; // May be rewritten... - void setUniform(const std::string& name, bool value) const; - void setUniform(const std::string& name, int value) const; - void setUniform(const std::string& name, float value) const; - void setUniform(const std::string& name, glm::vec3 vector) const; - void setUniform(const std::string& name, glm::mat3 matrix) const; - void setUniform(const std::string& name, glm::mat4 matrix) const; + void setUniform(const std::string &name, bool value) const; + void setUniform(const std::string &name, int value) const; + void setUniform(const std::string &name, float value) const; + void setUniform(const std::string &name, glm::vec3 vector) const; + void setUniform(const std::string &name, glm::mat3 matrix) const; + void setUniform(const std::string &name, glm::mat4 matrix) const; public: GLuint getShaderProgramId(); std::string getUniqueName(); private: - std::string parse(const std::string& filename); - GLuint compile(const std::string& shaderSource, GLenum type); + std::string parse(const std::string &filename); + GLuint compile(const std::string &shaderSource, GLenum type); private: GLuint shaderProgramId; diff --git a/src/Texture.cpp b/src/Texture.cpp index cbe5a0f..ea635f0 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -1,10 +1,10 @@ #include "Texture.h" -#include #include +#include -Texture::Texture(const std::string& texturePath, uint8_t textureType) : - texturePath(texturePath), textureType(textureType) +Texture::Texture(const std::string &texturePath, uint8_t textureType) + : texturePath(texturePath), textureType(textureType) { stbi_set_flip_vertically_on_load(1); auto *textureBuffer = stbi_load(texturePath.c_str(), &textureWidth, &textureHeight, &numComponents, 0); @@ -33,13 +33,13 @@ Texture::Texture(const std::string& texturePath, uint8_t textureType) : glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - if (!textureBuffer) { std::cout << "[Warning] Texture " << texturePath << " not found!" << std::endl; return; } - glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, textureWidth, textureHeight, 0, dataFormat, GL_UNSIGNED_BYTE, textureBuffer); + glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, textureWidth, textureHeight, 0, dataFormat, GL_UNSIGNED_BYTE, + textureBuffer); glGenerateMipmap(GL_TEXTURE_2D); stbi_image_free(textureBuffer); @@ -124,36 +124,34 @@ CubeMap::CubeMap(const char *texturePseudoPath) int32_t numComponents; auto *textureBuffer = stbi_load(texturePaths[i].c_str(), &textureWidth, &textureHeight, &numComponents, 0); - GLenum internalFormat; - GLenum dataFormat; - if (numComponents == 1) { - internalFormat = GL_RED; - dataFormat = GL_RED; - } else if (numComponents == 3) { - internalFormat = GL_SRGB8; - dataFormat = GL_RGB; - } else if (numComponents == 4) { - internalFormat = GL_SRGB8_ALPHA8; - dataFormat = GL_RGBA; - } + GLenum internalFormat; + GLenum dataFormat; + if (numComponents == 1) { + internalFormat = GL_RED; + dataFormat = GL_RED; + } else if (numComponents == 3) { + internalFormat = GL_SRGB8; + dataFormat = GL_RGB; + } else if (numComponents == 4) { + internalFormat = GL_SRGB8_ALPHA8; + dataFormat = GL_RGBA; + } if (!textureBuffer) { std::cout << "[Warning] CubeMap Texture " << texturePaths[i].c_str() << " not found!" << std::endl; return; } - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalFormat, textureWidth, textureHeight, 0, dataFormat, GL_UNSIGNED_BYTE, textureBuffer); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalFormat, textureWidth, textureHeight, 0, dataFormat, + GL_UNSIGNED_BYTE, textureBuffer); stbi_image_free(textureBuffer); - } glBindTexture(GL_TEXTURE_CUBE_MAP, 0); } -CubeMap::CubeMap(int RESOLUTION) : - textureWidth(RESOLUTION), - textureHeight(RESOLUTION) +CubeMap::CubeMap(int RESOLUTION) : textureWidth(RESOLUTION), textureHeight(RESOLUTION) { glGenTextures(1, &textureId); @@ -167,7 +165,8 @@ CubeMap::CubeMap(int RESOLUTION) : const unsigned int CUBEMAP_NUM_FACES = 6; for (unsigned int i = 0; i < CUBEMAP_NUM_FACES; i++) { - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT24, RESOLUTION, RESOLUTION, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT24, RESOLUTION, RESOLUTION, 0, + GL_DEPTH_COMPONENT, GL_FLOAT, NULL); } glBindTexture(GL_TEXTURE_CUBE_MAP, 0); @@ -195,12 +194,12 @@ void CubeMap::unbind() void CubeMap::fillTexturePathVector(const char *texturePseudoPath) { for (unsigned int i = 0; i < CUBEMAP_FACES_NUM_ITEMS; i++) { - texturePaths[cm_front] = std::string(texturePseudoPath) + "front.png"; - texturePaths[cm_back] = std::string(texturePseudoPath) + "back.png"; - texturePaths[cm_top] = std::string(texturePseudoPath) + "top.png"; + texturePaths[cm_front] = std::string(texturePseudoPath) + "front.png"; + texturePaths[cm_back] = std::string(texturePseudoPath) + "back.png"; + texturePaths[cm_top] = std::string(texturePseudoPath) + "top.png"; texturePaths[cm_bottom] = std::string(texturePseudoPath) + "bottom.png"; - texturePaths[cm_left] = std::string(texturePseudoPath) + "left.png"; - texturePaths[cm_right] = std::string(texturePseudoPath) + "right.png"; + texturePaths[cm_left] = std::string(texturePseudoPath) + "left.png"; + texturePaths[cm_right] = std::string(texturePseudoPath) + "right.png"; } } diff --git a/src/Texture.h b/src/Texture.h index 871fbcb..1e732b4 100644 --- a/src/Texture.h +++ b/src/Texture.h @@ -10,12 +10,21 @@ #include // Order is important! -enum cubeMapFaces {cm_right, cm_left, cm_top, cm_bottom, cm_back, cm_front, CUBEMAP_FACES_NUM_ITEMS}; +enum cubeMapFaces +{ + cm_right, + cm_left, + cm_top, + cm_bottom, + cm_back, + cm_front, + CUBEMAP_FACES_NUM_ITEMS +}; class Texture { public: - Texture(const std::string& texturePath, uint8_t textureType); + Texture(const std::string &texturePath, uint8_t textureType); ~Texture(); void bind(uint8_t textureUnit, ShaderProgram *shaderProgram, uint8_t textureTypeNum); diff --git a/src/VertexArray.cpp b/src/VertexArray.cpp index ce44052..0650fcd 100644 --- a/src/VertexArray.cpp +++ b/src/VertexArray.cpp @@ -19,23 +19,23 @@ VertexArray::VertexArray(void *vertexData, void *indexData, uint32_t numVertices // Position glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *) offsetof(struct Vertex, position)); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(struct Vertex, position)); // UV Texture Mapping glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *) offsetof(struct Vertex, textureCoords)); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(struct Vertex, textureCoords)); // Normal vectors glEnableVertexAttribArray(2); - glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *) offsetof(struct Vertex, normalVec)); + glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(struct Vertex, normalVec)); // Tangent vectors glEnableVertexAttribArray(3); - glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *) offsetof(struct Vertex, tangentVec)); + glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(struct Vertex, tangentVec)); // Bitangent vectors glEnableVertexAttribArray(4); - glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *) offsetof(struct Vertex, bitangentVec)); + glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(struct Vertex, bitangentVec)); // This will also unbind the vertex buffer and index buffer glBindVertexArray(0); @@ -56,18 +56,19 @@ void VertexArray::unbind() glBindVertexArray(0); } -std::vector VertexArray::createVertices(double *vertices, uint32_t numVertices, float *textureCoordinates) { +std::vector VertexArray::createVertices(double *vertices, uint32_t numVertices, float *textureCoordinates) +{ std::vector vertexVec; uint32_t i = 0; uint32_t k = 0; - for(; i < numVertices; i+=3) { + for (; i < numVertices; i += 3) { Vertex currentVertex = {}; currentVertex.position.x = vertices[i]; - currentVertex.position.y = vertices[i+1]; - currentVertex.position.z = vertices[i+2]; + currentVertex.position.y = vertices[i + 1]; + currentVertex.position.z = vertices[i + 2]; currentVertex.textureCoords.x = textureCoordinates[k]; - currentVertex.textureCoords.y = textureCoordinates[k+1]; - k+=2; + currentVertex.textureCoords.y = textureCoordinates[k + 1]; + k += 2; vertexVec.push_back(currentVertex); } return vertexVec; diff --git a/src/VertexArray.h b/src/VertexArray.h index 5a1fc54..1d8a218 100644 --- a/src/VertexArray.h +++ b/src/VertexArray.h @@ -12,7 +12,7 @@ public: void bind(); void unbind(); - + static std::vector createVertices(double *vertices, uint32_t numVertices, float *textureCoordinates); private: diff --git a/src/Widget.cpp b/src/Widget.cpp index b25cfb0..2fbf890 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -1,36 +1,23 @@ #include "Widget.h" -#include "VertexArray.h" #include "Menu.h" +#include "VertexArray.h" -Widget::Widget(std::string &name, Texture *texture, float p_x, float p_y, float p_w, float p_h, uint16_t callbackId) : - x(p_x), - y(p_y), - w(p_w), - h(p_h), - unique_name(name), - callbackId(callbackId) +Widget::Widget(std::string &name, Texture *texture, float p_x, float p_y, float p_w, float p_h, uint16_t callbackId) + : x(p_x), y(p_y), w(p_w), h(p_h), unique_name(name), callbackId(callbackId) { widgetTextures.push_back(texture); - + double widgetVerticesData[12] = { - 2 * (x + w) - 1.0f, 2 * (y) - 1.0f, 0.0f, // Bottom right - 2 * (x) - 1.0f, 2 * (y + h) - 1.0f, 0.0f, // Top left - 2 * (x) - 1.0f, 2 * (y) - 1.0f, 0.0f, // Bottom left - 2 * (x + w) - 1.0f, 2 * (y + h) - 1.0f, 0.0f // Top right + 2 * (x + w) - 1.0f, 2 * (y)-1.0f, 0.0f, // Bottom right + 2 * (x)-1.0f, 2 * (y + h) - 1.0f, 0.0f, // Top left + 2 * (x)-1.0f, 2 * (y)-1.0f, 0.0f, // Bottom left + 2 * (x + w) - 1.0f, 2 * (y + h) - 1.0f, 0.0f // Top right }; - - unsigned int widgetIndicesData[6] = { - 0, 1, 2, - 0, 3, 1 - }; - - float widgetTextureCoordinates[8] = { - 1.0f, 0.0f, - 0.0f, 1.0f, - 0.0f, 0.0f, - 1.0f, 1.0f - }; - + + unsigned int widgetIndicesData[6] = {0, 1, 2, 0, 3, 1}; + + float widgetTextureCoordinates[8] = {1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f}; + widgetVertices = VertexArray::createVertices(widgetVerticesData, 12, widgetTextureCoordinates); widgetIndices.assign(widgetIndicesData, widgetIndicesData + 6); widgetMesh = new Mesh(widgetVertices, widgetIndices, widgetTextures); @@ -69,7 +56,7 @@ bool Widget::isHovered(Window *window) double yrel = -ypos / height + 1; bool isHovered = false; - if(xrel >= x && xrel <= x + w && yrel >= y && yrel <= y + h) + if (xrel >= x && xrel <= x + w && yrel >= y && yrel <= y + h) isHovered = true; return isHovered; diff --git a/src/Widget.h b/src/Widget.h index ba7df76..152bbd5 100644 --- a/src/Widget.h +++ b/src/Widget.h @@ -1,8 +1,8 @@ #pragma once -#include "Texture.h" -#include "Mesh.h" #include "Framebuffer.h" +#include "Mesh.h" +#include "Texture.h" #include "Window.h" #include "eventActions.h" @@ -13,7 +13,7 @@ class Widget public: Widget(std::string &name, Texture *texture, float x, float y, float w, float h, uint16_t callbackId); ~Widget(); - + void draw(ShaderProgram *shaderProgram); std::string getUniqueName(); @@ -27,10 +27,10 @@ private: std::string unique_name; uint16_t callbackId; - + std::vector widgetVertices; std::vector widgetIndices; - std::vector widgetTextures; - + std::vector widgetTextures; + Mesh *widgetMesh; }; diff --git a/src/Window.cpp b/src/Window.cpp index fd39225..ff9ea3a 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -1,10 +1,10 @@ -#include #include +#include -#include "Window.h" -#include "eventActions.h" -#include "defines.h" #include "Helper.h" +#include "Window.h" +#include "defines.h" +#include "eventActions.h" Window::Window() { @@ -76,7 +76,7 @@ Window::Window() glViewport(0, 0, width, height); // Tell GLFW which function to call when window is resized - //glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + // glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); } Window::~Window() @@ -102,7 +102,6 @@ void Window::updateWindowDimensions() glViewport(0, 0, width, height); } - void Window::setCatchedCursor(bool value) { if (value) { @@ -148,7 +147,7 @@ void Window::framebuffer_size_callback(GLFWwindow *window, int width, int height glViewport(0, 0, width, height); } -GLFWwindow* Window::getGLFWwindow() +GLFWwindow *Window::getGLFWwindow() { return window; } @@ -165,7 +164,7 @@ int Window::getWindowHeight() float Window::getWindowAspectRatio() { - return (float) width / (float) height; + return (float)width / (float)height; } bool Window::getMouseIsCatched() diff --git a/src/World.cpp b/src/World.cpp index f2fc013..c4a75d9 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -1,12 +1,12 @@ #include "World.h" -#include "JsonParser.h" #include "Controller.h" +#include "JsonParser.h" #include -World::World(std::vector shaderPrograms) : - shaderProgram(Controller::getShaderProgramByName(shaderPrograms, "defaultProgram")), - depthMapDirectionalFBO(DEPTHMAP_NORMAL, SHADOW_RES) +World::World(std::vector shaderPrograms) + : shaderProgram(Controller::getShaderProgramByName(shaderPrograms, "defaultProgram")), + depthMapDirectionalFBO(DEPTHMAP_NORMAL, SHADOW_RES) { // Create 4 depthMaps for (int i = 0; i < 4; i++) { @@ -22,12 +22,13 @@ World::World(std::vector shaderPrograms) : JsonParser modelParser("data/models.json"); models = modelParser.getModels(); - for (const auto& it : models) { + for (const auto &it : models) { it->prepareModel(); } entities = modelParser.getEntities(models, shaderPrograms); - skybox = modelParser.getSkybox(getModelByName("cube"), Controller::getShaderProgramByName(shaderPrograms, "skyboxProgram")); + skybox = modelParser.getSkybox(getModelByName("cube"), + Controller::getShaderProgramByName(shaderPrograms, "skyboxProgram")); JsonParser lightParser("data/lights.json"); lights = lightParser.getLights(shaderProgram); @@ -76,7 +77,7 @@ void World::clearEntities() void World::updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color, float intensity) { - std::vector pointLights = getPointLights(); + std::vector pointLights = getPointLights(); pointLights[lightId]->setActive(active); pointLights[lightId]->setPosition(position); pointLights[lightId]->setIntensity(intensity); @@ -117,7 +118,8 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg glClear(GL_DEPTH_BUFFER_BIT); // --- Directional shadows --- - glm::mat4 directionalLightView = glm::lookAt(-5.0f * glm::vec3(-0.2f, -1.0f, -0.3f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); + glm::mat4 directionalLightView = + glm::lookAt(-5.0f * glm::vec3(-0.2f, -1.0f, -0.3f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 directionalLightViewProjectionMatrix = directionalLightProjection * directionalLightView; // Draw scene from light perspective @@ -142,7 +144,7 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg shaderProgram->unbind(); // --- Point shadows --- - std::vector pointLights = getPointLights(); + std::vector pointLights = getPointLights(); // 4 depthMaps for 4 point lights for (int i = 0; i < 1; i++) { @@ -153,17 +155,24 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg // Create 6 view matrices for every face of the cubeMap std::vector viewProjMatrices; glm::vec3 lightPos = pointLights[i]->getPosition(); - viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f))); - viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f))); - viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f))); - viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f))); - viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f))); - viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f))); + viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(1.0f, 0.0f, 0.0f), + glm::vec3(0.0f, -1.0f, 0.0f))); + viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(-1.0f, 0.0f, 0.0f), + glm::vec3(0.0f, -1.0f, 0.0f))); + viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 1.0f, 0.0f), + glm::vec3(0.0f, 0.0f, 1.0f))); + viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, -1.0f, 0.0f), + glm::vec3(0.0f, 0.0f, -1.0f))); + viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, 1.0f), + glm::vec3(0.0f, -1.0f, 0.0f))); + viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, -1.0f), + glm::vec3(0.0f, -1.0f, 0.0f))); pointShaderProgram->bind(); for (int i = 0; i < 6; i++) { - pointShaderProgram->setUniform(("u_shadowMatrices[" + std::to_string(i) + "]").c_str(), viewProjMatrices[i]); + pointShaderProgram->setUniform(("u_shadowMatrices[" + std::to_string(i) + "]").c_str(), + viewProjMatrices[i]); } pointShaderProgram->setUniform("pointShadowDepthMapFarPlane", far_plane_point); @@ -194,10 +203,10 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg glCullFace(GL_FRONT); } -Model* World::getModelByName(std::string name) +Model *World::getModelByName(std::string name) { for (auto it = models.begin(); it != models.end(); it++) { - if((*it)->getUniqueName() == name) { + if ((*it)->getUniqueName() == name) { return *it; } } @@ -205,10 +214,10 @@ Model* World::getModelByName(std::string name) return nullptr; } -Entity* World::getEntityByName(std::string name) +Entity *World::getEntityByName(std::string name) { for (auto it = entities.begin(); it != entities.end(); it++) { - if((*it)->getUniqueName() == name) { + if ((*it)->getUniqueName() == name) { return *it; } } @@ -216,10 +225,10 @@ Entity* World::getEntityByName(std::string name) return nullptr; } -Entity* World::getEntityById(uint32_t id) +Entity *World::getEntityById(uint32_t id) { for (auto it = entities.begin(); it != entities.end(); it++) { - if((*it)->getId() == id) { + if ((*it)->getId() == id) { return *it; } } @@ -227,12 +236,12 @@ Entity* World::getEntityById(uint32_t id) return nullptr; } -std::vector World::getPointLights() +std::vector World::getPointLights() { - std::vector temp_pointLights; + std::vector temp_pointLights; for (auto it = lights.begin(); it != lights.end(); it++) { - PointLight *temp_pointLight = dynamic_cast(*it); + PointLight *temp_pointLight = dynamic_cast(*it); if (temp_pointLight) { temp_pointLights.push_back(temp_pointLight); } @@ -241,25 +250,25 @@ std::vector World::getPointLights() return temp_pointLights; } -DirectionalLight * World::getDirectionalLight() +DirectionalLight *World::getDirectionalLight() { DirectionalLight *temp_directionalLight = nullptr; for (auto it = lights.begin(); it != lights.end(); it++) { - temp_directionalLight = dynamic_cast(*it); - if(temp_directionalLight) + temp_directionalLight = dynamic_cast(*it); + if (temp_directionalLight) break; } return temp_directionalLight; } -std::vector World::getEntities() +std::vector World::getEntities() { return entities; } -Skybox* World::getSkybox() +Skybox *World::getSkybox() { return skybox; } diff --git a/src/World.h b/src/World.h index 02e1504..4295551 100644 --- a/src/World.h +++ b/src/World.h @@ -2,16 +2,16 @@ #include -#include "Light.h" #include "Camera.h" #include "Entity.h" -#include "ShaderProgram.h" #include "Framebuffer.h" +#include "Light.h" +#include "ShaderProgram.h" class World { public: - World(std::vector shaderPrograms); + World(std::vector shaderPrograms); ~World(); void addEntity(Entity *entity); @@ -21,13 +21,13 @@ public: void updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color, float intensity); void updateDirectionalLight(bool active, glm::vec3 direction, glm::vec3 color); - std::vector getEntities(); - std::vector getPointLights(); - DirectionalLight * getDirectionalLight(); + std::vector getEntities(); + std::vector getPointLights(); + DirectionalLight *getDirectionalLight(); Skybox *getSkybox(); - Entity* getEntityByName(std::string name); - Entity* getEntityById(uint32_t id); - Model* getModelByName(std::string name); + Entity *getEntityByName(std::string name); + Entity *getEntityById(uint32_t id); + Model *getModelByName(std::string name); void draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition); void calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram); @@ -35,12 +35,12 @@ public: private: ShaderProgram *shaderProgram; - std::vector models; - std::vector entities; + std::vector models; + std::vector entities; Skybox *skybox; // Lights - std::vector lights; + std::vector lights; // Shadows const int SHADOW_RES = 4096 / 4; @@ -49,9 +49,11 @@ private: // Shadow projection matrices 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); + 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); + glm::mat4 pointLightProjection = + glm::perspective(glm::radians(90.0f), aspect_ratio_point, near_plane_point, far_plane_point); }; diff --git a/src/defines.h b/src/defines.h index 28c291d..a663029 100644 --- a/src/defines.h +++ b/src/defines.h @@ -5,7 +5,8 @@ #define INIT_WINDOW_WIDTH 1280 #define INIT_WINDOW_HEIGHT 720 -enum textureType { +enum textureType +{ texture_diffuse, texture_specular, texture_normal, diff --git a/src/eventActions.h b/src/eventActions.h index 8a8aa49..3277087 100644 --- a/src/eventActions.h +++ b/src/eventActions.h @@ -1,6 +1,7 @@ #pragma once -enum cameraActions { +enum cameraActions +{ cameraUp, cameraDown, cameraForward, @@ -10,20 +11,23 @@ enum cameraActions { CAMERA_ACTION_NUM_ITEMS }; -enum cameraMouseActions { +enum cameraMouseActions +{ cameraMouseDeltaX, cameraMouseDeltaY, CAMERA_MOUSE_ACTION_NUM_ITEMS }; -enum windowActions { +enum windowActions +{ wireFrameToggle, mouseCatchToggle, windowShouldClose, WINDOW_ACTION_NUM_ITEMS }; -enum mouseButtonActions { +enum mouseButtonActions +{ leftClicked, rightClicked, middleClicked, diff --git a/src/main.cpp b/src/main.cpp index 37109a0..6479420 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ -#include -#include #include +#include +#include #include "Controller.h" @@ -17,13 +17,13 @@ int main(int argc, char **argv) // Create controller Controller *mainController = new Controller(); - const char* fps_env = std::getenv("MAXFPS"); - if(fps_env) { + const char *fps_env = std::getenv("MAXFPS"); + if (fps_env) { uint16_t maxfps = std::stoul(fps_env); mainController->setMaxFps(maxfps); std::cout << "[Warning] Default max FPS overridden with " << maxfps << " by environment." << std::endl; } - + mainController->run(); delete mainController;