/******************************************************************************* Copyright (c) 2009, Charles McGarvey All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #ifndef _MOOF_OCTREE_HH_ #define _MOOF_OCTREE_HH_ #include #include #include #include #include #include #include #include #include #include namespace Mf { class Frustum; struct OctreeNode; typedef stlplus::ntree::iterator OctreeNodeP; class Octree; typedef boost::shared_ptr OctreeP; struct OctreeNode : public Entity { 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 Frustum& frustum) const { std::list::const_iterator it; for (it = objects.begin(); it != objects.end(); ++it) { (*it)->drawIfVisible(alpha, frustum); } if (!objects.empty()) aabb_.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]; } void sort() { //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); stlplus::ntree tree_; public: void print(OctreeNodeP node) { //logDebug("-----"); //logDebug("depth to node: %d", tree_.depth(node)); //logDebug("size of node: %d", tree_.size(node)); } static OctreeP alloc(const OctreeNode& rootNode) { return OctreeP(new Octree(rootNode)); } explicit Octree(const OctreeNode& rootNode) { tree_.insert(rootNode); } OctreeNodeP insert(EntityP entity) { return insert(entity, tree_.root()); } OctreeNodeP reinsert(EntityP entity, OctreeNodeP node); void drawIfVisible(Scalar alpha, const Frustum& frustum) { drawIfVisible(alpha, frustum, tree_.root()); } void sort(); }; } // namespace Mf #endif // _MOOF_OCTREE_HH_ /** vim: set ts=4 sw=4 tw=80: *************************************************/