From e76804079d0746884da9440e0d2ca4e3537c71d4 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 6 Apr 2020 01:10:59 -0400 Subject: [PATCH] COBBTree: Return std::array from GetTriangleVertexIndices() directly While the game binary uses an out pointer here, we can slightly modify it in this case in order to make it more difficult to misuse the call-sites by returning the data directly as a std::array. --- Runtime/Collision/CCollidableOBBTree.cpp | 15 +++++---------- Runtime/Collision/COBBTree.cpp | 20 ++++++++++++-------- Runtime/Collision/COBBTree.hpp | 6 +++++- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/Runtime/Collision/CCollidableOBBTree.cpp b/Runtime/Collision/CCollidableOBBTree.cpp index 0e7000f64..70e4da074 100644 --- a/Runtime/Collision/CCollidableOBBTree.cpp +++ b/Runtime/Collision/CCollidableOBBTree.cpp @@ -217,11 +217,9 @@ bool CCollidableOBBTree::SphereCollideWithLeafMoving(const COBBTree::CLeafData& } } - u16 vertIndices[3]; - x10_tree->GetTriangleVertexIndices(triIdx, vertIndices); - + const auto vertIndices = x10_tree->GetTriangleVertexIndices(triIdx); for (int k = 0; k < 3; ++k) { - u16 vertIdx = vertIndices[k]; + const u16 vertIdx = vertIndices[k]; if (testVert[k]) { if (CMetroidAreaCollider::g_DupPrimitiveCheckCount != CMetroidAreaCollider::g_DupVertexList[vertIdx]) { CMetroidAreaCollider::g_DupVertexList[vertIdx] = CMetroidAreaCollider::g_DupPrimitiveCheckCount; @@ -246,8 +244,7 @@ bool CCollidableOBBTree::SphereCollideWithLeafMoving(const COBBTree::CLeafData& CMetroidAreaCollider::g_DupEdgeList[edgeIndices[1]] = CMetroidAreaCollider::g_DupPrimitiveCheckCount; CMetroidAreaCollider::g_DupEdgeList[edgeIndices[2]] = CMetroidAreaCollider::g_DupPrimitiveCheckCount; - u16 vertIndices[3]; - x10_tree->GetTriangleVertexIndices(triIdx, vertIndices); + const auto vertIndices = x10_tree->GetTriangleVertexIndices(triIdx); CMetroidAreaCollider::g_DupVertexList[vertIndices[0]] = CMetroidAreaCollider::g_DupPrimitiveCheckCount; CMetroidAreaCollider::g_DupVertexList[vertIndices[1]] = CMetroidAreaCollider::g_DupPrimitiveCheckCount; CMetroidAreaCollider::g_DupVertexList[vertIndices[2]] = CMetroidAreaCollider::g_DupPrimitiveCheckCount; @@ -309,8 +306,7 @@ bool CCollidableOBBTree::AABoxCollideWithLeafMoving(const COBBTree::CLeafData& l if (CollisionUtil::TriBoxOverlap(center, extent, surf.GetVert(0), surf.GetVert(1), surf.GetVert(2))) { const_cast(*this).x1c_hits += 1; - u16 vertIndices[3]; - x10_tree->GetTriangleVertexIndices(triIdx, vertIndices); + const auto vertIndices = x10_tree->GetTriangleVertexIndices(triIdx); double d = dOut; if (CMetroidAreaCollider::MovingAABoxCollisionCheck_BoxVertexTri(surf, aabb, components.x6c4_vertIdxs, dir, d, @@ -363,8 +359,7 @@ bool CCollidableOBBTree::AABoxCollideWithLeafMoving(const COBBTree::CLeafData& l CMetroidAreaCollider::g_DupEdgeList[edgeIndices[1]] = CMetroidAreaCollider::g_DupPrimitiveCheckCount; CMetroidAreaCollider::g_DupEdgeList[edgeIndices[2]] = CMetroidAreaCollider::g_DupPrimitiveCheckCount; - u16 vertIndices[3]; - x10_tree->GetTriangleVertexIndices(triIdx, vertIndices); + const auto vertIndices = x10_tree->GetTriangleVertexIndices(triIdx); CMetroidAreaCollider::g_DupVertexList[vertIndices[0]] = CMetroidAreaCollider::g_DupPrimitiveCheckCount; CMetroidAreaCollider::g_DupVertexList[vertIndices[1]] = CMetroidAreaCollider::g_DupPrimitiveCheckCount; CMetroidAreaCollider::g_DupVertexList[vertIndices[2]] = CMetroidAreaCollider::g_DupPrimitiveCheckCount; diff --git a/Runtime/Collision/COBBTree.cpp b/Runtime/Collision/COBBTree.cpp index 90f68899b..2849ca96f 100644 --- a/Runtime/Collision/COBBTree.cpp +++ b/Runtime/Collision/COBBTree.cpp @@ -105,22 +105,26 @@ CCollisionSurface COBBTree::GetSurface(u16 idx) const { x18_indexData.x60_vertices[vert3], mat); } -void COBBTree::GetTriangleVertexIndices(u16 idx, u16 indicesOut[3]) const { +std::array COBBTree::GetTriangleVertexIndices(u16 idx) const { const auto surfIdx = size_t{idx} * 3; const CCollisionEdge& e0 = x18_indexData.x40_edges[x18_indexData.x50_surfaceIndices[surfIdx]]; const CCollisionEdge& e1 = x18_indexData.x40_edges[x18_indexData.x50_surfaceIndices[surfIdx + 1]]; - indicesOut[2] = (e1.GetVertIndex1() != e0.GetVertIndex1() && e1.GetVertIndex1() != e0.GetVertIndex2()) - ? e1.GetVertIndex1() - : e1.GetVertIndex2(); + std::array indices{}; + + indices[2] = (e1.GetVertIndex1() != e0.GetVertIndex1() && e1.GetVertIndex1() != e0.GetVertIndex2()) + ? e1.GetVertIndex1() + : e1.GetVertIndex2(); const u32 material = x18_indexData.x0_materials[x18_indexData.x30_surfaceMaterials[idx]]; if ((material & 0x2000000) != 0) { - indicesOut[0] = e0.GetVertIndex2(); - indicesOut[1] = e0.GetVertIndex1(); + indices[0] = e0.GetVertIndex2(); + indices[1] = e0.GetVertIndex1(); } else { - indicesOut[0] = e0.GetVertIndex1(); - indicesOut[1] = e0.GetVertIndex2(); + indices[0] = e0.GetVertIndex1(); + indices[1] = e0.GetVertIndex2(); } + + return indices; } CCollisionSurface COBBTree::GetTransformedSurface(u16 idx, const zeus::CTransform& xf) const { diff --git a/Runtime/Collision/COBBTree.hpp b/Runtime/Collision/COBBTree.hpp index cf5e5cf19..58ef0ec4d 100644 --- a/Runtime/Collision/COBBTree.hpp +++ b/Runtime/Collision/COBBTree.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -79,7 +80,10 @@ public: const zeus::CVector3f&); CCollisionSurface GetSurface(u16 idx) const; const u16* GetTriangleEdgeIndices(u16 idx) const { return &x18_indexData.x50_surfaceIndices[idx * 3]; } - void GetTriangleVertexIndices(u16 idx, u16 indicesOut[3]) const; + + // In the game binary, this used to use an out pointer for the indices after the index. + std::array GetTriangleVertexIndices(u16 idx) const; + const CCollisionEdge& GetEdge(int idx) const { return x18_indexData.x40_edges[idx]; } const zeus::CVector3f& GetVert(int idx) const { return x18_indexData.x60_vertices[idx]; } u32 GetVertMaterial(u16 idx) const { return x18_indexData.x0_materials[x18_indexData.x10_vertMaterials[idx]]; }