Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
CameraSystem.hpp
Go to the documentation of this file.
1#pragma once
2#include "../Registry.hpp"
4#include <GLFW/glfw3.h>
5#include <glm/gtc/matrix_transform.hpp>
6#include "../System.hpp"
12
17class CameraSystem : public System {
18public:
27
32 void update(float dt) override {
33 int width = 0;
34 int height = 0;
35 glfwGetWindowSize(renderer.getWindow(), &width, &height);
36 float aspect = 1.0f;
37 if (height > 0) {
38 aspect = static_cast<float>(width) / static_cast<float>(height);
39 }
40
41 if (!editorMode.isPlaying) {
42 // Editor Mode: update and render through the Editor Camera ONLY
43 for (auto [entity, cam, transform, input] : registry.view<Camera, Transform, InputComponent>()) {
44 if (registry.has<EditorCamera>(entity)) {
45 cam.aspect = aspect;
46
47 // Update rotation (fly mode)
48 transform.rotation.y += input.look.x * cam.mouseSensitivity;
49 transform.rotation.x += input.look.y * cam.mouseSensitivity;
50 transform.rotation.x = glm::clamp(transform.rotation.x, -89.f, 89.f);
51
52 // Compute directions
53 glm::vec3 forward;
54 forward.x = cos(glm::radians(transform.rotation.x)) * cos(glm::radians(transform.rotation.y));
55 forward.y = sin(glm::radians(transform.rotation.x));
56 forward.z = cos(glm::radians(transform.rotation.x)) * sin(glm::radians(transform.rotation.y));
57
58 float forwardLen = glm::length(forward);
59 forward = (forwardLen > 1e-4f) ? glm::normalize(forward) : glm::vec3(0.0f, 0.0f, -1.0f);
60
61 glm::vec3 crossProduct = glm::cross(forward, glm::vec3(0, 1, 0));
62 glm::vec3 right = glm::vec3(1, 0, 0);
63 if (glm::length(crossProduct) > 1e-4f) {
64 right = glm::normalize(crossProduct);
65 }
66
67 glm::vec3 upCross = glm::cross(right, forward);
68 glm::vec3 up = glm::vec3(0, 1, 0);
69 if (glm::length(upCross) > 1e-4f) {
70 up = glm::normalize(upCross);
71 }
72
73 glm::vec3 move =
74 forward * input.movement.z +
75 right * input.movement.x +
76 up * input.movement.y;
77
78 transform.position += move * cam.moveSpeed * dt;
79 renderer.setActiveCamera(cam.projection(), transform.position, cam.view(transform));
80 return; // Done
81 }
82 }
83 } else {
84 // Play Mode: update and render through the Scene's Camera (the first non-editor camera)
85 Entity fallbackCam = Entity();
86 for (auto [entity, cam, transform] : registry.view<Camera, Transform>()) {
87 if (!registry.has<EditorCamera>(entity)) {
88 cam.aspect = aspect;
89 // Note: Scene cameras are animated/controlled by scripts/physics, not editor controls
90 glm::mat4 vp = cam.projection() * cam.view(transform);
91 renderer.setActiveCamera(cam.projection(), transform.position, cam.view(transform));
92 renderer.setGameplayCamera(vp, transform.position);
93 return; // Done
94 } else {
95 fallbackCam = entity;
96 }
97 }
98
99 // Fallback to Editor Camera if no scene camera exists
100 if (fallbackCam.getId() != Entity::INVALID_ENTITY && registry.isValid(fallbackCam)) {
101 auto* cam = registry.get<Camera>(fallbackCam);
102 auto* transform = registry.get<Transform>(fallbackCam);
103 if (cam && transform) {
104 cam->aspect = aspect;
105 glm::mat4 vp = cam->projection() * cam->view(*transform);
106 renderer.setActiveCamera(cam->projection(), transform->position, cam->view(*transform));
107 renderer.setGameplayCamera(vp, transform->position);
108 }
109 }
110 }
111 }
112
113private:
120};
CameraSystem(Registry &reg, VulkanRenderer &renderer, EditorModeState &editorMode)
Construct a new Camera System object.
Definition CameraSystem.hpp:25
VulkanRenderer & renderer
Reference to the renderer.
Definition CameraSystem.hpp:117
Registry & registry
Reference to the entity registry.
Definition CameraSystem.hpp:115
void update(float dt) override
Updates the camera's rotation, movement, aspect ratio, and view-projection matrices.
Definition CameraSystem.hpp:32
EditorModeState & editorMode
Reference to the editor mode state.
Definition CameraSystem.hpp:119
Lightweight identifier representing a game object in the Entity Component System.
Definition Entity.hpp:11
static constexpr IdType INVALID_ENTITY
Constant representing a null or invalid entity.
Definition Entity.hpp:16
IdType getId() const
Retrieve the raw identifier of the entity.
Definition Entity.hpp:32
Coordinates entities and components, providing creation, removal, lookup, and views.
Definition Registry.hpp:30
Base class for ECS systems that process sets of entities.
Definition System.hpp:12
Manages core Vulkan pipeline states, device interaction, frames, buffers, and command submissions.
Definition VulkanRenderer.hpp:127
Represents a camera component for rendering and viewing.
Definition Camera.hpp:14
glm::mat4 view(const Transform &transform) const
Calculates the view matrix based on the given transform.
Definition Camera.hpp:46
Tag component identifying the persistent editor-only camera entity.
Definition EditorCamera.hpp:9
Struct keeping track of active editor states (e.g. fly mode vs editor UI interaction).
Definition EditorModeState.hpp:7
Component representing input state for an entity (e.g., player or camera).
Definition inputComponent.hpp:9
Represents the position, rotation, and scale of an entity in 3D space.
Definition Transform.hpp:127