Do some more reformatting

This commit is contained in:
bailehuni 2024-06-04 19:01:49 +01:00
parent 9c4e36ee91
commit 9b5fca62f5
30 changed files with 357 additions and 320 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@ Debug/
Release/ Release/
MinSizeRel/ MinSizeRel/
CMakeSettings.json CMakeSettings.json
CMakeUserPresets.json
compile_commands.json compile_commands.json
runme runme

View File

@ -5,7 +5,7 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# options # options
option(ENGINE_BUILD_TEST "Compile the test program" ON) option(ENGINE_BUILD_TEST "Compile the test program" ON)
SET(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo") set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo")
# enable link time code generation for all targets in the solution # enable link time code generation for all targets in the solution
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE) set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
@ -19,7 +19,11 @@ set(SRC_FILES
"src/application.cpp" "src/application.cpp"
"src/application_component.cpp" "src/application_component.cpp"
"src/ecs.cpp" "src/ecs.cpp"
"src/file_dialog.cpp"
"src/files.cpp"
"src/gen_tangents.cpp"
"src/gfx_device_vulkan.cpp" "src/gfx_device_vulkan.cpp"
"src/gltf_loader.cpp"
"src/input_manager.cpp" "src/input_manager.cpp"
"src/renderer.cpp" "src/renderer.cpp"
"src/resource_font.cpp" "src/resource_font.cpp"
@ -33,15 +37,11 @@ set(SRC_FILES
"src/system_custom_behaviour.cpp" "src/system_custom_behaviour.cpp"
"src/system_mesh_render.cpp" "src/system_mesh_render.cpp"
"src/system_transform.cpp" "src/system_transform.cpp"
"src/files.cpp"
"src/gen_tangents.cpp"
"src/gltf_loader.cpp"
"src/vulkan_device.cpp"
"src/vulkan_allocator.cpp" "src/vulkan_allocator.cpp"
"src/vulkan_device.cpp"
"src/vulkan_instance.cpp" "src/vulkan_instance.cpp"
"src/vulkan_swapchain.cpp" "src/vulkan_swapchain.cpp"
"src/window.cpp" "src/window.cpp"
"src/file_dialog.cpp"
) )
set(INCLUDE_FILES set(INCLUDE_FILES
@ -51,22 +51,24 @@ set(INCLUDE_FILES
"include/component_custom.h" "include/component_custom.h"
"include/component_mesh.h" "include/component_mesh.h"
"include/component_transform.h" "include/component_transform.h"
"include/debug_line.h"
"include/ecs.h" "include/ecs.h"
"include/vulkan_allocator.h" "include/entity.h"
"include/vulkan_device.h"
"include/vulkan_instance.h"
"include/vulkan_swapchain.h"
"include/event_system.h" "include/event_system.h"
"include/file_dialog.h"
"include/files.h"
"include/gen_tangents.h"
"include/gfx.h" "include/gfx.h"
"include/gfx_device.h" "include/gfx_device.h"
"include/input_manager.h" "include/gltf_loader.h"
"include/input_keys.h" "include/input_keys.h"
"include/input_manager.h"
"include/input_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_font.h" "include/resource_font.h"
"include/resource_manager.h"
"include/resource_material.h" "include/resource_material.h"
"include/resource_mesh.h" "include/resource_mesh.h"
"include/resource_shader.h" "include/resource_shader.h"
@ -78,36 +80,34 @@ set(INCLUDE_FILES
"include/system_mesh_render.h" "include/system_mesh_render.h"
"include/system_transform.h" "include/system_transform.h"
"include/util.h" "include/util.h"
"include/files.h" "include/vulkan_allocator.h"
"include/gen_tangents.h" "include/vulkan_device.h"
"include/gltf_loader.h" "include/vulkan_instance.h"
"include/vulkan_swapchain.h"
"include/window.h" "include/window.h"
"include/file_dialog.h"
) )
file(GLOB_RECURSE RES_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/res/engine/*")
add_library(${PROJECT_NAME} STATIC add_library(${PROJECT_NAME} STATIC
${SRC_FILES} ${SRC_FILES}
${INCLUDE_FILES} ${INCLUDE_FILES}
${RES_FILES} ${RES_FILES}
) )
# show game engine resources in VS generated solution explorer
file(GLOB_RECURSE RES_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/res/engine/*")
# Organise files in VS generated solution explorer
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "Source" FILES ${SRC_FILES}) source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "Source" FILES ${SRC_FILES})
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/include" PREFIX "Include" FILES ${INCLUDE_FILES}) source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/include" PREFIX "Include" FILES ${INCLUDE_FILES})
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/res/engine" PREFIX "Resources" FILES ${RES_FILES}) source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/res/engine" PREFIX "Resources" FILES ${RES_FILES})
# compiling options: # compiling options:
target_compile_definitions(${PROJECT_NAME} PRIVATE DEFINITIONS "ENGINE_EXPORTS")
if (WIN32) if (WIN32)
target_compile_definitions(${PROJECT_NAME} PRIVATE DEFINITIONS "NOMINMAX") # stop windows.h conflicting with 'std::max' target_compile_definitions(${PROJECT_NAME} PRIVATE DEFINITIONS "NOMINMAX") # stop windows.h conflicting with 'std::max'
endif() endif()
set_property(TARGET ${PROJECT_NAME} PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON) # This project uses C++20 and C11
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 20) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 20)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_EXTENSIONS OFF) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_EXTENSIONS OFF)
@ -133,7 +133,9 @@ target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
# Build the test # Build the test
if (ENGINE_BUILD_TEST) if (ENGINE_BUILD_TEST)
add_subdirectory(test) add_subdirectory(test)
# Copy DLLs to the enginetest.exe folder (I think that's what this does?)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:enginetest>) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $<TARGET_FILE_DIR:enginetest>)
# Set enginetest.exe as the default startup project in VS
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT enginetest) set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT enginetest)
endif() endif()
@ -151,7 +153,8 @@ add_subdirectory(vendor/volk)
target_link_libraries(${PROJECT_NAME} PUBLIC volk::volk) target_link_libraries(${PROJECT_NAME} PUBLIC volk::volk)
# Vulkan Memory Allocator # Vulkan Memory Allocator
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC vendor/VulkanMemoryAllocator/include) add_subdirectory(vendor/VulkanMemoryAllocator)
target_link_libraries(${PROJECT_NAME} PUBLIC VulkanMemoryAllocator)
# shaderc # shaderc
if (MSVC) if (MSVC)
@ -169,22 +172,20 @@ set(SDL_STATIC OFF CACHE INTERNAL "" FORCE)
set(SDL_TEST OFF CACHE INTERNAL "" FORCE) set(SDL_TEST OFF CACHE INTERNAL "" FORCE)
set(BUILD_SHARED_LIBS ON) set(BUILD_SHARED_LIBS ON)
add_subdirectory(vendor/SDL) add_subdirectory(vendor/SDL)
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC vendor/SDL/include) # TODO maybe delete this line?
target_link_libraries(${PROJECT_NAME} PUBLIC SDL2::SDL2) target_link_libraries(${PROJECT_NAME} PUBLIC SDL2::SDL2)
target_link_libraries(${PROJECT_NAME} PUBLIC SDL2::SDL2main) target_link_libraries(${PROJECT_NAME} PUBLIC SDL2::SDL2main)
# GLM: # GLM:
set(BUILD_SHARED_LIBS OFF) set(BUILD_SHARED_LIBS OFF)
add_subdirectory(vendor/glm) add_subdirectory(vendor/glm)
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC vendor/glm) target_link_libraries(${PROJECT_NAME} PUBLIC glm::glm)
target_compile_definitions(${PROJECT_NAME} PUBLIC GLM_ENABLE_EXPERIMENTAL)
# spdlog # spdlog
set(SPDLOG_BUILD_SHARED OFF CACHE INTERNAL "" FORCE) set(SPDLOG_BUILD_SHARED OFF)
set(SPDLOG_SYSTEM_INCLUDES ON CACHE INTERNAL "" FORCE) set(SPDLOG_SYSTEM_INCLUDES ON)
set(BUILD_SHARED_LIBS OFF) set(BUILD_SHARED_LIBS OFF)
add_subdirectory(vendor/spdlog) # automatically calls target_include_directories add_subdirectory(vendor/spdlog)
target_link_libraries(${PROJECT_NAME} PUBLIC spdlog) target_link_libraries(${PROJECT_NAME} PUBLIC spdlog::spdlog)
# stb # stb
add_subdirectory(vendor/stb) add_subdirectory(vendor/stb)

View File

@ -5,15 +5,17 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include "debug_line.h"
#include "gfx.h" #include "gfx.h"
#include "input_manager.h"
#include "renderer.h"
#include "resource_manager.h" #include "resource_manager.h"
#include "scene_manager.h"
#include "window.h"
namespace engine { namespace engine {
class Window; // forward-dec
class InputManager; // forward-dec
class Renderer; // forward-dec
class SceneManager; // forward-dec
struct AppConfiguration { struct AppConfiguration {
bool enable_frame_limiter; bool enable_frame_limiter;
}; };
@ -23,14 +25,15 @@ private:
std::unique_ptr<Window> m_window; std::unique_ptr<Window> m_window;
std::unique_ptr<InputManager> m_input_manager; std::unique_ptr<InputManager> m_input_manager;
std::unique_ptr<Renderer> m_renderer; std::unique_ptr<Renderer> m_renderer;
std::unique_ptr<SceneManager> m_scene_manager;
std::unordered_map<size_t, std::unique_ptr<IResourceManager>> m_resource_managers{}; std::unordered_map<size_t, std::unique_ptr<IResourceManager>> m_resource_managers{};
std::filesystem::path m_resources_path; std::filesystem::path m_resources_path;
std::unique_ptr<SceneManager> m_scene_manager;
AppConfiguration m_configuration; AppConfiguration m_configuration;
public: public:
const char* const app_name; const char* const app_name;
const char* const app_version; const char* const app_version;
std::vector<Line> debug_lines{}; std::vector<DebugLine> debug_lines{};
public: public:
Application(const char* app_name, const char* app_version, gfx::GraphicsSettings graphics_settings, AppConfiguration configuration); Application(const char* app_name, const char* app_version, gfx::GraphicsSettings graphics_settings, AppConfiguration configuration);
@ -48,10 +51,10 @@ public:
std::shared_ptr<T> getResource(const std::string& name); std::shared_ptr<T> getResource(const std::string& name);
void gameLoop(); void gameLoop();
void setFrameLimiter(bool on) { m_configuration.enable_frame_limiter = on; } void setFrameLimiter(bool on) { m_configuration.enable_frame_limiter = on; }
Window* window() { return m_window.get(); } Window* getWindow() { return m_window.get(); }
InputManager* input_manager() { return m_input_manager.get(); } InputManager* getInputManager() { return m_input_manager.get(); }
SceneManager* scene_manager() { return m_scene_manager.get(); } SceneManager* getSceneManager() { return m_scene_manager.get(); }
Renderer* renderer() { return m_renderer.get(); } Renderer* getRenderer() { return m_renderer.get(); }
std::string getResourcePath(const std::string relative_path) const { return (m_resources_path / relative_path).string(); } std::string getResourcePath(const std::string relative_path) const { return (m_resources_path / relative_path).string(); }
private: private:

View File

@ -8,23 +8,26 @@ namespace engine {
class Application; // forward-dec class Application; // forward-dec
// a class that extends many classes in the engine to expose 'global' functionality
class ApplicationComponent { class ApplicationComponent {
// a class that extends many classes in the engine to expose 'global' functionality to lower-tier classes
// if multithreading is ever implemented, this class must do synchronization as its instantiations may run concurrently
private: private:
Application& app_; Application& m_app;
protected:
ApplicationComponent(Application& app) : app_(app) {}
std::string GetResourcePath(const std::string& relative_path) const;
SDL_Window* GetWindowHandle() const;
const char* GetAppName() const;
const char* GetAppVersion() const;
public: public:
ApplicationComponent() = delete; ApplicationComponent() = delete;
ApplicationComponent(const ApplicationComponent&) = delete;
~ApplicationComponent() = default;
ApplicationComponent& operator=(const ApplicationComponent&) = delete;
protected:
ApplicationComponent(Application& app) : m_app(app) {}
std::string getResourcePath(const std::string& relative_path) const;
SDL_Window* getWindowHandle() const;
const char* getAppName() const;
const char* getAppVersion() const;
}; };
} // namespace engine } // namespace engine

View File

@ -2,11 +2,11 @@
#include <memory> #include <memory>
#include "resource_material.h"
#include "resource_mesh.h"
namespace engine { namespace engine {
class Mesh; // forward-dec
class Material; // forward-dec
struct MeshRenderableComponent { struct MeshRenderableComponent {
std::shared_ptr<Mesh> mesh; std::shared_ptr<Mesh> mesh;
std::shared_ptr<Material> material; std::shared_ptr<Material> material;

13
include/debug_line.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
#include <glm/vec3.hpp>
namespace engine {
struct DebugLine {
glm::vec3 pos1;
glm::vec3 pos2;
glm::vec3 color;
};
} // namespace engine

View File

@ -1,20 +1,21 @@
#pragma once #pragma once
#include <bitset>
#include <cassert> #include <cassert>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <bitset>
#include <map> #include <map>
#include <vector>
#include <set> #include <set>
#include <vector>
#include "entity.h"
namespace engine { namespace engine {
class Scene; class Scene; // forward-dec
using Entity = uint32_t; // ECS entity constexpr size_t MAX_COMPONENTS = 10;
constexpr size_t kMaxComponents = 10;
class IComponentArray { class IComponentArray {
public: public:
@ -23,49 +24,48 @@ class IComponentArray {
template <typename T> template <typename T>
class ComponentArray : public IComponentArray { class ComponentArray : public IComponentArray {
private:
std::vector<T> m_component_array{};
public: public:
void InsertData(Entity entity, const T& component) void insertData(Entity entity, const T& component)
{ {
if (component_array_.size() < entity + 1) { if (m_component_array.size() < entity + 1) {
component_array_.resize(entity + 1); m_component_array.resize(entity + 1);
} }
// bounds checking here as not performance critical // bounds checking here as not performance critical
component_array_.at(entity) = component; m_component_array.at(entity) = component;
} }
void DeleteData(Entity entity) void removeData(Entity entity)
{ {
(void)entity; // TODO (void)entity; // TODO
} }
T* GetData(Entity entity) T* getData(Entity entity)
{ {
assert(entity < component_array_.size()); assert(entity < m_component_array.size());
return &component_array_[entity]; return &m_component_array[entity];
} }
private:
std::vector<T> component_array_{};
}; };
class System { class System {
public:
Scene* const m_scene;
std::bitset<MAX_COMPONENTS> m_signature;
std::set<Entity> m_entities{}; // entities that contain the needed components
public: public:
System(Scene* scene, std::set<size_t> required_component_hashes); System(Scene* scene, std::set<size_t> required_component_hashes);
virtual ~System() {}
System(const System&) = delete; System(const System&) = delete;
virtual ~System() {};
System& operator=(const System&) = delete; System& operator=(const System&) = delete;
virtual void OnUpdate(float ts) = 0; virtual void onUpdate(float ts) = 0;
virtual void onComponentInsert(Entity) {}
virtual void OnComponentInsert(Entity) {} virtual void onComponentRemove(Entity) {}
virtual void OnComponentRemove(Entity) {}
Scene* const scene_;
std::bitset<kMaxComponents> signature_;
// entities that contain the needed components
std::set<Entity> entities_{};
}; };
} // namespace engine } // namespace engine

9
include/entity.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include <cstdint>
namespace engine {
using Entity = uint32_t;
}

View File

@ -9,6 +9,7 @@
#include "application_component.h" #include "application_component.h"
#include "gfx_device.h" #include "gfx_device.h"
#include "system_mesh_render.h" #include "system_mesh_render.h"
#include "debug_line.h"
namespace engine { namespace engine {
@ -23,12 +24,6 @@ struct UniformDescriptor {
gfx::UniformBuffer* uniform_buffer; gfx::UniformBuffer* uniform_buffer;
}; };
struct Line {
glm::vec3 pos1;
glm::vec3 pos2;
glm::vec3 color;
};
class Renderer : private ApplicationComponent { class Renderer : private ApplicationComponent {
public: public:
Renderer(Application& app, gfx::GraphicsSettings settings); Renderer(Application& app, gfx::GraphicsSettings settings);
@ -36,7 +31,7 @@ class Renderer : private ApplicationComponent {
~Renderer(); ~Renderer();
// staticList can be nullptr to render nothing // staticList can be nullptr to render nothing
void Render(bool window_is_resized, glm::mat4 camera_transform, const RenderList* static_list, const RenderList* dynamic_list, const std::vector<Line>& debug_lines); void Render(bool window_is_resized, glm::mat4 camera_transform, const RenderList* static_list, const RenderList* dynamic_list, const std::vector<DebugLine>& debug_lines);
// getters // getters

View File

@ -48,7 +48,7 @@ class Scene {
size_t signature_position = next_signature_position_; size_t signature_position = next_signature_position_;
++next_signature_position_; ++next_signature_position_;
assert(signature_position < kMaxComponents && "Registering too many components!"); assert(signature_position < MAX_COMPONENTS && "Registering too many components!");
assert(component_signature_positions_.contains(hash) == false); assert(component_signature_positions_.contains(hash) == false);
component_signature_positions_.emplace(hash, signature_position); component_signature_positions_.emplace(hash, signature_position);
} }
@ -65,7 +65,7 @@ class Scene {
} }
auto array = GetComponentArray<T>(); auto array = GetComponentArray<T>();
return array->GetData(entity); return array->getData(entity);
} }
// because GetComponent<Transformzzzzzz takes too long // because GetComponent<Transformzzzzzz takes too long
@ -83,7 +83,7 @@ class Scene {
size_t hash = typeid(T).hash_code(); size_t hash = typeid(T).hash_code();
auto array = GetComponentArray<T>(); auto array = GetComponentArray<T>();
array->InsertData(entity, comp); // errors if entity already exists in array array->insertData(entity, comp); // errors if entity already exists in array
// set the component bit for this entity // set the component bit for this entity
size_t signature_position = component_signature_positions_.at(hash); size_t signature_position = component_signature_positions_.at(hash);
@ -91,14 +91,14 @@ class Scene {
signature_ref.set(signature_position); signature_ref.set(signature_position);
for (auto& [system_hash, system] : ecs_systems_) { for (auto& [system_hash, system] : ecs_systems_) {
if (system->entities_.contains(entity)) continue; if (system->m_entities.contains(entity)) continue;
if ((system->signature_ & signature_ref) == system->signature_) { if ((system->m_signature & signature_ref) == system->m_signature) {
system->entities_.insert(entity); system->m_entities.insert(entity);
system->OnComponentInsert(entity); system->onComponentInsert(entity);
} }
} }
return array->GetData(entity); return array->getData(entity);
} }
template <typename T> template <typename T>
@ -148,7 +148,7 @@ class Scene {
// maps component hashes to signature positions // maps component hashes to signature positions
std::unordered_map<size_t, size_t> component_signature_positions_{}; std::unordered_map<size_t, size_t> component_signature_positions_{};
// maps entity ids to their signatures // maps entity ids to their signatures
std::unordered_map<Entity, std::bitset<kMaxComponents>> signatures_{}; std::unordered_map<Entity, std::bitset<MAX_COMPONENTS>> signatures_{};
// maps component hashes to their arrays // maps component hashes to their arrays
std::unordered_map<size_t, std::unique_ptr<IComponentArray>> component_arrays_{}; std::unordered_map<size_t, std::unique_ptr<IComponentArray>> component_arrays_{};

View File

@ -29,9 +29,9 @@ class CollisionSystem : public System {
public: public:
CollisionSystem(Scene* scene); CollisionSystem(Scene* scene);
void OnComponentInsert(Entity entity) override; void onComponentInsert(Entity entity) override;
void OnUpdate(float ts) override; void onUpdate(float ts) override;
Raycast GetRaycast(Ray ray); Raycast GetRaycast(Ray ray);

View File

@ -14,8 +14,8 @@ class CustomBehaviourSystem : public System {
CustomBehaviourSystem(Scene* scene); CustomBehaviourSystem(Scene* scene);
~CustomBehaviourSystem(); ~CustomBehaviourSystem();
void OnUpdate(float ts) override; void onUpdate(float ts) override;
void OnComponentInsert(Entity entity) override; void onComponentInsert(Entity entity) override;
private: private:
std::unordered_map<Entity, bool> entity_is_initialised_{}; std::unordered_map<Entity, bool> entity_is_initialised_{};

View File

@ -28,8 +28,8 @@ class MeshRenderSystem : public System {
const RenderList* GetStaticRenderList() const { return &static_render_list_; } const RenderList* GetStaticRenderList() const { return &static_render_list_; }
const RenderList* GetDynamicRenderList() const { return &dynamic_render_list_; } const RenderList* GetDynamicRenderList() const { return &dynamic_render_list_; }
void OnComponentInsert(Entity entity) override; void onComponentInsert(Entity entity) override;
void OnUpdate(float ts) override; void onUpdate(float ts) override;
private: private:
RenderList static_render_list_; RenderList static_render_list_;

View File

@ -9,7 +9,7 @@ class TransformSystem : public System {
public: public:
TransformSystem(Scene* scene); TransformSystem(Scene* scene);
void OnUpdate(float ts) override; void onUpdate(float ts) override;
/* /*
* Linear-searches for an entity that matches the arguments' criteria. * Linear-searches for an entity that matches the arguments' criteria.

View File

@ -24,6 +24,8 @@
#include "resource_mesh.h" #include "resource_mesh.h"
#include "resource_shader.h" #include "resource_shader.h"
#include "resource_texture.h" #include "resource_texture.h"
#include "renderer.h"
#include "debug_line.h"
#include "scene.h" #include "scene.h"
#include "scene_manager.h" #include "scene_manager.h"
#include "system_collisions.h" #include "system_collisions.h"
@ -95,7 +97,7 @@ Application::Application(const char* appName, const char* appVersion, gfx::Graph
shaderSettings.write_z = true; shaderSettings.write_z = true;
shaderSettings.render_order = 0; shaderSettings.render_order = 0;
auto fancyShader = auto fancyShader =
std::make_unique<Shader>(renderer(), getResourcePath("engine/shaders/fancy.vert"), getResourcePath("engine/shaders/fancy.frag"), shaderSettings); std::make_unique<Shader>(getRenderer(), getResourcePath("engine/shaders/fancy.vert"), getResourcePath("engine/shaders/fancy.frag"), shaderSettings);
getResourceManager<Shader>()->AddPersistent("builtin.fancy", std::move(fancyShader)); getResourceManager<Shader>()->AddPersistent("builtin.fancy", std::move(fancyShader));
} }
@ -107,7 +109,7 @@ Application::Application(const char* appName, const char* appVersion, gfx::Graph
samplerInfo.magnify = gfx::Filter::kNearest; samplerInfo.magnify = gfx::Filter::kNearest;
samplerInfo.mipmap = gfx::Filter::kNearest; samplerInfo.mipmap = gfx::Filter::kNearest;
samplerInfo.anisotropic_filtering = false; samplerInfo.anisotropic_filtering = false;
auto whiteTexture = std::make_unique<Texture>(renderer(), pixel, 1, 1, samplerInfo, true); auto whiteTexture = std::make_unique<Texture>(getRenderer(), pixel, 1, 1, samplerInfo, true);
getResourceManager<Texture>()->AddPersistent("builtin.white", std::move(whiteTexture)); getResourceManager<Texture>()->AddPersistent("builtin.white", std::move(whiteTexture));
} }
{ {
@ -117,7 +119,7 @@ Application::Application(const char* appName, const char* appVersion, gfx::Graph
samplerInfo.magnify = gfx::Filter::kNearest; samplerInfo.magnify = gfx::Filter::kNearest;
samplerInfo.mipmap = gfx::Filter::kNearest; samplerInfo.mipmap = gfx::Filter::kNearest;
samplerInfo.anisotropic_filtering = false; samplerInfo.anisotropic_filtering = false;
auto blackTexture = std::make_unique<Texture>(renderer(), pixel, 1, 1, samplerInfo, true); auto blackTexture = std::make_unique<Texture>(getRenderer(), pixel, 1, 1, samplerInfo, true);
getResourceManager<Texture>()->AddPersistent("builtin.black", std::move(blackTexture)); getResourceManager<Texture>()->AddPersistent("builtin.black", std::move(blackTexture));
} }
{ {
@ -127,7 +129,7 @@ Application::Application(const char* appName, const char* appVersion, gfx::Graph
samplerInfo.magnify = gfx::Filter::kNearest; samplerInfo.magnify = gfx::Filter::kNearest;
samplerInfo.mipmap = gfx::Filter::kNearest; samplerInfo.mipmap = gfx::Filter::kNearest;
samplerInfo.anisotropic_filtering = false; samplerInfo.anisotropic_filtering = false;
auto normalTexture = std::make_unique<Texture>(renderer(), pixel, 1, 1, samplerInfo, false); auto normalTexture = std::make_unique<Texture>(getRenderer(), pixel, 1, 1, samplerInfo, false);
getResourceManager<Texture>()->AddPersistent("builtin.normal", std::move(normalTexture)); getResourceManager<Texture>()->AddPersistent("builtin.normal", std::move(normalTexture));
} }
{ {
@ -137,13 +139,13 @@ Application::Application(const char* appName, const char* appVersion, gfx::Graph
samplerInfo.magnify = gfx::Filter::kNearest; samplerInfo.magnify = gfx::Filter::kNearest;
samplerInfo.mipmap = gfx::Filter::kNearest; samplerInfo.mipmap = gfx::Filter::kNearest;
samplerInfo.anisotropic_filtering = false; samplerInfo.anisotropic_filtering = false;
auto mrTexture = std::make_unique<Texture>(renderer(), pixel, 1, 1, samplerInfo, false); auto mrTexture = std::make_unique<Texture>(getRenderer(), pixel, 1, 1, samplerInfo, false);
getResourceManager<Texture>()->AddPersistent("builtin.mr", std::move(mrTexture)); getResourceManager<Texture>()->AddPersistent("builtin.mr", std::move(mrTexture));
} }
/* default materials */ /* default materials */
{ {
auto defaultMaterial = std::make_unique<Material>(renderer(), getResource<Shader>("builtin.fancy")); auto defaultMaterial = std::make_unique<Material>(getRenderer(), 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"));
@ -205,7 +207,7 @@ void Application::gameLoop()
if (now - lastTick >= 1000000000LL * 5LL) [[unlikely]] { if (now - lastTick >= 1000000000LL * 5LL) [[unlikely]] {
lastTick = now; lastTick = now;
LOG_DEBUG("fps: {}", std::lroundf(avg_fps)); LOG_DEBUG("fps: {}", std::lroundf(avg_fps));
renderer()->GetDevice()->LogPerformanceInfo(); getRenderer()->GetDevice()->LogPerformanceInfo();
m_window->ResetAvgFPS(); m_window->ResetAvgFPS();
} }
@ -327,84 +329,84 @@ void Application::gameLoop()
if (node.type1 == CollisionSystem::BiTreeNode::Type::Entity) { if (node.type1 == CollisionSystem::BiTreeNode::Type::Entity) {
const glm::vec3 col = const glm::vec3 col =
(node.type1 == CollisionSystem::BiTreeNode::Type::BoundingVolume) ? glm::vec3{1.0f, 0.0f, 0.0f} : glm::vec3{0.0f, 1.0f, 0.0f}; (node.type1 == CollisionSystem::BiTreeNode::Type::BoundingVolume) ? glm::vec3{1.0f, 0.0f, 0.0f} : glm::vec3{0.0f, 1.0f, 0.0f};
Line line1{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.min.z}, DebugLine line1{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.min.z},
glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.min.z}, col}; glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.min.z}, col};
debug_lines.push_back(line1); debug_lines.push_back(line1);
Line line2{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.min.z}, DebugLine line2{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.min.z},
glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.min.z}, col}; glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.min.z}, col};
debug_lines.push_back(line2); debug_lines.push_back(line2);
Line line3{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.min.z}, DebugLine line3{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.min.z},
glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.min.z}, col}; glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.min.z}, col};
debug_lines.push_back(line3); debug_lines.push_back(line3);
Line line4{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.min.z}, DebugLine line4{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.min.z},
glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.min.z}, col}; glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.min.z}, col};
debug_lines.push_back(line4); debug_lines.push_back(line4);
Line line5{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.min.z}, DebugLine line5{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.min.z},
glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.max.z}, col}; glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.max.z}, col};
debug_lines.push_back(line5); debug_lines.push_back(line5);
Line line6{glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.min.z}, DebugLine line6{glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.min.z},
glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.max.z}, col}; glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.max.z}, col};
debug_lines.push_back(line6); debug_lines.push_back(line6);
Line line7{glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.min.z}, DebugLine line7{glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.min.z},
glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.max.z}, col}; glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.max.z}, col};
debug_lines.push_back(line7); debug_lines.push_back(line7);
Line line8{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.min.z}, DebugLine line8{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.min.z},
glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.max.z}, col}; glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.max.z}, col};
debug_lines.push_back(line8); debug_lines.push_back(line8);
Line line9{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.max.z}, DebugLine line9{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.max.z},
glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.max.z}, col}; glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.max.z}, col};
debug_lines.push_back(line9); debug_lines.push_back(line9);
Line line10{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.max.z}, DebugLine line10{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.max.z},
glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.max.z}, col}; glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.max.z}, col};
debug_lines.push_back(line10); debug_lines.push_back(line10);
Line line11{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.max.z}, DebugLine line11{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.max.z},
glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.max.z}, col}; glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.max.z}, col};
debug_lines.push_back(line11); debug_lines.push_back(line11);
Line line12{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.max.z}, DebugLine line12{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.max.z},
glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.max.z}, col}; glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.max.z}, col};
debug_lines.push_back(line12); debug_lines.push_back(line12);
} }
if (node.type2 == CollisionSystem::BiTreeNode::Type::Entity) { if (node.type2 == CollisionSystem::BiTreeNode::Type::Entity) {
const glm::vec3 col = const glm::vec3 col =
(node.type2 == CollisionSystem::BiTreeNode::Type::BoundingVolume) ? glm::vec3{1.0f, 0.0f, 0.0f} : glm::vec3{0.0f, 1.0f, 0.0f}; (node.type2 == CollisionSystem::BiTreeNode::Type::BoundingVolume) ? glm::vec3{1.0f, 0.0f, 0.0f} : glm::vec3{0.0f, 1.0f, 0.0f};
Line line1{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.min.z}, DebugLine line1{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.min.z},
glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.min.z}, col}; glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.min.z}, col};
debug_lines.push_back(line1); debug_lines.push_back(line1);
Line line2{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.min.z}, DebugLine line2{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.min.z},
glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.min.z}, col}; glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.min.z}, col};
debug_lines.push_back(line2); debug_lines.push_back(line2);
Line line3{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.min.z}, DebugLine line3{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.min.z},
glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.min.z}, col}; glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.min.z}, col};
debug_lines.push_back(line3); debug_lines.push_back(line3);
Line line4{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.min.z}, DebugLine line4{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.min.z},
glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.min.z}, col}; glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.min.z}, col};
debug_lines.push_back(line4); debug_lines.push_back(line4);
Line line5{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.min.z}, DebugLine line5{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.min.z},
glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.max.z}, col}; glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.max.z}, col};
debug_lines.push_back(line5); debug_lines.push_back(line5);
Line line6{glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.min.z}, DebugLine line6{glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.min.z},
glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.max.z}, col}; glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.max.z}, col};
debug_lines.push_back(line6); debug_lines.push_back(line6);
Line line7{glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.min.z}, DebugLine line7{glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.min.z},
glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.max.z}, col}; glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.max.z}, col};
debug_lines.push_back(line7); debug_lines.push_back(line7);
Line line8{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.min.z}, DebugLine line8{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.min.z},
glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.max.z}, col}; glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.max.z}, col};
debug_lines.push_back(line8); debug_lines.push_back(line8);
Line line9{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.max.z}, DebugLine line9{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.max.z},
glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.max.z}, col}; glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.max.z}, col};
debug_lines.push_back(line9); debug_lines.push_back(line9);
Line line10{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.max.z}, DebugLine line10{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.max.z},
glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.max.z}, col}; glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.max.z}, col};
debug_lines.push_back(line10); debug_lines.push_back(line10);
Line line11{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.max.z}, DebugLine line11{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.max.z},
glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.max.z}, col}; glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.max.z}, col};
debug_lines.push_back(line11); debug_lines.push_back(line11);
Line line12{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.max.z}, DebugLine line12{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.max.z},
glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.max.z}, col}; glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.max.z}, col};
debug_lines.push_back(line12); debug_lines.push_back(line12);
} }
@ -417,84 +419,84 @@ void Application::gameLoop()
if (node.type1 == CollisionSystem::BiTreeNode::Type::BoundingVolume) { if (node.type1 == CollisionSystem::BiTreeNode::Type::BoundingVolume) {
const glm::vec3 col = const glm::vec3 col =
(node.type1 == CollisionSystem::BiTreeNode::Type::BoundingVolume) ? glm::vec3{1.0f, 0.0f, 0.0f} : glm::vec3{0.0f, 1.0f, 0.0f}; (node.type1 == CollisionSystem::BiTreeNode::Type::BoundingVolume) ? glm::vec3{1.0f, 0.0f, 0.0f} : glm::vec3{0.0f, 1.0f, 0.0f};
Line line1{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.min.z}, DebugLine line1{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.min.z},
glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.min.z}, col}; glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.min.z}, col};
debug_lines.push_back(line1); debug_lines.push_back(line1);
Line line2{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.min.z}, DebugLine line2{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.min.z},
glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.min.z}, col}; glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.min.z}, col};
debug_lines.push_back(line2); debug_lines.push_back(line2);
Line line3{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.min.z}, DebugLine line3{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.min.z},
glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.min.z}, col}; glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.min.z}, col};
debug_lines.push_back(line3); debug_lines.push_back(line3);
Line line4{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.min.z}, DebugLine line4{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.min.z},
glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.min.z}, col}; glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.min.z}, col};
debug_lines.push_back(line4); debug_lines.push_back(line4);
Line line5{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.min.z}, DebugLine line5{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.min.z},
glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.max.z}, col}; glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.max.z}, col};
debug_lines.push_back(line5); debug_lines.push_back(line5);
Line line6{glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.min.z}, DebugLine line6{glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.min.z},
glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.max.z}, col}; glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.max.z}, col};
debug_lines.push_back(line6); debug_lines.push_back(line6);
Line line7{glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.min.z}, DebugLine line7{glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.min.z},
glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.max.z}, col}; glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.max.z}, col};
debug_lines.push_back(line7); debug_lines.push_back(line7);
Line line8{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.min.z}, DebugLine line8{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.min.z},
glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.max.z}, col}; glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.max.z}, col};
debug_lines.push_back(line8); debug_lines.push_back(line8);
Line line9{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.max.z}, DebugLine line9{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.max.z},
glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.max.z}, col}; glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.max.z}, col};
debug_lines.push_back(line9); debug_lines.push_back(line9);
Line line10{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.max.z}, DebugLine line10{glm::vec3{node.box1.min.x, node.box1.min.y, node.box1.max.z},
glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.max.z}, col}; glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.max.z}, col};
debug_lines.push_back(line10); debug_lines.push_back(line10);
Line line11{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.max.z}, DebugLine line11{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.max.z},
glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.max.z}, col}; glm::vec3{node.box1.max.x, node.box1.min.y, node.box1.max.z}, col};
debug_lines.push_back(line11); debug_lines.push_back(line11);
Line line12{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.max.z}, DebugLine line12{glm::vec3{node.box1.max.x, node.box1.max.y, node.box1.max.z},
glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.max.z}, col}; glm::vec3{node.box1.min.x, node.box1.max.y, node.box1.max.z}, col};
debug_lines.push_back(line12); debug_lines.push_back(line12);
} }
if (node.type2 == CollisionSystem::BiTreeNode::Type::BoundingVolume) { if (node.type2 == CollisionSystem::BiTreeNode::Type::BoundingVolume) {
const glm::vec3 col = const glm::vec3 col =
(node.type2 == CollisionSystem::BiTreeNode::Type::BoundingVolume) ? glm::vec3{1.0f, 0.0f, 0.0f} : glm::vec3{0.0f, 1.0f, 0.0f}; (node.type2 == CollisionSystem::BiTreeNode::Type::BoundingVolume) ? glm::vec3{1.0f, 0.0f, 0.0f} : glm::vec3{0.0f, 1.0f, 0.0f};
Line line1{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.min.z}, DebugLine line1{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.min.z},
glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.min.z}, col}; glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.min.z}, col};
debug_lines.push_back(line1); debug_lines.push_back(line1);
Line line2{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.min.z}, DebugLine line2{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.min.z},
glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.min.z}, col}; glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.min.z}, col};
debug_lines.push_back(line2); debug_lines.push_back(line2);
Line line3{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.min.z}, DebugLine line3{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.min.z},
glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.min.z}, col}; glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.min.z}, col};
debug_lines.push_back(line3); debug_lines.push_back(line3);
Line line4{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.min.z}, DebugLine line4{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.min.z},
glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.min.z}, col}; glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.min.z}, col};
debug_lines.push_back(line4); debug_lines.push_back(line4);
Line line5{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.min.z}, DebugLine line5{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.min.z},
glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.max.z}, col}; glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.max.z}, col};
debug_lines.push_back(line5); debug_lines.push_back(line5);
Line line6{glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.min.z}, DebugLine line6{glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.min.z},
glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.max.z}, col}; glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.max.z}, col};
debug_lines.push_back(line6); debug_lines.push_back(line6);
Line line7{glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.min.z}, DebugLine line7{glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.min.z},
glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.max.z}, col}; glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.max.z}, col};
debug_lines.push_back(line7); debug_lines.push_back(line7);
Line line8{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.min.z}, DebugLine line8{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.min.z},
glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.max.z}, col}; glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.max.z}, col};
debug_lines.push_back(line8); debug_lines.push_back(line8);
Line line9{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.max.z}, DebugLine line9{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.max.z},
glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.max.z}, col}; glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.max.z}, col};
debug_lines.push_back(line9); debug_lines.push_back(line9);
Line line10{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.max.z}, DebugLine line10{glm::vec3{node.box2.min.x, node.box2.min.y, node.box2.max.z},
glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.max.z}, col}; glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.max.z}, col};
debug_lines.push_back(line10); debug_lines.push_back(line10);
Line line11{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.max.z}, DebugLine line11{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.max.z},
glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.max.z}, col}; glm::vec3{node.box2.max.x, node.box2.min.y, node.box2.max.z}, col};
debug_lines.push_back(line11); debug_lines.push_back(line11);
Line line12{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.max.z}, DebugLine line12{glm::vec3{node.box2.max.x, node.box2.max.y, node.box2.max.z},
glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.max.z}, col}; glm::vec3{node.box2.min.x, node.box2.max.y, node.box2.max.z}, col};
debug_lines.push_back(line12); debug_lines.push_back(line12);
} }
@ -506,7 +508,7 @@ void Application::gameLoop()
static_list = mesh_render_system->GetStaticRenderList(); static_list = mesh_render_system->GetStaticRenderList();
dynamic_list = mesh_render_system->GetDynamicRenderList(); dynamic_list = mesh_render_system->GetDynamicRenderList();
} }
m_renderer->Render(window()->GetWindowResized(), camera_transform, static_list, dynamic_list, debug_lines); m_renderer->Render(getWindow()->GetWindowResized(), camera_transform, static_list, dynamic_list, debug_lines);
debug_lines.clear(); // gets remade every frame :0 debug_lines.clear(); // gets remade every frame :0
/* poll events */ /* poll events */

