2019-05-15 10:52:37 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
class CMatrix
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RwMatrix m_matrix;
|
|
|
|
RwMatrix *m_attachment;
|
|
|
|
bool m_hasRwMatrix; // are we the owner?
|
|
|
|
|
|
|
|
CMatrix(void){
|
|
|
|
m_attachment = nil;
|
|
|
|
m_hasRwMatrix = false;
|
|
|
|
}
|
|
|
|
CMatrix(CMatrix const &m){
|
|
|
|
m_attachment = nil;
|
|
|
|
m_hasRwMatrix = false;
|
|
|
|
*this = m;
|
|
|
|
}
|
2019-06-12 14:07:37 -04:00
|
|
|
CMatrix(RwMatrix *matrix, bool owner = false){
|
2019-05-15 10:52:37 -04:00
|
|
|
m_attachment = nil;
|
2019-06-12 14:07:37 -04:00
|
|
|
Attach(matrix, owner);
|
2019-05-15 10:52:37 -04:00
|
|
|
}
|
2019-06-29 07:38:37 -04:00
|
|
|
CMatrix(float scale){
|
|
|
|
m_attachment = nil;
|
|
|
|
m_hasRwMatrix = false;
|
|
|
|
SetScale(scale);
|
|
|
|
}
|
2019-05-15 10:52:37 -04:00
|
|
|
~CMatrix(void){
|
|
|
|
if(m_hasRwMatrix && m_attachment)
|
|
|
|
RwMatrixDestroy(m_attachment);
|
|
|
|
}
|
2019-06-12 14:07:37 -04:00
|
|
|
void Attach(RwMatrix *matrix, bool owner = false){
|
2019-05-15 10:52:37 -04:00
|
|
|
if(m_hasRwMatrix && m_attachment)
|
|
|
|
RwMatrixDestroy(m_attachment);
|
|
|
|
m_attachment = matrix;
|
2019-06-12 14:07:37 -04:00
|
|
|
m_hasRwMatrix = owner;
|
2019-05-15 10:52:37 -04:00
|
|
|
Update();
|
|
|
|
}
|
2019-06-12 14:07:37 -04:00
|
|
|
void AttachRW(RwMatrix *matrix, bool owner = false){
|
2019-05-15 10:52:37 -04:00
|
|
|
if(m_hasRwMatrix && m_attachment)
|
|
|
|
RwMatrixDestroy(m_attachment);
|
|
|
|
m_attachment = matrix;
|
2019-06-12 14:07:37 -04:00
|
|
|
m_hasRwMatrix = owner;
|
2019-05-15 10:52:37 -04:00
|
|
|
UpdateRW();
|
|
|
|
}
|
|
|
|
void Detach(void){
|
|
|
|
if(m_hasRwMatrix && m_attachment)
|
|
|
|
RwMatrixDestroy(m_attachment);
|
|
|
|
m_attachment = nil;
|
|
|
|
}
|
|
|
|
void Update(void){
|
|
|
|
m_matrix = *m_attachment;
|
|
|
|
}
|
|
|
|
void UpdateRW(void){
|
|
|
|
if(m_attachment){
|
|
|
|
*m_attachment = m_matrix;
|
|
|
|
RwMatrixUpdate(m_attachment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void operator=(CMatrix const &rhs){
|
|
|
|
m_matrix = rhs.m_matrix;
|
|
|
|
if(m_attachment)
|
|
|
|
UpdateRW();
|
|
|
|
}
|
2019-06-29 07:38:37 -04:00
|
|
|
CMatrix& operator+=(CMatrix const &rhs){
|
|
|
|
m_matrix.right.x += rhs.m_matrix.right.x;
|
|
|
|
m_matrix.up.x += rhs.m_matrix.up.x;
|
|
|
|
m_matrix.at.x += rhs.m_matrix.at.x;
|
|
|
|
m_matrix.right.y += rhs.m_matrix.right.y;
|
|
|
|
m_matrix.up.y += rhs.m_matrix.up.y;
|
|
|
|
m_matrix.at.y += rhs.m_matrix.at.y;
|
|
|
|
m_matrix.right.z += rhs.m_matrix.right.z;
|
|
|
|
m_matrix.up.z += rhs.m_matrix.up.z;
|
|
|
|
m_matrix.at.z += rhs.m_matrix.at.z;
|
|
|
|
m_matrix.pos.x += rhs.m_matrix.pos.x;
|
|
|
|
m_matrix.pos.y += rhs.m_matrix.pos.y;
|
2019-06-29 11:01:43 -04:00
|
|
|
m_matrix.pos.z += rhs.m_matrix.pos.z;
|
2019-06-29 07:38:37 -04:00
|
|
|
return *this;
|
|
|
|
}
|
2019-05-15 10:52:37 -04:00
|
|
|
|
2019-07-08 11:07:34 -04:00
|
|
|
CVector &GetPosition(void){ return *(CVector*)&m_matrix.pos; }
|
|
|
|
CVector &GetRight(void) { return *(CVector*)&m_matrix.right; }
|
|
|
|
CVector &GetForward(void) { return *(CVector*)&m_matrix.up; }
|
|
|
|
CVector &GetUp(void) { return *(CVector*)&m_matrix.at; }
|
2019-07-08 15:37:47 -04:00
|
|
|
|
|
|
|
void SetTranslate(float x, float y, float z){
|
|
|
|
m_matrix.right.x = 1.0f;
|
|
|
|
m_matrix.right.y = 0.0f;
|
|
|
|
m_matrix.right.z = 0.0f;
|
|
|
|
|
|
|
|
m_matrix.up.x = 0.0f;
|
|
|
|
m_matrix.up.y = 1.0f;
|
|
|
|
m_matrix.up.z = 0.0f;
|
|
|
|
|
|
|
|
m_matrix.at.x = 0.0f;
|
|
|
|
m_matrix.at.y = 0.0f;
|
|
|
|
m_matrix.at.z = 1.0f;
|
|
|
|
|
|
|
|
m_matrix.pos.x = x;
|
|
|
|
m_matrix.pos.y = y;
|
|
|
|
m_matrix.pos.z = z;
|
|
|
|
}
|
|
|
|
void SetTranslate(const CVector &trans){ SetTranslate(trans.x, trans.y, trans.z); }
|
|
|
|
void Translate(float x, float y, float z){
|
|
|
|
m_matrix.pos.x += x;
|
|
|
|
m_matrix.pos.y += y;
|
|
|
|
m_matrix.pos.z += z;
|
|
|
|
}
|
|
|
|
void Translate(const CVector &trans){ Translate(trans.x, trans.y, trans.z); }
|
|
|
|
|
2019-05-15 10:52:37 -04:00
|
|
|
void SetScale(float s){
|
|
|
|
m_matrix.right.x = s;
|
|
|
|
m_matrix.right.y = 0.0f;
|
|
|
|
m_matrix.right.z = 0.0f;
|
|
|
|
|
|
|
|
m_matrix.up.x = 0.0f;
|
|
|
|
m_matrix.up.y = s;
|
|
|
|
m_matrix.up.z = 0.0f;
|
|
|
|
|
|
|
|
m_matrix.at.x = 0.0f;
|
|
|
|
m_matrix.at.y = 0.0f;
|
|
|
|
m_matrix.at.z = s;
|
|
|
|
|
|
|
|
m_matrix.pos.x = 0.0f;
|
|
|
|
m_matrix.pos.y = 0.0f;
|
|
|
|
m_matrix.pos.z = 0.0f;
|
|
|
|
}
|
2019-07-27 05:53:51 -04:00
|
|
|
void Scale(float scale)
|
|
|
|
{
|
|
|
|
// GTA treats this as 4x4 floats
|
|
|
|
m_matrix.right.x *= scale;
|
|
|
|
m_matrix.right.y *= scale;
|
|
|
|
m_matrix.right.z *= scale;
|
|
|
|
m_matrix.up.x *= scale;
|
|
|
|
m_matrix.up.y *= scale;
|
|
|
|
m_matrix.up.z *= scale;
|
|
|
|
m_matrix.at.x *= scale;
|
|
|
|
m_matrix.at.y *= scale;
|
|
|
|
m_matrix.at.z *= scale;
|
|
|
|
m_matrix.pos.x *= scale;
|
|
|
|
m_matrix.pos.y *= scale;
|
|
|
|
m_matrix.pos.z *= scale;
|
|
|
|
m_matrix.flags = 0;
|
|
|
|
}
|
|
|
|
|
2019-07-08 15:37:47 -04:00
|
|
|
|
2019-05-15 10:52:37 -04:00
|
|
|
void SetRotateXOnly(float angle){
|
2019-07-10 11:18:26 -04:00
|
|
|
float c = Cos(angle);
|
|
|
|
float s = Sin(angle);
|
2019-05-15 10:52:37 -04:00
|
|
|
|
|
|
|
m_matrix.right.x = 1.0f;
|
|
|
|
m_matrix.right.y = 0.0f;
|
|
|
|
m_matrix.right.z = 0.0f;
|
|
|
|
|
|
|
|
m_matrix.up.x = 0.0f;
|
|
|
|
m_matrix.up.y = c;
|
|
|
|
m_matrix.up.z = s;
|
|
|
|
|
|
|
|
m_matrix.at.x = 0.0f;
|
|
|
|
m_matrix.at.y = -s;
|
|
|
|
m_matrix.at.z = c;
|
|
|
|
}
|
|
|
|
void SetRotateX(float angle){
|
|
|
|
SetRotateXOnly(angle);
|
|
|
|
m_matrix.pos.x = 0.0f;
|
|
|
|
m_matrix.pos.y = 0.0f;
|
|
|
|
m_matrix.pos.z = 0.0f;
|
|
|
|
}
|
|
|
|
void SetRotateYOnly(float angle){
|
2019-07-10 11:18:26 -04:00
|
|
|
float c = Cos(angle);
|
|
|
|
float s = Sin(angle);
|
2019-05-15 10:52:37 -04:00
|
|
|
|
|
|
|
m_matrix.right.x = c;
|
|
|
|
m_matrix.right.y = 0.0f;
|
|
|
|
m_matrix.right.z = -s;
|
|
|
|
|
|
|
|
m_matrix.up.x = 0.0f;
|
|
|
|
m_matrix.up.y = 1.0f;
|
|
|
|
m_matrix.up.z = 0.0f;
|
|
|
|
|
|
|
|
m_matrix.at.x = s;
|
|
|
|
m_matrix.at.y = 0.0f;
|
|
|
|
m_matrix.at.z = c;
|
|
|
|
}
|
|
|
|
void SetRotateY(float angle){
|
|
|
|
SetRotateYOnly(angle);
|
|
|
|
m_matrix.pos.x = 0.0f;
|
|
|
|
m_matrix.pos.y = 0.0f;
|
|
|
|
m_matrix.pos.z = 0.0f;
|
|
|
|
}
|
|
|
|
void SetRotateZOnly(float angle){
|
2019-07-10 11:18:26 -04:00
|
|
|
float c = Cos(angle);
|
|
|
|
float s = Sin(angle);
|
2019-05-15 10:52:37 -04:00
|
|
|
|
|
|
|
m_matrix.right.x = c;
|
|
|
|
m_matrix.right.y = s;
|
|
|
|
m_matrix.right.z = 0.0f;
|
|
|
|
|
|
|
|
m_matrix.up.x = -s;
|
|
|
|
m_matrix.up.y = c;
|
|
|
|
m_matrix.up.z = 0.0f;
|
|
|
|
|
|
|
|
m_matrix.at.x = 0.0f;
|
|
|
|
m_matrix.at.y = 0.0f;
|
|
|
|
m_matrix.at.z = 1.0f;
|
|
|
|
}
|
2019-10-03 15:28:56 -04:00
|
|
|
void SetRotateZOnlyScaled(float angle, float scale) {
|
|
|
|
float c = Cos(angle);
|
|
|
|
float s = Sin(angle);
|
|
|
|
|
|
|
|
m_matrix.right.x = c * scale;
|
|
|
|
m_matrix.right.y = s * scale;
|
|
|
|
m_matrix.right.z = 0.0f;
|
|
|
|
|
|
|
|
m_matrix.up.x = -s * scale;
|
|
|
|
m_matrix.up.y = c * scale;
|
|
|
|
m_matrix.up.z = 0.0f;
|
|
|
|
|
|
|
|
m_matrix.at.x = 0.0f;
|
|
|
|
m_matrix.at.y = 0.0f;
|
|
|
|
m_matrix.at.z = scale;
|
|
|
|
}
|
2019-05-15 10:52:37 -04:00
|
|
|
void SetRotateZ(float angle){
|
|
|
|
SetRotateZOnly(angle);
|
|
|
|
m_matrix.pos.x = 0.0f;
|
|
|
|
m_matrix.pos.y = 0.0f;
|
|
|
|
m_matrix.pos.z = 0.0f;
|
|
|
|
}
|
2019-07-27 05:53:51 -04:00
|
|
|
void SetRotate(float xAngle, float yAngle, float zAngle);
|
|
|
|
void Rotate(float x, float y, float z);
|
2019-08-22 18:44:38 -04:00
|
|
|
void RotateX(float x);
|
2019-09-29 12:44:51 -04:00
|
|
|
void RotateZ(float z);
|
2019-06-24 10:57:54 -04:00
|
|
|
|
2019-07-27 05:53:51 -04:00
|
|
|
void Reorthogonalise(void);
|
2019-06-22 14:43:56 -04:00
|
|
|
void CopyOnlyMatrix(CMatrix *other){
|
|
|
|
m_matrix = other->m_matrix;
|
|
|
|
}
|
2019-06-30 06:59:55 -04:00
|
|
|
void SetUnity(void) {
|
|
|
|
m_matrix.right.x = 1.0f;
|
|
|
|
m_matrix.right.y = 0.0f;
|
|
|
|
m_matrix.right.z = 0.0f;
|
|
|
|
m_matrix.up.x = 0.0f;
|
|
|
|
m_matrix.up.y = 1.0f;
|
|
|
|
m_matrix.up.z = 0.0f;
|
|
|
|
m_matrix.at.x = 0.0f;
|
|
|
|
m_matrix.at.y = 0.0f;
|
|
|
|
m_matrix.at.z = 1.0f;
|
|
|
|
m_matrix.pos.x = 0.0f;
|
|
|
|
m_matrix.pos.y = 0.0f;
|
|
|
|
m_matrix.pos.z = 0.0f;
|
|
|
|
}
|
2019-05-15 10:52:37 -04:00
|
|
|
};
|
|
|
|
|
2019-07-27 05:53:51 -04:00
|
|
|
|
|
|
|
CMatrix &Invert(const CMatrix &src, CMatrix &dst);
|
|
|
|
CVector operator*(const CMatrix &mat, const CVector &vec);
|
|
|
|
CMatrix operator*(const CMatrix &m1, const CMatrix &m2);
|
|
|
|
CVector MultiplyInverse(const CMatrix &mat, const CVector &vec);
|
2019-10-06 08:49:01 -04:00
|
|
|
const CVector Multiply3x3(const CMatrix &mat, const CVector &vec);
|
|
|
|
const CVector Multiply3x3(const CVector &vec, const CMatrix &mat);
|
2019-05-15 10:52:37 -04:00
|
|
|
|
|
|
|
inline CMatrix
|
|
|
|
Invert(const CMatrix &matrix)
|
|
|
|
{
|
|
|
|
CMatrix inv;
|
|
|
|
return Invert(matrix, inv);
|
|
|
|
}
|
|
|
|
|
2019-07-07 12:36:55 -04:00
|
|
|
|
2019-06-22 18:34:11 -04:00
|
|
|
class CCompressedMatrixNotAligned
|
|
|
|
{
|
|
|
|
CVector m_vecPos;
|
|
|
|
int8 m_rightX;
|
|
|
|
int8 m_rightY;
|
|
|
|
int8 m_rightZ;
|
|
|
|
int8 m_upX;
|
|
|
|
int8 m_upY;
|
|
|
|
int8 m_upZ;
|
|
|
|
public:
|
|
|
|
void CompressFromFullMatrix(CMatrix &other)
|
|
|
|
{
|
2019-07-08 11:07:34 -04:00
|
|
|
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();
|
2019-06-22 18:34:11 -04:00
|
|
|
}
|
|
|
|
void DecompressIntoFullMatrix(CMatrix &other)
|
|
|
|
{
|
2019-07-08 11:07:34 -04:00
|
|
|
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;
|
2019-06-22 18:34:11 -04:00
|
|
|
other.Reorthogonalise();
|
|
|
|
}
|
2019-07-08 11:07:34 -04:00
|
|
|
};
|