dca3-game/src/liberty/math/Matrix.cpp

568 lines
11 KiB
C++
Raw Normal View History

#include "common.h"
CMatrix::CMatrix(CMatrix const &m)
{
m_attachment = nil;
m_hasRwMatrix = false;
mat_copy(*this, m);
}
CMatrix::CMatrix(RwMatrix *matrix, bool owner)
{
m_attachment = nil;
Attach(matrix, owner);
}
CMatrix::~CMatrix(void)
{
if (m_hasRwMatrix && m_attachment)
RwMatrixDestroy(m_attachment);
}
void
CMatrix::Attach(RwMatrix *matrix, bool owner)
{
#ifdef FIX_BUGS
if (m_attachment && m_hasRwMatrix)
#else
if (m_hasRwMatrix && m_attachment)
#endif
RwMatrixDestroy(m_attachment);
m_attachment = matrix;
m_hasRwMatrix = owner;
Update();
}
void
CMatrix::AttachRW(RwMatrix *matrix, bool owner)
{
if (m_hasRwMatrix && m_attachment)
RwMatrixDestroy(m_attachment);
m_attachment = matrix;
m_hasRwMatrix = owner;
UpdateRW();
}
void
CMatrix::Detach(void)
{
if (m_hasRwMatrix && m_attachment)
RwMatrixDestroy(m_attachment);
m_attachment = nil;
}
void
CMatrix::Update(void)
{
2021-01-19 17:09:06 +02:00
GetRight() = m_attachment->right;
GetForward() = m_attachment->up;
GetUp() = m_attachment->at;
GetPosition() = m_attachment->pos;
}
void
CMatrix::UpdateRW(void)
{
if (m_attachment) {
2021-01-19 17:09:06 +02:00
m_attachment->right = GetRight();
m_attachment->up = GetForward();
m_attachment->at = GetUp();
m_attachment->pos = GetPosition();
RwMatrixUpdate(m_attachment);
}
}
void
CMatrix::operator=(CMatrix const &rhs)
{
mat_copy(*this, rhs);
if (m_attachment)
UpdateRW();
}
void
2021-01-18 21:06:59 +02:00
CMatrix::CopyOnlyMatrix(const CMatrix &other)
{
mat_copy(*this, other);
}
CMatrix &
CMatrix::operator+=(CMatrix const &rhs)
{
2021-01-19 17:09:06 +02:00
GetRight() += rhs.GetRight();
GetForward() += rhs.GetForward();
GetUp() += rhs.GetUp();
GetPosition() += rhs.GetPosition();
return *this;
}
void
CMatrix::SetUnity(void)
{
2021-01-19 17:09:06 +02:00
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)
{
2021-01-19 17:09:06 +02:00
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)
{
2021-01-19 17:09:06 +02:00
rx = s;
ry = 0.0f;
rz = 0.0f;
2021-01-19 17:09:06 +02:00
fx = 0.0f;
fy = s;
fz = 0.0f;
2021-01-19 17:09:06 +02:00
ux = 0.0f;
uy = 0.0f;
uz = s;
2021-01-19 17:09:06 +02:00
px = 0.0f;
py = 0.0f;
pz = 0.0f;
}
void
CMatrix::SetTranslate(float x, float y, float z)
{
2021-01-19 17:09:06 +02:00
rx = 1.0f;
ry = 0.0f;
rz = 0.0f;
2021-01-19 17:09:06 +02:00
fx = 0.0f;
fy = 1.0f;
fz = 0.0f;
2021-01-19 17:09:06 +02:00
ux = 0.0f;
uy = 0.0f;
uz = 1.0f;
2021-01-19 17:09:06 +02:00
px = x;
py = y;
pz = z;
}
void
CMatrix::SetRotateXOnly(float angle)
{
2024-12-26 11:10:17 +02:00
auto [s, c] = SinCos(angle);
2021-01-19 17:09:06 +02:00
rx = 1.0f;
ry = 0.0f;
rz = 0.0f;
2021-01-19 17:09:06 +02:00
fx = 0.0f;
fy = c;
fz = s;
2021-01-19 17:09:06 +02:00
ux = 0.0f;
uy = -s;
uz = c;
}
void
CMatrix::SetRotateYOnly(float angle)
{
2024-12-26 11:10:17 +02:00
auto [s, c] = SinCos(angle);
2021-01-19 17:09:06 +02:00
rx = c;
ry = 0.0f;
rz = -s;
2021-01-19 17:09:06 +02:00
fx = 0.0f;
fy = 1.0f;
fz = 0.0f;
2021-01-19 17:09:06 +02:00
ux = s;
uy = 0.0f;
uz = c;
}
void
CMatrix::SetRotateZOnly(float angle)
{
2024-12-26 11:10:17 +02:00
auto [s, c] = SinCos(angle);
2021-01-19 17:09:06 +02:00
rx = c;
ry = s;
rz = 0.0f;
2021-01-19 17:09:06 +02:00
fx = -s;
fy = c;
fz = 0.0f;
2021-01-19 17:09:06 +02:00
ux = 0.0f;
uy = 0.0f;
uz = 1.0f;
}
void
CMatrix::SetRotateX(float angle)
{
SetRotateXOnly(angle);
2021-01-19 17:09:06 +02:00
px = 0.0f;
py = 0.0f;
pz = 0.0f;
}
void
CMatrix::SetRotateY(float angle)
{
SetRotateYOnly(angle);
2021-01-19 17:09:06 +02:00
px = 0.0f;
py = 0.0f;
pz = 0.0f;
}
void
CMatrix::SetRotateZ(float angle)
{
SetRotateZOnly(angle);
2021-01-19 17:09:06 +02:00
px = 0.0f;
py = 0.0f;
pz = 0.0f;
}
void
CMatrix::SetRotate(float xAngle, float yAngle, float zAngle)
{
2024-12-26 11:10:17 +02:00
auto [sX, cX] = SinCos(xAngle);
auto [sY, cY] = SinCos(yAngle);
auto [sZ, cZ] = SinCos(zAngle);
2021-01-19 17:09:06 +02:00
rx = cZ * cY - (sZ * sX) * sY;
ry = (cZ * sX) * sY + sZ * cY;
rz = -cX * sY;
2021-01-19 17:09:06 +02:00
fx = -sZ * cX;
fy = cZ * cX;
fz = sX;
2021-01-19 17:09:06 +02:00
ux = (sZ * sX) * cY + cZ * sY;
uy = sZ * sY - (cZ * sX) * cY;
uz = cX * cY;
2021-01-19 17:09:06 +02:00
px = 0.0f;
py = 0.0f;
pz = 0.0f;
}
void
CMatrix::RotateX(float x)
{
#if 0 && defined(DC_SH4) // this is bugged and does not yield correct results
dc::mat_load2(reinterpret_cast<matrix_t *>(this));
mat_rotate_x(x);
dc::mat_store2(reinterpret_cast<matrix_t *>(this));
2024-12-26 11:10:17 +02:00
#else
auto [s, c] = SinCos(x);
2021-01-19 17:09:06 +02:00
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;
2024-12-26 11:10:17 +02:00
#endif
}
void
CMatrix::RotateY(float y)
{
#if 0 && defined(DC_SH4) // this is bugged and does not yield correct results
dc::mat_load2(reinterpret_cast<matrix_t *>(this));
mat_rotate_y(y);
dc::mat_store2(reinterpret_cast<matrix_t *>(this));
2024-12-26 11:10:17 +02:00
#else
auto [s, c] = SinCos(y);
2021-01-19 17:09:06 +02:00
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;
2024-12-26 11:10:17 +02:00
#endif
}
void
CMatrix::RotateZ(float z)
{
#if 0 && defined(DC_SH4) // this is bugged and does not yield correct results
dc::mat_load2(reinterpret_cast<matrix_t *>(this));
mat_rotate_z(z);
dc::mat_store2(reinterpret_cast<matrix_t *>(this));
2024-12-26 11:10:17 +02:00
#else
auto [s, c] = SinCos(z);
2021-01-19 17:09:06 +02:00
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;
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;
2024-12-26 11:10:17 +02:00
#endif
}
void
CMatrix::Rotate(float x, float y, float z)
{
#if 0 && defined(DC_SH4) // this is bugged and does not yield correct results
dc::mat_load2(reinterpret_cast<matrix_t *>(this));
mat_rotate(x, y, z);
dc::mat_store2(reinterpret_cast<matrix_t *>(this));
2024-12-26 11:10:17 +02:00
#else
auto [sX, cX] = SinCos(x);
auto [sY, cY] = SinCos(y);
auto [sZ, cZ] = SinCos(z);
2021-01-19 17:09:06 +02:00
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;
float x3 = -cX * sY;
float y1 = -sZ * cX;
float y2 = cZ * cX;
float y3 = sX;
float z1 = (sZ * sX) * cY + cZ * sY;
float z2 = sZ * sY - (cZ * sX) * cY;
float z3 = cX * cY;
2025-01-05 10:15:30 +02:00
#if !defined(DC_TEXCONV) && !defined(DC_SIM)
this->rx = fipr(x1, y1, z1, 0, rx, ry, rz, 0);
this->ry = fipr(x2, y2, z2, 0, rx, ry, rz, 0);
this->rz = fipr(x3, y3, z3, 0, rx, ry, rz, 0);
this->fx = fipr(x1, y1, z1, 0, ux, uy, uz, 0);
this->fy = fipr(x2, y2, z2, 0, ux, uy, uz, 0);
this->fz = fipr(x3, y3, z3, 0, ux, uy, uz, 0);
this->ux = fipr(x1, y1, z1, 0, ax, ay, az, 0);
this->uy = fipr(x2, y2, z2, 0, ax, ay, az, 0);
this->uz = fipr(x3, y3, z3, 0, ax, ay, az, 0);
this->px = fipr(x1, y1, z1, 0, px, py, pz, 0);
this->py = fipr(x2, y2, z2, 0, px, py, pz, 0);
this->pz = fipr(x3, y3, z3, 0, px, py, pz, 0);
2025-01-05 10:15:30 +02:00
#else
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;
#endif
2024-12-26 11:10:17 +02:00
#endif
}
CMatrix &
CMatrix::operator*=(CMatrix const &rhs)
{
// TODO: VU0 code
*this = *this * rhs;
return *this;
}
void
CMatrix::Reorthogonalise(void)
{
CVector &r = GetRight();
CVector &f = GetForward();
CVector &u = GetUp();
u = CrossProduct(r, f);
u.Normalise();
r = CrossProduct(f, u);
r.Normalise();
f = CrossProduct(u, r);
}
CMatrix
operator*(const CMatrix &m1, const CMatrix &m2)
{
// TODO: VU0 code
CMatrix out;
2025-04-15 13:53:19 -05:00
#ifdef DC_SH4
mat_mult(out, m1, m2);
2024-12-26 11:10:17 +02:00
#else
2021-01-19 17:09:06 +02:00
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;
2024-12-26 11:10:17 +02:00
#endif
return out;
}
CMatrix &
Invert(const CMatrix &src, CMatrix &dst)
{
// TODO: VU0 code
// GTA handles this as a raw 4x4 orthonormal matrix
// and trashes the RW flags, let's not do that
2021-01-18 21:06:59 +02:00
dst.f[3][0] = dst.f[3][1] = dst.f[3][2] = 0.0f;
#ifndef FIX_BUGS
2021-01-18 21:06:59 +02:00
dst.f[3][3] = src.f[3][3];
#endif
2021-01-18 21:06:59 +02:00
dst.f[0][0] = src.f[0][0];
dst.f[0][1] = src.f[1][0];
dst.f[0][2] = src.f[2][0];
#ifndef FIX_BUGS
2021-01-18 21:06:59 +02:00
dst.f[0][3] = src.f[3][0];
#endif
2021-01-18 21:06:59 +02:00
dst.f[1][0] = src.f[0][1];
dst.f[1][1] = src.f[1][1];
dst.f[1][2] = src.f[2][1];
#ifndef FIX_BUGS
2021-01-18 21:06:59 +02:00
dst.f[1][3] = src.f[3][1];
#endif
2021-01-18 21:06:59 +02:00
dst.f[2][0] = src.f[0][2];
dst.f[2][1] = src.f[1][2];
dst.f[2][2] = src.f[2][2];
#ifndef FIX_BUGS
2021-01-18 21:06:59 +02:00
dst.f[2][3] = src.f[3][2];
#endif
2021-01-18 21:06:59 +02:00
dst.f[3][0] += dst.f[0][0] * src.f[3][0];
dst.f[3][1] += dst.f[0][1] * src.f[3][0];
dst.f[3][2] += dst.f[0][2] * src.f[3][0];
#ifndef FIX_BUGS
2021-01-18 21:06:59 +02:00
dst.f[3][3] += dst.f[0][3] * src.f[3][0];
#endif
2021-01-18 21:06:59 +02:00
dst.f[3][0] += dst.f[1][0] * src.f[3][1];
dst.f[3][1] += dst.f[1][1] * src.f[3][1];
dst.f[3][2] += dst.f[1][2] * src.f[3][1];
#ifndef FIX_BUGS
2021-01-18 21:06:59 +02:00
dst.f[3][3] += dst.f[1][3] * src.f[3][1];
#endif
2021-01-18 21:06:59 +02:00
dst.f[3][0] += dst.f[2][0] * src.f[3][2];
dst.f[3][1] += dst.f[2][1] * src.f[3][2];
dst.f[3][2] += dst.f[2][2] * src.f[3][2];
#ifndef FIX_BUGS
2021-01-18 21:06:59 +02:00
dst.f[3][3] += dst.f[2][3] * src.f[3][2];
#endif
2021-01-18 21:06:59 +02:00
dst.f[3][0] = -dst.f[3][0];
dst.f[3][1] = -dst.f[3][1];
dst.f[3][2] = -dst.f[3][2];
#ifndef FIX_BUGS
2021-01-18 21:06:59 +02:00
dst.f[3][3] = src.f[3][3] - dst.f[3][3];
#endif
return dst;
}
CMatrix
Invert(const CMatrix &matrix)
{
CMatrix inv;
return Invert(matrix, inv);
}
void
CCompressedMatrixNotAligned::CompressFromFullMatrix(CMatrix &other)
{
m_rightX = 127.0f * other.GetRight().x;
m_rightY = 127.0f * other.GetRight().y;
m_rightZ = 127.0f * other.GetRight().z;
m_upX = 127.0f * other.GetForward().x;
m_upY = 127.0f * other.GetForward().y;
m_upZ = 127.0f * other.GetForward().z;
m_vecPos = other.GetPosition();
}
void
CCompressedMatrixNotAligned::DecompressIntoFullMatrix(CMatrix &other)
{
other.GetRight().x = m_rightX / 127.0f;
other.GetRight().y = m_rightY / 127.0f;
other.GetRight().z = m_rightZ / 127.0f;
other.GetForward().x = m_upX / 127.0f;
other.GetForward().y = m_upY / 127.0f;
other.GetForward().z = m_upZ / 127.0f;
other.GetUp() = CrossProduct(other.GetRight(), other.GetForward());
other.GetPosition() = m_vecPos;
other.Reorthogonalise();
}