| [1] | 1 | /* | 
|---|
 | 2 | ----------------------------------------------------------------------------- | 
|---|
 | 3 | This source file is part of OGRE | 
|---|
 | 4 | (Object-oriented Graphics Rendering Engine) | 
|---|
 | 5 | For the latest info, see http://www.ogre3d.org/ | 
|---|
 | 6 |  | 
|---|
 | 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd | 
|---|
 | 8 | Also see acknowledgements in Readme.html | 
|---|
 | 9 |  | 
|---|
 | 10 | This program is free software; you can redistribute it and/or modify it under | 
|---|
 | 11 | the terms of the GNU Lesser General Public License as published by the Free Software | 
|---|
 | 12 | Foundation; either version 2 of the License, or (at your option) any later | 
|---|
 | 13 | version. | 
|---|
 | 14 |  | 
|---|
 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT | 
|---|
 | 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 
|---|
 | 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. | 
|---|
 | 18 |  | 
|---|
 | 19 | You should have received a copy of the GNU Lesser General Public License along with | 
|---|
 | 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple | 
|---|
 | 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to | 
|---|
 | 22 | http://www.gnu.org/copyleft/lesser.txt. | 
|---|
 | 23 |  | 
|---|
 | 24 | You may alternatively use this source under the terms of a specific version of | 
|---|
 | 25 | the OGRE Unrestricted License provided you have obtained such a license from | 
|---|
 | 26 | Torus Knot Software Ltd. | 
|---|
 | 27 | ----------------------------------------------------------------------------- | 
|---|
 | 28 | */ | 
|---|
 | 29 | /*************************************************************************** | 
|---|
 | 30 | octree.h  -  description | 
|---|
 | 31 | ------------------- | 
|---|
 | 32 | begin                : Mon Sep 30 2002 | 
|---|
 | 33 | copyright            : (C) 2002 by Jon Anderson | 
|---|
 | 34 | email                : janders@users.sf.net | 
|---|
 | 35 |  | 
|---|
 | 36 | Enhancements 2003 - 2004 (C) The OGRE Team | 
|---|
 | 37 | ***************************************************************************/ | 
|---|
 | 38 |  | 
|---|
 | 39 | #ifndef OCTREE_H | 
|---|
 | 40 | #define OCTREE_H | 
|---|
 | 41 |  | 
|---|
 | 42 | #include <OgreAxisAlignedBox.h> | 
|---|
 | 43 | #include <OgreWireBoundingBox.h> | 
|---|
 | 44 |  | 
|---|
 | 45 | #include <list> | 
|---|
 | 46 |  | 
|---|
 | 47 | namespace Ogre | 
