]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Octree.hh
considerable refactoring
[chaz/yoink] / src / Moof / Octree.hh
index 2ee617f3744f0baa6155c15df3a767617cb35c56..774eece11c56cb34236a82d2b23ce6f501765351 100644 (file)
 #ifndef _MOOF_OCTREE_HH_
 #define _MOOF_OCTREE_HH_
 
+#include <algorithm>
 #include <list>
 
 #include <boost/shared_ptr.hpp>
 
+#include <stlplus/ntree.hpp>
+
+#include <Moof/Aabb.hh>
+#include <Moof/Drawable.hh>
+#include <Moof/Entity.hh>
 #include <Moof/Math.hh>
-#include <Moof/Tree.hh>
+#include <Moof/Sphere.hh>
        
 
 namespace Mf {
 
 
-class Entity;
+class Camera;
 
 
-class Octree
+struct OctreeNode : public Entity
 {
-public:
+       std::list<EntityP> objects;
+
+       OctreeNode()
+       {
+               aabb_.min = Vector3(-1.0, -1.0, -1.0);
+               aabb_.max = Vector3(1.0, 1.0, 1.0);
+               sphere_.init(Vector3(0.0, 0.0, 0.0), 1.41421);
+       }
+
+       OctreeNode(const Aabb& aabb)
+       {
+               aabb_ = aabb;
+               sphere_.point = aabb.getCenter();
+               sphere_.radius = (aabb.min - sphere_.point).length();
+       }
+
+       void draw(Scalar alpha) const
+       {
+               std::list<EntityP>::const_iterator it;
+
+               for (it = objects.begin(); it != objects.end(); ++it)
+               {
+                       (*it)->draw(alpha);
+               }
+
+               if (!objects.empty())
+                       aabb_.draw(); // temporary
+       }
+
+       void drawIfVisible(Scalar alpha, const Camera& cam) const
+       {
+               std::list<EntityP>::const_iterator it;
+
+               for (it = objects.begin(); it != objects.end(); ++it)
+               {
+                       (*it)->drawIfVisible(alpha, cam);
+               }
+
+               if (!objects.empty())
+                       aabb_.draw();
+       }
+
+
+       bool isVisible(const Camera& cam) const
+       {
+               if (sphere_.isVisible(cam))
+               {
+                       return aabb_.isVisible(cam);
+               }
+
+               return false;
+       }
+
 
-       class Node
+       static bool compareZOrder(EntityP a, EntityP b)
        {
-               Aabb    aabb_;
-               Vector3 center_;
+               return a->getSphere().point[2] < b->getSphere().point[2];
+       }
 
-               std::list<boost::shared_ptr<Entity> > objects_;
+       void sort()
+       {
+               //std::sort(objects.begin(), objects.end(), compareZOrder);
+               objects.sort(compareZOrder);
+       }
+};
 
-       public:
 
-               Node() :
-                       aabb_(-1.0, -1.0, -1.0, 1.0, 1.0, 1.0),
-                       center_(0.0, 0.0, 0.0) {}
+class Octree;
+typedef boost::shared_ptr<Octree> OctreeP;
 
-               Node(const Aabb& aabb) :
-                       aabb_(aabb),
-                       center_(aabb.getCenter()) {}
-       };
+class Octree
+{
+       stlplus::ntree<OctreeNode>::iterator
+               insert(stlplus::ntree<OctreeNode>::iterator node, EntityP entity);
+       
+       void addChild(stlplus::ntree<OctreeNode>::iterator node, int index);
 
-       Octree() :
-               root_(new Tree<Node>()) {}
+       void draw(stlplus::ntree<OctreeNode>::iterator node, Scalar alpha);
+       void drawIfVisible(stlplus::ntree<OctreeNode>::iterator node,
+                       Scalar alpha, const Camera& cam);
 
-       Octree(const Aabb& aabb) :
-               root_(new Tree<Node>(Node(aabb))) {}
+       stlplus::ntree<OctreeNode> tree_;
 
+public:
 
-       Tree<Node>::WeakPtr add(EntityPtr object);
+       inline static OctreeP alloc(const OctreeNode& rootNode)
+       {
+               return OctreeP(new Octree(rootNode));
+       }
+
+       explicit Octree(const OctreeNode& rootNode)
+       {
+               tree_.insert(rootNode);
+       }
+
+       stlplus::ntree<OctreeNode>::iterator insert(EntityP entity)
+       {
+               return insert(tree_.root(), entity);
+       }
+
+       stlplus::ntree<OctreeNode>::iterator reinsert(EntityP entity,
+                       stlplus::ntree<OctreeNode>::iterator node);
+
+       void drawIfVisible(Scalar alpha, const Camera& cam)
+       {
+               drawIfVisible(tree_.root(), alpha, cam);
+       }
 
-private:
-       Tree<Node>::Ptr root_;
+       void sort();
 };
 
 
This page took 0.023033 seconds and 4 git commands to generate.