[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 | #ifndef _BspNode_H__ |
---|
| 30 | #define _BspNode_H__ |
---|
| 31 | |
---|
| 32 | #include "OgreBspPrerequisites.h" |
---|
| 33 | #include "OgrePlane.h" |
---|
| 34 | #include "OgreAxisAlignedBox.h" |
---|
| 35 | #include "OgreSceneQuery.h" |
---|
| 36 | |
---|
| 37 | namespace Ogre { |
---|
| 38 | |
---|
| 39 | /** Encapsulates a node in a BSP tree. |
---|
| 40 | A BSP tree represents space partitioned by planes . The space which is |
---|
| 41 | partitioned is either the world (in the case of the root node) or the space derived |
---|
| 42 | from their parent node. Each node can have elements which are in front or behind it, which are |
---|
| 43 | it's children and these elements can either be further subdivided by planes, |
---|
| 44 | or they can be undivided spaces or 'leaf nodes' - these are the nodes which actually contain |
---|
| 45 | objects and world geometry.The leaves of the tree are the stopping point of any tree walking algorithm, |
---|
| 46 | both for rendering and collision detection etc.</p> |
---|
| 47 | Ogre chooses not to represent splitting nodes and leaves as separate structures, but to merge the two for simplicity |
---|
| 48 | of the walking algorithm. If a node is a leaf, the isLeaf() method returns true and both getFront() and |
---|
| 49 | getBack() return null pointers. If the node is a partitioning plane isLeaf() returns false and getFront() |
---|
| 50 | and getBack() will return the corresponding BspNode objects. |
---|
| 51 | */ |
---|
| 52 | class BspNode |
---|
| 53 | { |
---|
| 54 | friend class BspLevel; |
---|
| 55 | |
---|
| 56 | public: |
---|
| 57 | /** Constructor, only to be used by BspLevel. */ |
---|
| 58 | BspNode(BspLevel* owner, bool isLeaf); |
---|
| 59 | |
---|
| 60 | BspNode(); |
---|
| 61 | ~BspNode(); |
---|
| 62 | |
---|
| 63 | /** Returns true if this node is a leaf (i.e. contains geometry) or false if it is a splitting plane. |
---|
| 64 | A BspNode can either be a splitting plane (the typical representation of a BSP node) or an undivided |
---|
| 65 | region contining geometry (a leaf node). Ogre represents both using the same class for simplicity |
---|
| 66 | of tree walking. However it is important that you use this method to determine which type you are dealing |
---|
| 67 | with, since certain methods are only supported with one of the subtypes. Details are given in the individual methods. |
---|
| 68 | Note that I could have represented splitting / leaf nodes as a class hierarchy but the |
---|
| 69 | virtual methods / run-time type identification would have a performance hit, and it would not make the |
---|
| 70 | code much (any?) simpler anyway. I think this is a fair trade-off in this case. |
---|
| 71 | */ |
---|
| 72 | bool isLeaf(void) const; |
---|
| 73 | |
---|
| 74 | /** Returns a pointer to a BspNode containing the subspace on the positive side of the splitting plane. |
---|
| 75 | This method should only be called on a splitting node, i.e. where isLeaf() returns false. Calling this |
---|
| 76 | method on a leaf node will throw an exception. |
---|
| 77 | */ |
---|
| 78 | BspNode* getFront(void) const; |
---|
| 79 | |
---|
| 80 | /** Returns a pointer to a BspNode containing the subspace on the negative side of the splitting plane. |
---|
| 81 | This method should only be called on a splitting node, i.e. where isLeaf() returns false. Calling this |
---|
| 82 | method on a leaf node will throw an exception. |
---|
| 83 | */ |
---|
| 84 | BspNode* getBack(void) const; |
---|
| 85 | |
---|
| 86 | /** Determines which side of the splitting plane a worldspace point is. |
---|
| 87 | This method should only be called on a splitting node, i.e. where isLeaf() returns false. Calling this |
---|
| 88 | method on a leaf node will throw an exception. |
---|
| 89 | */ |
---|
| 90 | Plane::Side getSide (const Vector3& point) const; |
---|
| 91 | |
---|
| 92 | /** Gets the next node down in the tree, with the intention of |
---|
| 93 | locating the leaf containing the given point. |
---|
| 94 | This method should only be called on a splitting node, i.e. where isLeaf() returns false. Calling this |
---|
| 95 | method on a leaf node will throw an exception. |
---|
| 96 | */ |
---|
| 97 | BspNode* getNextNode(const Vector3& point) const; |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | /** Returns details of the plane which is used to subdivide the space of his node's children. |
---|
| 101 | This method should only be called on a splitting node, i.e. where isLeaf() returns false. Calling this |
---|
| 102 | method on a leaf node will throw an exception. |
---|
| 103 | */ |
---|
| 104 | const Plane& getSplitPlane(void) const; |
---|
| 105 | |
---|
| 106 | /** Returns the axis-aligned box which contains this node if it is a leaf. |
---|
| 107 | This method should only be called on a leaf node. It returns a box which can be used in calls like |
---|
| 108 | Camera::isVisible to determine if the leaf node is visible in the view. |
---|
| 109 | */ |
---|
| 110 | const AxisAlignedBox& getBoundingBox(void) const; |
---|
| 111 | |
---|
| 112 | /** Returns the number of faces contained in this leaf node. |
---|
| 113 | Should only be called on a leaf node. |
---|
| 114 | */ |
---|
| 115 | int getNumFaceGroups(void) const; |
---|
| 116 | /** Returns the index to the face group index list for this leaf node. |
---|
| 117 | The contents of this buffer is a list of indexes which point to the |
---|
| 118 | actual face groups held in a central buffer in the BspLevel class (in |
---|
| 119 | actual fact for efficency the indexes themselves are also held in a single |
---|
| 120 | buffer in BspLevel too). The reason for this indirection is that the buffer |
---|
| 121 | of indexes to face groups is organised in chunks relative to nodes, whilst the |
---|
| 122 | main buffer of face groups may not be. |
---|
| 123 | Should only be called on a leaf node. |
---|
| 124 | */ |
---|
| 125 | int getFaceGroupStart(void) const; |
---|
| 126 | |
---|
| 127 | /** Determines if the passed in node (must also be a leaf) is visible from this leaf. |
---|
| 128 | Must only be called on a leaf node, and the parameter must also be a leaf node. If |
---|
| 129 | this method returns true, then the leaf passed in is visible from this leaf. |
---|
| 130 | Note that internally this uses the Potentially Visible Set (PVS) which is precalculated |
---|
| 131 | and stored with the BSP level. |
---|
| 132 | */ |
---|
| 133 | bool isLeafVisible(const BspNode* leaf) const; |
---|
| 134 | |
---|
| 135 | friend std::ostream& operator<< (std::ostream& o, BspNode& n); |
---|
| 136 | |
---|
| 137 | /// Internal method for telling the node that a movable intersects it |
---|
| 138 | void _addMovable(const MovableObject* mov); |
---|
| 139 | |
---|
| 140 | /// Internal method for telling the node that a movable no longer intersects it |
---|
| 141 | void _removeMovable(const MovableObject* mov); |
---|
| 142 | |
---|
| 143 | /// Gets the signed distance to the dividing plane |
---|
| 144 | Real getDistance(const Vector3& pos) const; |
---|
| 145 | |
---|
| 146 | typedef std::set<const MovableObject*> IntersectingObjectSet; |
---|
| 147 | |
---|
| 148 | struct Brush |
---|
| 149 | { |
---|
| 150 | std::list<Plane> planes; |
---|
| 151 | SceneQuery::WorldFragment fragment; // For query reporting |
---|
| 152 | }; |
---|
| 153 | typedef std::vector<Brush*> NodeBrushList; // Main brush memory held on level |
---|
| 154 | |
---|
| 155 | /** Get the list of solid Brushes for this node. |
---|
| 156 | @remarks Only applicable for leaf nodes. |
---|
| 157 | */ |
---|
| 158 | const NodeBrushList& getSolidBrushes(void) const; |
---|
| 159 | protected: |
---|
| 160 | BspLevel* mOwner; // Back-reference to containing level |
---|
| 161 | bool mIsLeaf; |
---|
| 162 | |
---|
| 163 | // Node-only members |
---|
| 164 | /** The plane which splits space in a non-leaf node. |
---|
| 165 | Note that nodes do not allocate the memory for other nodes - for simplicity and bulk-allocation |
---|
| 166 | of memory the BspLevel is responsible for assigning enough memory for all nodes in one go. |
---|
| 167 | */ |
---|
| 168 | Plane mSplitPlane; |
---|
| 169 | /** Pointer to the node in front of this non-leaf node. */ |
---|
| 170 | BspNode* mFront; |
---|
| 171 | /** Pointer to the node behind this non-leaf node. */ |
---|
| 172 | BspNode* mBack; |
---|
| 173 | |
---|
| 174 | // Leaf-only members |
---|
| 175 | /** The cluster number of this leaf. |
---|
| 176 | Leaf nodes are assigned to 'clusters' of nodes, which are used to group nodes together for |
---|
| 177 | visibility testing. There is a lookup table which is used to determine if one cluster of leaves |
---|
| 178 | is visible from another cluster. Whilst it would be possible to expand all this out so that |
---|
| 179 | each node had a list of pointers to other visible nodes, this would be very expensive in terms |
---|
| 180 | of storage (using the cluster method there is a table which is 1-bit squared per cluster, rounded |
---|
| 181 | up to the nearest byte obviously, which uses far less space than 4-bytes per linked node per source |
---|
| 182 | node). Of course the limitation here is that you have to each leaf in turn to determine if it is visible |
---|
| 183 | rather than just following a list, but since this is only done once per frame this is not such a big |
---|
| 184 | overhead. |
---|
| 185 | */ |
---|
| 186 | int mVisCluster; |
---|
| 187 | |
---|
| 188 | /** The axis-aligned box which bounds node if it is a leaf. */ |
---|
| 189 | AxisAlignedBox mBounds; |
---|
| 190 | /** Number of face groups in this node if it is a leaf. */ |
---|
| 191 | int mNumFaceGroups; |
---|
| 192 | /** Index to the part of the main leaf facegroup index buffer(held in BspLevel) for this leaf. |
---|
| 193 | This leaf uses mNumFaceGroups from this pointer onwards. From here you use the index |
---|
| 194 | in this buffer to look up the actual face. |
---|
| 195 | Note that again for simplicity and bulk memory allocation the face |
---|
| 196 | group list itself is allocated by the BspLevel for all nodes, and each leaf node is given a section of it to |
---|
| 197 | work on. This saves lots of small memory allocations / deallocations which limits memory fragmentation. |
---|
| 198 | */ |
---|
| 199 | int mFaceGroupStart; |
---|
| 200 | |
---|
| 201 | IntersectingObjectSet mMovables; |
---|
| 202 | |
---|
| 203 | NodeBrushList mSolidBrushes; |
---|
| 204 | public: |
---|
| 205 | const IntersectingObjectSet& getObjects(void) const { return mMovables; } |
---|
| 206 | |
---|
| 207 | |
---|
| 208 | }; |
---|
| 209 | |
---|
| 210 | |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | #endif |
---|