diff options
| author | Mel <einebeere@gmail.com> | 2022-10-06 02:48:43 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2022-10-06 02:48:43 +0200 |
| commit | fdbfa8e36f85eee051fc562f1a8588970257a20f (patch) | |
| tree | e861a56c50b1e8532bd71a21064d76d0c148ed3a /src/Math | |
| parent | 731846a0c654b39e23c26f611470e401df404c9d (diff) | |
| download | meowcraft-fdbfa8e36f85eee051fc562f1a8588970257a20f.tar.zst meowcraft-fdbfa8e36f85eee051fc562f1a8588970257a20f.zip | |
Rotating camera with mouse
Diffstat (limited to 'src/Math')
| -rw-r--r-- | src/Math/MVP.cpp | 2 | ||||
| -rw-r--r-- | src/Math/Math.hpp | 3 | ||||
| -rw-r--r-- | src/Math/Matrix.hpp | 7 | ||||
| -rw-r--r-- | src/Math/Rotation.hpp | 16 | ||||
| -rw-r--r-- | src/Math/Trig.hpp | 17 |
5 files changed, 39 insertions, 6 deletions
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 |
