summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-10-06 02:48:43 +0200
committerMel <einebeere@gmail.com>2022-10-06 02:48:43 +0200
commitfdbfa8e36f85eee051fc562f1a8588970257a20f (patch)
treee861a56c50b1e8532bd71a21064d76d0c148ed3a /src
parent731846a0c654b39e23c26f611470e401df404c9d (diff)
downloadmeowcraft-fdbfa8e36f85eee051fc562f1a8588970257a20f.tar.zst
meowcraft-fdbfa8e36f85eee051fc562f1a8588970257a20f.zip
Rotating camera with mouse
Diffstat (limited to 'src')
-rw-r--r--src/Camera.cpp6
-rw-r--r--src/Math/MVP.cpp2
-rw-r--r--src/Math/Math.hpp3
-rw-r--r--src/Math/Matrix.hpp7
-rw-r--r--src/Math/Rotation.hpp16
-rw-r--r--src/Math/Trig.hpp17
-rw-r--r--src/Mouse.cpp25
-rw-r--r--src/Mouse.hpp19
-rw-r--r--src/Window.cpp1
-rw-r--r--src/main.cpp20
10 files changed, 101 insertions, 15 deletions
diff --git a/src/Camera.cpp b/src/Camera.cpp
index 59d677c..3a9d7cd 100644
--- a/src/Camera.cpp
+++ b/src/Camera.cpp
@@ -31,6 +31,12 @@ void Camera::set_angles(Rotation angles) {
 
 void Camera::rotate(Rotation by) {
     m_angles = m_angles + by;
+
+    if (m_angles.pitch() > 89.0f) {
+        m_angles.pitch() = 89.0f;
+    } else if (m_angles.pitch() < -89.0f) {
+        m_angles.pitch() = -89.0f;
+    }
 }
 
 }
