diff options
author | Sergeanur <s.anureev@yandex.ua> | 2021-01-19 16:09:06 +0100 |
---|---|---|
committer | Sergeanur <s.anureev@yandex.ua> | 2021-01-19 17:38:10 +0100 |
commit | e6ef1644418401e5ace5a12537d6bd500db7f0e2 (patch) | |
tree | de1de5510b1ddcffc3af0b374440fb9cc5cb2c9a | |
parent | Get rid of RwMatrix in CMatrix (diff) | |
download | re3-e6ef1644418401e5ace5a12537d6bd500db7f0e2.tar re3-e6ef1644418401e5ace5a12537d6bd500db7f0e2.tar.gz re3-e6ef1644418401e5ace5a12537d6bd500db7f0e2.tar.bz2 re3-e6ef1644418401e5ace5a12537d6bd500db7f0e2.tar.lz re3-e6ef1644418401e5ace5a12537d6bd500db7f0e2.tar.xz re3-e6ef1644418401e5ace5a12537d6bd500db7f0e2.tar.zst re3-e6ef1644418401e5ace5a12537d6bd500db7f0e2.zip |
-rw-r--r-- | src/math/Matrix.cpp | 382 | ||||
-rw-r--r-- | src/math/Matrix.h | 62 | ||||
-rw-r--r-- | src/math/Vector.cpp | 18 |
3 files changed, 233 insertions, 229 deletions
diff --git a/src/math/Matrix.cpp b/src/math/Matrix.cpp index bee3ffba..b11e8a1c 100644 --- a/src/math/Matrix.cpp +++ b/src/math/Matrix.cpp @@ -60,20 +60,20 @@ CMatrix::Detach(void) void CMatrix::Update(void) { - right = m_attachment->right; - forward = m_attachment->up; - up = m_attachment->at; - pos = m_attachment->pos; + GetRight() = m_attachment->right; + GetForward() = m_attachment->up; + GetUp() = m_attachment->at; + GetPosition() = m_attachment->pos; } void CMatrix::UpdateRW(void) { if (m_attachment) { - m_attachment->right = right; - m_attachment->up = forward; - m_attachment->at = up; - m_attachment->pos = pos; + m_attachment->right = GetRight(); + m_attachment->up = GetForward(); + m_attachment->at = GetUp(); + m_attachment->pos = GetPosition(); RwMatrixUpdate(m_attachment); } } @@ -95,82 +95,82 @@ CMatrix::CopyOnlyMatrix(const CMatrix &other) CMatrix & CMatrix::operator+=(CMatrix const &rhs) { - right += rhs.right; - forward += rhs.forward; - up += rhs.up; - pos += rhs.pos; + GetRight() += rhs.GetRight(); + GetForward() += rhs.GetForward(); + GetUp() += rhs.GetUp(); + GetPosition() += rhs.GetPosition(); return *this; } void CMatrix::SetUnity(void) { - right.x = 1.0f; - right.y = 0.0f; - right.z = 0.0f; - forward.x = 0.0f; - forward.y = 1.0f; - forward.z = 0.0f; - up.x = 0.0f; - up.y = 0.0f; - up.z = 1.0f; - pos.x = 0.0f; - pos.y = 0.0f; - pos.z = 0.0f; + rx = 1.0f; + ry = 0.0f; + rz = 0.0f; + fx = 0.0f; + fy = 1.0f; + fz = 0.0f; + ux = 0.0f; + uy = 0.0f; + uz = 1.0f; + px = 0.0f; + py = 0.0f; + pz = 0.0f; } void CMatrix::ResetOrientation(void) { - right.x = 1.0f; - right.y = 0.0f; - right.z = 0.0f; - forward.x = 0.0f; - forward.y = 1.0f; - forward.z = 0.0f; - up.x = 0.0f; - up.y = 0.0f; - up.z = 1.0f; + rx = 1.0f; + ry = 0.0f; + rz = 0.0f; + fx = 0.0f; + fy = 1.0f; + fz = 0.0f; + ux = 0.0f; + uy = 0.0f; + uz = 1.0f; } void CMatrix::SetScale(float s) { - right.x = s; - right.y = 0.0f; - right.z = 0.0f; + rx = s; + ry = 0.0f; + rz = 0.0f; - forward.x = 0.0f; - forward.y = s; - forward.z = 0.0f; + fx = 0.0f; + fy = s; + fz = 0.0f; - up.x = 0.0f; - up.y = 0.0f; - up.z = s; + ux = 0.0f; + uy = 0.0f; + uz = s; - pos.x = 0.0f; - pos.y = 0.0f; - pos.z = 0.0f; + px = 0.0f; + py = 0.0f; + pz = 0.0f; } void CMatrix::SetTranslate(float x, float y, float z) { - right.x = 1.0f; - right.y = 0.0f; - right.z = 0.0f; + rx = 1.0f; + ry = 0.0f; + rz = 0.0f; - forward.x = 0.0f; - forward.y = 1.0f; - forward.z = 0.0f; + fx = 0.0f; + fy = 1.0f; + fz = 0.0f; - up.x = 0.0f; - up.y = 0.0f; - up.z = 1.0f; + ux = 0.0f; + uy = 0.0f; + uz = 1.0f; - pos.x = x; - pos.y = y; - pos.z = z; + px = x; + py = y; + pz = z; } void @@ -179,17 +179,17 @@ CMatrix::SetRotateXOnly(float angle) float c = Cos(angle); float s = Sin(angle); - right.x = 1.0f; - right.y = 0.0f; - right.z = 0.0f; + rx = 1.0f; + ry = 0.0f; + rz = 0.0f; - forward.x = 0.0f; - forward.y = c; - forward.z = s; + fx = 0.0f; + fy = c; + fz = s; - up.x = 0.0f; - up.y = -s; - up.z = c; + ux = 0.0f; + uy = -s; + uz = c; } void @@ -198,17 +198,17 @@ CMatrix::SetRotateYOnly(float angle) float c = Cos(angle); float s = Sin(angle); - right.x = c; - right.y = 0.0f; - right.z = -s; + rx = c; + ry = 0.0f; + rz = -s; - forward.x = 0.0f; - forward.y = 1.0f; - forward.z = 0.0f; + fx = 0.0f; + fy = 1.0f; + fz = 0.0f; - up.x = s; - up.y = 0.0f; - up.z = c; + ux = s; + uy = 0.0f; + uz = c; } void @@ -217,26 +217,26 @@ CMatrix::SetRotateZOnly(float angle) float c = Cos(angle); float s = Sin(angle); - right.x = c; - right.y = s; - right.z = 0.0f; + rx = c; + ry = s; + rz = 0.0f; - forward.x = -s; - forward.y = c; - forward.z = 0.0f; + fx = -s; + fy = c; + fz = 0.0f; - up.x = 0.0f; - up.y = 0.0f; - up.z = 1.0f; + ux = 0.0f; + uy = 0.0f; + uz = 1.0f; } void CMatrix::SetRotateX(float angle) { SetRotateXOnly(angle); - pos.x = 0.0f; - pos.y = 0.0f; - pos.z = 0.0f; + px = 0.0f; + py = 0.0f; + pz = 0.0f; } @@ -244,18 +244,18 @@ void CMatrix::SetRotateY(float angle) { SetRotateYOnly(angle); - pos.x = 0.0f; - pos.y = 0.0f; - pos.z = 0.0f; + px = 0.0f; + py = 0.0f; + pz = 0.0f; } void CMatrix::SetRotateZ(float angle) { SetRotateZOnly(angle); - pos.x = 0.0f; - pos.y = 0.0f; - pos.z = 0.0f; + px = 0.0f; + py = 0.0f; + pz = 0.0f; } void @@ -268,21 +268,21 @@ CMatrix::SetRotate(float xAngle, float yAngle, float zAngle) float cZ = Cos(zAngle); float sZ = Sin(zAngle); - right.x = cZ * cY - (sZ * sX) * sY; - right.y = (cZ * sX) * sY + sZ * cY; - right.z = -cX * sY; + rx = cZ * cY - (sZ * sX) * sY; + ry = (cZ * sX) * sY + sZ * cY; + rz = -cX * sY; - forward.x = -sZ * cX; - forward.y = cZ * cX; - forward.z = sX; + fx = -sZ * cX; + fy = cZ * cX; + fz = sX; - up.x = (sZ * sX) * cY + cZ * sY; - up.y = sZ * sY - (cZ * sX) * cY; - up.z = cX * cY; + ux = (sZ * sX) * cY + cZ * sY; + uy = sZ * sY - (cZ * sX) * cY; + uz = cX * cY; - pos.x = 0.0f; - pos.y = 0.0f; - pos.z = 0.0f; + px = 0.0f; + py = 0.0f; + pz = 0.0f; } void @@ -291,23 +291,23 @@ CMatrix::RotateX(float x) float c = Cos(x); float s = Sin(x); - float ry = right.y; - float rz = right.z; - float uy = forward.y; - float uz = forward.z; - float ay = up.y; - float az = up.z; - float py = pos.y; - float pz = pos.z; - - right.y = c * ry - s * rz; - right.z = c * rz + s * ry; - forward.y = c * uy - s * uz; - forward.z = c * uz + s * uy; - up.y = c * ay - s * az; - up.z = c * az + s * ay; - pos.y = c * py - s * pz; - pos.z = c * pz + s * py; + float ry = this->ry; + float rz = this->rz; + float uy = this->fy; + float uz = this->fz; + float ay = this->uy; + float az = this->uz; + float py = this->py; + float pz = this->pz; + + this->ry = c * ry - s * rz; + this->rz = c * rz + s * ry; + this->fy = c * uy - s * uz; + this->fz = c * uz + s * uy; + this->uy = c * ay - s * az; + this->uz = c * az + s * ay; + this->py = c * py - s * pz; + this->pz = c * pz + s * py; } void @@ -316,23 +316,23 @@ CMatrix::RotateY(float y) float c = Cos(y); float s = Sin(y); - float rx = right.x; - float rz = right.z; - float ux = forward.x; - float uz = forward.z; - float ax = up.x; - float az = up.z; - float px = pos.x; - float pz = pos.z; - - right.x = c * rx + s * rz; - right.z = c * rz - s * rx; - forward.x = c * ux + s * uz; - forward.z = c * uz - s * ux; - up.x = c * ax + s * az; - up.z = c * az - s * ax; - pos.x = c * px + s * pz; - pos.z = c * pz - s * px; + float rx = this->rx; + float rz = this->rz; + float ux = this->fx; + float uz = this->fz; + float ax = this->ux; + float az = this->uz; + float px = this->px; + float pz = this->pz; + + this->rx = c * rx + s * rz; + this->rz = c * rz - s * rx; + this->fx = c * ux + s * uz; + this->fz = c * uz - s * ux; + this->ux = c * ax + s * az; + this->uz = c * az - s * ax; + this->px = c * px + s * pz; + this->pz = c * pz - s * px; } void @@ -341,23 +341,23 @@ CMatrix::RotateZ(float z) float c = Cos(z); float s = Sin(z); - float ry = right.y; - float rx = right.x; - float uy = forward.y; - float ux = forward.x; - float ay = up.y; - float ax = up.x; - float py = pos.y; - float px = pos.x; + float ry = this->ry; + float rx = this->rx; + float uy = this->fy; + float ux = this->fx; + float ay = this->uy; + float ax = this->ux; + float py = this->py; + float px = this->px; - right.x = c * rx - s * ry; - right.y = c * ry + s * rx; - forward.x = c * ux - s * uy; - forward.y = c * uy + s * ux; - up.x = c * ax - s * ay; - up.y = c * ay + s * ax; - pos.x = c * px - s * py; - pos.y = c * py + s * px; + this->rx = c * rx - s * ry; + this->ry = c * ry + s * rx; + this->fx = c * ux - s * uy; + this->fy = c * uy + s * ux; + this->ux = c * ax - s * ay; + this->uy = c * ay + s * ax; + this->px = c * px - s * py; + this->py = c * py + s * px; } @@ -371,18 +371,18 @@ CMatrix::Rotate(float x, float y, float z) float cZ = Cos(z); float sZ = Sin(z); - float rx = right.x; - float ry = right.y; - float rz = right.z; - float ux = forward.x; - float uy = forward.y; - float uz = forward.z; - float ax = up.x; - float ay = up.y; - float az = up.z; - float px = pos.x; - float py = pos.y; - float pz = pos.z; + float rx = this->rx; + float ry = this->ry; + float rz = this->rz; + float ux = this->fx; + float uy = this->fy; + float uz = this->fz; + float ax = this->ux; + float ay = this->uy; + float az = this->uz; + float px = this->px; + float py = this->py; + float pz = this->pz; float x1 = cZ * cY - (sZ * sX) * sY; float x2 = (cZ * sX) * sY + sZ * cY; @@ -394,18 +394,18 @@ CMatrix::Rotate(float x, float y, float z) float z2 = sZ * sY - (cZ * sX) * cY; float z3 = cX * cY; - right.x = x1 * rx + y1 * ry + z1 * rz; - right.y = x2 * rx + y2 * ry + z2 * rz; - right.z = x3 * rx + y3 * ry + z3 * rz; - forward.x = x1 * ux + y1 * uy + z1 * uz; - forward.y = x2 * ux + y2 * uy + z2 * uz; - forward.z = x3 * ux + y3 * uy + z3 * uz; - up.x = x1 * ax + y1 * ay + z1 * az; - up.y = x2 * ax + y2 * ay + z2 * az; - up.z = x3 * ax + y3 * ay + z3 * az; - pos.x = x1 * px + y1 * py + z1 * pz; - pos.y = x2 * px + y2 * py + z2 * pz; - pos.z = x3 * px + y3 * py + z3 * pz; + this->rx = x1 * rx + y1 * ry + z1 * rz; + this->ry = x2 * rx + y2 * ry + z2 * rz; + this->rz = x3 * rx + y3 * ry + z3 * rz; + this->fx = x1 * ux + y1 * uy + z1 * uz; + this->fy = x2 * ux + y2 * uy + z2 * uz; + this->fz = x3 * ux + y3 * uy + z3 * uz; + this->ux = x1 * ax + y1 * ay + z1 * az; + this->uy = x2 * ax + y2 * ay + z2 * az; + this->uz = x3 * ax + y3 * ay + z3 * az; + this->px = x1 * px + y1 * py + z1 * pz; + this->py = x2 * px + y2 * py + z2 * pz; + this->pz = x3 * px + y3 * py + z3 * pz; } CMatrix & @@ -434,18 +434,18 @@ operator*(const CMatrix &m1, const CMatrix &m2) { // TODO: VU0 code CMatrix out; - out.right.x = m1.right.x * m2.right.x + m1.forward.x * m2.right.y + m1.up.x * m2.right.z; - out.right.y = m1.right.y * m2.right.x + m1.forward.y * m2.right.y + m1.up.y * m2.right.z; - out.right.z = m1.right.z * m2.right.x + m1.forward.z * m2.right.y + m1.up.z * m2.right.z; - out.forward.x = m1.right.x * m2.forward.x + m1.forward.x * m2.forward.y + m1.up.x * m2.forward.z; - out.forward.y = m1.right.y * m2.forward.x + m1.forward.y * m2.forward.y + m1.up.y * m2.forward.z; - out.forward.z = m1.right.z * m2.forward.x + m1.forward.z * m2.forward.y + m1.up.z * m2.forward.z; - out.up.x = m1.right.x * m2.up.x + m1.forward.x * m2.up.y + m1.up.x * m2.up.z; - out.up.y = m1.right.y * m2.up.x + m1.forward.y * m2.up.y + m1.up.y * m2.up.z; - out.up.z = m1.right.z * m2.up.x + m1.forward.z * m2.up.y + m1.up.z * m2.up.z; - out.pos.x = m1.right.x * m2.pos.x + m1.forward.x * m2.pos.y + m1.up.x * m2.pos.z + m1.pos.x; - out.pos.y = m1.right.y * m2.pos.x + m1.forward.y * m2.pos.y + m1.up.y * m2.pos.z + m1.pos.y; - out.pos.z = m1.right.z * m2.pos.x + m1.forward.z * m2.pos.y + m1.up.z * m2.pos.z + m1.pos.z; + out.rx = m1.rx * m2.rx + m1.fx * m2.ry + m1.ux * m2.rz; + out.ry = m1.ry * m2.rx + m1.fy * m2.ry + m1.uy * m2.rz; + out.rz = m1.rz * m2.rx + m1.fz * m2.ry + m1.uz * m2.rz; + out.fx = m1.rx * m2.fx + m1.fx * m2.fy + m1.ux * m2.fz; + out.fy = m1.ry * m2.fx + m1.fy * m2.fy + m1.uy * m2.fz; + out.fz = m1.rz * m2.fx + m1.fz * m2.fy + m1.uz * m2.fz; + out.ux = m1.rx * m2.ux + m1.fx * m2.uy + m1.ux * m2.uz; + out.uy = m1.ry * m2.ux + m1.fy * m2.uy + m1.uy * m2.uz; + out.uz = m1.rz * m2.ux + m1.fz * m2.uy + m1.uz * m2.uz; + out.px = m1.rx * m2.px + m1.fx * m2.py + m1.ux * m2.pz + m1.px; + out.py = m1.ry * m2.px + m1.fy * m2.py + m1.uy * m2.pz + m1.py; + out.pz = m1.rz * m2.px + m1.fz * m2.py + m1.uz * m2.pz + m1.pz; return out; } diff --git a/src/math/Matrix.h b/src/math/Matrix.h index fdbd0b60..9d50a4f6 100644 --- a/src/math/Matrix.h +++ b/src/math/Matrix.h @@ -1,7 +1,5 @@ #pragma once -#include "VuVector.h" - class CMatrix { public: @@ -10,10 +8,10 @@ public: float f[4][4]; struct { - CVuVector right; - CVuVector forward; - CVuVector up; - CVuVector pos; + float rx, ry, rz, rw; + float fx, fy, fz, fw; + float ux, uy, uz, uw; + float px, py, pz, pw; }; }; @@ -38,17 +36,23 @@ public: CMatrix &operator+=(CMatrix const &rhs); CMatrix &operator*=(CMatrix const &rhs); - CVector &GetPosition(void){ return pos; } - CVector &GetRight(void) { return right; } - CVector &GetForward(void) { return forward; } - CVector &GetUp(void) { return up; } + CVector &GetPosition(void) { return *(CVector*)&px; } + CVector &GetRight(void) { return *(CVector*)℞ } + CVector &GetForward(void) { return *(CVector*)&fx; } + CVector &GetUp(void) { return *(CVector*)&ux; } + + const CVector &GetPosition(void) const { return *(CVector*)&px; } + const CVector &GetRight(void) const { return *(CVector*)℞ } + const CVector &GetForward(void) const { return *(CVector*)&fx; } + const CVector &GetUp(void) const { return *(CVector*)&ux; } + void SetTranslate(float x, float y, float z); void SetTranslate(const CVector &trans){ SetTranslate(trans.x, trans.y, trans.z); } void Translate(float x, float y, float z){ - pos.x += x; - pos.y += y; - pos.z += z; + px += x; + py += y; + pz += z; } void Translate(const CVector &trans){ Translate(trans.x, trans.y, trans.z); } @@ -72,17 +76,17 @@ public: float c = Cos(angle); float s = Sin(angle); - right.x = c * scale; - right.y = s * scale; - right.z = 0.0f; + rx = c * scale; + ry = s * scale; + rz = 0.0f; - forward.x = -s * scale; - forward.y = c * scale; - forward.z = 0.0f; + fx = -s * scale; + fy = c * scale; + fz = 0.0f; - up.x = 0.0f; - up.y = 0.0f; - up.z = scale; + ux = 0.0f; + uy = 0.0f; + uz = scale; } void SetRotateX(float angle); void SetRotateY(float angle); @@ -98,9 +102,9 @@ public: void SetUnity(void); void ResetOrientation(void); void SetTranslateOnly(float x, float y, float z) { - pos.x = x; - pos.y = y; - pos.z = z; + px = x; + py = y; + pz = z; } void SetTranslateOnly(const CVector& pos) { SetTranslateOnly(pos.x, pos.y, pos.z); @@ -114,11 +118,11 @@ CMatrix Invert(const CMatrix &matrix); CMatrix operator*(const CMatrix &m1, const CMatrix &m2); inline CVector MultiplyInverse(const CMatrix &mat, const CVector &vec) { - CVector v(vec.x - mat.pos.x, vec.y - mat.pos.y, vec.z - mat.pos.z); + CVector v(vec.x - mat.px, vec.y - mat.py, vec.z - mat.pz); return CVector( - mat.right.x * v.x + mat.right.y * v.y + mat.right.z * v.z, - mat.forward.x * v.x + mat.forward.y * v.y + mat.forward.z * v.z, - mat.up.x * v.x + mat.up.y * v.y + mat.up.z * v.z); + mat.rx * v.x + mat.ry * v.y + mat.rz * v.z, + mat.fx * v.x + mat.fy * v.y + mat.fz * v.z, + mat.ux * v.x + mat.uy * v.y + mat.uz * v.z); } diff --git a/src/math/Vector.cpp b/src/math/Vector.cpp index 3bc395d8..ee76e555 100644 --- a/src/math/Vector.cpp +++ b/src/math/Vector.cpp @@ -23,24 +23,24 @@ CVector Multiply3x3(const CMatrix &mat, const CVector &vec) { // TODO: VU0 code - return CVector(mat.right.x * vec.x + mat.forward.x * vec.y + mat.up.x * vec.z, - mat.right.y * vec.x + mat.forward.y * vec.y + mat.up.y * vec.z, - mat.right.z * vec.x + mat.forward.z * vec.y + mat.up.z * vec.z); + return CVector(mat.rx * vec.x + mat.fx * vec.y + mat.ux * vec.z, + mat.ry * vec.x + mat.fy * vec.y + mat.uy * vec.z, + mat.rz * vec.x + mat.fz * vec.y + mat.uz * vec.z); } CVector Multiply3x3(const CVector &vec, const CMatrix &mat) { - return CVector(mat.right.x * vec.x + mat.right.y * vec.y + mat.right.z * vec.z, - mat.forward.x * vec.x + mat.forward.y * vec.y + mat.forward.z * vec.z, - mat.up.x * vec.x + mat.up.y * vec.y + mat.up.z * vec.z); + return CVector(mat.rx * vec.x + mat.ry * vec.y + mat.rz * vec.z, + mat.fx * vec.x + mat.fy * vec.y + mat.fz * vec.z, + mat.ux * vec.x + mat.uy * vec.y + mat.uz * vec.z); } CVector operator*(const CMatrix &mat, const CVector &vec) { // TODO: VU0 code - return CVector(mat.right.x * vec.x + mat.forward.x * vec.y + mat.up.x * vec.z + mat.pos.x, - mat.right.y * vec.x + mat.forward.y * vec.y + mat.up.y * vec.z + mat.pos.y, - mat.right.z * vec.x + mat.forward.z * vec.y + mat.up.z * vec.z + mat.pos.z); + return CVector(mat.rx * vec.x + mat.fx * vec.y + mat.ux * vec.z + mat.px, + mat.ry * vec.x + mat.fy * vec.y + mat.uy * vec.z + mat.py, + mat.rz * vec.x + mat.fz * vec.y + mat.uz * vec.z + mat.pz); } |