From 991093d3522b713a106d09f31c2599958845d93b Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Sun, 20 Oct 2024 15:53:57 +0200 Subject: [PATCH] Move flycam to binary --- src/bin/CMakeLists.txt | 9 +- src/bin/fall-fever/CMakeLists.txt | 9 ++ src/bin/{ => fall-fever}/controller.cpp | 38 +++---- src/bin/{ => fall-fever}/controller.h | 4 +- src/bin/{ => fall-fever}/debug.cpp | 0 src/bin/fall-fever/flycam.cpp | 98 +++++++++++++++++ src/bin/fall-fever/flycam.h | 12 ++ src/bin/{ => fall-fever}/main.cpp | 0 src/lib/CMakeLists.txt | 2 +- .../core/{game_loop.cpp => application.cpp} | 28 +++-- src/lib/core/{game_loop.h => application.h} | 22 ++-- src/lib/core/camera.cpp | 104 +----------------- src/lib/core/camera.h | 6 - src/lib/scene/gltf.cpp | 40 ++++--- 14 files changed, 194 insertions(+), 178 deletions(-) create mode 100644 src/bin/fall-fever/CMakeLists.txt rename src/bin/{ => fall-fever}/controller.cpp (68%) rename src/bin/{ => fall-fever}/controller.h (64%) rename src/bin/{ => fall-fever}/debug.cpp (100%) create mode 100644 src/bin/fall-fever/flycam.cpp create mode 100644 src/bin/fall-fever/flycam.h rename src/bin/{ => fall-fever}/main.cpp (100%) rename src/lib/core/{game_loop.cpp => application.cpp} (86%) rename src/lib/core/{game_loop.h => application.h} (81%) diff --git a/src/bin/CMakeLists.txt b/src/bin/CMakeLists.txt index e038700..b50ac92 100644 --- a/src/bin/CMakeLists.txt +++ b/src/bin/CMakeLists.txt @@ -1,8 +1 @@ -CPMAddPackage(NAME argparse URL "https://github.com/p-ranav/argparse/archive/refs/tags/v3.1.tar.gz") - -add_executable(Fall-Fever - main.cpp - controller.cpp -) - -target_link_libraries(Fall-Fever PRIVATE fever_core argparse) +add_subdirectory(fall-fever) diff --git a/src/bin/fall-fever/CMakeLists.txt b/src/bin/fall-fever/CMakeLists.txt new file mode 100644 index 0000000..0dfd65f --- /dev/null +++ b/src/bin/fall-fever/CMakeLists.txt @@ -0,0 +1,9 @@ +CPMAddPackage(NAME argparse URL "https://github.com/p-ranav/argparse/archive/refs/tags/v3.1.tar.gz") + +add_executable(Fall-Fever + main.cpp + controller.cpp + flycam.cpp +) + +target_link_libraries(Fall-Fever PRIVATE fever_core argparse) diff --git a/src/bin/controller.cpp b/src/bin/fall-fever/controller.cpp similarity index 68% rename from src/bin/controller.cpp rename to src/bin/fall-fever/controller.cpp index 438555d..302924a 100644 --- a/src/bin/controller.cpp +++ b/src/bin/fall-fever/controller.cpp @@ -1,11 +1,14 @@ #include "controller.h" +#include "flycam.h" + #include "components/name.h" #include "components/transform.h" #include "core/camera.h" #include "core/light.h" -#include "spdlog/spdlog.h" #include "window/window.h" +#include + using namespace entt::literals; Controller::Controller(std::string_view path) @@ -19,24 +22,6 @@ Controller::Controller(std::string_view path) gltf_document->spawn_default_scene(registry(), gltf_node_cache); - // Convert meshes - auto mesh_view = registry().view>(); - for (auto [entity, mesh] : mesh_view.each()) { - registry().emplace(entity, GpuMesh(mesh)); - - // Remove Mesh resource as it is no longer needed. - registry().erase>(entity); - } - - // Convert materials - auto material_view = registry().view>(); - for (auto [entity, material] : material_view.each()) { - registry().emplace(entity, GpuMaterial(material)); - - // Remove Material resource as it is no longer needed. - registry().erase>(entity); - } - // Spawn default lights auto directional_light = registry().create(); registry().emplace(directional_light, "Directional Light"); @@ -55,13 +40,24 @@ Controller::Controller(std::string_view path) registry().emplace(point_light, GlobalTransform{}); registry().emplace(point_light, PointLight{.intensity = PointLight::DEFAULT_INTENSITY}); + + // Spawn default camera + auto camera_view = registry().view(); + if (camera_view.empty()) { + auto entity = registry().create(); + registry().emplace(entity, "Camera"); + registry().emplace(entity, Transform{.translation = glm::vec3(0.0, 0.25, -1.0)}); + registry().emplace(entity, GlobalTransform{}); + registry().emplace(entity, Camera{.projection = Camera::Perspective{}}); + registry().emplace(entity); + } } void Controller::update() { - Camera::keyboard_movement(registry()); + Flycam::keyboard_movement(registry()); if (registry().ctx().get().catched) { - Camera::mouse_orientation(registry()); + Flycam::mouse_orientation(registry()); } } diff --git a/src/bin/controller.h b/src/bin/fall-fever/controller.h similarity index 64% rename from src/bin/controller.h rename to src/bin/fall-fever/controller.h index 2c7d77c..de406e0 100644 --- a/src/bin/controller.h +++ b/src/bin/fall-fever/controller.h @@ -1,11 +1,11 @@ #pragma once -#include "core/game_loop.h" +#include "core/application.h" #include #include -class Controller : public GameLoop +class Controller : public FeverCore::Application { public: Controller(std::string_view path); diff --git a/src/bin/debug.cpp b/src/bin/fall-fever/debug.cpp similarity index 100% rename from src/bin/debug.cpp rename to src/bin/fall-fever/debug.cpp diff --git a/src/bin/fall-fever/flycam.cpp b/src/bin/fall-fever/flycam.cpp new file mode 100644 index 0000000..d7a7943 --- /dev/null +++ b/src/bin/fall-fever/flycam.cpp @@ -0,0 +1,98 @@ +#include "flycam.h" + +#include "core/camera.h" +#include "core/time.h" +#include "input/input.h" + +#include +#include +#include +#include + +void Flycam::keyboard_movement(entt::registry& registry) +{ + struct KeyboardMovementContext + { + bool accelerate{}; + }; + + auto& movement_context = registry.ctx().emplace(); + auto const& key_state = registry.ctx().get>(); + auto const& delta_time = registry.ctx().get(); + + auto camera_view = + registry.view(); + auto camera_entity = camera_view.front(); + + if (camera_entity == entt::null) { + spdlog::debug("No camera entity found"); + return; + } + + auto [camera, camera_transform, camera_global_transform] = camera_view.get(camera_entity); + + glm::vec3 front_vec = Camera::front_vector(camera_global_transform); + front_vec.y = 0; + + glm::vec3 delta_pos = glm::vec3(0., 0., 0.); + float acceleration = movement_context.accelerate ? ACCELERATION : 1.0F; + float delta_factor = static_cast(delta_time.delta.count()) * SPEED * acceleration; + movement_context.accelerate = false; + + if (key_state.pressed(Input::KeyCode{GLFW_KEY_W})) { + delta_pos += delta_factor * glm::normalize(front_vec); + } + if (key_state.pressed(Input::KeyCode{GLFW_KEY_S})) { + delta_pos -= delta_factor * glm::normalize(front_vec); + } + if (key_state.pressed(Input::KeyCode{GLFW_KEY_A})) { + delta_pos -= delta_factor * glm::normalize(glm::cross(front_vec, Camera::UP_VECTOR)); + } + if (key_state.pressed(Input::KeyCode{GLFW_KEY_D})) { + delta_pos += delta_factor * glm::normalize(glm::cross(front_vec, Camera::UP_VECTOR)); + } + if (key_state.pressed(Input::KeyCode{GLFW_KEY_SPACE})) { + delta_pos += delta_factor * Camera::UP_VECTOR; + } + if (key_state.pressed(Input::KeyCode{GLFW_KEY_LEFT_SHIFT})) { + delta_pos -= delta_factor * Camera::UP_VECTOR; + } + if (key_state.pressed(Input::KeyCode{GLFW_KEY_LEFT_ALT})) { + movement_context.accelerate = true; + } + + camera_transform.translation += delta_pos; +} + +void Flycam::mouse_orientation(entt::registry& registry) +{ + auto camera_view = registry.view(); + auto camera_entity = camera_view.front(); + + if (camera_entity == entt::null) { + spdlog::debug("No camera entity found"); + return; + } + + auto [camera, camera_transform] = camera_view.get(camera_entity); + + auto const& mouse_cursor_input = registry.ctx().get(); + auto delta_x = mouse_cursor_input.delta.x; + auto delta_y = mouse_cursor_input.delta.y; + + auto pitch = static_cast(-delta_y); + auto yaw = static_cast(delta_x); + + // Orthographic projection currently unsupported + auto& camera_perspective = std::get(camera.projection); + + camera_perspective.pitch += glm::radians(pitch); + camera_perspective.yaw += glm::radians(yaw); + + static constexpr float PITCH_CLIP = glm::radians(89.); + camera_perspective.pitch = + std::clamp(static_cast(camera_perspective.pitch), -PITCH_CLIP, PITCH_CLIP); + + camera_transform.orientation = + glm::quat(glm::vec3(-camera_perspective.pitch, -camera_perspective.yaw, 0.0)); +} diff --git a/src/bin/fall-fever/flycam.h b/src/bin/fall-fever/flycam.h new file mode 100644 index 0000000..78f9e33 --- /dev/null +++ b/src/bin/fall-fever/flycam.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +struct Flycam +{ + static constexpr float SPEED = 0.5; + static constexpr float ACCELERATION = 5.0; + + static void keyboard_movement(entt::registry& registry); + static void mouse_orientation(entt::registry& registry); +}; diff --git a/src/bin/main.cpp b/src/bin/fall-fever/main.cpp similarity index 100% rename from src/bin/main.cpp rename to src/bin/fall-fever/main.cpp diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index a7573aa..1b0c4de 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -1,7 +1,7 @@ add_library(fever_core components/transform.cpp + core/application.cpp core/camera.cpp - core/game_loop.cpp core/glad.cpp core/graphics/framebuffer.cpp core/graphics/image.cpp diff --git a/src/lib/core/game_loop.cpp b/src/lib/core/application.cpp similarity index 86% rename from src/lib/core/game_loop.cpp rename to src/lib/core/application.cpp index d7b730c..b85050c 100644 --- a/src/lib/core/game_loop.cpp +++ b/src/lib/core/application.cpp @@ -1,29 +1,25 @@ -#include "game_loop.h" +#include "application.h" + #include "core/camera.h" #include "core/light.h" #include "core/render.h" #include "core/shader.h" #include "core/time.h" #include "input/input.h" -#include "scene/scene.h" #include "window/window.h" #include -#include -#include -#include #include #include #include #include -#include #include -#include -#include -GameLoop::~GameLoop() = default; +namespace FeverCore { -GameLoop::GameLoop() : +Application::~Application() = default; + +Application::Application() : game_window(std::make_shared(event_dispatcher)), post_processing_framebuffer(game_window->physical_dimensions()), key_listener{.registry = entt_registry}, @@ -38,13 +34,13 @@ GameLoop::GameLoop() : { register_context_variables(); - event_dispatcher.sink().connect<&GameLoop::recreate_framebuffer>(this); + event_dispatcher.sink().connect<&Application::recreate_framebuffer>(this); event_dispatcher.sink().connect<&Input::KeyListener::key_event>(key_listener); event_dispatcher.sink().connect<&Input::CursorListener::cursor_event>( cursor_listener); } -void GameLoop::run() +void Application::run() { entt::hashed_string shader_hash(Material::SHADER_NAME.data()); auto standard_material_shader = @@ -70,7 +66,7 @@ void GameLoop::run() GlobalTransform::update(entt_registry); Camera::aspect_ratio_update(entt_registry); - update(); + this->update(); Input::State::update_state(entt_registry); Input::reset_mouse_motion(entt_registry); @@ -91,14 +87,16 @@ void GameLoop::run() } } -void GameLoop::register_context_variables() +void Application::register_context_variables() { entt_registry.ctx().emplace>(); entt_registry.ctx().emplace(); } -void GameLoop::recreate_framebuffer() +void Application::recreate_framebuffer() { auto dimensions = game_window->physical_dimensions(); post_processing_framebuffer = Framebuffer(dimensions); } + +} // namespace FeverCore diff --git a/src/lib/core/game_loop.h b/src/lib/core/application.h similarity index 81% rename from src/lib/core/game_loop.h rename to src/lib/core/application.h index 2925264..d7259ee 100644 --- a/src/lib/core/game_loop.h +++ b/src/lib/core/application.h @@ -10,22 +10,22 @@ #include #include #include -#include -#include class Camera; class Window; -class GameLoop +namespace FeverCore { + +class Application { public: - GameLoop(); + Application(); - virtual ~GameLoop(); - GameLoop(GameLoop const&) = delete; - GameLoop(GameLoop&&) = delete; - auto operator=(GameLoop const&) -> GameLoop& = delete; - auto operator=(GameLoop&&) -> GameLoop& = delete; + virtual ~Application(); + Application(Application const&) = delete; + Application(Application&&) = delete; + auto operator=(Application const&) -> Application& = delete; + auto operator=(Application&&) -> Application& = delete; auto registry() -> entt::registry& { return entt_registry; } auto registry() const -> entt::registry const& { return entt_registry; } @@ -62,4 +62,6 @@ protected: GltfLoader gltf_loader; entt::resource_cache gltf_cache; -}; \ No newline at end of file +}; + +} // namespace FeverCore diff --git a/src/lib/core/camera.cpp b/src/lib/core/camera.cpp index 4386bfa..e155490 100644 --- a/src/lib/core/camera.cpp +++ b/src/lib/core/camera.cpp @@ -1,10 +1,7 @@ #include "camera.h" -#include "core/time.h" -#include "input/input.h" #include "window/window.h" #include -#include #include #include @@ -33,108 +30,15 @@ auto Camera::front_vector(GlobalTransform const& transform) -> glm::vec3 return glm::normalize(transform.transform * glm::vec4(0.0, 0.0, 1.0, 0.0)); } -void Camera::keyboard_movement(entt::registry& registry) -{ - struct KeyboardMovementContext - { - bool accelerate{}; - }; - - auto& movement_context = registry.ctx().emplace(); - auto const& key_state = registry.ctx().get>(); - auto const& delta_time = registry.ctx().get(); - - auto camera_view = registry.view(); - auto camera_entity = camera_view.front(); - - if (camera_entity == entt::null) { - spdlog::debug("No camera entity found"); - return; - } - - auto [camera, camera_transform, camera_global_transform] = camera_view.get(camera_entity); - - glm::vec3 front_vec = front_vector(camera_global_transform); - front_vec.y = 0; - - glm::vec3 delta_pos = glm::vec3(0., 0., 0.); - float acceleration = movement_context.accelerate ? ACCELERATION : 1.0F; - float delta_factor = static_cast(delta_time.delta.count()) * SPEED * acceleration; - movement_context.accelerate = false; - - if (key_state.pressed(Input::KeyCode{GLFW_KEY_W})) { - delta_pos += delta_factor * glm::normalize(front_vec); - } - if (key_state.pressed(Input::KeyCode{GLFW_KEY_S})) { - delta_pos -= delta_factor * glm::normalize(front_vec); - } - if (key_state.pressed(Input::KeyCode{GLFW_KEY_A})) { - delta_pos -= delta_factor * glm::normalize(glm::cross(front_vec, Camera::UP_VECTOR)); - } - if (key_state.pressed(Input::KeyCode{GLFW_KEY_D})) { - delta_pos += delta_factor * glm::normalize(glm::cross(front_vec, Camera::UP_VECTOR)); - } - if (key_state.pressed(Input::KeyCode{GLFW_KEY_SPACE})) { - delta_pos += delta_factor * UP_VECTOR; - } - if (key_state.pressed(Input::KeyCode{GLFW_KEY_LEFT_SHIFT})) { - delta_pos -= delta_factor * UP_VECTOR; - } - if (key_state.pressed(Input::KeyCode{GLFW_KEY_LEFT_ALT})) { - movement_context.accelerate = true; - } - - camera_transform.translation += delta_pos; -} - -void Camera::mouse_orientation(entt::registry& registry) -{ - auto camera_view = registry.view(); - auto camera_entity = camera_view.front(); - - if (camera_entity == entt::null) { - spdlog::debug("No camera entity found"); - return; - } - - auto [camera, camera_transform] = camera_view.get(camera_entity); - - auto const& mouse_cursor_input = registry.ctx().get(); - auto delta_x = mouse_cursor_input.delta.x; - auto delta_y = mouse_cursor_input.delta.y; - - auto pitch = static_cast(-delta_y); - auto yaw = static_cast(delta_x); - - // Orthographic projection currently unsupported - auto& camera_perspective = std::get(camera.projection); - - camera_perspective.pitch += glm::radians(pitch); - camera_perspective.yaw += glm::radians(yaw); - - static constexpr float PITCH_CLIP = glm::radians(89.); - camera_perspective.pitch = - std::clamp(static_cast(camera_perspective.pitch), -PITCH_CLIP, PITCH_CLIP); - - camera_transform.orientation = - glm::quat(glm::vec3(-camera_perspective.pitch, -camera_perspective.yaw, 0.0)); -} - void Camera::aspect_ratio_update(entt::registry& registry) { float aspect_ratio = registry.ctx().get().aspect_ratio; auto camera_view = registry.view(); - auto camera_entity = camera_view.front(); - if (camera_entity == entt::null) { - spdlog::debug("No camera entity found"); - return; + for (auto [entity, camera] : camera_view.each()) { + // Orthographic projection currently unsupported + auto& camera_perspective = std::get(camera.projection); + camera_perspective.aspect_ratio = aspect_ratio; } - - auto [camera] = camera_view.get(camera_entity); - - // Orthographic projection currently unsupported - auto& camera_perspective = std::get(camera.projection); - camera_perspective.aspect_ratio = aspect_ratio; } diff --git a/src/lib/core/camera.h b/src/lib/core/camera.h index 440bdd0..bac1dd2 100644 --- a/src/lib/core/camera.h +++ b/src/lib/core/camera.h @@ -2,7 +2,6 @@ #include "components/transform.h" -#include #include #include #include @@ -15,11 +14,6 @@ struct Camera static constexpr glm::vec3 UP_VECTOR = glm::vec3(0.0, 1.0, 0.0); - static constexpr float SPEED = 0.5; - static constexpr float ACCELERATION = 5.0; - - static constexpr glm::vec3 DEFAULT_POSITION = glm::vec3(0.0, 0.25, -1.0); - struct Perspective { float fov = DEFAULT_FOV; diff --git a/src/lib/scene/gltf.cpp b/src/lib/scene/gltf.cpp index cfe3a35..0033c21 100644 --- a/src/lib/scene/gltf.cpp +++ b/src/lib/scene/gltf.cpp @@ -84,16 +84,6 @@ auto Gltf::spawn_scene(std::size_t index, registry.get(scene_entity).children.push_back(node_entity); } - auto camera_view = registry.view(); - if (camera_view.empty()) { - // Spawn default camera - auto entity = registry.create(); - registry.emplace(entity, "Camera"); - registry.emplace(entity, Transform{.translation = Camera::DEFAULT_POSITION}); - registry.emplace(entity, GlobalTransform{}); - registry.emplace(entity, Camera{.projection = Camera::Perspective{}}); - } - return scene_entity; } @@ -113,12 +103,32 @@ auto Gltf::spawn_scene(std::string_view name, return entt::null; } -auto Gltf::spawn_default_scene(entt::registry& registry, entt::resource_cache& node_cache) - -> entt::entity +auto Gltf::spawn_default_scene(entt::registry& registry, + entt::resource_cache& node_cache) -> entt::entity { - if (document.scene != -1) { - return spawn_scene(document.scene, registry, node_cache); + if (document.scene == -1) { + return entt::null; } - return entt::null; + auto scene = spawn_scene(document.scene, registry, node_cache); + + // Convert meshes + auto mesh_view = registry.view>(); + for (auto [entity, mesh] : mesh_view.each()) { + registry.emplace(entity, GpuMesh(mesh)); + + // Remove Mesh resource as it is no longer needed. + registry.erase>(entity); + } + + // Convert materials + auto material_view = registry.view>(); + for (auto [entity, material] : material_view.each()) { + registry.emplace(entity, GpuMaterial(material)); + + // Remove Material resource as it is no longer needed. + registry.erase>(entity); + } + + return scene; }