diff --git a/src/Math/MVP.cpp b/src/Math/MVP.cpp
index 146b208..de9f2ee 100644
--- a/src/Math/MVP.cpp
+++ b/src/Math/MVP.cpp
@@ -19,7 +19,7 @@ Matrix<4, 4> view(Vector<3> position, Rotation angles) {
 }
 
 Matrix<4, 4> projection(float aspect, float fov, float near, float far) {
-    auto fov_radians = (fov * M_PI) / 180.0f;
+    auto fov_radians = Math::radians(fov);
 
     float x_scale = 1.0f / (tan(fov_radians / 2.0f) * aspect);
     float y_scale = 1.0f / tan(fov_radians / 2.0f);
diff --git a/src/Math/Math.hpp b/src/Math/Math.hpp
index b8b61a1..94840b1 100644
--- a/src/Math/Math.hpp
+++ b/src/Math/Math.hpp
@@ -2,4 +2,5 @@
 
 #include "Vector.hpp"
 #include "Matrix.hpp"
-#include "Rotation.hpp"
\ No newline at end of file
+#include "Rotation.hpp"
+#include "Trig.hpp"
\ No newline at end of file
diff --git a/src/Math/Matrix.hpp b/src/Math/Matrix.hpp
index d77760a..545b6a5 100644
--- a/src/Math/Matrix.hpp
+++ b/src/Math/Matrix.hpp
@@ -4,6 +4,7 @@
 #include <cstddef>
 #include <iostream>
 #include "Rotation.hpp"
+#include "Trig.hpp"
 
 template <size_t R, size_t C, typename T = float>
 struct Matrix {
@@ -39,8 +40,10 @@ public:
     }
 
     static Matrix<4, 4> rotation(Rotation angles) {
-        auto c = angles.vector.map([](auto a) { return cos(a); });
-        auto s = angles.vector.map([](auto a) { return sin(a); });
+        auto radians = angles.vector.map([](auto a) { return Math::radians(a); });
+
+        auto c = radians.map([](auto a) { return cos(a); });
+        auto s = radians.map([](auto a) { return sin(a); });
 
         Matrix<4, 4> rotation_x{
             1.0f, 0.0f,  0.0f,   0.0f,
diff --git a/src/Math/Rotation.hpp b/src/Math/Rotation.hpp
index 73a8219..0c5c606 100644
--- a/src/Math/Rotation.hpp
+++ b/src/Math/Rotation.hpp
@@ -11,8 +11,8 @@ public:
 
     Rotation(float angles[3]) : Rotation(angles[0], angles[1], angles[2]) {};
 
-    Rotation(float x, float y, float z) {
-        vector = wrap({ x, y, z });
+    Rotation(float pitch, float yaw, float roll) {
+        vector = wrap({pitch, yaw, roll });
     };
 
     Rotation operator+(Rotation other) const {
@@ -27,5 +27,17 @@ public:
         return v.map([](auto a) { return fmod(a, 360.0f); });
     }
 
+    float& pitch() {
+        return vector[0];
+    }
+
+    float& yaw() {
+        return vector[1];
+    }
+
+    float& roll() {
+        return vector[2];
+    }
+
     Vector<3> vector;
 };
\ No newline at end of file
diff --git a/src/Math/Trig.hpp b/src/Math/Trig.hpp
new file mode 100644
index 0000000..c64e2f2
--- /dev/null
+++ b/src/Math/Trig.hpp
@@ -0,0 +1,17 @@
+#pragma once
+
+namespace Math {
+
+constexpr double PI = 3.14159265358979323846;
+
+template<typename T>
+T radians(T degrees) {
+    return (degrees * PI) / 180.0f;
+}
+
+template<typename T>
+T degrees(T radians) {
+    return radians * 180.0f / PI;
+}
+
+}
\ No newline at end of file
diff --git a/src/Mouse.cpp b/src/Mouse.cpp
new file mode 100644
index 0000000..4715362
--- /dev/null
+++ b/src/Mouse.cpp
@@ -0,0 +1,25 @@
+#include "Mouse.hpp"
+#include "Window.hpp"
+
+namespace MC {
+
+Vector<2> Mouse::update(Window& window) {
+    double x, y;
+    glfwGetCursorPos(window.get(), &x, &y);
+
+    if (m_first_event) {
+        m_last_x = x;
+        m_last_y = y;
+
+        m_first_event = false;
+    }
+
+    Vector<2> movement{static_cast<float>(x) - m_last_x,  static_cast<float>(y) - m_last_y};
+
+    m_last_x = x;
+    m_last_y = y;
+
+    return movement;
+}
+
+}
\ No newline at end of file
diff --git a/src/Mouse.hpp b/src/Mouse.hpp
new file mode 100644
index 0000000..c546a7e
--- /dev/null
+++ b/src/Mouse.hpp
@@ -0,0 +1,19 @@
+#pragma once
+
+#include <cstdint>
+#include "Math/Vector.hpp"
+#include "Window.hpp"
+
+namespace MC {
+
+class Mouse {
+public:
+    Mouse() = default;
+
+    Vector<2> update(Window &window);
+private:
+    bool m_first_event = true;
+    float m_last_x, m_last_y = 0.0f;
+};
+
+}
\ No newline at end of file
diff --git a/src/Window.cpp b/src/Window.cpp
index 413cd47..2f1c2c0 100644
--- a/src/Window.cpp
+++ b/src/Window.cpp
@@ -16,6 +16,7 @@ Window::Window(const char *title, uint32_t width, uint32_t height) {
     }
 
     glfwMakeContextCurrent(m_window);
+    glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
 }
 
 Window::~Window() {
diff --git a/src/main.cpp b/src/main.cpp
index 63d371a..3da3d90 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -10,6 +10,7 @@
 #include "Binder.hpp"
 #include "Math/MVP.hpp"
 #include "Shader/ShaderProgram.hpp"
+#include "Mouse.hpp"
 
 #define APP_NAME "Meowcraft"
 
@@ -21,7 +22,7 @@
 
 void run();
 void render(MC::BindableMesh&);
-void process_input(MC::Window&, MC::Camera&);
+void process_input(MC::Window&, MC::Mouse&, MC::Camera&);
 void setup_gl();
 void fix_macos_render(MC::Window&);
 
@@ -42,6 +43,7 @@ int main() {
 
 void run() {
     MC::Window window(APP_NAME, WINDOW_WIDTH, WINDOW_HEIGHT);
+    MC::Mouse mouse{};
     setup_gl();
 
     glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
@@ -92,7 +94,7 @@ void run() {
         fix_macos_render(window);
 #endif
 
-        process_input(window, camera);
+        process_input(window, mouse, camera);
 
         program.bind();
 
@@ -117,24 +119,24 @@ void render(MC::BindableMesh& mesh) {
     mesh.unbind();
 }
 
-void process_input(MC::Window& window, MC::Camera& camera) {
+void process_input(MC::Window& window, MC::Mouse& mouse, MC::Camera& camera) {
     if (window.key(GLFW_KEY_ESCAPE, GLFW_PRESS)) {
         window.close();
     }
 
+    auto r = mouse.update(window);
+
     auto key = [&](int key) -> float { return window.key(key, GLFW_PRESS); };
 
     float x = key(GLFW_KEY_D) - key(GLFW_KEY_A);
     float y = key(GLFW_KEY_SPACE) - key(GLFW_KEY_LEFT_SHIFT);
     float z = key(GLFW_KEY_S) - key(GLFW_KEY_W);
 
-    float rx = key(GLFW_KEY_DOWN) - key(GLFW_KEY_UP);
-    float ry = key(GLFW_KEY_RIGHT) - key(GLFW_KEY_LEFT);
-
-    auto speed = 0.05f;
+    auto move_speed = 0.05f;
+    auto rotation_speed = 0.1f;
 
-    camera.move_relative({x * speed, y * speed, z * speed});
-    camera.rotate({rx * speed, ry * speed, 0.0f});
+    camera.move_relative({x * move_speed, y * move_speed, z * move_speed});
+    camera.rotate({r.y() * rotation_speed, r.x() * rotation_speed, 0.0f});
 }
 
 void setup_gl() {