#include <Moof/Aabb.hh>
#include <Moof/Drawable.hh>
#include <Moof/Entity.hh>
+#include <Moof/Log.hh>
#include <Moof/Math.hh>
#include <Moof/Sphere.hh>
namespace Mf {
-class Camera;
+class Frustum;
+
+
+struct OctreeNode;
+typedef stlplus::ntree<OctreeNode>::iterator OctreeNodeP;
+
+class Octree;
+typedef boost::shared_ptr<Octree> OctreeP;
struct OctreeNode : public Entity
{
- std::list<EntityPtr> objects;
+ std::list<EntityP> objects;
OctreeNode()
{
void draw(Scalar alpha) const
{
- std::list<EntityPtr>::const_iterator it;
+ std::list<EntityP>::const_iterator it;
for (it = objects.begin(); it != objects.end(); ++it)
{
(*it)->draw(alpha);
}
- //if (!objects.empty())
- //aabb_.draw(); // temporary
+ if (!objects.empty())
+ aabb_.draw(); // temporary
}
- void drawIfVisible(Scalar alpha, const Camera& cam) const
+ void drawIfVisible(Scalar alpha, const Frustum& frustum) const
{
- std::list<EntityPtr>::const_iterator it;
+ std::list<EntityP>::const_iterator it;
for (it = objects.begin(); it != objects.end(); ++it)
{
- (*it)->drawIfVisible(alpha, cam);
+ (*it)->drawIfVisible(alpha, frustum);
}
- //if (!objects.empty())
- //aabb_.draw();
+ if (!objects.empty())
+ {
+ aabb_.draw();
+ //sphere_.draw();
+ }
}
- bool isVisible(const Camera& cam) const
+ bool isVisible(const Frustum& frustum) const
{
- if (sphere_.isVisible(cam))
+ if (sphere_.isVisible(frustum))
{
- return aabb_.isVisible(cam);
+ return aabb_.isVisible(frustum);
}
return false;
}
- static bool compareZOrder(EntityPtr a, EntityPtr b)
+ static bool compareZOrder(EntityP a, EntityP b)
{
return a->getSphere().point[2] < b->getSphere().point[2];
}
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);
+
+ stlplus::ntree<OctreeNode> tree_;
+
public:
- explicit Octree(const OctreeNode& rootNode)
+ void print(OctreeNodeP node)
{
- tree_.insert(rootNode);
+ //logDebug("-----");
+ //logDebug("depth to node: %d", tree_.depth(node));
+ //logDebug("size of node: %d", tree_.size(node));
}
- stlplus::ntree<OctreeNode>::iterator insert(EntityPtr entity)
+ static OctreeP alloc(const OctreeNode& rootNode)
{
- return insert(tree_.root(), entity);
+ return OctreeP(new Octree(rootNode));
}
- stlplus::ntree<OctreeNode>::iterator reinsert(EntityPtr entity,
- stlplus::ntree<OctreeNode>::iterator node);
+ explicit Octree(const OctreeNode& rootNode)
+ {
+ tree_.insert(rootNode);
+ }
- void drawIfVisible(Scalar alpha, const Camera& cam)
+ OctreeNodeP insert(EntityP entity)
{
- drawIfVisible(tree_.root(), alpha, cam);
+ return insert(entity, tree_.root());
}
- void sort();
+ OctreeNodeP reinsert(EntityP entity, OctreeNodeP node);
-private:
- stlplus::ntree<OctreeNode>::iterator insert(stlplus::ntree<OctreeNode>::iterator node, EntityPtr entity);
-
- void addChild(stlplus::ntree<OctreeNode>::iterator node, int index);
+ void draw(Scalar alpha)
+ {
+ draw(alpha, tree_.root());
+ }
- void draw(stlplus::ntree<OctreeNode>::iterator node, Scalar alpha);
- void drawIfVisible(stlplus::ntree<OctreeNode>::iterator node,
- Scalar alpha, const Camera& cam);
+ void drawIfVisible(Scalar alpha, const Frustum& frustum)
+ {
+ drawIfVisible(alpha, frustum, tree_.root());
+ }
- stlplus::ntree<OctreeNode> tree_;
+ void sort();
};
-typedef boost::shared_ptr<Octree> OctreePtr;
-
} // namespace Mf