]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Octree.hh
added missing octree method
[chaz/yoink] / src / Moof / Octree.hh
index 2ee617f3744f0baa6155c15df3a767617cb35c56..e3e2225f03299636137b21aa3a54aeb38fd25b38 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/Log.hh>
 #include <Moof/Math.hh>
-#include <Moof/Tree.hh>
+#include <Moof/Sphere.hh>
        
 
 namespace Mf {
 
 
-class Entity;
+class Frustum;
 
 
-class Octree
+struct OctreeNode;
+typedef stlplus::ntree<OctreeNode>::iterator OctreeNodeP;
+
+class Octree;
+typedef boost::shared_ptr<Octree> OctreeP;
+
+
+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 Frustum& frustum) const
+       {
+               std::list<EntityP>::const_iterator it;
+
+               for (it = objects.begin(); it != objects.end(); ++it)
+               {
+                       (*it)->drawIfVisible(alpha, frustum);
+               }
+
+               if (!objects.empty())
+               {
+                       aabb_.draw();
+                       //sphere_.draw();
+               }
+       }
+
+
+       bool isVisible(const Frustum& frustum) const
+       {
+               if (sphere_.isVisible(frustum))
+               {
+                       return aabb_.isVisible(frustum);
+               }
+
+               return false;
+       }
+
+
+       static bool compareZOrder(EntityP a, EntityP b)
+       {
+               return a->getSphere().point[2] < b->getSphere().point[2];
+       }
 
-       class Node
+       void sort()
        {
-               Aabb    aabb_;
-               Vector3 center_;
+               //std::sort(objects.begin(), objects.end(), compareZOrder);
+               objects.sort(compareZOrder);
+       }
+};
+
+
+class Octree
+{
+       OctreeNodeP insert(EntityP entity, OctreeNodeP node);
+       
+       void addChild(OctreeNodeP node, int index);
+
+       void draw(Scalar alpha, OctreeNodeP node);
+       void drawIfVisible(Scalar alpha, const Frustum& frustum, OctreeNodeP node);
 
-               std::list<boost::shared_ptr<Entity> > objects_;
+       stlplus::ntree<OctreeNode> tree_;
 
-       public:
+public:
+
+       void print(OctreeNodeP node)
+       {
+               //logDebug("-----");
+               //logDebug("depth to node: %d", tree_.depth(node));
+               //logDebug("size of node: %d", tree_.size(node));
+       }
 
-               Node() :
-                       aabb_(-1.0, -1.0, -1.0, 1.0, 1.0, 1.0),
-                       center_(0.0, 0.0, 0.0) {}
+       static OctreeP alloc(const OctreeNode& rootNode)
+       {
+               return OctreeP(new Octree(rootNode));
+       }
 
-               Node(const Aabb& aabb) :
-                       aabb_(aabb),
-                       center_(aabb.getCenter()) {}
-       };
+       explicit Octree(const OctreeNode& rootNode)
+       {
+               tree_.insert(rootNode);
+       }
 
-       Octree() :
-               root_(new Tree<Node>()) {}
+       OctreeNodeP insert(EntityP entity)
+       {
+               return insert(entity, tree_.root());
+       }
 
-       Octree(const Aabb& aabb) :
-               root_(new Tree<Node>(Node(aabb))) {}
+       OctreeNodeP reinsert(EntityP entity, OctreeNodeP node);
 
+       void draw(Scalar alpha)
+       {
+               draw(alpha, tree_.root());
+       }
 
-       Tree<Node>::WeakPtr add(EntityPtr object);
+       void drawIfVisible(Scalar alpha, const Frustum& frustum)
+       {
+               drawIfVisible(alpha, frustum, tree_.root());
+       }
 
-private:
-       Tree<Node>::Ptr root_;
+       void sort();
 };
 
 
This page took 0.02198 seconds and 4 git commands to generate.