metaforce/Runtime/Graphics/CPVSVisOctree.cpp

114 lines
2.7 KiB
C++
Raw Normal View History

2016-07-27 23:06:57 +00:00
#include "CPVSVisOctree.hpp"
namespace urde
{
2017-02-19 09:27:01 +00:00
CPVSVisOctree CPVSVisOctree::MakePVSVisOctree(const u8* data)
2016-07-27 23:06:57 +00:00
{
2017-02-19 09:27:01 +00:00
CMemoryInStream r(data, 68);
zeus::CAABox aabb = aabb.ReadBoundingBoxBig(r);
2018-06-28 06:47:34 +00:00
u32 numObjects = r.readUint32Big();
u32 numLights = r.readUint32Big();
2017-02-19 09:27:01 +00:00
r.readUint32Big();
2018-06-28 06:47:34 +00:00
return CPVSVisOctree(aabb, numObjects, numLights, data + r.position());
2016-07-27 23:06:57 +00:00
}
2018-06-28 06:47:34 +00:00
CPVSVisOctree::CPVSVisOctree(const zeus::CAABox& aabb, u32 numObjects, u32 numLights, const u8* c)
: x0_aabb(aabb), x18_numObjects(numObjects), x1c_numLights(numLights), x20_bufferFlag(c != nullptr), x24_octreeData(c)
2016-07-27 23:06:57 +00:00
{
2017-02-19 09:27:01 +00:00
x2c_searchAabb = x0_aabb;
x20_bufferFlag = 0;
2016-07-27 23:06:57 +00:00
}
2017-02-19 09:27:01 +00:00
static const u32 NumChildTable[] =
2016-07-27 23:06:57 +00:00
{
2017-02-19 09:27:01 +00:00
0, 2, 2, 4, 2, 4, 4, 8
};
u32 CPVSVisOctree::GetNumChildren(u8 byte) const
{
return NumChildTable[byte & 0x7];
2016-07-27 23:06:57 +00:00
}
2017-02-19 09:27:01 +00:00
u32 CPVSVisOctree::GetChildIndex(const u8*, const zeus::CVector3f&) const
2016-07-27 23:06:57 +00:00
{
return 0;
}
2017-02-19 09:27:01 +00:00
s32 CPVSVisOctree::IterateSearch(u8 nodeData, const zeus::CVector3f& tp) const
{
if (!(nodeData & 0x7))
return -1; // Leaf node
zeus::CVector3f newMin = x2c_searchAabb.center();
zeus::CVector3f newMax;
bool highFlags[3];
2018-12-08 01:49:15 +00:00
if (tp.x() > newMin.x())
2017-02-19 09:27:01 +00:00
{
2018-12-08 01:49:15 +00:00
newMax.x() = x2c_searchAabb.max.x();
2017-02-19 09:27:01 +00:00
highFlags[0] = true;
}
else
{
2018-12-08 01:49:15 +00:00
newMax.x() = float(newMin.x());
newMin.x() = float(x2c_searchAabb.min.x());
2017-02-19 09:27:01 +00:00
highFlags[0] = false;
}
2018-12-08 01:49:15 +00:00
if (tp.y() > newMin.y())
2017-02-19 09:27:01 +00:00
{
2018-12-08 01:49:15 +00:00
newMax.y() = float(x2c_searchAabb.max.y());
2017-02-19 09:27:01 +00:00
highFlags[1] = true;
}
else
{
2018-12-08 01:49:15 +00:00
newMax.y() = float(newMin.y());
newMin.y() = float(x2c_searchAabb.min.y());
2017-02-19 09:27:01 +00:00
highFlags[1] = false;
}
2018-12-08 01:49:15 +00:00
if (tp.z() > newMin.z())
2017-02-19 09:27:01 +00:00
{
2018-12-08 01:49:15 +00:00
newMax.z() = float(x2c_searchAabb.max.z());
2017-02-19 09:27:01 +00:00
highFlags[2] = true;
}
else
{
2018-12-08 01:49:15 +00:00
newMax.z() = float(newMin.z());
newMin.z() = float(x2c_searchAabb.min.z());
2017-02-19 09:27:01 +00:00
highFlags[2] = false;
}
u8 axisCounts[2] = {1, 1};
if (nodeData & 0x1)
axisCounts[0] = 2;
if (nodeData & 0x2)
axisCounts[1] = 2;
zeus::CAABox& newSearch = const_cast<zeus::CAABox&>(x2c_searchAabb);
if (nodeData & 0x1)
{
2018-12-08 01:49:15 +00:00
newSearch.min.x() = float(newMin.x());
newSearch.max.x() = float(newMax.x());
2017-02-19 09:27:01 +00:00
}
if (nodeData & 0x2)
{
2018-12-08 01:49:15 +00:00
newSearch.min.y() = float(newMin.y());
newSearch.max.y() = float(newMax.y());
2017-02-19 09:27:01 +00:00
}
if (nodeData & 0x4)
{
2018-12-08 01:49:15 +00:00
newSearch.min.z() = float(newMin.z());
newSearch.max.z() = float(newMax.z());
2017-02-19 09:27:01 +00:00
}
// Branch node - calculate next relative pointer
return
highFlags[0] * bool(nodeData & 0x1) +
highFlags[1] * axisCounts[0] * bool(nodeData & 0x2) +
highFlags[2] * axisCounts[0] * axisCounts[1] * bool(nodeData & 0x4);
}
2016-07-27 23:06:57 +00:00
}