galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Camera.cpp
Go to the documentation of this file.
1
7
8#include <algorithm>
9
10#include <glm/gtc/matrix_transform.hpp>
11#include <glm/gtc/type_ptr.hpp>
12
13#include "Camera.hpp"
14
15const constexpr auto identity_matrix = glm::mat4 {1.0f};
16const constexpr auto rotation_vec = glm::vec3 {0.0f, 0.0f, 1.0f};
17
18namespace galaxy
19{
20 Camera::Camera() noexcept
21 : m_allow_rotation {false}
22 , m_translation_speed {1.0f}
23 , m_rotation_speed {1.0f}
24 , m_pos {0.0f, 0.0f}
25 , m_rotation {0.0f}
26 , m_scale {1.0f, 1.0f}
27 , m_origin {0.0f, 0.0f}
28 , m_dirty {true}
29 {
30 }
31
32 Camera::Camera(Camera&& c) noexcept
33 {
34 this->m_allow_rotation = c.m_allow_rotation;
35 this->m_translation_speed = c.m_translation_speed;
36 this->m_rotation_speed = c.m_rotation_speed;
37 this->m_data.m_model_view = std::move(c.m_data.m_model_view);
38 this->m_data.m_projection = std::move(c.m_data.m_projection);
39 this->m_pos = std::move(c.m_pos);
40 this->m_rotation = c.m_rotation;
41 this->m_scale = std::move(c.m_scale);
42 this->m_origin = std::move(c.m_origin);
43 this->m_dirty = c.m_dirty;
44 this->m_transform = std::move(c.m_transform);
45 }
46
48 {
49 if (this != &c)
50 {
51 this->m_allow_rotation = c.m_allow_rotation;
52 this->m_translation_speed = c.m_translation_speed;
53 this->m_rotation_speed = c.m_rotation_speed;
54 this->m_data.m_model_view = std::move(c.m_data.m_model_view);
55 this->m_data.m_projection = std::move(c.m_data.m_projection);
56 this->m_pos = std::move(c.m_pos);
57 this->m_rotation = c.m_rotation;
58 this->m_scale = std::move(c.m_scale);
59 this->m_origin = std::move(c.m_origin);
60 this->m_dirty = c.m_dirty;
61 this->m_transform = std::move(c.m_transform);
62 }
63
64 return *this;
65 }
66
67 Camera::Camera(const Camera& c) noexcept
68 {
69 this->m_allow_rotation = c.m_allow_rotation;
70 this->m_translation_speed = c.m_translation_speed;
71 this->m_rotation_speed = c.m_rotation_speed;
72 this->m_data.m_model_view = c.m_data.m_model_view;
73 this->m_data.m_projection = c.m_data.m_projection;
74 this->m_pos = c.m_pos;
75 this->m_rotation = c.m_rotation;
76 this->m_scale = c.m_scale;
77 this->m_origin = c.m_origin;
78 this->m_dirty = c.m_dirty;
79 this->m_transform = c.m_transform;
80 }
81
82 Camera& Camera::operator=(const Camera& c) noexcept
83 {
84 if (this != &c)
85 {
86 this->m_allow_rotation = c.m_allow_rotation;
87 this->m_translation_speed = c.m_translation_speed;
88 this->m_rotation_speed = c.m_rotation_speed;
89 this->m_data.m_model_view = c.m_data.m_model_view;
90 this->m_data.m_projection = c.m_data.m_projection;
91 this->m_pos = c.m_pos;
92 this->m_rotation = c.m_rotation;
93 this->m_scale = c.m_scale;
94 this->m_origin = c.m_origin;
95 this->m_dirty = c.m_dirty;
96 this->m_transform = c.m_transform;
97 }
98
99 return *this;
100 }
101
103 {
104 }
105
106 void Camera::set_projection(const float left, const float right, const float bottom, const float top) noexcept
107 {
108 // By default sets origin to center of right, bottom.
109 m_origin.x = right * 0.5f;
110 m_origin.y = bottom * 0.5f;
111
112 m_data.m_projection = glm::ortho(left, right, bottom, top, -1.0f, 1.0f);
113
114 m_dirty = true;
115 }
116
117 void Camera::move(const float x, const float y) noexcept
118 {
119 m_pos.x += (x * m_translation_speed);
120 m_pos.y += (y * m_translation_speed);
121
122 m_dirty = true;
123 }
124
125 void Camera::move_x(const float x) noexcept
126 {
127 m_pos.x += (x * m_translation_speed);
128 m_dirty = true;
129 }
130
131 void Camera::move_y(const float y) noexcept
132 {
133 m_pos.y += (y * m_translation_speed);
134 m_dirty = true;
135 }
136
137 void Camera::rotate(const float degrees) noexcept
138 {
139 m_rotation += (degrees * m_rotation_speed);
140 m_rotation = std::clamp(m_rotation, 0.0f, 360.0f);
141
142 m_dirty = true;
143 }
144
145 void Camera::scale(const float scale) noexcept
146 {
147 m_scale.x = scale;
148 m_scale.y = scale;
149
150 m_dirty = true;
151 }
152
153 void Camera::set_scale_horizontal(const float x) noexcept
154 {
155 m_scale.x = x;
156 m_dirty = true;
157 }
158
159 void Camera::set_scale_vertical(const float y) noexcept
160 {
161 m_scale.y = y;
162 m_dirty = true;
163 }
164
165 void Camera::set_rotation(const float degrees) noexcept
166 {
167 m_rotation = std::clamp(degrees, 0.0f, 360.0f);
168 m_dirty = true;
169 }
170
171 void Camera::set_positon(const float x, const float y) noexcept
172 {
173 m_pos.x = x;
174 m_pos.y = y;
175
176 m_dirty = true;
177 }
178
179 void Camera::set_positon_horizontal(const float x) noexcept
180 {
181 m_pos.x = x;
182 m_dirty = true;
183 }
184
185 void Camera::set_positon_vertical(const float y) noexcept
186 {
187 m_pos.y = y;
188 m_dirty = true;
189 }
190
191 void Camera::set_origin(const float x, const float y) noexcept
192 {
193 m_origin.x = x;
194 m_origin.y = y;
195
196 m_dirty = true;
197 }
198
199 void Camera::reset() noexcept
200 {
201 m_pos = {0.0f, 0.0f};
202 m_rotation = 0.0f;
203 m_scale = {1.0f, 1.0f};
204 m_transform = {1.0f};
205
206 m_dirty = true;
207 }
208
209 const glm::vec2& Camera::get_pos() const noexcept
210 {
211 return m_pos;
212 }
213
214 float Camera::get_rotation() const noexcept
215 {
216 return m_rotation;
217 }
218
219 const glm::vec2& Camera::get_scale() const noexcept
220 {
221 return m_scale;
222 }
223
224 const glm::vec2& Camera::get_origin() const noexcept
225 {
226 return m_origin;
227 }
228
229 glm::mat4& Camera::get_transform() noexcept
230 {
231 recalculate();
232 return m_transform;
233 }
234
235 const glm::mat4& Camera::get_model_view() noexcept
236 {
237 recalculate();
238 return m_data.m_model_view;
239 }
240
241 const glm::mat4& Camera::get_proj() noexcept
242 {
243 return m_data.m_projection;
244 }
245
247 {
248 recalculate();
249 return m_data;
250 }
251
252 void Camera::recalculate() noexcept
253 {
254 if (m_dirty)
255 {
256 const auto origin = glm::vec3 {m_origin, 0.0f};
257
258 auto rotation = glm::translate(identity_matrix, origin);
259 rotation = glm::rotate(rotation, glm::radians(m_rotation), rotation_vec);
260 rotation = glm::translate(rotation, -origin);
261
262 auto scale = glm::translate(identity_matrix, origin);
263 scale = glm::scale(scale, {m_scale, 1.0f});
264 scale = glm::translate(scale, -origin);
265
266 m_transform = glm::translate(identity_matrix, {m_pos, 0.0f}) * rotation * scale;
267 m_data.m_model_view = glm::inverse(m_transform);
268 m_dirty = false;
269 }
270 }
271} // namespace galaxy
const constexpr auto identity_matrix
Camera.cpp galaxy.
Definition Camera.cpp:15
const constexpr auto rotation_vec
Definition Camera.cpp:16
Orthographic 2D camera.
Definition Camera.hpp:20
void set_positon(const float x, const float y) noexcept
Set postion of camera.
Definition Camera.cpp:171
void move(const float x, const float y) noexcept
Move position.
Definition Camera.cpp:117
Camera & operator=(Camera &&) noexcept
Move assignment operator.
Definition Camera.cpp:47
Data m_data
Camera data.
Definition Camera.hpp:264
glm::mat4 m_transform
Combined transform.
Definition Camera.hpp:297
Camera() noexcept
Constructor.
Definition Camera.cpp:20
void set_scale_vertical(const float y) noexcept
Set entity scale.
Definition Camera.cpp:159
void scale(const float scale) noexcept
Set entity scale.
Definition Camera.cpp:145
void reset() noexcept
Reset transform.
Definition Camera.cpp:199
void move_x(const float x) noexcept
Move on x axis.
Definition Camera.cpp:125
const glm::mat4 & get_model_view() noexcept
Retrieve internal transformation matrix.
Definition Camera.cpp:235
void set_positon_vertical(const float y) noexcept
Set postion of camera.
Definition Camera.cpp:185
glm::vec2 m_pos
Cached for easy retrieval. Pos.
Definition Camera.hpp:270
void set_rotation(const float degrees) noexcept
Set camera rotation.
Definition Camera.cpp:165
glm::mat4 & get_transform() noexcept
Retrieve internal transformation matrix.
Definition Camera.cpp:229
void recalculate() noexcept
Recalculates the model view matrix.
Definition Camera.cpp:252
void set_origin(const float x, const float y) noexcept
Set the origin point.
Definition Camera.cpp:191
void move_y(const float y) noexcept
Move on y axis.
Definition Camera.cpp:131
const glm::vec2 & get_scale() const noexcept
Get stored scale.
Definition Camera.cpp:219
float m_rotation
Cached for easy retrieval. Rotation.
Definition Camera.hpp:276
const glm::vec2 & get_pos() const noexcept
Get stored position.
Definition Camera.cpp:209
void rotate(const float degrees) noexcept
Rotate entity.
Definition Camera.cpp:137
bool m_dirty
Flag to see if transform needs to be recalculated.
Definition Camera.hpp:292
void set_positon_horizontal(const float x) noexcept
Set postion of camera.
Definition Camera.cpp:179
const glm::mat4 & get_proj() noexcept
Get the Camera projection.
Definition Camera.cpp:241
glm::vec2 m_scale
Cached for easy retrieval. Scale.
Definition Camera.hpp:282
float get_rotation() const noexcept
Get stored rotation.
Definition Camera.cpp:214
glm::vec2 m_origin
Transform origin point.
Definition Camera.hpp:287
Data & get_data() noexcept
Get camera view and proj.
Definition Camera.cpp:246
const glm::vec2 & get_origin() const noexcept
Get origin point.
Definition Camera.cpp:224
~Camera() noexcept
Destructor.
Definition Camera.cpp:102
void set_scale_horizontal(const float x) noexcept
Set entity scale.
Definition Camera.cpp:153
void set_projection(const float left, const float right, const float bottom, const float top) noexcept
Set camera projection.
Definition Camera.cpp:106
Animated.cpp galaxy.
Definition Animated.cpp:16
Camera data.
Definition Camera.hpp:26
glm::mat4 m_projection
Camera projection matrix.
Definition Camera.hpp:35
glm::mat4 m_model_view
Combined transformation matrix.
Definition Camera.hpp:30