View File

@ -7,17 +7,17 @@
namespace engine { namespace engine {
std::string ApplicationComponent::GetResourcePath(const std::string& relative_path) const { return app_.getResourcePath(relative_path); } std::string ApplicationComponent::getResourcePath(const std::string& relative_path) const { return m_app.getResourcePath(relative_path); }
SDL_Window* ApplicationComponent::GetWindowHandle() const { SDL_Window* ApplicationComponent::getWindowHandle() const {
return app_.window()->GetHandle(); return m_app.getWindow()->GetHandle();
} }
const char* ApplicationComponent::GetAppName() const { const char* ApplicationComponent::getAppName() const {
return app_.app_name; return m_app.app_name;
} }
const char* ApplicationComponent::GetAppVersion() const { const char* ApplicationComponent::getAppVersion() const {
return app_.app_version; return m_app.app_version;
} }
} // namespace engine } // namespace engine

View File

@ -5,11 +5,11 @@
namespace engine { namespace engine {
System::System(Scene* scene, std::set<size_t> requiredComponentHashes) System::System(Scene* scene, std::set<size_t> requiredComponentHashes)
: scene_(scene) : m_scene(scene)
{ {
for (size_t componentHash : requiredComponentHashes) { for (size_t componentHash : requiredComponentHashes) {
size_t componentSignaturePosition = scene_->GetComponentSignaturePosition(componentHash); size_t componentSignaturePosition = m_scene->GetComponentSignaturePosition(componentHash);
signature_.set(componentSignaturePosition); m_signature.set(componentSignaturePosition);
} }
} }

View File

@ -1,15 +1,17 @@
#include "gltf_loader.h" #include "gltf_loader.h"
#include "log.h"
#include "files.h"
#include <mikktspace.h> #include <mikktspace.h>
#include <weldmesh.h>
#include <tiny_gltf.h> #include <tiny_gltf.h>
#include <weldmesh.h>
#include "component_collider.h"
#include "component_mesh.h" #include "component_mesh.h"
#include "component_transform.h" #include "component_transform.h"
#include "component_collider.h" #include "files.h"
#include "log.h"
#include "renderer.h"
#include "resource_material.h"
#include "resource_texture.h"
struct Color { struct Color {
uint8_t r, g, b, a; uint8_t r, g, b, a;
@ -203,7 +205,7 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
const tg::Image& image = model.images.at(texture.source); const tg::Image& image = model.images.at(texture.source);
if (image.as_is == false && image.bits == 8 && image.component == 4 && image.pixel_type == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) { if (image.as_is == false && image.bits == 8 && image.component == 4 && image.pixel_type == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) {
// create texture on GPU // create texture on GPU
textures.back() = std::make_shared<Texture>(scene.app()->renderer(), image.image.data(), image.width, image.height, samplerInfo, textures.back() = std::make_shared<Texture>(scene.app()->getRenderer(), image.image.data(), image.width, image.height, samplerInfo,
tex_index_is_base_color[texture_idx]); tex_index_is_base_color[texture_idx]);
} }
else { else {
@ -245,7 +247,7 @@ 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()->getRenderer(), 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"));
@ -267,7 +269,7 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
samplerInfo.magnify = gfx::Filter::kNearest; samplerInfo.magnify = gfx::Filter::kNearest;
samplerInfo.mipmap = gfx::Filter::kNearest; samplerInfo.mipmap = gfx::Filter::kNearest;
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()->getRenderer(), pixel, 1, 1, samplerInfo, true)));
} }
materials.back()->setAlbedoTexture(colour_textures.at(c)); materials.back()->setAlbedoTexture(colour_textures.at(c));
} }
@ -296,7 +298,7 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
samplerInfo.magnify = gfx::Filter::kNearest; samplerInfo.magnify = gfx::Filter::kNearest;
samplerInfo.mipmap = gfx::Filter::kNearest; samplerInfo.mipmap = gfx::Filter::kNearest;
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()->getRenderer(), pixel, 1, 1, samplerInfo, false)));
} }
materials.back()->setOcclusionRoughnessMetallicTexture(metal_rough_textures.at(mr)); materials.back()->setOcclusionRoughnessMetallicTexture(metal_rough_textures.at(mr));
} }
@ -550,7 +552,7 @@ engine::Entity LoadGLTF(Scene& scene, const std::string& path, bool isStatic)
} }
// generate mesh on GPU // generate mesh on GPU
std::shared_ptr<Mesh> engine_mesh = std::make_shared<Mesh>(scene.app()->renderer()->GetDevice(), vertices, indices); std::shared_ptr<Mesh> engine_mesh = std::make_shared<Mesh>(scene.app()->getRenderer()->GetDevice(), vertices, indices);
// get material // get material
std::shared_ptr<Material> engine_material = nullptr; std::shared_ptr<Material> engine_material = nullptr;

