Restructure project

This commit is contained in:
2024-12-07 15:46:02 +01:00
parent 991093d352
commit e4378dcddc
52 changed files with 36 additions and 37 deletions

1
apps/CMakeLists.txt Normal file
View File

@@ -0,0 +1 @@
add_subdirectory(fall-fever)

View File

@@ -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)

View File

@@ -0,0 +1,63 @@
#include "controller.h"
#include "flycam.h"
#include "components/name.h"
#include "components/transform.h"
#include "core/camera.h"
#include "core/light.h"
#include "window/window.h"
#include <spdlog/spdlog.h>
using namespace entt::literals;
Controller::Controller(std::string_view path)
{
spdlog::info("Open {}", path);
std::filesystem::path document_path(path);
entt::hashed_string document_hash(document_path.string().c_str());
entt::resource<Gltf> gltf_document =
gltf_cache.load(document_hash, document_path).first->second;
gltf_document->spawn_default_scene(registry(), gltf_node_cache);
// Spawn default lights
auto directional_light = registry().create();
registry().emplace<Name>(directional_light, "Directional Light");
registry().emplace<Transform>(
directional_light,
Transform{.orientation = glm::toQuat(
glm::lookAt({}, DirectionalLight::DEFAULT_DIRECTION, Camera::UP_VECTOR))});
registry().emplace<GlobalTransform>(directional_light, GlobalTransform{});
registry().emplace<DirectionalLight>(
directional_light, DirectionalLight{.illuminance = DirectionalLight::DEFAULT_ILLUMINANCE});
auto point_light = registry().create();
registry().emplace<Name>(point_light, "Point Light");
registry().emplace<Transform>(point_light,
Transform{.translation = PointLight::DEFAULT_POSITION});
registry().emplace<GlobalTransform>(point_light, GlobalTransform{});
registry().emplace<PointLight>(point_light,
PointLight{.intensity = PointLight::DEFAULT_INTENSITY});
// Spawn default camera
auto camera_view = registry().view<Camera const>();
if (camera_view.empty()) {
auto entity = registry().create();
registry().emplace<Name>(entity, "Camera");
registry().emplace<Transform>(entity, Transform{.translation = glm::vec3(0.0, 0.25, -1.0)});
registry().emplace<GlobalTransform>(entity, GlobalTransform{});
registry().emplace<Camera>(entity, Camera{.projection = Camera::Perspective{}});
registry().emplace<Flycam>(entity);
}
}
void Controller::update()
{
Flycam::keyboard_movement(registry());
if (registry().ctx().get<Window::MouseCatched>().catched) {
Flycam::mouse_orientation(registry());
}
}

View File

@@ -0,0 +1,13 @@
#pragma once
#include "core/application.h"
#include <entt/entt.hpp>
#include <glm/glm.hpp>
class Controller : public FeverCore::Application
{
public:
Controller(std::string_view path);
void update() override;
};

View File

@@ -0,0 +1 @@
// todo camera movement here ...

View File

@@ -0,0 +1,98 @@
#include "flycam.h"
#include "core/camera.h"
#include "core/time.h"
#include "input/input.h"
#include <GLFW/glfw3.h>
#include <algorithm>
#include <chrono>
#include <spdlog/spdlog.h>
void Flycam::keyboard_movement(entt::registry& registry)
{
struct KeyboardMovementContext
{
bool accelerate{};
};
auto& movement_context = registry.ctx().emplace<KeyboardMovementContext>();
auto const& key_state = registry.ctx().get<Input::State<Input::KeyCode>>();
auto const& delta_time = registry.ctx().get<Time::Delta>();
auto camera_view =
registry.view<Flycam const, Camera const, Transform, GlobalTransform const>();
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<float>(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<Flycam const, Camera, Transform>();
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<Input::MouseMotion>();
auto delta_x = mouse_cursor_input.delta.x;
auto delta_y = mouse_cursor_input.delta.y;
auto pitch = static_cast<float>(-delta_y);
auto yaw = static_cast<float>(delta_x);
// Orthographic projection currently unsupported
auto& camera_perspective = std::get<Camera::Perspective>(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<float>(camera_perspective.pitch), -PITCH_CLIP, PITCH_CLIP);
camera_transform.orientation =
glm::quat(glm::vec3(-camera_perspective.pitch, -camera_perspective.yaw, 0.0));
}

12
apps/fall-fever/flycam.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
#include <entt/entt.hpp>
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);
};

39
apps/fall-fever/main.cpp Normal file
View File

@@ -0,0 +1,39 @@
#include "controller.h"
#include "util/log.h"
#include <GLFW/glfw3.h>
#include <argparse/argparse.hpp>
#include <spdlog/spdlog.h>
auto main(int argc, char* argv[]) -> int
{
Log::initialize();
argparse::ArgumentParser program("Fall-Fever");
program.add_argument("model").help("model file to load");
try {
program.parse_args(argc, argv);
} catch (std::exception const& err) {
std::cerr << err.what() << std::endl;
std::cerr << program;
return 1;
}
auto model = program.get<std::string>("model");
// Initialize GLFW
if (glfwInit() == 0) {
spdlog::critical("Could not initialize GLFW");
return -1;
}
{
// Create controller
Controller controller(model);
controller.run();
}
glfwTerminate();
return 0;
}