Refactoring

This commit is contained in:
2022-10-09 17:34:23 +02:00
parent 028829a291
commit 41d09c2b5f
14 changed files with 53 additions and 88 deletions

View File

@@ -7,6 +7,7 @@ AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakTemplateDeclarations: 'true'
BreakBeforeBraces: Mozilla
ColumnLimit: '120'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
IndentWidth: '4'
PointerAlignment: Right

3
.gitmodules vendored
View File

@@ -7,3 +7,6 @@
[submodule "lib/entt"]
path = lib/entt
url = https://github.com/skypjack/entt.git
[submodule "lib/tinygltf"]
path = lib/tinygltf
url = https://github.com/syoyo/tinygltf.git

4
lib/CMakeLists.txt vendored
View File

@@ -1,4 +1,8 @@
option(SPDLOG_NO_EXCEPTIONS "" ON)
set(TINYGLTF_HEADER_ONLY OFF CACHE INTERNAL "" FORCE)
set(TINYGLTF_INSTALL OFF CACHE INTERNAL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/glad)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/entt)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tinygltf)

1
lib/tinygltf vendored Submodule

Submodule lib/tinygltf added at 091a1fcc1a

View File

@@ -8,7 +8,7 @@ add_library(fever_engine
Mesh.cpp
Entity.cpp
Light.cpp
Scene.cpp
# Scene.cpp
FrameBuffer.cpp
Helper.cpp
resources/Resource.cpp

View File

@@ -16,41 +16,20 @@
#include <glm/gtc/matrix_transform.hpp>
Controller::Controller()
: m_gameWindow(std::make_shared<Window>()), m_camera(std::make_shared<Camera>(90., m_gameWindow->aspectRatio()))
: m_gameWindow(std::make_shared<Window>()),
m_camera(std::make_shared<Camera>(90., m_gameWindow->aspectRatio())),
m_postProcessFrameBuffer(m_gameWindow->dimensions().first, m_gameWindow->dimensions().second,
postProcessingProgram)
{
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"},
};
for (auto &prototype : shaderProgramPrototypes) {
m_shaderPrograms.push_back(std::make_shared<ShaderProgram>(prototype));
Log::logger().info("Loaded shaderprogram \"{}\"", prototype.name);
}
auto dimensions = m_gameWindow->dimensions();
m_postProcessFrameBuffer = std::make_shared<FrameBuffer>(dimensions.first, dimensions.second,
getShaderProgramByName("postProcessingProgram").get());
m_scene = std::make_shared<Scene>(m_shaderPrograms);
}
void Controller::run()
{
updateExposure(*getShaderProgramByName("postProcessingProgram"));
auto lightSource = m_scene->getEntityByName("light");
lightSource->setScale(.1F);
lightSource->setRotation(glm::vec3(0.));
lightSource->setPosition(glm::vec3(-2., 1.5, 2.));
updateExposure(postProcessingProgram);
m_camera->translate(glm::vec3(0., 1.5, 5.));
static constexpr float INTENSITY = 7.5;
glm::vec3 lightColor{1., 1., 1.};
Log::logger().info("Startup complete.");
// This is the game loop
while (glfwWindowShouldClose(&m_gameWindow->glfw_window()) == GLFW_FALSE) {
@@ -59,26 +38,21 @@ void Controller::run()
limit_framerate();
// --- Update game ---
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", glm::vec3{1., 1., 1.} * 100.0F);
getShaderProgramByName("lightProgram")->unbind();
lightProgram.bind();
lightProgram.setUniform("v_lightColor", glm::vec3{1., 1., 1.} * 100.0F);
lightProgram.unbind();
// --- Render and buffer swap ---
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_postProcessFrameBuffer->bind();
m_postProcessFrameBuffer.bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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_postProcessFrameBuffer->unbind();
m_postProcessFrameBuffer->drawOnEntireScreen();
m_postProcessFrameBuffer.unbind();
m_postProcessFrameBuffer.drawOnEntireScreen();
glfwSwapBuffers(&m_gameWindow->glfw_window());
@@ -127,7 +101,7 @@ void Controller::update_window_dimensions()
// m_gameEventHandler->setFirstMouseInput(1);
auto dimensions = m_gameWindow->dimensions();
m_postProcessFrameBuffer->changeDimensions(dimensions.first, dimensions.second);
m_postProcessFrameBuffer.changeDimensions(dimensions.first, dimensions.second);
}
void Controller::updateExposure(ShaderProgram &shaderProgram) const
@@ -136,20 +110,3 @@ void Controller::updateExposure(ShaderProgram &shaderProgram) const
shaderProgram.setUniform("u_exposure", m_exposure);
shaderProgram.unbind();
}
std::shared_ptr<ShaderProgram> Controller::getShaderProgramByName(const std::string &name)
{
return getShaderProgramByName(name, m_shaderPrograms);
}
std::shared_ptr<ShaderProgram>
Controller::getShaderProgramByName(const std::string &name, std::vector<std::shared_ptr<ShaderProgram>> shaderPrograms)
{
for (auto program : shaderPrograms) {
if (program->getUniqueName() == name) {
return program;
}
}
Log::logger().critical("Shaderprogram could not be found by name \"{}\"", name);
return {};
}

