}
-stlplus::ntree<OctreeNode>::iterator Octree::insert(stlplus::ntree<OctreeNode>::iterator node,
- EntityP entity)
+OctreeNodeP Octree::insert(EntityP entity, OctreeNodeP node)
{
ASSERT(node.valid() && "invalid node passed");
ASSERT(entity && "null entity passed");
- Plane::Halfspace halfspace;
int octantNum = -1;
+ Plane::Halfspace halfspace;
+
+ // TODO this method needs a lot of work
Plane xy = node->getAabb().getPlaneXY();
+
+
+ // make sure the entity is fully inside the volume
+ if (!(entity->getAabb().max[0] < node->getAabb().max[0] &&
+ entity->getAabb().min[0] > node->getAabb().min[0] &&
+ entity->getAabb().max[1] < node->getAabb().max[1] &&
+ entity->getAabb().min[1] > node->getAabb().min[1] &&
+ entity->getAabb().max[2] < node->getAabb().max[2] &&
+ entity->getAabb().min[2] > node->getAabb().min[2]))
+ {
+ goto done;
+ }
+
halfspace = xy.intersectsSphere(entity->getSphere());
if (halfspace == Plane::INTERSECT)
{
}
}
+done:
+
if (octantNum == -1)
{
node->objects.push_front(entity);
addChild(node, octantNum);
}
- stlplus::ntree<OctreeNode>::iterator child = tree_.child(node, octantNum);
+ OctreeNodeP child = tree_.child(node, octantNum);
ASSERT(child.valid() && "expected valid child node");
- return insert(child, entity);
+ return insert(entity, child);
}
}
-stlplus::ntree<OctreeNode>::iterator Octree::reinsert(EntityP entity,
- stlplus::ntree<OctreeNode>::iterator node)
+OctreeNodeP Octree::reinsert(EntityP entity, OctreeNodeP node)
{
ASSERT(entity && "null entity passed");
ASSERT(node.valid() && "invalid node passed");
}
-void Octree::addChild(stlplus::ntree<OctreeNode>::iterator node, int index)
+void Octree::addChild(OctreeNodeP node, int index)
{
ASSERT(node.valid() && "invalid node passed");
}
-void Octree::draw(stlplus::ntree<OctreeNode>::iterator node, Scalar alpha)
+void Octree::draw(Scalar alpha, OctreeNodeP node)
{
ASSERT(node.valid() && "invalid node passed");
for (unsigned i = 0; i < tree_.children(node); ++i)
{
- stlplus::ntree<OctreeNode>::iterator child = tree_.child(node, i);
+ OctreeNodeP child = tree_.child(node, i);
ASSERT(child.valid() && "expected valid child node");
- draw(child, alpha);
+ draw(alpha, child);
}
}
-void Octree::drawIfVisible(stlplus::ntree<OctreeNode>::iterator node,
- Scalar alpha, const Camera& cam)
+void Octree::drawIfVisible(Scalar alpha, const Camera& cam, OctreeNodeP node)
{
ASSERT(node.valid() && "invalid node passed");
{
for (unsigned i = 0; i < tree_.children(node); ++i)
{
- stlplus::ntree<OctreeNode>::iterator child = tree_.child(node, i);
+ OctreeNodeP child = tree_.child(node, i);
ASSERT(child.valid() && "expected valid child node");
- draw(child, alpha);
+ draw(alpha, child);
}
}
else // collision == Frustum::INTERSECT
{
for (unsigned i = 0; i < tree_.children(node); ++i)
{
- stlplus::ntree<OctreeNode>::iterator child = tree_.child(node, i);
+ OctreeNodeP child = tree_.child(node, i);
ASSERT(child.valid() && "expected valid child node");
- drawIfVisible(child, alpha, cam);
+ drawIfVisible(alpha, cam, child);
}
}
}
class Camera;
+struct OctreeNode;
+typedef stlplus::ntree<OctreeNode>::iterator OctreeNodeP;
+
+class Octree;
+typedef boost::shared_ptr<Octree> OctreeP;
+
+
struct OctreeNode : public Entity
{
std::list<EntityP> objects;
};
-class Octree;
-typedef boost::shared_ptr<Octree> OctreeP;
-
class Octree
{
- stlplus::ntree<OctreeNode>::iterator
- insert(stlplus::ntree<OctreeNode>::iterator node, EntityP entity);
+ OctreeNodeP insert(EntityP entity, OctreeNodeP node);
- void addChild(stlplus::ntree<OctreeNode>::iterator node, int index);
+ void addChild(OctreeNodeP node, int index);
- void draw(stlplus::ntree<OctreeNode>::iterator node, Scalar alpha);
- void drawIfVisible(stlplus::ntree<OctreeNode>::iterator node,
- Scalar alpha, const Camera& cam);
+ void draw(Scalar alpha, OctreeNodeP node);
+ void drawIfVisible(Scalar alpha, const Camera& cam, OctreeNodeP node);
stlplus::ntree<OctreeNode> tree_;
tree_.insert(rootNode);
}
- stlplus::ntree<OctreeNode>::iterator insert(EntityP entity)
+ OctreeNodeP insert(EntityP entity)
{
- return insert(tree_.root(), entity);
+ return insert(entity, tree_.root());
}
- stlplus::ntree<OctreeNode>::iterator reinsert(EntityP entity,
- stlplus::ntree<OctreeNode>::iterator node);
+ OctreeNodeP reinsert(EntityP entity, OctreeNodeP node);
void drawIfVisible(Scalar alpha, const Camera& cam)
{
- drawIfVisible(tree_.root(), alpha, cam);
+ drawIfVisible(alpha, cam, tree_.root());
}
void sort();