Begin refactor (broken builds)

This commit is contained in:
bailehuni 2024-06-01 23:03:39 +01:00
parent 52f4203e9e
commit 774e80aa8e
54 changed files with 282 additions and 325 deletions

View File

@ -1,7 +1,7 @@
--- ---
Language: Cpp Language: Cpp
# BasedOnStyle: Google # BasedOnStyle: Google
AccessModifierOffset: -1 AccessModifierOffset: -4
AlignAfterOpenBracket: Align AlignAfterOpenBracket: Align
AlignArrayOfStructures: None AlignArrayOfStructures: None
AlignConsecutiveAssignments: AlignConsecutiveAssignments:

View File

@ -43,70 +43,69 @@ set(SRC_FILES
"src/libs/weldmesh.c" "src/libs/weldmesh.c"
"src/libs/weldmesh.h" "src/libs/weldmesh.h"
"src/renderer.cpp" "src/renderer.cpp"
"src/resources/font.cpp" "src/resource_font.cpp"
"src/resources/material.cpp" "src/resource_material.cpp"
"src/resources/mesh.cpp" "src/resource_mesh.cpp"
"src/resources/shader.cpp" "src/resource_shader.cpp"
"src/resources/texture.cpp" "src/resource_texture.cpp"
"src/scene.cpp" "src/scene.cpp"
"src/scene_manager.cpp" "src/scene_manager.cpp"
"src/systems/collisions.cpp" "src/system_collisions.cpp"
"src/systems/custom_behaviour.cpp" "src/system_custom_behaviour.cpp"
"src/systems/mesh_render_system.cpp" "src/system_mesh_render.cpp"
"src/systems/ui_render_system.cpp" "src/system_transform.cpp"
"src/systems/transform.cpp" "src/files.cpp"
"src/util/files.cpp" "src/gen_tangents.cpp"
"src/util/gen_tangents.cpp" "src/gltf_loader.cpp"
"src/util/gltf_loader.cpp" "src/vulkan_device.cpp"
"src/vulkan/device.cpp" "src/vulkan_allocator.cpp"
"src/vulkan/device.h" "src/vulkan_instance.cpp"
"src/vulkan/gpu_allocator.cpp" "src/vulkan_swapchain.cpp"
"src/vulkan/gpu_allocator.h"
"src/vulkan/instance.cpp"
"src/vulkan/instance.h"
"src/vulkan/swapchain.cpp"
"src/vulkan/swapchain.h"
"src/window.cpp" "src/window.cpp"
"src/util/file_dialog.cpp") "src/file_dialog.cpp"
)
set(INCLUDE_FILES set(INCLUDE_FILES
"include/application.h" "include/application.h"
"include/application_component.h" "include/application_component.h"
"include/components/collider.h" "include/component_collider.h"
"include/components/custom.h" "include/component_custom.h"
"include/components/mesh_renderable.h" "include/component_mesh.h"
"include/components/ui_renderable.h" "include/component_transform.h"
"include/components/transform.h"
"include/ecs.h" "include/ecs.h"
"include/engine_api.h" "include/vulkan_allocator.h"
"include/vulkan_device.h"
"include/vulkan_instance.h"
"include/vulkan_swapchain.h"
"include/event_system.h" "include/event_system.h"
"include/gfx.h" "include/gfx.h"
"include/gfx_device.h" "include/gfx_device.h"
"include/input_manager.h" "include/input_manager.h"
"include/inputs/keyboard.h" "include/input_keys.h"
"include/inputs/mouse.h" "include/input_mouse.h"
"include/log.h" "include/log.h"
"include/logger.h" "include/logger.h"
"include/renderer.h" "include/renderer.h"
"include/resource_manager.h" "include/resource_manager.h"
"include/resources/font.h" "include/resource_font.h"
"include/resources/material.h" "include/resource_material.h"
"include/resources/mesh.h" "include/resource_mesh.h"
"include/resources/shader.h" "include/resource_shader.h"
"include/resources/texture.h" "include/resource_texture.h"
"include/scene.h" "include/scene.h"
"include/scene_manager.h" "include/scene_manager.h"
"include/systems/collisions.h" "include/system_collisions.h"
"include/systems/custom_behaviour.h" "include/system_custom_behaviour.h"
"include/systems/mesh_render_system.h" "include/system_mesh_render.h"
"include/systems/ui_render_system.h" "include/system_transform.h"
"include/systems/transform.h"
"include/util.h" "include/util.h"
"include/util/files.h" "include/files.h"
"include/util/gen_tangents.h" "include/gen_tangents.h"
"include/util/gltf_loader.h" "include/gltf_loader.h"
"include/window.h" "include/window.h"
"include/util/file_dialog.h") "include/file_dialog.h"
)
file(GLOB_RECURSE RES_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/res/engine/*") file(GLOB_RECURSE RES_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/res/engine/*")

View File

@ -119,6 +119,19 @@ private:
}; };
` `
Structs are laid out as follows:
`
struct Y {
/* static data members */
/* non-static data members */
/* static methods */
/* non-static methods */
};
`
Avoid having access specifiers in structs.
Structs should almost always be POD. If not, consider using a class instead.
Inlined methods are to be avoided as much as possible. Even for trivial getters/setters. Inlined methods are to be avoided as much as possible. Even for trivial getters/setters.
Inline methods are only used when doing so provides a tangible performance gain. Inline methods are only used when doing so provides a tangible performance gain.
@ -127,3 +140,22 @@ and copy/move assignment operators explicitly deleted.
Ensure that constructors with a single argument are marked `explicit` unless implicit construction Ensure that constructors with a single argument are marked `explicit` unless implicit construction
is intended. is intended.
################################################################################
######################
# INCLUDE DIRECTIVES #
######################
Header files should be included in the following order:
- Header corresponding to source file
- C standard library includes (as, e.g., cstdint)
- C++ STL includes
- External libraries from, e.g., 'dependencies/' directory or Vulkan SDK. Use angle brackets.
- Libraries in the source tree. Use double quotes. (TODO: Move these into 'dependencies/')
- Header files from the current project. Use double quotes.
Each section should be sorted alphabetically (In VS: highlight the includes, right-click -> #include directives -> sort).

View File

@ -0,0 +1,12 @@
#pragma once
#include <functional>
namespace engine {
struct CustomComponent {
std::function<void(void)> on_init; // void on_init(void);
std::function<void(float)> on_update; // void on_update(float ts);
};
} // namespace engine

View File

@ -1,12 +0,0 @@
#pragma once
#include <functional>
namespace engine {
struct CustomComponent {
std::function<void(void)> onInit; // void onInit(void);
std::function<void(float)> onUpdate; // void onUpdate(float ts);
};
} // namespace engine

View File

@ -1,9 +0,0 @@
#pragma once
namespace engine {
struct UIRenderableComponent {
int x;
};
} // namespace engine

View File

@ -1,16 +0,0 @@
#pragma once
/*
#ifndef ENGINE_API
# ifdef _MSC_VER
# ifdef ENGINE_EXPORTS
# define ENGINE_API __declspec(dllexport)
# else
# define ENGINE_API __declspec(dllimport)
# endif
# else
# define ENGINE_API
# endif
#endif
*/
#define ENGINE_API

View File

@ -1,11 +1,12 @@
#pragma once #pragma once
#include <cstdint>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
namespace engine { namespace engine {
namespace util {
std::unique_ptr<std::vector<char>> ReadTextFile(const std::string& path); std::unique_ptr<std::vector<char>> ReadTextFile(const std::string& path);
std::unique_ptr<std::vector<uint8_t>> ReadBinaryFile(const std::string& path); std::unique_ptr<std::vector<uint8_t>> ReadBinaryFile(const std::string& path);
@ -14,5 +15,4 @@ std::unique_ptr<std::vector<uint8_t>> ReadBinaryFile(const std::string& path);
// minimum. Output format is R8G8B8A8_UINT // minimum. Output format is R8G8B8A8_UINT
std::unique_ptr<std::vector<uint8_t>> ReadImageFile(const std::string& path, int& width, int& height); std::unique_ptr<std::vector<uint8_t>> ReadImageFile(const std::string& path, int& width, int& height);
} // namespace util
} // namespace engine } // namespace engine

View File

@ -2,8 +2,7 @@
// Keyboard scancodes, taken from SDL_scancode.h // Keyboard scancodes, taken from SDL_scancode.h
namespace engine { namespace engine::inputs {
namespace inputs {
enum class Key : int { enum class Key : int {
K_UNKNOWN = 0, K_UNKNOWN = 0,
@ -372,5 +371,4 @@ enum class Key : int {
for array bounds */ for array bounds */
}; };
} // namespace inputs } // namespace engine::inputs
} // namespace engine

View File

@ -1,11 +1,9 @@
#pragma once #pragma once
namespace engine { namespace engine::inputs {
namespace inputs {
enum class MouseButton : int { M_LEFT, M_MIDDLE, M_RIGHT, M_X1, M_X2, M_INVALID = 7, M_SIZE = 7 }; enum class MouseButton : int { M_LEFT, M_MIDDLE, M_RIGHT, M_X1, M_X2, M_INVALID = 7, M_SIZE = 7 };
enum class MouseAxis : int { X, Y, X_SCR, Y_SCR }; enum class MouseAxis : int { X, Y, X_SCR, Y_SCR };
} // namespace inputs } // namespace engine::inputs
} // namespace engine

View File

@ -10,20 +10,22 @@
namespace engine { namespace engine {
class Font { class Font {
std::unique_ptr<stbtt_fontinfo> m_font_info{};
std::unique_ptr<std::vector<uint8_t>> m_font_buffer{};
std::map<int, int> m_unicode_to_glyph{};
public: public:
Font(const std::string& path); explicit Font(const std::string& path);
~Font();
Font(const Font&) = delete; Font(const Font&) = delete;
~Font();
Font& operator=(const Font&) = delete; Font& operator=(const Font&) = delete;
std::unique_ptr<std::vector<uint8_t>> GetTextBitmap(const std::string& text, float height_px, int& width_out, int& height_out); std::unique_ptr<std::vector<uint8_t>> getTextBitmap(const std::string& text, float height_px, int& width_out, int& height_out);
private: private:
std::unique_ptr<stbtt_fontinfo> font_info_{}; int getGlyphIndex(int unicode_codepoint);
std::unique_ptr<std::vector<uint8_t>> font_buffer_{};
std::map<int, int> unicode_to_glyph_{};
int GetGlyphIndex(int unicode_codepoint);
}; };
} // namespace engine } // namespace engine

View File

@ -0,0 +1,36 @@
#pragma once
#include <memory>
#include "resources/shader.h"
#include "resources/texture.h"
// a material is just a shader with assigned textures/parameters
namespace engine {
// copyable
class Material {
const std::shared_ptr<Shader> m_shader;
std::shared_ptr<Texture> m_texture_albedo;
std::shared_ptr<Texture> m_texture_normal;
std::shared_ptr<Texture> m_texture_occlusion_roughness_metallic;
const gfx::DescriptorSet* m_material_set = nullptr;
Renderer* const m_renderer;
public:
Material(Renderer* renderer, std::shared_ptr<engine::Shader> shader);
Material(const Material&) = delete;
~Material();
Material& operator=(const Material&) = delete;
void setAlbedoTexture(std::shared_ptr<Texture> texture);
void setNormalTexture(std::shared_ptr<Texture> texture);
void setOcclusionRoughnessMetallicTexture(std::shared_ptr<Texture> texture);
const gfx::DescriptorSet* getDescriptorSet() { return m_material_set; }
Shader* getShader() { return m_shader.get(); }
};
} // namespace engine

View File

@ -1,9 +1,10 @@
#pragma once #pragma once
#include <vector>
#include <glm/vec2.hpp> #include <glm/vec2.hpp>
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
#include <glm/vec4.hpp> #include <glm/vec4.hpp>
#include <vector>
#include "gfx.h" #include "gfx.h"
#include "gfx_device.h" #include "gfx_device.h"
@ -15,33 +16,31 @@ struct Vertex {
glm::vec3 norm; glm::vec3 norm;
glm::vec4 tangent; // w component flips binormal if -1. w should be 1 or -1 glm::vec4 tangent; // w component flips binormal if -1. w should be 1 or -1
glm::vec2 uv; glm::vec2 uv;
static constexpr int FloatsPerVertex() { return static_cast<int>(sizeof(Vertex) / sizeof(float)); }
static constexpr int floatsPerVertex() { return static_cast<int>(sizeof(Vertex) / sizeof(float)); }
}; };
} // namespace engine
namespace engine {
class Mesh { class Mesh {
GFXDevice* const m_gfx;
const gfx::Buffer* m_vb;
const gfx::Buffer* m_ib;
uint32_t m_count;
public: public:
Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices); Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices);
Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices); Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices);
~Mesh();
Mesh(const Mesh&) = delete; Mesh(const Mesh&) = delete;
~Mesh();
Mesh& operator=(const Mesh&) = delete; Mesh& operator=(const Mesh&) = delete;
const gfx::Buffer* GetVB(); const gfx::Buffer* getVB();
const gfx::Buffer* GetIB(); const gfx::Buffer* getIB();
uint32_t GetCount(); uint32_t getCount();
private: private:
GFXDevice* const gfx_; void initMesh(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices);
const gfx::Buffer* vb_;
const gfx::Buffer* ib_;
uint32_t count_;
void InitMesh(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices);
}; };
} // namespace engine } // namespace engine

View File

@ -1,37 +0,0 @@
#pragma once
#include <memory>
#include "resources/shader.h"
#include "resources/texture.h"
// a material is just a shader with assigned textures/parameters
namespace engine {
// copyable
class Material {
public:
Material(Renderer* renderer, std::shared_ptr<engine::Shader> shader);
~Material();
Material& operator=(const Material&) = delete;
void SetAlbedoTexture(std::shared_ptr<Texture> texture);
void SetNormalTexture(std::shared_ptr<Texture> texture);
void SetOcclusionRoughnessMetallicTexture(std::shared_ptr<Texture> texture);
const gfx::DescriptorSet* GetDescriptorSet() { return material_set_; }
Shader* GetShader() { return shader_.get(); }
private:
const std::shared_ptr<Shader> shader_;
std::shared_ptr<Texture> texture_albedo_;
std::shared_ptr<Texture> texture_normal_;
std::shared_ptr<Texture> texture_occlusion_roughness_metallic_;
const gfx::DescriptorSet* material_set_ = nullptr;
Renderer* const renderer_;
};
} // namespace engine

View File

@ -1,22 +0,0 @@
#pragma once
#include <glm/mat4x4.hpp>
#include "ecs.h"
#include "scene.h"
#include "gfx.h"
namespace engine {
class UIRenderSystem : public System {
public:
UIRenderSystem(Scene* scene);
~UIRenderSystem();
void OnComponentInsert(Entity entity) override;
void OnUpdate(float ts) override;
private:
};
} // namespace engine

View File

@ -1,12 +1,11 @@
#include "application.h" #include "application.h"
#include <cinttypes>
#include <filesystem> #include <filesystem>
#include <memory> #include <memory>
#include <numeric>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <thread> #include <thread>
#include <numeric>
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
@ -14,6 +13,8 @@
#include "imgui/imgui_impl_sdl2.h" #include "imgui/imgui_impl_sdl2.h"
#include "imgui/imgui_impl_vulkan.h" #include "imgui/imgui_impl_vulkan.h"
#include "components/collider.h"
#include "components/transform.h"
#include "gfx.h" #include "gfx.h"
#include "gfx_device.h" #include "gfx_device.h"
#include "input_manager.h" #include "input_manager.h"
@ -23,15 +24,13 @@
#include "resources/mesh.h" #include "resources/mesh.h"
#include "resources/shader.h" #include "resources/shader.h"
#include "resources/texture.h" #include "resources/texture.h"
#include "systems/mesh_render_system.h"
#include "components/transform.h"
#include "components/collider.h"
#include "scene.h" #include "scene.h"
#include "scene_manager.h" #include "scene_manager.h"
#include "window.h" #include "systems/collisions.h"
#include "util/gltf_loader.h" #include "systems/mesh_render_system.h"
#include "util/file_dialog.h" #include "util/file_dialog.h"
#include <systems/collisions.h> #include "util/gltf_loader.h"
#include "window.h"
static struct ImGuiThings { static struct ImGuiThings {
ImGuiContext* context; ImGuiContext* context;
@ -145,9 +144,9 @@ Application::Application(const char* appName, const char* appVersion, gfx::Graph
/* default materials */ /* default materials */
{ {
auto defaultMaterial = std::make_unique<Material>(renderer(), GetResource<Shader>("builtin.fancy")); auto defaultMaterial = std::make_unique<Material>(renderer(), GetResource<Shader>("builtin.fancy"));
defaultMaterial->SetAlbedoTexture(GetResource<Texture>("builtin.white")); defaultMaterial->setAlbedoTexture(GetResource<Texture>("builtin.white"));
defaultMaterial->SetNormalTexture(GetResource<Texture>("builtin.normal")); defaultMaterial->setNormalTexture(GetResource<Texture>("builtin.normal"));
defaultMaterial->SetOcclusionRoughnessMetallicTexture(GetResource<Texture>("builtin.mr")); defaultMaterial->setOcclusionRoughnessMetallicTexture(GetResource<Texture>("builtin.mr"));
GetResourceManager<Material>()->AddPersistent("builtin.default", std::move(defaultMaterial)); GetResourceManager<Material>()->AddPersistent("builtin.default", std::move(defaultMaterial));
} }
} }

View File

@ -65,7 +65,7 @@ std::vector<uint32_t> engine::util::GenTangents(std::vector<engine::Vertex>& ver
std::vector<Vertex> vertex_data_out(vertices.size()); // initialised to zeros std::vector<Vertex> vertex_data_out(vertices.size()); // initialised to zeros
const int new_vertex_count = WeldMesh(reinterpret_cast<int*>(remap_table.data()), reinterpret_cast<float*>(vertex_data_out.data()), reinterpret_cast<float*>(vertices.data()), const int new_vertex_count = WeldMesh(reinterpret_cast<int*>(remap_table.data()), reinterpret_cast<float*>(vertex_data_out.data()), reinterpret_cast<float*>(vertices.data()),
static_cast<int>(vertices.size()), Vertex::FloatsPerVertex()); static_cast<int>(vertices.size()), Vertex::floatsPerVertex());
assert(new_vertex_count >= 0); assert(new_vertex_count >= 0);
// get new vertices into the vector. // get new vertices into the vector.

View File

@ -248,10 +248,10 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
materials.emplace_back(std::make_shared<Material>(scene.app()->renderer(), scene.app()->GetResource<Shader>("builtin.fancy"))); materials.emplace_back(std::make_shared<Material>(scene.app()->renderer(), scene.app()->GetResource<Shader>("builtin.fancy")));
// base color // base color
materials.back()->SetAlbedoTexture(scene.app()->GetResource<Texture>("builtin.white")); materials.back()->setAlbedoTexture(scene.app()->GetResource<Texture>("builtin.white"));
if (material.pbrMetallicRoughness.baseColorTexture.index != -1) { if (material.pbrMetallicRoughness.baseColorTexture.index != -1) {
if (material.pbrMetallicRoughness.baseColorTexture.texCoord == 0) { if (material.pbrMetallicRoughness.baseColorTexture.texCoord == 0) {
materials.back()->SetAlbedoTexture(textures.at(material.pbrMetallicRoughness.baseColorTexture.index)); materials.back()->setAlbedoTexture(textures.at(material.pbrMetallicRoughness.baseColorTexture.index));
} }
else { else {
LOG_WARN("Material {} base color texture specifies a UV channel other than zero which is unsupported.", material.name); LOG_WARN("Material {} base color texture specifies a UV channel other than zero which is unsupported.", material.name);
@ -269,16 +269,16 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
samplerInfo.anisotropic_filtering = false; samplerInfo.anisotropic_filtering = false;
colour_textures.emplace(std::make_pair(c, std::make_shared<Texture>(scene.app()->renderer(), pixel, 1, 1, samplerInfo, true))); colour_textures.emplace(std::make_pair(c, std::make_shared<Texture>(scene.app()->renderer(), pixel, 1, 1, samplerInfo, true)));
} }
materials.back()->SetAlbedoTexture(colour_textures.at(c)); materials.back()->setAlbedoTexture(colour_textures.at(c));
} }
// occlusion roughness metallic // occlusion roughness metallic
materials.back()->SetOcclusionRoughnessMetallicTexture( materials.back()->setOcclusionRoughnessMetallicTexture(
scene.app()->GetResource<Texture>("builtin.white")); // default ao = 1.0, rough = 1.0, metal = 1.0 scene.app()->GetResource<Texture>("builtin.white")); // default ao = 1.0, rough = 1.0, metal = 1.0
if (material.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) { if (material.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) {
if (material.pbrMetallicRoughness.metallicRoughnessTexture.texCoord == 0) { if (material.pbrMetallicRoughness.metallicRoughnessTexture.texCoord == 0) {
LOG_DEBUG("Setting occlusion roughness metallic texture!"); LOG_DEBUG("Setting occlusion roughness metallic texture!");
materials.back()->SetOcclusionRoughnessMetallicTexture(textures.at(material.pbrMetallicRoughness.metallicRoughnessTexture.index)); materials.back()->setOcclusionRoughnessMetallicTexture(textures.at(material.pbrMetallicRoughness.metallicRoughnessTexture.index));
} }
else { else {
LOG_WARN("Material {} metallic roughness texture specifies a UV channel other than zero which is unsupported.", material.name); LOG_WARN("Material {} metallic roughness texture specifies a UV channel other than zero which is unsupported.", material.name);
@ -298,7 +298,7 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
samplerInfo.anisotropic_filtering = false; samplerInfo.anisotropic_filtering = false;
metal_rough_textures.emplace(std::make_pair(mr, std::make_shared<Texture>(scene.app()->renderer(), pixel, 1, 1, samplerInfo, false))); metal_rough_textures.emplace(std::make_pair(mr, std::make_shared<Texture>(scene.app()->renderer(), pixel, 1, 1, samplerInfo, false)));
} }
materials.back()->SetOcclusionRoughnessMetallicTexture(metal_rough_textures.at(mr)); materials.back()->setOcclusionRoughnessMetallicTexture(metal_rough_textures.at(mr));
} }
// occlusion texture // occlusion texture
@ -316,10 +316,10 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
} }
// normal map // normal map
materials.back()->SetNormalTexture(scene.app()->GetResource<Texture>("builtin.normal")); materials.back()->setNormalTexture(scene.app()->GetResource<Texture>("builtin.normal"));
if (material.normalTexture.index != -1) { if (material.normalTexture.index != -1) {
if (material.normalTexture.texCoord == 0) { if (material.normalTexture.texCoord == 0) {
materials.back()->SetNormalTexture(textures.at(material.normalTexture.index)); materials.back()->setNormalTexture(textures.at(material.normalTexture.index));
} }
else { else {
LOG_WARN("Material {} normal texture specifies a UV channel other than zero which is unsupported.", material.name); LOG_WARN("Material {} normal texture specifies a UV channel other than zero which is unsupported.", material.name);
@ -521,7 +521,7 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
std::vector<Vertex> vertex_data_out(num_indices); // initialised to zeros std::vector<Vertex> vertex_data_out(num_indices); // initialised to zeros
const int num_unq_vertices = WeldMesh(remap_table.data(), reinterpret_cast<float*>(vertex_data_out.data()), const int num_unq_vertices = WeldMesh(remap_table.data(), reinterpret_cast<float*>(vertex_data_out.data()),
reinterpret_cast<float*>(vertices.data()), static_cast<int>(num_indices), Vertex::FloatsPerVertex()); reinterpret_cast<float*>(vertices.data()), static_cast<int>(num_indices), Vertex::floatsPerVertex());
assert(num_unq_vertices >= 0); assert(num_unq_vertices >= 0);
// get new vertices into the vector // get new vertices into the vector

View File

@ -585,7 +585,7 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG
return true; return true;
} }
// For reference, the average total _allocation count_ for a table is: // For reference, the average total _allocation m_count for a table is:
// + 0 (for ImGuiTable instance, we are pooling allocations in g.Tables[]) // + 0 (for ImGuiTable instance, we are pooling allocations in g.Tables[])
// + 1 (for table->RawData allocated below) // + 1 (for table->RawData allocated below)
// + 1 (for table->ColumnsNames, if names are used) // + 1 (for table->ColumnsNames, if names are used)

View File

@ -12,10 +12,10 @@ namespace engine {
Font::Font(const std::string& path) Font::Font(const std::string& path)
{ {
font_buffer_ = util::ReadBinaryFile(path); m_font_buffer = util::ReadBinaryFile(path);
font_info_ = std::make_unique<stbtt_fontinfo>(); m_font_info = std::make_unique<stbtt_fontinfo>();
if (stbtt_InitFont(font_info_.get(), font_buffer_->data(), 0) == 0) { if (stbtt_InitFont(m_font_info.get(), m_font_buffer->data(), 0) == 0) {
throw std::runtime_error("Failed to initialise font!"); throw std::runtime_error("Failed to initialise font!");
} }
@ -24,12 +24,12 @@ Font::Font(const std::string& path)
Font::~Font() { LOG_DEBUG("Destroyed font"); } Font::~Font() { LOG_DEBUG("Destroyed font"); }
std::unique_ptr<std::vector<uint8_t>> Font::GetTextBitmap(const std::string& text, float height_px, int& width_out, int& height_out) std::unique_ptr<std::vector<uint8_t>> Font::getTextBitmap(const std::string& text, float height_px, int& width_out, int& height_out)
{ {
const float sf = stbtt_ScaleForPixelHeight(font_info_.get(), height_px); const float sf = stbtt_ScaleForPixelHeight(m_font_info.get(), height_px);
int ascent, descent, line_gap; int ascent, descent, line_gap;
stbtt_GetFontVMetrics(font_info_.get(), &ascent, &descent, &line_gap); stbtt_GetFontVMetrics(m_font_info.get(), &ascent, &descent, &line_gap);
struct CharacterRenderInfo { struct CharacterRenderInfo {
int advance; // bitmap advance int advance; // bitmap advance
@ -42,10 +42,10 @@ std::unique_ptr<std::vector<uint8_t>> Font::GetTextBitmap(const std::string& tex
int width = 0; int width = 0;
for (size_t i = 0; i < text.size(); i++) { for (size_t i = 0; i < text.size(); i++) {
const int glyph_index = GetGlyphIndex(static_cast<int>(text.at(i))); const int glyph_index = getGlyphIndex(static_cast<int>(text.at(i)));
int advanceWidth, leftSideBearing; int advanceWidth, leftSideBearing;
stbtt_GetGlyphHMetrics(font_info_.get(), glyph_index, &advanceWidth, &leftSideBearing); stbtt_GetGlyphHMetrics(m_font_info.get(), glyph_index, &advanceWidth, &leftSideBearing);
if (i == 0 && leftSideBearing < 0) { if (i == 0 && leftSideBearing < 0) {
// if the character extends before the current point // if the character extends before the current point
@ -57,7 +57,7 @@ std::unique_ptr<std::vector<uint8_t>> Font::GetTextBitmap(const std::string& tex
renderInfo.advance = static_cast<int>(static_cast<float>(advanceWidth) * sf); renderInfo.advance = static_cast<int>(static_cast<float>(advanceWidth) * sf);
if (stbtt_IsGlyphEmpty(font_info_.get(), glyph_index) == 0) { if (stbtt_IsGlyphEmpty(m_font_info.get(), glyph_index) == 0) {
renderInfo.isEmpty = false; renderInfo.isEmpty = false;
} }
else { else {
@ -66,7 +66,7 @@ std::unique_ptr<std::vector<uint8_t>> Font::GetTextBitmap(const std::string& tex
if (!renderInfo.isEmpty) { if (!renderInfo.isEmpty) {
renderInfo.bitmap = renderInfo.bitmap =
stbtt_GetGlyphBitmap(font_info_.get(), sf, sf, glyph_index, &renderInfo.width, &renderInfo.height, &renderInfo.xoff, &renderInfo.yoff); stbtt_GetGlyphBitmap(m_font_info.get(), sf, sf, glyph_index, &renderInfo.width, &renderInfo.height, &renderInfo.xoff, &renderInfo.yoff);
} }
characterRenderInfos.push_back(renderInfo); characterRenderInfos.push_back(renderInfo);
@ -114,17 +114,17 @@ std::unique_ptr<std::vector<uint8_t>> Font::GetTextBitmap(const std::string& tex
return bitmap; return bitmap;
} }
int Font::GetGlyphIndex(int unicode_codepoint) int Font::getGlyphIndex(int unicode_codepoint)
{ {
if (unicode_to_glyph_.contains(unicode_codepoint)) { if (m_unicode_to_glyph.contains(unicode_codepoint)) {
return unicode_to_glyph_.at(unicode_codepoint); return m_unicode_to_glyph.at(unicode_codepoint);
} }
else { else {
const int glyph_index = stbtt_FindGlyphIndex(font_info_.get(), unicode_codepoint); const int glyph_index = stbtt_FindGlyphIndex(m_font_info.get(), unicode_codepoint);
if (glyph_index == 0) { if (glyph_index == 0) {
throw std::runtime_error("Glyph not found in font!"); throw std::runtime_error("Glyph not found in font!");
} }
unicode_to_glyph_.emplace(std::make_pair(unicode_codepoint, glyph_index)); m_unicode_to_glyph.emplace(std::make_pair(unicode_codepoint, glyph_index));
return glyph_index; return glyph_index;
} }
} }

33
src/resource_material.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "resources/material.h"
#include "resources/shader.h"
namespace engine {
Material::Material(Renderer* renderer, std::shared_ptr<Shader> shader) : m_shader(shader), m_renderer(renderer)
{
m_material_set = renderer->GetDevice()->AllocateDescriptorSet(renderer->GetMaterialSetLayout());
LOG_DEBUG("Created material");
}
Material::~Material() { m_renderer->GetDevice()->FreeDescriptorSet(m_material_set); LOG_DEBUG("Destroyed material"); }
void Material::setAlbedoTexture(std::shared_ptr<Texture> texture)
{
m_renderer->GetDevice()->UpdateDescriptorCombinedImageSampler(m_material_set, 0, texture->GetImage(), texture->GetSampler());
m_texture_albedo = texture;
}
void Material::setNormalTexture(std::shared_ptr<Texture> texture)
{
m_renderer->GetDevice()->UpdateDescriptorCombinedImageSampler(m_material_set, 1, texture->GetImage(), texture->GetSampler());
m_texture_normal = texture;
}
void Material::setOcclusionRoughnessMetallicTexture(std::shared_ptr<Texture> texture)
{
m_renderer->GetDevice()->UpdateDescriptorCombinedImageSampler(m_material_set, 2, texture->GetImage(), texture->GetSampler());
m_texture_occlusion_roughness_metallic = texture;
}
} // namespace engine

40
src/resource_mesh.cpp Normal file
View File

@ -0,0 +1,40 @@
#include "resources/mesh.h"
#include "log.h"
#include "gfx_device.h"
namespace engine {
Mesh::Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices) : m_gfx(gfx)
{
std::vector<uint32_t> indices(vertices.size());
for (uint32_t i = 0; i < indices.size(); i++) {
indices[i] = i;
}
initMesh(vertices, indices);
}
Mesh::Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices) : m_gfx(gfx) { initMesh(vertices, indices); }
Mesh::~Mesh()
{
m_gfx->DestroyBuffer(m_ib);
m_gfx->DestroyBuffer(m_vb);
LOG_DEBUG("Destroyed mesh");
}
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<Vertex>& vertices, const std::vector<uint32_t>& indices)
{
m_vb = m_gfx->CreateBuffer(gfx::BufferType::kVertex, vertices.size() * sizeof(Vertex), vertices.data());
m_ib = m_gfx->CreateBuffer(gfx::BufferType::kIndex, indices.size() * sizeof(uint32_t), indices.data());
m_count = (uint32_t)indices.size();
LOG_DEBUG("Created mesh, vertices: {}, indices: {}", vertices.size(), indices.size());
}
} // namespace engine

View File

@ -1,33 +0,0 @@
#include "resources/material.h"
#include "resources/shader.h"
namespace engine {
Material::Material(Renderer* renderer, std::shared_ptr<Shader> shader) : shader_(shader), renderer_(renderer)
{
material_set_ = renderer->GetDevice()->AllocateDescriptorSet(renderer->GetMaterialSetLayout());
LOG_DEBUG("Created material");
}
Material::~Material() { renderer_->GetDevice()->FreeDescriptorSet(material_set_); LOG_DEBUG("Destroyed material"); }
void Material::SetAlbedoTexture(std::shared_ptr<Texture> texture)
{
renderer_->GetDevice()->UpdateDescriptorCombinedImageSampler(material_set_, 0, texture->GetImage(), texture->GetSampler());
texture_albedo_ = texture;
}
void Material::SetNormalTexture(std::shared_ptr<Texture> texture)
{
renderer_->GetDevice()->UpdateDescriptorCombinedImageSampler(material_set_, 1, texture->GetImage(), texture->GetSampler());
texture_normal_ = texture;
}
void Material::SetOcclusionRoughnessMetallicTexture(std::shared_ptr<Texture> texture)
{
renderer_->GetDevice()->UpdateDescriptorCombinedImageSampler(material_set_, 2, texture->GetImage(), texture->GetSampler());
texture_occlusion_roughness_metallic_ = texture;
}
} // namespace engine

View File

@ -1,40 +0,0 @@
#include "resources/mesh.h"
#include "log.h"
#include "gfx_device.h"
namespace engine {
Mesh::Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices) : gfx_(gfx)
{
std::vector<uint32_t> indices(vertices.size());
for (uint32_t i = 0; i < indices.size(); i++) {
indices[i] = i;
}
InitMesh(vertices, indices);
}
Mesh::Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices) : gfx_(gfx) { InitMesh(vertices, indices); }
Mesh::~Mesh()
{
gfx_->DestroyBuffer(ib_);
gfx_->DestroyBuffer(vb_);
LOG_DEBUG("Destroyed mesh");
}
const gfx::Buffer* Mesh::GetVB() { return vb_; }
const gfx::Buffer* Mesh::GetIB() { return ib_; }
uint32_t Mesh::GetCount() { return count_; }
void Mesh::InitMesh(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices)
{
vb_ = gfx_->CreateBuffer(gfx::BufferType::kVertex, vertices.size() * sizeof(Vertex), vertices.data());
ib_ = gfx_->CreateBuffer(gfx::BufferType::kIndex, indices.size() * sizeof(uint32_t), indices.data());
count_ = (uint32_t)indices.size();
LOG_DEBUG("Created mesh, vertices: {}, indices: {}", vertices.size(), indices.size());
}
} // namespace engine

View File

@ -22,12 +22,12 @@ void CustomBehaviourSystem::OnUpdate(float ts) {
assert(c != nullptr); assert(c != nullptr);
bool& entity_initialised = entity_is_initialised_.at(entity); bool& entity_initialised = entity_is_initialised_.at(entity);
if (entity_initialised == false) { if (entity_initialised == false) {
if (c->onInit == nullptr) throw std::runtime_error("CustomComponent::onInit not set! Entity: " + std::to_string(entity)); if (c->on_init == nullptr) throw std::runtime_error("CustomComponent::on_init not set! Entity: " + std::to_string(entity));
if (c->onUpdate == nullptr) throw std::runtime_error("CustomComponent::onUpdate not set! Entity: " + std::to_string(entity)); if (c->on_update == nullptr) throw std::runtime_error("CustomComponent::on_update not set! Entity: " + std::to_string(entity));
c->onInit(); c->on_init();
entity_initialised = true; entity_initialised = true;
} }
c->onUpdate(ts); c->on_update(ts);
} }
} }

View File

@ -52,17 +52,17 @@ void MeshRenderSystem::BuildRenderList(RenderList& render_list, bool with_static
if (renderable->visible == false) continue; if (renderable->visible == false) continue;
const gfx::Pipeline* pipeline = renderable->material->GetShader()->GetPipeline(); const gfx::Pipeline* pipeline = renderable->material->getShader()->GetPipeline();
render_list.emplace_back(RenderListEntry{.pipeline = pipeline, render_list.emplace_back(RenderListEntry{.pipeline = pipeline,
.vertex_buffer = renderable->mesh->GetVB(), .vertex_buffer = renderable->mesh->getVB(),
.index_buffer = renderable->mesh->GetIB(), .index_buffer = renderable->mesh->getIB(),
.material_set = renderable->material->GetDescriptorSet(), .material_set = renderable->material->getDescriptorSet(),
.model_matrix = transform->world_matrix, .model_matrix = transform->world_matrix,
.index_count = renderable->mesh->GetCount()}); .index_count = renderable->mesh->getCount()});
if (render_orders.contains(pipeline) == false) { if (render_orders.contains(pipeline) == false) {
render_orders.emplace(pipeline, renderable->material->GetShader()->GetRenderOrder()); render_orders.emplace(pipeline, renderable->material->getShader()->GetRenderOrder());
} }
} }

View File

@ -1,22 +0,0 @@
#include "systems/ui_render_system.h"
#include "components/transform.h"
#include "components/ui_renderable.h"
namespace engine {
UIRenderSystem::UIRenderSystem(Scene* scene)
: System(scene, { typeid(TransformComponent).hash_code(),
typeid(UIRenderableComponent).hash_code() }) {}
UIRenderSystem::~UIRenderSystem() {}
void UIRenderSystem::OnComponentInsert(Entity entity) {
(void)entity;
}
void UIRenderSystem::OnUpdate(float ts) {
(void)ts;
}
} // namespace engine

View File

@ -139,8 +139,8 @@ void PlayGame(GameSettings settings)
cube_ren->mesh = GenCuboidMesh(app.renderer()->GetDevice(), 1.0f, 1.0f, 1.0f); cube_ren->mesh = GenCuboidMesh(app.renderer()->GetDevice(), 1.0f, 1.0f, 1.0f);
cube_ren->visible = true; cube_ren->visible = true;
auto cubeCustom = main_scene->AddComponent<engine::CustomComponent>(cube); auto cubeCustom = main_scene->AddComponent<engine::CustomComponent>(cube);
cubeCustom->onInit = [] {}; cubeCustom->on_init = [] {};
cubeCustom->onUpdate = [&main_scene, cube](float dt) { cubeCustom->on_update = [&main_scene, cube](float dt) {
static float yaw = 0.0f; static float yaw = 0.0f;
yaw += dt; yaw += dt;
main_scene->GetRotation(cube) = glm::angleAxis(yaw, glm::vec3{0.0f, 0.0f, 1.0f}); main_scene->GetRotation(cube) = glm::angleAxis(yaw, glm::vec3{0.0f, 0.0f, 1.0f});