doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BrushBSP.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 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.
23 
24 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.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __BRUSHBSP_H__
30 #define __BRUSHBSP_H__
31 
32 /*
33 ===============================================================================
34 
35  BrushBSP
36 
37 ===============================================================================
38 */
39 
40 class idBrushBSP;
41 class idBrushBSPNode;
42 class idBrushBSPPortal;
43 
44 
45 //===============================================================
46 //
47 // idBrushBSPPortal
48 //
49 //===============================================================
50 
52 
53  friend class idBrushBSP;
54  friend class idBrushBSPNode;
55 
56 public:
57  idBrushBSPPortal( void );
58  ~idBrushBSPPortal( void );
59  void AddToNodes( idBrushBSPNode *front, idBrushBSPNode *back );
61  void Flip( void );
62  int Split( const idPlane &splitPlane, idBrushBSPPortal **front, idBrushBSPPortal **back );
63  idWinding * GetWinding( void ) const { return winding; }
64  const idPlane & GetPlane( void ) const { return plane; }
65  void SetFaceNum( int num ) { faceNum = num; }
66  int GetFaceNum( void ) const { return faceNum; }
67  int GetFlags( void ) const { return flags; }
68  void SetFlag( int flag ) { flags |= flag; }
69  void RemoveFlag( int flag ) { flags &= ~flag; }
70  idBrushBSPPortal * Next( int side ) const { return next[side]; }
71  idBrushBSPNode * GetNode( int side ) const { return nodes[side]; }
72 
73 private:
74  idPlane plane; // portal plane
75  int planeNum; // number of plane this portal is on
76  idWinding * winding; // portal winding
77  idBrushBSPNode * nodes[2]; // nodes this portal seperates
78  idBrushBSPPortal * next[2]; // next portal in list for both nodes
79  int flags; // portal flags
80  int faceNum; // number of the face created for this portal
81 };
82 
83 
84 //===============================================================
85 //
86 // idBrushBSPNode
87 //
88 //===============================================================
89 
90 #define NODE_VISITED BIT(30)
91 #define NODE_DONE BIT(31)
92 
94 
95  friend class idBrushBSP;
96  friend class idBrushBSPPortal;
97 
98 public:
99  idBrushBSPNode( void );
100  ~idBrushBSPNode( void );
101  void SetContentsFromBrushes( void );
102  idBounds GetPortalBounds( void );
103  idBrushBSPNode * GetChild( int index ) const { return children[index]; }
104  idBrushBSPNode * GetParent( void ) const { return parent; }
105  void SetContents( int contents ) { this->contents = contents; }
106  int GetContents( void ) const { return contents; }
107  const idPlane & GetPlane( void ) const { return plane; }
108  idBrushBSPPortal * GetPortals( void ) const { return portals; }
109  void SetAreaNum( int num ) { areaNum = num; }
110  int GetAreaNum( void ) const { return areaNum; }
111  int GetFlags( void ) const { return flags; }
112  void SetFlag( int flag ) { flags |= flag; }
113  void RemoveFlag( int flag ) { flags &= ~flag; }
114  bool TestLeafNode( void );
115  // remove the flag from nodes found by flooding through portals to nodes with the flag set
116  void RemoveFlagFlood( int flag );
117  // recurse down the tree and remove the flag from all visited nodes
118  void RemoveFlagRecurse( int flag );
119  // first recurse down the tree and flood from there
120  void RemoveFlagRecurseFlood( int flag );
121  // returns side of the plane the node is on
122  int PlaneSide( const idPlane &plane, float epsilon = ON_EPSILON ) const;
123  // split the leaf node with a plane
124  bool Split( const idPlane &splitPlane, int splitPlaneNum );
125 
126 
127 private:
128  idPlane plane; // split plane if this is not a leaf node
129  idBrush * volume; // node volume
130  int contents; // node contents
131  idBrushList brushList; // list with brushes for this node
132  idBrushBSPNode * parent; // parent of this node
133  idBrushBSPNode * children[2]; // both are NULL if this is a leaf node
134  idBrushBSPPortal * portals; // portals of this node
135  int flags; // node flags
136  int areaNum; // number of the area created for this node
137  int occupied; // true when portal is occupied
138 };
139 
140 
141 //===============================================================
142 //
143 // idBrushBSP
144 //
145 //===============================================================
146 
147 class idBrushBSP {
148 
149 public:
150  idBrushBSP( void );
151  ~idBrushBSP( void );
152  // build a bsp tree from a set of brushes
153  void Build( idBrushList brushList, int skipContents,
154  bool (*ChopAllowed)( idBrush *b1, idBrush *b2 ),
155  bool (*MergeAllowed)( idBrush *b1, idBrush *b2 ) );
156  // remove splits in subspaces with the given contents
157  void PruneTree( int contents );
158  // portalize the bsp tree
159  void Portalize( void );
160  // remove subspaces outside the map not reachable by entities
161  bool RemoveOutside( const idMapFile *mapFile, int contents, const idStrList &classNames );
162  // write file with a trace going through a leak
163  void LeakFile( const idStr &fileName );
164  // try to merge portals
165  void MergePortals( int skipContents );
166  // try to merge the two leaf nodes at either side of the portal
167  bool TryMergeLeafNodes( idBrushBSPPortal *portal, int side );
168  void PruneMergedTree_r( idBrushBSPNode *node );
169  // melt portal windings
170  void MeltPortals( int skipContents );
171  // write a map file with a brush for every leaf node that has the given contents
172  void WriteBrushMap( const idStr &fileName, const idStr &ext, int contents );
173  // bounds for the whole tree
174  const idBounds & GetTreeBounds( void ) const { return treeBounds; }
175  // root node of the tree
176  idBrushBSPNode * GetRootNode( void ) const { return root; }
177 
178 private:
196 
199 
200 private:
202  void Free_r( idBrushBSPNode *node );
203  void IncreaseNumSplits( void );
204  bool IsValidSplitter( const idBrushSide *side );
205  int BrushSplitterStats( const idBrush *brush, int planeNum, const idPlaneSet &planeList, bool *testedPlanes, struct splitterStats_s &stats );
206  int FindSplitter( idBrushBSPNode *node, const idPlaneSet &planeList, bool *testedPlanes, struct splitterStats_s &bestStats );
207  void SetSplitterUsed( idBrushBSPNode *node, int planeNum );
208  idBrushBSPNode * BuildBrushBSP_r( idBrushBSPNode *node, const idPlaneSet &planeList, bool *testedPlanes, int skipContents );
209  idBrushBSPNode * ProcessGridCell( idBrushBSPNode *node, int skipContents );
210  void BuildGrid_r( idList<idBrushBSPNode *> &gridCells, idBrushBSPNode *node );
211  void PruneTree_r( idBrushBSPNode *node, int contents );
212  void MakeOutsidePortals( void );
214  void MakeNodePortal( idBrushBSPNode *node );
215  void SplitNodePortals( idBrushBSPNode *node );
216  void MakeTreePortals_r( idBrushBSPNode *node );
217  void FloodThroughPortals_r( idBrushBSPNode *node, int contents, int depth );
218  bool FloodFromOrigin( const idVec3 &origin, int contents );
219  bool FloodFromEntities( const idMapFile *mapFile, int contents, const idStrList &classNames );
220  void RemoveOutside_r( idBrushBSPNode *node, int contents );
221  void SetPortalPlanes_r( idBrushBSPNode *node, idPlaneSet &planeList );
222  void SetPortalPlanes( void );
223  void MergePortals_r( idBrushBSPNode *node, int skipContents );
224  void MergeLeafNodePortals( idBrushBSPNode *node, int skipContents );
225  void UpdateTreeAfterMerge_r( idBrushBSPNode *node, const idBounds &bounds, idBrushBSPNode *oldNode, idBrushBSPNode *newNode );
227  void RemoveColinearPoints_r( idBrushBSPNode *node, int skipContents );
228  void MeltFlood_r( idBrushBSPNode *node, int skipContents, idBounds &bounds, idVectorSet<idVec3,3> &vertexList );
229  void MeltLeafNodePortals( idBrushBSPNode *node, int skipContents, idVectorSet<idVec3,3> &vertexList );
230  void MeltPortals_r( idBrushBSPNode *node, int skipContents, idVectorSet<idVec3,3> &vertexList );
231 };
232 
233 #endif /* !__BRUSHBSP_H__ */
bool(* BrushChopAllowed)(idBrush *b1, idBrush *b2)
Definition: BrushBSP.h:197
void MergePortals(int skipContents)
Definition: BrushBSP.cpp:1813
bool(* BrushMergeAllowed)(idBrush *b1, idBrush *b2)
Definition: BrushBSP.h:198
int GetContents(void) const
Definition: BrushBSP.h:106
void RemoveFlag(int flag)
Definition: BrushBSP.h:69
idVec3 leakOrigin
Definition: BrushBSP.h:193
void MeltPortals(int skipContents)
Definition: BrushBSP.cpp:2142
void IncreaseNumSplits(void)
bool RemoveOutside(const idMapFile *mapFile, int contents, const idStrList &classNames)
Definition: BrushBSP.cpp:1591
void FloodThroughPortals_r(idBrushBSPNode *node, int contents, int depth)
Definition: BrushBSP.cpp:1417
int numMergedPortals
Definition: BrushBSP.h:191
int FindSplitter(idBrushBSPNode *node, const idPlaneSet &planeList, bool *testedPlanes, struct splitterStats_s &bestStats)
Definition: BrushBSP.cpp:677
void MergePortals_r(idBrushBSPNode *node, int skipContents)
Definition: BrushBSP.cpp:1789
idBrushList brushList
Definition: BrushBSP.h:131
const idPlane & GetPlane(void) const
Definition: BrushBSP.h:64
idWinding * winding
Definition: BrushBSP.h:76
void MakeTreePortals_r(idBrushBSPNode *node)
Definition: BrushBSP.cpp:1255
void SetSplitterUsed(idBrushBSPNode *node, int planeNum)
Definition: BrushBSP.cpp:760
const idPlane & GetPlane(void) const
Definition: BrushBSP.h:107
Definition: Vector.h:316
int BrushSplitterStats(const idBrush *brush, int planeNum, const idPlaneSet &planeList, bool *testedPlanes, struct splitterStats_s &stats)
Definition: BrushBSP.cpp:589
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glext.h:2878
int PlaneSide(const idPlane &plane, float epsilon=ON_EPSILON) const
Definition: BrushBSP.cpp:399
bool TryMergeLeafNodes(idBrushBSPPortal *portal, int side)
Definition: BrushBSP.cpp:1894
idBrushBSPPortal * next[2]
Definition: BrushBSP.h:78
void SetFaceNum(int num)
Definition: BrushBSP.h:65
idBrushBSP(void)
Definition: BrushBSP.cpp:501
void WriteBrushMap(const idStr &fileName, const idStr &ext, int contents)
Definition: BrushBSP.cpp:1014
void SplitNodePortals(idBrushBSPNode *node)
Definition: BrushBSP.cpp:1162
void SetPortalPlanes(void)
Definition: BrushBSP.cpp:1641
idPlaneSet portalPlanes
Definition: BrushBSP.h:182
int GetFlags(void) const
Definition: BrushBSP.h:111
GLuint GLuint num
Definition: glext.h:5390
idBrushBSPNode * GetRootNode(void) const
Definition: BrushBSP.h:176
void RemoveFlagRecurse(int flag)
Definition: BrushBSP.cpp:459
idBrushBSPNode * nodes[2]
Definition: BrushBSP.h:77
list l
Definition: prepare.py:17
void SetContents(int contents)
Definition: BrushBSP.h:105
idBrushBSPNode * root
Definition: BrushBSP.h:179
idBrushBSPPortal * portals
Definition: BrushBSP.h:134
void MergeLeafNodePortals(idBrushBSPNode *node, int skipContents)
Definition: BrushBSP.cpp:1650
int GetAreaNum(void) const
Definition: BrushBSP.h:110
int solidLeafNodes
Definition: BrushBSP.h:188
idBrush * volume
Definition: BrushBSP.h:129
int outsideLeafNodes
Definition: BrushBSP.h:189
bool FloodFromEntities(const idMapFile *mapFile, int contents, const idStrList &classNames)
Definition: BrushBSP.cpp:1499
int numPortals
Definition: BrushBSP.h:187
void SetFlag(int flag)
Definition: BrushBSP.h:68
int GetFaceNum(void) const
Definition: BrushBSP.h:66
int numPrunedSplits
Definition: BrushBSP.h:186
GLuint index
Definition: glext.h:3476
idBrushBSPNode * GetNode(int side) const
Definition: BrushBSP.h:71
bool TestLeafNode(void)
Definition: BrushBSP.cpp:292
bool IsValidSplitter(const idBrushSide *side)
Definition: BrushBSP.cpp:572
void RemoveColinearPoints_r(idBrushBSPNode *node, int skipContents)
Definition: BrushBSP.cpp:2119
int numSplits
Definition: BrushBSP.h:184
void Portalize(void)
Definition: BrushBSP.cpp:1346
void MeltFlood_r(idBrushBSPNode *node, int skipContents, idBounds &bounds, idVectorSet< idVec3, 3 > &vertexList)
Definition: BrushBSP.cpp:1988
int insideLeafNodes
Definition: BrushBSP.h:190
~idBrushBSPPortal(void)
Definition: BrushBSP.cpp:72
Definition: Plane.h:71
idBrushBSPNode(void)
Definition: BrushBSP.cpp:216
void UpdateTreeAfterMerge_r(idBrushBSPNode *node, const idBounds &bounds, idBrushBSPNode *oldNode, idBrushBSPNode *newNode)
Definition: BrushBSP.cpp:1856
void RemoveOutside_r(idBrushBSPNode *node, int contents)
Definition: BrushBSP.cpp:1560
void SetPortalPlanes_r(idBrushBSPNode *node, idPlaneSet &planeList)
Definition: BrushBSP.cpp:1616
~idBrushBSPNode(void)
Definition: BrushBSP.cpp:232
void RemoveFlagFlood(int flag)
Definition: BrushBSP.cpp:437
void LeakFile(const idStr &fileName)
Definition: BrushBSP.cpp:1363
int brushMapContents
Definition: BrushBSP.h:194
int numGridCellSplits
Definition: BrushBSP.h:185
idBrushBSPNode * children[2]
Definition: BrushBSP.h:133
void MakeNodePortal(idBrushBSPNode *node)
Definition: BrushBSP.cpp:1116
void Flip(void)
Definition: BrushBSP.cpp:149
void RemoveLeafNodeColinearPoints(idBrushBSPNode *node)
Definition: BrushBSP.cpp:2103
idBrushMap * brushMap
Definition: BrushBSP.h:195
bool FloodFromOrigin(const idVec3 &origin, int contents)
Definition: BrushBSP.cpp:1458
void BuildGrid_r(idList< idBrushBSPNode * > &gridCells, idBrushBSPNode *node)
Definition: BrushBSP.cpp:900
#define ON_EPSILON
Definition: Plane.h:44
int numInsertedPoints
Definition: BrushBSP.h:192
idBrushBSPNode * GetChild(int index) const
Definition: BrushBSP.h:103
idBrushBSPNode * outside
Definition: BrushBSP.h:180
idWinding * BaseWindingForNode(idBrushBSPNode *node)
Definition: BrushBSP.cpp:1085
idBrushBSPNode * parent
Definition: BrushBSP.h:132
void SetAreaNum(int num)
Definition: BrushBSP.h:109
const idBounds & GetTreeBounds(void) const
Definition: BrushBSP.h:174
Definition: Str.h:116
idBrushBSPPortal * GetPortals(void) const
Definition: BrushBSP.h:108
idBounds GetPortalBounds(void)
Definition: BrushBSP.cpp:271
idBrushBSPNode * ProcessGridCell(idBrushBSPNode *node, int skipContents)
Definition: BrushBSP.cpp:858
void SetFlag(int flag)
Definition: BrushBSP.h:112
idPlane plane
Definition: BrushBSP.h:128
int GetFlags(void) const
Definition: BrushBSP.h:67
idWinding * GetWinding(void) const
Definition: BrushBSP.h:63
void Build(idBrushList brushList, int skipContents, bool(*ChopAllowed)(idBrush *b1, idBrush *b2), bool(*MergeAllowed)(idBrush *b1, idBrush *b2))
Definition: BrushBSP.cpp:964
unsigned char bool
Definition: setup.h:74
idBrushBSPPortal * Next(int side) const
Definition: BrushBSP.h:70
void RemoveFlagRecurseFlood(int flag)
Definition: BrushBSP.cpp:474
void SetContentsFromBrushes(void)
Definition: BrushBSP.cpp:257
idPlane plane
Definition: BrushBSP.h:74
int Split(const idPlane &splitPlane, idBrushBSPPortal **front, idBrushBSPPortal **back)
Definition: BrushBSP.cpp:173
idBrushBSPPortal(void)
Definition: BrushBSP.cpp:58
void RemoveMultipleLeafNodeReferences_r(idBrushBSPNode *node)
Definition: BrushBSP.cpp:528
void MeltPortals_r(idBrushBSPNode *node, int skipContents, idVectorSet< idVec3, 3 > &vertexList)
Definition: BrushBSP.cpp:2080
idBrushBSPNode * BuildBrushBSP_r(idBrushBSPNode *node, const idPlaneSet &planeList, bool *testedPlanes, int skipContents)
Definition: BrushBSP.cpp:789
bool MergeAllowed(idBrush *b1, idBrush *b2)
Definition: AASBuild.cpp:597
void RemoveFromNode(idBrushBSPNode *l)
Definition: BrushBSP.cpp:104
idBrushBSPNode * GetParent(void) const
Definition: BrushBSP.h:104
void MakeOutsidePortals(void)
Definition: BrushBSP.cpp:1291
Definition: Brush.h:98
idBounds treeBounds
Definition: BrushBSP.h:181
void AddToNodes(idBrushBSPNode *front, idBrushBSPNode *back)
Definition: BrushBSP.cpp:83
void PruneTree(int contents)
Definition: BrushBSP.cpp:1071
void RemoveFlag(int flag)
Definition: BrushBSP.h:113
~idBrushBSP(void)
Definition: BrushBSP.cpp:513
void Free_r(idBrushBSPNode *node)
Definition: BrushBSP.cpp:556
void MeltLeafNodePortals(idBrushBSPNode *node, int skipContents, idVectorSet< idVec3, 3 > &vertexList)
Definition: BrushBSP.cpp:2039
bool Split(const idPlane &splitPlane, int splitPlaneNum)
Definition: BrushBSP.cpp:330
int numGridCells
Definition: BrushBSP.h:183
void PruneTree_r(idBrushBSPNode *node, int contents)
Definition: BrushBSP.cpp:1024
void PruneMergedTree_r(idBrushBSPNode *node)
Definition: BrushBSP.cpp:1826