BrushBSP.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __BRUSHBSP_H__
  21. #define __BRUSHBSP_H__
  22. /*
  23. ===============================================================================
  24. BrushBSP
  25. ===============================================================================
  26. */
  27. class idBrushBSP;
  28. class idBrushBSPNode;
  29. class idBrushBSPPortal;
  30. //===============================================================
  31. //
  32. // idBrushBSPPortal
  33. //
  34. //===============================================================
  35. class idBrushBSPPortal {
  36. friend class idBrushBSP;
  37. friend class idBrushBSPNode;
  38. public:
  39. idBrushBSPPortal( void );
  40. ~idBrushBSPPortal( void );
  41. void AddToNodes( idBrushBSPNode *front, idBrushBSPNode *back );
  42. void RemoveFromNode( idBrushBSPNode *l );
  43. void Flip( void );
  44. int Split( const idPlane &splitPlane, idBrushBSPPortal **front, idBrushBSPPortal **back );
  45. idWinding * GetWinding( void ) const { return winding; }
  46. const idPlane & GetPlane( void ) const { return plane; }
  47. void SetFaceNum( int num ) { faceNum = num; }
  48. int GetFaceNum( void ) const { return faceNum; }
  49. int GetFlags( void ) const { return flags; }
  50. void SetFlag( int flag ) { flags |= flag; }
  51. void RemoveFlag( int flag ) { flags &= ~flag; }
  52. idBrushBSPPortal * Next( int side ) const { return next[side]; }
  53. idBrushBSPNode * GetNode( int side ) const { return nodes[side]; }
  54. private:
  55. idPlane plane; // portal plane
  56. int planeNum; // number of plane this portal is on
  57. idWinding * winding; // portal winding
  58. idBrushBSPNode * nodes[2]; // nodes this portal seperates
  59. idBrushBSPPortal * next[2]; // next portal in list for both nodes
  60. int flags; // portal flags
  61. int faceNum; // number of the face created for this portal
  62. };
  63. //===============================================================
  64. //
  65. // idBrushBSPNode
  66. //
  67. //===============================================================
  68. #define NODE_VISITED BIT(30)
  69. #define NODE_DONE BIT(31)
  70. class idBrushBSPNode {
  71. friend class idBrushBSP;
  72. friend class idBrushBSPPortal;
  73. public:
  74. idBrushBSPNode( void );
  75. ~idBrushBSPNode( void );
  76. void SetContentsFromBrushes( void );
  77. idBounds GetPortalBounds( void );
  78. idBrushBSPNode * GetChild( int index ) const { return children[index]; }
  79. idBrushBSPNode * GetParent( void ) const { return parent; }
  80. void SetContents( int contents ) { this->contents = contents; }
  81. int GetContents( void ) const { return contents; }
  82. const idPlane & GetPlane( void ) const { return plane; }
  83. idBrushBSPPortal * GetPortals( void ) const { return portals; }
  84. void SetAreaNum( int num ) { areaNum = num; }
  85. int GetAreaNum( void ) const { return areaNum; }
  86. int GetFlags( void ) const { return flags; }
  87. void SetFlag( int flag ) { flags |= flag; }
  88. void RemoveFlag( int flag ) { flags &= ~flag; }
  89. bool TestLeafNode( void );
  90. // remove the flag from nodes found by flooding through portals to nodes with the flag set
  91. void RemoveFlagFlood( int flag );
  92. // recurse down the tree and remove the flag from all visited nodes
  93. void RemoveFlagRecurse( int flag );
  94. // first recurse down the tree and flood from there
  95. void RemoveFlagRecurseFlood( int flag );
  96. // returns side of the plane the node is on
  97. int PlaneSide( const idPlane &plane, float epsilon = ON_EPSILON ) const;
  98. // split the leaf node with a plane
  99. bool Split( const idPlane &splitPlane, int splitPlaneNum );
  100. private:
  101. idPlane plane; // split plane if this is not a leaf node
  102. idBrush * volume; // node volume
  103. int contents; // node contents
  104. idBrushList brushList; // list with brushes for this node
  105. idBrushBSPNode * parent; // parent of this node
  106. idBrushBSPNode * children[2]; // both are NULL if this is a leaf node
  107. idBrushBSPPortal * portals; // portals of this node
  108. int flags; // node flags
  109. int areaNum; // number of the area created for this node
  110. int occupied; // true when portal is occupied
  111. };
  112. //===============================================================
  113. //
  114. // idBrushBSP
  115. //
  116. //===============================================================
  117. class idBrushBSP {
  118. public:
  119. idBrushBSP( void );
  120. ~idBrushBSP( void );
  121. // build a bsp tree from a set of brushes
  122. void Build( idBrushList brushList, int skipContents,
  123. bool (*ChopAllowed)( idBrush *b1, idBrush *b2 ),
  124. bool (*MergeAllowed)( idBrush *b1, idBrush *b2 ) );
  125. // remove splits in subspaces with the given contents
  126. void PruneTree( int contents );
  127. // portalize the bsp tree
  128. void Portalize( void );
  129. // remove subspaces outside the map not reachable by entities
  130. bool RemoveOutside( const idMapFile *mapFile, int contents, const idStrList &classNames );
  131. // write file with a trace going through a leak
  132. void LeakFile( const idStr &fileName );
  133. // try to merge portals
  134. void MergePortals( int skipContents );
  135. // try to merge the two leaf nodes at either side of the portal
  136. bool TryMergeLeafNodes( idBrushBSPPortal *portal, int side );
  137. void PruneMergedTree_r( idBrushBSPNode *node );
  138. // melt portal windings
  139. void MeltPortals( int skipContents );
  140. // write a map file with a brush for every leaf node that has the given contents
  141. void WriteBrushMap( const idStr &fileName, const idStr &ext, int contents );
  142. // bounds for the whole tree
  143. const idBounds & GetTreeBounds( void ) const { return treeBounds; }
  144. // root node of the tree
  145. idBrushBSPNode * GetRootNode( void ) const { return root; }
  146. private:
  147. idBrushBSPNode * root;
  148. idBrushBSPNode * outside;
  149. idBounds treeBounds;
  150. idPlaneSet portalPlanes;
  151. int numGridCells;
  152. int numSplits;
  153. int numGridCellSplits;
  154. int numPrunedSplits;
  155. int numPortals;
  156. int solidLeafNodes;
  157. int outsideLeafNodes;
  158. int insideLeafNodes;
  159. int numMergedPortals;
  160. int numInsertedPoints;
  161. idVec3 leakOrigin;
  162. int brushMapContents;
  163. idBrushMap * brushMap;
  164. bool (*BrushChopAllowed)( idBrush *b1, idBrush *b2 );
  165. bool (*BrushMergeAllowed)( idBrush *b1, idBrush *b2 );
  166. private:
  167. void RemoveMultipleLeafNodeReferences_r( idBrushBSPNode *node );
  168. void Free_r( idBrushBSPNode *node );
  169. void IncreaseNumSplits( void );
  170. bool IsValidSplitter( const idBrushSide *side );
  171. int BrushSplitterStats( const idBrush *brush, int planeNum, const idPlaneSet &planeList, bool *testedPlanes, struct splitterStats_s &stats );
  172. int FindSplitter( idBrushBSPNode *node, const idPlaneSet &planeList, bool *testedPlanes, struct splitterStats_s &bestStats );
  173. void SetSplitterUsed( idBrushBSPNode *node, int planeNum );
  174. idBrushBSPNode * BuildBrushBSP_r( idBrushBSPNode *node, const idPlaneSet &planeList, bool *testedPlanes, int skipContents );
  175. idBrushBSPNode * ProcessGridCell( idBrushBSPNode *node, int skipContents );
  176. void BuildGrid_r( idList<idBrushBSPNode *> &gridCells, idBrushBSPNode *node );
  177. void PruneTree_r( idBrushBSPNode *node, int contents );
  178. void MakeOutsidePortals( void );
  179. idWinding * BaseWindingForNode( idBrushBSPNode *node );
  180. void MakeNodePortal( idBrushBSPNode *node );
  181. void SplitNodePortals( idBrushBSPNode *node );
  182. void MakeTreePortals_r( idBrushBSPNode *node );
  183. void FloodThroughPortals_r( idBrushBSPNode *node, int contents, int depth );
  184. bool FloodFromOrigin( const idVec3 &origin, int contents );
  185. bool FloodFromEntities( const idMapFile *mapFile, int contents, const idStrList &classNames );
  186. void RemoveOutside_r( idBrushBSPNode *node, int contents );
  187. void SetPortalPlanes_r( idBrushBSPNode *node, idPlaneSet &planeList );
  188. void SetPortalPlanes( void );
  189. void MergePortals_r( idBrushBSPNode *node, int skipContents );
  190. void MergeLeafNodePortals( idBrushBSPNode *node, int skipContents );
  191. void UpdateTreeAfterMerge_r( idBrushBSPNode *node, const idBounds &bounds, idBrushBSPNode *oldNode, idBrushBSPNode *newNode );
  192. void RemoveLeafNodeColinearPoints( idBrushBSPNode *node );
  193. void RemoveColinearPoints_r( idBrushBSPNode *node, int skipContents );
  194. void MeltFlood_r( idBrushBSPNode *node, int skipContents, idBounds &bounds, idVectorSet<idVec3,3> &vertexList );
  195. void MeltLeafNodePortals( idBrushBSPNode *node, int skipContents, idVectorSet<idVec3,3> &vertexList );
  196. void MeltPortals_r( idBrushBSPNode *node, int skipContents, idVectorSet<idVec3,3> &vertexList );
  197. };
  198. #endif /* !__BRUSHBSP_H__ */