|---|
 | 48 | { | 
|---|
 | 49 |  | 
|---|
 | 50 | class OctreeNode; | 
|---|
 | 51 |  | 
|---|
 | 52 | typedef std::list < OctreeNode * > NodeList; | 
|---|
 | 53 |  | 
|---|
 | 54 |  | 
|---|
 | 55 | /** Octree datastructure for managing scene nodes. | 
|---|
 | 56 | @remarks | 
|---|
 | 57 | This is a loose octree implementation, meaning that each | 
|---|
 | 58 | octant child of the octree actually overlaps it's siblings by a factor | 
|---|
 | 59 | of .5.  This guarantees that any thing that is half the size of the parent will | 
|---|
 | 60 | fit completely into a child, with no splitting necessary. | 
|---|
 | 61 | */ | 
|---|
 | 62 |  | 
|---|
 | 63 | class Octree | 
|---|
 | 64 | { | 
|---|
 | 65 | public: | 
|---|
 | 66 |     Octree( Octree * p ); | 
|---|
 | 67 |     ~Octree(); | 
|---|
 | 68 |  | 
|---|
 | 69 |     /** Adds an Octree scene node to this octree level. | 
|---|
 | 70 |     @remarks | 
|---|
 | 71 |     This is called by the OctreeSceneManager after | 
|---|
 | 72 |     it has determined the correct Octree to insert the node into. | 
|---|
 | 73 |     */ | 
|---|
 | 74 |     void _addNode( OctreeNode * ); | 
|---|
 | 75 |  | 
|---|
 | 76 |     /** Removes an Octree scene node to this octree level. | 
|---|
 | 77 |     */ | 
|---|
 | 78 |     void _removeNode( OctreeNode * ); | 
|---|
 | 79 |  | 
|---|
 | 80 |     /** Returns the number of scene nodes attached to this octree | 
|---|
 | 81 |     */ | 
|---|
 | 82 |     int numNodes() | 
|---|
 | 83 |     { | 
|---|
 | 84 |         return mNumNodes; | 
|---|
 | 85 |     }; | 
|---|
 | 86 |  | 
|---|
 | 87 |     /** The bounding box of the octree | 
|---|
 | 88 |     @remarks | 
|---|
 | 89 |     This is used for octant index determination and rendering, but not culling | 
|---|
 | 90 |     */ | 
|---|
 | 91 |     AxisAlignedBox mBox; | 
|---|
 | 92 |     WireBoundingBox* mWireBoundingBox; | 
|---|
 | 93 |      | 
|---|
 | 94 |     /** Creates the wire frame bounding box for this octant | 
|---|
 | 95 |     */ | 
|---|
 | 96 |     WireBoundingBox* getWireBoundingBox(); | 
|---|
 | 97 |  | 
|---|
 | 98 |     /** Vector containing the dimensions of this octree / 2 | 
|---|
 | 99 |     */ | 
|---|
 | 100 |     Vector3 mHalfSize; | 
|---|
 | 101 |  | 
|---|
 | 102 |     /** 3D array of children of this octree. | 
|---|
 | 103 |     @remarks | 
|---|
 | 104 |     Children are dynamically created as needed when nodes are inserted in the Octree. | 
|---|
 | 105 |     If, later, all the nodes are removed from the child, it is still kept around. | 
|---|
 | 106 |     */ | 
|---|
 | 107 |     Octree * mChildren[ 2 ][ 2 ][ 2 ]; | 
|---|
 | 108 |  | 
|---|
 | 109 |     /** Determines if this octree is twice as big as the given box. | 
|---|
 | 110 |     @remarks | 
|---|
 | 111 |     This method is used by the OctreeSceneManager to determine if the given | 
|---|
 | 112 |     box will fit into a child of this octree. | 
|---|
 | 113 |     */ | 
|---|
 | 114 |     bool _isTwiceSize( const AxisAlignedBox &box ) const; | 
|---|
 | 115 |  | 
|---|
 | 116 |     /**  Returns the appropriate indexes for the child of this octree into which the box will fit. | 
|---|
 | 117 |     @remarks | 
|---|
 | 118 |     This is used by the OctreeSceneManager to determine which child to traverse next when | 
|---|
 | 119 |     finding the appropriate octree to insert the box.  Since it is a loose octree, only the | 
|---|
 | 120 |     center of the box is checked to determine the octant. | 
|---|
 | 121 |     */ | 
|---|
 | 122 |     void _getChildIndexes( const AxisAlignedBox &, int *x, int *y, int *z ) const; | 
|---|
 | 123 |  | 
|---|
 | 124 |     /** Creates the AxisAlignedBox used for culling this octree. | 
|---|
 | 125 |     @remarks | 
|---|
 | 126 |     Since it's a loose octree, the culling bounds can be different than the actual bounds of the octree. | 
|---|
 | 127 |     */ | 
|---|
 | 128 |     void _getCullBounds( AxisAlignedBox * ) const; | 
|---|
 | 129 |  | 
|---|
 | 130 |  | 
|---|
 | 131 |     /** Public list of SceneNodes attached to this particular octree | 
|---|
 | 132 |     */ | 
|---|
 | 133 |     NodeList mNodes; | 
|---|
 | 134 |  | 
|---|
 | 135 | protected: | 
|---|
 | 136 |  | 
|---|
 | 137 |     /** Increments the overall node count of this octree and all its parents | 
|---|
 | 138 |     */ | 
|---|
 | 139 |     inline void _ref() | 
|---|
 | 140 |     { | 
|---|
 | 141 |         mNumNodes++; | 
|---|
 | 142 |  | 
|---|
 | 143 |         if ( mParent != 0 ) mParent -> _ref(); | 
|---|
 | 144 |     }; | 
|---|
 | 145 |  | 
|---|
 | 146 |     /** Decrements the overall node count of this octree and all its parents | 
|---|
 | 147 |     */ | 
|---|
 | 148 |     inline void _unref() | 
|---|
 | 149 |     { | 
|---|
 | 150 |         mNumNodes--; | 
|---|
 | 151 |  | 
|---|
 | 152 |         if ( mParent != 0 ) mParent -> _unref(); | 
|---|
 | 153 |     }; | 
|---|
 | 154 |  | 
|---|
 | 155 |     ///number of SceneNodes in this octree and all its children. | 
|---|
 | 156 |     int mNumNodes; | 
|---|
 | 157 |  | 
|---|
 | 158 |     ///parent octree | 
|---|
 | 159 |     Octree * mParent; | 
|---|
 | 160 |  | 
|---|
 | 161 | }; | 
|---|
 | 162 |  | 
|---|
 | 163 | } | 
|---|
 | 164 |  | 
|---|
 | 165 | #endif | 
|---|
 | 166 |  | 
|---|
 | 167 |  | 
|---|