View File

@ -16,7 +16,7 @@ namespace engine {
Renderer::Renderer(Application& app, gfx::GraphicsSettings settings) : ApplicationComponent(app) Renderer::Renderer(Application& app, gfx::GraphicsSettings settings) : ApplicationComponent(app)
{ {
device_ = std::make_unique<GFXDevice>(GetAppName(), GetAppVersion(), GetWindowHandle(), settings); device_ = std::make_unique<GFXDevice>(getAppName(), getAppVersion(), getWindowHandle(), settings);
// sort out descriptor set layouts: // sort out descriptor set layouts:
std::vector<gfx::DescriptorSetLayoutBinding> globalSetBindings; std::vector<gfx::DescriptorSetLayoutBinding> globalSetBindings;
@ -75,8 +75,8 @@ Renderer::Renderer(Application& app, gfx::GraphicsSettings settings) : Applicati
{ {
gfx::PipelineInfo debug_pipeline_info{}; gfx::PipelineInfo debug_pipeline_info{};
debug_pipeline_info.vert_shader_path = GetResourcePath("engine/shaders/debug.vert"); debug_pipeline_info.vert_shader_path = getResourcePath("engine/shaders/debug.vert");
debug_pipeline_info.frag_shader_path = GetResourcePath("engine/shaders/debug.frag"); debug_pipeline_info.frag_shader_path = getResourcePath("engine/shaders/debug.frag");
debug_pipeline_info.vertex_format = debug_vertex_format; debug_pipeline_info.vertex_format = debug_vertex_format;
debug_pipeline_info.alpha_blending = false; debug_pipeline_info.alpha_blending = false;
debug_pipeline_info.face_cull_mode = gfx::CullMode::kCullNone; // probably ignored for line rendering debug_pipeline_info.face_cull_mode = gfx::CullMode::kCullNone; // probably ignored for line rendering
@ -97,7 +97,7 @@ Renderer::Renderer(Application& app, gfx::GraphicsSettings settings) : Applicati
for (int face = 0; face < 6; ++face) { for (int face = 0; face < 6; ++face) {
std::string path = std::string("engine/textures/skybox") + std::to_string(face) + std::string(".jpg"); std::string path = std::string("engine/textures/skybox") + std::to_string(face) + std::string(".jpg");
face_unq_ptrs[face] = ReadImageFile(GetResourcePath(path), w, h); face_unq_ptrs[face] = ReadImageFile(getResourcePath(path), w, h);
if (cubemap_w != w || cubemap_h != h) throw std::runtime_error("Skybox textures must be 512x512!"); if (cubemap_w != w || cubemap_h != h) throw std::runtime_error("Skybox textures must be 512x512!");
face_unsafe_ptrs[face] = face_unq_ptrs[face]->data(); face_unsafe_ptrs[face] = face_unq_ptrs[face]->data();
} }
@ -119,12 +119,12 @@ Renderer::Renderer(Application& app, gfx::GraphicsSettings settings) : Applicati
// create skybox shader // create skybox shader
{ {
gfx::VertexFormat vertex_format{}; gfx::VertexFormat vertex_format{};
vertex_format.attribute_descriptions.emplace_back(0, gfx::VertexAttribFormat::kFloat3, 0); vertex_format.attribute_descriptions.emplace_back(0u, gfx::VertexAttribFormat::kFloat3, 0u);
vertex_format.stride = 3 * sizeof(float); vertex_format.stride = 3 * sizeof(float);
gfx::PipelineInfo pipeline_info{}; gfx::PipelineInfo pipeline_info{};
pipeline_info.vert_shader_path = GetResourcePath("engine/shaders/skybox.vert"); pipeline_info.vert_shader_path = getResourcePath("engine/shaders/skybox.vert");
pipeline_info.frag_shader_path = GetResourcePath("engine/shaders/skybox.frag"); pipeline_info.frag_shader_path = getResourcePath("engine/shaders/skybox.frag");
pipeline_info.vertex_format = vertex_format; pipeline_info.vertex_format = vertex_format;
pipeline_info.alpha_blending = false; pipeline_info.alpha_blending = false;
pipeline_info.face_cull_mode = gfx::CullMode::kCullBack; pipeline_info.face_cull_mode = gfx::CullMode::kCullBack;
@ -195,11 +195,11 @@ Renderer::Renderer(Application& app, gfx::GraphicsSettings settings) : Applicati
// shadow map pipeline // shadow map pipeline
gfx::VertexFormat shadowVertexFormat{}; gfx::VertexFormat shadowVertexFormat{};
shadowVertexFormat.stride = sizeof(float) * 12; // using the full meshes so a lot of data is skipped shadowVertexFormat.stride = sizeof(float) * 12; // using the full meshes so a lot of data is skipped
shadowVertexFormat.attribute_descriptions.emplace_back(0, gfx::VertexAttribFormat::kFloat3, 0); // position shadowVertexFormat.attribute_descriptions.emplace_back(0u, gfx::VertexAttribFormat::kFloat3, 0u); // position
shadowVertexFormat.attribute_descriptions.emplace_back(1, gfx::VertexAttribFormat::kFloat2, sizeof(float) * 10); // uv shadowVertexFormat.attribute_descriptions.emplace_back(1u, gfx::VertexAttribFormat::kFloat2, static_cast<uint32_t>(sizeof(float)) * 10u); // uv
gfx::PipelineInfo shadowPipelineInfo{}; gfx::PipelineInfo shadowPipelineInfo{};
shadowPipelineInfo.vert_shader_path = GetResourcePath("engine/shaders/shadow.vert"); shadowPipelineInfo.vert_shader_path = getResourcePath("engine/shaders/shadow.vert");
shadowPipelineInfo.frag_shader_path = GetResourcePath("engine/shaders/shadow.frag"); shadowPipelineInfo.frag_shader_path = getResourcePath("engine/shaders/shadow.frag");
shadowPipelineInfo.vertex_format = shadowVertexFormat; shadowPipelineInfo.vertex_format = shadowVertexFormat;
shadowPipelineInfo.face_cull_mode = gfx::CullMode::kCullFront; // shadows care about back faces shadowPipelineInfo.face_cull_mode = gfx::CullMode::kCullFront; // shadows care about back faces
shadowPipelineInfo.alpha_blending = false; shadowPipelineInfo.alpha_blending = false;
@ -250,7 +250,7 @@ Renderer::~Renderer()
device_->DestroyDescriptorSetLayout(global_uniform.layout); device_->DestroyDescriptorSetLayout(global_uniform.layout);
} }
void Renderer::Render(bool window_is_resized, glm::mat4 camera_transform, const RenderList* static_list, const RenderList* dynamic_list, const std::vector<Line>& debug_lines) void Renderer::Render(bool window_is_resized, glm::mat4 camera_transform, const RenderList* static_list, const RenderList* dynamic_list, const std::vector<DebugLine>& debug_lines)
{ {
if (window_is_resized) { if (window_is_resized) {
@ -321,7 +321,7 @@ void Renderer::Render(bool window_is_resized, glm::mat4 camera_transform, const
// draw debug shit here // draw debug shit here
device_->CmdBindPipeline(draw_buffer, debug_rendering_things_.pipeline); device_->CmdBindPipeline(draw_buffer, debug_rendering_things_.pipeline);
DebugPush push{}; DebugPush push{};
for (const Line& l : debug_lines) { for (const DebugLine& l : debug_lines) {
push.pos1 = global_uniform.uniform_buffer_data.data.proj * frame_uniform.uniform_buffer_data.data * glm::vec4(l.pos1, 1.0f); push.pos1 = global_uniform.uniform_buffer_data.data.proj * frame_uniform.uniform_buffer_data.data * glm::vec4(l.pos1, 1.0f);
push.pos2 = global_uniform.uniform_buffer_data.data.proj * frame_uniform.uniform_buffer_data.data * glm::vec4(l.pos2, 1.0f); push.pos2 = global_uniform.uniform_buffer_data.data.proj * frame_uniform.uniform_buffer_data.data * glm::vec4(l.pos2, 1.0f);
push.color = l.color; push.color = l.color;

View File

@ -1,5 +1,6 @@
#include "resource_material.h" #include "resource_material.h"
#include "renderer.h"
#include "resource_shader.h" #include "resource_shader.h"
namespace engine { namespace engine {

View File

@ -3,6 +3,7 @@
#include "application.h" #include "application.h"
#include "gfx_device.h" #include "gfx_device.h"
#include "log.h" #include "log.h"
#include "renderer.h"
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>

View File

@ -3,6 +3,7 @@
#include "application.h" #include "application.h"
#include "files.h" #include "files.h"
#include "log.h" #include "log.h"
#include "renderer.h"
#include <vector> #include <vector>

View File

@ -36,7 +36,7 @@ Entity Scene::CreateEntity(const std::string& tag, Entity parent,
const glm::vec3& scl) { const glm::vec3& scl) {
Entity id = next_entity_id_++; Entity id = next_entity_id_++;
signatures_.emplace(id, std::bitset<kMaxComponents>{}); signatures_.emplace(id, std::bitset<MAX_COMPONENTS>{});
auto t = AddComponent<TransformComponent>(id); auto t = AddComponent<TransformComponent>(id);
@ -61,7 +61,7 @@ size_t Scene::GetComponentSignaturePosition(size_t hash) {
void Scene::Update(float ts) { void Scene::Update(float ts) {
for (auto& [name, system] : ecs_systems_) { for (auto& [name, system] : ecs_systems_) {
system->OnUpdate(ts); system->onUpdate(ts);
} }
event_system_->DespatchEvents(); // clears event queue event_system_->DespatchEvents(); // clears event queue

View File

@ -74,19 +74,19 @@ static std::pair<bool, float> RayBoxIntersection(const Ray& ray, const AABB& box
CollisionSystem::CollisionSystem(Scene* scene) : System(scene, {typeid(TransformComponent).hash_code(), typeid(ColliderComponent).hash_code()}) {} CollisionSystem::CollisionSystem(Scene* scene) : System(scene, {typeid(TransformComponent).hash_code(), typeid(ColliderComponent).hash_code()}) {}
void CollisionSystem::OnComponentInsert(Entity entity) { ++colliders_size_now_; } void CollisionSystem::onComponentInsert(Entity entity) { ++colliders_size_now_; }
void CollisionSystem::OnUpdate(float ts) void CollisionSystem::onUpdate(float ts)
{ {
(void)ts; (void)ts;
if (colliders_size_last_update_ != colliders_size_now_) { if (colliders_size_last_update_ != colliders_size_now_) {
std::vector<PrimitiveInfo> prims{}; std::vector<PrimitiveInfo> prims{};
prims.reserve(entities_.size()); prims.reserve(m_entities.size());
for (Entity entity : entities_) { for (Entity entity : m_entities) {
const auto t = scene_->GetComponent<TransformComponent>(entity); const auto t = m_scene->GetComponent<TransformComponent>(entity);
const auto c = scene_->GetComponent<ColliderComponent>(entity); const auto c = m_scene->GetComponent<ColliderComponent>(entity);
AABB transformed_box{}; AABB transformed_box{};
transformed_box.min = t->world_matrix * glm::vec4(c->aabb.min, 1.0f); transformed_box.min = t->world_matrix * glm::vec4(c->aabb.min, 1.0f);
@ -107,7 +107,7 @@ void CollisionSystem::OnUpdate(float ts)
} }
bvh_.clear(); bvh_.clear();
bvh_.reserve(entities_.size() * 3 / 2); // from testing, bvh is usually 20% larger than number of objects bvh_.reserve(m_entities.size() * 3 / 2); // from testing, bvh is usually 20% larger than number of objects
BuildNode(prims, bvh_); BuildNode(prims, bvh_);
// check AABB mins and maxes are the correct order // check AABB mins and maxes are the correct order
@ -297,7 +297,7 @@ int CollisionSystem::BuildNode(std::vector<PrimitiveInfo>& prims, std::vector<Bi
box2.max.z = box2_main_max; box2.max.z = box2_main_max;
break; break;
} }
const float combined_surface_area = (GetBoxArea(box1) * (i + 1)) + (GetBoxArea(box2) * (prims.size() - (i + 1))); const float combined_surface_area = (GetBoxArea(box1) * (i + 1)) + (GetBoxArea(box2) * (static_cast<int>(prims.size()) - (i + 1)));
if (combined_surface_area < sah) { if (combined_surface_area < sah) {
sah = combined_surface_area; sah = combined_surface_area;
optimal_box1 = box1; optimal_box1 = box1;

View File

@ -16,9 +16,9 @@ CustomBehaviourSystem::CustomBehaviourSystem(Scene* scene)
CustomBehaviourSystem::~CustomBehaviourSystem() {} CustomBehaviourSystem::~CustomBehaviourSystem() {}
void CustomBehaviourSystem::OnUpdate(float ts) { void CustomBehaviourSystem::onUpdate(float ts) {
for (Entity entity : entities_) { for (Entity entity : m_entities) {
auto c = scene_->GetComponent<CustomComponent>(entity); auto c = m_scene->GetComponent<CustomComponent>(entity);
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) {
@ -31,7 +31,7 @@ void CustomBehaviourSystem::OnUpdate(float ts) {
} }
} }
void CustomBehaviourSystem::OnComponentInsert(Entity entity) void CustomBehaviourSystem::onComponentInsert(Entity entity)
{ {
entity_is_initialised_.emplace(entity, false); entity_is_initialised_.emplace(entity, false);
} }

View File

@ -2,9 +2,10 @@
#include <algorithm> #include <algorithm>
#include "component_transform.h"
#include "component_mesh.h" #include "component_mesh.h"
#include "component_transform.h"
#include "log.h" #include "log.h"
#include "resource_material.h"
namespace engine { namespace engine {
@ -18,13 +19,13 @@ void MeshRenderSystem::RebuildStaticRenderList()
list_needs_rebuild_ = false; list_needs_rebuild_ = false;
} }
void MeshRenderSystem::OnComponentInsert(Entity entity) void MeshRenderSystem::onComponentInsert(Entity entity)
{ {
(void)entity; (void)entity;
list_needs_rebuild_ = true; list_needs_rebuild_ = true;
} }
void MeshRenderSystem::OnUpdate(float ts) void MeshRenderSystem::onUpdate(float ts)
{ {
// do stuff // do stuff
(void)ts; (void)ts;
@ -39,16 +40,16 @@ void MeshRenderSystem::OnUpdate(float ts)
void MeshRenderSystem::BuildRenderList(RenderList& render_list, bool with_static_entities) void MeshRenderSystem::BuildRenderList(RenderList& render_list, bool with_static_entities)
{ {
render_list.clear(); render_list.clear();
render_list.reserve(entities_.size()); render_list.reserve(m_entities.size());
std::unordered_map<const gfx::Pipeline*, int> render_orders; std::unordered_map<const gfx::Pipeline*, int> render_orders;
for (Entity entity : entities_) { for (Entity entity : m_entities) {
auto transform = scene_->GetComponent<engine::TransformComponent>(entity); auto transform = m_scene->GetComponent<engine::TransformComponent>(entity);
if (transform->is_static != with_static_entities) continue; if (transform->is_static != with_static_entities) continue;
auto renderable = scene_->GetComponent<engine::MeshRenderableComponent>(entity); auto renderable = m_scene->GetComponent<engine::MeshRenderableComponent>(entity);
if (renderable->visible == false) continue; if (renderable->visible == false) continue;

View File

@ -9,12 +9,12 @@ namespace engine {
TransformSystem::TransformSystem(Scene* scene) : System(scene, {typeid(TransformComponent).hash_code()}) {} TransformSystem::TransformSystem(Scene* scene) : System(scene, {typeid(TransformComponent).hash_code()}) {}
void TransformSystem::OnUpdate(float ts) void TransformSystem::onUpdate(float ts)
{ {
(void)ts; (void)ts;
for (Entity entity : entities_) { for (Entity entity : m_entities) {
auto t = scene_->GetComponent<TransformComponent>(entity); auto t = m_scene->GetComponent<TransformComponent>(entity);
glm::mat4 transform; glm::mat4 transform;
@ -26,7 +26,7 @@ void TransformSystem::OnUpdate(float ts)
transform = glm::scale(transform, t->scale); transform = glm::scale(transform, t->scale);
if (t->parent != 0) { if (t->parent != 0) {
auto parent_t = scene_->GetComponent<TransformComponent>(t->parent); auto parent_t = m_scene->GetComponent<TransformComponent>(t->parent);
transform = parent_t->world_matrix * transform; transform = parent_t->world_matrix * transform;
// (debug builds only) ensure static objects have static parents // (debug builds only) ensure static objects have static parents
@ -39,8 +39,8 @@ void TransformSystem::OnUpdate(float ts)
Entity TransformSystem::GetChildEntity(Entity parent, const std::string& tag) Entity TransformSystem::GetChildEntity(Entity parent, const std::string& tag)
{ {
for (Entity entity : entities_) { for (Entity entity : m_entities) {
auto t = scene_->GetComponent<TransformComponent>(entity); auto t = m_scene->GetComponent<TransformComponent>(entity);
if (t->parent == parent) { if (t->parent == parent) {
if (t->tag == tag) { if (t->tag == tag) {
return entity; return entity;

View File

@ -1,32 +1,34 @@
#include "camera_controller.hpp" #include "camera_controller.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtc/constants.hpp> #include <glm/gtc/constants.hpp>
#include <glm/gtc/quaternion.hpp> #include <glm/gtc/quaternion.hpp>
#include <glm/gtx/rotate_vector.hpp> #include <glm/gtx/rotate_vector.hpp>
#include <glm/trigonometric.hpp> #include <glm/trigonometric.hpp>
#include "application.h" #include "application.h"
#include "component_mesh.h"
#include "component_transform.h" #include "component_transform.h"
#include "ecs.h" #include "ecs.h"
#include "input_manager.h" #include "input_manager.h"
#include "log.h" #include "log.h"
#include "scene.h" #include "scene.h"
#include "scene_manager.h" #include "scene_manager.h"
#include "window.h"
#include "system_collisions.h" #include "system_collisions.h"
#include "component_mesh.h" #include "system_mesh_render.h"
#include "window.h"
CameraControllerSystem::CameraControllerSystem(engine::Scene* scene) CameraControllerSystem::CameraControllerSystem(engine::Scene* scene)
: System(scene, {typeid(engine::TransformComponent).hash_code(), typeid(CameraControllerComponent).hash_code()}) : System(scene, {typeid(engine::TransformComponent).hash_code(), typeid(CameraControllerComponent).hash_code()})
{ {
} }
void CameraControllerSystem::OnUpdate(float ts) void CameraControllerSystem::onUpdate(float ts)
{ {
if (t == nullptr || c == nullptr) { if (t == nullptr || c == nullptr) {
for (engine::Entity entity : entities_) { for (engine::Entity entity : m_entities) {
t = scene_->GetComponent<engine::TransformComponent>(entity); t = m_scene->GetComponent<engine::TransformComponent>(entity);
c = scene_->GetComponent<CameraControllerComponent>(entity); c = m_scene->GetComponent<CameraControllerComponent>(entity);
break; break;
} }
if (t == nullptr) return; if (t == nullptr) return;
@ -35,21 +37,21 @@ void CameraControllerSystem::OnUpdate(float ts)
const float dt = ts; const float dt = ts;
float dx = scene_->app()->input_manager()->GetAxis("movex") * CameraControllerComponent::kSpeedStrafe; float dx = m_scene->app()->getInputManager()->GetAxis("movex") * CameraControllerComponent::kSpeedStrafe;
float dy = scene_->app()->input_manager()->GetAxis("movey") * CameraControllerComponent::kSpeedForwardBack; float dy = m_scene->app()->getInputManager()->GetAxis("movey") * CameraControllerComponent::kSpeedForwardBack;
if (scene_->app()->input_manager()->GetButton("sprint")) { if (m_scene->app()->getInputManager()->GetButton("sprint")) {
dy *= CameraControllerComponent::kSprintMultiplier; dy *= CameraControllerComponent::kSprintMultiplier;
} }
// calculate new pitch and yaw // calculate new pitch and yaw
float d_pitch = scene_->app()->input_manager()->GetAxis("looky") * -1.0f * CameraControllerComponent::kCameraSensitivity; float d_pitch = m_scene->app()->getInputManager()->GetAxis("looky") * -1.0f * CameraControllerComponent::kCameraSensitivity;
c->pitch += d_pitch; c->pitch += d_pitch;
if (c->pitch <= CameraControllerComponent::kMinPitch || c->pitch >= CameraControllerComponent::kMaxPitch) { if (c->pitch <= CameraControllerComponent::kMinPitch || c->pitch >= CameraControllerComponent::kMaxPitch) {
c->pitch -= d_pitch; c->pitch -= d_pitch;
} }
c->yaw += scene_->app()->input_manager()->GetAxis("lookx") * -1.0f * CameraControllerComponent::kCameraSensitivity; c->yaw += m_scene->app()->getInputManager()->GetAxis("lookx") * -1.0f * CameraControllerComponent::kCameraSensitivity;
// update position relative to camera direction in xz plane // update position relative to camera direction in xz plane
const glm::vec3 d2x_rotated = glm::rotateZ(glm::vec3{dx, 0.0f, 0.0f}, c->yaw); const glm::vec3 d2x_rotated = glm::rotateZ(glm::vec3{dx, 0.0f, 0.0f}, c->yaw);
@ -63,7 +65,7 @@ void CameraControllerSystem::OnUpdate(float ts)
c->vel.z += CameraControllerComponent::kGravAccel * dt; c->vel.z += CameraControllerComponent::kGravAccel * dt;
// jumping // jumping
if (scene_->app()->input_manager()->GetButtonPress("jump") && (c->grounded || c->noclip)) { if (m_scene->app()->getInputManager()->GetButtonPress("jump") && (c->grounded || c->noclip)) {
c->vel.z += CameraControllerComponent::kJumpVelocity; // m/s c->vel.z += CameraControllerComponent::kJumpVelocity; // m/s
} }
@ -86,7 +88,7 @@ void CameraControllerSystem::OnUpdate(float ts)
ray.direction.x = c->vel.x; ray.direction.x = c->vel.x;
ray.direction.y = c->vel.y; // this is normalized by GetRayCast() ray.direction.y = c->vel.y; // this is normalized by GetRayCast()
ray.direction.z = 0.0f; ray.direction.z = 0.0f;
raycasts[i] = scene_->GetSystem<engine::CollisionSystem>()->GetRaycast(ray); raycasts[i] = m_scene->GetSystem<engine::CollisionSystem>()->GetRaycast(ray);
if (raycasts[i].hit && raycasts[i].distance < smallest_distance) { if (raycasts[i].hit && raycasts[i].distance < smallest_distance) {
smallest_distance = raycasts[i].distance; smallest_distance = raycasts[i].distance;
@ -119,7 +121,7 @@ void CameraControllerSystem::OnUpdate(float ts)
fall_ray.origin = t->position; fall_ray.origin = t->position;
fall_ray.origin.z += CameraControllerComponent::kMaxStairHeight - CameraControllerComponent::kPlayerHeight; fall_ray.origin.z += CameraControllerComponent::kMaxStairHeight - CameraControllerComponent::kPlayerHeight;
fall_ray.direction = { 0.0f, 0.0f, -1.0f }; // down fall_ray.direction = { 0.0f, 0.0f, -1.0f }; // down
const engine::Raycast fall_raycast = scene_->GetSystem<engine::CollisionSystem>()->GetRaycast(fall_ray); const engine::Raycast fall_raycast = m_scene->GetSystem<engine::CollisionSystem>()->GetRaycast(fall_ray);
if (fall_raycast.hit) { // there is ground below player if (fall_raycast.hit) { // there is ground below player
// find how far the player will move (downwards) if velocity is applied without collision // find how far the player will move (downwards) if velocity is applied without collision
const float mag_dz = fabsf(c->vel.z * dt); const float mag_dz = fabsf(c->vel.z * dt);
@ -144,7 +146,7 @@ void CameraControllerSystem::OnUpdate(float ts)
engine::Ray jump_ray{}; engine::Ray jump_ray{};
jump_ray.origin = t->position; jump_ray.origin = t->position;
jump_ray.direction = { 0.0f, 0.0f, 1.0f }; jump_ray.direction = { 0.0f, 0.0f, 1.0f };
const engine::Raycast jump_raycast = scene_->GetSystem<engine::CollisionSystem>()->GetRaycast(jump_ray); const engine::Raycast jump_raycast = m_scene->GetSystem<engine::CollisionSystem>()->GetRaycast(jump_ray);
if (jump_raycast.hit) { if (jump_raycast.hit) {
// find how far the player will move (upwards) if velocity is applied without collision // find how far the player will move (upwards) if velocity is applied without collision
const float mag_dz = fabsf(c->vel.z * dt); const float mag_dz = fabsf(c->vel.z * dt);
@ -183,47 +185,47 @@ void CameraControllerSystem::OnUpdate(float ts)
/* user interface inputs */ /* user interface inputs */
if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_R) || glm::length(t->position) > CameraControllerComponent::kMaxDistanceFromOrigin) { if (m_scene->app()->getWindow()->GetKeyPress(engine::inputs::Key::K_R) || glm::length(t->position) > CameraControllerComponent::kMaxDistanceFromOrigin) {
t->position = {0.0f, 0.0f, 10.0f}; t->position = {0.0f, 0.0f, 10.0f};
c->vel = {0.0f, 0.0f, 0.0f}; c->vel = {0.0f, 0.0f, 0.0f};
c->pitch = glm::half_pi<float>(); c->pitch = glm::half_pi<float>();
c->yaw = 0.0f; c->yaw = 0.0f;
} }
if (scene_->app()->input_manager()->GetButtonPress("fullscreen")) { if (m_scene->app()->getInputManager()->GetButtonPress("fullscreen")) {
scene_->app()->window()->ToggleFullscreen(); m_scene->app()->getWindow()->ToggleFullscreen();
} }
if (scene_->app()->input_manager()->GetButtonPress("exit")) { if (m_scene->app()->getInputManager()->GetButtonPress("exit")) {
scene_->app()->window()->SetCloseFlag(); m_scene->app()->getWindow()->SetCloseFlag();
} }
if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_F)) { if (m_scene->app()->getWindow()->GetKeyPress(engine::inputs::Key::K_F)) {
scene_->app()->scene_manager()->SetActiveScene(next_scene_); m_scene->app()->getSceneManager()->SetActiveScene(next_scene_);
} }
if (scene_->app()->window()->GetKeyPress(engine::inputs::Key::K_Q)) { if (m_scene->app()->getWindow()->GetKeyPress(engine::inputs::Key::K_Q)) {
c->noclip ^= true; c->noclip ^= true;
} }
if (scene_->app()->window()->GetButtonPress(engine::inputs::MouseButton::M_LEFT)) { if (m_scene->app()->getWindow()->GetButtonPress(engine::inputs::MouseButton::M_LEFT)) {
engine::Ray ray{}; engine::Ray ray{};
ray.origin = t->position; ray.origin = t->position;
ray.direction = glm::vec3(glm::mat4_cast(t->rotation) * glm::vec4{0.0f, 0.0f, -1.0f, 1.0f}); ray.direction = glm::vec3(glm::mat4_cast(t->rotation) * glm::vec4{0.0f, 0.0f, -1.0f, 1.0f});
engine::Raycast cast = scene_->GetSystem<engine::CollisionSystem>()->GetRaycast(ray); engine::Raycast cast = m_scene->GetSystem<engine::CollisionSystem>()->GetRaycast(ray);
if (cast.hit) { if (cast.hit) {
LOG_TRACE("Distance: {} m", cast.distance); LOG_TRACE("Distance: {} m", cast.distance);
LOG_TRACE("Location: {} {} {}", cast.location.x, cast.location.y, cast.location.z); LOG_TRACE("Location: {} {} {}", cast.location.x, cast.location.y, cast.location.z);
LOG_TRACE("Normal: {} {} {}", cast.normal.x, cast.normal.y, cast.normal.z); LOG_TRACE("Normal: {} {} {}", cast.normal.x, cast.normal.y, cast.normal.z);
LOG_TRACE("Ray direction: {} {} {}", ray.direction.x, ray.direction.y, ray.direction.z); LOG_TRACE("Ray direction: {} {} {}", ray.direction.x, ray.direction.y, ray.direction.z);
LOG_TRACE("Hit Entity: {}", scene_->GetComponent<engine::TransformComponent>(cast.hit_entity)->tag); LOG_TRACE("Hit Entity: {}", m_scene->GetComponent<engine::TransformComponent>(cast.hit_entity)->tag);
c->perm_lines.clear(); c->perm_lines.clear();
c->perm_lines.emplace_back(ray.origin, cast.location, glm::vec3{0.0f, 0.0f, 1.0f}); c->perm_lines.emplace_back(ray.origin, cast.location, glm::vec3{0.0f, 0.0f, 1.0f});
scene_->GetComponent<engine::MeshRenderableComponent>(cast.hit_entity)->visible ^= true; m_scene->GetComponent<engine::MeshRenderableComponent>(cast.hit_entity)->visible ^= true;
scene_->GetSystem<engine::MeshRenderSystem>()->RebuildStaticRenderList(); m_scene->GetSystem<engine::MeshRenderSystem>()->RebuildStaticRenderList();
} }
} }
scene_->app()->debug_lines.insert(scene_->app()->debug_lines.end(), c->perm_lines.begin(), c->perm_lines.end()); m_scene->app()->debug_lines.insert(m_scene->app()->debug_lines.end(), c->perm_lines.begin(), c->perm_lines.end());
} }

View File

@ -2,8 +2,9 @@
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
#include "component_transform.h"
#include "application.h" #include "application.h"
#include "component_transform.h"
#include "debug_line.h"
#include "ecs.h" #include "ecs.h"
struct CameraControllerComponent { struct CameraControllerComponent {
@ -36,7 +37,7 @@ struct CameraControllerComponent {
glm::vec3 vel{0.0f, 0.0f, 0.0f}; glm::vec3 vel{0.0f, 0.0f, 0.0f};
bool grounded = false; bool grounded = false;
std::vector<engine::Line> perm_lines{}; // raycasting lines std::vector<engine::DebugLine> perm_lines{}; // raycasting lines
}; };
class CameraControllerSystem : public engine::System { class CameraControllerSystem : public engine::System {
@ -44,7 +45,7 @@ class CameraControllerSystem : public engine::System {
CameraControllerSystem(engine::Scene* scene); CameraControllerSystem(engine::Scene* scene);
// engine::System overrides // engine::System overrides
void OnUpdate(float ts) override; void onUpdate(float ts) override;
engine::TransformComponent* t = nullptr; engine::TransformComponent* t = nullptr;
CameraControllerComponent* c = nullptr; CameraControllerComponent* c = nullptr;

View File

@ -6,8 +6,11 @@
#include "component_custom.h" #include "component_custom.h"
#include "component_mesh.h" #include "component_mesh.h"
#include "component_transform.h" #include "component_transform.h"
#include "gltf_loader.h"
#include "input_manager.h" #include "input_manager.h"
#include "log.h"
#include "meshgen.hpp" #include "meshgen.hpp"
#include "renderer.h"
#include "resource_font.h" #include "resource_font.h"
#include "resource_material.h" #include "resource_material.h"
#include "resource_texture.h" #include "resource_texture.h"
@ -15,8 +18,6 @@
#include "scene_manager.h" #include "scene_manager.h"
#include "system_mesh_render.h" #include "system_mesh_render.h"
#include "system_transform.h" #include "system_transform.h"
#include "gltf_loader.h"
#include "log.h"
#include "window.h" #include "window.h"
#include "config.h" #include "config.h"
@ -54,10 +55,10 @@ void PlayGame(GameSettings settings)
configuration.enable_frame_limiter = settings.enable_frame_limiter; configuration.enable_frame_limiter = settings.enable_frame_limiter;
engine::Application app(PROJECT_NAME, PROJECT_VERSION, graphics_settings, configuration); engine::Application app(PROJECT_NAME, PROJECT_VERSION, graphics_settings, configuration);
app.window()->SetRelativeMouseMode(true); app.getWindow()->SetRelativeMouseMode(true);
ConfigureInputs(*app.input_manager()); ConfigureInputs(*app.getInputManager());
engine::Scene* start_scene = app.scene_manager()->CreateEmptyScene(); engine::Scene* start_scene = app.getSceneManager()->CreateEmptyScene();
{ {
/* create camera */ /* create camera */
engine::Entity camera = start_scene->CreateEntity("camera"); engine::Entity camera = start_scene->CreateEntity("camera");
@ -73,7 +74,7 @@ void PlayGame(GameSettings settings)
start_scene->GetPosition(camera).z += 10.0f; start_scene->GetPosition(camera).z += 10.0f;
} }
engine::Scene* main_scene = app.scene_manager()->CreateEmptyScene(); engine::Scene* main_scene = app.getSceneManager()->CreateEmptyScene();
{ {
/* create camera */ /* create camera */
engine::Entity camera = main_scene->CreateEntity("camera"); engine::Entity camera = main_scene->CreateEntity("camera");
@ -82,7 +83,7 @@ void PlayGame(GameSettings settings)
main_scene->GetTransform(camera_child)->is_static = false; main_scene->GetTransform(camera_child)->is_static = false;
auto camren = main_scene->AddComponent<engine::MeshRenderableComponent>(camera_child); auto camren = main_scene->AddComponent<engine::MeshRenderableComponent>(camera_child);
camren->visible = false; camren->visible = false;
camren->mesh = GenSphereMesh(app.renderer()->GetDevice(), 1.0f, /*16*/32); camren->mesh = GenSphereMesh(app.getRenderer()->GetDevice(), 1.0f, /*16*/32);
camren->material = app.getResource<engine::Material>("builtin.default"); camren->material = app.getResource<engine::Material>("builtin.default");
/* as of right now, the entity with tag 'camera' is used to build the view /* as of right now, the entity with tag 'camera' is used to build the view
@ -136,7 +137,7 @@ void PlayGame(GameSettings settings)
main_scene->GetTransform(cube)->is_static = false; main_scene->GetTransform(cube)->is_static = false;
auto cube_ren = main_scene->AddComponent<engine::MeshRenderableComponent>(cube); auto cube_ren = main_scene->AddComponent<engine::MeshRenderableComponent>(cube);
cube_ren->material = app.getResource<engine::Material>("builtin.default"); cube_ren->material = app.getResource<engine::Material>("builtin.default");
cube_ren->mesh = GenCuboidMesh(app.renderer()->GetDevice(), 1.0f, 1.0f, 1.0f); cube_ren->mesh = GenCuboidMesh(app.getRenderer()->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->on_init = [] {}; cubeCustom->on_init = [] {};
@ -166,6 +167,6 @@ void PlayGame(GameSettings settings)
start_scene->GetSystem<CameraControllerSystem>()->next_scene_ = main_scene; start_scene->GetSystem<CameraControllerSystem>()->next_scene_ = main_scene;
main_scene->GetSystem<CameraControllerSystem>()->next_scene_ = start_scene; main_scene->GetSystem<CameraControllerSystem>()->next_scene_ = start_scene;
app.scene_manager()->SetActiveScene(main_scene); app.getSceneManager()->SetActiveScene(main_scene);
app.gameLoop(); app.gameLoop();
} }