summaryrefslogtreecommitdiffstats
path: root/src/math
diff options
context:
space:
mode:
authoraap <aap@papnet.eu>2019-06-11 08:59:28 +0200
committeraap <aap@papnet.eu>2019-06-11 08:59:28 +0200
commite7ed4d009636804d5dbe05aae9e7ab23b80fdd37 (patch)
tree4c95f6e07923b5ed0a7046afeb42a1ea2b8693bf /src/math
parentMerge branch 'master' of github.com:GTAmodding/re3 (diff)
downloadre3-e7ed4d009636804d5dbe05aae9e7ab23b80fdd37.tar
re3-e7ed4d009636804d5dbe05aae9e7ab23b80fdd37.tar.gz
re3-e7ed4d009636804d5dbe05aae9e7ab23b80fdd37.tar.bz2
re3-e7ed4d009636804d5dbe05aae9e7ab23b80fdd37.tar.lz
re3-e7ed4d009636804d5dbe05aae9e7ab23b80fdd37.tar.xz
re3-e7ed4d009636804d5dbe05aae9e7ab23b80fdd37.tar.zst
re3-e7ed4d009636804d5dbe05aae9e7ab23b80fdd37.zip
Diffstat (limited to 'src/math')
-rw-r--r--src/math/Quaternion.h83
-rw-r--r--src/math/math.cpp57
2 files changed, 140 insertions, 0 deletions
diff --git a/src/math/Quaternion.h b/src/math/Quaternion.h
new file mode 100644
index 00000000..702fc72f
--- /dev/null
+++ b/src/math/Quaternion.h
@@ -0,0 +1,83 @@
+#pragma once
+
+// TODO: actually implement this
+class CQuaternion
+{
+public:
+ float x, y, z, w;
+ CQuaternion(void) {}
+ CQuaternion(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
+
+ float Magnitude(void) const { return sqrt(x*x + y*y + z*z + w*w); }
+ float MagnitudeSqr(void) const { return x*x + y*y + z*z + w*w; }
+
+ const CQuaternion &operator+=(CQuaternion const &right) {
+ x += right.x;
+ y += right.y;
+ z += right.z;
+ w += right.w;
+ return *this;
+ }
+
+ const CQuaternion &operator-=(CQuaternion const &right) {
+ x -= right.x;
+ y -= right.y;
+ z -= right.z;
+ w -= right.w;
+ return *this;
+ }
+
+ const CQuaternion &operator*=(float right) {
+ x *= right;
+ y *= right;
+ z *= right;
+ w *= right;
+ return *this;
+ }
+
+ const CQuaternion &operator/=(float right) {
+ x /= right;
+ y /= right;
+ z /= right;
+ w /= right;
+ return *this;
+ }
+
+ CQuaternion operator-() const {
+ return CQuaternion(-x, -y, -z, -w);
+ }
+
+ void Slerp(const CQuaternion &q1, const CQuaternion &q2, float theta, float invSin, float t);
+ void Get(RwMatrix *matrix);
+};
+
+inline float
+DotProduct(const CQuaternion &q1, const CQuaternion &q2)
+{
+ return q1.x*q2.x + q1.y*q2.y + q1.z*q2.z + q1.w*q2.w;
+}
+
+inline CQuaternion operator+(const CQuaternion &left, const CQuaternion &right)
+{
+ return CQuaternion(left.x + right.x, left.y + right.y, left.z + right.z, left.w + right.w);
+}
+
+inline CQuaternion operator-(const CQuaternion &left, const CQuaternion &right)
+{
+ return CQuaternion(left.x - right.x, left.y - right.y, left.z - right.z, left.w - right.w);
+}
+
+inline CQuaternion operator*(const CQuaternion &left, float right)
+{
+ return CQuaternion(left.x * right, left.y * right, left.z * right, left.w * right);
+}
+
+inline CQuaternion operator*(float left, const CQuaternion &right)
+{
+ return CQuaternion(left * right.x, left * right.y, left * right.z, left * right.w);
+}
+
+inline CQuaternion operator/(const CQuaternion &left, float right)
+{
+ return CQuaternion(left.x / right, left.y / right, left.z / right, left.w / right);
+}
diff --git a/src/math/math.cpp b/src/math/math.cpp
new file mode 100644
index 00000000..b76db4ae
--- /dev/null
+++ b/src/math/math.cpp
@@ -0,0 +1,57 @@
+#include "common.h"
+#include "patcher.h"
+#include "Quaternion.h"
+
+// TODO: move more stuff into here
+
+void
+CQuaternion::Slerp(const CQuaternion &q1, const CQuaternion &q2, float theta, float invSin, float t)
+{
+ if(theta == 0.0f)
+ *this = q2;
+ else{
+ float w1, w2;
+ if(theta > PI/2){
+ theta = PI - theta;
+ w1 = sin((1.0f - t) * theta) * invSin;
+ w2 = -sin(t * theta) * invSin;
+ }else{
+ w1 = sin((1.0f - t) * theta) * invSin;
+ w2 = sin(t * theta) * invSin;
+ }
+ *this = w1*q1 + w2*q2;
+ }
+}
+
+void
+CQuaternion::Get(RwMatrix *matrix)
+{
+ float x2 = x+x;
+ float y2 = y+y;
+ float z2 = z+z;
+
+ float x_2x = x * x2;
+ float x_2y = x * y2;
+ float x_2z = x * z2;
+ float y_2y = y * y2;
+ float y_2z = y * z2;
+ float z_2z = z * z2;
+ float w_2x = w * x2;
+ float w_2y = w * y2;
+ float w_2z = w * z2;
+
+ matrix->right.x = 1.0f - (y_2y + z_2z);
+ matrix->up.x = x_2y - w_2z;
+ matrix->at.x = x_2z + w_2y;
+ matrix->right.y = x_2y + w_2z;
+ matrix->up.y = 1.0f - (x_2x + z_2z);
+ matrix->at.y = y_2z - w_2x;
+ matrix->right.z = x_2z - w_2y;
+ matrix->up.z = y_2z + w_2x;
+ matrix->at.z = 1.0f - (x_2x + y_2y);
+}
+
+STARTPATCHES
+ InjectHook(0x4BA1C0, &CQuaternion::Slerp, PATCH_JUMP);
+ InjectHook(0x4BA0D0, &CQuaternion::Get, PATCH_JUMP);
+ENDPATCHES