2019-05-15 14:52:37 +00: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 18:07:37 +00:00
|
|
|
CMatrix(RwMatrix *matrix, bool owner = false){
|
2019-05-15 14:52:37 +00:00
|
|
|
m_attachment = nil;
|
2019-06-12 18:07:37 +00:00
|
|
|
Attach(matrix, owner);
|
2019-05-15 14:52:37 +00:00
|
|
|
}
|
2019-06-29 11:38:37 +00:00
|
|
|
CMatrix(float scale){
|
|
|
|
m_attachment = nil;
|
|
|
|
m_hasRwMatrix = false;
|
|
|
|
SetScale(scale);
|
|
|
|
}
|
2019-05-15 14:52:37 +00:00
|
|
|
~CMatrix(void){
|
|
|
|
if(m_hasRwMatrix && m_attachment)
|
|
|
|
RwMatrixDestroy(m_attachment);
|
|
|
|
}
|
2019-06-12 18:07:37 +00:00
|
|
|
void Attach(RwMatrix *matrix, bool owner = false){
|
2019-05-15 14:52:37 +00:00
|
|
|
if(m_hasRwMatrix && m_attachment)
|
|
|
|
RwMatrixDestroy(m_attachment);
|
|
|
|
m_attachment = matrix;
|
2019-06-12 18:07:37 +00:00
|
|
|
m_hasRwMatrix = owner;
|
2019-05-15 14:52:37 +00:00
|
|
|
Update();
|
|
|
|
}
|
2019-06-12 18:07:37 +00:00
|
|
|
void AttachRW(RwMatrix *matrix, bool owner = false){
|
2019-05-15 14:52:37 +00:00
|
|
|
if(m_hasRwMatrix && m_attachment)
|
|
|
|
RwMatrixDestroy(m_attachment);
|
|
|
|
m_attachment = matrix;
|
2019-06-12 18:07:37 +00:00
|
|
|
m_hasRwMatrix = owner;
|
2019-05-15 14:52:37 +00: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 11:38:37 +00: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 15:01:43 +00:00
|
|
|
m_matrix.pos.z += rhs.m_matrix.pos.z;
|
2019-06-29 11:38:37 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2019-05-15 14:52:37 +00:00
|
|
|
|
2019-07-08 15:07:34 +00: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 19:37:47 +00: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 14:52:37 +00: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 09:53:51 +00:00
|
|
|
void Scale(float scale)
|
|
|
|
{
|
2019-10-16 23:22:39 +00:00
|
|
|
float *pFloatMatrix = (float*)&m_matrix;
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
#ifdef FIX_BUGS // BUGFIX from VC
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
#else
|
|
|
|
for (int j = 0; j < 4; j++)
|
|
|
|
#endif
|
|
|
|
pFloatMatrix[i * 4 + j] *= scale;
|
2019-07-27 09:53:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 19:37:47 +00:00
|
|
|
|
2019-05-15 14:52:37 +00:00
|
|
|
void SetRotateXOnly(float angle){
|
2019-07-10 15:18:26 +00:00
|
|
|
float c = Cos(angle);
|
|
|
|
float s = Sin(angle);
|
2019-05-15 14:52:37 +00: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 15:18:26 +00:00
|
|
|
float c = Cos(angle);
|
|
|
|
float s = Sin(angle);
|
2019-05-15 14:52:37 +00: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 15:18:26 +00:00
|
|
|
float c = Cos(angle);
|
|
|
|
float s = Sin(angle);
|
2019-05-15 14:52:37 +00: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 19:28:56 +00: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 14:52:37 +00: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 09:53:51 +00:00
|
|
|
void SetRotate(float xAngle, float yAngle, float zAngle);
|
|
|
|
void Rotate(float x, float y, float z);
|
2019-08-22 22:44:38 +00:00
|
|
|
void RotateX(float x);
|
2019-09-29 16:44:51 +00:00
|
|
|
void RotateZ(float z);
|
2019-06-24 14:57:54 +00:00
|
|
|
|
2019-07-27 09:53:51 +00:00
|
|
|
void Reorthogonalise(void);
|
2019-06-22 18:43:56 +00:00
|
|
|
void CopyOnlyMatrix(CMatrix *other){
|
|
|
|
m_matrix = other->m_matrix;
|
|
|
|
}
|
2019-06-30 10:59:55 +00: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 14:52:37 +00:00
|
|
|
};
|
|
|
|
|
2019-07-27 09:53:51 +00: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 12:49:01 +00:00
|
|
|
const CVector Multiply3x3(const CMatrix &mat, const CVector &vec);
|
|
|
|
const CVector Multiply3x3(const CVector &vec, const CMatrix &mat);
|
2019-05-15 14:52:37 +00:00
|
|
|
|
|
|
|
inline CMatrix
|
|
|
|
Invert(const CMatrix &matrix)
|
|
|
|
{
|
|
|
|
CMatrix inv;
|
|
|
|
return Invert(matrix, inv);
|
|
|
|
}
|
|
|
|
|
2019-07-07 16:36:55 +00:00
|
|
|
|
2019-06-22 22:34:11 +00: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 15:07:34 +00: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 22:34:11 +00:00
|
|
|
}
|
|
|
|
void DecompressIntoFullMatrix(CMatrix &other)
|
|
|
|
{
|
2019-07-08 15:07:34 +00: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 22:34:11 +00:00
|
|
|
other.Reorthogonalise();
|
|
|
|
}
|
2019-07-08 15:07:34 +00:00
|
|
|
};
|