Loading...
Searching...
No Matches
CameraComponent.cpp
Go to the documentation of this file.
1//
2// Created by denzel on 22/08/2025.
3//
4
5#include "hellfire/ecs/CameraComponent.h"
6
7#include "Entity.h"
8
9namespace hellfire {
12 }
13
15 camera_type_ = type;
16 projection_dirty_ = true;
17 }
18
19 void CameraComponent::set_perspective(float fov, float aspect, float near_plane, float far_plane) {
21 fov_ = fov;
22 aspect_ratio_ = aspect;
23 near_plane_ = near_plane;
24 far_plane_ = far_plane;
25 projection_dirty_ = true;
26 }
27
28 void CameraComponent::set_orthographic(float left, float right, float bottom, float top, float near_plane,
29 float far_plane) {
31 ortho_left_ = left;
32 ortho_right_ = right;
33 ortho_bottom_ = bottom;
34 ortho_top_ = top;
35 near_plane_ = near_plane;
36 far_plane_ = far_plane;
37 projection_dirty_ = true;
38 }
39
41 if (view_dirty_) {
43 }
44 return view_matrix_;
45 }
46
50 }
51 return projection_matrix_;
52 }
53
54 void CameraComponent::set_fov(float fov) {
55 fov_ = fov;
56 projection_dirty_ = true;
57 }
58
59 void CameraComponent::set_aspect_ratio(float aspect) {
60 if (aspect_ratio_ > 0.0f) {
61 aspect_ratio_ = aspect;
62 projection_dirty_ = true;
63 }
64 }
65
66 void CameraComponent::set_clip_planes(float near_plane, float far_plane) {
67 near_plane_ = near_plane;
68 far_plane_ = far_plane;
69 projection_dirty_ = true;
70 }
71
72 void CameraComponent::set_yaw(float yaw) {
73 camera_data_.yaw = yaw;
75 view_dirty_ = true;
76 }
77
78 void CameraComponent::set_pitch(float pitch) {
79 camera_data_.pitch = glm::clamp(pitch, -89.0f, 89.0f);
81 view_dirty_ = true;
82 }
83
84 void CameraComponent::set_mouse_sensitivity(float sensitivity) {
85 camera_data_.sensitivity = sensitivity;
86 }
87
88 void CameraComponent::look_at(const glm::vec3 &target) {
89 auto *transform = get_owner().get_component<TransformComponent>();
90 if (!transform) return;
91
92 target_ = target;
93 const glm::vec3 position = transform->get_position();
94 const glm::vec3 direction = glm::normalize(target - position);
95
96 camera_data_.pitch = glm::degrees(asin(direction.y));
97 camera_data_.yaw = glm::degrees(atan2(direction.z, direction.x));
98
100 view_dirty_ = true;
101 }
102
103 void CameraComponent::set_target(const glm::vec3 &target) {
104 target_ = target;
105 look_at(target);
106 }
107
108 CameraComponent * CameraComponent::create_perspective(float fov, float aspect, float near_plane, float far_plane) {
110 camera->set_perspective(fov, aspect, near_plane, far_plane);
111 return camera;
112 }
113
115 create_orthographic(float size, float aspect, float near_plane, float far_plane) {
117 const float half_width = size * aspect * 0.5f;
118 const float half_height = size * 0.5f;
119 camera->set_orthographic(-half_width, half_width, -half_height, half_height, near_plane, far_plane);
120 return camera;
121 }
122
124 // Calculate front vector from yaw and pitch
125 glm::vec3 front;
126 front.x = cos(glm::radians(camera_data_.yaw)) * cos(glm::radians(camera_data_.pitch));
127 front.y = sin(glm::radians(camera_data_.pitch));
128 front.z = sin(glm::radians(camera_data_.yaw)) * cos(glm::radians(camera_data_.pitch));
129 front_ = glm::normalize(front);
130
131 // Calculate right and up vectors
132 right_ = glm::normalize(glm::cross(front_, world_up_));
133 up_ = glm::normalize(glm::cross(right_, front_));
134 }
135
137 auto* transform = get_owner().get_component<TransformComponent>();
138 if (!transform) {
139 view_matrix_ = glm::mat4(1.0f);
140 view_dirty_ = false;
141 return;
142 }
143
144 const glm::vec3 position = transform->get_position();
145 view_matrix_ = glm::lookAt(position, position + front_, up_);
146 view_dirty_ = false;
147 }
148
151 projection_matrix_ = glm::perspective(glm::radians(fov_), aspect_ratio_, near_plane_, far_plane_);
152 } else {
153 projection_matrix_ = glm::ortho(ortho_left_, ortho_right_, ortho_bottom_, ortho_top_, near_plane_, far_plane_);
154 }
155 projection_dirty_ = false;
156 }
157
159 const float half_width = ortho_size_ * aspect_ratio_ * 0.5f;
160 const float half_height = ortho_size_ * 0.5f;
161 ortho_left_ = -half_width;
162 ortho_right_ = half_width;
163 ortho_bottom_ = -half_height;
164 ortho_top_ = half_height;
165 }
166}
void set_camera_type(CameraType type)
void look_at(const glm::vec3 &target)
void set_orthographic(float left, float right, float bottom, float top, float near_plane, float far_plane)
static CameraComponent * create_orthographic(float size=10.0f, float aspect=16.0f/9.0f, float near_plane=0.1f, float far_plane=100.0f)
glm::mat4 get_projection_matrix() const
void set_clip_planes(float near_plane, float far_plane)
static CameraComponent * create_perspective(float fov=45.0f, float aspect=16.0f/9.0f, float near_plane=0.1f, float far_plane=100.0f)
glm::mat4 get_view_matrix() const
CameraComponent(CameraType type=CameraType::PERSPECTIVE)
void set_mouse_sensitivity(float sensitivity)
void set_aspect_ratio(float aspect)
void set_perspective(float fov, float aspect, float near_plane, float far_plane)
void set_target(const glm::vec3 &target)
Entity & get_owner() const
Definition Component.h:13