Brenta Engine 1.2
Loading...
Searching...
No Matches
camera.cpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#include <brenta/camera.hpp>
7#include <brenta/logger.hpp>
8#include <cmath>
9
10using namespace brenta;
11
13{
14 this->cam_type = conf.cam_type;
15 this->proj_type = conf.proj_type;
16 this->position = conf.position;
17 this->world_up = conf.world_up;
18 this->center = conf.center;
19 this->movement_speed = conf.movement_speed;
20 this->mouse_sensitivity = conf.mouse_sensitivity;
21 this->zoom = conf.zoom;
22 this->spherical_coordinates = conf.spherical_coordinates;
23 this->euler_angles = conf.euler_angles;
24 this->front = conf.front;
25 this->up = conf.up;
26 this->right = conf.right;
27
28 // Update the camera
29 switch (this->cam_type)
30 {
31 case camera_type::spherical:
33 break;
34 case camera_type::aircraft:
36 break;
37 default:
38 break;
39 }
40}
41
42glm::mat4 camera::get_view_matrix()
43{
44 switch (this->cam_type)
45 {
46 case camera_type::spherical:
47 return glm::lookAt(this->position, this->center, this->world_up);
48 case camera_type::aircraft:
49 return glm::lookAt(this->position, this->position + this->front, this->up);
50 default:
51 return glm::mat4(1.0f);
52 }
53}
54
55glm::mat4 camera::get_projection_matrix(int window_width, int window_height)
56{
57 switch (this->proj_type)
58 {
59 case projection_type::perspective:
60 return glm::perspective(glm::radians(this->zoom),
61 (float) window_width
62 / (float) window_height,
63 0.1f, 1000.0f);
64 case projection_type::orthographic:
65 return glm::ortho((float) -window_width / 2.0f,
66 (float) window_width / 2.0f,
67 (float) -window_height / 2.0f,
68 (float) window_height / 2.0f, 0.1f, 100.0f);
69 default:
70 return glm::mat4(1.0f);
71 }
72}
73
75{
76 this->position.x = sin(this->spherical_coordinates.theta)
77 * cos(this->spherical_coordinates.phi)
78 * this->spherical_coordinates.radius
79 + this->center.x;
80 this->position.y =
81 cos(this->spherical_coordinates.theta) * this->spherical_coordinates.radius
82 + this->center.y;
83 this->position.z = sin(spherical_coordinates.theta)
84 * sin(this->spherical_coordinates.phi)
85 * this->spherical_coordinates.radius
86 + this->center.z;
87}
88
89// For aircraft camera
91{
92 // calculate the new Front vector
93 glm::vec3 new_front;
94 new_front.x =
95 cos(glm::radians(euler_angles.yaw)) * cos(glm::radians(euler_angles.pitch));
96 new_front.y = sin(glm::radians(euler_angles.pitch));
97 new_front.z =
98 sin(glm::radians(euler_angles.yaw)) * cos(glm::radians(euler_angles.pitch));
99 this->front = glm::normalize(new_front);
100 // also re-calculate the Right and Up vector
101 this->right = glm::normalize(glm::cross(this->front, this->world_up));
102 this->up = glm::normalize(glm::cross(this->right, this->front));
103}
104
105camera::camera_type camera::get_camera_type()
106{
107 return this->cam_type;
108}
109
110void camera::set_camera_type(camera::camera_type camera_type)
111{
112 this->cam_type = camera_type;
113}
114
115camera::projection_type camera::get_projection_type()
116{
117 return this->proj_type;
118}
119
120void camera::set_projection_type(projection_type projection_type)
121{
122 this->proj_type = projection_type;
123}
124
125glm::vec3 camera::get_position()
126{
127 return this->position;
128}
129
130void camera::set_position(glm::vec3 position)
131{
132 this->position = position;
133}
134
135glm::vec3 camera::get_world_up()
136{
137 return this->world_up;
138}
139
140void camera::set_world_up(glm::vec3 world_up)
141{
142 this->world_up = world_up;
143}
144
145glm::vec3 camera::get_center()
146{
147 return this->center;
148}
149
150void camera::set_center(glm::vec3 center)
151{
152 this->center = center;
153}
154
155float camera::get_movement_speed()
156{
157 return this->movement_speed;
158}
159
160void camera::set_movement_speed(float movement_speed)
161{
162 this->movement_speed = movement_speed;
163}
164
165float camera::get_mouse_sensitivity()
166{
167 return this->mouse_sensitivity;
168}
169
170void camera::set_mouse_sensitivity(float mouse_sensitivity)
171{
172 this->mouse_sensitivity = mouse_sensitivity;
173}
174
175float camera::get_zoom()
176{
177 return this->zoom;
178}
179
180void camera::set_zoom(float zoom)
181{
182 this->zoom = zoom;
183}
184
185types::spherical_coordinates camera::get_spherical_coordinates()
186{
187 return this->spherical_coordinates;
188}
189
190void camera::set_spherical_coordinates(
191 types::spherical_coordinates spherical_coordinates)
192{
193 this->spherical_coordinates = spherical_coordinates;
194}
195
196types::euler_angles camera::get_euler_angles()
197{
198 return this->euler_angles;
199}
200
201void camera::set_euler_angles(types::euler_angles euler_angles)
202{
203 this->euler_angles = euler_angles;
204}
205
206glm::vec3 camera::get_front()
207{
208 return this->front;
209}
210
211void camera::set_front(glm::vec3 front)
212{
213 this->front = front;
214}
215
216glm::vec3 camera::get_up()
217{
218 return this->up;
219}
220
221void camera::set_up(glm::vec3 up)
222{
223 this->up = up;
224}
225
226glm::vec3 camera::get_right()
227{
228 return this->right;
229}
230
231void camera::set_right(glm::vec3 right)
232{
233 this->right = right;
234}
235
237{
238 return this->first_mouse;
239}
240
241void camera::set_first_mouse(bool first_mouse)
242{
243 this->first_mouse = first_mouse;
244}
245
247{
248 return this->last_x;
249}
250
251void camera::set_last_x(float last_x)
252{
253 this->last_x = last_x;
254}
255
257{
258 return this->last_y;
259}
260
261void camera::set_last_y(float last_y)
262{
263 this->last_y = last_y;
264}
265
266//
267// Builder
268//
269
271camera::builder::camera_type(camera::camera_type camera_type)
272{
273 this->conf.cam_type = camera_type;
274 return *this;
275}
276
278camera::builder::projection_type(camera::projection_type projection_type)
279{
280 this->conf.proj_type = projection_type;
281 return *this;
282}
283
284camera::builder &camera::builder::position(glm::vec3 position)
285{
286 this->conf.position = position;
287 return *this;
288}
289
290camera::builder &camera::builder::world_up(glm::vec3 world_up)
291{
292 this->conf.world_up = world_up;
293 return *this;
294}
295
296camera::builder &camera::builder::center(glm::vec3 center)
297{
298 this->conf.center = center;
299 return *this;
300}
301
302camera::builder &camera::builder::movement_speed(float movement_speed)
303{
304 this->conf.movement_speed = movement_speed;
305 return *this;
306}
307
308camera::builder &camera::builder::mouse_sensitivity(float mouse_sensitivity)
309{
310 this->conf.mouse_sensitivity = mouse_sensitivity;
311 return *this;
312}
313
314camera::builder &camera::builder::zoom(float zoom)
315{
316 this->conf.zoom = zoom;
317 return *this;
318}
319
320camera::builder &camera::builder::spherical_coordinates(
322{
323 this->conf.spherical_coordinates = spherical_coordinates;
324 return *this;
325}
326
328camera::builder::euler_angles(types::euler_angles euler_angles)
329{
330 this->conf.euler_angles = euler_angles;
331 return *this;
332}
333
334camera::builder &camera::builder::front(glm::vec3 front)
335{
336 this->conf.front = front;
337 return *this;
338}
339
340camera::builder &camera::builder::up(glm::vec3 up)
341{
342 this->conf.up = up;
343 return *this;
344}
345
346camera::builder &camera::builder::right(glm::vec3 right)
347{
348 this->conf.right = right;
349 return *this;
350}
351
352brenta::camera camera::camera::builder::build()
353{
354 return camera(this->conf);
355}
Builder pattern for the Camera class.
Definition camera.hpp:330
The Camera class.
Definition camera.hpp:97
types::spherical_coordinates spherical_coordinates
Spherical coordinates.
Definition camera.hpp:159
glm::vec3 front
Front vector.
Definition camera.hpp:173
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:196
void set_last_x(float last_x)
Set the last x position of the mouse.
Definition camera.cpp:251
glm::vec3 right
Right vector.
Definition camera.hpp:185
glm::vec3 world_up
The world up vector.
Definition camera.hpp:134
float get_last_x()
Get the last x position of the mouse.
Definition camera.cpp:246
void update_camera_euler()
Update the camera euler angles.
Definition camera.cpp:90
float movement_speed
Space translational movement speed.
Definition camera.hpp:145
bool get_first_mouse()
Get the first mouse flag.
Definition camera.cpp:236
camera()=default
Default constructor.
types::euler_angles euler_angles
Euler angles.
Definition camera.hpp:167
void spherical_to_cartesian()
Update the camera spherical coordinates.
Definition camera.cpp:74
float zoom
Zoom level (field of view)
Definition camera.hpp:150
bool first_mouse
Is the first mouse movement?
Definition camera.hpp:192
glm::vec3 center
The center of the spehere.
Definition camera.hpp:141
glm::vec3 up
Up vector.
Definition camera.hpp:179
float get_last_y()
Get the last y position of the mouse.
Definition camera.cpp:256
float last_y
Last y position of the mouse.
Definition camera.hpp:200
Spherical coordinates.
Definition camera.hpp:25