X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FOctree.hh;h=d896844f0379a4ab7ca1cebe32d52cb5a719a775;hp=78d3aa875706cd61f4558740c7b90a8cf4f82cd3;hb=bfa6212d09d8735d8fd5e2638188e4a99f21ada4;hpb=72d4af22710317acffab861421c4364b1780b6fe diff --git a/src/Moof/Octree.hh b/src/Moof/Octree.hh index 78d3aa8..d896844 100644 --- a/src/Moof/Octree.hh +++ b/src/Moof/Octree.hh @@ -29,21 +29,26 @@ #ifndef _MOOF_OCTREE_HH_ #define _MOOF_OCTREE_HH_ +#include #include #include +#include + #include #include #include #include #include -#include namespace Mf { +class Camera; + + struct OctreeNode : public Entity { std::list objects; @@ -70,8 +75,9 @@ struct OctreeNode : public Entity { (*it)->draw(alpha); } - if (!objects.empty()) - aabb_.draw(); // temporary + + //if (!objects.empty()) + //aabb_.draw(); // temporary } void drawIfVisible(Scalar alpha, const Camera& cam) const @@ -82,8 +88,9 @@ struct OctreeNode : public Entity { (*it)->drawIfVisible(alpha, cam); } - if (!objects.empty()) - aabb_.draw(); + + //if (!objects.empty()) + //aabb_.draw(); } @@ -96,243 +103,58 @@ struct OctreeNode : public Entity return false; } -}; -class Octree; -typedef boost::shared_ptr OctreePtr; - -class Octree : public Tree -{ - - Octree() {} - - explicit Octree(const OctreeNode& initNode) : - Tree(initNode) {} - -public: - - inline static OctreePtr createNewNode(const OctreeNode& item) + static bool compareZOrder(EntityPtr a, EntityPtr b) { - OctreePtr newNode = OctreePtr(new Octree(item)); - init(newNode); - return newNode; + return a->getSphere().point[2] < b->getSphere().point[2]; } - - static Tree::WeakPtr add(Ptr node, EntityPtr entity) + void sort() { - Plane::Halfspace halfspace; - int octantNum = -1; - - Plane xy = node->node.getAabb().getPlaneXY(); - halfspace = xy.intersectsSphere(entity->getSphere()); - - if (halfspace == Plane::POSITIVE) - { - Plane xz = node->node.getAabb().getPlaneXZ(); - halfspace = xz.intersectsSphere(entity->getSphere()); - - if (halfspace == Plane::POSITIVE) - { - Plane yz = node->node.getAabb().getPlaneYZ(); - halfspace = yz.intersectsSphere(entity->getSphere()); - - if (halfspace == Plane::POSITIVE) - { - octantNum = 2; - } - else if (halfspace == Plane::NEGATIVE) - { - octantNum = 3; - } - } - else if (halfspace == Plane::NEGATIVE) - { - Plane yz = node->node.getAabb().getPlaneYZ(); - halfspace = yz.intersectsSphere(entity->getSphere()); - - if (halfspace == Plane::POSITIVE) - { - octantNum = 1; - } - else if (halfspace == Plane::NEGATIVE) - { - octantNum = 0; - } - } - } - else if (halfspace == Plane::NEGATIVE) - { - Plane xz = node->node.getAabb().getPlaneXZ(); - halfspace = xz.intersectsSphere(entity->getSphere()); - - if (halfspace == Plane::POSITIVE) - { - Plane yz = node->node.getAabb().getPlaneYZ(); - halfspace = yz.intersectsSphere(entity->getSphere()); - - if (halfspace == Plane::POSITIVE) - { - octantNum = 6; - } - else if (halfspace == Plane::NEGATIVE) - { - octantNum = 7; - } - } - else if (halfspace == Plane::NEGATIVE) - { - Plane yz = node->node.getAabb().getPlaneYZ(); - halfspace = yz.intersectsSphere(entity->getSphere()); - - if (halfspace == Plane::POSITIVE) - { - octantNum = 5; - } - else if (halfspace == Plane::NEGATIVE) - { - octantNum = 4; - } - } - } - - 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(); - } + //std::sort(objects.begin(), objects.end(), compareZOrder); + objects.sort(compareZOrder); } +}; - 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); - } - } +class Octree +{ +public: - void draw(Ptr node, Scalar alpha) + explicit Octree(const OctreeNode& rootNode) { - 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); - } + tree_.insert(rootNode); } - void drawIfVisible(Ptr node, Scalar alpha, const Camera& cam) + stlplus::ntree::iterator insert(EntityPtr entity) { - //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); - } - } + return insert(tree_.root(), entity); } + stlplus::ntree::iterator reinsert(EntityPtr entity, + stlplus::ntree::iterator node); + void drawIfVisible(Scalar alpha, const Camera& cam) { - drawIfVisible(getThis(), alpha, cam); + drawIfVisible(tree_.root(), alpha, cam); } + void sort(); + +private: + stlplus::ntree::iterator insert(stlplus::ntree::iterator node, EntityPtr entity); + + void addChild(stlplus::ntree::iterator node, int index); + + void draw(stlplus::ntree::iterator node, Scalar alpha); + void drawIfVisible(stlplus::ntree::iterator node, + Scalar alpha, const Camera& cam); + + stlplus::ntree tree_; }; +typedef boost::shared_ptr OctreePtr; } // namespace Mf