metaforce/Runtime/Collision/COBBTree.cpp

212 lines
6.3 KiB
C++
Raw Normal View History

2016-04-26 10:40:56 +00:00
#include "COBBTree.hpp"
2017-07-03 03:34:19 +00:00
#include "CCollidableOBBTreeGroup.hpp"
2016-04-26 10:40:56 +00:00
namespace urde
{
/* This is exactly what retro did >.< */
u32 verify_deaf_babe(CInputStream& in)
{
return in.readUint32Big();
}
/* This is exactly what retro did >.< */
u32 verify_version(CInputStream& in)
{
return in.readUint32Big();
}
2016-06-24 20:09:38 +00:00
COBBTree::COBBTree(const SIndexData& indexData, const CNode* root)
: x18_indexData(indexData),
x88_root((CNode*)root)
2016-04-26 10:40:56 +00:00
{
}
COBBTree::COBBTree(CInputStream& in)
: x0_magic(verify_deaf_babe(in)),
x4_version(verify_version(in)),
x8_memsize(in.readUint32()),
2016-04-27 00:26:02 +00:00
x18_indexData(in),
x88_root(new CNode(in))
2016-04-26 10:40:56 +00:00
{
}
2017-07-03 03:34:19 +00:00
std::unique_ptr<CCollidableOBBTreeGroupContainer>
COBBTree::BuildOrientedBoundingBoxTree(const zeus::CVector3f& a, const zeus::CVector3f& b)
{
return std::make_unique<CCollidableOBBTreeGroupContainer>(a, b);
}
2016-07-28 06:37:50 +00:00
CCollisionSurface COBBTree::GetSurface(u16 idx) const
{
2018-06-29 20:21:36 +00:00
int surfIdx = idx * 3;
CCollisionEdge e0 = x18_indexData.x40_edges[x18_indexData.x50_surfaceIndices[surfIdx]];
CCollisionEdge e1 = x18_indexData.x40_edges[x18_indexData.x50_surfaceIndices[surfIdx + 1]];
u16 vert1 = e0.GetVertIndex1();
u16 vert2 = e0.GetVertIndex2();
u16 vert3 = e1.GetVertIndex1();
2016-07-28 06:37:50 +00:00
2018-06-29 20:21:36 +00:00
if (vert3 == vert1 || vert3 == vert2)
vert3 = e1.GetVertIndex2();
2016-07-28 06:37:50 +00:00
u32 mat = x18_indexData.x0_materials[x18_indexData.x30_surfaceMaterials[idx]];
if ((mat & 0x2000000) != 0)
{
return CCollisionSurface(x18_indexData.x60_vertices[vert2], x18_indexData.x60_vertices[vert1],
x18_indexData.x60_vertices[vert3], mat);
}
return CCollisionSurface(x18_indexData.x60_vertices[vert1], x18_indexData.x60_vertices[vert2],
x18_indexData.x60_vertices[vert3], mat);
}
2017-07-03 03:34:19 +00:00
void COBBTree::GetTriangleVertexIndices(u16 idx, u16 indicesOut[3]) const
{
const CCollisionEdge& e0 = x18_indexData.x40_edges[x18_indexData.x50_surfaceIndices[idx*3]];
const CCollisionEdge& e1 = x18_indexData.x40_edges[x18_indexData.x50_surfaceIndices[idx*3+1]];
indicesOut[2] =
(e1.GetVertIndex1() != e0.GetVertIndex1() && e1.GetVertIndex1() != e0.GetVertIndex2()) ?
e1.GetVertIndex1() : e1.GetVertIndex2();
u32 material = x18_indexData.x0_materials[x18_indexData.x30_surfaceMaterials[idx]];
if (material & 0x2000000)
{
indicesOut[0] = e0.GetVertIndex2();
indicesOut[1] = e0.GetVertIndex1();
}
else
{
indicesOut[0] = e0.GetVertIndex1();
indicesOut[1] = e0.GetVertIndex2();
}
}
CCollisionSurface COBBTree::GetTransformedSurface(u16 idx, const zeus::CTransform& xf) const
{
2018-06-28 06:47:34 +00:00
int surfIdx = idx * 3;
CCollisionEdge e0 = x18_indexData.x40_edges[x18_indexData.x50_surfaceIndices[surfIdx]];
CCollisionEdge e1 = x18_indexData.x40_edges[x18_indexData.x50_surfaceIndices[surfIdx + 1]];
u16 vert1 = e0.GetVertIndex1();
u16 vert2 = e0.GetVertIndex2();
u16 vert3 = e1.GetVertIndex1();
2017-07-03 03:34:19 +00:00
2018-06-28 06:47:34 +00:00
if (vert3 == vert1 || vert3 == vert2)
vert3 = e1.GetVertIndex2();
2017-07-03 03:34:19 +00:00
u32 mat = x18_indexData.x0_materials[x18_indexData.x30_surfaceMaterials[idx]];
if ((mat & 0x2000000) != 0)
{
return CCollisionSurface(xf * x18_indexData.x60_vertices[vert2], xf * x18_indexData.x60_vertices[vert1],
xf * x18_indexData.x60_vertices[vert3], mat);
}
return CCollisionSurface(xf * x18_indexData.x60_vertices[vert1], xf * x18_indexData.x60_vertices[vert2],
xf * x18_indexData.x60_vertices[vert3], mat);
}
2016-04-27 02:41:00 +00:00
zeus::CAABox COBBTree::CalculateLocalAABox() const
{
return CalculateAABox(zeus::CTransform::Identity());
}
zeus::CAABox COBBTree::CalculateAABox(const zeus::CTransform& xf) const
{
if (x88_root)
return x88_root->GetOBB().calculateAABox(xf);
return zeus::CAABox::skInvertedBox;
}
2016-04-26 10:40:56 +00:00
COBBTree::SIndexData::SIndexData(CInputStream& in)
{
u32 count = in.readUint32Big();
2016-07-28 06:37:50 +00:00
x0_materials.reserve(count);
2016-04-26 10:40:56 +00:00
for (u32 i = 0 ; i < count ; i++)
2016-07-28 06:37:50 +00:00
x0_materials.push_back(in.readUint32Big());
2016-04-26 10:40:56 +00:00
count = in.readUint32Big();
for (u32 i = 0 ; i < count ; i++)
2016-07-28 06:37:50 +00:00
x10_vertMaterials.push_back(in.readUByte());
2016-04-26 10:40:56 +00:00
count = in.readUint32Big();
for (u32 i = 0 ; i < count ; i++)
2016-07-28 06:37:50 +00:00
x20_edgeMaterials.push_back(in.readUByte());
2016-04-26 10:40:56 +00:00
count = in.readUint32Big();
for (u32 i = 0 ; i < count ; i++)
2016-07-28 06:37:50 +00:00
x30_surfaceMaterials.push_back(in.readUByte());
2016-04-26 10:40:56 +00:00
count = in.readUint32Big();
for (u32 i = 0 ; i < count ; i++)
2016-07-28 06:37:50 +00:00
x40_edges.push_back(in);
2016-04-26 10:40:56 +00:00
count = in.readUint32Big();
for (u32 i = 0 ; i < count ; i++)
2016-07-28 06:37:50 +00:00
x50_surfaceIndices.push_back(in.readUint16Big());
2016-04-26 10:40:56 +00:00
count = in.readUint32Big();
for (u32 i = 0 ; i < count ; i++)
2016-07-28 06:37:50 +00:00
x60_vertices.push_back(zeus::CVector3f::ReadBig(in));
2016-04-26 10:40:56 +00:00
}
2016-04-27 00:26:02 +00:00
COBBTree::CNode::CNode(const zeus::CTransform& xf, const zeus::CVector3f& point,
const COBBTree::CNode* left, const COBBTree::CNode* right,
const COBBTree::CLeafData* leaf)
: x0_obb(xf, point),
x3c_isLeaf(leaf != nullptr)
{
x40_left.reset((CNode*)left);
x44_right.reset((CNode*)right);
x48_leaf.reset((CLeafData*)leaf);
}
2016-04-26 10:40:56 +00:00
COBBTree::CNode::CNode(CInputStream& in)
{
x0_obb = zeus::COBBox::ReadBig(in);
2016-04-27 00:26:02 +00:00
x3c_isLeaf = in.readBool();
if (x3c_isLeaf)
x48_leaf.reset(new CLeafData(in));
else
{
x40_left.reset(new CNode(in));
x44_right.reset(new CNode(in));
}
}
2016-06-24 20:09:38 +00:00
size_t COBBTree::CNode::GetMemoryUsage() const
{
size_t ret = 0;
if (x3c_isLeaf)
ret = x48_leaf->GetMemoryUsage() + /*sizeof(CNode)*/ 80;
2016-06-24 20:09:38 +00:00
else
{
if (x40_left)
ret = x40_left->GetMemoryUsage() + /*sizeof(CNode)*/ 80;
2016-06-24 20:09:38 +00:00
if (x44_right)
ret += x44_right->GetMemoryUsage();
}
return (ret + 3) & ~3;
2016-06-24 20:09:38 +00:00
}
2016-04-27 00:26:02 +00:00
COBBTree::CLeafData::CLeafData(const std::vector<u16>& surface)
: x0_surface(surface)
{
}
const std::vector<u16>& COBBTree::CLeafData::GetSurfaceVector() const
{
return x0_surface;
}
2016-06-24 20:09:38 +00:00
size_t COBBTree::CLeafData::GetMemoryUsage() const
{
size_t ret = (x0_surface.size() * 2) + /*sizeof(CLeafData)*/ 16;
return (ret + 3) & ~3;
2016-06-24 20:09:38 +00:00
}
2016-04-27 00:26:02 +00:00
COBBTree::CLeafData::CLeafData(CInputStream& in)
{
u32 edgeCount = in.readUint32Big();
for (u32 i = 0 ; i < edgeCount ; i++)
x0_surface.push_back(in.readUint16Big());
2016-04-26 10:40:56 +00:00
}
}