Brenta Engine 1.0
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;
35using namespace Brenta::Utils;
36
37Camera::Camera(Enums::CameraType camera_type,
38 Enums::ProjectionType projection_type, glm::vec3 position,
39 glm::vec3 worldUp, glm::vec3 center, float movementSpeed,
40 float mouseSensitivity, float zoom,
41 Types::SphericalCoordinates sphericalCoordinates,
42 Types::EulerAngles eulerAngles, glm::vec3 front, glm::vec3 up,
43 glm::vec3 right)
44{
45 this->CameraType = camera_type;
46 this->ProjectionType = projection_type;
47 this->Position = position;
48 this->WorldUp = worldUp;
49 this->center = center;
50 this->MovementSpeed = movementSpeed;
51 this->MouseSensitivity = mouseSensitivity;
52 this->Zoom = zoom;
53 this->sphericalCoordinates = sphericalCoordinates;
54 this->eulerAngles = eulerAngles;
55 this->Front = front;
56 this->Up = up;
57 this->Right = right;
58
59 /* Update the camera */
60 switch (this->CameraType)
61 {
62 case Enums::CameraType::SPHERICAL:
64 break;
65 case Enums::CameraType::AIRCRAFT:
67 break;
68 default:
69 break;
70 }
71
72 INFO("Camera created");
73}
74
76{
77 switch (CameraType)
78 {
79 case Enums::CameraType::SPHERICAL:
80 return glm::lookAt(Position, center, WorldUp);
81 case Enums::CameraType::AIRCRAFT:
82 return glm::lookAt(Position, Position + Front, Up);
83 default:
84 return glm::mat4(1.0f);
85 }
86}
87
89{
90 switch (ProjectionType)
91 {
92 case Enums::ProjectionType::PERSPECTIVE:
93 return glm::perspective(glm::radians(Zoom),
94 (float) Screen::GetWidth()
95 / (float) Screen::GetHeight(),
96 0.1f, 1000.0f);
97 case Enums::ProjectionType::ORTHOGRAPHIC:
98 return glm::ortho((float) -Screen::GetWidth() / 2.0f,
99 (float) Screen::GetWidth() / 2.0f,
100 (float) -Screen::GetHeight() / 2.0f,
101 (float) Screen::GetHeight() / 2.0f, 0.1f, 100.0f);
102 default:
103 return glm::mat4(1.0f);
104 }
105}
106
108{
109 Position.x = sin(sphericalCoordinates.theta) * cos(sphericalCoordinates.phi)
110 * sphericalCoordinates.radius
111 + center.x;
113 + center.y;
114 Position.z = sin(sphericalCoordinates.theta) * sin(sphericalCoordinates.phi)
115 * sphericalCoordinates.radius
116 + center.z;
117}
118
119/* For aircraft camera */
121{
122 /* calculate the new Front vector */
123 glm::vec3 front;
124 front.x = cos(glm::radians(eulerAngles.yaw))
125 * cos(glm::radians(eulerAngles.pitch));
126 front.y = sin(glm::radians(eulerAngles.pitch));
127 front.z = sin(glm::radians(eulerAngles.yaw))
128 * cos(glm::radians(eulerAngles.pitch));
129 Front = glm::normalize(front);
130 /* also re-calculate the Right and Up vector */
131 Right = glm::normalize(glm::cross(Front, WorldUp));
132 Up = glm::normalize(glm::cross(Right, Front));
133}
134
135Enums::CameraType Camera::GetCameraType()
136{
137 return CameraType;
138}
139
140void Camera::SetCameraType(Enums::CameraType cameraType)
141{
142 Camera::CameraType = cameraType;
143}
144
145Enums::ProjectionType Camera::GetProjectionType()
146{
147 return ProjectionType;
148}
149
150void Camera::SetProjectionType(Enums::ProjectionType projectionType)
151{
152 Camera::ProjectionType = projectionType;
153}
154
156{
157 return Position;
158}
159
160void Camera::SetPosition(glm::vec3 position)
161{
162 Camera::Position = position;
163}
164
166{
167 return WorldUp;
168}
169
170void Camera::SetWorldUp(glm::vec3 worldUp)
171{
172 Camera::WorldUp = worldUp;
173}
174
176{
177 return center;
178}
179
180void Camera::SetCenter(glm::vec3 center)
181{
183}
184
186{
187 return MovementSpeed;
188}
189
190void Camera::SetMovementSpeed(float movementSpeed)
191{
192 Camera::MovementSpeed = movementSpeed;
193}
194
196{
197 return MouseSensitivity;
198}
199
200void Camera::SetMouseSensitivity(float mouseSensitivity)
201{
202 Camera::MouseSensitivity = mouseSensitivity;
203}
204
206{
207 return Zoom;
208}
209
210void Camera::SetZoom(float zoom)
211{
212 Camera::Zoom = zoom;
213}
214
219
225
230
235
237{
238 return Front;
239}
240
241void Camera::SetFront(glm::vec3 front)
242{
243 Camera::Front = front;
244}
245
246glm::vec3 Camera::GetUp()
247{
248 return Up;
249}
250
251void Camera::SetUp(glm::vec3 up)
252{
253 Camera::Up = up;
254}
255
257{
258 return Right;
259}
260
261void Camera::SetRight(glm::vec3 right)
262{
263 Camera::Right = right;
264}
265
267{
268 return firstMouse;
269}
270
271void Camera::SetFirstMouse(bool firstMouse)
272{
274}
275
277{
278 return lastX;
279}
280
281void Camera::SetLastX(float lastX)
282{
284}
285
287{
288 return lastY;
289}
290
291void Camera::SetLastY(float lastY)
292{
294}
295
296Camera::Builder &Camera::Builder::set_camera_type(Enums::CameraType camera_type)
297{
298 this->camera_type = camera_type;
299 return *this;
300}
301
303Camera::Builder::set_projection_type(Enums::ProjectionType projection_type)
304{
305 this->projection_type = projection_type;
306 return *this;
307}
308
309Camera::Builder &Camera::Builder::set_position(glm::vec3 position)
310{
311 this->position = position;
312 return *this;
313}
314
315Camera::Builder &Camera::Builder::set_world_up(glm::vec3 worldUp)
316{
317 this->worldUp = worldUp;
318 return *this;
319}
320
321Camera::Builder &Camera::Builder::set_center(glm::vec3 center)
322{
323 this->center = center;
324 return *this;
325}
326
327Camera::Builder &Camera::Builder::set_movement_speed(float movementSpeed)
328{
329 this->movementSpeed = movementSpeed;
330 return *this;
331}
332
333Camera::Builder &Camera::Builder::set_mouse_sensitivity(float mouseSensitivity)
334{
335 this->mouseSensitivity = mouseSensitivity;
336 return *this;
337}
338
339Camera::Builder &Camera::Builder::set_zoom(float zoom)
340{
341 this->zoom = zoom;
342 return *this;
343}
344
345Camera::Builder &Camera::Builder::set_spherical_coordinates(
347{
348 this->sphericalCoordinates = sphericalCoordinates;
349 return *this;
350}
351
353Camera::Builder::set_eulerAngles(Types::EulerAngles eulerAngles)
354{
355 this->eulerAngles = eulerAngles;
356 return *this;
357}
358
359Camera::Builder &Camera::Builder::set_front(glm::vec3 front)
360{
361 this->front = front;
362 return *this;
363}
364
365Camera::Builder &Camera::Builder::set_up(glm::vec3 up)
366{
367 this->up = up;
368 return *this;
369}
370
371Camera::Builder &Camera::Builder::set_right(glm::vec3 right)
372{
373 this->right = right;
374 return *this;
375}
376
377Camera Camera::Camera::Builder::build()
378{
379 return Camera(camera_type, projection_type, position, worldUp, center,
380 movementSpeed, mouseSensitivity, zoom, sphericalCoordinates,
381 eulerAngles, front, up, right);
382}
Builder pattern for the Camera class.
Definition camera.hpp:441
The Camera class.
Definition camera.hpp:120
glm::vec3 Position
Camera position.
Definition camera.hpp:133
Types::EulerAngles GetEulerAngles()
Get the euler angles of the camera.
Definition camera.cpp:226
Types::SphericalCoordinates GetSphericalCoordinates()
Get the spherical coordinates of the camera.
Definition camera.cpp:215
Types::SphericalCoordinates sphericalCoordinates
Spherical coordinates.
Definition camera.hpp:167
float GetLastX()
Get the last x position of the mouse.
Definition camera.cpp:276
float lastX
Last x position of the mouse.
Definition camera.hpp:204
void updateCameraEuler()
Update the camera euler angles.
Definition camera.cpp:120
float MouseSensitivity
Mouse sensitivity.
Definition camera.hpp:154
Enums::ProjectionType GetProjectionType()
Get the projection type.
Definition camera.cpp:145
void SetZoom(float zoom)
Set the zoom level.
Definition camera.cpp:210
Enums::CameraType GetCameraType()
Get the camera type.
Definition camera.cpp:135
void SetMovementSpeed(float movementSpeed)
Set the movement speed.
Definition camera.cpp:190
float GetZoom()
Get the zoom level.
Definition camera.cpp:205
glm::vec3 WorldUp
The world up vector.
Definition camera.hpp:139
void SetPosition(glm::vec3 position)
Set the position of the camera.
Definition camera.cpp:160
void SetLastY(float lastY)
Set the last y position of the mouse.
Definition camera.cpp:291
void SetCameraType(Enums::CameraType camera_type)
Set the camera type.
Definition camera.cpp:140
glm::vec3 GetRight()
Get the right vector.
Definition camera.cpp:256
float lastY
Last y position of the mouse.
Definition camera.hpp:208
Types::EulerAngles eulerAngles
Euler angles.
Definition camera.hpp:175
glm::vec3 GetPosition()
Get the position of the camera.
Definition camera.cpp:155
float GetMovementSpeed()
Get the movement speed.
Definition camera.cpp:185
float GetLastY()
Get the last y position of the mouse.
Definition camera.cpp:286
float MovementSpeed
Space translational movement speed.
Definition camera.hpp:150
bool firstMouse
Is the first mouse movement?
Definition camera.hpp:200
void SetRight(glm::vec3 right)
Set the right vector.
Definition camera.cpp:261
void SetEulerAngles(Types::EulerAngles eulerAngles)
Set the euler angles of the camera.
Definition camera.cpp:231
glm::vec3 GetCenter()
Get the center of the sphere.
Definition camera.cpp:175
void SetWorldUp(glm::vec3 worldUp)
Set the world up vector.
Definition camera.cpp:170
void SetLastX(float lastX)
Set the last x position of the mouse.
Definition camera.cpp:281
void SetCenter(glm::vec3 center)
Set the center of the sphere.
Definition camera.cpp:180
void SetProjectionType(Enums::ProjectionType projection_type)
Set the projection type.
Definition camera.cpp:150
float Zoom
Zoom level (field of view)
Definition camera.hpp:158
float GetMouseSensitivity()
Get the mouse sensitivity.
Definition camera.cpp:195
glm::vec3 Up
Up vector.
Definition camera.hpp:187
Enums::CameraType CameraType
Camera type.
Definition camera.hpp:129
Camera()=default
Default constructor.
glm::mat4 GetProjectionMatrix()
Get the projection matrix.
Definition camera.cpp:88
void SetFirstMouse(bool firstMouse)
Set the first mouse flag.
Definition camera.cpp:271
void SetUp(glm::vec3 up)
Set the up vector.
Definition camera.cpp:251
glm::vec3 Front
Front vector.
Definition camera.hpp:181
void SetSphericalCoordinates(Types::SphericalCoordinates sphericalCoordinates)
Set the spherical coordinates of the camera.
Definition camera.cpp:220
glm::vec3 GetWorldUp()
Get the world up vector.
Definition camera.cpp:165
glm::vec3 Right
Right vector.
Definition camera.hpp:193
void SetMouseSensitivity(float mouseSensitivity)
Set the mouse sensitivity.
Definition camera.cpp:200
bool GetFirstMouse()
Get the first mouse flag.
Definition camera.cpp:266
void SetFront(glm::vec3 front)
Set the front vector.
Definition camera.cpp:241
glm::vec3 GetFront()
Get the front vector.
Definition camera.cpp:236
glm::vec3 center
The center of the spehere.
Definition camera.hpp:146
Enums::ProjectionType ProjectionType
Projection type.
Definition camera.hpp:125
void SphericalToCartesian()
Update the camera spherical coordinates.
Definition camera.cpp:107
glm::vec3 GetUp()
Get the up vector.
Definition camera.cpp:246
glm::mat4 GetViewMatrix()
Get the view matrix.
Definition camera.cpp:75
static int GetHeight()
Get the height of the window.
Definition screen.cpp:110
static int GetWidth()
Get the width of the window.
Definition screen.cpp:105
Spherical coordinates.
Definition camera.hpp:60