X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2FMoof%2FOctree.hh;h=d896844f0379a4ab7ca1cebe32d52cb5a719a775;hb=bfa6212d09d8735d8fd5e2638188e4a99f21ada4;hp=2ee617f3744f0baa6155c15df3a767617cb35c56;hpb=29e3d45f7bbbf31eadf793c41ff2b3d9c47b7539;p=chaz%2Fyoink diff --git a/src/Moof/Octree.hh b/src/Moof/Octree.hh index 2ee617f..d896844 100644 --- a/src/Moof/Octree.hh +++ b/src/Moof/Octree.hh @@ -29,55 +29,133 @@ #ifndef _MOOF_OCTREE_HH_ #define _MOOF_OCTREE_HH_ +#include #include #include +#include + +#include +#include +#include #include -#include +#include namespace Mf { -class Entity; +class Camera; -class Octree +struct OctreeNode : public Entity { -public: + std::list 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::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::const_iterator it; + + for (it = objects.begin(); it != objects.end(); ++it) + { + (*it)->drawIfVisible(alpha, cam); + } + + //if (!objects.empty()) + //aabb_.draw(); + } + - class Node + bool isVisible(const Camera& cam) const { - Aabb aabb_; - Vector3 center_; + if (sphere_.isVisible(cam)) + { + return aabb_.isVisible(cam); + } - std::list > objects_; + return false; + } - public: - Node() : - aabb_(-1.0, -1.0, -1.0, 1.0, 1.0, 1.0), - center_(0.0, 0.0, 0.0) {} + static bool compareZOrder(EntityPtr a, EntityPtr b) + { + return a->getSphere().point[2] < b->getSphere().point[2]; + } + + void sort() + { + //std::sort(objects.begin(), objects.end(), compareZOrder); + objects.sort(compareZOrder); + } +}; + + +class Octree +{ +public: - Node(const Aabb& aabb) : - aabb_(aabb), - center_(aabb.getCenter()) {} - }; + explicit Octree(const OctreeNode& rootNode) + { + tree_.insert(rootNode); + } - Octree() : - root_(new Tree()) {} + stlplus::ntree::iterator insert(EntityPtr entity) + { + return insert(tree_.root(), entity); + } - Octree(const Aabb& aabb) : - root_(new Tree(Node(aabb))) {} + stlplus::ntree::iterator reinsert(EntityPtr entity, + stlplus::ntree::iterator node); + void drawIfVisible(Scalar alpha, const Camera& cam) + { + drawIfVisible(tree_.root(), alpha, cam); + } - Tree::WeakPtr add(EntityPtr object); + void sort(); private: - Tree::Ptr root_; + 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