+ if (octantNum == -1)
+ {
+ node->node.objects.push_front(entity);
+ return node;
+ }
+ else
+ {
+ if (node->isLeaf())
+ {
+ addChildren(node);
+ }
+
+ Ptr child = node->getChild(octantNum);
+ if (child)
+ {
+ return add(child, entity);
+ }
+ else
+ {
+ std::cerr << "no child at index " << octantNum << std::endl;
+ return Ptr();
+ }
+ //return WeakPtr();
+ }
+ }
+
+ static void addChildren(Ptr node)
+ {
+ Aabb octant;
+
+ for (int i = 0; i < 8; ++i)
+ {
+ node->node.getAabb().getOctant(octant, i);
+ //OctreeNode octantNode(octant);
+
+ Ptr newChild = createNewNode(octant);
+ node->addChild(newChild);
+ }
+ }
+
+ void draw(Ptr node, Scalar alpha)
+ {
+ if (!node)
+ {
+ std::cerr << "null child :-(" << std::endl;
+ return;
+ }
+
+ node->node.draw(alpha);
+
+ if (!node->isLeaf())
+ {
+ Ptr firstChild = node->getFirstChild();
+ Ptr temp = firstChild;
+
+ if (!firstChild)
+ {
+ std::cerr << "node is not a leaf, but has no first child :-(" << std::endl;
+ return;
+ }
+
+ do
+ {
+ draw(temp, alpha);
+ temp = temp->getNextSibling();
+ }
+ while (temp && temp != firstChild);
+ }
+ }
+
+ void drawIfVisible(Ptr node, Scalar alpha, const Camera& cam)
+ {
+ //node.drawIfVisible(alpha, cam);
+
+ if (!node)
+ {
+ std::cerr << "null child :-(" << std::endl;
+ return;
+ }
+
+ Frustum::Collision collision =
+ cam.getFrustum().containsSphere(node->node.getSphere());
+ if (collision == Frustum::OUTSIDE) return;
+
+ collision = cam.getFrustum().containsAabb(node->node.getAabb());
+ if (collision == Frustum::OUTSIDE) return;
+
+
+ if (collision == Frustum::INSIDE)
+ {
+ node->node.draw(alpha);
+ }
+ else // collision == Frustum::INTERSECT
+ {
+ node->node.drawIfVisible(alpha, cam);
+ }
+
+ if (!node->isLeaf())
+ {
+ Ptr firstChild = node->getFirstChild();
+ Ptr temp = firstChild;
+
+ if (!firstChild)
+ {
+ std::cerr << "node is not a leaf, but has no first child :-(" << std::endl;
+ return;
+ }
+
+ if (collision == Frustum::INSIDE)
+ {
+ do
+ {
+ draw(temp, alpha);
+ temp = temp->getNextSibling();
+ }
+ while (temp && temp != firstChild);
+ }
+ else // collision == Frustum::INTERSECT
+ {
+ do
+ {
+ drawIfVisible(temp, alpha, cam);
+ temp = temp->getNextSibling();
+ }
+ while (temp && temp != firstChild);
+ }
+ }
+ }
+
+ void drawIfVisible(Scalar alpha, const Camera& cam)
+ {
+ drawIfVisible(getThis(), alpha, cam);
+ }