CG-Project2
Loading...
Searching...
No Matches
Camera.hpp
1#pragma once
2
3#include <glm/ext/matrix_clip_space.hpp>
4#include <glm/fwd.hpp>
5#include <glm/glm.hpp>
6#include <glm/gtc/quaternion.hpp>
7#include <iostream>
8
9namespace ogl {
10 inline const float YAW = -90.f;
11 inline const float PITCH = 0.f;
12 inline const float ROLL = 0.f;
13 inline const float SPEED = 0.2f;
14 inline const float SENSITIVITY = 0.02f;
15 inline const float ZOOM = 45.f;
16 inline const glm::vec3 POSITION = glm::vec3(0, 0, 3);
17 inline const float TB_SPEED = 20.f;
18
20 glm::vec3 cameraPos = POSITION;
21 glm::vec3 cameraFront = glm::vec3(0, 0, -1);
22 glm::vec3 cameraUp = glm::vec3(0, 1, 0);
23 glm::vec3 cameraRight = glm::vec3(1, 0, 0);
24 glm::vec3 cameraDirection = glm::vec3(0);
25 glm::vec3 cameraTarget = glm::vec3(0);
26 };
27
28 struct CameraInfo {
29 float speed = SPEED;
30 float sensitivity = SENSITIVITY;
31 float zoom = ZOOM;
32 float ratio = 1;
33 float tbSpeed = TB_SPEED;
34 };
35
37 float yaw = YAW;
38 float pitch = PITCH;
39 float roll = ROLL;
40 glm::quat rotQuat{};
41 };
42
43 class Camera {
44 public:
45 glm::mat4 getViewProjMatrix() const;
46
47 inline glm::mat4 getProjMatrix() const { return this->m_proj; }
48 inline glm::mat4 getViewMatrix() const { return this->m_view; }
49
50 // Updates the Proj * View results BUT it assumes that the scene is 2D.
51 inline void updateOrthoProjection(const float &left, const float &right, const float &bot, const float &top) {
52 this->m_proj = glm::ortho(left, right, bot, top);
53 this->m_view = glm::mat4(1);
54 this->m_viewProj = this->m_proj * this->m_view;
55 }
56
57 inline void updatePerspProjection(const float &fov, const float &width, const float &height, const float &near, const float &far) {
58 this->m_proj = glm::perspective(glm::radians(fov), width / height, near, far);
59 this->m_info.ratio = width / height;
60 this->updateCameraVectors();
61 }
62
63 inline void updatePerspProjection(const float &fov, const float &ratio, const float &near, const float &far) {
64 this->m_proj = glm::perspective(glm::radians(fov), ratio, near, far);
65 this->m_info.ratio = ratio;
66 this->updateCameraVectors();
67 }
68
69 void moveCamera(const glm::vec3 &axis);
70
71 void processMouseMovement(const float &xoffset, const float &yoffset, const bool &constrainPitch = true);
72
73 inline void setCameraFront(glm::vec3 vec) { this->m_vectors.cameraFront = vec; }
74
75 inline glm::vec3 getCameraFront() const { return this->m_vectors.cameraFront; }
76
77 inline glm::vec3 getCameraUp() const { return this->m_vectors.cameraUp; }
78
79 inline void setCameraVelocity(const float velocity) { this->m_info.speed = velocity; }
80
81 inline float getCameraVelocity() const { return this->m_info.speed; }
82
83 inline glm::vec3 getCameraRight() const { return this->m_vectors.cameraRight; }
84
85 inline glm::vec3 getCameraPosition() const { return this->m_vectors.cameraPos; }
86
87 void setCameraPosition(const glm::vec3 &position);
88
89 inline float getMouseSensitivity() const { return this->m_info.sensitivity; }
90
91 inline void setMouseSensitivity(float val) { this->m_info.sensitivity = val; }
92
93 inline float getCameraZoom() const { return this->m_info.zoom; }
94
95 inline void setCameraZoom(const float zoom) {
96 this->m_info.zoom = zoom;
97 this->updatePerspProjection(this->m_info.zoom, this->m_info.ratio, 0.1f, 100.f);
98 }
99
100 inline CameraRotation getCameraRotation() const { return this->m_rotation; }
101
102 inline void setCameraYaw(const float &yaw) {
103 this->m_rotation.yaw = yaw;
104 this->updateCameraVectors();
105 }
106
107 inline void setCameraPitch(const float &pitch) {
108 this->m_rotation.pitch = pitch;
109 this->updateCameraVectors();
110 }
111
112 inline float getTrackballSpeed() { return this->m_info.tbSpeed; }
113
114 inline void setTrackballSpeed(const float speed) { this->m_info.tbSpeed = speed; }
115
116 inline glm::vec3 getCameraDirection() const { return this->m_vectors.cameraDirection; }
117
118 inline void setCameraDirection(glm::vec3 dir) { this->m_vectors.cameraDirection = dir; }
119
120 inline glm::vec3 getCameraTarget() const { return this->m_vectors.cameraTarget; }
121
122 inline void setCameraTarget(glm::vec3 target) { this->m_vectors.cameraTarget = target; }
123
124 Camera() { this->updateCameraVectors(); }
125
126 ~Camera() = default;
127
128 private:
129 glm::vec3 m_worldUp = glm::vec3(0, 1, 0);
130 CameraVectors m_vectors{};
131 CameraInfo m_info{};
132 CameraRotation m_rotation{};
133
134 glm::mat4 m_view = glm::mat4(0);
135 glm::mat4 m_proj = glm::mat4(0);
136 glm::mat4 m_viewProj = glm::mat4(0);
137
138 void updateCameraVectors();
139 };
140} // namespace ogl
Definition Camera.hpp:28
Definition Camera.hpp:36
Definition Camera.hpp:19