From 5c8ad270ef357de43e3463b2eb5c51db97dfe22a Mon Sep 17 00:00:00 2001 From: Nathan Date: Fri, 10 Nov 2023 17:27:28 -0500 Subject: [PATCH] Implement ViewROI and base classes --- CMakeLists.txt | 2 + LEGO1/lodlist.h | 180 ++++++++++++++++++++ LEGO1/mxmatrix.cpp | 117 ++++++------- LEGO1/mxmatrix.h | 63 ++++--- LEGO1/mxtypes.h | 10 +- LEGO1/mxvector.h | 50 +++++- LEGO1/orientableroi.cpp | 66 ++++++++ LEGO1/orientableroi.h | 52 ++++++ LEGO1/roi.h | 105 ++++++++++++ LEGO1/tgl.h | 364 ++++++++++++++++++++++++++++++++++++++++ LEGO1/tglvector.h | 277 ++++++++++++++++++++++++++++++ LEGO1/viewlodlist.h | 116 +++++++++++++ LEGO1/viewroi.cpp | 45 +++++ LEGO1/viewroi.h | 47 ++++++ 14 files changed, 1413 insertions(+), 81 deletions(-) create mode 100644 LEGO1/lodlist.h create mode 100644 LEGO1/orientableroi.cpp create mode 100644 LEGO1/orientableroi.h create mode 100644 LEGO1/roi.h create mode 100644 LEGO1/tgl.h create mode 100644 LEGO1/tglvector.h create mode 100644 LEGO1/viewlodlist.h create mode 100644 LEGO1/viewroi.cpp create mode 100644 LEGO1/viewroi.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a297f7f4..d94e3bb5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -189,6 +189,7 @@ add_library(lego1 SHARED LEGO1/mxvideoparamflags.cpp LEGO1/mxvideopresenter.cpp LEGO1/mxwavepresenter.cpp + LEGO1/orientableroi.cpp LEGO1/pizza.cpp LEGO1/pizzamissionstate.cpp LEGO1/pizzeria.cpp @@ -210,6 +211,7 @@ add_library(lego1 SHARED LEGO1/towtrack.cpp LEGO1/towtrackmissionstate.cpp LEGO1/viewmanager.cpp + LEGO1/viewroi.cpp ) if (MINGW) target_compile_definitions(lego1 PRIVATE DIRECTINPUT_VERSION=0x0500) diff --git a/LEGO1/lodlist.h b/LEGO1/lodlist.h new file mode 100644 index 00000000..8e40d92a --- /dev/null +++ b/LEGO1/lodlist.h @@ -0,0 +1,180 @@ +#ifndef LODLIST_H +#define LODLIST_H + +#include "assert.h" + +#include // size_t + +class LODObject; + +// disable: identifier was truncated to '255' characters in the debug information +#pragma warning(disable : 4786) + +////////////////////////////////////////////////////////////////////////////// +// +// LODListBase +// +// An LODListBase is an ordered list of LODObjects +// where each successive object in the list has a more complex +// geometric representation than the one preceeding it. +// + +class LODListBase { +protected: + LODListBase(size_t capacity); + + const LODObject* PushBack(const LODObject*); + const LODObject* PopBack(); + +public: + virtual ~LODListBase(); + const LODObject* operator[](int) const; + + // current number of LODObject* in LODListBase + size_t Size() const; + + // maximum number of LODObject* LODListBase can hold + size_t Capacity() const; + +#ifdef _DEBUG + virtual void Dump(void (*pTracer)(const char*, ...)) const; +#endif + +private: + // not implemented + LODListBase(const LODListBase&); + LODListBase& operator=(const LODListBase&); + +private: + const LODObject** m_ppLODObject; + size_t m_capacity; + size_t m_size; +}; + +////////////////////////////////////////////////////////////////////////////// +// +// LODList +// + +template +class LODList : public LODListBase { +public: + LODList(size_t capacity); + + const T* operator[](int) const; + const T* PushBack(const T*); + const T* PopBack(); +}; + +////////////////////////////////////////////////////////////////////////////// +// +// LODListBase implementation + +inline LODListBase::LODListBase(size_t capacity) + : m_capacity(capacity), m_size(0), m_ppLODObject(new const LODObject*[capacity]) +{ +#ifdef _DEBUG + int i; + + for (i = 0; i < (int) m_capacity; i++) { + m_ppLODObject[i] = 0; + } +#endif +} + +inline LODListBase::~LODListBase() +{ + // all LODObject* should have been popped by client + assert(m_size == 0); + + delete[] m_ppLODObject; +} + +inline size_t LODListBase::Size() const +{ + return m_size; +} + +inline size_t LODListBase::Capacity() const +{ + return m_capacity; +} + +inline const LODObject* LODListBase::operator[](int i) const +{ + assert((0 <= i) && (i < (int) m_size)); + + return m_ppLODObject[i]; +} + +inline const LODObject* LODListBase::PushBack(const LODObject* pLOD) +{ + assert(m_size < m_capacity); + + m_ppLODObject[m_size++] = pLOD; + return pLOD; +} + +inline const LODObject* LODListBase::PopBack() +{ + const LODObject* pLOD; + + assert(m_size > 0); + + pLOD = m_ppLODObject[--m_size]; + +#ifdef _DEBUG + m_ppLODObject[m_size] = 0; +#endif + + return pLOD; +} + +#ifdef _DEBUG +inline void LODListBase::Dump(void (*pTracer)(const char*, ...)) const +{ + int i; + + pTracer("LODListBase<0x%x>: Capacity=%d, Size=%d\n", (void*) this, m_capacity, m_size); + + for (i = 0; i < (int) m_size; i++) { + pTracer(" [%d]: LOD<0x%x>\n", i, m_ppLODObject[i]); + } + + for (i = (int) m_size; i < (int) m_capacity; i++) { + assert(m_ppLODObject[i] == 0); + } +} +#endif + +////////////////////////////////////////////////////////////////////////////// +// +// LODList implementation + +template +inline LODList::LODList(size_t capacity) : LODListBase(capacity) +{ +} + +template +inline const T* LODList::operator[](int i) const +{ + return static_cast(LODListBase::operator[](i)); +} + +template +inline const T* LODList::PushBack(const T* pLOD) +{ + return static_cast(LODListBase::PushBack(pLOD)); +} + +template +inline const T* LODList::PopBack() +{ + return static_cast(LODListBase::PopBack()); +} + +// re-enable: identifier was truncated to '255' characters in the debug information +#pragma warning(default : 4786) + +#endif // LODLIST_H diff --git a/LEGO1/mxmatrix.cpp b/LEGO1/mxmatrix.cpp index 97a65727..8380f4de 100644 --- a/LEGO1/mxmatrix.cpp +++ b/LEGO1/mxmatrix.cpp @@ -6,55 +6,56 @@ #include +DECOMP_SIZE_ASSERT(Matrix4, 0x40); DECOMP_SIZE_ASSERT(MxMatrix, 0x8); DECOMP_SIZE_ASSERT(MxMatrixData, 0x48); +// OFFSET: LEGO1 0x10002320 +void MxMatrix::EqualsMatrixData(const Matrix4& p_matrix) +{ + *m_data = p_matrix; +} + // OFFSET: LEGO1 0x10002340 void MxMatrix::EqualsMxMatrix(const MxMatrix* p_other) { - memcpy(m_data, p_other->m_data, 16 * sizeof(float)); -} - -// OFFSET: LEGO1 0x10002320 -void MxMatrix::EqualsMatrixData(const float* p_matrix) -{ - memcpy(m_data, p_matrix, 16 * sizeof(float)); -} - -// OFFSET: LEGO1 0x10002370 -void MxMatrix::SetData(float* p_data) -{ - m_data = p_data; + *m_data = *p_other->m_data; } // OFFSET: LEGO1 0x10002360 -void MxMatrix::AnotherSetData(float* p_data) +void MxMatrix::AnotherSetData(Matrix4& p_data) { - m_data = p_data; + m_data = &p_data; } -// OFFSET: LEGO1 0x10002390 -float* MxMatrix::GetData() +// OFFSET: LEGO1 0x10002370 +void MxMatrix::SetData(Matrix4& p_data) { - return m_data; + m_data = &p_data; } // OFFSET: LEGO1 0x10002380 -const float* MxMatrix::GetData() const +const Matrix4* MxMatrix::GetData() const { return m_data; } -// OFFSET: LEGO1 0x100023c0 -float* MxMatrix::Element(int p_row, int p_col) +// OFFSET: LEGO1 0x10002390 +Matrix4* MxMatrix::GetData() { - return &m_data[p_row * 4 + p_col]; + return m_data; } // OFFSET: LEGO1 0x100023a0 const float* MxMatrix::Element(int p_row, int p_col) const { - return &m_data[p_row * 4 + p_col]; + return &(*m_data)[p_row][p_col]; +} + +// OFFSET: LEGO1 0x100023c0 +float* MxMatrix::Element(int p_row, int p_col) +{ + return &(*m_data)[p_row][p_col]; } // OFFSET: LEGO1 0x100023e0 @@ -67,23 +68,17 @@ void MxMatrix::Clear() void MxMatrix::SetIdentity() { Clear(); - m_data[0] = 1.0f; - m_data[5] = 1.0f; - m_data[10] = 1.0f; - m_data[15] = 1.0f; -} - -// OFFSET: LEGO1 0x10002850 -void MxMatrix::operator=(const MxMatrix& p_other) -{ - EqualsMxMatrix(&p_other); + (*m_data)[0][0] = 1.0f; + (*m_data)[1][1] = 1.0f; + (*m_data)[2][2] = 1.0f; + (*m_data)[3][3] = 1.0f; } // OFFSET: LEGO1 0x10002430 -MxMatrix* MxMatrix::operator+=(const float* p_matrix) +MxMatrix* MxMatrix::operator+=(const Matrix4& p_matrix) { for (int i = 0; i < 16; ++i) - m_data[i] += p_matrix[i]; + ((float*) m_data)[i] += ((float*) &p_matrix)[i]; return this; } @@ -92,38 +87,38 @@ MxMatrix* MxMatrix::operator+=(const float* p_matrix) // OFFSET: LEGO1 0x10002460 void MxMatrix::TranslateBy(const float* p_x, const float* p_y, const float* p_z) { - m_data[12] += *p_x; - m_data[13] += *p_y; - m_data[14] += *p_z; + ((float*) m_data)[12] += *p_x; + ((float*) m_data)[13] += *p_y; + ((float*) m_data)[14] += *p_z; } // OFFSET: LEGO1 0x100024a0 void MxMatrix::SetTranslation(const float* p_x, const float* p_y, const float* p_z) { - m_data[12] = *p_x; - m_data[13] = *p_y; - m_data[14] = *p_z; + (*m_data)[3][0] = *p_x; + (*m_data)[3][1] = *p_y; + (*m_data)[3][2] = *p_z; +} + +// OFFSET: LEGO1 0x100024d0 +void MxMatrix::EqualsDataProduct(const Matrix4& p_a, const Matrix4& p_b) +{ + float* cur = (float*) m_data; + for (int row = 0; row < 4; ++row) { + for (int col = 0; col < 4; ++col) { + *cur = 0.0f; + for (int k = 0; k < 4; ++k) { + *cur += p_a[row][k] * p_b[k][col]; + } + cur++; + } + } } // OFFSET: LEGO1 0x10002530 void MxMatrix::EqualsMxProduct(const MxMatrix* p_a, const MxMatrix* p_b) { - EqualsDataProduct(p_a->m_data, p_b->m_data); -} - -// OFFSET: LEGO1 0x100024d0 -void MxMatrix::EqualsDataProduct(const float* p_a, const float* p_b) -{ - float* cur = m_data; - for (int row = 0; row < 4; ++row) { - for (int col = 0; col < 4; ++col) { - *cur = 0.0f; - for (int k = 0; k < 4; ++k) { - *cur += p_a[row * 4 + k] * p_b[k * 4 + col]; - } - cur++; - } - } + EqualsDataProduct(*p_a->m_data, *p_b->m_data); } // Not close, Ghidra struggles understinging this method so it will have to @@ -132,6 +127,7 @@ void MxMatrix::EqualsDataProduct(const float* p_a, const float* p_b) // OFFSET: LEGO1 0x10002550 STUB void MxMatrix::ToQuaternion(MxVector4* p_outQuat) { + /* float trace = m_data[0] + m_data[5] + m_data[10]; if (trace > 0) { trace = sqrt(trace + 1.0); @@ -166,6 +162,7 @@ void MxMatrix::ToQuaternion(MxVector4* p_outQuat) 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; + */ } // No idea what this function is doing and it will be hard to tell until @@ -176,6 +173,12 @@ MxResult MxMatrix::FUN_10002710(const MxVector3* p_vec) return FAILURE; } +// OFFSET: LEGO1 0x10002850 +void MxMatrix::operator=(const MxMatrix& p_other) +{ + EqualsMxMatrix(&p_other); +} + // OFFSET: LEGO1 0x10002860 void MxMatrixData::operator=(const MxMatrixData& p_other) { diff --git a/LEGO1/mxmatrix.h b/LEGO1/mxmatrix.h index b9d5420d..32140ee9 100644 --- a/LEGO1/mxmatrix.h +++ b/LEGO1/mxmatrix.h @@ -3,21 +3,48 @@ #include "mxvector.h" +/* + * A simple array of four Vector4s that can be indexed into. + */ +class Matrix4 { +public: + float rows[4][4]; // storage is public for easy access + inline Matrix4() {} + /* + Matrix4(const Vector4& x_axis, const Vector4& y_axis, const Vector4& z_axis, const Vector4& position) + { + rows[0] = x_axis; + rows[1] = y_axis; + rows[2] = z_axis; + rows[3] = position; + } + Matrix4(const float m[4][4]) + { + rows[0] = m[0]; + rows[1] = m[1]; + rows[2] = m[2]; + rows[3] = m[3]; + } + */ + const float* operator[](long i) const { return rows[i]; } + float* operator[](long i) { return rows[i]; } +}; + // VTABLE 0x100d4350 // SIZE 0x8 class MxMatrix { public: - inline MxMatrix(float* p_data) : m_data(p_data) {} + inline MxMatrix(Matrix4& p_data) : m_data(&p_data) {} // vtable + 0x00 virtual void EqualsMxMatrix(const MxMatrix* p_other); - virtual void EqualsMatrixData(const float* p_matrix); - virtual void SetData(float* p_data); - virtual void AnotherSetData(float* p_data); + virtual void EqualsMatrixData(const Matrix4& p_matrix); + virtual void SetData(Matrix4& p_data); + virtual void AnotherSetData(Matrix4& p_data); // vtable + 0x10 - virtual float* GetData(); - virtual const float* GetData() const; + virtual Matrix4* GetData(); + virtual const Matrix4* GetData() const; virtual float* Element(int p_row, int p_col); virtual const float* Element(int p_row, int p_col) const; @@ -25,44 +52,38 @@ class MxMatrix { virtual void Clear(); virtual void SetIdentity(); virtual void operator=(const MxMatrix& p_other); - virtual MxMatrix* operator+=(const float* p_matrix); + virtual MxMatrix* operator+=(const Matrix4& p_matrix); // vtable + 0x30 virtual void TranslateBy(const float* p_x, const float* p_y, const float* p_z); virtual void SetTranslation(const float* p_x, const float* p_y, const float* p_z); virtual void EqualsMxProduct(const MxMatrix* p_a, const MxMatrix* p_b); - virtual void EqualsDataProduct(const float* p_a, const float* p_b); + virtual void EqualsDataProduct(const Matrix4& p_a, const Matrix4& p_b); // vtable + 0x40 virtual void ToQuaternion(MxVector4* p_resultQuat); virtual MxResult FUN_10002710(const MxVector3* p_vec); - inline float& operator[](size_t idx) { return m_data[idx]; } + inline float& operator[](size_t idx) { return (*m_data)[idx >> 2][idx & 3]; } -private: - float* m_data; +protected: + Matrix4* m_data; }; // VTABLE 0x100d4300 // SIZE 0x48 class MxMatrixData : public MxMatrix { public: - inline MxMatrixData() : MxMatrix(e) {} + inline MxMatrixData() : MxMatrix(m) {} + inline MxMatrixData(MxMatrixData& p_other) : MxMatrix(m) { m = *p_other.m_data; } + inline Matrix4& GetMatrix() { return *m_data; } // No idea why there's another equals. Maybe to some other type like the // DirectX Retained Mode Matrix type which is also a float* alias? // vtable + 0x44 virtual void operator=(const MxMatrixData& p_other); - // Alias an easy way to access the translation part of the matrix, because - // various members / other functions benefit from the clarity. - union { - float e[16]; - struct { - float _[12]; - float x, y, z, w; - }; - }; + Matrix4 m; }; #endif // MXMATRIX_H diff --git a/LEGO1/mxtypes.h b/LEGO1/mxtypes.h index f8158a73..153289a1 100644 --- a/LEGO1/mxtypes.h +++ b/LEGO1/mxtypes.h @@ -33,8 +33,14 @@ typedef unsigned int MxULong; typedef MxS32 MxTime; typedef MxLong MxResult; -const MxResult SUCCESS = 0; -const MxResult FAILURE = -1; + +#ifndef SUCCESS +#define SUCCESS 0 +#endif + +#ifndef FAILURE +#define FAILURE -1 +#endif typedef MxU8 MxBool; diff --git a/LEGO1/mxvector.h b/LEGO1/mxvector.h index 783f3f22..3b850ee1 100644 --- a/LEGO1/mxvector.h +++ b/LEGO1/mxvector.h @@ -5,6 +5,54 @@ #include +/* + * A simple array of three floats that can be indexed into. + */ +class Vector3 { +public: + float elements[3]; // storage is public for easy access + Vector3() {} + Vector3(float x, float y, float z) + { + elements[0] = x; + elements[1] = y; + elements[2] = z; + } + Vector3(const float v[3]) + { + elements[0] = v[0]; + elements[1] = v[1]; + elements[2] = v[2]; + } + const float& operator[](long i) const { return elements[i]; } + float& operator[](long i) { return elements[i]; } +}; + +/* + * A simple array of four floats that can be indexed into. + */ +struct Vector4 { +public: + float elements[4]; // storage is public for easy access + inline Vector4() {} + Vector4(float x, float y, float z, float w) + { + elements[0] = x; + elements[1] = y; + elements[2] = z; + elements[3] = w; + } + Vector4(const float v[4]) + { + elements[0] = v[0]; + elements[1] = v[1]; + elements[2] = v[2]; + elements[3] = v[3]; + } + const float& operator[](long i) const { return elements[i]; } + float& operator[](long i) { return elements[i]; } +}; + // VTABLE 0x100d4288 // SIZE 0x8 class MxVector2 { @@ -28,8 +76,8 @@ class MxVector2 { // vtable + 0x20 virtual void EqualsImpl(float* p_data) = 0; - virtual const float* GetData() const; virtual float* GetData(); + virtual const float* GetData() const; virtual void Clear() = 0; // vtable + 0x30 diff --git a/LEGO1/orientableroi.cpp b/LEGO1/orientableroi.cpp new file mode 100644 index 00000000..a5915847 --- /dev/null +++ b/LEGO1/orientableroi.cpp @@ -0,0 +1,66 @@ +#include "orientableroi.h" + +#include "decomp.h" + +DECOMP_SIZE_ASSERT(OrientableROI, 0xdc) + +// OFFSET: LEGO1 0x100a5910 +void OrientableROI::VTable0x1c() +{ + UpdateWorldBoundingVolumes(); + UpdateWorldVelocity(); +} + +// OFFSET: LEGO1 0x100a5930 +void OrientableROI::SetLocalTransform(const MxMatrix& p_transform) +{ + reinterpret_cast(m_local2world) = p_transform; + UpdateWorldBoundingVolumes(); + UpdateWorldVelocity(); +} + +// OFFSET: LEGO1 0x100a5960 +void OrientableROI::VTable0x24(const MxMatrixData& p_transform) +{ + MxMatrixData l_matrix(m_local2world); + m_local2world.EqualsMxProduct(&p_transform, &l_matrix); + UpdateWorldBoundingVolumes(); + UpdateWorldVelocity(); +} + +// OFFSET: LEGO1 0x100a59b0 +void OrientableROI::UpdateWorldData(const MxMatrixData& p_transform) +{ + MxMatrixData l_matrix(m_local2world); + m_local2world.EqualsMxProduct(&l_matrix, &p_transform); + UpdateWorldBoundingVolumes(); + UpdateWorldVelocity(); + + // iterate over comps + if (m_comp) + for (CompoundObject::iterator iter = m_comp->begin(); !(iter == m_comp->end()); iter++) { + ROI* child = *iter; + static_cast(child)->UpdateWorldData(m_local2world); + } +} + +// OFFSET: LEGO1 0x100a5a50 +void OrientableROI::UpdateWorldVelocity() +{ +} + +// OFFSET: LEGO1 0x100a5d80 +const Vector3& OrientableROI::GetWorldVelocity() const +{ + return (Vector3&) *m_world_velocity.GetData(); +} +// OFFSET: LEGO1 0x100a5d90 +const BoundingBox& OrientableROI::GetWorldBoundingBox() const +{ + return m_world_bounding_box; +} +// OFFSET: LEGO1 0x100a5da0 +const BoundingSphere& OrientableROI::GetWorldBoundingSphere() const +{ + return m_world_bounding_sphere; +} diff --git a/LEGO1/orientableroi.h b/LEGO1/orientableroi.h new file mode 100644 index 00000000..a45ac77d --- /dev/null +++ b/LEGO1/orientableroi.h @@ -0,0 +1,52 @@ +#ifndef ORIENTABLEROI_H +#define ORIENTABLEROI_H + +#include "mxmatrix.h" +#include "roi.h" + +class OrientableROI : public ROI { +public: + // OFFSET: LEGO1 0x100a4420 + OrientableROI() + { + FILLVEC3(m_world_bounding_box.Min(), 888888.8); + FILLVEC3(m_world_bounding_box.Max(), -888888.8); + ZEROVEC3(m_world_bounding_sphere.Center()); + m_world_bounding_sphere.Radius() = 0.0; + ZEROVEC3(m_world_velocity); + IDENTMAT4(m_local2world.GetMatrix()); + } + // OFFSET: LEGO1 0x100a4630 TEMPLATE + // OrientableROI::`scalar deleting destructor' + + virtual const Vector3& GetWorldVelocity() const; + virtual const BoundingBox& GetWorldBoundingBox() const; + virtual const BoundingSphere& GetWorldBoundingSphere() const; + // virtual float* GetWorldPosition() const { return (float*) m_local2world.GetData() + 12; } + // virtual float* GetWorldDirection() const { return (float*) m_local2world.GetData() + 8; } + // virtual float* GetWorldUp() const { return (float*) m_local2world.GetData() + 4; } + +protected: + // vtable + 0x14 + virtual void VTable0x14() { VTable0x1c(); } + virtual void UpdateWorldBoundingVolumes() = 0; + +public: + virtual void OrientableROI::VTable0x1c(); + // vtable + 0x20 + virtual void OrientableROI::SetLocalTransform(const MxMatrix& p_transform); + virtual void OrientableROI::VTable0x24(const MxMatrixData& p_transform); + virtual void OrientableROI::UpdateWorldData(const MxMatrixData& p_transform); + virtual void OrientableROI::UpdateWorldVelocity(); + +protected: + char m_unkc; + MxMatrixData m_local2world; // 0x10 + BoundingBox m_world_bounding_box; // 0x58 + BoundingSphere m_world_bounding_sphere; // 0xa8 + MxVector3Data m_world_velocity; // 0xc0 + MxU32 m_unkd4; + MxU32 m_unkd8; +}; + +#endif // ORIENTABLEROI_H diff --git a/LEGO1/roi.h b/LEGO1/roi.h new file mode 100644 index 00000000..58d03745 --- /dev/null +++ b/LEGO1/roi.h @@ -0,0 +1,105 @@ +#ifndef ROI_H +#define ROI_H + +#include "compat.h" +#include "lodlist.h" +#include "mxstl.h" +#include "mxvector.h" +#include "realtime/realtime.h" + +/* + * A simple bounding box object with Min and Max accessor methods. + */ +class BoundingBox { +public: + const MxVector3Data& Min() const { return min; } + MxVector3Data& Min() { return min; } + const MxVector3Data& Max() const { return max; } + MxVector3Data& Max() { return max; } + +private: + MxVector3Data min; + MxVector3Data max; + MxVector3Data m_unk28; + MxVector3Data m_unk3c; +}; + +/* + * A simple bounding sphere object with center and radius accessor methods. + */ +class BoundingSphere { +public: + const MxVector3Data& Center() const { return center; } + MxVector3Data& Center() { return center; } + const float& Radius() const { return radius; } + float& Radius() { return radius; } + +private: + MxVector3Data center; + float radius; +}; + +/* + * Abstract base class representing a single LOD version of + * a geometric object. + */ +class LODObject { +public: + // LODObject(); + virtual ~LODObject() {} + virtual float Cost(float pixels_covered) const = 0; + virtual float AveragePolyArea() const = 0; + virtual int NVerts() const = 0; +}; + +/* + * A CompoundObject is simply a set of ROI objects which + * all together represent a single object with sub-parts. + */ +class ROI; +// typedef std::set > CompoundObject; +typedef list CompoundObject; + +/* + * A ROIList is a list of ROI objects. + */ +typedef vector ROIList; + +/* + * A simple list of integers. + * Returned by RealtimeView::SelectLODs as indices into an ROIList. + */ +typedef vector IntList; + +class ROI { +public: + ROI() + { + m_comp = 0; + m_lods = 0; + } + virtual ~ROI() + { + // if derived class set the comp and lods, it should delete them + assert(!m_comp); + assert(!m_lods); + } + virtual float IntrinsicImportance() const = 0; + virtual const Vector3& GetWorldVelocity() const = 0; + virtual const BoundingBox& GetWorldBoundingBox() const = 0; + virtual const BoundingSphere& GetWorldBoundingSphere() const = 0; + + const LODListBase* GetLODs() const { return m_lods; } + const LODObject* GetLOD(int i) const + { + assert(m_lods); + return (*m_lods)[i]; + } + int GetLODCount() const { return m_lods ? m_lods->Size() : 0; } + const CompoundObject* GetComp() const { return m_comp; } + +protected: + CompoundObject* m_comp; + LODListBase* m_lods; +}; +#endif // ROI_H diff --git a/LEGO1/tgl.h b/LEGO1/tgl.h new file mode 100644 index 00000000..080c8ea0 --- /dev/null +++ b/LEGO1/tgl.h @@ -0,0 +1,364 @@ +#ifndef TGL_H +#define TGL_H + +#ifdef _WIN32 + +#define NOMINMAX // to avoid conflict with STL +#include +#include +#include // HWND + +#endif /* _WIN32 */ + +#include "mxmatrix.h" +#include "tglVector.h" + +namespace Tgl +{ + +// ??? +enum ColorModel { + Ramp, + RGB +}; + +// ??? +enum ShadingModel { + Wireframe, + UnlitFlat, + Flat, + Gouraud, + Phong +}; + +// ????? +enum LightType { + Ambient, + Point, + Spot, + Directional, + ParallelPoint +}; + +// ??? +enum ProjectionType { + Perspective, + Orthographic +}; + +enum TextureMappingMode { + Linear, + PerspectiveCorrect +}; + +struct PaletteEntry { + unsigned char m_red; + unsigned char m_green; + unsigned char m_blue; +}; + +#ifdef _WIN32 + +struct DeviceDirectDrawCreateData { + const GUID* m_driverGUID; + HWND m_hWnd; // ??? derive from m_pDirectDraw + IDirectDraw* m_pDirectDraw; + IDirectDrawSurface* m_pFrontBuffer; // ??? derive from m_pDirectDraw + IDirectDrawSurface* m_pBackBuffer; + IDirectDrawPalette* m_pPalette; // ??? derive from m_pDirectDraw + int m_isFullScreen; // ??? derive from m_pDirectDraw +}; + +struct DeviceDirect3DCreateData { + IDirect3D* m_pDirect3D; + IDirect3DDevice* m_pDirect3DDevice; +}; + +#else + +struct DeviceDirectDrawCreateData {}; + +#endif + +////////////////////////////////////////////////////////////////////////////// +// +// Result (return value type) + +enum Result { + Error = 0, + Success = 1 +}; + +inline int Succeeded(Result result) +{ + return (result == Success); +} + +////////////////////////////////////////////////////////////////////////////// +// +// Forward declarations + +class Renderer; +class Object; +class Device; +class View; +class Light; +class Camera; +class Group; +class Mesh; +class Texture; + +////////////////////////////////////////////////////////////////////////////// +// +// Object + +class Object { +public: + virtual ~Object() {} + + // returns pointer to implementation data + virtual void* ImplementationDataPtr() = 0; +}; + +////////////////////////////////////////////////////////////////////////////// +// +// Renderer + +// ??? for now until we figured out how an app should pass the Renderer around +Renderer* CreateRenderer(); + +class Renderer : public Object { +public: + virtual Device* CreateDevice(const DeviceDirectDrawCreateData&) = 0; + virtual Device* CreateDevice(const DeviceDirect3DCreateData&) = 0; + virtual View* CreateView( + const Device*, + const Camera*, + unsigned long x, + unsigned long y, + unsigned long width, + unsigned long height + ) = 0; + virtual Camera* CreateCamera() = 0; + virtual Light* CreateLight(LightType, double r, double g, double b) = 0; + virtual Group* CreateGroup(const Group* pParent = 0) = 0; + + // pTextureCoordinates is pointer to array of vertexCount elements + // (each element being two floats), or NULL + // pFaceData is faceCount tuples, each of format + // [vertex1index, ... vertexNindex], where N = vertexPerFaceCount + virtual Mesh* CreateMesh( + unsigned long vertexCount, + const float (*pVertices)[3], + const float (*pTextureCoordinates)[2], + unsigned long faceCount, + unsigned long vertexPerFaceCount, + unsigned long* pFaceData + ) = 0; + // pTextureCoordinates is pointer to array of vertexCount elements + // (each element being two floats), or NULL + // pFaceData is: + // [face1VertexCount face1Vertex1index, ... face1VertexMindex + // face2VertexCount face2Vertex1index, ... face2VertexNindex + // ... + // 0] + virtual Mesh* CreateMesh( + unsigned long vertexCount, + const float (*pVertices)[3], + const float (*pTextureCoordinates)[2], + unsigned long* pFaceData + ) = 0; + virtual Texture* CreateTexture( + int width, + int height, + int bitsPerTexel, + const void* pTexels, + int pTexelsArePersistent, + int paletteEntryCount, + const PaletteEntry* pEntries + ) = 0; + virtual Texture* CreateTexture() = 0; + + virtual Result SetTextureDefaultShadeCount(unsigned long) = 0; + virtual Result SetTextureDefaultColorCount(unsigned long) = 0; +}; + +////////////////////////////////////////////////////////////////////////////// +// +// Device + +class Device : public Object { +public: + virtual unsigned long GetWidth() = 0; + virtual unsigned long GetHeight() = 0; + virtual Result SetColorModel(ColorModel) = 0; + virtual Result SetShadingModel(ShadingModel) = 0; + virtual Result SetShadeCount(unsigned long) = 0; + virtual Result SetDither(int) = 0; + virtual Result Update() = 0; + + // ??? should this be handled by app ??? + // ??? this needs to be called when the window on which the device is ... + // is being activated + virtual void HandleActivate(int bActivate) = 0; + + // ??? this needs to be called when the window on which this device is based + // needs to be repainted + virtual void HandlePaint(void*) = 0; + +#ifdef _DEBUG + virtual unsigned long GetDrawnTriangleCount() = 0; +#endif +}; + +////////////////////////////////////////////////////////////////////////////// +// +// View + +class View : public Object { +public: + virtual Result Add(const Light*) = 0; + virtual Result Remove(const Light*) = 0; + + virtual Result SetCamera(const Camera*) = 0; + virtual Result SetProjection(ProjectionType) = 0; + virtual Result SetFrustrum(double frontClippingDistance, double backClippingDistance, double degrees) = 0; + virtual Result SetBackgroundColor(double r, double g, double b) = 0; + + virtual Result Clear() = 0; + virtual Result Render(const Group*) = 0; + // ??? needed for fine grain control when using DirectDraw/D3D ??? + virtual Result ForceUpdate(unsigned long x, unsigned long y, unsigned long width, unsigned long height) = 0; + + // ??? for now: used by Mesh Cost calculation + virtual Result TransformWorldToScreen(const double world[3], double screen[4]) = 0; + + // Pick(): + // x, y: + // view coordinates + // + // ppGroupsToPickFrom: + // array of (Group*) in any order + // Groups to pick from + // + // groupsToPickFromCount: + // size of ppGroupsToPickFrom + // + // rppPickedGroups: + // output parameter + // array of (Group*) representing a Group hierarchy + // top-down order (element 0 is root/scene) + // caller must deallocate array + // ref count of each element (Group*) has not been increased + // an element will be 0, if a corresponding Group was not found in ppGroupsToPickFrom + // + // rPickedGroupCount: + // output parameter + // size of rppPickedGroups + virtual Result Pick( + unsigned long x, + unsigned long y, + const Group** ppGroupsToPickFrom, + int groupsToPickFromCount, + const Group**& rppPickedGroups, + int& rPickedGroupCount + ) = 0; +}; + +////////////////////////////////////////////////////////////////////////////// +// +// Camera + +class Camera : public Object { +public: +#if 0 + virtual Result SetPosition(const double[3]) = 0; + virtual Result SetOrientation(const double direction[3], + const double up[3]) = 0; +#endif + virtual Result SetTransformation(const FloatMatrix4&) = 0; +}; + +////////////////////////////////////////////////////////////////////////////// +// +// Light + +class Light : public Object { +public: +#if 0 + virtual Result SetPosition(const double[3]) = 0; + virtual Result SetOrientation(const double direction[3], + const double up[3]) = 0; +#endif + virtual Result SetTransformation(const FloatMatrix4&) = 0; +}; + +////////////////////////////////////////////////////////////////////////////// +// +// Group + +class Group : public Object { +public: +#if 0 + virtual Result SetPosition(const double[3]) = 0; + virtual Result SetOrientation(const double direction[3], + const double up[3]) = 0; +#endif + virtual Result SetTransformation(const Matrix4&) = 0; + + // ??? not yet fully implemented + virtual Result SetColor(double r, double g, double b) = 0; + virtual Result SetTexture(const Texture*) = 0; + + virtual Result Add(const Group*) = 0; + virtual Result Add(const Mesh*) = 0; + + virtual Result Remove(const Group*) = 0; + virtual Result Remove(const Mesh*) = 0; + + virtual Result RemoveAll() = 0; + + // ??? for now: used by Mesh Cost calculation + virtual Result TransformLocalToWorld(const double local[3], double world[3]) = 0; +}; + +////////////////////////////////////////////////////////////////////////////// +// +// Mesh + +class Mesh : public Object { +public: + // ??? also on Group + virtual Result SetColor(double r, double g, double b) = 0; + virtual Result SetTexture(const Texture*) = 0; + virtual Result SetTextureMappingMode(TextureMappingMode) = 0; + virtual Result SetShadingModel(ShadingModel) = 0; + +#ifdef _DEBUG + virtual Result GetBoundingBox(float min[3], float max[3]) = 0; + virtual unsigned long GetFaceCount() = 0; + virtual unsigned long GetVertexCount() = 0; +#endif +}; + +////////////////////////////////////////////////////////////////////////////// +// +// Texture + +class Texture : public Object { +public: + virtual Result SetTexels( + int width, + int height, + int bitsPerTexel, + const void* pTexels, + int pTexelsArePersistent + ) = 0; + virtual Result SetPalette(int entryCount, const PaletteEntry* pEntries) = 0; +}; + +////////////////////////////////////////////////////////////////////////////// + +} // namespace Tgl + +#endif // TGL_H diff --git a/LEGO1/tglvector.h b/LEGO1/tglvector.h new file mode 100644 index 00000000..a617928d --- /dev/null +++ b/LEGO1/tglvector.h @@ -0,0 +1,277 @@ +#ifndef TGLVECTOR_H +#define TGLVECTOR_H + +#include "math.h" // ??? sin() in RotateAroundY() + +#include // offsetof() + +namespace Tgl +{ + +namespace Constant +{ +const float Pi = 3.14159265358979323846; +}; + +inline float DegreesToRadians(float degrees) +{ + return Constant::Pi * (degrees / 180.0); +} + +inline float RadiansToDegrees(float radians) +{ + return (radians / Constant::Pi) * 180.0; +} + +////////////////////////////////////////////////////////////////////////////// +// +// Array + +template +class Array { +public: + Array() {} + Array(const Array& rArray) { *this = rArray; } + ~Array() {} + + const T& operator[](int i) const { return m_elements[i]; }; + T& operator[](int i) { return m_elements[i]; }; + + Array& operator=(const Array&); + void operator+=(const Array&); + +protected: + T m_elements[N]; +}; + +////////////////////////////////////////////////////////////////////////////// +// +// Array implementation + +template +inline Array& Array::operator=(const Array& rArray) +{ + int i; + + for (i = 0; i < N; i++) { + m_elements[i] = rArray.m_elements[i]; + } + + return *this; +} + +template +inline void Array::operator+=(const Array& rArray) +{ + int i; + + for (i = 0; i < N; i++) { + m_elements[i] += rArray.m_elements[i]; + } +} + +////////////////////////////////////////////////////////////////////////////// +// +// FloatMatrix4 + +class FloatMatrix4 : public Array, 4> { +public: + FloatMatrix4() {} + FloatMatrix4(const FloatMatrix4& rMatrix) { *this = rMatrix; } + FloatMatrix4(const FloatMatrix4&, const FloatMatrix4&); + + void operator*=(const FloatMatrix4&); +}; + +////////////////////////////////////////////////////////////////////////////// +// +// FloatMatrix4 implementation + +inline FloatMatrix4::FloatMatrix4(const FloatMatrix4& rMatrix1, const FloatMatrix4& rMatrix2) +{ + for (int row = 0; row < 4; row++) { + for (int column = 0; column < 4; column++) { + float element = 0; + + for (int i = 0; i < 4; i++) { + element += rMatrix1[row][i] * rMatrix2[i][column]; + } + + m_elements[row][column] = element; + } + } +} + +inline void FloatMatrix4::operator*=(const FloatMatrix4& rMatrix) +{ + FloatMatrix4 temp(*this, rMatrix); + + // *this = FloatMatrix4(*this, rMatrix); + *this = temp; +} + +////////////////////////////////////////////////////////////////////////////// +// +// Transformation matrices + +class Translation : public FloatMatrix4 { +public: + Translation(const float[3]); + Translation(float x, float y, float z); + +protected: + void Init(float x, float y, float z); +}; + +class Scale : public FloatMatrix4 { +public: + Scale(const float[3]); + Scale(float x, float y, float z); + Scale(float); + +protected: + void Init(float x, float y, float z); +}; + +class RotationX : public FloatMatrix4 { +public: + RotationX(float radians); +}; + +class RotationY : public FloatMatrix4 { +public: + RotationY(float radians); +}; + +////////////////////////////////////////////////////////////////////////////// +// +// Transformation matrices implementation + +inline Translation::Translation(const float vector[3]) +{ + Init(vector[0], vector[1], vector[2]); +} + +inline Translation::Translation(float x, float y, float z) +{ + Init(x, y, z); +} + +inline void Translation::Init(float x, float y, float z) +{ + m_elements[0][0] = 1; + m_elements[0][1] = 0; + m_elements[0][2] = 0; + m_elements[0][3] = 0; + + m_elements[1][0] = 0; + m_elements[1][1] = 1; + m_elements[1][2] = 0; + m_elements[1][3] = 0; + + m_elements[2][0] = 0; + m_elements[2][1] = 0; + m_elements[2][2] = 1; + m_elements[2][3] = 0; + + m_elements[3][0] = x; + m_elements[3][1] = y; + m_elements[3][2] = z; + m_elements[3][3] = 1; +} + +inline Scale::Scale(const float vector[3]) +{ + Init(vector[0], vector[1], vector[2]); +} + +inline Scale::Scale(float x, float y, float z) +{ + Init(x, y, z); +} + +inline Scale::Scale(float scale) +{ + Init(scale, scale, scale); +} + +inline void Scale::Init(float x, float y, float z) +{ + m_elements[0][0] = x; + m_elements[0][1] = 0; + m_elements[0][2] = 0; + m_elements[0][3] = 0; + + m_elements[1][0] = 0; + m_elements[1][1] = y; + m_elements[1][2] = 0; + m_elements[1][3] = 0; + + m_elements[2][0] = 0; + m_elements[2][1] = 0; + m_elements[2][2] = z; + m_elements[2][3] = 0; + + m_elements[3][0] = 0; + m_elements[3][1] = 0; + m_elements[3][2] = 0; + m_elements[3][3] = 1; +} + +inline RotationX::RotationX(float radians) +{ + float cosRadians = cos(radians); + float sinRadians = sin(radians); + + m_elements[0][0] = 1; + m_elements[0][1] = 0; + m_elements[0][2] = 0; + m_elements[0][3] = 0; + + m_elements[1][0] = 0; + m_elements[1][1] = cosRadians; + m_elements[1][2] = -sinRadians; + m_elements[1][3] = 0; + + m_elements[2][0] = 0; + m_elements[2][1] = sinRadians; + m_elements[2][2] = cosRadians; + m_elements[2][3] = 0; + + m_elements[3][0] = 0; + m_elements[3][1] = 0; + m_elements[3][2] = 0; + m_elements[3][3] = 1; +} + +inline RotationY::RotationY(float radians) +{ + float cosRadians = cos(radians); + float sinRadians = sin(radians); + + m_elements[0][0] = cosRadians; + m_elements[0][1] = 0; + m_elements[0][2] = sinRadians; + m_elements[0][3] = 0; + + m_elements[1][0] = 0; + m_elements[1][1] = 1; + m_elements[1][2] = 0; + m_elements[1][3] = 0; + + m_elements[2][0] = -sinRadians; + m_elements[2][1] = 0; + m_elements[2][2] = cosRadians; + m_elements[2][3] = 0; + + m_elements[3][0] = 0; + m_elements[3][1] = 0; + m_elements[3][2] = 0; + m_elements[3][3] = 1; +} + +////////////////////////////////////////////////////////////////////////////// + +} // namespace Tgl + +#endif // TLGVECTOR_H diff --git a/LEGO1/viewlodlist.h b/LEGO1/viewlodlist.h new file mode 100644 index 00000000..b996d088 --- /dev/null +++ b/LEGO1/viewlodlist.h @@ -0,0 +1,116 @@ +#ifndef VIEWLODLIST_H +#define VIEWLODLIST_H + +#include "LODList.h" +#include "assert.h" +#include "compat.h" +#include "mxtypes.h" +#pragma warning(disable : 4786) + +class ViewLOD; +class ViewLODListManager; + +////////////////////////////////////////////////////////////////////////////// +// ViewLODList +// +// An ViewLODList is an LODList that is shared among instances of the "same ROI". +// +// ViewLODLists are managed (created and destroyed) by ViewLODListManager. +// + +class ViewLODList : public LODList { + friend ViewLODListManager; + +protected: + ViewLODList(size_t capacity); + ~ViewLODList(); + +public: + inline int AddRef(); + inline int Release(); + +#ifdef _DEBUG + void Dump(void (*pTracer)(const char*, ...)) const; +#endif + +private: + int m_refCount; + ViewLODListManager* m_owner; +}; + +////////////////////////////////////////////////////////////////////////////// +// + +// ??? for now, until we have symbol management +typedef const char* ROIName; +struct ROINameComparator { + MxBool operator()(const ROIName& rName1, const ROIName& rName2) const + { + return strcmp((const char*) rName1, (const char*) rName2) > 0; + } +}; + +////////////////////////////////////////////////////////////////////////////// +// +// ViewLODListManager +// +// ViewLODListManager manages creation and sharing of ViewLODLists. +// It stores ViewLODLists under a name, the name of the ROI where +// the ViewLODList belongs. + +class ViewLODListManager { + + typedef map ViewLODListMap; + +public: + ViewLODListManager(); + virtual ~ViewLODListManager(); + + // ??? should LODList be const + + // creates an LODList with room for lodCount LODs for a named ROI + // returned LODList has a refCount of 1, i.e. caller must call Release() + // when it no longer holds on to the list + ViewLODList* Create(const ROIName&, int lodCount); + + // returns an LODList for a named ROI + // returned LODList's refCount is increased, i.e. caller must call Release() + // when it no longer holds on to the list + ViewLODList* Lookup(const ROIName&) const; + void Destroy(ViewLODList* lodList); + +#ifdef _DEBUG + void Dump(void (*pTracer)(const char*, ...)) const; +#endif + +private: + ViewLODListMap m_map; +}; + +////////////////////////////////////////////////////////////////////////////// +// +// ViewLODList implementation + +inline ViewLODList::ViewLODList(size_t capacity) : LODList(capacity), m_refCount(0) +{ +} + +inline ViewLODList::~ViewLODList() +{ + assert(m_refCount == 0); +} + +inline int ViewLODList::AddRef() +{ + return ++m_refCount; +} + +inline int ViewLODList::Release() +{ + assert(m_refCount > 0); + if (!--m_refCount) + m_owner->Destroy(this); + return m_refCount; +} + +#endif // VIEWLODLIST_H diff --git a/LEGO1/viewroi.cpp b/LEGO1/viewroi.cpp new file mode 100644 index 00000000..7bfc4ab8 --- /dev/null +++ b/LEGO1/viewroi.cpp @@ -0,0 +1,45 @@ +#include "viewroi.h" + +#include "decomp.h" + +DECOMP_SIZE_ASSERT(ViewROI, 0xe0) + +// OFFSET: LEGO1 0x100a9eb0 +float ViewROI::IntrinsicImportance() const +{ + return .5; +} // for now + +// OFFSET: LEGO1 0x100a9ec0 +const Tgl::Group* ViewROI::GetGeometry() const +{ + return geometry; +} + +// OFFSET: LEGO1 0x100a9ed0 +Tgl::Group* ViewROI::GetGeometry() +{ + return geometry; +} + +// OFFSET: LEGO1 0x100a9ee0 +void ViewROI::UpdateWorldData(const MxMatrixData& parent2world) +{ + OrientableROI::UpdateWorldData(parent2world); + if (geometry) { + // Tgl::FloatMatrix4 tgl_mat; + Matrix4 mat; + SETMAT4(mat, m_local2world.GetMatrix()); + Tgl::Result result = geometry->SetTransformation(mat); + // assert(Tgl::Succeeded(result)); + } +} + +// OFFSET: LEGO1 0x100aa250 TEMPLATE +// ViewROI::`scalar deleting destructor' +inline ViewROI::~ViewROI() +{ + // SetLODList() will decrease refCount of LODList + SetLODList(0); + delete geometry; +} diff --git a/LEGO1/viewroi.h b/LEGO1/viewroi.h new file mode 100644 index 00000000..48b1510e --- /dev/null +++ b/LEGO1/viewroi.h @@ -0,0 +1,47 @@ +#ifndef VIEWROI_H +#define VIEWROI_H + +#include "orientableroi.h" +#include "tgl.h" +#include "viewlodlist.h" + +/* + ViewROI objects represent view objects, collections of view objects, + etc. Basically, anything which can be placed in a scene and manipilated + by the view manager is a ViewROI. +*/ +class ViewROI : public OrientableROI { +public: + inline ViewROI(Tgl::Renderer* pRenderer, ViewLODList* lodList) + { + SetLODList(lodList); + geometry = pRenderer->CreateGroup(); + } + inline ~ViewROI(); + inline void SetLODList(ViewLODList* lodList) + { + // ??? inherently type unsafe - kind of... because, now, ROI + // does not expose SetLODs() ... + // solution: create pure virtual LODListBase* ROI::GetLODList() + // and let derived ROI classes hold the LODList + + if (m_lods) { + reinterpret_cast(m_lods)->Release(); + } + + m_lods = lodList; + + if (m_lods) { + reinterpret_cast(m_lods)->AddRef(); + } + } + virtual float IntrinsicImportance() const; + virtual Tgl::Group* GetGeometry(); + virtual const Tgl::Group* GetGeometry() const; + +protected: + Tgl::Group* geometry; + void UpdateWorldData(const MxMatrixData& parent2world); +}; + +#endif // VIEWROI_H