metaforce/Runtime/Graphics/CPVSVisOctree.cpp

95 lines
2.7 KiB
C++
Raw Normal View History

#include "Runtime/Graphics/CPVSVisOctree.hpp"
2022-02-16 05:21:24 +00:00
#include "IOStreams.hpp"
2016-07-27 23:06:57 +00:00
#include <array>
2021-04-10 08:42:06 +00:00
namespace metaforce {
2016-07-27 23:06:57 +00:00
2018-12-08 05:30:43 +00:00
CPVSVisOctree CPVSVisOctree::MakePVSVisOctree(const u8* data) {
CMemoryInStream r(data, 68);
const zeus::CAABox aabb = zeus::CAABox::ReadBoundingBoxBig(r);
const u32 numObjects = r.readUint32Big();
const u32 numLights = r.readUint32Big();
2018-12-08 05:30:43 +00:00
r.readUint32Big();
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)
, x2c_searchAabb(x0_aabb) {
x20_bufferFlag = false;
2016-07-27 23:06:57 +00:00
}
u32 CPVSVisOctree::GetNumChildren(u8 byte) const {
static constexpr std::array<u32, 8> numChildTable{0, 2, 2, 4, 2, 4, 4, 8};
return numChildTable[byte & 0x7];
}
2016-07-27 23:06:57 +00:00
2018-12-08 05:30:43 +00:00
u32 CPVSVisOctree::GetChildIndex(const u8*, const zeus::CVector3f&) const { return 0; }
2016-07-27 23:06:57 +00:00
2018-12-08 05:30:43 +00:00
s32 CPVSVisOctree::IterateSearch(u8 nodeData, const zeus::CVector3f& tp) const {
if (!(nodeData & 0x7))
return -1; // Leaf node
2017-02-19 09:27:01 +00:00
2018-12-08 05:30:43 +00:00
zeus::CVector3f newMin = x2c_searchAabb.center();
zeus::CVector3f newMax;
std::array<bool, 3> highFlags{};
2017-02-19 09:27:01 +00:00
2018-12-08 05:30:43 +00:00
if (tp.x() > newMin.x()) {
newMax.x() = x2c_searchAabb.max.x();
highFlags[0] = true;
} else {
newMax.x() = float(newMin.x());
newMin.x() = float(x2c_searchAabb.min.x());
highFlags[0] = false;
}
2017-02-19 09:27:01 +00:00
2018-12-08 05:30:43 +00:00
if (tp.y() > newMin.y()) {
newMax.y() = float(x2c_searchAabb.max.y());
highFlags[1] = true;
} else {
newMax.y() = float(newMin.y());
newMin.y() = float(x2c_searchAabb.min.y());
highFlags[1] = false;
}
2017-02-19 09:27:01 +00:00
2018-12-08 05:30:43 +00:00
if (tp.z() > newMin.z()) {
newMax.z() = float(x2c_searchAabb.max.z());
highFlags[2] = true;
} else {
newMax.z() = float(newMin.z());
newMin.z() = float(x2c_searchAabb.min.z());
highFlags[2] = false;
}
2017-02-19 09:27:01 +00:00
std::array<u8, 2> axisCounts{1, 1};
2018-12-08 05:30:43 +00:00
if (nodeData & 0x1)
axisCounts[0] = 2;
if (nodeData & 0x2)
axisCounts[1] = 2;
2017-02-19 09:27:01 +00:00
2018-12-08 05:30:43 +00:00
zeus::CAABox& newSearch = const_cast<zeus::CAABox&>(x2c_searchAabb);
if (nodeData & 0x1) {
newSearch.min.x() = float(newMin.x());
newSearch.max.x() = float(newMax.x());
}
if (nodeData & 0x2) {
newSearch.min.y() = float(newMin.y());
newSearch.max.y() = float(newMax.y());
}
if (nodeData & 0x4) {
newSearch.min.z() = float(newMin.z());
newSearch.max.z() = float(newMax.z());
}
2017-02-19 09:27:01 +00:00
2018-12-08 05:30:43 +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);
2017-02-19 09:27:01 +00:00
}
2021-04-10 08:42:06 +00:00
} // namespace metaforce