View File

@@ -1,10 +1,12 @@
#pragma once
#include "FrameBuffer.h"
#include "ShaderProgram.h"
#include <glm/glm.hpp>
#include <memory>
#include <vector>
class ShaderProgram;
class Window;
class Scene;
class Camera;
@@ -17,11 +19,6 @@ public:
void run();
// TODO remove...
std::shared_ptr<ShaderProgram> getShaderProgramByName(const std::string &name);
static std::shared_ptr<ShaderProgram>
getShaderProgramByName(const std::string &name, std::vector<std::shared_ptr<ShaderProgram>> shaderPrograms);
void updateExposure(ShaderProgram &shaderProgram) const;
private:
@@ -29,13 +26,16 @@ private:
void update_window_dimensions();
std::shared_ptr<Window> m_gameWindow;
std::shared_ptr<Scene> m_scene;
std::shared_ptr<Camera> m_camera;
std::vector<std::shared_ptr<ShaderProgram>> m_shaderPrograms;
ShaderProgram defaultProgram{{"defaultProgram", "data/shaders/basic.vert", "data/shaders/basic.frag"}};
ShaderProgram lightProgram{{"lightProgram", "data/shaders/light.vert", "data/shaders/light.frag"}};
ShaderProgram skyboxProgram{{"skyboxProgram", "data/shaders/skybox.vert", "data/shaders/skybox.frag"}};
ShaderProgram postProcessingProgram{
{"postProcessingProgram", "data/shaders/postprocessing.vert", "data/shaders/postprocessing.frag"}};
std::shared_ptr<FrameBuffer> m_postProcessFrameBuffer;
FrameBuffer m_postProcessFrameBuffer;
static constexpr unsigned MAX_FPS = 60;

View File

@@ -26,7 +26,7 @@ GLuint AbstractFrameBuffer::getFBO() const
return m_FBO;
}
FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, ShaderProgram *shaderProgram) : m_shaderProgram(shaderProgram)
FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, ShaderProgram &shaderProgram) : m_shaderProgram(shaderProgram)
{
glGenFramebuffers(1, &m_FBO);
@@ -53,11 +53,11 @@ void FrameBuffer::drawOnEntireScreen() const
glGetIntegerv(GL_POLYGON_MODE, &wireframe);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
m_shaderProgram->bind();
m_shaderProgram.bind();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, getTextureId());
GLint location = glGetUniformLocation(m_shaderProgram->getShaderProgramId(), "u_texture");
GLint location = glGetUniformLocation(m_shaderProgram.getShaderProgramId(), "u_texture");
glUniform1i(location, 0);
// A VAO is necessary although no data is stored in it
@@ -68,7 +68,7 @@ void FrameBuffer::drawOnEntireScreen() const
glBindVertexArray(0);
glPolygonMode(GL_FRONT_AND_BACK, static_cast<GLenum>(wireframe));
m_shaderProgram->unbind();
m_shaderProgram.unbind();
}
void FrameBuffer::changeDimensions(uint32_t width, uint32_t height)
@@ -111,9 +111,9 @@ void FrameBuffer::generateTextures(uint32_t width, uint32_t height)
void FrameBuffer::setExposureCorrection(bool exposureCorrection) const
{
m_shaderProgram->bind();
m_shaderProgram->setUniform("u_exposureCorrection", exposureCorrection);
m_shaderProgram->unbind();
m_shaderProgram.bind();
m_shaderProgram.setUniform("u_exposureCorrection", exposureCorrection);
m_shaderProgram.unbind();
}
AbstractDepthMap::~AbstractDepthMap()

