From a31407c69cb299375aa5c5fff6718a7258c1fd23 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Sun, 9 Oct 2022 13:54:18 -0700 Subject: [PATCH] Match CAABox::GetTransformedAABox, minor fixes start CMetroidModelInstance --- asm/WorldFormat/CMetroidModelInstance.s | 12 +++++----- include/Kyoto/Basics/CBasics.hpp | 6 +++++ include/Kyoto/Math/CTransform4f.hpp | 4 +++- include/WorldFormat/CMetroidModelInstance.hpp | 18 +++++++++++++++ src/Kyoto/Math/CAABox.cpp | 15 +++++++------ src/WorldFormat/CMetroidModelInstance.cpp | 22 +++++++++++++++++++ 6 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 include/WorldFormat/CMetroidModelInstance.hpp create mode 100644 src/WorldFormat/CMetroidModelInstance.cpp diff --git a/asm/WorldFormat/CMetroidModelInstance.s b/asm/WorldFormat/CMetroidModelInstance.s index da5ad728..6c5ef374 100644 --- a/asm/WorldFormat/CMetroidModelInstance.s +++ b/asm/WorldFormat/CMetroidModelInstance.s @@ -20,13 +20,13 @@ /* 802A27AC 0029F70C 80 04 00 00 */ lwz r0, 0(r4) /* 802A27B0 0029F710 90 03 00 00 */ stw r0, 0(r3) /* 802A27B4 0029F714 38 77 00 04 */ addi r3, r23, 4 -/* 802A27B8 0029F718 48 00 01 65 */ bl nullsub_30 +/* 802A27B8 0029F718 48 00 01 65 */ bl TransformFromData__FPCv /* 802A27BC 0029F71C 7C 64 1B 78 */ mr r4, r3 /* 802A27C0 0029F720 38 79 00 04 */ addi r3, r25, 4 /* 802A27C4 0029F724 48 07 03 B1 */ bl __ct__12CTransform4fFRC12CTransform4f /* 802A27C8 0029F728 38 61 00 08 */ addi r3, r1, 8 /* 802A27CC 0029F72C 38 97 00 34 */ addi r4, r23, 0x34 -/* 802A27D0 0029F730 48 00 00 F9 */ bl sub_802a28c8 +/* 802A27D0 0029F730 48 00 00 F9 */ bl BoundingBoxFromData__FPCv /* 802A27D4 0029F734 C0 01 00 08 */ lfs f0, 8(r1) /* 802A27D8 0029F738 D0 19 00 34 */ stfs f0, 0x34(r25) /* 802A27DC 0029F73C C0 01 00 0C */ lfs f0, 0xc(r1) @@ -95,8 +95,8 @@ lbl_802A289C: /* 802A28C0 0029F820 38 21 00 50 */ addi r1, r1, 0x50 /* 802A28C4 0029F824 4E 80 00 20 */ blr -.global sub_802a28c8 -sub_802a28c8: +.global BoundingBoxFromData__FPCv +BoundingBoxFromData__FPCv: /* 802A28C8 0029F828 94 21 FF E0 */ stwu r1, -0x20(r1) /* 802A28CC 0029F82C C0 24 00 00 */ lfs f1, 0(r4) /* 802A28D0 0029F830 C0 A4 00 04 */ lfs f5, 4(r4) @@ -119,7 +119,8 @@ sub_802a28c8: /* 802A2914 0029F874 38 21 00 20 */ addi r1, r1, 0x20 /* 802A2918 0029F878 4E 80 00 20 */ blr -nullsub_30: +.global TransformFromData__FPCv +TransformFromData__FPCv: /* 802A291C 0029F87C 4E 80 00 20 */ blr .section .rodata @@ -129,4 +130,3 @@ lbl_803D6018: # ROM: 0x3D3018 .asciz "??(??)" .balign 4 - diff --git a/include/Kyoto/Basics/CBasics.hpp b/include/Kyoto/Basics/CBasics.hpp index 8b6740ff..e8c8f581 100644 --- a/include/Kyoto/Basics/CBasics.hpp +++ b/include/Kyoto/Basics/CBasics.hpp @@ -6,6 +6,12 @@ namespace CBasics { void Init(); char* Stringize(const char* fmt, ...); +inline void SwapBytes(uchar* v) {} +inline float SwapBytes(float f) { + u8* tmp = reinterpret_cast(&f); + SwapBytes(tmp); + return *reinterpret_cast(tmp); + } }; // namespace CBasics #endif // _CBASICS diff --git a/include/Kyoto/Math/CTransform4f.hpp b/include/Kyoto/Math/CTransform4f.hpp index b6342c0c..eceb16ce 100644 --- a/include/Kyoto/Math/CTransform4f.hpp +++ b/include/Kyoto/Math/CTransform4f.hpp @@ -54,7 +54,9 @@ public: inline const CVector3f& GetRow(EDimX dim) const { return m0; } inline const CVector3f& GetRow(EDimY dim) const { return m1; } inline const CVector3f& GetRow(EDimZ dim) const { return m2; } - inline const CVector3f& GetRow(int i) const { return *(&m0 + i); } + inline const CVector3f& GetRow(int i) const { + return *(reinterpret_cast< const CVector3f* >(reinterpret_cast< const float* >(&m0) + i * 4)); + } // GetUp__12CTransform4fCFv static CTransform4f LookAt(const CVector3f& pos, const CVector3f& lookPos, const CVector3f& up = CVector3f::Up()); diff --git a/include/WorldFormat/CMetroidModelInstance.hpp b/include/WorldFormat/CMetroidModelInstance.hpp new file mode 100644 index 00000000..8ca3d6ef --- /dev/null +++ b/include/WorldFormat/CMetroidModelInstance.hpp @@ -0,0 +1,18 @@ +#ifndef _CMETROIDMODELINSTANCE +#define _CMETROIDMODELINSTANCE + +#include "Kyoto/Math/CTransform4f.hpp" +#include "Kyoto/Math/CAABox.hpp" + +#include "rstl/vector.hpp" + +class CMetroidModelInstance { +public: + CMetroidModelInstance(const void* header, const void* firstGeom, + const void* positions, const void* normals, + const void* colors, const void* uvs, + const void* packedUvs, + const rstl::vector< void* >& surfaces); +}; + +#endif // _CMETROIDMODELINSTANCE diff --git a/src/Kyoto/Math/CAABox.cpp b/src/Kyoto/Math/CAABox.cpp index c77f61b4..a0207adf 100644 --- a/src/Kyoto/Math/CAABox.cpp +++ b/src/Kyoto/Math/CAABox.cpp @@ -154,7 +154,6 @@ bool CAABox::InsidePlane(const CPlane& plane) const { return !(CVector3f::Dot(plane.GetNormal(), vec) >= plane.GetConstant()); } -// TODO non-matching CAABox CAABox::GetTransformedAABox(const CTransform4f& xf) const { if (&xf == &CTransform4f::Identity()) { return *this; @@ -163,16 +162,18 @@ CAABox CAABox::GetTransformedAABox(const CTransform4f& xf) const { CVector3f newMin = xf.GetTranslation(); CVector3f newMax = xf.GetTranslation(); for (int x = 0; x < 3; ++x) { + const CVector3f& row = xf.GetRow(x); + const CVector3f& minPoint = GetMinPoint(); + const CVector3f& maxPoint = GetMaxPoint(); for (int y = 0; y < 3; ++y) { - float mul = xf.GetRow(x)[y]; - float minMul = mul * GetMinPoint()[y]; - float maxMul = mul * GetMaxPoint()[y]; + float minMul = row[y] * minPoint[y]; + float maxMul = row[y] * maxPoint[y]; if (minMul < maxMul) { - newMin[x] += maxMul; - newMax[x] += minMul; - } else { newMin[x] += minMul; newMax[x] += maxMul; + } else { + newMin[x] += maxMul; + newMax[x] += minMul; } } } diff --git a/src/WorldFormat/CMetroidModelInstance.cpp b/src/WorldFormat/CMetroidModelInstance.cpp new file mode 100644 index 00000000..8a70699f --- /dev/null +++ b/src/WorldFormat/CMetroidModelInstance.cpp @@ -0,0 +1,22 @@ +#include "WorldFormat/CMetroidModelInstance.hpp" + +#include "Kyoto/Basics/CBasics.hpp" + +const CTransform4f& TransformFromData(const void* ptr) { + return *static_cast< const CTransform4f* >(ptr); +} + +CAABox BoundingBoxFromData(const void* ptr) { + const CAABox* tmp = static_cast< const CAABox* >(ptr); + float minX = CBasics::SwapBytes(tmp->GetMinPoint().GetX()); + float minY = CBasics::SwapBytes(tmp->GetMinPoint().GetY()); + float minZ = CBasics::SwapBytes(tmp->GetMinPoint().GetZ()); + float maxX = CBasics::SwapBytes(tmp->GetMaxPoint().GetX()); + float maxY = CBasics::SwapBytes(tmp->GetMaxPoint().GetY()); + float maxZ = CBasics::SwapBytes(tmp->GetMaxPoint().GetZ()); + return CAABox(minX, minY, minZ, maxX, maxY, maxZ); +} + +CMetroidModelInstance::CMetroidModelInstance(const void*, const void*, const void*, const void*, + const void*, const void*, const void*, + const rstl::vector< void* >&) {}