I want to use octree for object culling. My idea is as follows:
struct Node
{
bool Visible;
AABB Box;
bool IsLeaf;
Node* Children[8];
};
//called only once for objects at init time
Node** Octree::AddObject(AABB box, Node* parent)
{
if (parent->IsLeaf)
return &Node;
if (IntersectAABBAABB(parent->box, box))
{
for (int i=0; i < 8; +++i)
{
if (IntersectAABBAABB(parent->Children->box, box))
AddObject(box, parent);
}
}
}
//update is called everyframe
void Octree::Update()
{
if (IntersectAABBFrustum(parent->box, frustum)
{
parent->visible = true;
for (int i = 0; i < 8; ++i)
{
//move down the tree
.....
//check for box against frustum
..
//.if visible
//set visible bool to true
...
// check it's children and do samething
...
//else if false, set its all children's visible bool to false without checking
}
} else
{
parent->visible = false;
//set all children's visible bool to false without checking
}
}
//get node for this object
Object* house;
Node** houseNode = Octree.AddObject(house.box);
if (*houseNode->Visible)
//draw
I need help, do you see any problems? what should I do when object is inbetween Node AABBs ?
Please give me suggestions/improvements for octree or any other data structure object culling.
I do my terrain culling in hull shader.
Also I got another problem as boxes are not in same space, I think transforming them to world space would result into wrong assumptions about object being in a particular node because AABB in world space does not tightly surround the object.