diff --git a/.gitmodules b/.gitmodules index 0bca1e8..83efd45 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "lib/spdlog"] path = lib/spdlog url = git@github.com:gabime/spdlog.git +[submodule "lib/entt"] + path = lib/entt + url = https://github.com/skypjack/entt.git diff --git a/data/scene.json b/data/scene.json deleted file mode 100644 index c79c57d..0000000 --- a/data/scene.json +++ /dev/null @@ -1,108 +0,0 @@ -{ - "models": [ - { - "unique_name": "fallback", - "path": "data/res/models/fallback.ffo" - }, - { - "unique_name": "backpack", - "path": "data/res/models/backpack.ffo" - }, - { - "unique_name": "ground", - "path": "data/res/models/wood_floor.ffo" - }, - { - "unique_name": "cube", - "path": "data/res/models/cube.ffo" - }, - { - "unique_name": "container", - "path": "data/res/models/container.ffo" - } - ], - "entities": [ - { - "unique_name": "backpack", - "model": "backpack", - "shaderProgram": "defaultProgram", - "position": [ - 0.0, - 1.0, - 0.0 - ], - "rotation": [ - 0.0, - 0.0, - 0.0 - ], - "scale": 0.6 - }, - { - "unique_name": "container", - "parent": "backpack", - "model": "container", - "shaderProgram": "defaultProgram", - "position": [ - 10.0, - 1.0, - 0.0 - ], - "rotation": [ - 45.0, - 45.0, - 45.0 - ] - }, - { - "unique_name": "ground", - "model": "ground", - "shaderProgram": "defaultProgram" - }, - { - "unique_name": "light", - "model": "cube", - "shaderProgram": "lightProgram" - } - ], - "directionalLight": { - "unique_name": "directionalLight", - "intensity": 0.25, - "direction": [ - -0.2, - -1.0, - -0.3 - ], - "color": [ - 1.0, - 1.0, - 1.0 - ] - }, - "pointLights": [ - { - "unique_name": "pointLight0", - "parent": "light", - "intensity": 7.5, - "position": [ - 0.0, - 1.0, - 0.0 - ], - "color": [ - 1.0, - 1.0, - 1.0 - ] - } - ], - "textures": [ - { - "unique_name": "fallback_normal", - "path": "data/res/models/tex/fallback_normal.png" - } - ], - "skybox": { - "texturePath": "data/res/textures/skybox/" - } -} \ No newline at end of file diff --git a/data/screens.json b/data/screens.json deleted file mode 100644 index 91a5cee..0000000 --- a/data/screens.json +++ /dev/null @@ -1,57 +0,0 @@ -{ - "loadingScreen": [ - { - "unique_name": "background", - "position": [ - 0.0, - 0.0 - ], - "dimensions": [ - 1.0, - 1.0 - ], - "texture": "data/res/textures/loading.png" - } - ], - "mainMenuScreen": [ - { - "unique_name": "play", - "position": [ - 0.4, - 0.5 - ], - "dimensions": [ - 0.25, - 0.1 - ], - "texture": "data/res/textures/play.png", - "callbackId": 1 - }, - { - "unique_name": "exit", - "position": [ - 0.4, - 0.2 - ], - "dimensions": [ - 0.25, - 0.10 - ], - "texture": "data/res/textures/exit.png", - "callbackId": 2 - }, - { - "unique_name": "background", - "position": [ - 0.0, - 0.0 - ], - "dimensions": [ - 1.0, - 1.0 - ], - "texture": "data/res/textures/mainMenu.png", - "callbackId": 0 - } - ] -} \ No newline at end of file diff --git a/data/shaderPrograms.json b/data/shaderPrograms.json deleted file mode 100644 index 7fbab54..0000000 --- a/data/shaderPrograms.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "shaderPrograms": [ - { - "unique_name": "defaultProgram", - "vertexPath": "data/shaders/basic.vert", - "fragmentPath": "data/shaders/basic.frag" - }, - { - "unique_name": "lightProgram", - "vertexPath": "data/shaders/light.vert", - "fragmentPath": "data/shaders/light.frag" - }, - { - "unique_name": "skyboxProgram", - "vertexPath": "data/shaders/skybox.vert", - "fragmentPath": "data/shaders/skybox.frag" - }, - { - "unique_name": "postProcessingProgram", - "vertexPath": "data/shaders/postprocessing.vert", - "fragmentPath": "data/shaders/postprocessing.frag" - }, - { - "unique_name": "menuProgram", - "vertexPath": "data/shaders/menu.vert", - "fragmentPath": "data/shaders/menu.frag", - "geometryPath": "data/shaders/menu.geom" - }, - { - "unique_name": "directionalShadowDepthProgram", - "vertexPath": "data/shaders/directionalShadowDepth.vert", - "fragmentPath": "data/shaders/directionalShadowDepth.frag" - }, - { - "unique_name": "pointShadowDepthProgram", - "vertexPath": "data/shaders/pointShadowDepth.vert", - "fragmentPath": "data/shaders/pointShadowDepth.frag", - "geometryPath": "data/shaders/pointShadowDepth.geom" - } - ] -} \ No newline at end of file diff --git a/data/shaders/menu.frag b/data/shaders/menu.frag deleted file mode 100644 index 08b96d9..0000000 --- a/data/shaders/menu.frag +++ /dev/null @@ -1,16 +0,0 @@ -#version 330 core - -layout(location = 0) out vec4 f_color; - -in vec2 v_texCoord; - -struct Material -{ - sampler2D texture_diffuse0; -}; -uniform Material u_material; - -void main() -{ - f_color = texture(u_material.texture_diffuse0, v_texCoord); -} diff --git a/data/shaders/menu.geom b/data/shaders/menu.geom deleted file mode 100644 index b0cecc4..0000000 --- a/data/shaders/menu.geom +++ /dev/null @@ -1,41 +0,0 @@ -#version 330 core - -layout(points) in; -layout(triangle_strip, max_vertices = 4) out; - -struct WidgetData -{ - vec2 position; - vec2 dimensions; -}; -uniform WidgetData u_widgetData; - -out vec2 v_texCoord; - -void main() -{ - float glWidth = u_widgetData.dimensions.x * 2.0f; - float glHeight = u_widgetData.dimensions.y * 2.0f; - - vec4 offset = vec4(0.0, 0.0, 0.0, 0.0); - gl_Position = gl_in[0].gl_Position + offset; - v_texCoord = vec2(0.0, 0.0); - EmitVertex(); - - offset = vec4(glWidth, 0.0, 0.0, 0.0); - gl_Position = gl_in[0].gl_Position + offset; - v_texCoord = vec2(1.0, 0.0); - EmitVertex(); - - offset = vec4(0.0, glHeight, 0.0, 0.0); - gl_Position = gl_in[0].gl_Position + offset; - v_texCoord = vec2(0.0, 1.0); - EmitVertex(); - - offset = vec4(glWidth, glHeight, 0.0, 0.0); - gl_Position = gl_in[0].gl_Position + offset; - v_texCoord = vec2(1.0, 1.0); - EmitVertex(); - - EndPrimitive(); -} \ No newline at end of file diff --git a/data/shaders/menu.vert b/data/shaders/menu.vert deleted file mode 100644 index 58eb2d3..0000000 --- a/data/shaders/menu.vert +++ /dev/null @@ -1,16 +0,0 @@ -#version 330 core - -struct WidgetData -{ - vec2 position; - vec2 dimensions; -}; -uniform WidgetData u_widgetData; - -void main() -{ - float glPosX = u_widgetData.position.x * 2.0f - 1.0f; - float glPosY = u_widgetData.position.y * 2.0f - 1.0f; - - gl_Position = vec4(glPosX, glPosY, 0, 1); -} diff --git a/lib/entt b/lib/entt new file mode 160000 index 0000000..de0e586 --- /dev/null +++ b/lib/entt @@ -0,0 +1 @@ +Subproject commit de0e5862dd02fa669325a0a06b7936af4d2a841d diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0e92faf..f230690 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,21 +11,17 @@ add_executable(Fall-Fever Light.cpp Scene.cpp FrameBuffer.cpp - Widget.cpp - Screen.cpp - Menu.cpp - JsonParser.cpp Helper.cpp - imgui/EntityWindow.cpp - imgui/GeneralInfoWindow.cpp - imgui/Handler.cpp - imgui/Window.cpp - util/Log.cpp resources/Resource.cpp resources/ResourceHandler.cpp resources/Texture.cpp resources/Model.cpp resources/CubeMap.cpp + util/Log.cpp + # imgui/EntityWindow.cpp + # imgui/GeneralInfoWindow.cpp + # imgui/Handler.cpp + # imgui/Window.cpp ) target_link_libraries( diff --git a/src/Controller.cpp b/src/Controller.cpp index d9660db..05cbfec 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -1,45 +1,41 @@ -#include - -#include - -#include -#include - -#include - -#ifdef _DEBUG -#include "imgui/EntityWindow.h" -#include "imgui/GeneralInfoWindow.h" -#include "imgui/Handler.h" -#endif - -#include "Camera.h" #include "Controller.h" +#include "Camera.h" #include "Entity.h" #include "EventHandler.h" #include "Helper.h" -#include "JsonParser.h" #include "Light.h" -#include "Menu.h" #include "Scene.h" -#include "Screen.h" #include "ShaderProgram.h" #include "VertexArray.h" -#include "Widget.h" #include "Window.h" #include "resources/Model.h" #include "resources/ResourceHandler.h" #include "resources/Texture.h" #include "util/Log.h" +#include +#include +#include +#include +#include + Controller::Controller() : m_gameWindow(std::unique_ptr(new Window)) { m_gameEventHandler = new EventHandler(m_gameWindow->getGLFWwindow()); m_camera = new Camera(90.0f, m_gameWindow->getWindowAspectRatio()); - JsonParser shaderParser("data/shaderPrograms.json"); - auto shaderProgramPrototypes = shaderParser.getShaderProgramPrototypes(); + std::array shaderProgramPrototypes{ + ShaderProgram::Prototype{"defaultProgram", "data/shaders/basic.vert", "data/shaders/basic.frag", ""}, + ShaderProgram::Prototype{"lightProgram", "data/shaders/light.vert", "data/shaders/light.frag", ""}, + ShaderProgram::Prototype{"skyboxProgram", "data/shaders/skybox.vert", "data/shaders/skybox.frag", ""}, + ShaderProgram::Prototype{"postProcessingProgram", "data/shaders/postprocessing.vert", + "data/shaders/postprocessing.frag", ""}, + ShaderProgram::Prototype{"directionalShadowDepthProgram", "data/shaders/directionalShadowDepth.vert", + "data/shaders/directionalShadowDepth.frag", ""}, + ShaderProgram::Prototype{"pointShadowDepthProgram", "data/shaders/pointShadowDepth.vert", + "data/shaders/pointShadowDepth.frag", "data/shaders/pointShadowDepth.geom"}, + }; for (auto &prototype : shaderProgramPrototypes) { m_shaderPrograms.push_back(new ShaderProgram(prototype)); @@ -49,21 +45,7 @@ Controller::Controller() : m_gameWindow(std::unique_ptr(new Window)) m_postProcessFrameBuffer = new FrameBuffer(m_gameWindow->getWindowWidth(), m_gameWindow->getWindowHeight(), getShaderProgramByName("postProcessingProgram")); - m_menu = new Menu(m_postProcessFrameBuffer, getShaderProgramByName("menuProgram")); - - // Show loading screen... - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - m_menu->showScreenByName("loadingScreen"); - glfwSwapBuffers(m_gameWindow->getGLFWwindow()); - - // Show main menu when loading is finished... - m_menu->showScreenByName("mainMenuScreen"); - m_scene = new Scene(m_shaderPrograms); - -#ifdef _DEBUG - m_imguiHandler = std::unique_ptr(new Imgui::Handler(m_gameWindow->getGLFWwindow())); -#endif } Controller::~Controller() @@ -74,17 +56,10 @@ Controller::~Controller() delete m_scene; delete m_camera; - delete m_menu; delete m_postProcessFrameBuffer; delete m_gameEventHandler; } -Controller &Controller::instance() -{ - static Controller s_instance; - return s_instance; -} - void Controller::run() { updateExposure(getShaderProgramByName("postProcessingProgram")); @@ -96,19 +71,9 @@ void Controller::run() m_camera->translate(glm::vec3(0.0f, 1.5f, 5.0f)); - // imgui stuff - bool rotateLightSource = false, rotateEntity = false, drawShadows = false; - glm::vec3 lightColor = glm::vec3(1.f); - float intensity = 7.5f; -#ifdef _DEBUG - std::shared_ptr generalWindow = std::make_shared( - this, m_scene, getShaderProgramByName("postProcessingProgram"), &rotateEntity, &drawShadows, &rotateLightSource, - &lightColor, &m_exposure, &intensity); - m_imguiHandler->addImguiWindow(generalWindow); - - // std::shared_ptr entityWindow = std::make_shared(m_scene->getEntities()); - // m_imguiHandler->addImguiWindow(entityWindow); -#endif + bool drawShadows = false; + float intensity = 7.5; + glm::vec3 lightColor{1., 1., 1.}; // This is the game loop while (!glfwWindowShouldClose(m_gameWindow->getGLFWwindow())) { @@ -117,20 +82,10 @@ void Controller::run() limit_framerate(); // --- Update game --- - - if (rotateLightSource) { - float radius = 4.0; - glm::vec3 newPos = glm::vec3(-cos(glfwGetTime() * 0.5), 0.5f, sin(glfwGetTime() * 0.5)) * radius; - m_scene->getEntityByName("light")->setPosition(newPos); - } - if (rotateEntity) { - m_scene->getEntityById(0)->rotate(glm::vec3(0.0f, 1.0f, 0.0f), -0.2f * m_deltaTime); - } - m_scene->updatePointLight(0, true, m_scene->getEntityByName("light")->getPosition(), lightColor, intensity); m_scene->updateDirectionalLight(true, m_scene->getDirectionalLight()->getDirection(), lightColor); getShaderProgramByName("lightProgram")->bind(); - getShaderProgramByName("lightProgram")->setUniform("v_lightColor", lightColor * 100.0f); + getShaderProgramByName("lightProgram")->setUniform("v_lightColor", glm::vec3{1., 1., 1.} * 100.0f); getShaderProgramByName("lightProgram")->unbind(); // --- Render and buffer swap --- @@ -148,26 +103,18 @@ void Controller::run() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - auto activeScreen = m_menu->getActiveScreen(); - if (activeScreen) { - activeScreen->draw(); - } else { - m_postProcessFrameBuffer->bind(); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + m_postProcessFrameBuffer->bind(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - m_camera->lookForward(); - m_camera->updateVPM(); + m_camera->lookForward(); + m_camera->updateVPM(); - m_scene->getSkybox()->draw(m_camera->getView(), m_camera->getProj()); - m_scene->draw(m_camera->getViewProj(), m_camera->getPosition()); + m_scene->getSkybox()->draw(m_camera->getView(), m_camera->getProj()); + m_scene->draw(m_camera->getViewProj(), m_camera->getPosition()); - m_postProcessFrameBuffer->unbind(); - m_postProcessFrameBuffer->drawOnEntireScreen(); + m_postProcessFrameBuffer->unbind(); + m_postProcessFrameBuffer->drawOnEntireScreen(); -#ifdef _DEBUG - m_imguiHandler->renderWindows(); -#endif - } glfwSwapBuffers(m_gameWindow->getGLFWwindow()); // Update window size @@ -184,12 +131,7 @@ void Controller::run() m_camera->updateDirectionFromMouseInput(m_gameEventHandler->getCameraMouseActionMap()); } - m_menu->writeWindowActions(m_gameEventHandler->getWindowActionMap()); m_gameWindow->handleWindowActionMap(m_gameEventHandler->getWindowActionMap()); - - // Handle widget pressed event only when a screen is currently active - if (m_menu->getActiveScreen()) - m_menu->handleMouseButtonActionMap(m_gameEventHandler->getMouseButtonActionMap(), m_gameWindow.get()); } } diff --git a/src/Controller.h b/src/Controller.h index 1a9942d..5b98b66 100644 --- a/src/Controller.h +++ b/src/Controller.h @@ -19,22 +19,20 @@ class Handler; class Controller { public: + Controller(); ~Controller(); - static Controller &instance(); - void run(); void setMaxFps(uint16_t fps); + // TODO remove... ShaderProgram *getShaderProgramByName(const std::string &name); static ShaderProgram *getShaderProgramByName(const std::string &name, std::vector shaderPrograms); void updateExposure(ShaderProgram *shaderProgram); private: - Controller(); - void limit_framerate(); void updateWindowDimensions(); @@ -48,14 +46,9 @@ private: Scene *m_scene; Camera *m_camera; - Menu *m_menu; std::vector m_shaderPrograms; -#ifdef _DEBUG - std::unique_ptr m_imguiHandler; -#endif - FrameBuffer *m_postProcessFrameBuffer; uint16_t m_MAX_FPS = 60; diff --git a/src/JsonParser.h b/src/JsonParser.h deleted file mode 100644 index 9a1ed66..0000000 --- a/src/JsonParser.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "Entity.h" -#include "Light.h" -#include "Screen.h" -#include "ShaderProgram.h" -#include "Widget.h" -#include "resources/Model.h" - -class Screen; -class ShaderProgram; -class FrameBuffer; - -class JsonParser -{ -public: - JsonParser(const std::string &jsonFilepath); - ~JsonParser(); - - std::vector getModelDescriptors() const; - std::vector getEntityPrototypes() const; - std::vector> getLightPrototypes() const; - std::vector getScreenPrototypes() const; - Skybox::Prototype getSkyboxPrototype() const; - - std::vector getShaderProgramPrototypes(); - -private: - std::vector getWidgetPrototypesFromScreen(const Json::Value &screenJson) const; - - Json::Value m_root; - Json::CharReaderBuilder m_rbuilder; -}; diff --git a/src/Menu.cpp b/src/Menu.cpp deleted file mode 100644 index afdcbce..0000000 --- a/src/Menu.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include "Menu.h" -#include "FrameBuffer.h" -#include "Helper.h" -#include "JsonParser.h" -#include "Screen.h" -#include "ShaderProgram.h" -#include "Widget.h" -#include "Window.h" -#include "definitions/eventActions.h" -#include "util/Log.h" - -Menu::Menu(FrameBuffer *p_framebuffer, ShaderProgram *p_shaderProgram) - : m_frameBuffer(p_framebuffer), m_shaderProgram(p_shaderProgram) -{ - JsonParser screenParser("data/screens.json"); - std::vector screenPrototypes = screenParser.getScreenPrototypes(); - - for (auto &prototype : screenPrototypes) { - auto currentScreen = new Screen(prototype, p_framebuffer, p_shaderProgram); - m_screens.push_back(currentScreen); - } -} - -Menu::~Menu() -{ - // Iterate over screens delete all items - for (auto it = m_screens.begin(); it != m_screens.end(); it++) { - delete (*it); - } -} - -Screen *Menu::getScreenByName(const std::string &name) const -{ - if (m_screenNameCache.find(name) != m_screenNameCache.end()) - return m_screenNameCache[name]; - - for (auto it : m_screens) { - if (it->getUniqueName() == name) { - m_screenNameCache[name] = it; - return it; - } - } - - return nullptr; -} - -Screen *Menu::getActiveScreen() const -{ - return m_activeScreen; -} - -void Menu::showScreenByName(const std::string &name) -{ - Screen *screen = getScreenByName(name); - - if (!screen) - return; - - screen->draw(); - m_activeScreen = screen; -} - -void Menu::resetActiveScreen() -{ - m_activeScreen = nullptr; -} - -void Menu::handleMouseButtonActionMap(const MouseButtonActionMap &mouseButtonActionMap, Window *window) -{ - if (mouseButtonActionMap.at(MouseButtonAction::LeftClicked)) { - auto widgets = m_activeScreen->getWidgets(); - for (auto it = widgets.begin(); it != widgets.end(); it++) { - if ((*it)->isHovered(window)) { - if ((*it)->getCallbackId() == 1) - resetActiveScreen(); - if ((*it)->getCallbackId() == 2) - m_shouldExit = true; - } - } - } -} - -void Menu::writeWindowActions(WindowActionMap &windowActionMap) -{ - if (m_shouldExit) - windowActionMap[WindowAction::WindowShouldClose] = true; -} - -void Menu::onPlayPressed() -{} - -void Menu::onExitPressed() -{} diff --git a/src/Menu.h b/src/Menu.h deleted file mode 100644 index 518212d..0000000 --- a/src/Menu.h +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once - -#include "definitions/eventActions.h" - -#include -#include -#include - -class ShaderProgram; -class FrameBuffer; -class Screen; -class Window; - -class Menu -{ -public: - Menu(FrameBuffer *p_framebuffer, ShaderProgram *p_shaderProgram); - ~Menu(); - - Screen *getScreenByName(const std::string &name) const; - void showScreenByName(const std::string &name); - - Screen *getActiveScreen() const; - void writeWindowActions(WindowActionMap &windowActionMap); - - void resetActiveScreen(); - - void handleMouseButtonActionMap(const MouseButtonActionMap &mouseButtonActionMap, Window *window); - - void onPlayPressed(); - void onExitPressed(); - -private: - FrameBuffer *m_frameBuffer; - ShaderProgram *m_shaderProgram; - - bool m_shouldExit = false; - - std::vector m_screens; - Screen *m_activeScreen; - /*Screen *loadingScreen; - Screen *mainMenuScreen; - Screen *optionMenuScreen; - Screen *pauseMenuScreen;*/ - - mutable std::unordered_map m_screenNameCache; -}; diff --git a/src/Scene.cpp b/src/Scene.cpp index 0a8968a..6ba92b9 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -3,7 +3,6 @@ #include "Controller.h" #include "Entity.h" #include "FrameBuffer.h" -#include "JsonParser.h" #include "Light.h" #include "ShaderProgram.h" #include "resources/Model.h" @@ -12,6 +11,7 @@ #include "util/Log.h" #include +#include #include Scene::Scene(std::vector shaderPrograms) @@ -29,9 +29,13 @@ Scene::Scene(std::vector shaderPrograms) m_shaderProgram->setUniform("u_material.shininess", 100.0f); m_shaderProgram->unbind(); - JsonParser modelParser("data/scene.json"); - - std::vector modelDescriptors = modelParser.getModelDescriptors(); + std::array modelDescriptors{ + ModelDescriptor{"fallback", "data/res/models/fallback.ffo"}, + ModelDescriptor{"backpack", "data/res/models/backpack.ffo"}, + ModelDescriptor{"ground", "data/res/models/wood_floor.ffo"}, + ModelDescriptor{"cube", "data/res/models/cube.ffo"}, + ModelDescriptor{"container", "data/res/models/container.ffo"}, + }; { std::vector> futures; @@ -53,7 +57,7 @@ Scene::Scene(std::vector shaderPrograms) } // TODO: use geometry shader instead of model and load skybox before models. - Skybox::Prototype skyboxPrototype = modelParser.getSkyboxPrototype(); + Skybox::Prototype skyboxPrototype{"data/res/textures/skybox/"}; m_skybox = new Skybox(skyboxPrototype, std::static_pointer_cast(ResourceHandler::instance().resource("cube")).get(), Controller::getShaderProgramByName("skyboxProgram", shaderPrograms)); @@ -61,7 +65,13 @@ Scene::Scene(std::vector shaderPrograms) Log::logger().info("Loaded skybox: {}", skyboxPrototype.texturePath); m_skybox->initializeOnGPU(); - std::vector entityPrototypes = modelParser.getEntityPrototypes(); + std::array entityPrototypes{ + ModelEntity::Prototype{"backpack", "", {0., 1., 0.}, {}, 0.6, "backpack", "defaultProgram"}, + ModelEntity::Prototype{ + "container", "backpack", {10., 1., 0.}, {45., 45., 45.}, 1., "container", "defaultProgram"}, + ModelEntity::Prototype{"ground", "", {}, {}, 1., "ground", "defaultProgram"}, + ModelEntity::Prototype{"light", "", {}, {}, 1., "cube", "lightProgram"}, + }; { for (auto &prototype : entityPrototypes) { @@ -100,8 +110,13 @@ Scene::Scene(std::vector shaderPrograms) } } - JsonParser lightParser("data/scene.json"); - auto lightPrototypes = lightParser.getLightPrototypes(); + // Order important + std::array, 2> lightPrototypes{ + std::unique_ptr( + new DirectionalLight::Prototype{"directionalLight", {-0.2, -1.0, -0.3}, {1., 1., 1.}, 0.25}), + std::unique_ptr( + new PointLight::Prototype{"pointLight0", "light", {0., 1., 0.}, {1., 1., 1.}, 7.5}), + }; std::vector lights; { @@ -119,10 +134,13 @@ Scene::Scene(std::vector shaderPrograms) if (parent) { parent->addChild(currentLight); currentLight->setParent(parent); + } else { + Log::logger().warn("Could not find entity \"{}\"", pointPrototype->parent); } } } lights.push_back(currentLight); + Log::logger().info("Loaded light \"{}\"", prototype->name); } } m_lights = lights; diff --git a/src/Screen.cpp b/src/Screen.cpp deleted file mode 100644 index 91e19c6..0000000 --- a/src/Screen.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "Screen.h" -#include "FrameBuffer.h" -#include "Helper.h" -#include "Menu.h" -#include "ShaderProgram.h" -// #include "Texture.h" -#include "Widget.h" -#include "resources/ResourceHandler.h" - -uint32_t Screen::s_idCounter = 0; - -Screen::Screen(Prototype prototype, FrameBuffer *framebuffer, ShaderProgram *shaderProgram) - : m_id(s_idCounter++), m_uniqueName(prototype.name), m_frameBuffer(framebuffer), m_shaderProgram(shaderProgram) -{ - for (auto &prototype : prototype.widgetPrototypes) { - auto texturePrototype = prototype.texturePrototype; - ResourceId textureId = ResourceHandler::instance().registerResource(texturePrototype); - - Widget *currentWidget = new Widget(prototype, textureId); - m_widgets.push_back(currentWidget); - } -} - -Screen::~Screen() -{ - // Iterate over Widgets and Textures to delete all of them - for (auto it = m_widgets.begin(); it != m_widgets.end(); it++) { - delete *it; - } -} - -const std::string &Screen::getUniqueName() const -{ - return m_uniqueName; -} - -std::vector Screen::getWidgets() const -{ - return m_widgets; -} - -Widget *Screen::getWidgetByName(const std::string &name) const -{ - if (m_widgetNameCache.find(name) != m_widgetNameCache.end()) - return m_widgetNameCache[name]; - - for (auto it : m_widgets) { - if (it->getUniqueName() == name) { - m_widgetNameCache[name] = it; - return it; - } - } - return nullptr; -} - -void Screen::addWidget(Widget *widget) -{ - m_widgets.push_back(widget); -} - -void Screen::draw() const -{ - m_frameBuffer->setExposureCorrection(false); - m_frameBuffer->bind(); - - for (auto it = m_widgets.begin(); it != m_widgets.end(); it++) { - (*it)->draw(m_shaderProgram); - } - - m_frameBuffer->unbind(); - m_frameBuffer->drawOnEntireScreen(); - m_frameBuffer->setExposureCorrection(true); -} diff --git a/src/Screen.h b/src/Screen.h deleted file mode 100644 index 64374a4..0000000 --- a/src/Screen.h +++ /dev/null @@ -1,45 +0,0 @@ -#pragma once - -#include "Widget.h" -#include "resources/Texture.h" - -#include -#include -#include - -class Menu; -class FrameBuffer; -class ShaderProgram; - -class Screen -{ -public: - struct Prototype - { - std::string name; - std::vector widgetPrototypes; - }; - - Screen(Prototype prototype, 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; - -private: - uint32_t m_id; - static uint32_t s_idCounter; - std::string m_uniqueName; - - FrameBuffer *m_frameBuffer; - ShaderProgram *m_shaderProgram; - - std::vector m_textures; - std::vector m_widgets; - - mutable std::unordered_map m_widgetNameCache; -}; diff --git a/src/Texture_old.cpp b/src/Texture_old.cpp deleted file mode 100644 index dd98385..0000000 --- a/src/Texture_old.cpp +++ /dev/null @@ -1,145 +0,0 @@ -#include "ShaderProgram.h" -#include "Texture.h" -#include "util/Log.h" - -CubeMap::CubeMap(const std::string &texturePseudoPath) -{ - // Reserve space in vector so that elements can be accessed explicitly. - m_texturePaths.resize(static_cast(cubeMapFaces::CUBEMAP_FACES_NUM_ITEMS)); - m_textureBuffers.resize(static_cast(cubeMapFaces::CUBEMAP_FACES_NUM_ITEMS)); - m_numComponents.resize(static_cast(cubeMapFaces::CUBEMAP_FACES_NUM_ITEMS)); - - fillTexturePathVector(texturePseudoPath); - - stbi_set_flip_vertically_on_load(0); - - glGenTextures(1, &m_textureId); - glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureId); - - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); - - int i = 0; - for (auto &path : m_texturePaths) { - - int32_t numComponents; - auto textureBuffer = stbi_load(path.c_str(), &m_textureWidth, &m_textureHeight, &numComponents, 0); - - if (!textureBuffer) { - Log::logger().warn("CubeMap texture {} could not be loaded", path); - return; - } - - m_textureBuffers[i] = textureBuffer; - m_numComponents[i] = numComponents; - i++; - } - - glBindTexture(GL_TEXTURE_CUBE_MAP, 0); -} - -CubeMap::CubeMap(int RESOLUTION) : m_textureWidth(RESOLUTION), m_textureHeight(RESOLUTION) -{ - - glGenTextures(1, &m_textureId); - glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureId); - - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); - - 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); - } - - glBindTexture(GL_TEXTURE_CUBE_MAP, 0); -} - -CubeMap::~CubeMap() -{ - // glDeleteTextures(1, &m_textureId); -} - -void CubeMap::initializeOnGPU() -{ - if (m_isInitialized) - return; - - m_isInitialized = true; - - glGenTextures(1, &m_textureId); - glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureId); - - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); - - int i = 0; - for (auto &textureBuffer : m_textureBuffers) { - GLenum internalFormat; - GLenum dataFormat; - - switch (m_numComponents[i]) { - case 1: - internalFormat = GL_RED; - dataFormat = GL_RED; - break; - case 3: - internalFormat = GL_SRGB8; - dataFormat = GL_RGB; - break; - case 4: - internalFormat = GL_SRGB8_ALPHA8; - dataFormat = GL_RGBA; - break; - } - - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalFormat, m_textureWidth, m_textureHeight, 0, - dataFormat, GL_UNSIGNED_BYTE, textureBuffer); - - stbi_image_free(textureBuffer); - i++; - } - - glBindTexture(GL_TEXTURE_CUBE_MAP, 0); -} - -void CubeMap::bind(ShaderProgram *shaderProgram) -{ - std::string uniformName = "u_skybox"; - - shaderProgram->setUniform(uniformName.c_str(), 0); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureId); -} - -void CubeMap::unbind() -{ - glBindTexture(GL_TEXTURE_CUBE_MAP, 0); -} - -void CubeMap::fillTexturePathVector(const std::string &texturePseudoPath) -{ - for (unsigned int i = 0; i < static_cast(cubeMapFaces::CUBEMAP_FACES_NUM_ITEMS); i++) { - m_texturePaths[static_cast(cubeMapFaces::cm_front)] = texturePseudoPath + "front.png"; - m_texturePaths[static_cast(cubeMapFaces::cm_back)] = texturePseudoPath + "back.png"; - m_texturePaths[static_cast(cubeMapFaces::cm_top)] = texturePseudoPath + "top.png"; - m_texturePaths[static_cast(cubeMapFaces::cm_bottom)] = texturePseudoPath + "bottom.png"; - m_texturePaths[static_cast(cubeMapFaces::cm_left)] = texturePseudoPath + "left.png"; - m_texturePaths[static_cast(cubeMapFaces::cm_right)] = texturePseudoPath + "right.png"; - } -} - -GLuint CubeMap::getTextureId() const -{ - return m_textureId; -} diff --git a/src/Texture_old.h b/src/Texture_old.h deleted file mode 100644 index 2e8b80f..0000000 --- a/src/Texture_old.h +++ /dev/null @@ -1,54 +0,0 @@ -#pragma once - -#include "definitions/models.h" -#include "resources/Texture.h" - -#include -#include -#include -#include -#include - -class ShaderProgram; - -// Order is important! -enum class cubeMapFaces -{ - cm_right, - cm_left, - cm_top, - cm_bottom, - cm_back, - cm_front, - CUBEMAP_FACES_NUM_ITEMS -}; - -class CubeMap -{ -public: - CubeMap(const std::string &texturePseudoPath); - CubeMap(int RESOLUTION); - - void initializeOnGPU(); - - ~CubeMap(); - - void bind(ShaderProgram *shaderProgram); - void unbind(); - - GLuint getTextureId() const; - -private: - void fillTexturePathVector(const std::string &texturePseudoPath); - - bool m_isInitialized = false; - - std::vector m_texturePaths; - std::vector m_textureBuffers; - std::vector m_numComponents; - - GLuint m_textureId; - - int32_t m_textureWidth; - int32_t m_textureHeight; -}; diff --git a/src/Widget.cpp b/src/Widget.cpp deleted file mode 100644 index 54a1a7e..0000000 --- a/src/Widget.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "Widget.h" -#include "Menu.h" -#include "Mesh.h" -#include "ShaderProgram.h" -#include "VertexArray.h" -#include "Window.h" -#include "resources/ResourceHandler.h" - -#include - -Widget::Widget(Prototype prototype, ResourceId texture) - : m_position(prototype.position), m_dimensions(prototype.dimensions), m_uniqueName(prototype.name), - m_callbackId(prototype.callBackId) -{ - m_widgetTextures.push_back(texture); -} - -std::string Widget::getUniqueName() -{ - return m_uniqueName; -} - -uint16_t Widget::getCallbackId() -{ - return m_callbackId; -} - -void Widget::draw(ShaderProgram *shaderProgram) -{ - shaderProgram->bind(); - shaderProgram->setUniform("u_widgetData.position", m_position); - shaderProgram->setUniform("u_widgetData.dimensions", m_dimensions); - - GLint wireframe; - glGetIntegerv(GL_POLYGON_MODE, &wireframe); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - - glActiveTexture(GL_TEXTURE0); - auto texture = std::static_pointer_cast(ResourceHandler::instance().resource(m_widgetTextures[0])); - glBindTexture(GL_TEXTURE_2D, texture->glId()); - - GLint location = glGetUniformLocation(shaderProgram->getShaderProgramId(), "u_texture"); - glUniform1i(location, 0); - - // A VAO is necessary although no data is stored in it - GLuint temp_vao; - glGenVertexArrays(1, &temp_vao); - glBindVertexArray(temp_vao); - glDrawArrays(GL_POINTS, 0, 3); - glBindVertexArray(0); - - glPolygonMode(GL_FRONT_AND_BACK, wireframe); - - shaderProgram->unbind(); -} - -bool Widget::isHovered(Window *window) -{ - double xpos, ypos, width, height; - glfwGetCursorPos(window->getGLFWwindow(), &xpos, &ypos); - width = window->getWindowWidth(); - height = window->getWindowHeight(); - - double xrel = xpos / width; - double yrel = -ypos / height + 1; - - bool isHovered = false; - if (xrel >= m_position.x && xrel <= m_position.x + m_dimensions.x && yrel >= m_position.y && - yrel <= m_position.y + m_dimensions.x) - isHovered = true; - - return isHovered; -} diff --git a/src/Widget.h b/src/Widget.h deleted file mode 100644 index e71ae06..0000000 --- a/src/Widget.h +++ /dev/null @@ -1,45 +0,0 @@ -#pragma once - -#include "definitions/models.h" -#include "resources/Texture.h" - -#include -#include -#include - -class Menu; -class ShaderProgram; -class Window; -class Mesh; - -class Widget -{ -public: - struct Prototype - { - std::string name; - glm::vec2 position; - glm::vec2 dimensions; - TextureDescriptor texturePrototype; - uint16_t callBackId; // TODO: will be removed... - }; - - Widget(Prototype prototype, ResourceId texture); - - void draw(ShaderProgram *shaderProgram); - - std::string getUniqueName(); - uint16_t getCallbackId(); - - bool isHovered(Window *window); - -private: - glm::vec2 m_position; - glm::vec2 m_dimensions; - - std::string m_uniqueName; - - uint16_t m_callbackId; - - std::vector m_widgetTextures; -}; diff --git a/src/main.cpp b/src/main.cpp index 297a6a6..bc6a54c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,7 +13,7 @@ int main(int argc, char **argv) #endif // Create controller - Controller &controller = Controller::instance(); + Controller controller; const char *fps_env = std::getenv("MAXFPS"); if (fps_env) { diff --git a/src/resources/Model.h b/src/resources/Model.h index 261184a..0ad9b80 100644 --- a/src/resources/Model.h +++ b/src/resources/Model.h @@ -8,8 +8,8 @@ struct ModelDescriptor { - std::string path; std::string name; + std::string path; }; class Model : public Resource, public NamedResource diff --git a/src/util/Log.cpp b/src/util/Log.cpp index 8271c39..34a3181 100644 --- a/src/util/Log.cpp +++ b/src/util/Log.cpp @@ -7,7 +7,7 @@ Log Log::s_instance; Log::Log() { m_logger = spdlog::stdout_color_mt("Core"); - // m_logger->set_pattern(""); + m_logger->set_pattern("[%H:%M:%S.%e] [%n] [%^%l%$] %v"); #ifdef _DEBUG m_logger->set_level(spdlog::level::debug);