Brenta Engine 1.1
Loading...
Searching...
No Matches
camera.hpp
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#pragma once
28
29#include <glad/glad.h>
30#include <glm/glm.hpp>
31#include <glm/gtc/matrix_transform.hpp>
32
33namespace brenta
34{
35
36namespace types
37{
38
46{
47 float theta;
48 float phi;
49 float radius;
60 spherical_coordinates(float theta, float phi, float radius)
61 : theta(theta), phi(phi), radius(radius)
62 {
63 }
64};
65
73{
74 float yaw;
75 float pitch;
76 float roll;
80 euler_angles() = default;
87 euler_angles(float yaw, float pitch, float roll)
88 : yaw(yaw), pitch(pitch), roll(roll)
89 {
90 }
91};
92
93} // namespace types
94
95namespace enums
96{
97
98enum camera_type
99{
100 AIRCRAFT,
101 SPHERICAL
102};
103
104enum projection_type
105{
106 PERSPECTIVE,
107 ORTHOGRAPHIC
108};
109
116enum camera_movement
117{
118 FORWARD,
119 BACKWARD,
120 LEFT,
121 RIGHT
122};
123
124} // namespace enums
125
148{
149 public:
153 enums::projection_type projection_type;
157 enums::camera_type camera_type;
161 glm::vec3 position;
167 glm::vec3 world_up;
174 glm::vec3 center;
186 float zoom;
209 glm::vec3 front;
215 glm::vec3 up;
221 glm::vec3 right;
228 bool first_mouse = true;
232 float last_x;
236 float last_y;
237
238 /* Constructors */
239
245 camera() = default;
251 camera(enums::camera_type camera_type,
252 enums::projection_type projection_type, glm::vec3 position,
253 glm::vec3 world_up, glm::vec3 center, float movement_speed,
254 float mouse_sensitivity, float zoom,
256 types::euler_angles euler_angles, glm::vec3 front, glm::vec3 up,
257 glm::vec3 right);
258
265 class builder;
266
267 /* Getters */
268
273 enums::camera_type get_camera_type();
278 enums::projection_type get_projection_type();
283 glm::vec3 get_position();
288 glm::vec3 get_world_up();
293 glm::vec3 get_center();
298 float get_movement_speed();
303 float get_mouse_sensitivity();
308 float get_zoom();
323 glm::mat4 get_view_matrix();
328 glm::mat4 get_projection_matrix();
333 glm::vec3 get_front();
338 glm::vec3 get_up();
343 glm::vec3 get_right();
348 bool get_first_mouse();
353 float get_last_x();
358 float get_last_y();
359
360 /* Setters */
361
366 void set_camera_type(enums::camera_type camera_type);
371 void set_projection_type(enums::projection_type projection_type);
376 void set_world_up(glm::vec3 world_up);
381 void set_center(glm::vec3 center);
396 void set_zoom(float zoom);
412 void set_front(glm::vec3 front);
417 void set_up(glm::vec3 up);
422 void set_right(glm::vec3 right);
427 void set_position(glm::vec3 position);
432 void set_first_mouse(bool first_mouse);
437 void set_last_x(float last_x);
442 void set_last_y(float last_y);
443
444 /* Utilities */
445
454 void update_camera_euler();
464};
465
470{
471 private:
472 enums::camera_type camera_type = enums::camera_type::AIRCRAFT;
473 enums::projection_type projection_type =
474 enums::projection_type::PERSPECTIVE;
475 glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
476 glm::vec3 world_up = glm::vec3(0.0f, 1.0f, 0.0f);
477 glm::vec3 center = glm::vec3(0.0f, 0.0f, 0.0f);
478 float movement_speed = 2.5f;
479 float mouse_sensitivity = 0.1f;
480 float zoom = 45.0f;
481 glm::vec3 front = glm::vec3(0.0f, 0.0f, -1.0f);
482 glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
483 glm::vec3 right = glm::vec3(1.0f, 0.0f, 0.0f);
484 types::spherical_coordinates spherical_coordinates = {0.0f, 0.0f, 10.0f};
485 types::euler_angles euler_angles = {0.0f, 0.0f, 0.0f};
486
487 public:
488 builder &set_camera_type(enums::camera_type camera_type);
489 builder &set_projection_type(enums::projection_type projection_type);
490 builder &set_position(glm::vec3 position);
491 builder &set_world_up(glm::vec3 worldUp);
492 builder &set_center(glm::vec3 center);
493 builder &set_movement_speed(float movement_speed);
494 builder &set_mouse_sensitivity(float mouse_sensitivity);
495 builder &set_zoom(float zoom);
496 builder &set_spherical_coordinates(
497 types::spherical_coordinates spherical_coordinates);
498 builder &set_euler_angles(types::euler_angles euler_angles);
499 builder &set_front(glm::vec3 front);
500 builder &set_up(glm::vec3 up);
501 builder &set_right(glm::vec3 right);
502
503 camera build();
504};
505
511extern camera default_camera;
512
513} // namespace brenta
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
euler_angles(float yaw, float pitch, float roll)
Constructor.
Definition camera.hpp:87
euler_angles()=default
Default constructor.
Spherical coordinates.
Definition camera.hpp:46
spherical_coordinates(float theta, float phi, float radius)
Constructor.
Definition camera.hpp:60
spherical_coordinates()=default
Default constructor.