void Octant::Insert(SceneNode* obj) { const BoundingBox& box = obj->GetWorldBoundingBox(); // If drawable is outside the root octant bounds, insert to root bool insertHere; if (this == root_) insertHere = cullingBox_.IsInside(box) != Intersection::INSIDE || CheckFit(box); else insertHere = CheckFit(box); if (insertHere) { Octant* oldOctant = obj->GetOctant(); if (oldOctant != this) { // Add first, then remove, because drawable count going to zero deletes the octree branch in question Add(obj); if (oldOctant) oldOctant->Remove(obj, false); } } else { Vector3 boxCenter = box.Center(); unsigned x = boxCenter.x < center_.x ? 0 : 1; unsigned y = boxCenter.y < center_.y ? 0 : 2; unsigned z = boxCenter.z < center_.z ? 0 : 4; GetOrCreateChild(x + y + z)->Insert(obj); } }
void Octree::Remove(SceneNode* obj) { Octant* octant = obj->GetOctant(); if (octant) { octant->Remove(obj); allDrawablesSet_.erase(obj); allDrawables_.erase(std::remove(allDrawables_.begin(), allDrawables_.end(), obj), allDrawables_.end()); } }