6#include <brenta/renderer/camera.hpp>
7#include <brenta/logger.hpp>
11using namespace brenta;
13Camera::Camera(Config conf)
15 this->proj_type = conf.proj_type;
17 this->world_up = conf.world_up;
18 this->front = conf.front;
20 this->right = conf.right;
22 this->set_pos(conf.pos);
26Camera::Camera(Builder& builder)
28 *
this = builder.build();
31glm::mat4 Camera::get_view_matrix()
const
35 Spherical spos = std::get<Spherical>(this->pos);
36 return glm::lookAt(this->transform.get_pos(), spos.center, this->world_up);
39 catch ([[maybe_unused]]
const std::bad_variant_access& ex)
41 return glm::lookAt(this->transform.get_pos(),
42 this->transform.get_pos() + this->front, this->up);
48glm::mat4 Camera::get_projection_matrix(
int window_width,
49 int window_height)
const
51 switch (this->proj_type)
53 case ProjectionType::Perspective:
58 return glm::perspective(glm::radians(this->fov),
60 / (
float) window_height,
62 case ProjectionType::Orthographic:
63 return glm::ortho((
float) -window_width / 2.0f,
64 (
float) window_width / 2.0f,
65 (
float) -window_height / 2.0f,
66 (
float) window_height / 2.0f, 0.1f, 100.0f);
68 return glm::mat4(1.0f);
72void Camera::update_spherical(Spherical pos)
74 this->transform.set_x(sin(pos.theta) * cos(pos.phi) * pos.radius + pos.center.x);
75 this->transform.set_y(cos(pos.theta) * pos.radius + pos.center.y);
76 this->transform.set_z(sin(pos.theta) * sin(pos.phi) * pos.radius + pos.center.z);
81void Camera::update_aircraft(Aircraft pos)
86 cos(glm::radians(pos.yaw)) * cos(glm::radians(pos.pitch));
87 new_front.y = sin(glm::radians(pos.pitch));
89 sin(glm::radians(pos.yaw)) * cos(glm::radians(pos.pitch));
90 this->front = glm::normalize(new_front);
92 this->right = glm::normalize(glm::cross(this->front, this->world_up));
93 this->up = glm::normalize(glm::cross(this->right, this->front));
94 this->transform.set_pos(pos.pos);
102Camera::ProjectionType Camera::get_projection_type()
const
104 return this->proj_type;
107std::variant<Camera::Spherical, Camera::Aircraft> Camera::get_pos()
const
114 return this->transform;
117float Camera::get_fov()
const
122glm::vec3 Camera::get_front()
const
127glm::vec3 Camera::get_up()
const
132glm::vec3 Camera::get_right()
const
141void Camera::set_projection_type(ProjectionType projection_type)
143 this->proj_type = projection_type;
146void Camera::set_pos(std::variant<Spherical, Aircraft> new_pos)
152 this->update_spherical(std::get<Spherical>(this->pos));
154 catch ([[maybe_unused]]
const std::bad_variant_access& ex)
156 this->update_aircraft(std::get<Aircraft>(this->pos));
162glm::vec3 Camera::get_world_up()
const
164 return this->world_up;
167void Camera::set_world_up(glm::vec3 world_up)
169 this->world_up = world_up;
172void Camera::set_fov(
float fov)
177void Camera::set_front(glm::vec3 front)
182void Camera::set_up(glm::vec3 up)
187void Camera::set_right(glm::vec3 right)
199Camera::Spherical::Builder::center(glm::vec3 center)
201 this->scam.center = center;
206Camera::Spherical::Builder::theta(
float theta)
208 this->scam.theta = theta;
213Camera::Spherical::Builder::phi(
float phi)
215 this->scam.phi = phi;
220Camera::Spherical::Builder::radius(
float radius)
222 this->scam.radius = radius;
234Camera::Aircraft::Builder::pos(glm::vec3 pos)
236 this->acam.pos = pos;
241Camera::Aircraft::Builder::yaw(
float yaw)
243 this->acam.yaw = yaw;
248Camera::Aircraft::Builder::pitch(
float pitch)
250 this->acam.pitch = pitch;
255Camera::Aircraft::Builder::roll(
float roll)
257 this->acam.roll = roll;
269Camera::Builder::projection_type(Camera::ProjectionType projection_type)
271 this->conf.proj_type = projection_type;
278 this->conf.pos = pos;
284 this->conf.fov = fov;
290 this->conf.world_up = world_up;
296 this->conf.front = front;
308 this->conf.right = right;
314 return Camera(this->conf);