Brenta Engine 1.1
Loading...
Searching...
No Matches
camera.cpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2024 Giovanni Santini
5
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#include "camera.hpp"
28
29#include "engine_logger.hpp"
30#include "screen.hpp"
31
32#include <cmath>
33
34using namespace brenta;
35
36camera::camera(enums::camera_type camera_type,
37 enums::projection_type projection_type, glm::vec3 position,
38 glm::vec3 world_up, glm::vec3 center, float movement_speed,
39 float mouse_sensitivity, float zoom,
40 types::spherical_coordinates spherical_coordinates,
41 types::euler_angles euler_angles, glm::vec3 front, glm::vec3 up,
42 glm::vec3 right)
43{
44 this->camera_type = camera_type;
45 this->projection_type = projection_type;
46 this->position = position;
47 this->world_up = world_up;
48 this->center = center;
49 this->movement_speed = movement_speed;
50 this->mouse_sensitivity = mouse_sensitivity;
51 this->zoom = zoom;
52 this->spherical_coordinates = spherical_coordinates;
53 this->euler_angles = euler_angles;
54 this->front = front;
55 this->up = up;
56 this->right = right;
57
58 /* Update the camera */
59 switch (this->camera_type)
60 {
61 case enums::camera_type::SPHERICAL:
63 break;
64 case enums::camera_type::AIRCRAFT:
66 break;
67 default:
68 break;
69 }
70
71 INFO("Camera created");
72}
73
75{
76 switch (camera_type)
77 {
78 case enums::camera_type::SPHERICAL:
79 return glm::lookAt(this->position, this->center, this->world_up);
80 case enums::camera_type::AIRCRAFT:
81 return glm::lookAt(this->position, this->position + this->front,
82 this->up);
83 default:
84 return glm::mat4(1.0f);
85 }
86}
87
89{
90 switch (this->projection_type)
91 {
92 case enums::projection_type::PERSPECTIVE:
93 return glm::perspective(glm::radians(this->zoom),
94 (float) screen::get_width()
95 / (float) screen::get_height(),
96 0.1f, 1000.0f);
97 case enums::projection_type::ORTHOGRAPHIC:
98 return glm::ortho((float) -screen::get_width() / 2.0f,
99 (float) screen::get_width() / 2.0f,
100 (float) -screen::get_height() / 2.0f,
101 (float) screen::get_height() / 2.0f, 0.1f, 100.0f);
102 default:
103 return glm::mat4(1.0f);
104 }
105}
106
108{
109 this->position.x = sin(this->spherical_coordinates.theta)
110 * cos(this->spherical_coordinates.phi)
111 * this->spherical_coordinates.radius
112 + this->center.x;
113 this->position.y = cos(this->spherical_coordinates.theta)
114 * this->spherical_coordinates.radius
115 + this->center.y;
116 this->position.z = sin(spherical_coordinates.theta)
117 * sin(this->spherical_coordinates.phi)
118 * this->spherical_coordinates.radius
119 + this->center.z;
120}
121
122/* For aircraft camera */
124{
125 /* calculate the new Front vector */
126 glm::vec3 new_front;
127 new_front.x = cos(glm::radians(euler_angles.yaw))
128 * cos(glm::radians(euler_angles.pitch));
129 new_front.y = sin(glm::radians(euler_angles.pitch));
130 new_front.z = sin(glm::radians(euler_angles.yaw))
131 * cos(glm::radians(euler_angles.pitch));
132 this->front = glm::normalize(new_front);
133 /* also re-calculate the Right and Up vector */
134 this->right = glm::normalize(glm::cross(this->front, this->world_up));
135 this->up = glm::normalize(glm::cross(this->right, this->front));
136}
137
138enums::camera_type camera::get_camera_type()
139{
140 return this->camera_type;
141}
142
143void camera::set_camera_type(enums::camera_type camera_type)
144{
145 this->camera_type = camera_type;
146}
147
148enums::projection_type camera::get_projection_type()
149{
150 return this->projection_type;
151}
152
153void camera::set_projection_type(enums::projection_type projection_type)
154{
155 this->projection_type = projection_type;
156}
157
159{
160 return this->position;
161}
162
163void camera::set_position(glm::vec3 position)
164{
165 this->position = position;
166}
167
169{
170 return this->world_up;
171}
172
173void camera::set_world_up(glm::vec3 world_up)
174{
175 this->world_up = world_up;
176}
177
179{
180 return this->center;
181}
182
183void camera::set_center(glm::vec3 center)
184{
185 this->center = center;
186}
187
189{
190 return this->movement_speed;
191}
192
193void camera::set_movement_speed(float movement_speed)
194{
195 this->movement_speed = movement_speed;
196}
197
199{
200 return this->mouse_sensitivity;
201}
202
203void camera::set_mouse_sensitivity(float mouse_sensitivity)
204{
205 this->mouse_sensitivity = mouse_sensitivity;
206}
207
209{
210 return this->zoom;
211}
212
213void camera::set_zoom(float zoom)
214{
215 this->zoom = zoom;
216}
217
222
224 types::spherical_coordinates spherical_coordinates)
225{
226 this->spherical_coordinates = spherical_coordinates;
227}
228
233
238
240{
241 return this->front;
242}
243
244void camera::set_front(glm::vec3 front)
245{
246 this->front = front;
247}
248
249glm::vec3 camera::get_up()
250{
251 return this->up;
252}
253
254void camera::set_up(glm::vec3 up)
255{
256 this->up = up;
257}
258
260{
261 return this->right;
262}
263
264void camera::set_right(glm::vec3 right)
265{
266 this->right = right;
267}
268
270{
271 return this->first_mouse;
272}
273
274void camera::set_first_mouse(bool first_mouse)
275{
276 this->first_mouse = first_mouse;
277}
278
280{
281 return this->last_x;
282}
283
284void camera::set_last_x(float last_x)
285{
286 this->last_x = last_x;
287}
288
290{
291 return this->last_y;
292}
293
294void camera::set_last_y(float last_y)
295{
296 this->last_y = last_y;
297}
298
300camera::builder::set_camera_type(enums::camera_type camera_type)
301{
302 this->camera_type = camera_type;
303 return *this;
304}
305
307camera::builder::set_projection_type(enums::projection_type projection_type)
308{
309 this->projection_type = projection_type;
310 return *this;
311}
312
313camera::builder &camera::builder::set_position(glm::vec3 position)
314{
315 this->position = position;
316 return *this;
317}
318
319camera::builder &camera::builder::set_world_up(glm::vec3 world_up)
320{
321 this->world_up = world_up;
322 return *this;
323}
324
325camera::builder &camera::builder::set_center(glm::vec3 center)
326{
327 this->center = center;
328 return *this;
329}
330
331camera::builder &camera::builder::set_movement_speed(float movement_speed)
332{
333 this->movement_speed = movement_speed;
334 return *this;
335}
336
337camera::builder &camera::builder::set_mouse_sensitivity(float mouse_sensitivity)
338{
339 this->mouse_sensitivity = mouse_sensitivity;
340 return *this;
341}
342
343camera::builder &camera::builder::set_zoom(float zoom)
344{
345 this->zoom = zoom;
346 return *this;
347}
348
349camera::builder &camera::builder::set_spherical_coordinates(
351{
352 this->spherical_coordinates = spherical_coordinates;
353 return *this;
354}
355
357camera::builder::set_euler_angles(types::euler_angles euler_angles)
358{
359 this->euler_angles = euler_angles;
360 return *this;
361}
362
363camera::builder &camera::builder::set_front(glm::vec3 front)
364{
365 this->front = front;
366 return *this;
367}
368
369camera::builder &camera::builder::set_up(glm::vec3 up)
370{
371 this->up = up;
372 return *this;
373}
374
375camera::builder &camera::builder::set_right(glm::vec3 right)
376{
377 this->right = right;
378 return *this;
379}
380
381brenta::camera camera::camera::builder::build()
382{
386}
Builder pattern for the Camera class.
Definition camera.hpp:470
The Camera class.
Definition camera.hpp:148
types::spherical_coordinates spherical_coordinates
Spherical coordinates.
Definition camera.hpp:195
float get_zoom()
Get the zoom level.
Definition camera.cpp:208
glm::vec3 get_center()
Get the center of the sphere.
Definition camera.cpp:178
glm::vec3 front
Front vector.
Definition camera.hpp:209
void set_last_y(float last_y)
Set the last y position of the mouse.
Definition camera.cpp:294
glm::vec3 get_position()
Get the position of the camera.
Definition camera.cpp:158
float last_x
Last x position of the mouse.
Definition camera.hpp:232
void set_zoom(float zoom)
Set the zoom level.
Definition camera.cpp:213
glm::vec3 get_right()
Get the right vector.
Definition camera.cpp:259
types::spherical_coordinates get_spherical_coordinates()
Get the spherical coordinates of the camera.
Definition camera.cpp:218
glm::vec3 get_up()
Get the up vector.
Definition camera.cpp:249
void set_first_mouse(bool first_mouse)
Set the first mouse flag.
Definition camera.cpp:274
void set_last_x(float last_x)
Set the last x position of the mouse.
Definition camera.cpp:284
void set_camera_type(enums::camera_type camera_type)
Set the camera type.
Definition camera.cpp:143
void set_up(glm::vec3 up)
Set the up vector.
Definition camera.cpp:254
void set_mouse_sensitivity(float mouse_sensitivity)
Set the mouse sensitivity.
Definition camera.cpp:203
void set_world_up(glm::vec3 world_up)
Set the world up vector.
Definition camera.cpp:173
glm::vec3 right
Right vector.
Definition camera.hpp:221
glm::vec3 world_up
The world up vector.
Definition camera.hpp:167
types::euler_angles get_euler_angles()
Get the euler angles of the camera.
Definition camera.cpp:229
glm::vec3 get_front()
Get the front vector.
Definition camera.cpp:239
float get_last_x()
Get the last x position of the mouse.
Definition camera.cpp:279
void update_camera_euler()
Update the camera euler angles.
Definition camera.cpp:123
enums::camera_type get_camera_type()
Get the camera type.
Definition camera.cpp:138
void set_projection_type(enums::projection_type projection_type)
Set the projection type.
Definition camera.cpp:153
enums::camera_type camera_type
Camera type.
Definition camera.hpp:157
float movement_speed
Space translational movement speed.
Definition camera.hpp:178
float mouse_sensitivity
Mouse sensitivity.
Definition camera.hpp:182
bool get_first_mouse()
Get the first mouse flag.
Definition camera.cpp:269
camera()=default
Default constructor.
types::euler_angles euler_angles
Euler angles.
Definition camera.hpp:203
void spherical_to_cartesian()
Update the camera spherical coordinates.
Definition camera.cpp:107
float get_movement_speed()
Get the movement speed.
Definition camera.cpp:188
void set_movement_speed(float movement_speed)
Set the movement speed.
Definition camera.cpp:193
enums::projection_type projection_type
Projection type.
Definition camera.hpp:153
void set_right(glm::vec3 right)
Set the right vector.
Definition camera.cpp:264
glm::mat4 get_projection_matrix()
Get the projection matrix.
Definition camera.cpp:88
void set_spherical_coordinates(types::spherical_coordinates spherical_coordinates)
Set the spherical coordinates of the camera.
Definition camera.cpp:223
void set_euler_angles(types::euler_angles euler_angles)
Set the euler angles of the camera.
Definition camera.cpp:234
float zoom
Zoom level (field of view)
Definition camera.hpp:186
glm::mat4 get_view_matrix()
Get the view matrix.
Definition camera.cpp:74
void set_center(glm::vec3 center)
Set the center of the sphere.
Definition camera.cpp:183
float get_mouse_sensitivity()
Get the mouse sensitivity.
Definition camera.cpp:198
bool first_mouse
Is the first mouse movement?
Definition camera.hpp:228
glm::vec3 center
The center of the spehere.
Definition camera.hpp:174
void set_front(glm::vec3 front)
Set the front vector.
Definition camera.cpp:244
enums::projection_type get_projection_type()
Get the projection type.
Definition camera.cpp:148
glm::vec3 up
Up vector.
Definition camera.hpp:215
glm::vec3 get_world_up()
Get the world up vector.
Definition camera.cpp:168
glm::vec3 position
Camera position.
Definition camera.hpp:161
float get_last_y()
Get the last y position of the mouse.
Definition camera.cpp:289
float last_y
Last y position of the mouse.
Definition camera.hpp:236
void set_position(glm::vec3 position)
Set the position of the camera.
Definition camera.cpp:163
static int get_width()
Get the width of the window.
Definition screen.cpp:105
static int get_height()
Get the height of the window.
Definition screen.cpp:110
Spherical coordinates.
Definition camera.hpp:46