From 028829a29148f57713e69e207ad19c5eee3091dc Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Sat, 8 Oct 2022 19:05:31 +0200 Subject: [PATCH] CMake refactor, fix warnings --- .gitignore | 1 + CMakeLists.txt | 9 +- CMakePresets.json | 57 ++++++++ Fall-Fever.kdev4 | 4 - PreLoad.cmake | 7 - data/shaders/directionalShadowDepth.frag | 7 - data/shaders/directionalShadowDepth.vert | 11 -- data/shaders/pointShadowDepth.frag | 17 --- data/shaders/pointShadowDepth.geom | 25 ---- data/shaders/pointShadowDepth.vert | 10 -- src/Camera.cpp | 11 +- src/Controller.cpp | 17 +-- src/Entity.h | 13 +- src/FrameBuffer.cpp | 17 ++- src/Light.h | 14 +- src/Mesh.cpp | 22 +-- src/Mesh.h | 4 +- src/Scene.cpp | 126 +----------------- src/Scene.h | 19 --- src/ShaderProgram.cpp | 18 +-- src/ShaderProgram.h | 1 - src/VertexArray.cpp | 18 --- src/VertexArray.h | 4 - src/Window.cpp | 35 +++-- src/Window.h | 8 +- src/resources/AbstractTexture.h | 6 +- src/resources/CubeMap.cpp | 23 +++- src/resources/Model.cpp | 7 +- src/resources/Resource.cpp | 20 +-- src/resources/Resource.h | 22 +-- src/resources/ResourceHandler.cpp | 32 ++--- src/resources/ResourceHandler.h | 8 +- src/resources/Texture.cpp | 15 ++- src/resources/Texture.h | 3 +- .../{TextureTypes.h => TextureType.h} | 0 tools/CMakeLists.txt | 2 - 36 files changed, 219 insertions(+), 394 deletions(-) create mode 100644 CMakePresets.json delete mode 100644 Fall-Fever.kdev4 delete mode 100644 PreLoad.cmake delete mode 100644 data/shaders/directionalShadowDepth.frag delete mode 100644 data/shaders/directionalShadowDepth.vert delete mode 100644 data/shaders/pointShadowDepth.frag delete mode 100644 data/shaders/pointShadowDepth.geom delete mode 100644 data/shaders/pointShadowDepth.vert rename src/resources/{TextureTypes.h => TextureType.h} (100%) diff --git a/.gitignore b/.gitignore index cbdbfb4..d6e2c6f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ res/textures .kdev4 .cache imgui.ini +CMakeUserPresets.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 0207b4a..50c9d30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.24) +cmake_minimum_required(VERSION 3.14) project( Fall-Fever @@ -13,12 +13,5 @@ find_package(glfw3 REQUIRED) find_package(glm REQUIRED) find_package(spdlog REQUIRED) -# Colored output with Ninja -if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - add_compile_options (-fdiagnostics-color=always) -elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - add_compile_options (-fcolor-diagnostics) -endif() - add_subdirectory(${PROJECT_SOURCE_DIR}/lib) add_subdirectory(${PROJECT_SOURCE_DIR}/src) diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..61b597b --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,57 @@ +{ + "version": 2, + "cmakeMinimumRequired": { + "major": 3, + "minor": 14, + "patch": 0 + }, + "configurePresets": [ + { + "name": "cmake-pedantic", + "hidden": true, + "warnings": { + "dev": true, + "deprecated": true, + "uninitialized": true, + "unusedCli": true, + "systemVars": false + }, + "errors": { + "dev": true, + "deprecated": true + } + }, + { + "name": "dev-mode", + "hidden": true, + "inherits": "cmake-pedantic", + "cacheVariables": { + "CMAKE_COLOR_DIAGNOSTICS": true + } + }, + { + "name": "ci-std", + "description": "This preset makes sure the project actually builds with at least the specified standard", + "hidden": true, + "cacheVariables": { + "CMAKE_CXX_EXTENSIONS": "OFF" + } + }, + { + "name": "flags-unix", + "hidden": true, + "cacheVariables": { + "CMAKE_CXX_FLAGS": "-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wcast-qual -Wshadow -Wformat=2 -Wundef -Werror=float-equal" + } + }, + { + "name": "ci-unix", + "generator": "Unix Makefiles", + "hidden": true, + "inherits": ["flags-unix", "ci-std"], + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Release" + } + } + ] +} \ No newline at end of file diff --git a/Fall-Fever.kdev4 b/Fall-Fever.kdev4 deleted file mode 100644 index ee47a51..0000000 --- a/Fall-Fever.kdev4 +++ /dev/null @@ -1,4 +0,0 @@ -[Project] -CreatedFrom=CMakeLists.txt -Manager=KDevCMakeManager -Name=Fall-Fever diff --git a/PreLoad.cmake b/PreLoad.cmake deleted file mode 100644 index 770bbfc..0000000 --- a/PreLoad.cmake +++ /dev/null @@ -1,7 +0,0 @@ -if(UNIX) - set(CMAKE_GENERATOR "Ninja" CACHE INTERNAL "" FORCE) -endif(UNIX) - -if(WIN32) - set(CMAKE_GENERATOR "MinGW Makefiles" CACHE INTERNAL "" FORCE) -endif(WIN32) diff --git a/data/shaders/directionalShadowDepth.frag b/data/shaders/directionalShadowDepth.frag deleted file mode 100644 index 3ff54d5..0000000 --- a/data/shaders/directionalShadowDepth.frag +++ /dev/null @@ -1,7 +0,0 @@ -#version 330 core - -void main() { - - // Empty as we aren't rendering to any color buffer - -} diff --git a/data/shaders/directionalShadowDepth.vert b/data/shaders/directionalShadowDepth.vert deleted file mode 100644 index e7365fe..0000000 --- a/data/shaders/directionalShadowDepth.vert +++ /dev/null @@ -1,11 +0,0 @@ -#version 330 core - -layout(location = 0) in vec3 a_position; - -uniform mat4 u_modelViewProjMatrix; - -void main() { - - gl_Position = u_modelViewProjMatrix * vec4(a_position, 1.0f); - -} diff --git a/data/shaders/pointShadowDepth.frag b/data/shaders/pointShadowDepth.frag deleted file mode 100644 index 100b98a..0000000 --- a/data/shaders/pointShadowDepth.frag +++ /dev/null @@ -1,17 +0,0 @@ -#version 330 core -in vec4 v_fragmentPosition; - -uniform vec3 v_lightPos; -uniform float pointShadowDepthMapFarPlane; - -void main() { - - // Distance between fragment and light source - float lightDistance = length(v_fragmentPosition.xyz - v_lightPos); - - // map to [0;1] range - lightDistance = lightDistance / pointShadowDepthMapFarPlane; - - gl_FragDepth = lightDistance; - -} \ No newline at end of file diff --git a/data/shaders/pointShadowDepth.geom b/data/shaders/pointShadowDepth.geom deleted file mode 100644 index b4eca83..0000000 --- a/data/shaders/pointShadowDepth.geom +++ /dev/null @@ -1,25 +0,0 @@ -#version 330 core -layout (triangles) in; -layout (triangle_strip, max_vertices=18) out; - -uniform mat4 u_shadowMatrices[6]; - -out vec4 v_fragmentPosition; - -void main() { - - for(int face = 0; face < 6; face++) { - - gl_Layer = face; - for(int i = 0; i < 3; i++) { - - v_fragmentPosition = gl_in[i].gl_Position; - gl_Position = u_shadowMatrices[face] * v_fragmentPosition; - EmitVertex(); - - } - - EndPrimitive(); - - } -} diff --git a/data/shaders/pointShadowDepth.vert b/data/shaders/pointShadowDepth.vert deleted file mode 100644 index 160b717..0000000 --- a/data/shaders/pointShadowDepth.vert +++ /dev/null @@ -1,10 +0,0 @@ -#version 330 core - -layout(location = 0) in vec3 a_position; - -uniform mat4 u_modelMatrix; - -void main() { - - gl_Position = u_modelMatrix * vec4(a_position, 1.0f); -} diff --git a/src/Camera.cpp b/src/Camera.cpp index 347f145..9050c35 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -18,7 +18,6 @@ void Camera::updateVPM() void Camera::updateAspectRatio(float aspectRatio) { - // m_projectionMatrix = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.f, 100.0f); m_projectionMatrix = glm::perspective(m_fov / 2.F, aspectRatio, .1F, 1000.F); updateVPM(); } @@ -71,12 +70,12 @@ void Camera::updateDirectionFromMouseInput(MouseCursorInput const &mouse_cursor_ { auto [deltaX, deltaY] = mouse_cursor_input; - if (deltaX == 0 && deltaY == 0) { + if (std::abs(deltaX) < std::numeric_limits::epsilon() && std::abs(deltaY) < std::numeric_limits::epsilon()) { return; } - m_yaw += deltaX; - m_pitch += deltaY; + m_yaw += static_cast(deltaX); + m_pitch += static_cast(deltaY); static constexpr float CLIP = 89.; @@ -88,9 +87,9 @@ void Camera::updateDirectionFromMouseInput(MouseCursorInput const &mouse_cursor_ } glm::vec3 direction; - direction.x = std::cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch)); + direction.x = std::cos(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch)); direction.y = std::sin(glm::radians(m_pitch)); - direction.z = std::sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch)); + direction.z = std::sin(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch)); m_front_vec = glm::normalize(direction); } diff --git a/src/Controller.cpp b/src/Controller.cpp index fdad341..4b5204c 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -1,6 +1,7 @@ #include "Controller.h" #include "Camera.h" #include "Entity.h" +#include "FrameBuffer.h" #include "Helper.h" #include "Light.h" #include "Scene.h" @@ -18,15 +19,11 @@ Controller::Controller() : m_gameWindow(std::make_shared()), m_camera(std::make_shared(90., m_gameWindow->aspectRatio())) { 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{"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"}, + "data/shaders/postprocessing.frag"}, }; for (auto &prototype : shaderProgramPrototypes) { @@ -46,7 +43,7 @@ void Controller::run() updateExposure(*getShaderProgramByName("postProcessingProgram")); auto lightSource = m_scene->getEntityByName("light"); - lightSource->setScale(.1); + lightSource->setScale(.1F); lightSource->setRotation(glm::vec3(0.)); lightSource->setPosition(glm::vec3(-2., 1.5, 2.)); @@ -116,7 +113,7 @@ void Controller::limit_framerate() double frameTime = 1 / (double)MAX_FPS; if (frameTime > lastTime) { - Helper::sleep((frameTime - lastTime) * 1000000); + Helper::sleep(static_cast(frameTime - lastTime) * 1000000); } m_deltaTime = glfwGetTime() - startingTime; diff --git a/src/Entity.h b/src/Entity.h index b0bc6d2..89cc269 100644 --- a/src/Entity.h +++ b/src/Entity.h @@ -18,8 +18,8 @@ class Entity public: struct Prototype { - Prototype(const std::string &name, glm::vec3 position, glm::vec3 rotation, float scale) - : name(name), position(position), rotation(rotation), scale(scale) + Prototype(const std::string &_name, glm::vec3 _position, glm::vec3 _rotation, float _scale) + : name(_name), position(_position), rotation(_rotation), scale(_scale) {} virtual ~Prototype() = default; @@ -66,17 +66,16 @@ class ModelEntity : public Entity public: struct Prototype : public Entity::Prototype { - Prototype(const std::string &name, glm::vec3 position, glm::vec3 rotation, float scale, - const std::string &modelName, const std::string &shaderProgramName) - : Entity::Prototype(name, position, rotation, scale), modelName(modelName), - shaderProgramName(shaderProgramName) + Prototype(const std::string &_name, glm::vec3 _position, glm::vec3 _rotation, float _scale, + std::string _modelName, std::string _shaderProgramName) + : Entity::Prototype(_name, _position, _rotation, _scale), modelName(std::move(_modelName)), + shaderProgramName(std::move(_shaderProgramName)) {} std::string modelName; std::string shaderProgramName; }; ModelEntity(Prototype prototype, const Model *model, ShaderProgram *shaderProgram); - ~ModelEntity() = default; void draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition); void drawDirectionalShadows(glm::mat4 viewProjMatrix, ShaderProgram *p_shaderProgram); diff --git a/src/FrameBuffer.cpp b/src/FrameBuffer.cpp index 6b20261..cb3120b 100644 --- a/src/FrameBuffer.cpp +++ b/src/FrameBuffer.cpp @@ -8,7 +8,8 @@ #include AbstractFrameBuffer::~AbstractFrameBuffer() -{} +{ +} void AbstractFrameBuffer::bind() const { @@ -66,7 +67,7 @@ void FrameBuffer::drawOnEntireScreen() const glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(0); - glPolygonMode(GL_FRONT_AND_BACK, wireframe); + glPolygonMode(GL_FRONT_AND_BACK, static_cast(wireframe)); m_shaderProgram->unbind(); } @@ -88,13 +89,15 @@ void FrameBuffer::generateTextures(uint32_t width, uint32_t height) glGenRenderbuffers(1, &m_depthStencilBuffer); glBindTexture(GL_TEXTURE_2D, m_colorBuffer); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, static_cast(width), static_cast(height), 0, GL_RGBA, + GL_FLOAT, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_colorBuffer, 0); glBindRenderbuffer(GL_RENDERBUFFER, m_depthStencilBuffer); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, static_cast(width), + static_cast(height)); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_depthStencilBuffer); glBindTexture(GL_TEXTURE_2D, 0); @@ -114,7 +117,8 @@ void FrameBuffer::setExposureCorrection(bool exposureCorrection) const } AbstractDepthMap::~AbstractDepthMap() -{} +{ +} DepthMap::DepthMap(int RESOLUTION) { @@ -171,7 +175,8 @@ DepthMapCube::DepthMapCube(int resolution) } DepthMapCube::~DepthMapCube() -{} +{ +} GLuint DepthMapCube::getCubeMapTextureId() { diff --git a/src/Light.h b/src/Light.h index dd9aea7..743f484 100644 --- a/src/Light.h +++ b/src/Light.h @@ -14,8 +14,8 @@ class Light : public Entity public: struct Prototype { - Prototype(const std::string &name, const std::string &parent, glm::vec3 color, float intensity) - : name(name), parent(parent), color(color), intensity(intensity) + Prototype(const std::string &_name, const std::string &_parent, glm::vec3 _color, float _intensity) + : name(_name), parent(_parent), color(_color), intensity(_intensity) {} virtual ~Prototype() = default; @@ -57,9 +57,9 @@ class PointLight : public Light public: struct Prototype : public Light::Prototype { - Prototype(const std::string &name, const std::string &parent, glm::vec3 position, glm::vec3 color, - float intensity) - : Light::Prototype(name, parent, color, intensity), position(position) + Prototype(const std::string &_name, const std::string &_parent, glm::vec3 _position, glm::vec3 _color, + float _intensity) + : Light::Prototype(_name, _parent, _color, _intensity), position(_position) {} glm::vec3 position; }; @@ -83,8 +83,8 @@ class DirectionalLight : public Light public: struct Prototype : public Light::Prototype { - Prototype(const std::string &name, glm::vec3 direction, glm::vec3 color, float intensity) - : Light::Prototype(name, "", color, intensity), direction(direction) + Prototype(const std::string &_name, glm::vec3 _direction, glm::vec3 _color, float _intensity) + : Light::Prototype(_name, "", _color, _intensity), direction(_direction) {} glm::vec3 direction; }; diff --git a/src/Mesh.cpp b/src/Mesh.cpp index f2b8ff5..395b49e 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -5,15 +5,17 @@ #include "resources/Texture.h" Mesh::Mesh(std::vector vertices, std::vector indices, std::vector textures) - : m_preInitializationVertexData{vertices, indices}, m_numElements(indices.size()), m_textures(textures) -{} + : m_preInitializationVertexData{vertices, indices}, m_numElements(static_cast(indices.size())), + m_textures(textures) +{ +} void Mesh::initializeOnGPU() { - m_vertexArray = - new VertexArray(static_cast(m_preInitializationVertexData.vertices.data()), - static_cast(m_preInitializationVertexData.indices.data()), - m_preInitializationVertexData.vertices.size(), m_preInitializationVertexData.indices.size()); + m_vertexArray = new VertexArray(static_cast(m_preInitializationVertexData.vertices.data()), + static_cast(m_preInitializationVertexData.indices.data()), + static_cast(m_preInitializationVertexData.vertices.size()), + static_cast(m_preInitializationVertexData.indices.size())); } Mesh::~Mesh() @@ -26,12 +28,12 @@ void Mesh::draw(ShaderProgram *shaderProgram) uint8_t typeNumberCount[static_cast(TextureType::TEXTURE_TYPE_NUM_ITEMS)]{0}; glBindTexture(GL_TEXTURE_2D, 0); // Bind all textures in order to its texture unit - int i = 0; + std::size_t i = 0; for (auto it : m_textures) { auto texture = std::static_pointer_cast(ResourceHandler::instance().resource(it)); TextureType currentTextureType = texture->textureType(); - texture->bind(i, shaderProgram, typeNumberCount[static_cast(currentTextureType)]); + texture->bind(static_cast(i), shaderProgram, typeNumberCount[static_cast(currentTextureType)]); typeNumberCount[static_cast(currentTextureType)] += 1; @@ -40,7 +42,7 @@ void Mesh::draw(ShaderProgram *shaderProgram) // Draw elements m_vertexArray->bind(); - glDrawElements(GL_TRIANGLES, m_numElements, GL_UNSIGNED_INT, 0); + glDrawElements(GL_TRIANGLES, static_cast(m_numElements), GL_UNSIGNED_INT, 0); m_vertexArray->unbind(); // Unbind all textures @@ -53,7 +55,7 @@ void Mesh::draw(ShaderProgram *shaderProgram) void Mesh::drawWithoutTextures() { m_vertexArray->bind(); - glDrawElements(GL_TRIANGLES, m_numElements, GL_UNSIGNED_INT, 0); + glDrawElements(GL_TRIANGLES, static_cast(m_numElements), GL_UNSIGNED_INT, 0); m_vertexArray->unbind(); } diff --git a/src/Mesh.h b/src/Mesh.h index 2e675f4..b166cb2 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -2,7 +2,7 @@ #include "VertexArray.h" #include "definitions/models.h" -#include "resources/Texture.h" +#include "resources/Resource.h" #include @@ -19,7 +19,7 @@ public: void draw(ShaderProgram *shaderProgram); void drawWithoutTextures(); - VertexArray *getVertexArray(); + auto getVertexArray() -> VertexArray *; private: struct PreInitializationVertexData diff --git a/src/Scene.cpp b/src/Scene.cpp index 8fda284..8ffe3a7 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -1,13 +1,10 @@ #include "Scene.h" -#include "Camera.h" #include "Controller.h" #include "Entity.h" -#include "FrameBuffer.h" #include "Light.h" #include "ShaderProgram.h" #include "resources/Model.h" #include "resources/ResourceHandler.h" -#include "resources/Texture.h" #include "util/Log.h" #include @@ -16,14 +13,7 @@ Scene::Scene(std::vector> shaderPrograms) : m_shaderProgram(*Controller::getShaderProgramByName("defaultProgram", shaderPrograms)) -// m_depthMapDirectionalFBO(SHADOW_RES) { - // // Create 4 depthMaps - // for (int i = 0; i < 4; i++) { - // DepthMapCube *temp_depthMap = new DepthMapCube(SHADOW_RES); - // m_depthMapPointFBO.push_back(temp_depthMap); - // } - // This will be removed in future when gloss maps are implemented m_shaderProgram.bind(); m_shaderProgram.setUniform("u_material.shininess", 100.0f); @@ -43,7 +33,7 @@ Scene::Scene(std::vector> shaderPrograms) for (const auto &descriptor : modelDescriptors) { - auto loadModel = [=, &mutex]() { + auto loadModel = [=, this, &mutex]() { ResourceId model = ResourceHandler::instance().registerResource(descriptor); Log::logger().info("Loaded model \"{}\": {}", descriptor.name, descriptor.path); @@ -66,7 +56,7 @@ Scene::Scene(std::vector> shaderPrograms) m_skybox->initializeOnGPU(); std::array entityPrototypes{ - ModelEntity::Prototype{"backpack", {0., 1., 0.}, {}, 0.6, "backpack", "defaultProgram"}, + ModelEntity::Prototype{"backpack", {0., 1., 0.}, {}, 0.6F, "backpack", "defaultProgram"}, ModelEntity::Prototype{"container", {10., 1., 0.}, {45., 45., 45.}, 1., "container", "defaultProgram"}, ModelEntity::Prototype{"ground", {}, {}, 1., "ground", "defaultProgram"}, ModelEntity::Prototype{"light", {}, {}, 1., "cube", "lightProgram"}, @@ -118,7 +108,7 @@ Scene::Scene(std::vector> shaderPrograms) auto pointPrototype = dynamic_cast(prototype.get()); if (pointPrototype) { currentLight = std::make_shared(*pointPrototype, &m_shaderProgram); - } + } lights.push_back(currentLight); Log::logger().info("Loaded light \"{}\"", prototype->name); @@ -150,118 +140,8 @@ void Scene::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) for (auto it = m_entities.begin(); it != m_entities.end(); it++) { (*it)->draw(viewProjMatrix, viewPosition); } - - // Calculate shadows (unclear if it should belong here or somewhere else) - // calculateShadows(); } -// void Scene::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram) -// { -// // Get old viewport dimensions to reset them later... -// GLint VIEWPORT[4]; -// glGetIntegerv(GL_VIEWPORT, VIEWPORT); - -// glViewport(0, 0, SHADOW_RES, SHADOW_RES); -// // Switch face culling (Peter panning) -// glCullFace(GL_BACK); - -// m_depthMapDirectionalFBO.bind(); - -// 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 directionalLightViewProjectionMatrix = m_directionalLightProjection * directionalLightView; - -// // Draw scene from light perspective -// // Draw all entities -// for (auto it = m_entities.begin(); it != m_entities.end(); it++) { -// (*it)->drawDirectionalShadows(directionalLightViewProjectionMatrix, directionalShaderProgram); -// } - -// m_depthMapDirectionalFBO.unbind(); - -// m_shaderProgram->bind(); - -// // Send lightViewProjMatrix to basic shader -// m_shaderProgram->setUniform("u_directionalLightViewProjMatrix", directionalLightViewProjectionMatrix); - -// // Send shadowMap to basic shader -// int textureUnit = static_cast(TextureType::TEXTURE_TYPE_NUM_ITEMS) * 2; -// m_shaderProgram->setUniform("u_texture_directionalShadowMap", (int)textureUnit); -// glActiveTexture(GL_TEXTURE0 + textureUnit); -// glBindTexture(GL_TEXTURE_2D, m_depthMapDirectionalFBO.getDepthMap()); - -// m_shaderProgram->unbind(); - -// // --- Point shadows --- -// std::vector pointLights = getPointLights(); - -// // 4 depthMaps for 4 point lights -// for (int i = 0; i < 1; i++) { -// m_depthMapPointFBO[i]->bind(); - -// glClear(GL_DEPTH_BUFFER_BIT); - -// // Create 6 view matrices for every face of the cubeMap -// std::vector viewProjMatrices; -// glm::vec3 lightPos = pointLights[i]->getPosition(); -// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, -// lightPos + glm::vec3(1.0f, 0.0f, 0.0f), -// glm::vec3(0.0f, -1.0f, 0.0f))); -// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, -// lightPos + glm::vec3(-1.0f, 0.0f, 0.0f), -// glm::vec3(0.0f, -1.0f, 0.0f))); -// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, -// lightPos + glm::vec3(0.0f, 1.0f, 0.0f), -// glm::vec3(0.0f, 0.0f, 1.0f))); -// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, -// lightPos + glm::vec3(0.0f, -1.0f, 0.0f), -// glm::vec3(0.0f, 0.0f, -1.0f))); -// viewProjMatrices.push_back(m_pointLightProjection * glm::lookAt(lightPos, -// lightPos + glm::vec3(0.0f, 0.0f, 1.0f), -// glm::vec3(0.0f, -1.0f, 0.0f))); -// viewProjMatrices.push_back(m_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("pointShadowDepthMapFarPlane", m_farPlanePoint); -// pointShaderProgram->setUniform("v_lightPos", lightPos); - -// // Draw scene from light perspective -// // Draw all entities -// for (auto it = m_entities.begin(); it != m_entities.end(); it++) { -// (*it)->drawPointShadows(pointShaderProgram); -// } - -// m_depthMapPointFBO[i]->unbind(); - -// m_shaderProgram->bind(); - -// m_shaderProgram->setUniform("pointShadowDepthMapFarPlane", m_farPlanePoint); - -// textureUnit = static_cast(TextureType::TEXTURE_TYPE_NUM_ITEMS) * 2 + i + 1; -// m_shaderProgram->setUniform("u_texture_pointShadowMap0", (int)textureUnit); -// glActiveTexture(GL_TEXTURE0 + textureUnit); -// glBindTexture(GL_TEXTURE_CUBE_MAP, m_depthMapPointFBO[i]->getCubeMapTextureId()); - -// m_shaderProgram->unbind(); -// } - -// // Reset viewport size -// glViewport(VIEWPORT[0], VIEWPORT[1], VIEWPORT[2], VIEWPORT[3]); -// glCullFace(GL_FRONT); -// } - std::shared_ptr Scene::getEntityByName(const std::string &name) { for (auto it = m_entities.begin(); it != m_entities.end(); it++) { diff --git a/src/Scene.h b/src/Scene.h index 6592609..7025949 100644 --- a/src/Scene.h +++ b/src/Scene.h @@ -1,6 +1,5 @@ #pragma once -#include "FrameBuffer.h" #include "resources/Resource.h" #include @@ -9,14 +8,12 @@ #include #include -class Camera; class ModelEntity; class Light; class PointLight; class DirectionalLight; class ShaderProgram; class Skybox; -class Model; class Scene { @@ -34,7 +31,6 @@ public: std::shared_ptr getEntityById(uint32_t id); void draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition); - // void calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram); private: ShaderProgram &m_shaderProgram; @@ -45,19 +41,4 @@ private: // Lights std::vector> m_lights; - - // // Shadows - // const int SHADOW_RES = 4096 / 4; - // DepthMap m_depthMapDirectionalFBO; - // std::vector m_depthMapPointFBO; - // // Shadow projection matrices - // const float m_nearPlaneDirectional = 1.0f; - // const float m_farPlaneDirectional = 15.0f; - // glm::mat4 m_directionalLightProjection = - // glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, m_nearPlaneDirectional, m_farPlaneDirectional); - // const float m_aspectRatioPoint = 1.0f; - // const float m_nearPlanePoint = 1.0f; - // const float m_farPlanePoint = 25.0f; - // glm::mat4 m_pointLightProjection = - // glm::perspective(glm::radians(90.0f), m_aspectRatioPoint, m_nearPlanePoint, m_farPlanePoint); }; diff --git a/src/ShaderProgram.cpp b/src/ShaderProgram.cpp index 3da4178..bcb9840 100644 --- a/src/ShaderProgram.cpp +++ b/src/ShaderProgram.cpp @@ -11,25 +11,18 @@ ShaderProgram::ShaderProgram(Prototype prototype) : m_uniqueName(prototype.name) m_shaderProgramId = glCreateProgram(); GLuint vs = compile(vertexShaderSource, GL_VERTEX_SHADER); - GLuint gs; GLuint fs = compile(fragmentShaderSource, GL_FRAGMENT_SHADER); glAttachShader(m_shaderProgramId, vs); glAttachShader(m_shaderProgramId, fs); - if (!prototype.geometryPath.empty()) { - std::string geometryShaderSource = parse(prototype.geometryPath.c_str()); - gs = compile(geometryShaderSource, GL_GEOMETRY_SHADER); - glAttachShader(m_shaderProgramId, gs); - } - glLinkProgram(m_shaderProgramId); GLint isLinked = 0; glGetProgramiv(m_shaderProgramId, GL_LINK_STATUS, &isLinked); if (!isLinked) - Log::logger().critical("Failed to link shaderProgram \"{}\", \"{}\", \"{}\"", prototype.vertexPath, - prototype.geometryPath, prototype.fragmentPath); + Log::logger().critical("Failed to link shaderProgram \"{}\", \"{}\"", prototype.vertexPath, + prototype.fragmentPath); #ifdef _RELEASE glDetachShader(program, vs); @@ -37,11 +30,6 @@ ShaderProgram::ShaderProgram(Prototype prototype) : m_uniqueName(prototype.name) glDeleteShader(vs); glDeleteShader(fs); - - if (!prototype.geometryPath.empty()) { - glDetachShader(program, gs); - glDeleteShader(gs); - } #endif } @@ -87,7 +75,7 @@ GLuint ShaderProgram::compile(const std::string &shaderSource, GLenum type) if (result != GL_TRUE) { int length; glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &length); - char *message = new char[length]; + char *message = new char[static_cast(length)]; glGetShaderInfoLog(shaderId, length, &length, message); Log::logger().error("Shader compilation failed: {}", message); delete[] message; diff --git a/src/ShaderProgram.h b/src/ShaderProgram.h index f6434aa..ece2a07 100644 --- a/src/ShaderProgram.h +++ b/src/ShaderProgram.h @@ -13,7 +13,6 @@ public: std::string name; std::string vertexPath; std::string fragmentPath; - std::string geometryPath; }; ShaderProgram(Prototype prototype); diff --git a/src/VertexArray.cpp b/src/VertexArray.cpp index 049e018..393ca2a 100644 --- a/src/VertexArray.cpp +++ b/src/VertexArray.cpp @@ -55,21 +55,3 @@ void VertexArray::unbind() { glBindVertexArray(0); } - -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) { - Vertex currentVertex = {}; - currentVertex.position.x = vertices[i]; - 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; - vertexVec.push_back(currentVertex); - } - return vertexVec; -} diff --git a/src/VertexArray.h b/src/VertexArray.h index 4dc1ca2..b9dde17 100644 --- a/src/VertexArray.h +++ b/src/VertexArray.h @@ -1,7 +1,5 @@ #pragma once -#include "definitions/models.h" - #include #include @@ -14,8 +12,6 @@ public: void bind(); void unbind(); - static std::vector createVertices(double *vertices, uint32_t numVertices, float *textureCoordinates); - private: GLuint m_VAO; GLuint m_VBO; diff --git a/src/Window.cpp b/src/Window.cpp index 61ba1f5..df28fe6 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -1,11 +1,10 @@ #include "Window.h" #include "Helper.h" -#include "ShaderProgram.h" #include "definitions/window.h" #include "util/Log.h" -#include #include +#include Window::Window() { @@ -29,8 +28,9 @@ Window::Window() glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE); #endif - m_glfw_window = std::shared_ptr(glfwCreateWindow(m_width, m_height, "OpenGL", nullptr, nullptr), - [](GLFWwindow *window) { glfwDestroyWindow(window); }); + m_glfw_window = std::shared_ptr( + glfwCreateWindow(static_cast(m_width), static_cast(m_height), "OpenGL", nullptr, nullptr), + [](GLFWwindow *window) { glfwDestroyWindow(window); }); if (!m_glfw_window) { Log::logger().critical("Failed to create window"); } @@ -38,8 +38,13 @@ Window::Window() // Wait for window to maximize (in case) // Helper::sleep(1000); - glfwGetWindowPos(m_glfw_window.get(), &m_posX, &m_posY); - glfwGetWindowSize(m_glfw_window.get(), &m_width, &m_height); + int width{}; + int height{}; + + glfwGetWindowSize(m_glfw_window.get(), &width, &height); + + m_width = static_cast(width); + m_height = static_cast(height); // Create OpenGL context glfwMakeContextCurrent(m_glfw_window.get()); @@ -75,7 +80,7 @@ Window::Window() set_catched_cursor(m_mouse_catched); - glViewport(0, 0, m_width, m_height); + glViewport(0, 0, static_cast(m_width), static_cast(m_height)); glfwSetWindowUserPointer(m_glfw_window.get(), this); glfwSetKeyCallback(m_glfw_window.get(), key_callback); @@ -96,15 +101,20 @@ auto Window::dimensions_changed() const -> bool glfwGetFramebufferSize(m_glfw_window.get(), &new_width, &new_height); glfwGetWindowPos(m_glfw_window.get(), &new_posx, &new_posy); - return !(new_width == m_width && new_height == m_height && new_posx == m_posX && new_posy == m_posY); + return !(static_cast(new_width) == m_width && static_cast(new_height) == m_height); } void Window::update_dimensions() { - glfwGetFramebufferSize(m_glfw_window.get(), &m_width, &m_height); - glfwGetWindowPos(m_glfw_window.get(), &m_posX, &m_posY); + int width{}; + int height{}; - glViewport(0, 0, m_width, m_height); + glfwGetFramebufferSize(m_glfw_window.get(), &width, &height); + + m_width = static_cast(width); + m_height = static_cast(height); + + glViewport(0, 0, width, height); } void Window::set_catched_cursor(bool value) @@ -173,7 +183,8 @@ void Window::mouse_cursor_callback(GLFWwindow *glfw_window, double xpos, double window.m_last_cursor_pos_y = ypos; // Check if this is the first VALID mouse event after window being resized - if (window.m_first_mouse_input && !(deltaCursorPosX == 0 && deltaCursorPosY == 0)) { + if (window.m_first_mouse_input && !(std::abs(deltaCursorPosX) < std::numeric_limits::epsilon() && + std::abs(deltaCursorPosY) < std::numeric_limits::epsilon())) { window.m_first_mouse_input = false; deltaCursorPosX = 0.0; deltaCursorPosY = 0.0; diff --git a/src/Window.h b/src/Window.h index b0b248d..25e8e1d 100644 --- a/src/Window.h +++ b/src/Window.h @@ -35,7 +35,7 @@ private: static void glfw_error_callback(int error, const char *description); static void framebuffer_size_callback(GLFWwindow *glfw_window, int width, int height); - static constexpr float MOUSE_SENSITIVITY = 0.15; + static constexpr float MOUSE_SENSITIVITY = 0.15f; void set_catched_cursor(bool value); @@ -46,10 +46,8 @@ private: MouseButtonInput m_mouse_button_input; MouseCursorInput m_mouse_cursor_input; - int m_posX{}; - int m_posY{}; - int m_width{}; - int m_height{}; + uint32_t m_width{}; + uint32_t m_height{}; double m_last_cursor_pos_x = 0.0; double m_last_cursor_pos_y = 0.0; diff --git a/src/resources/AbstractTexture.h b/src/resources/AbstractTexture.h index dcced96..69963b5 100644 --- a/src/resources/AbstractTexture.h +++ b/src/resources/AbstractTexture.h @@ -9,7 +9,7 @@ public: {} protected: - int32_t m_textureWidth; - int32_t m_textureHeight; - int32_t m_numComponents; + uint32_t m_textureWidth; + uint32_t m_textureHeight; + uint32_t m_numComponents; }; diff --git a/src/resources/CubeMap.cpp b/src/resources/CubeMap.cpp index 10a1c6e..22838ab 100644 --- a/src/resources/CubeMap.cpp +++ b/src/resources/CubeMap.cpp @@ -20,11 +20,19 @@ TextureCubeMap::TextureCubeMap(const TextureCubeMapDescriptor &descriptor) : Abs 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; + std::size_t i = 0; for (const auto &faceName : FACE_NAMES) { std::string texturePath = descriptor.path + faceName; - auto textureBuffer = stbi_load(texturePath.c_str(), &m_textureWidth, &m_textureHeight, &m_numComponents, 0); + int textureWidth{}; + int textureHeight{}; + int numComponents{}; + + auto textureBuffer = stbi_load(texturePath.c_str(), &textureWidth, &textureHeight, &numComponents, 0); + + m_textureWidth = static_cast(textureWidth); + m_textureHeight = static_cast(textureHeight); + m_numComponents = static_cast(numComponents); if (!textureBuffer) { Log::logger().warn("CubeMap texture {} could not be loaded", texturePath); @@ -53,7 +61,7 @@ void TextureCubeMap::initialize() int i = 0; for (auto &textureBuffer : m_textureBuffers) { - GLenum internalFormat; + GLint internalFormat; GLenum dataFormat; switch (m_numComponents) { @@ -71,8 +79,9 @@ void TextureCubeMap::initialize() break; } - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internalFormat, m_textureWidth, m_textureHeight, 0, - dataFormat, GL_UNSIGNED_BYTE, textureBuffer); + glTexImage2D(static_cast(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i), 0, internalFormat, + static_cast(m_textureWidth), static_cast(m_textureHeight), 0, dataFormat, + GL_UNSIGNED_BYTE, textureBuffer); stbi_image_free(textureBuffer); i++; @@ -111,8 +120,8 @@ InternalCubeMap::InternalCubeMap(unsigned int resolution) : AbstractCubeMap("int glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); for (unsigned int i = 0; i < static_cast(CubeMapFace::CUBEMAP_FACES_NUM_ITEMS); 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, static_cast(resolution), + static_cast(resolution), 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); } glBindTexture(GL_TEXTURE_CUBE_MAP, 0); diff --git a/src/resources/Model.cpp b/src/resources/Model.cpp index 6237226..a4c670d 100644 --- a/src/resources/Model.cpp +++ b/src/resources/Model.cpp @@ -1,6 +1,7 @@ #include "Model.h" #include "../util/Log.h" #include "ResourceHandler.h" +#include "Texture.h" #include #include @@ -63,12 +64,12 @@ void Model::loadModel(const std::string &pathToModel) std::vector textureSources; for (unsigned int i = 0; i < numTextures; i++) { std::string currentTextureSource; - for (unsigned int i = 0; i < 128; i++) { + for (unsigned int k = 0; k < 128; k++) { uint8_t currentChar; input.read((char *)¤tChar, sizeof(uint8_t)); if (currentChar) { - currentTextureSource.push_back(currentChar); + currentTextureSource.push_back(static_cast(currentChar)); } } textureSources.push_back(currentTextureSource); @@ -82,7 +83,7 @@ void Model::loadModel(const std::string &pathToModel) for (unsigned int i = 0; i < numTextures; i++) { std::string texturePath = m_workingPath + '/' + textureSources[i].c_str(); - auto loadModel = [=, &mutex]() { + auto loadModel = [=, this, &mutex]() { ResourceId texture = ResourceHandler::instance().registerResource( TextureDescriptor{texturePath, textureTypes[i]}); diff --git a/src/resources/Resource.cpp b/src/resources/Resource.cpp index 244b514..2516d63 100644 --- a/src/resources/Resource.cpp +++ b/src/resources/Resource.cpp @@ -3,38 +3,38 @@ ResourceId Resource::s_idCounter = 0; -Resource::Resource(const std::filesystem::path &path) : m_id(s_idCounter++), m_path(path) +Resource::Resource(std::filesystem::path path) : m_id(s_idCounter++), m_path(std::move(path)) { Log::logger().info("Resource \"{}\" with id {} created", m_path.string(), m_id); } -ResourceId Resource::id() const +auto Resource::id() const -> ResourceId { return m_id; } -bool Resource::isInitialized() const +auto Resource::isInitialized() const -> bool { return m_initialized; } -const std::filesystem::path &Resource::resourcePath() const +auto Resource::resourcePath() const -> const std::filesystem::path & { return m_path; } -GLuint GlResource::glId() const +auto GlResource::glId() const -> GLuint { return m_glId; } -NamedResource::NamedResource(const std::string &name) : m_name(name) -{} +NamedResource::NamedResource(std::string name) : m_name(std::move(name)) +{ +} -const std::string &NamedResource::name() const +auto NamedResource::name() const -> const std::string & { return m_name; } -NamedResource::~NamedResource() -{} +NamedResource::~NamedResource() = default; diff --git a/src/resources/Resource.h b/src/resources/Resource.h index 38400e4..bc0174b 100644 --- a/src/resources/Resource.h +++ b/src/resources/Resource.h @@ -10,17 +10,17 @@ using ResourceId = uint64_t; class Resource { public: - Resource(const std::filesystem::path &path); + Resource(std::filesystem::path path); Resource(const Resource &other) = delete; Resource(Resource &&other) = delete; - Resource &operator=(const Resource &other) = delete; - Resource &operator=(Resource &&other) = delete; + auto operator=(const Resource &other) -> Resource & = delete; + auto operator=(Resource &&other) -> Resource & = delete; - ResourceId id() const; - const std::filesystem::path &resourcePath() const; + [[nodiscard]] auto id() const -> ResourceId; + [[nodiscard]] auto resourcePath() const -> const std::filesystem::path &; protected: - bool isInitialized() const; + [[nodiscard]] auto isInitialized() const -> bool; virtual void initialize() = 0; bool m_initialized = false; @@ -40,7 +40,7 @@ public: // virtual ~GlResource() = 0; TODO!! virtual void unbind() const = 0; - GLuint glId() const; + auto glId() const -> GLuint; protected: GLuint m_glId; @@ -49,11 +49,11 @@ protected: class NamedResource { public: - NamedResource(const std::string &name); + NamedResource(std::string name); virtual ~NamedResource() = 0; - const std::string &name() const; + [[nodiscard]] auto name() const -> const std::string &; -protected: - std::string m_name; +private: + const std::string m_name; }; diff --git a/src/resources/ResourceHandler.cpp b/src/resources/ResourceHandler.cpp index 255b367..2d7ff5d 100644 --- a/src/resources/ResourceHandler.cpp +++ b/src/resources/ResourceHandler.cpp @@ -8,13 +8,13 @@ ResourceHandler ResourceHandler::s_instance; -ResourceHandler &ResourceHandler::instance() +auto ResourceHandler::instance() -> ResourceHandler & { return s_instance; } template -ResourceId ResourceHandler::registerResource(Param const &...param) +auto ResourceHandler::registerResource(Param const &...param) -> ResourceId { auto resource = std::make_shared(param...); m_resources.emplace(resource->id(), resource); @@ -26,26 +26,27 @@ template ResourceId ResourceHandler::registerResource(TextureCub template ResourceId ResourceHandler::registerResource(int const &); template ResourceId ResourceHandler::registerResource(ModelDescriptor const &); -const std::shared_ptr ResourceHandler::resource(const ResourceId id) const +auto ResourceHandler::resource(const ResourceId resourceId) const -> std::shared_ptr { - auto it = m_resources.find(id); + auto resourceIt = m_resources.find(resourceId); - if (it != m_resources.end()) { - auto resource = it->second; + if (resourceIt != m_resources.end()) { + auto resource = resourceIt->second; - if (!resource->isInitialized()) + if (!resource->isInitialized()) { resource->initialize(); + } return resource; } - Log::logger().warn("Could not find resource with id {}", id); - return std::shared_ptr(); + Log::logger().warn("Could not find resource with id {}", resourceId); + return {}; } -const std::shared_ptr ResourceHandler::resource(const std::string &name) const +auto ResourceHandler::resource(const std::string &name) const -> std::shared_ptr { - auto it = std::find_if(m_resources.begin(), m_resources.end(), [&name](const auto &resource) { + auto resourceIt = std::find_if(m_resources.begin(), m_resources.end(), [&name](const auto &resource) { if (auto namedResource = std::dynamic_pointer_cast(resource.second)) { return namedResource->name() == name; } @@ -53,15 +54,16 @@ const std::shared_ptr ResourceHandler::resource(const std::string &nam return false; }); - if (it != m_resources.end()) { - auto resource = it->second; + if (resourceIt != m_resources.end()) { + auto resource = resourceIt->second; - if (!resource->isInitialized()) + if (!resource->isInitialized()) { resource->initialize(); + } return resource; } Log::logger().warn("Could not find resource with unique name \"{}\"", name); - return std::shared_ptr(); + return {}; } diff --git a/src/resources/ResourceHandler.h b/src/resources/ResourceHandler.h index bc4a8ef..f7c54fb 100644 --- a/src/resources/ResourceHandler.h +++ b/src/resources/ResourceHandler.h @@ -8,13 +8,13 @@ class ResourceHandler { public: - static ResourceHandler &instance(); + static auto instance() -> ResourceHandler &; template - ResourceId registerResource(Param const &...param); + auto registerResource(Param const &...param) -> ResourceId; - const std::shared_ptr resource(ResourceId id) const; - const std::shared_ptr resource(const std::string &name) const; + [[nodiscard]] auto resource(ResourceId resourceId) const -> std::shared_ptr; + [[nodiscard]] auto resource(const std::string &name) const -> std::shared_ptr; private: ResourceHandler() = default; diff --git a/src/resources/Texture.cpp b/src/resources/Texture.cpp index 563b511..ae873c8 100644 --- a/src/resources/Texture.cpp +++ b/src/resources/Texture.cpp @@ -9,7 +9,16 @@ Texture::Texture(const TextureDescriptor &descriptor) : AbstractTexture(descriptor.path), m_textureType(descriptor.textureType) { stbi_set_flip_vertically_on_load(1); - m_textureBuffer = stbi_load(resourcePath().c_str(), &m_textureWidth, &m_textureHeight, &m_numComponents, 0); + + int textureWidth{}; + int textureHeight{}; + int numComponents{}; + + m_textureBuffer = stbi_load(resourcePath().c_str(), &textureWidth, &textureHeight, &numComponents, 0); + + m_textureWidth = static_cast(textureWidth); + m_textureHeight = static_cast(textureHeight); + m_numComponents = static_cast(numComponents); if (!m_textureBuffer) Log::logger().warn("Texture {} could not be loaded", resourcePath().string()); @@ -48,8 +57,8 @@ void Texture::initialize() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, m_textureWidth, m_textureHeight, 0, dataFormat, GL_UNSIGNED_BYTE, - m_textureBuffer); + glTexImage2D(GL_TEXTURE_2D, 0, static_cast(internalFormat), static_cast(m_textureWidth), + static_cast(m_textureHeight), 0, dataFormat, GL_UNSIGNED_BYTE, m_textureBuffer); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); diff --git a/src/resources/Texture.h b/src/resources/Texture.h index 8f28a83..0e315fb 100644 --- a/src/resources/Texture.h +++ b/src/resources/Texture.h @@ -1,8 +1,7 @@ #pragma once #include "AbstractTexture.h" -#include "Resource.h" -#include "TextureTypes.h" +#include "TextureType.h" #include #include diff --git a/src/resources/TextureTypes.h b/src/resources/TextureType.h similarity index 100% rename from src/resources/TextureTypes.h rename to src/resources/TextureType.h diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 1194973..769e9fd 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,5 +1,3 @@ -project(obj-converter) - add_executable(obj-converter main.cpp) target_link_libraries(obj-converter assimp glm)