Team Fortress 2 Source Code as on 22/4/2020
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.

657 lines
18 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #if !defined( VBSP_H )
  9. #define VBSP_H
  10. #include "cmdlib.h"
  11. #include "mathlib/vector.h"
  12. #include "scriplib.h"
  13. #include "polylib.h"
  14. #include "threads.h"
  15. #include "bsplib.h"
  16. #include "qfiles.h"
  17. #include "utilmatlib.h"
  18. #include "ChunkFile.h"
  19. #ifdef WIN32
  20. #pragma warning( disable: 4706 )
  21. #endif
  22. class CUtlBuffer;
  23. #define MAX_BRUSH_SIDES 128
  24. #define CLIP_EPSILON 0.1
  25. #define TEXINFO_NODE -1 // side is allready on a node
  26. // this will output glview files for the given brushmodel. Brushmodel 1 is the world, 2 is the first brush entity, etc.
  27. #define DEBUG_BRUSHMODEL 0
  28. struct portal_t;
  29. struct node_t;
  30. struct plane_t : public dplane_t
  31. {
  32. plane_t *hash_chain;
  33. plane_t() { normal.Init(); }
  34. };
  35. struct brush_texture_t
  36. {
  37. Vector UAxis;
  38. Vector VAxis;
  39. vec_t shift[2];
  40. vec_t rotate;
  41. vec_t textureWorldUnitsPerTexel[2];
  42. vec_t lightmapWorldUnitsPerLuxel;
  43. char name[TEXTURE_NAME_LENGTH];
  44. int flags;
  45. brush_texture_t() : UAxis(0,0,0), VAxis(0,0,0) {}
  46. };
  47. struct mapdispinfo_t;
  48. struct side_t
  49. {
  50. int planenum;
  51. int texinfo;
  52. mapdispinfo_t *pMapDisp;
  53. winding_t *winding;
  54. side_t *original; // bspbrush_t sides will reference the mapbrush_t sides
  55. int contents; // from miptex
  56. int surf; // from miptex
  57. qboolean visible; // choose visble planes first
  58. qboolean tested; // this plane allready checked as a split
  59. qboolean bevel; // don't ever use for bsp splitting
  60. side_t *next;
  61. int origIndex;
  62. int id; // This is the unique id generated by worldcraft for this side.
  63. unsigned int smoothingGroups;
  64. CUtlVector<int> aOverlayIds; // List of overlays that reside on this side.
  65. CUtlVector<int> aWaterOverlayIds; // List of water overlays that reside on this side.
  66. bool m_bDynamicShadowsEnabled; // Goes into dface_t::SetDynamicShadowsEnabled().
  67. };
  68. struct mapbrush_t
  69. {
  70. int entitynum;
  71. int brushnum;
  72. int id; // The unique ID of this brush in the editor, used for reporting errors.
  73. int contents;
  74. Vector mins, maxs;
  75. int numsides;
  76. side_t *original_sides;
  77. };
  78. #define PLANENUM_LEAF -1
  79. #define MAXEDGES 32
  80. struct face_t
  81. {
  82. int id;
  83. face_t *next; // on node
  84. // the chain of faces off of a node can be merged or split,
  85. // but each face_t along the way will remain in the chain
  86. // until the entire tree is freed
  87. face_t *merged; // if set, this face isn't valid anymore
  88. face_t *split[2]; // if set, this face isn't valid anymore
  89. portal_t *portal;
  90. int texinfo;
  91. int dispinfo;
  92. // This is only for surfaces that are the boundaries of fog volumes
  93. // (ie. water surfaces)
  94. // All of the rest of the surfaces can look at their leaf to find out
  95. // what fog volume they are in.
  96. node_t *fogVolumeLeaf;
  97. int planenum;
  98. int contents; // faces in different contents can't merge
  99. int outputnumber;
  100. winding_t *w;
  101. int numpoints;
  102. qboolean badstartvert; // tjunctions cannot be fixed without a midpoint vertex
  103. int vertexnums[MAXEDGES];
  104. side_t *originalface; // save the "side" this face came from
  105. int firstPrimID;
  106. int numPrims;
  107. unsigned int smoothingGroups;
  108. };
  109. void EmitFace( face_t *f, qboolean onNode );
  110. struct mapdispinfo_t
  111. {
  112. face_t face;
  113. int entitynum;
  114. int power;
  115. int minTess;
  116. float smoothingAngle;
  117. Vector uAxis;
  118. Vector vAxis;
  119. Vector startPosition;
  120. float alphaValues[MAX_DISPVERTS];
  121. float maxDispDist;
  122. float dispDists[MAX_DISPVERTS];
  123. Vector vectorDisps[MAX_DISPVERTS];
  124. Vector vectorOffsets[MAX_DISPVERTS];
  125. int contents;
  126. int brushSideID;
  127. unsigned short triTags[MAX_DISPTRIS];
  128. int flags;
  129. #ifdef VSVMFIO
  130. float m_elevation; // "elevation"
  131. Vector m_offsetNormals[ MAX_DISPTRIS ]; // "offset_normals"
  132. #endif // VSVMFIO
  133. };
  134. extern int nummapdispinfo;
  135. extern mapdispinfo_t mapdispinfo[MAX_MAP_DISPINFO];
  136. extern float g_defaultLuxelSize;
  137. extern float g_luxelScale;
  138. extern float g_minLuxelScale;
  139. extern bool g_BumpAll;
  140. extern int g_nDXLevel;
  141. int GetDispInfoEntityNum( mapdispinfo_t *pDisp );
  142. void ComputeBoundsNoSkybox( );
  143. struct bspbrush_t
  144. {
  145. int id;
  146. bspbrush_t *next;
  147. Vector mins, maxs;
  148. int side, testside; // side of node during construction
  149. mapbrush_t *original;
  150. int numsides;
  151. side_t sides[6]; // variably sized
  152. };
  153. #define MAX_NODE_BRUSHES 8
  154. struct leafface_t
  155. {
  156. face_t *pFace;
  157. leafface_t *pNext;
  158. };
  159. struct node_t
  160. {
  161. int id;
  162. // both leafs and nodes
  163. int planenum; // -1 = leaf node
  164. node_t *parent;
  165. Vector mins, maxs; // valid after portalization
  166. bspbrush_t *volume; // one for each leaf/node
  167. // nodes only
  168. side_t *side; // the side that created the node
  169. node_t *children[2];
  170. face_t *faces; // these are the cutup ones that live in the plane of "side".
  171. // leafs only
  172. bspbrush_t *brushlist; // fragments of all brushes in this leaf
  173. leafface_t *leaffacelist;
  174. int contents; // OR of all brush contents
  175. int occupied; // 1 or greater can reach entity
  176. entity_t *occupant; // for leak file testing
  177. int cluster; // for portalfile writing
  178. int area; // for areaportals
  179. portal_t *portals; // also on nodes during construction
  180. int diskId; // dnodes or dleafs index after this has been emitted
  181. };
  182. struct portal_t
  183. {
  184. int id;
  185. plane_t plane;
  186. node_t *onnode; // NULL = outside box
  187. node_t *nodes[2]; // [0] = front side of plane
  188. portal_t *next[2];
  189. winding_t *winding;
  190. qboolean sidefound; // false if ->side hasn't been checked
  191. side_t *side; // NULL = non-visible
  192. face_t *face[2]; // output face in bsp file
  193. };
  194. struct tree_t
  195. {
  196. node_t *headnode;
  197. node_t outside_node;
  198. Vector mins, maxs;
  199. bool leaked;
  200. };
  201. extern int entity_num;
  202. struct LoadSide_t;
  203. struct LoadEntity_t;
  204. class CManifest;
  205. class CMapFile
  206. {
  207. public:
  208. CMapFile( void ) { Init(); }
  209. void Init( void );
  210. void AddPlaneToHash (plane_t *p);
  211. int CreateNewFloatPlane (Vector& normal, vec_t dist);
  212. int FindFloatPlane (Vector& normal, vec_t dist);
  213. int PlaneFromPoints(const Vector &p0, const Vector &p1, const Vector &p2);
  214. void AddBrushBevels (mapbrush_t *b);
  215. qboolean MakeBrushWindings (mapbrush_t *ob);
  216. void MoveBrushesToWorld( entity_t *mapent );
  217. void MoveBrushesToWorldGeneral( entity_t *mapent );
  218. void RemoveContentsDetailFromEntity( entity_t *mapent );
  219. int SideIDToIndex( int brushSideID );
  220. void AddLadderKeys( entity_t *mapent );
  221. ChunkFileResult_t LoadEntityCallback(CChunkFile *pFile, int nParam);
  222. void ForceFuncAreaPortalWindowContents();
  223. ChunkFileResult_t LoadSideCallback(CChunkFile *pFile, LoadSide_t *pSideInfo);
  224. ChunkFileResult_t LoadConnectionsKeyCallback(const char *szKey, const char *szValue, LoadEntity_t *pLoadEntity);
  225. ChunkFileResult_t LoadSolidCallback(CChunkFile *pFile, LoadEntity_t *pLoadEntity);
  226. void TestExpandBrushes(void);
  227. static char m_InstancePath[ MAX_PATH ];
  228. static void SetInstancePath( const char *pszInstancePath );
  229. static const char *GetInstancePath( void ) { return m_InstancePath; }
  230. static bool DeterminePath( const char *pszBaseFileName, const char *pszInstanceFileName, char *pszOutFileName );
  231. void CheckForInstances( const char *pszFileName );
  232. void MergeInstance( entity_t *pInstanceEntity, CMapFile *Instance );
  233. void MergePlanes( entity_t *pInstanceEntity, CMapFile *Instance, Vector &InstanceOrigin, QAngle &InstanceAngle, matrix3x4_t &InstanceMatrix );
  234. void MergeBrushes( entity_t *pInstanceEntity, CMapFile *Instance, Vector &InstanceOrigin, QAngle &InstanceAngle, matrix3x4_t &InstanceMatrix );
  235. void MergeBrushSides( entity_t *pInstanceEntity, CMapFile *Instance, Vector &InstanceOrigin, QAngle &InstanceAngle, matrix3x4_t &InstanceMatrix );
  236. void ReplaceInstancePair( epair_t *pPair, entity_t *pInstanceEntity );
  237. void MergeEntities( entity_t *pInstanceEntity, CMapFile *Instance, Vector &InstanceOrigin, QAngle &InstanceAngle, matrix3x4_t &InstanceMatrix );
  238. void MergeOverlays( entity_t *pInstanceEntity, CMapFile *Instance, Vector &InstanceOrigin, QAngle &InstanceAngle, matrix3x4_t &InstanceMatrix );
  239. static int m_InstanceCount;
  240. static int c_areaportals;
  241. plane_t mapplanes[MAX_MAP_PLANES];
  242. int nummapplanes;
  243. #define PLANE_HASHES 1024
  244. plane_t *planehash[PLANE_HASHES];
  245. int nummapbrushes;
  246. mapbrush_t mapbrushes[MAX_MAP_BRUSHES];
  247. Vector map_mins, map_maxs;
  248. int nummapbrushsides;
  249. side_t brushsides[MAX_MAP_BRUSHSIDES];
  250. brush_texture_t side_brushtextures[MAX_MAP_BRUSHSIDES];
  251. int num_entities;
  252. entity_t entities[MAX_MAP_ENTITIES];
  253. int c_boxbevels;
  254. int c_edgebevels;
  255. int c_clipbrushes;
  256. int g_ClipTexinfo;
  257. class CConnectionPairs
  258. {
  259. public:
  260. CConnectionPairs( epair_t *pair, CConnectionPairs *next )
  261. {
  262. m_Pair = pair;
  263. m_Next = next;
  264. }
  265. epair_t *m_Pair;
  266. CConnectionPairs *m_Next;
  267. };
  268. CConnectionPairs *m_ConnectionPairs;
  269. int m_StartMapOverlays;
  270. int m_StartMapWaterOverlays;
  271. };
  272. extern CMapFile *g_MainMap;
  273. extern CMapFile *g_LoadingMap;
  274. extern CUtlVector< CMapFile * > g_Maps;
  275. extern int g_nMapFileVersion;
  276. extern qboolean noprune;
  277. extern qboolean nodetail;
  278. extern qboolean fulldetail;
  279. extern qboolean nomerge;
  280. extern qboolean nomergewater;
  281. extern qboolean nosubdiv;
  282. extern qboolean nowater;
  283. extern qboolean noweld;
  284. extern qboolean noshare;
  285. extern qboolean notjunc;
  286. extern qboolean nocsg;
  287. extern qboolean noopt;
  288. extern qboolean dumpcollide;
  289. extern qboolean nodetailcuts;
  290. extern qboolean g_DumpStaticProps;
  291. extern qboolean g_bSkyVis;
  292. extern vec_t microvolume;
  293. extern bool g_snapAxialPlanes;
  294. extern bool g_NodrawTriggers;
  295. extern bool g_DisableWaterLighting;
  296. extern bool g_bAllowDetailCracks;
  297. extern bool g_bNoVirtualMesh;
  298. extern char outbase[32];
  299. extern char source[1024];
  300. extern char mapbase[ 64 ];
  301. extern CUtlVector<int> g_SkyAreas;
  302. bool LoadMapFile( const char *pszFileName );
  303. int GetVertexnum( Vector& v );
  304. bool Is3DSkyboxArea( int area );
  305. //=============================================================================
  306. // textures.c
  307. struct textureref_t
  308. {
  309. char name[TEXTURE_NAME_LENGTH];
  310. int flags;
  311. float lightmapWorldUnitsPerLuxel;
  312. int contents;
  313. };
  314. extern textureref_t textureref[MAX_MAP_TEXTURES];
  315. int FindMiptex (const char *name);
  316. int TexinfoForBrushTexture (plane_t *plane, brush_texture_t *bt, const Vector& origin);
  317. int GetSurfaceProperties2( MaterialSystemMaterial_t matID, const char *pMatName );
  318. extern int g_SurfaceProperties[MAX_MAP_TEXDATA];
  319. void LoadSurfaceProperties( void );
  320. int PointLeafnum ( dmodel_t* pModel, const Vector& p );
  321. //=============================================================================
  322. void FindGCD (int *v);
  323. mapbrush_t *Brush_LoadEntity (entity_t *ent);
  324. int PlaneTypeForNormal (Vector& normal);
  325. qboolean MakeBrushPlanes (mapbrush_t *b);
  326. int FindIntPlane (int *inormal, int *iorigin);
  327. void CreateBrush (int brushnum);
  328. //=============================================================================
  329. // detail objects
  330. //=============================================================================
  331. void LoadEmitDetailObjectDictionary( char const* pGameDir );
  332. void EmitDetailObjects();
  333. //=============================================================================
  334. // static props
  335. //=============================================================================
  336. void EmitStaticProps();
  337. bool LoadStudioModel( char const* pFileName, char const* pEntityType, CUtlBuffer& buf );
  338. //=============================================================================
  339. //=============================================================================
  340. // procedurally created .vmt files
  341. //=============================================================================
  342. void EmitStaticProps();
  343. // draw.c
  344. extern Vector draw_mins, draw_maxs;
  345. extern bool g_bLightIfMissing;
  346. void Draw_ClearWindow (void);
  347. void DrawWinding (winding_t *w);
  348. void GLS_BeginScene (void);
  349. void GLS_Winding (winding_t *w, int code);
  350. void GLS_EndScene (void);
  351. //=============================================================================
  352. // csg
  353. enum detailscreen_e
  354. {
  355. FULL_DETAIL = 0,
  356. ONLY_DETAIL = 1,
  357. NO_DETAIL = 2,
  358. };
  359. #define TRANSPARENT_CONTENTS (CONTENTS_GRATE|CONTENTS_WINDOW)
  360. #include "csg.h"
  361. //=============================================================================
  362. // brushbsp
  363. void WriteBrushList (char *name, bspbrush_t *brush, qboolean onlyvis);
  364. bspbrush_t *CopyBrush (bspbrush_t *brush);
  365. void SplitBrush (bspbrush_t *brush, int planenum,
  366. bspbrush_t **front, bspbrush_t **back);
  367. tree_t *AllocTree (void);
  368. node_t *AllocNode (void);
  369. bspbrush_t *AllocBrush (int numsides);
  370. int CountBrushList (bspbrush_t *brushes);
  371. void FreeBrush (bspbrush_t *brushes);
  372. vec_t BrushVolume (bspbrush_t *brush);
  373. node_t *NodeForPoint (node_t *node, Vector& origin);
  374. void BoundBrush (bspbrush_t *brush);
  375. void FreeBrushList (bspbrush_t *brushes);
  376. node_t *PointInLeaf (node_t *node, Vector& point);
  377. tree_t *BrushBSP (bspbrush_t *brushlist, Vector& mins, Vector& maxs);
  378. #define PSIDE_FRONT 1
  379. #define PSIDE_BACK 2
  380. #define PSIDE_BOTH (PSIDE_FRONT|PSIDE_BACK)
  381. #define PSIDE_FACING 4
  382. int BrushBspBoxOnPlaneSide (const Vector& mins, const Vector& maxs, dplane_t *plane);
  383. extern qboolean WindingIsTiny (winding_t *w);
  384. //=============================================================================
  385. // portals.c
  386. int VisibleContents (int contents);
  387. void MakeHeadnodePortals (tree_t *tree);
  388. void MakeNodePortal (node_t *node);
  389. void SplitNodePortals (node_t *node);
  390. qboolean Portal_VisFlood (portal_t *p);
  391. qboolean FloodEntities (tree_t *tree);
  392. void FillOutside (node_t *headnode);
  393. void FloodAreas (tree_t *tree);
  394. void MarkVisibleSides (tree_t *tree, int start, int end, int detailScreen);
  395. void MarkVisibleSides (tree_t *tree, mapbrush_t **ppBrushes, int nCount );
  396. void FreePortal (portal_t *p);
  397. void EmitAreaPortals (node_t *headnode);
  398. void MakeTreePortals (tree_t *tree);
  399. //=============================================================================
  400. // glfile.c
  401. void OutputWinding (winding_t *w, FileHandle_t glview);
  402. void OutputWindingColor (winding_t *w, FileHandle_t glview, int r, int g, int b);
  403. void WriteGLView (tree_t *tree, char *source);
  404. void WriteGLViewFaces (tree_t *tree, const char *source);
  405. void WriteGLViewBrushList( bspbrush_t *pList, const char *pName );
  406. //=============================================================================
  407. // leakfile.c
  408. void LeakFile (tree_t *tree);
  409. void AreaportalLeakFile( tree_t *tree, portal_t *pStartPortal, portal_t *pEndPortal, node_t *pStart );
  410. //=============================================================================
  411. // prtfile.c
  412. void AddVisCluster( entity_t *pFuncVisCluster );
  413. void WritePortalFile (tree_t *tree);
  414. //=============================================================================
  415. // writebsp.c
  416. void SetModelNumbers (void);
  417. void SetLightStyles (void);
  418. void BeginBSPFile (void);
  419. void WriteBSP (node_t *headnode, face_t *pLeafFaceList);
  420. void EndBSPFile (void);
  421. void BeginModel (void);
  422. void EndModel (void);
  423. extern int firstmodeledge;
  424. extern int firstmodelface;
  425. //=============================================================================
  426. // faces.c
  427. void MakeFaces (node_t *headnode);
  428. void MakeDetailFaces (node_t *headnode);
  429. face_t *FixTjuncs( node_t *headnode, face_t *pLeafFaceList );
  430. face_t *AllocFace (void);
  431. void FreeFace (face_t *f);
  432. void FreeFaceList( face_t *pFaces );
  433. void MergeFaceList(face_t **pFaceList);
  434. void SubdivideFaceList(face_t **pFaceList);
  435. extern face_t *edgefaces[MAX_MAP_EDGES][2];
  436. //=============================================================================
  437. // tree.c
  438. void FreeTree (tree_t *tree);
  439. void FreeTree_r (node_t *node);
  440. void PrintTree_r (node_t *node, int depth);
  441. void FreeTreePortals_r (node_t *node);
  442. void PruneNodes_r (node_t *node);
  443. void PruneNodes (node_t *node);
  444. // Returns true if the entity is a func_occluder
  445. bool IsFuncOccluder( int entity_num );
  446. //=============================================================================
  447. // ivp.cpp
  448. class CPhysCollide;
  449. void EmitPhysCollision();
  450. void DumpCollideToGlView( CPhysCollide *pCollide, const char *pFilename );
  451. void EmitWaterVolumesForBSP( dmodel_t *pModel, node_t *headnode );
  452. //=============================================================================
  453. // find + find or create the texdata
  454. int FindTexData( const char *pName );
  455. int FindOrCreateTexData( const char *pName );
  456. // Add a clone of an existing texdata with a new name
  457. int AddCloneTexData( dtexdata_t *pExistingTexData, char const *cloneTexDataName );
  458. int FindOrCreateTexInfo( const texinfo_t &searchTexInfo );
  459. int FindAliasedTexData( const char *pName, dtexdata_t *sourceTexture );
  460. int FindTexInfo( const texinfo_t &searchTexInfo );
  461. //=============================================================================
  462. // normals.c
  463. void SaveVertexNormals( void );
  464. //=============================================================================
  465. // cubemap.cpp
  466. void Cubemap_InsertSample( const Vector& origin, int size );
  467. void Cubemap_CreateDefaultCubemaps( void );
  468. void Cubemap_SaveBrushSides( const char *pSideListStr );
  469. void Cubemap_FixupBrushSidesMaterials( void );
  470. void Cubemap_AttachDefaultCubemapToSpecularSides( void );
  471. // Add skipped cubemaps that are referenced by the engine
  472. void Cubemap_AddUnreferencedCubemaps( void );
  473. //=============================================================================
  474. // overlay.cpp
  475. #define OVERLAY_MAP_STRLEN 256
  476. struct mapoverlay_t
  477. {
  478. int nId;
  479. unsigned short m_nRenderOrder;
  480. char szMaterialName[OVERLAY_MAP_STRLEN];
  481. float flU[2];
  482. float flV[2];
  483. float flFadeDistMinSq;
  484. float flFadeDistMaxSq;
  485. Vector vecUVPoints[4];
  486. Vector vecOrigin;
  487. Vector vecBasis[3];
  488. CUtlVector<int> aSideList;
  489. CUtlVector<int> aFaceList;
  490. };
  491. extern CUtlVector<mapoverlay_t> g_aMapOverlays;
  492. extern CUtlVector<mapoverlay_t> g_aMapWaterOverlays;
  493. int Overlay_GetFromEntity( entity_t *pMapEnt );
  494. void Overlay_UpdateSideLists( int StartIndex );
  495. void Overlay_AddFaceToLists( int iFace, side_t *pSide );
  496. void Overlay_EmitOverlayFaces( void );
  497. void OverlayTransition_UpdateSideLists( int StartIndex );
  498. void OverlayTransition_AddFaceToLists( int iFace, side_t *pSide );
  499. void OverlayTransition_EmitOverlayFaces( void );
  500. void Overlay_Translate( mapoverlay_t *pOverlay, Vector &OriginOffset, QAngle &AngleOffset, matrix3x4_t &Matrix );
  501. //=============================================================================
  502. void RemoveAreaPortalBrushes_R( node_t *node );
  503. dtexdata_t *GetTexData( int index );
  504. #endif