aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-04-02 16:09:59 +0000
committerJoursoir <chat@joursoir.net>2021-04-02 16:09:59 +0000
commit5ccf499a04871129cb188f0ab7c376716a87c823 (patch)
treeb1c32db812cc5f67ba4a3a53e043bd7e0b1996b2
parent0256dcc887070341470fbaea5a2fc75c8bc73d74 (diff)
downloadspace-simulator-5ccf499a04871129cb188f0ab7c376716a87c823.tar.gz
space-simulator-5ccf499a04871129cb188f0ab7c376716a87c823.tar.bz2
space-simulator-5ccf499a04871129cb188f0ab7c376716a87c823.zip
add camera subsystem
-rw-r--r--src/graphics/Camera.cpp90
-rw-r--r--src/graphics/Camera.hpp49
2 files changed, 139 insertions, 0 deletions
diff --git a/src/graphics/Camera.cpp b/src/graphics/Camera.cpp
new file mode 100644
index 0000000..2058d5e
--- /dev/null
+++ b/src/graphics/Camera.cpp
@@ -0,0 +1,90 @@
+#include <glm/gtc/matrix_transform.hpp>
+
+#include "Camera.hpp"
+using namespace glm;
+
+Camera::Camera(vec3 a_pos, vec3 a_front, vec3 a_up,
+ GLfloat a_yaw, GLfloat a_pitch)
+ : move_speed(init_speed),
+ mouse_sensitivity(init_sensitivity),
+ fov(init_fov)
+{
+ position = a_pos;
+ up = a_up;
+ world_up = up;
+ yaw = a_yaw;
+ pitch = a_pitch;
+ UpdateVectors();
+}
+
+mat4 Camera::GetProjViewMatrix(int w_width, int w_height)
+{
+ return
+ perspective(fov, (GLfloat)w_width / (GLfloat)w_height, 0.1f, 100.0f) *
+ lookAt(position, position + front, up);
+}
+
+mat4 Camera::GetSkyboxMatrix(int w_width, int w_height)
+{
+ return
+ perspective(fov, (GLfloat)w_width / (GLfloat)w_height, 0.1f, 100.0f) *
+ mat4(mat3(lookAt(position, position + front, up)));
+}
+
+void Camera::Movement(camera_move direction, GLfloat delta_time)
+{
+ GLfloat velocity;
+ velocity = move_speed * delta_time;
+ switch(direction) {
+ case FORWARD:
+ position += front * velocity;
+ break;
+ case BACKWARD:
+ position -= front * velocity;
+ break;
+ case LEFT:
+ position -= right * velocity;
+ break;
+ case RIGHT:
+ position += right * velocity;
+ break;
+ }
+ UpdateVectors();
+}
+
+void Camera::View(GLfloat delta_x, GLfloat delta_y)
+{
+ yaw += delta_x * mouse_sensitivity;
+ pitch -= delta_y * mouse_sensitivity; // if '+' then inverse
+
+ if(pitch > 89.0f)
+ pitch = 89.0f;
+ else if(pitch < -89.0f)
+ pitch = -89.0f;
+
+ UpdateVectors();
+}
+
+void Camera::Fov(GLfloat delta_y)
+{
+ if(!delta_y)
+ return;
+
+ fov -= radians(delta_y);
+ if(fov > radians(45.0f))
+ fov = radians(45.0f);
+ else if(fov < radians(1.0f))
+ fov = radians(1.0f);
+}
+
+void Camera::UpdateVectors()
+{
+ front.x = cos(radians(yaw)) * cos(radians(pitch));
+ front.y = sin(radians(pitch));
+ front.z = sin(radians(yaw)) * cos(radians(pitch));
+
+ /* Normalize the vectors, because their length gets closer to 0
+ the more you look up or down which results in slower movement. */
+ right = normalize(cross(front, world_up));
+ up = normalize(cross(right, front));
+}
diff --git a/src/graphics/Camera.hpp b/src/graphics/Camera.hpp
new file mode 100644
index 0000000..6182b5f
--- /dev/null
+++ b/src/graphics/Camera.hpp
@@ -0,0 +1,49 @@
+#ifndef ENGINE_CAMERA_H
+#define ENGINE_CAMERA_H
+
+#include <GL/glew.h>
+#include <glm/glm.hpp>
+
+enum camera_move {
+ FORWARD,
+ BACKWARD,
+ LEFT,
+ RIGHT
+};
+
+const GLfloat
+ init_yaw = -90.0f,
+ init_pitch = 0.0f,
+ init_speed = 2.5f,
+ init_sensitivity = 0.25f,
+ init_fov = glm::radians(45.0f);
+
+class Camera {
+ glm::vec3 position;
+ glm::vec3 front;
+ glm::vec3 up;
+ glm::vec3 right;
+ glm::vec3 world_up;
+
+ GLfloat yaw; // around x
+ GLfloat pitch; // around y
+
+ GLfloat move_speed;
+ GLfloat mouse_sensitivity;
+ GLfloat fov;
+public:
+ Camera(glm::vec3 a_pos = glm::vec3(0.0f, 0.0f, 5.0f),
+ glm::vec3 a_front = glm::vec3(0.0f, 0.0f, -1.0f),
+ glm::vec3 a_up = glm::vec3(0.0f, 1.0f, 0.0f),
+ GLfloat a_yaw = init_yaw, GLfloat a_pitch = init_pitch);
+
+ glm::mat4 GetProjViewMatrix(int w_width, int w_height);
+ glm::mat4 GetSkyboxMatrix(int w_width, int w_height);
+ void Movement(camera_move direction, GLfloat deltaTime);
+ void View(GLfloat delta_x, GLfloat delta_y);
+ void Fov(GLfloat delta_y);
+private:
+ void UpdateVectors();
+};
+
+#endif /* ENGINE_CAMERA_H */