View File

@@ -22,7 +22,7 @@ protected:
class FrameBuffer : public AbstractFrameBuffer
{
public:
FrameBuffer(uint32_t width, uint32_t height, ShaderProgram *shaderProgram);
FrameBuffer(uint32_t width, uint32_t height, ShaderProgram &shaderProgram);
~FrameBuffer();
void drawOnEntireScreen() const;
@@ -39,7 +39,7 @@ private:
GLuint m_colorBuffer;
GLuint m_depthStencilBuffer;
ShaderProgram *m_shaderProgram;
ShaderProgram &m_shaderProgram;
};
class AbstractDepthMap : public AbstractFrameBuffer

View File

@@ -31,6 +31,8 @@ ShaderProgram::ShaderProgram(Prototype prototype) : m_uniqueName(prototype.name)
glDeleteShader(vs);
glDeleteShader(fs);
#endif
Log::logger().info("Loaded shaderprogram \"{}\"", prototype.name);
}
ShaderProgram::~ShaderProgram()

View File

@@ -20,7 +20,7 @@ Window::Window()
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef _DEBUG
#ifndef NDEBUG
glfwSetErrorCallback(glfw_error_callback);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
#else
@@ -55,8 +55,8 @@ Window::Window()
std::terminate();
}
#ifdef _DEBUG
Log::logger().debug("OpenGL version: {}", glGetString(GL_VERSION));
#ifndef NDEBUG
Log::logger().debug("OpenGL version: {}", reinterpret_cast<const char *>(glGetString(GL_VERSION)));
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(Helper::gl_debug_callback, nullptr);
@@ -95,11 +95,8 @@ auto Window::dimensions_changed() const -> bool
{
int new_width{};
int new_height{};
int new_posx{};
int new_posy{};
glfwGetFramebufferSize(m_glfw_window.get(), &new_width, &new_height);
glfwGetWindowPos(m_glfw_window.get(), &new_posx, &new_posy);
return !(static_cast<uint32_t>(new_width) == m_width && static_cast<uint32_t>(new_height) == m_height);
}
@@ -131,7 +128,7 @@ void Window::set_catched_cursor(bool value)
// GLFW error function
void Window::glfw_error_callback(int error, const char *description)
{
Log::logger().warn("GLFW [%d]: %s\n", error, description);
Log::logger().warn("GLFW [{:d}]: {:s}\n", error, description);
}
// This function is called when the window gets resized (currently not used)

View File

@@ -1,15 +1,15 @@
#include "Controller.h"
#include <GLFW/glfw3.h>
#include <iostream>
#include "Controller.h"
auto main(int argc, char **argv) -> int
{
// Suppress warning about unused variable
(void)argc;
(void)argv;
#ifdef _DEBUG
#ifndef NDEBUG
std::cout << "[Debug Mode]" << std::endl;
#endif

View File

@@ -4,12 +4,12 @@
Log Log::s_instance;
Log::Log()
Log::Log() noexcept
{
m_logger = spdlog::stdout_color_mt("Core");
m_logger->set_pattern("[%H:%M:%S.%e] [%n] [%^%l%$] %v");
#ifdef _DEBUG
#ifndef NDEBUG
m_logger->set_level(spdlog::level::debug);
#else
m_logger->set_level(spdlog::level::warn);

View File

@@ -8,7 +8,7 @@ public:
static spdlog::logger &logger();
private:
Log();
Log() noexcept;
static Log s_instance;