Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

400 lines
17 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. //
  3. // Class to represent and write out a BSP file.
  4. //
  5. //===============================================================================
  6. #ifndef SIMPLEBSPFILE_H
  7. #define SIMPLEBSPFILE_H
  8. #if defined( COMPILER_MSVC )
  9. #pragma once
  10. #endif
  11. #include "bitvec.h"
  12. #include "simplemapfile.h"
  13. #include "vbspmathutil.h"
  14. class CSimpleMapFile;
  15. struct MapEntity_t;
  16. //-----------------------------------------------------------------------------
  17. // Indicates that the BSP node is a leaf (m_pChildren will be all NULL)
  18. //-----------------------------------------------------------------------------
  19. static const int LEAF_PLANE_INDEX = -1;
  20. //-----------------------------------------------------------------------------
  21. // A sentinel cluster index used to indicate that a node does not
  22. // belong to any cluster.
  23. //-----------------------------------------------------------------------------
  24. static const int INVALID_CLUSTER_INDEX = -1;
  25. //-----------------------------------------------------------------------------
  26. // Default maximum visibility radius used to compute cluster-to-cluster
  27. // visibility.
  28. //-----------------------------------------------------------------------------
  29. static const float DEFAULT_VISIBILITY_RADIUS = 2500.0f;
  30. class CBSPBrush;
  31. class CBSPFace;
  32. class CBSPPortal;
  33. //-----------------------------------------------------------------------------
  34. // A node in a BSP tree.
  35. //-----------------------------------------------------------------------------
  36. class CBSPNode
  37. {
  38. public:
  39. CBSPNode();
  40. ~CBSPNode();
  41. bool IsLeaf() const { return m_nSplitPlaneIndex == LEAF_PLANE_INDEX; }
  42. CBSPNode *m_pParent;
  43. CBSPNode *m_pChildren[2];
  44. // If this node is a leaf, the value is LEAF_PLANE_INDEX.
  45. // Otherwise, the split plane is always the "positive" plane of a pair (that is, even numbered).
  46. int m_nSplitPlaneIndex;
  47. // Flags indicating the contents of the BSP node.
  48. MapBrushContentsFlags_t m_ContentsFlags;
  49. // This field owns the contents' memory
  50. CUtlVector< CBSPBrush * > m_ClippedBrushes;
  51. // A list of portal faces or detail faces on this node.
  52. // Leaf nodes may contain only detail faces, non-leaf nodes may contain only portal faces.
  53. // This field does NOT own the contents' memory; the faces are owned by the containing model or the portal
  54. // (depending on what kind of face it is).
  55. CUtlVector< CBSPFace * > m_Faces;
  56. // This field does NOT own the contents' memory; the portals are owned by the containing model.
  57. CUtlVector< CBSPPortal * > m_Portals;
  58. // AABB around the node
  59. Vector m_vMinBounds, m_vMaxBounds;
  60. // True if an entity can reach this node via portals, False if unreachable
  61. bool m_bEntityCanReach;
  62. // The BSP cluster to which this leaf node belongs (only applies to the world BSP tree).
  63. // A cluster is simply a group of leaves which share visibility information.
  64. // In practice, there is 1 cluster per non-solid world leaf.
  65. // This value is ignored for non-leaf nodes.
  66. int m_nClusterIndex;
  67. private:
  68. // Disallow value semantics
  69. CBSPNode( const CBSPNode &other );
  70. CBSPNode &operator=( const CBSPNode &other );
  71. };
  72. //-----------------------------------------------------------------------------
  73. // A binary space partition tree.
  74. //-----------------------------------------------------------------------------
  75. class CBSPTree
  76. {
  77. public:
  78. CBSPTree();
  79. ~CBSPTree();
  80. CBSPNode *m_pRoot;
  81. CBSPNode m_OutsideNode; // a sentinel node which exists in the space outside of the BSP tree
  82. // AABB around the tree
  83. Vector m_vMinBounds, m_vMaxBounds;
  84. private:
  85. // Disallow value semantics
  86. CBSPTree( const CBSPTree &other );
  87. CBSPTree &operator=( const CBSPTree &other );
  88. };
  89. //-----------------------------------------------------------------------------
  90. // A portal between two nodes of a BSP tree.
  91. //-----------------------------------------------------------------------------
  92. class CBSPPortal
  93. {
  94. public:
  95. //-----------------------------------------------------------------------------
  96. // Constructs a default, null portal.
  97. //-----------------------------------------------------------------------------
  98. CBSPPortal();
  99. //-----------------------------------------------------------------------------
  100. // Constructs a new portal along the same plane as the given portal,
  101. // except with a different shape.
  102. // This is used when a single portal is cut by a node's splitting plane
  103. // into two new portals along the same plane.
  104. //
  105. // This constructor swaps its polygon's points with the given polygon's
  106. // for efficiency!
  107. //-----------------------------------------------------------------------------
  108. CBSPPortal( const CBSPPortal &other, Polygon_t *pPortalShape );
  109. ~CBSPPortal();
  110. CBSPNode *GetOtherNode( const CBSPNode *pNode ) const;
  111. //-----------------------------------------------------------------------------
  112. // Returns either 0 or 1 to indicate which node attached to this portal
  113. // is responsible for drawing the face.
  114. // This call is only valid if m_PortalFaces.Count() > 0.
  115. //-----------------------------------------------------------------------------
  116. int GetNodeIndexForFace() const;
  117. // The nodes on either side of the portal.
  118. // The convention is that the plane of the portal faces in the direction from node 1 towards node 0.
  119. // (That is, node 0 is in the positive half-space of the plane while node 1 is in the the negative half-space).
  120. // In a properly built tree, these nodes are always leaves (during construction they may
  121. // point to interior nodes).
  122. CBSPNode *m_pNodes[2];
  123. // Not NULL if this portal lives on a node's split plane (which is usually the case, except for
  124. // portals at the edge of the world). This node is never a leaf.
  125. CBSPNode *m_pOnNode;
  126. // Shape of the portal.
  127. Polygon_t m_Polygon;
  128. // Plane along which the portal lives.
  129. Plane_t m_Plane;
  130. // These are the faces generated when the portal is in-between solid and non-solid contents.
  131. // Logically, there is only 1 convex face, but some constraints (e.g. max lightmap size per face)
  132. // can require subdivision of faces.
  133. // This class owns the faces and frees them on destruction.
  134. CUtlVector< CBSPFace * > m_PortalFaces;
  135. private:
  136. // Disallow value semantics
  137. CBSPPortal( const CBSPPortal &other );
  138. CBSPPortal &operator=( const CBSPPortal &other );
  139. };
  140. //-----------------------------------------------------------------------------
  141. // The side of a BSP brush (convex solid).
  142. // This class is copyable.
  143. //-----------------------------------------------------------------------------
  144. class CBSPBrushSide
  145. {
  146. public:
  147. //-----------------------------------------------------------------------------
  148. // Constructs a default, invalid brush side.
  149. //-----------------------------------------------------------------------------
  150. CBSPBrushSide();
  151. //-----------------------------------------------------------------------------
  152. // Constructs a new brush side initialized from map data.
  153. //-----------------------------------------------------------------------------
  154. CBSPBrushSide( const MapBrushSide_t *pMapBrushSide );
  155. // Index into BSP creation context's plane list
  156. int m_nPlaneIndex;
  157. // Index into map's texture infos array
  158. int m_nTextureInfoIndex;
  159. MapBrushSideSurfaceFlags_t m_SurfaceFlags;
  160. Polygon_t m_Polygon;
  161. };
  162. //-----------------------------------------------------------------------------
  163. // A convex solid in a BSP file, produced from CSG operations on brushes
  164. // found in the original map.
  165. // This class is copyable.
  166. //-----------------------------------------------------------------------------
  167. class CBSPBrush
  168. {
  169. public:
  170. //-----------------------------------------------------------------------------
  171. // Constructs a default brush solid with no sides.
  172. //-----------------------------------------------------------------------------
  173. CBSPBrush();
  174. //-----------------------------------------------------------------------------
  175. // Constructs a new default brush solid initialized from map data.
  176. //-----------------------------------------------------------------------------
  177. CBSPBrush( const MapBrush_t *pMapBrush, const MapBrushSide_t *pMapBrushSides );
  178. Vector m_vMinBounds, m_vMaxBounds;
  179. CCopyableUtlVector< CBSPBrushSide > m_Sides;
  180. MapBrushContentsFlags_t m_ContentsFlags;
  181. const MapBrush_t *m_pOriginalBrush;
  182. // Which side of the node splitting plane this brush is on.
  183. // (both values only used during BSP construction in order to split nodes).
  184. PlaneSide_t m_nTempSplitSide;
  185. PlaneSide_t m_nSplitSide;
  186. };
  187. //-----------------------------------------------------------------------------
  188. // A renderable face associated with the side of a detail brush or a portal.
  189. // This class is copyable.
  190. //-----------------------------------------------------------------------------
  191. class CBSPFace
  192. {
  193. public:
  194. //-----------------------------------------------------------------------------
  195. // Constructs a default, empty face.
  196. //-----------------------------------------------------------------------------
  197. CBSPFace();
  198. //-----------------------------------------------------------------------------
  199. // Constructs a face from the side of a BSP brush solid.
  200. //-----------------------------------------------------------------------------
  201. CBSPFace( const CBSPBrush *pBrush, const CBSPBrushSide *pSide );
  202. //-----------------------------------------------------------------------------
  203. // Constructs a face from another face, except with a different shape.
  204. // Useful when cutting a face into parts.
  205. //
  206. // This constructor swaps its polygon's points with the given polygon's
  207. // for efficiency.
  208. //-----------------------------------------------------------------------------
  209. CBSPFace( const CBSPFace &other, Polygon_t *pPolygon );
  210. // Index into map's texture infos array
  211. int m_nTextureInfoIndex;
  212. // Index into BSP creation context's plane list
  213. int m_nPlaneIndex;
  214. Polygon_t m_Polygon;
  215. // Index into the map's displacement array, if this face comes from a displacement surface.
  216. int m_nDisplacementIndex;
  217. // The index of this face in the final output.
  218. // All faces in a BSP file appear sequentially by model.
  219. // Within a model's contiguous list of faces, portal faces are serialized first
  220. // via depth-first traversal of the BSP tree, followed by detail faces.
  221. int m_nSerializedFaceIndex;
  222. };
  223. //-----------------------------------------------------------------------------
  224. // An entity in a map with associated CSG brush data.
  225. // The 0th model in a BSP file corresponds to the world entity of the map
  226. // and includes all detail brushes from detail entities in the original map.
  227. //-----------------------------------------------------------------------------
  228. class CBSPModel
  229. {
  230. public:
  231. CBSPModel();
  232. ~CBSPModel();
  233. CBSPTree *m_pTree;
  234. // Detail faces are owned by the model and are freed on destruction.
  235. CUtlVector< CBSPFace * > m_DetailFaces;
  236. // Portals are owned by the model and are freed on destruction.
  237. CUtlVector< CBSPPortal * > m_Portals;
  238. Vector m_vMinBounds, m_vMaxBounds;
  239. private:
  240. // Disallow value semantics
  241. CBSPModel( const CBSPModel &other );
  242. CBSPModel &operator=( const CBSPModel &other );
  243. };
  244. //-----------------------------------------------------------------------------
  245. // In theory, a cluster is a set of BSP leaf nodes.
  246. // In practice, it is simply a pointer to a single, existing BSP leaf node.
  247. //-----------------------------------------------------------------------------
  248. struct BSPCluster_t
  249. {
  250. const CBSPNode *m_pLeafNode;
  251. };
  252. //-----------------------------------------------------------------------------
  253. // An in-memory representation of a BSP file, built from a map file.
  254. // This class requires that the map file remain valid for its lifetime,
  255. // though the map file is accessed in a read-only manner.
  256. //-----------------------------------------------------------------------------
  257. class CSimpleBSPFile
  258. {
  259. public:
  260. CSimpleBSPFile();
  261. ~CSimpleBSPFile();
  262. const CSimpleMapFile *GetOriginalMap() const { return m_pMapFile; }
  263. const CPlaneHash *GetPlaneHash() const { return &m_PlaneHash; }
  264. const CBSPModel * const *GetModels() const { return m_Models.Base(); }
  265. int GetModelCount() const { return m_Models.Count(); }
  266. const CBSPFace * GetDisplacementFaces() const { return m_DisplacementFaces.Base(); }
  267. int GetDisplacementFaceCount() const { return m_DisplacementFaces.Count(); }
  268. void SetVisibilityRadius( float flRadius ) { m_flVisibilityRadius = flRadius; }
  269. int GetClusterCount() const { return m_Clusters.Count(); }
  270. const byte * GetVisibilityData() const { return m_VisibilityData.Base(); }
  271. //-----------------------------------------------------------------------------
  272. // Populates this in-memory BSP file object from the given in-memory map file.
  273. //-----------------------------------------------------------------------------
  274. void CreateFromMapFile( const CSimpleMapFile *pMapFile );
  275. private:
  276. // Source map file; must remain valid for the lifetime of this object.
  277. const CSimpleMapFile *m_pMapFile;
  278. // Array of planes with acceleration structure for fast lookup.
  279. // Starts off copied from the map (so all map plane indices are valid) but is expanded
  280. // during the BSP process via CSG operations.
  281. CPlaneHash m_PlaneHash;
  282. CUtlVector< CBSPModel * > m_Models;
  283. CUtlVector< CBSPFace > m_DisplacementFaces;
  284. CUtlVector< BSPCluster_t > m_Clusters;
  285. CUtlVector< byte > m_VisibilityData;
  286. // A monotonically increasing value which increments for every face generated during the BSP construction process.
  287. int m_nNextFaceIndex;
  288. // The radius value beyond which clusters cannot see each other.
  289. float m_flVisibilityRadius;
  290. void ProcessWorldEntity( const MapEntity_t *pMapEntity );
  291. void ProcessEntity( const MapEntity_t *pMapEntity );
  292. CBSPNode *GenerateBSPGrid( int nMinX, int nMinY, int nMaxX, int nMaxY, int nAbsoluteMins[2], int nAbsoluteMaxs[2], CBSPNode **ppNodeList );
  293. CBSPNode *CreateGridNode( int nX, int nY );
  294. void CreateBSPBrushList( const MapBrush_t *pBrushes, int nBrushCount, bool bIncludeDetail, bool bIncludeStructural, const Vector &vClipMin, const Vector &vClipMax, CUtlVector< CBSPBrush * > *pBrushList );
  295. CBSPBrush *CreateClippedBrush( const MapBrush_t *pMapBrush, const Vector &vClipMin, const Vector &vClipMax, int nClipMinPlanes[2], int nClipMaxPlanes[2] );
  296. void SplitBrush( CBSPBrush *pBrush, int nPlaneIndex, CBSPBrush **ppFrontBrush, CBSPBrush **ppBackBrush );
  297. PlaneSide_t GetPrimaryPlaneSide( CBSPBrush *pBrush, Plane_t *pPlane );
  298. CBSPTree *BuildBSPTree( CUtlVector< CBSPBrush * > *pBrushList, const Vector &vMin, const Vector &vMax );
  299. void BuildBSPChildren( CBSPNode *pNode, CUtlVector< CBSPBrush * > *pBrushList );
  300. int FindBestSplitPlane( CUtlVector< CBSPBrush * > *pBrushList );
  301. void MakeBrushPolygons( CBSPBrush *pBrush );
  302. void SplitBrushList( CUtlVector< CBSPBrush * > *pBrushList, CBSPNode *pNode, CUtlVector< CBSPBrush * > *pFrontChildList, CUtlVector< CBSPBrush * > *pBackChildList );
  303. PlaneSide_t TestBrushAgainstPlaneIndex( CBSPBrush *pBrush, int nPlaneIndex, int *nNumSplits );
  304. void PruneNodes( CBSPNode *pNode, CUtlVector< CBSPPortal * > *pPortalList );
  305. void BuildTreePortals( CBSPTree *pTree, CUtlVector< CBSPPortal * > *pPortalList );
  306. void BuildNodePortals( CBSPNode *pNode, CUtlVector< CBSPPortal * > *pPortalList );
  307. void FloodEntities( CBSPTree *pTree );
  308. void FloodEntity( CBSPNode *pNode, const Vector &vPosition );
  309. void FloodFillThroughPortals( CBSPNode *pNode );
  310. void MakeUnreachableNodesSolid( CBSPNode *pNode );
  311. void MakeFacesFromPortals( CBSPModel *pModel );
  312. void NumberPortalFaces( CBSPNode *pNode );
  313. void BuildRadialVisibilityData( CBSPNode *pRootNode );
  314. void AssignClusterIndicesToLeaves( CBSPNode *pNode );
  315. void CreateDisplacementFaces();
  316. void PopulateTreeWithDetail( const CBSPTree *pTree, int nFirstBrush, int nNumBrushes, CUtlVector< CBSPFace * > *pDetailFaceList );
  317. void FilterFaceIntoTree( CBSPNode *pNode, CBSPFace *pClippedFace, CBSPFace *pOriginalFace );
  318. void FilterBrushIntoTree( CBSPNode *pNode, CBSPBrush *pBrush );
  319. };
  320. //-----------------------------------------------------------------------------
  321. // Dumps useful information about the BSP to several files beginning
  322. // with the path string pPrefixName and suffixed appropriately
  323. // (e.g. "_ent.txt", "_bsp.csv", "_tex.csv").
  324. //-----------------------------------------------------------------------------
  325. void DumpBSPInfo( const char *pPrefixName, byte *pBSPData, int nBSPDataSize );
  326. //-----------------------------------------------------------------------------
  327. // Writes all faces to a .gl file which can be viewed in GLView.
  328. //-----------------------------------------------------------------------------
  329. void WriteGLBSPFile( FileHandle_t fileHandle, byte *pBSPData, int nBSPDataSize );
  330. //-----------------------------------------------------------------------------
  331. // Dumps the contents of the given lump to the specified file.
  332. //-----------------------------------------------------------------------------
  333. void DumpLump( FileHandle_t fileHandle, byte *pBSPData, int nBSPDataSize, int nLumpIndex );
  334. #endif // SIMPLEBSPFILE_H