aboutsummaryrefslogtreecommitdiff
path: root/src/math/quat.h
diff options
context:
space:
mode:
authorvimene <vincent.menegaux@gmail.com>2024-12-31 03:40:14 +0100
committervimene <vincent.menegaux@gmail.com>2024-12-31 03:40:14 +0100
commitcc6fb8c33637566a7b116d46440e6063f016deea (patch)
tree5cc83f84525507994add57df7dd974e6611d675a /src/math/quat.h
parent6b765a85cf81bf4b7162e4c9280dd4054581c611 (diff)
downloadengine-cc6fb8c33637566a7b116d46440e6063f016deea.tar.gz
various improvements
- added quaternions and rewrote all rotations to use them - added transforms to put all object transforms in a single place - added Wavefront .obj file parser - removed frame buffer's abstract class - improved vectors, matrices, triangles, vertices and vertices data by putting all code in header file - added vector's operations - changed from NULL to nullptr - miscellaneous improvements
Diffstat (limited to 'src/math/quat.h')
-rw-r--r--src/math/quat.h74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/math/quat.h b/src/math/quat.h
new file mode 100644
index 0000000..763253b
--- /dev/null
+++ b/src/math/quat.h
@@ -0,0 +1,74 @@
+#ifndef MATH_QUAT_H
+#define MATH_QUAT_H
+
+#include <cmath>
+
+namespace engine::math {
+
+struct Quaternion {
+ static constexpr Quaternion zero() {
+ return {0.f, 0.f, 0.f, 0.f};
+ }
+
+ static constexpr Quaternion one() {
+ return {1.f, 0.f, 0.f, 0.f};
+ }
+
+ static constexpr Quaternion euler_zyx(float a, float b, float c) {
+ float ca = std::cos(a / 2.f), sa = std::sin(a / 2.f),
+ cb = std::cos(b / 2.f), sb = std::sin(b / 2.f),
+ cc = std::cos(c / 2.f), sc = std::sin(c / 2.f);
+ return {
+ ca * cb * cc - sa * sb * sc,
+ sa * cb * cc + ca * sb * sc,
+ ca * sb * cc - sa * cb * sc,
+ ca * cb * sc + sa * sb * cc,
+ };
+ }
+
+ float w, x, y, z;
+
+ constexpr Quaternion() {}
+ constexpr Quaternion(float w, float x, float y, float z) : w{w}, x{x}, y{y}, z{z} {}
+
+ constexpr bool operator==(const Quaternion& other) const & {
+ return w == other.w && x == other.x && y == other.y && z == other.z;
+ }
+
+ constexpr bool operator!=(const Quaternion& other) const & {
+ return !(*this == other);
+ }
+
+ constexpr Quaternion operator+() const & {
+ return *this;
+ }
+
+ constexpr Quaternion operator-() const & {
+ return { -w, -x, -y, -z };
+ }
+
+ constexpr Quaternion operator+(const Quaternion& other) const & {
+ return { w + other.w, x + other.x, y + other.y, z + other.z };
+ }
+
+ constexpr Quaternion operator-(const Quaternion& other) const & {
+ return *this + (-other);
+ }
+
+ constexpr Quaternion operator*(const Quaternion& other) const & {
+ return {
+ w * other.w - x * other.x - y * other.y - z * other.z,
+ w * other.x + x * other.w + y * other.z - z * other.y,
+ w * other.y + y * other.w + z * other.x - x * other.z,
+ w * other.z + z * other.w + x * other.y - y * other.x,
+ };
+ }
+
+ constexpr Quaternion conjugate() const & {
+ return {w, -x, -y, -z};
+ }
+};
+
+}
+
+#endif // MATH_QUAT_H