Brenta Engine 1.2
Loading...
Searching...
No Matches
camera.hpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#pragma once
7
8#include <glad/glad.h>
9#include <glm/glm.hpp>
10#include <glm/gtc/matrix_transform.hpp>
11
12namespace brenta
13{
14
15namespace types
16{
17
25{
26 float theta;
27 float phi;
28 float radius;
29
30 spherical_coordinates() = default;
31 spherical_coordinates(float theta, float phi, float radius)
32 : theta(theta), phi(phi), radius(radius)
33 {
34 }
35};
36
44{
45 float yaw;
46 float pitch;
47 float roll;
48
49 euler_angles() = default;
50 euler_angles(float yaw, float pitch, float roll)
51 : yaw(yaw), pitch(pitch), roll(roll)
52 {
53 }
54};
55
56} // namespace types
57
80class camera
81{
82public:
83
84 enum class camera_type
85 {
86 aircraft,
87 spherical
88 };
89
90 enum class projection_type
91 {
92 perspective,
93 orthographic
94 };
95
103 {
104 forward,
105 backward,
106 left,
107 right
108 };
109
110 projection_type proj_type;
111 camera_type cam_type;
112 glm::vec3 position;
118 glm::vec3 world_up;
125 glm::vec3 center;
130 float mouse_sensitivity;
134 float zoom;
157 glm::vec3 front;
163 glm::vec3 up;
169 glm::vec3 right;
176 bool first_mouse = true;
180 float last_x;
184 float last_y;
185
186 class config;
187
188 // Constructors
189
195 camera() = default;
202 camera(config conf);
203
210 class builder;
211
212 // Getters
213
214 camera::camera_type get_camera_type() const;
215 camera::projection_type get_projection_type() const;
216 glm::vec3 get_position() const;
217 glm::vec3 get_world_up() const;
218 glm::vec3 get_center() const;
219 float get_movement_speed() const;
220 float get_mouse_sensitivity() const;
221 float get_zoom() const;
222 types::spherical_coordinates get_spherical_coordinates() const;
223 types::euler_angles get_euler_angles() const;
224 glm::mat4 get_view_matrix() const;
225 glm::mat4 get_projection_matrix(int window_width, int window_height) const;
226 glm::vec3 get_front() const;
227 glm::vec3 get_up() const;
228 glm::vec3 get_right() const;
233 bool get_first_mouse() const;
238 float get_last_x() const;
243 float get_last_y() const;
244
245 // Setters
246
247 void set_camera_type(camera::camera_type camera_type);
248 void set_projection_type(camera::projection_type projection_type);
249 void set_world_up(glm::vec3 world_up);
250 void set_center(glm::vec3 center);
251 void set_movement_speed(float movement_speed);
252 void set_mouse_sensitivity(float mouse_sensitivity);
253 void set_zoom(float zoom);
254 void
255 set_spherical_coordinates(types::spherical_coordinates spherical_coordinates);
256 void set_euler_angles(types::euler_angles euler_angles);
257 void set_front(glm::vec3 front);
258 void set_up(glm::vec3 up);
259 void set_right(glm::vec3 right);
260 void set_position(glm::vec3 position);
261 void set_first_mouse(bool first_mouse);
266 void set_last_x(float last_x);
271 void set_last_y(float last_y);
272
273 // Utilities
274
282 void update_camera_euler();
291};
292
294{
295 camera_type cam_type;
296 projection_type proj_type;
297 glm::vec3 position;
298 glm::vec3 world_up;
299 glm::vec3 center;
300 float movement_speed;
301 float mouse_sensitivity;
302 float zoom;
303 types::spherical_coordinates spherical_coordinates;
304 types::euler_angles euler_angles;
305 glm::vec3 front;
306 glm::vec3 up;
307 glm::vec3 right;
308};
309
314{
315private:
316
317 camera::config conf = {
318 camera::camera_type::aircraft,
319 camera::projection_type::perspective,
320 glm::vec3(0.0f, 0.0f, 0.0f),
321 glm::vec3(0.0f, 1.0f, 0.0f),
322 glm::vec3(0.0f, 0.0f, 0.0f),
323 2.5f,
324 0.1f,
325 45.0f,
326 {0.0f, 0.0f, 10.0f},
327 {0.0f, 0.0f, 0.0f},
328 glm::vec3(0.0f, 0.0f, -1.0f),
329 glm::vec3(0.0f, 1.0f, 0.0f),
330 glm::vec3(1.0f, 0.0f, 0.0f),
331 };
332
333public:
334
335 builder &camera_type(camera::camera_type camera_type);
336 builder &projection_type(camera::projection_type projection_type);
337 builder &position(glm::vec3 position);
338 builder &world_up(glm::vec3 worldUp);
339 builder &center(glm::vec3 center);
341 builder &mouse_sensitivity(float mouse_sensitivity);
342 builder &zoom(float zoom);
343 builder &
346 builder &front(glm::vec3 front);
347 builder &up(glm::vec3 up);
348 builder &right(glm::vec3 right);
349
350 camera build();
351};
352
353} // namespace brenta
Builder pattern for the Camera class.
Definition camera.hpp:314
The Camera class.
Definition camera.hpp:81
types::spherical_coordinates spherical_coordinates
Spherical coordinates.
Definition camera.hpp:143
bool get_first_mouse() const
Get the first mouse flag.
Definition camera.cpp:236
glm::vec3 front
Front vector.
Definition camera.hpp:157
void set_last_y(float last_y)
Set the last y position of the mouse.
Definition camera.cpp:261
float last_x
Last x position of the mouse.
Definition camera.hpp:180
camera_movement
Camera movement directions.
Definition camera.hpp:103
void set_last_x(float last_x)
Set the last x position of the mouse.
Definition camera.cpp:251
float get_last_x() const
Get the last x position of the mouse.
Definition camera.cpp:246
glm::vec3 right
Right vector.
Definition camera.hpp:169
glm::vec3 world_up
The world up vector.
Definition camera.hpp:118
void update_camera_euler()
Update the camera euler angles.
Definition camera.cpp:90
float movement_speed
Space translational movement speed.
Definition camera.hpp:129
camera()=default
Default constructor.
types::euler_angles euler_angles
Euler angles.
Definition camera.hpp:151
void spherical_to_cartesian()
Update the camera spherical coordinates.
Definition camera.cpp:74
float get_last_y() const
Get the last y position of the mouse.
Definition camera.cpp:256
float zoom
Zoom level (field of view)
Definition camera.hpp:134
bool first_mouse
Is the first mouse movement?
Definition camera.hpp:176
glm::vec3 center
The center of the spehere.
Definition camera.hpp:125
glm::vec3 up
Up vector.
Definition camera.hpp:163
float last_y
Last y position of the mouse.
Definition camera.hpp:184
Spherical coordinates.
Definition camera.hpp:25