Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,85 @@ BSP::BSP(BSPNode type, const std::vector<size_t>& quadblockIndexes, BSP* parent,
}
}

void BSP::PopulateBranch(PSX::BSPBranch& branch, std::vector<BSP*>& bspArray, size_t global_id)
{
g_id = global_id;
m_id = branch.id;
m_node = BSPNode::BRANCH;
if (branch.axis.x != 0) { m_axis = AxisSplit::X; }
else if (branch.axis.y != 0) { m_axis = AxisSplit::Y; }
else if (branch.axis.z != 0) { m_axis = AxisSplit::Z; }
else { m_axis = AxisSplit::NONE; }
m_flags = branch.flag;
if (branch.leftChild != BSPID::EMPTY)
{
uint16_t leftId = branch.leftChild & ~BSPID::LEAF;
if (leftId < bspArray.size())
{
m_left = bspArray[leftId];
m_left->SetParent(this);
}
}
if (branch.rightChild != BSPID::EMPTY)
{
uint16_t rightId = branch.rightChild & ~BSPID::LEAF;
if (rightId < bspArray.size())
{
m_right = bspArray[rightId];
m_right->SetParent(this);
}
}
m_bbox = BoundingBox();
m_bbox.min = ConvertPSXVec3(branch.bbox.min, FP_ONE_GEO);
m_bbox.max = ConvertPSXVec3(branch.bbox.max, FP_ONE_GEO);
m_quadblockIndexes = std::vector<size_t>(); // Need to be set later
}

void BSP::PopulateLeaf(PSX::BSPLeaf& leaf, std::vector<BSP*>& bspArray, const std::vector<Quadblock>& quadblocks, uint32_t offQuadblocks, size_t global_id)
{
g_id = global_id;
m_id = leaf.id;
m_node = BSPNode::LEAF;
m_axis = AxisSplit::NONE;
m_flags = leaf.flag;
m_left = nullptr;
m_right = nullptr;
m_bbox = BoundingBox();
m_bbox.min = ConvertPSXVec3(leaf.bbox.min, FP_ONE_GEO);
m_bbox.max = ConvertPSXVec3(leaf.bbox.max, FP_ONE_GEO);
m_quadblockIndexes = std::vector<size_t>();
for (size_t i = 0; i < leaf.numQuads; i++)
{
uint32_t relative_offset = leaf.offQuads - offQuadblocks;
m_quadblockIndexes.push_back(i + relative_offset/sizeof(PSX::Quadblock));
}
for (size_t i : m_quadblockIndexes)
{
if (i < quadblocks.size()) { quadblocks[i].SetBSPID(m_id); }
}
}


void BSP::PopulateBranchQuadIndexes()
{
if (m_node == BSPNode::BRANCH)
{
m_quadblockIndexes.clear();
if (m_left != nullptr)
{
m_left->PopulateBranchQuadIndexes();
const std::vector<size_t>& leftIndexes = m_left->GetQuadblockIndexes();
m_quadblockIndexes.insert(m_quadblockIndexes.end(), leftIndexes.begin(), leftIndexes.end());
}
if (m_right != nullptr)
{
m_right->PopulateBranchQuadIndexes();
const std::vector<size_t>& rightIndexes = m_right->GetQuadblockIndexes();
m_quadblockIndexes.insert(m_quadblockIndexes.end(), rightIndexes.begin(), rightIndexes.end());
}
}
}

size_t BSP::GetId() const
{
return m_id;
Expand Down Expand Up @@ -145,6 +224,11 @@ void BSP::SetQuadblockIndexes(const std::vector<size_t>& quadblockIndexes)
m_quadblockIndexes = quadblockIndexes;
}

void BSP::SetParent(BSP* parent)
{
m_parent = parent;
}

void BSP::Clear()
{
std::vector<BSP*> vBSP = {this};
Expand Down
4 changes: 4 additions & 0 deletions src/bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class BSP
public:
BSP();
BSP(BSPNode type, const std::vector<size_t>& quadblockIndexes, BSP* parent, const std::vector<Quadblock>& quadblocks);
void PopulateLeaf(PSX::BSPLeaf& leaf, std::vector<BSP*>& bspArray, const std::vector<Quadblock>& quadblocks, uint32_t offQuadblocks, size_t global_id);
void PopulateBranch(PSX::BSPBranch& branch, std::vector<BSP*>& bspArray, size_t global_id);
void PopulateBranchQuadIndexes();
size_t GetId() const;
bool IsEmpty() const;
bool IsValid() const;
Expand All @@ -53,6 +56,7 @@ class BSP
const std::vector<const BSP*> GetTree() const;
std::vector<const BSP*> GetLeaves() const;
void SetQuadblockIndexes(const std::vector<size_t>& quadblockIndexes);
void SetParent(BSP* parent);
void Clear();
void Generate(const std::vector<Quadblock>& quadblocks, const size_t maxQuadsPerLeaf, const float maxAxisLength);
std::vector<uint8_t> Serialize(size_t offQuads) const;
Expand Down
Loading