2023-08-03 14:25:29 -04:00
|
|
|
|
2023-11-19 09:38:07 -05:00
|
|
|
#include "matrix.h"
|
2023-08-03 14:25:29 -04:00
|
|
|
|
2023-11-19 09:38:07 -05:00
|
|
|
#include "../decomp.h"
|
2023-08-03 14:25:29 -04:00
|
|
|
#include "math.h"
|
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
#include <memory.h>
|
2023-08-03 14:25:29 -04:00
|
|
|
|
2023-11-19 09:38:07 -05:00
|
|
|
DECOMP_SIZE_ASSERT(Matrix4, 0x40);
|
|
|
|
DECOMP_SIZE_ASSERT(Matrix4Impl, 0x8);
|
|
|
|
DECOMP_SIZE_ASSERT(Matrix4Data, 0x48);
|
2023-08-03 14:25:29 -04:00
|
|
|
|
|
|
|
// OFFSET: LEGO1 0x10002320
|
2023-11-19 09:38:07 -05:00
|
|
|
void Matrix4Impl::EqualsMatrixData(const Matrix4& p_matrix)
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-11-19 09:38:07 -05:00
|
|
|
*m_data = p_matrix;
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
2023-11-19 09:38:07 -05:00
|
|
|
// OFFSET: LEGO1 0x10002340
|
|
|
|
void Matrix4Impl::EqualsMatrixImpl(const Matrix4Impl* p_other)
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-11-19 09:38:07 -05:00
|
|
|
*m_data = *p_other->m_data;
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OFFSET: LEGO1 0x10002360
|
2023-11-19 09:38:07 -05:00
|
|
|
void Matrix4Impl::AnotherSetData(Matrix4& p_data)
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-11-19 09:38:07 -05:00
|
|
|
m_data = &p_data;
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
2023-11-19 09:38:07 -05:00
|
|
|
// OFFSET: LEGO1 0x10002370
|
|
|
|
void Matrix4Impl::SetData(Matrix4& p_data)
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-11-19 09:38:07 -05:00
|
|
|
m_data = &p_data;
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OFFSET: LEGO1 0x10002380
|
2023-11-19 09:38:07 -05:00
|
|
|
const Matrix4* Matrix4Impl::GetData() const
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
return m_data;
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
2023-11-19 09:38:07 -05:00
|
|
|
// OFFSET: LEGO1 0x10002390
|
|
|
|
Matrix4* Matrix4Impl::GetData()
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-11-19 09:38:07 -05:00
|
|
|
return m_data;
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OFFSET: LEGO1 0x100023a0
|
2023-11-19 09:38:07 -05:00
|
|
|
const float* Matrix4Impl::Element(int p_row, int p_col) const
|
|
|
|
{
|
|
|
|
return &(*m_data)[p_row][p_col];
|
|
|
|
}
|
|
|
|
|
|
|
|
// OFFSET: LEGO1 0x100023c0
|
|
|
|
float* Matrix4Impl::Element(int p_row, int p_col)
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-11-19 09:38:07 -05:00
|
|
|
return &(*m_data)[p_row][p_col];
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OFFSET: LEGO1 0x100023e0
|
2023-11-19 09:38:07 -05:00
|
|
|
void Matrix4Impl::Clear()
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
memset(m_data, 0, 16 * sizeof(float));
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OFFSET: LEGO1 0x100023f0
|
2023-11-19 09:38:07 -05:00
|
|
|
void Matrix4Impl::SetIdentity()
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
Clear();
|
2023-11-19 09:38:07 -05:00
|
|
|
(*m_data)[0][0] = 1.0f;
|
|
|
|
(*m_data)[1][1] = 1.0f;
|
|
|
|
(*m_data)[2][2] = 1.0f;
|
|
|
|
(*m_data)[3][3] = 1.0f;
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OFFSET: LEGO1 0x10002430
|
2023-11-19 09:38:07 -05:00
|
|
|
Matrix4Impl* Matrix4Impl::operator+=(const Matrix4& p_matrix)
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-10-24 19:38:27 -04:00
|
|
|
for (int i = 0; i < 16; ++i)
|
2023-11-19 09:38:07 -05:00
|
|
|
((float*) m_data)[i] += ((float*) &p_matrix)[i];
|
2023-10-24 19:38:27 -04:00
|
|
|
return this;
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Matches but instructions are significantly out of order. Probably not wrong
|
|
|
|
// code given that the very similar SetTranslation does match.
|
|
|
|
// OFFSET: LEGO1 0x10002460
|
2023-11-19 09:38:07 -05:00
|
|
|
void Matrix4Impl::TranslateBy(const float* p_x, const float* p_y, const float* p_z)
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-11-19 09:38:07 -05:00
|
|
|
((float*) m_data)[12] += *p_x;
|
|
|
|
((float*) m_data)[13] += *p_y;
|
|
|
|
((float*) m_data)[14] += *p_z;
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OFFSET: LEGO1 0x100024a0
|
2023-11-19 09:38:07 -05:00
|
|
|
void Matrix4Impl::SetTranslation(const float* p_x, const float* p_y, const float* p_z)
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-11-19 09:38:07 -05:00
|
|
|
(*m_data)[3][0] = *p_x;
|
|
|
|
(*m_data)[3][1] = *p_y;
|
|
|
|
(*m_data)[3][2] = *p_z;
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
2023-09-21 05:27:27 -04:00
|
|
|
// OFFSET: LEGO1 0x100024d0
|
2023-11-19 09:38:07 -05:00
|
|
|
void Matrix4Impl::EqualsDataProduct(const Matrix4& p_a, const Matrix4& p_b)
|
2023-10-24 19:38:27 -04:00
|
|
|
{
|
2023-11-19 09:38:07 -05:00
|
|
|
float* cur = (float*) m_data;
|
2023-10-24 19:38:27 -04:00
|
|
|
for (int row = 0; row < 4; ++row) {
|
|
|
|
for (int col = 0; col < 4; ++col) {
|
|
|
|
*cur = 0.0f;
|
|
|
|
for (int k = 0; k < 4; ++k) {
|
2023-11-19 09:38:07 -05:00
|
|
|
*cur += p_a[row][k] * p_b[k][col];
|
2023-10-24 19:38:27 -04:00
|
|
|
}
|
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
}
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
2023-11-19 09:38:07 -05:00
|
|
|
// OFFSET: LEGO1 0x10002530
|
|
|
|
void Matrix4Impl::EqualsMxProduct(const Matrix4Impl* p_a, const Matrix4Impl* p_b)
|
|
|
|
{
|
|
|
|
EqualsDataProduct(*p_a->m_data, *p_b->m_data);
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:25:29 -04:00
|
|
|
// Not close, Ghidra struggles understinging this method so it will have to
|
|
|
|
// be manually worked out. Included since I at least figured out what it was
|
|
|
|
// doing with rotateIndex and what overall operation it's trying to do.
|
|
|
|
// OFFSET: LEGO1 0x10002550 STUB
|
2023-11-19 09:38:07 -05:00
|
|
|
void Matrix4Impl::ToQuaternion(Vector4Impl* p_outQuat)
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-11-19 09:38:07 -05:00
|
|
|
/*
|
2023-10-24 19:38:27 -04:00
|
|
|
float trace = m_data[0] + m_data[5] + m_data[10];
|
|
|
|
if (trace > 0) {
|
|
|
|
trace = sqrt(trace + 1.0);
|
|
|
|
p_outQuat->GetData()[3] = trace * 0.5f;
|
|
|
|
p_outQuat->GetData()[0] = (m_data[9] - m_data[6]) * trace;
|
|
|
|
p_outQuat->GetData()[1] = (m_data[2] - m_data[8]) * trace;
|
|
|
|
p_outQuat->GetData()[2] = (m_data[4] - m_data[1]) * trace;
|
|
|
|
return;
|
|
|
|
}
|
2023-08-03 14:25:29 -04:00
|
|
|
|
2023-11-21 03:44:45 -05:00
|
|
|
// 0x100d4090
|
2023-10-24 19:38:27 -04:00
|
|
|
static int rotateIndex[] = {1, 2, 0};
|
2023-08-03 14:25:29 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
// Largest element along the trace
|
|
|
|
int largest = m_data[0] < m_data[5];
|
|
|
|
if (*Element(largest, largest) < m_data[10])
|
|
|
|
largest = 2;
|
2023-08-03 14:25:29 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
int next = rotateIndex[largest];
|
|
|
|
int nextNext = rotateIndex[next];
|
|
|
|
float valueA = *Element(nextNext, nextNext);
|
|
|
|
float valueB = *Element(next, next);
|
|
|
|
float valueC = *Element(largest, largest);
|
2023-08-03 14:25:29 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
// Above is somewhat decomped, below is pure speculation since the automatic
|
|
|
|
// decomp becomes very garbled.
|
|
|
|
float traceValue = sqrt(valueA - valueB - valueC + 1.0);
|
2023-08-03 14:25:29 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
p_outQuat->GetData()[largest] = traceValue * 0.5f;
|
|
|
|
traceValue = 0.5f / traceValue;
|
2023-08-03 14:25:29 -04:00
|
|
|
|
2023-10-24 19:38:27 -04:00
|
|
|
p_outQuat->GetData()[3] = (m_data[next + 4 * nextNext] - m_data[nextNext + 4 * next]) * traceValue;
|
|
|
|
p_outQuat->GetData()[next] = (m_data[next + 4 * largest] + m_data[largest + 4 * next]) * traceValue;
|
|
|
|
p_outQuat->GetData()[nextNext] = (m_data[nextNext + 4 * largest] + m_data[largest + 4 * nextNext]) * traceValue;
|
2023-11-19 09:38:07 -05:00
|
|
|
*/
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// No idea what this function is doing and it will be hard to tell until
|
|
|
|
// we have a confirmed usage site.
|
|
|
|
// OFFSET: LEGO1 0x10002710 STUB
|
2023-11-19 09:38:07 -05:00
|
|
|
int Matrix4Impl::FUN_10002710(const Vector3Impl* p_vec)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// OFFSET: LEGO1 0x10002850
|
|
|
|
void Matrix4Impl::operator=(const Matrix4Impl& p_other)
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-11-19 09:38:07 -05:00
|
|
|
EqualsMatrixImpl(&p_other);
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OFFSET: LEGO1 0x10002860
|
2023-11-19 09:38:07 -05:00
|
|
|
void Matrix4Data::operator=(const Matrix4Data& p_other)
|
2023-08-03 14:25:29 -04:00
|
|
|
{
|
2023-11-19 09:38:07 -05:00
|
|
|
EqualsMatrixImpl(&p_other);
|
2023-08-03 14:25:29 -04:00
|
|
|
}
|