From 59cfd3c2f20cb419353808f25c092bb51756d7dc Mon Sep 17 00:00:00 2001 From: bailwillharr Date: Sun, 8 Jan 2023 15:22:44 +0000 Subject: [PATCH] Move some code into source files. Make stb impl. --- CMakeLists.txt | 4 +++ include/resources/mesh.hpp | 38 ++++++-------------- include/resources/shader.hpp | 61 ++++---------------------------- include/resources/texture.hpp | 2 ++ include/scene.hpp | 5 +-- include/systems/render.hpp | 2 +- include/window.hpp | 5 +-- src/libs/stb_image.cpp | 2 ++ src/resources/mesh.cpp | 47 +++++++++++++++++++++++++ src/resources/shader.cpp | 66 +++++++++++++++++++++++++++++++++++ src/scene.cpp | 5 +++ src/scene_manager.cpp | 2 +- src/systems/render.cpp | 5 +++ src/util/files.cpp | 1 - src/util/model_loader.cpp | 4 +-- src/window.cpp | 5 +++ test/src/meshgen.hpp | 2 ++ 17 files changed, 160 insertions(+), 96 deletions(-) create mode 100644 src/libs/stb_image.cpp create mode 100644 src/resources/mesh.cpp create mode 100644 src/resources/shader.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 781056f..1d626ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,10 +12,14 @@ set(SRC_FILES "src/ecs_system.cpp" "src/application.cpp" + "src/libs/stb_image.cpp" + "src/systems/transform.cpp" "src/systems/render.cpp" + "src/resources/shader.cpp" "src/resources/material.cpp" + "src/resources/mesh.cpp" "src/resources/texture.cpp" "src/scene.cpp" diff --git a/include/resources/mesh.hpp b/include/resources/mesh.hpp index 86922f9..6111cf2 100644 --- a/include/resources/mesh.hpp +++ b/include/resources/mesh.hpp @@ -1,16 +1,16 @@ #pragma once -#include "log.hpp" - #include "gfx.hpp" -#include "gfx_device.hpp" #include #include -#include +#include namespace engine { + + class GFXDevice; + struct Vertex { glm::vec3 pos; glm::vec3 norm; @@ -23,31 +23,19 @@ namespace engine::resources { class Mesh { public: - Mesh(GFXDevice* gfx, const std::vector& vertices) - : m_gfx(gfx) - { - std::vector indices(vertices.size()); - for (uint32_t i = 0; i < indices.size(); i++) { - indices[i] = i; - } - initMesh(vertices, indices); - } + Mesh(GFXDevice* gfx, const std::vector& vertices); Mesh(GFXDevice* gfx, const std::vector& vertices, const std::vector& indices) : m_gfx(gfx) { initMesh(vertices, indices); } - ~Mesh() - { - m_gfx->destroyBuffer(m_ib); - m_gfx->destroyBuffer(m_vb); - } + ~Mesh(); Mesh(const Mesh&) = delete; Mesh& operator=(const Mesh&) = delete; - auto getVB() { return m_vb; } - auto getIB() { return m_ib; } - auto getCount() { return m_count; } + const gfx::Buffer* getVB(); + const gfx::Buffer* getIB(); + uint32_t getCount(); private: GFXDevice* const m_gfx; @@ -56,13 +44,7 @@ private: const gfx::Buffer* m_ib; uint32_t m_count; - void initMesh(const std::vector& vertices, const std::vector& indices) - { - m_vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, vertices.size() * sizeof(Vertex), vertices.data()); - m_ib = m_gfx->createBuffer(gfx::BufferType::INDEX, indices.size() * sizeof(uint32_t), indices.data()); - m_count = indices.size(); - INFO("Loaded mesh, vertices: {}, indices: {}", vertices.size(), indices.size()); - } + void initMesh(const std::vector& vertices, const std::vector& indices); }; diff --git a/include/resources/shader.hpp b/include/resources/shader.hpp index abacbb4..97e3338 100644 --- a/include/resources/shader.hpp +++ b/include/resources/shader.hpp @@ -1,13 +1,10 @@ #pragma once -#include "log.hpp" - #include "gfx.hpp" -#include "gfx_device.hpp" -#include - -#include +namespace engine { + class GFXDevice; +} namespace engine::resources { @@ -26,58 +23,12 @@ public: bool hasColor; }; - Shader(GFXDevice* gfx, const char* vertPath, const char* fragPath, const VertexParams& vertexParams, bool alphaBlending, bool cullBackFace) - : m_gfx(gfx) - { - int index = 0; - uint32_t stride = 0; - gfx::VertexFormat vertFormat{}; - - vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride); - stride += 3 * sizeof(float); - - if (vertexParams.hasNormal) { - vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride); - stride += 3 * sizeof(float); - } - if (vertexParams.hasUV0) { - vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride); - stride += 2 * sizeof(float); - } - if (vertexParams.hasUV1) { - vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride); - stride += 2 * sizeof(float); - } - if (vertexParams.hasUV2) { - vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride); - stride += 2 * sizeof(float); - } - if (vertexParams.hasUV3) { - vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride); - stride += 2 * sizeof(float); - } - if (vertexParams.hasTangent) { - vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride); - stride += 3 * sizeof(float); - } - if (vertexParams.hasColor) { - vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride); - stride += 3 * sizeof(float); - } - vertFormat.stride = stride; - - m_pipeline = m_gfx->createPipeline(vertPath, fragPath, vertFormat, sizeof(glm::mat4), alphaBlending, cullBackFace); - - INFO("Loaded shader: {}, vertex attribs: {}", vertPath, vertFormat.attributeDescriptions.size()); - } - ~Shader() - { - m_gfx->destroyPipeline(m_pipeline); - } + Shader(GFXDevice* gfx, const char* vertPath, const char* fragPath, const VertexParams& vertexParams, bool alphaBlending, bool cullBackFace); + ~Shader(); Shader(const Shader&) = delete; Shader& operator=(const Shader&) = delete; - const gfx::Pipeline* getPipeline() { return m_pipeline; } + const gfx::Pipeline* getPipeline(); private: GFXDevice* const m_gfx; diff --git a/include/resources/texture.hpp b/include/resources/texture.hpp index 07c8a4b..249483b 100644 --- a/include/resources/texture.hpp +++ b/include/resources/texture.hpp @@ -2,6 +2,8 @@ #include "gfx_device.hpp" +#include + namespace engine::resources { class Texture { diff --git a/include/scene.hpp b/include/scene.hpp index 59d1227..b197747 100644 --- a/include/scene.hpp +++ b/include/scene.hpp @@ -56,10 +56,7 @@ namespace engine { uint32_t getEntity(const std::string& tag, uint32_t parent = 0); - size_t getComponentSignaturePosition(size_t hash) - { - return m_componentSignaturePositions.at(hash); - } + size_t getComponentSignaturePosition(size_t hash); template void registerComponent() diff --git a/include/systems/render.hpp b/include/systems/render.hpp index 1ea9dae..5f8f29b 100644 --- a/include/systems/render.hpp +++ b/include/systems/render.hpp @@ -16,7 +16,7 @@ namespace engine { void onUpdate(float ts) override; - void setCameraEntity(uint32_t entity) { m_camera.camEntity = entity; } + void setCameraEntity(uint32_t entity); private: struct { diff --git a/include/window.hpp b/include/window.hpp index 8505e3e..5d92a4a 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -63,10 +63,7 @@ namespace engine { // Returns true if the window was just resized during the previous frame bool getWindowResized() const; // Set the window resized flag (to recalculate aspect ratios and such) - inline void setResizedFlag() - { - m_justResized = true; - } + void setResizedFlag(); // keyboard events diff --git a/src/libs/stb_image.cpp b/src/libs/stb_image.cpp new file mode 100644 index 0000000..badb3ef --- /dev/null +++ b/src/libs/stb_image.cpp @@ -0,0 +1,2 @@ +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" \ No newline at end of file diff --git a/src/resources/mesh.cpp b/src/resources/mesh.cpp new file mode 100644 index 0000000..8376518 --- /dev/null +++ b/src/resources/mesh.cpp @@ -0,0 +1,47 @@ +#include "resources/mesh.hpp" + +#include "log.hpp" +#include "gfx_device.hpp" + +namespace engine::resources { + + Mesh::Mesh(GFXDevice* gfx, const std::vector& vertices) + : m_gfx(gfx) + { + std::vector indices(vertices.size()); + for (uint32_t i = 0; i < indices.size(); i++) { + indices[i] = i; + } + initMesh(vertices, indices); + } + + Mesh::~Mesh() + { + m_gfx->destroyBuffer(m_ib); + m_gfx->destroyBuffer(m_vb); + } + + const gfx::Buffer* Mesh::getVB() + { + return m_vb; + } + + const gfx::Buffer* Mesh::getIB() + { + return m_ib; + } + + uint32_t Mesh::getCount() + { + return m_count; + } + + void Mesh::initMesh(const std::vector& vertices, const std::vector& indices) + { + m_vb = m_gfx->createBuffer(gfx::BufferType::VERTEX, vertices.size() * sizeof(Vertex), vertices.data()); + m_ib = m_gfx->createBuffer(gfx::BufferType::INDEX, indices.size() * sizeof(uint32_t), indices.data()); + m_count = (uint32_t)indices.size(); + INFO("Loaded mesh, vertices: {}, indices: {}", vertices.size(), indices.size()); + } + +} diff --git a/src/resources/shader.cpp b/src/resources/shader.cpp new file mode 100644 index 0000000..26d4ec6 --- /dev/null +++ b/src/resources/shader.cpp @@ -0,0 +1,66 @@ +#include "resources/shader.hpp" + +#include "gfx_device.hpp" +#include "log.hpp" + +#include + +namespace engine::resources { + + + Shader::Shader(GFXDevice* gfx, const char* vertPath, const char* fragPath, const VertexParams& vertexParams, bool alphaBlending, bool cullBackFace) + : m_gfx(gfx) + { + int index = 0; + uint32_t stride = 0; + gfx::VertexFormat vertFormat{}; + + vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride); + stride += 3 * sizeof(float); + + if (vertexParams.hasNormal) { + vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride); + stride += 3 * sizeof(float); + } + if (vertexParams.hasUV0) { + vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride); + stride += 2 * sizeof(float); + } + if (vertexParams.hasUV1) { + vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride); + stride += 2 * sizeof(float); + } + if (vertexParams.hasUV2) { + vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride); + stride += 2 * sizeof(float); + } + if (vertexParams.hasUV3) { + vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC2, stride); + stride += 2 * sizeof(float); + } + if (vertexParams.hasTangent) { + vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride); + stride += 3 * sizeof(float); + } + if (vertexParams.hasColor) { + vertFormat.attributeDescriptions.emplace_back(index++, gfx::VertexAttribFormat::VEC3, stride); + stride += 3 * sizeof(float); + } + vertFormat.stride = stride; + + m_pipeline = m_gfx->createPipeline(vertPath, fragPath, vertFormat, sizeof(glm::mat4), alphaBlending, cullBackFace); + + INFO("Loaded shader: {}, vertex attribs: {}", vertPath, vertFormat.attributeDescriptions.size()); + } + + Shader::~Shader() + { + m_gfx->destroyPipeline(m_pipeline); + } + + const gfx::Pipeline* Shader::getPipeline() + { + return m_pipeline; + } + +} diff --git a/src/scene.cpp b/src/scene.cpp index 1a0982e..26cc73f 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -41,6 +41,11 @@ namespace engine { return getSystem()->getChildEntity(parent, tag); } + size_t Scene::getComponentSignaturePosition(size_t hash) + { + return m_componentSignaturePositions.at(hash); + } + void Scene::update(float ts) { diff --git a/src/scene_manager.cpp b/src/scene_manager.cpp index 62477ba..e8210a1 100644 --- a/src/scene_manager.cpp +++ b/src/scene_manager.cpp @@ -18,7 +18,7 @@ namespace engine { { auto scene = std::make_unique(m_app); m_scenes.emplace_back(std::move(scene)); - m_activeSceneIndex = m_scenes.size() - 1; + m_activeSceneIndex = (int)m_scenes.size() - 1; return m_scenes.back().get(); } diff --git a/src/systems/render.cpp b/src/systems/render.cpp index e10f1a2..fc48b56 100644 --- a/src/systems/render.cpp +++ b/src/systems/render.cpp @@ -79,5 +79,10 @@ namespace engine { } + void RenderSystem::setCameraEntity(uint32_t entity) + { + { m_camera.camEntity = entity; } + } + } diff --git a/src/util/files.cpp b/src/util/files.cpp index 28d939c..8ec8118 100644 --- a/src/util/files.cpp +++ b/src/util/files.cpp @@ -1,6 +1,5 @@ #include "util/files.hpp" -#define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" #include diff --git a/src/util/model_loader.cpp b/src/util/model_loader.cpp index 7305d8b..745f029 100644 --- a/src/util/model_loader.cpp +++ b/src/util/model_loader.cpp @@ -181,8 +181,8 @@ namespace engine::util { absPath = absPath.parent_path(); absPath /= texPath.C_Str(); try { - textures[i] = std::make_shared(parent->app()->gfx(), absPath); - } catch (const std::runtime_error& e) { + textures[i] = std::make_shared(parent->app()->gfx(), absPath.string()); + } catch (const std::runtime_error&) { textures[i] = parent->getResource("textures/white.png"); } } diff --git a/src/window.cpp b/src/window.cpp index 0e275ca..1d4b1ca 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -243,6 +243,11 @@ namespace engine { return m_justResized; } + void Window::setResizedFlag() + { + m_justResized = true; + } + void Window::show() { SDL_ShowWindow(m_handle); diff --git a/test/src/meshgen.hpp b/test/src/meshgen.hpp index 724587e..6fd186d 100644 --- a/test/src/meshgen.hpp +++ b/test/src/meshgen.hpp @@ -2,6 +2,8 @@ #include "resources/mesh.hpp" +#include + // generates a UV sphere std::unique_ptr genSphereMesh(engine::GFXDevice* gfx, float r, int detail, bool windInside = false, bool flipNormals = false); std::unique_ptr genCuboidMesh(engine::GFXDevice* gfx, float x, float y, float z);