Zen 0.3.0
Loading...
Searching...
No Matches
ZEN_Camera.cpp
1#include <zen/camera/ZEN_Camera.h>
2
3#include <algorithm>
4#include <glm/ext/matrix_clip_space.hpp>
5#include <glm/ext/matrix_transform.hpp>
6
7namespace Zen {
8 Camera::Camera(CameraType type) : m_cameraType(type) {
9 updateDirection();
10 updateProjection();
11 updateView();
12 }
13
14 void Camera::setCameraType(CameraType type) {
15 m_cameraType = type;
16 updateProjection();
17 updateView();
18 }
19
20 void Camera::setPerspective(float fov, float aspect, float near, float far) {
21 m_cameraType = CameraType::Perspective;
22 m_fov = fov;
23 m_aspectRatio = aspect;
24 m_near = near;
25 m_far = far;
26 updateProjection();
27 }
28
29 void Camera::setOrthographic(float left, float right, float bottom, float top) {
30 m_cameraType = CameraType::Orthographic;
31 m_orthoLeft = left;
32 m_orthoRight = right;
33 m_orthoBottom = bottom;
34 m_orthoTop = top;
35 updateProjection();
36 }
37
38 void Camera::setAspectRatio(float aspect) {
39 m_aspectRatio = aspect;
40 if (m_cameraType == CameraType::Perspective) {
41 updateProjection();
42 }
43 }
44
45 void Camera::setClip(float near, float far) {
46 m_near = near;
47 m_far = far;
48 if (m_cameraType == CameraType::Perspective) {
49 updateProjection();
50 }
51 }
52
53 void Camera::setPosition(const glm::vec3 &position) {
54 m_position = position;
55 updateView();
56 }
57
58 void Camera::setRotation(float yaw, float pitch, float roll) {
59 m_yaw = yaw;
60 m_pitch = pitch;
61 m_roll = roll;
62 clampPitch();
63 updateDirection();
64 updateView();
65 }
66
67 void Camera::setSpeed(float speed) { m_cameraSpeed = speed; }
68
69 void Camera::setSensitivity(float sensitivity) { m_cameraSensitivity = sensitivity; }
70
71 CameraType Camera::type() const { return m_cameraType; }
72
73 const glm::vec3 &Camera::position() const { return m_position; }
74
75 glm::vec3 Camera::rotation() const { return {m_yaw, m_pitch, m_roll}; }
76
77 float Camera::speed() const { return m_cameraSpeed; }
78
79 float Camera::sensitivity() const { return m_cameraSensitivity; }
80
81 float Camera::aspectRatio() const { return m_aspectRatio; }
82
83 const glm::mat4 &Camera::projectionMatrix() const { return m_projection; }
84
85 const glm::mat4 &Camera::viewMatrix() const { return m_view; }
86
87 glm::mat4 Camera::viewProjectionMatrix() const { return m_projection * m_view; }
88
89 void Camera::updateProjection() {
90 if (m_cameraType == CameraType::Perspective) {
91 m_projection = glm::perspective(glm::radians(m_fov), m_aspectRatio, m_near, m_far);
92 } else {
93 m_projection = glm::ortho(m_orthoLeft, m_orthoRight, m_orthoBottom, m_orthoTop, -1.0f, 1.0f);
94 }
95 }
96
97 void Camera::updateView() {
98 if (m_cameraType == CameraType::Orthographic) {
99
100 glm::mat4 translate = glm::translate(glm::mat4(1.0f), m_position);
101 glm::mat4 roll = glm::rotate(glm::mat4(1.0f), glm::radians(m_roll), glm::vec3(0, 0, 1));
102
103 m_view = glm::inverse(translate * roll);
104
105 } else {
106 const glm::vec3 target = m_position + m_front;
107
108 m_view = glm::lookAt(m_position, target, m_up);
109 }
110 }
111
112 void Camera::updateDirection() {
113 const float yaw = glm::radians(m_yaw);
114 const float pitch = glm::radians(m_pitch);
115
116 glm::vec3 front;
117 front.x = std::cos(yaw) * std::cos(pitch);
118 front.y = std::sin(pitch);
119 front.z = std::sin(yaw) * std::cos(pitch);
120 m_front = glm::normalize(front);
121
122 m_right = glm::normalize(glm::cross(m_front, m_worldUp));
123 m_up = glm::normalize(glm::cross(m_right, m_front));
124 }
125
126 void Camera::clampPitch() {
127 m_pitch = std::min(m_pitch, 89.0f);
128 m_pitch = std::max(m_pitch, -89.0f);
129 }
130
131} // namespace Zen