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.

456 lines
16 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef DISPCOLL_COMMON_H
  8. #define DISPCOLL_COMMON_H
  9. #pragma once
  10. #include "trace.h"
  11. #include "builddisp.h"
  12. #include "bitvec.h"
  13. #ifdef ENGINE_DLL
  14. #include "../engine/zone.h"
  15. #endif
  16. #ifdef ENGINE_DLL
  17. template<typename T>
  18. class CDispVector : public CUtlVector<T, CHunkMemory<T> >
  19. {
  20. };
  21. #else
  22. template<typename T>
  23. class CDispVector : public CUtlVector<T, CUtlMemoryAligned<T,16> >
  24. {
  25. };
  26. #endif
  27. FORWARD_DECLARE_HANDLE( memhandle_t );
  28. #define DISPCOLL_TREETRI_SIZE MAX_DISPTRIS
  29. #define DISPCOLL_DIST_EPSILON 0.03125f
  30. #define DISPCOLL_ROOTNODE_INDEX 0
  31. #define DISPCOLL_INVALID_TRI -1
  32. #define DISPCOLL_INVALID_FRAC -99999.9f
  33. #define DISPCOLL_NORMAL_UNDEF 0xffff
  34. extern double g_flDispCollSweepTimer;
  35. extern double g_flDispCollIntersectTimer;
  36. extern double g_flDispCollInCallTimer;
  37. struct RayDispOutput_t
  38. {
  39. short ndxVerts[4]; // 3 verts and a pad
  40. float u, v; // the u, v paramters (edgeU = v1 - v0, edgeV = v2 - v0)
  41. float dist; // intersection distance
  42. };
  43. // Assumptions:
  44. // Max patch is 17x17, therefore 9 bits needed to represent a triangle index
  45. //
  46. //=============================================================================
  47. // Displacement Collision Triangle
  48. class CDispCollTri
  49. {
  50. struct index_t
  51. {
  52. union
  53. {
  54. struct
  55. {
  56. unsigned short uiVert:9;
  57. unsigned short uiMin:2;
  58. unsigned short uiMax:2;
  59. } m_Index;
  60. unsigned short m_IndexDummy;
  61. };
  62. };
  63. index_t m_TriData[3];
  64. public:
  65. unsigned short m_ucSignBits:3; // Plane test.
  66. unsigned short m_ucPlaneType:3; // Axial test?
  67. unsigned short m_uiFlags:5; // Uses 5-bits - maybe look into merging it with something?
  68. Vector m_vecNormal; // Triangle normal (plane normal).
  69. float m_flDist; // Triangle plane dist.
  70. // Creation.
  71. CDispCollTri();
  72. void Init( void );
  73. void CalcPlane( CDispVector<Vector> &m_aVerts );
  74. void FindMinMax( CDispVector<Vector> &m_aVerts );
  75. // Triangle data.
  76. inline void SetVert( int iPos, int iVert ) { Assert( ( iPos >= 0 ) && ( iPos < 3 ) ); Assert( ( iVert >= 0 ) && ( iVert < ( 1 << 9 ) ) ); m_TriData[iPos].m_Index.uiVert = iVert; }
  77. inline int GetVert( int iPos ) const { Assert( ( iPos >= 0 ) && ( iPos < 3 ) ); return m_TriData[iPos].m_Index.uiVert; }
  78. inline void SetMin( int iAxis, int iMin ) { Assert( ( iAxis >= 0 ) && ( iAxis < 3 ) ); Assert( ( iMin >= 0 ) && ( iMin < 3 ) ); m_TriData[iAxis].m_Index.uiMin = iMin; }
  79. inline int GetMin( int iAxis ) const { Assert( ( iAxis >= 0 ) && ( iAxis < 3 ) ); return m_TriData[iAxis].m_Index.uiMin; }
  80. inline void SetMax( int iAxis, int iMax ) { Assert( ( iAxis >= 0 ) && ( iAxis < 3 ) ); Assert( ( iMax >= 0 ) && ( iMax < 3 ) ); m_TriData[iAxis].m_Index.uiMax = iMax; }
  81. inline int GetMax( int iAxis ) const { Assert( ( iAxis >= 0 ) && ( iAxis < 3 ) ); return m_TriData[iAxis].m_Index.uiMax; }
  82. };
  83. //=============================================================================
  84. // Helper
  85. class CDispCollHelper
  86. {
  87. public:
  88. float m_flStartFrac;
  89. float m_flEndFrac;
  90. Vector m_vecImpactNormal;
  91. float m_flImpactDist;
  92. };
  93. //=============================================================================
  94. // Cache
  95. #pragma pack(1)
  96. class CDispCollTriCache
  97. {
  98. public:
  99. unsigned short m_iCrossX[3];
  100. unsigned short m_iCrossY[3];
  101. unsigned short m_iCrossZ[3];
  102. };
  103. #pragma pack()
  104. #include "mathlib/ssemath.h"
  105. class CDispCollNode
  106. {
  107. public:
  108. FourVectors m_mins;
  109. FourVectors m_maxs;
  110. };
  111. class CDispCollLeaf
  112. {
  113. public:
  114. short m_tris[2];
  115. };
  116. // a power 4 displacement can have 341 nodes, pad out to 344 for 16-byte alignment
  117. const int MAX_DISP_AABB_NODES = 341;
  118. const int MAX_AABB_LIST = 344;
  119. struct rayleaflist_t
  120. {
  121. FourVectors rayStart;
  122. FourVectors rayExtents;
  123. FourVectors invDelta;
  124. int nodeList[MAX_AABB_LIST];
  125. int maxIndex;
  126. };
  127. //=============================================================================
  128. //
  129. // Displacement Collision Tree Data
  130. //
  131. class CDispCollTree
  132. {
  133. public:
  134. // Creation/Destruction.
  135. CDispCollTree();
  136. ~CDispCollTree();
  137. virtual bool Create( CCoreDispInfo *pDisp );
  138. // Raycasts.
  139. // NOTE: These assume you've precalculated invDelta as well as culled to the bounds of this disp
  140. bool AABBTree_Ray( const Ray_t &ray, const Vector &invDelta, CBaseTrace *pTrace, bool bSide = true );
  141. bool AABBTree_Ray( const Ray_t &ray, const Vector &invDelta, RayDispOutput_t &output );
  142. // NOTE: Lower perf helper function, should not be used in the game runtime
  143. bool AABBTree_Ray( const Ray_t &ray, RayDispOutput_t &output );
  144. // Hull Sweeps.
  145. // NOTE: These assume you've precalculated invDelta as well as culled to the bounds of this disp
  146. bool AABBTree_SweepAABB( const Ray_t &ray, const Vector &invDelta, CBaseTrace *pTrace );
  147. // Hull Intersection.
  148. bool AABBTree_IntersectAABB( const Vector &absMins, const Vector &absMaxs );
  149. // Point/Box vs. Bounds.
  150. bool PointInBounds( Vector const &vecBoxCenter, Vector const &vecBoxMin, Vector const &vecBoxMax, bool bPoint );
  151. // Utility.
  152. inline void SetPower( int power ) { m_nPower = power; }
  153. inline int GetPower( void ) { return m_nPower; }
  154. inline int GetFlags( void ) { return m_nFlags; }
  155. inline void SetFlags( int nFlags ) { m_nFlags = nFlags; }
  156. inline bool CheckFlags( int nFlags ) { return ( ( nFlags & GetFlags() ) != 0 ) ? true : false; }
  157. inline int GetWidth( void ) { return ( ( 1 << m_nPower ) + 1 ); }
  158. inline int GetHeight( void ) { return ( ( 1 << m_nPower ) + 1 ); }
  159. inline int GetSize( void ) { return ( ( 1 << m_nPower ) + 1 ) * ( ( 1 << m_nPower ) + 1 ); }
  160. inline int GetTriSize( void ) { return ( ( 1 << m_nPower ) * ( 1 << m_nPower ) * 2 ); }
  161. // inline void SetTriFlags( short iTri, unsigned short nFlags ) { m_aTris[iTri].m_uiFlags = nFlags; }
  162. inline void GetStabDirection( Vector &vecDir ) { vecDir = m_vecStabDir; }
  163. inline void GetBounds( Vector &vecBoxMin, Vector &vecBoxMax ) { vecBoxMin = m_mins; vecBoxMax = m_maxs; }
  164. inline int GetContents( void ) { return m_nContents; }
  165. inline void SetSurfaceProps( int iProp, short nSurfProp ) { Assert( ( iProp >= 0 ) && ( iProp < 2 ) ); m_nSurfaceProps[iProp] = nSurfProp; }
  166. inline short GetSurfaceProps( int iProp ) { return m_nSurfaceProps[iProp]; }
  167. inline unsigned int GetMemorySize( void ) { return m_nSize; }
  168. inline unsigned int GetCacheMemorySize( void ) { return ( m_aTrisCache.Count() * sizeof(CDispCollTriCache) + m_aEdgePlanes.Count() * sizeof(Vector) ); }
  169. inline bool IsCached( void ) { return m_aTrisCache.Count() == m_aTris.Count(); }
  170. void GetVirtualMeshList( struct virtualmeshlist_t *pList );
  171. int AABBTree_GetTrisInSphere( const Vector &center, float radius, unsigned short *pIndexOut, int indexMax );
  172. public:
  173. inline int Nodes_GetChild( int iNode, int nDirection );
  174. inline int Nodes_CalcCount( int nPower );
  175. inline int Nodes_GetParent( int iNode );
  176. inline int Nodes_GetLevel( int iNode );
  177. inline int Nodes_GetIndexFromComponents( int x, int y );
  178. void LockCache();
  179. void UnlockCache();
  180. void Cache( void );
  181. void Uncache() { m_aTrisCache.Purge(); m_aEdgePlanes.Purge(); }
  182. #ifdef ENGINE_DLL
  183. // Data manager methods
  184. static size_t EstimatedSize( CDispCollTree *pTree )
  185. {
  186. return pTree->GetCacheMemorySize();
  187. }
  188. static CDispCollTree *CreateResource( CDispCollTree *pTree )
  189. {
  190. // Created ahead of time
  191. return pTree;
  192. }
  193. bool GetData()
  194. {
  195. return IsCached();
  196. }
  197. size_t Size()
  198. {
  199. return GetCacheMemorySize();
  200. }
  201. void DestroyResource()
  202. {
  203. Uncache();
  204. m_hCache = NULL;
  205. }
  206. #endif
  207. protected:
  208. bool AABBTree_Create( CCoreDispInfo *pDisp );
  209. void AABBTree_CopyDispData( CCoreDispInfo *pDisp );
  210. void AABBTree_CreateLeafs( void );
  211. void AABBTree_GenerateBoxes_r( int nodeIndex, Vector *pMins, Vector *pMaxs );
  212. void AABBTree_CalcBounds( void );
  213. int AABBTree_BuildTreeTrisInSphere_r( const Vector &center, float radius, int iNode, unsigned short *pIndexOut, unsigned short indexMax );
  214. void AABBTree_TreeTrisRayTest( const Ray_t &ray, const Vector &vecInvDelta, int iNode, CBaseTrace *pTrace, bool bSide, CDispCollTri **pImpactTri );
  215. void AABBTree_TreeTrisRayBarycentricTest( const Ray_t &ray, const Vector &vecInvDelta, int iNode, RayDispOutput_t &output, CDispCollTri **pImpactTri );
  216. int FORCEINLINE BuildRayLeafList( int iNode, rayleaflist_t &list );
  217. struct AABBTree_TreeTrisSweepTest_Args_t
  218. {
  219. AABBTree_TreeTrisSweepTest_Args_t( const Ray_t &ray, const Vector &vecInvDelta, const Vector &rayDir, CBaseTrace *pTrace )
  220. : ray( ray ), vecInvDelta( vecInvDelta ), rayDir( rayDir ), pTrace( pTrace ) {}
  221. const Ray_t &ray;
  222. const Vector &vecInvDelta;
  223. const Vector &rayDir;
  224. CBaseTrace *pTrace;
  225. };
  226. protected:
  227. void SweepAABBTriIntersect( const Ray_t &ray, const Vector &rayDir, int iTri, CDispCollTri *pTri, CBaseTrace *pTrace );
  228. void Cache_Create( CDispCollTri *pTri, int iTri ); // Testing!
  229. bool Cache_EdgeCrossAxisX( const Vector &vecEdge, const Vector &vecOnEdge, const Vector &vecOffEdge, CDispCollTri *pTri, unsigned short &iPlane );
  230. bool Cache_EdgeCrossAxisY( const Vector &vecEdge, const Vector &vecOnEdge, const Vector &vecOffEdge, CDispCollTri *pTri, unsigned short &iPlane );
  231. bool Cache_EdgeCrossAxisZ( const Vector &vecEdge, const Vector &vecOnEdge, const Vector &vecOffEdge, CDispCollTri *pTri, unsigned short &iPlane );
  232. inline bool FacePlane( const Ray_t &ray, const Vector &rayDir, CDispCollTri *pTri, CDispCollHelper *pHelper );
  233. bool FORCEINLINE AxisPlanesXYZ( const Ray_t &ray, CDispCollTri *pTri, CDispCollHelper *pHelper );
  234. inline bool EdgeCrossAxisX( const Ray_t &ray, unsigned short iPlane, CDispCollHelper *pHelper );
  235. inline bool EdgeCrossAxisY( const Ray_t &ray, unsigned short iPlane, CDispCollHelper *pHelper );
  236. inline bool EdgeCrossAxisZ( const Ray_t &ray, unsigned short iPlane, CDispCollHelper *pHelper );
  237. bool ResolveRayPlaneIntersect( float flStart, float flEnd, const Vector &vecNormal, float flDist, CDispCollHelper *pHelper );
  238. template <int AXIS> bool FORCEINLINE TestOneAxisPlaneMin( const Ray_t &ray, CDispCollTri *pTri );
  239. template <int AXIS> bool FORCEINLINE TestOneAxisPlaneMax( const Ray_t &ray, CDispCollTri *pTri );
  240. template <int AXIS> bool EdgeCrossAxis( const Ray_t &ray, unsigned short iPlane, CDispCollHelper *pHelper );
  241. // Utility
  242. inline void CalcClosestBoxPoint( const Vector &vecPlaneNormal, const Vector &vecBoxStart, const Vector &vecBoxExtents, Vector &vecBoxPoint );
  243. inline void CalcClosestExtents( const Vector &vecPlaneNormal, const Vector &vecBoxExtents, Vector &vecBoxPoint );
  244. int AddPlane( const Vector &vecNormal );
  245. bool FORCEINLINE IsLeafNode(int iNode);
  246. public:
  247. Vector m_mins; // Bounding box of the displacement surface and base face
  248. int m_iCounter;
  249. Vector m_maxs; // Bounding box of the displacement surface and base face
  250. protected:
  251. int m_nContents; // The displacement surface "contents" (solid, etc...)
  252. #ifdef ENGINE_DLL
  253. memhandle_t m_hCache;
  254. #endif
  255. int m_nPower; // Size of the displacement ( 2^power + 1 )
  256. int m_nFlags;
  257. Vector m_vecSurfPoints[4]; // Base surface points.
  258. // Collision data.
  259. Vector m_vecStabDir; // Direction to stab for this displacement surface (is the base face normal)
  260. short m_nSurfaceProps[2]; // Surface properties (save off from texdata for impact responses)
  261. protected:
  262. CDispVector<Vector> m_aVerts; // Displacement verts.
  263. CDispVector<CDispCollTri> m_aTris; // Displacement triangles.
  264. CDispVector<CDispCollNode> m_nodes; // Nodes.
  265. CDispVector<CDispCollLeaf> m_leaves; // Leaves.
  266. // Cache
  267. CUtlVector<CDispCollTriCache> m_aTrisCache;
  268. CUtlVector<Vector> m_aEdgePlanes;
  269. CDispCollHelper m_Helper;
  270. unsigned int m_nSize;
  271. };
  272. FORCEINLINE bool CDispCollTree::IsLeafNode(int iNode)
  273. {
  274. return iNode >= m_nodes.Count() ? true : false;
  275. }
  276. //-----------------------------------------------------------------------------
  277. // Purpose: get the child node index given the current node index and direction
  278. // of the child (1 of 4)
  279. // Input: iNode - current node index
  280. // nDirection - direction of the child ( [0...3] - SW, SE, NW, NE )
  281. // Output: int - the index of the child node
  282. //-----------------------------------------------------------------------------
  283. inline int CDispCollTree::Nodes_GetChild( int iNode, int nDirection )
  284. {
  285. // node range [0...m_NodeCount)
  286. Assert( iNode >= 0 );
  287. Assert( iNode < m_nodes.Count() );
  288. // ( node index * 4 ) + ( direction + 1 )
  289. return ( ( iNode << 2 ) + ( nDirection + 1 ) );
  290. }
  291. //-----------------------------------------------------------------------------
  292. // Purpose:
  293. //-----------------------------------------------------------------------------
  294. inline int CDispCollTree::Nodes_CalcCount( int nPower )
  295. {
  296. Assert( nPower >= 1 );
  297. Assert( nPower <= 4 );
  298. return ( ( 1 << ( ( nPower + 1 ) << 1 ) ) / 3 );
  299. }
  300. //-----------------------------------------------------------------------------
  301. // Purpose: get the parent node index given the current node
  302. // Input: iNode - current node index
  303. // Output: int - the index of the parent node
  304. //-----------------------------------------------------------------------------
  305. inline int CDispCollTree::Nodes_GetParent( int iNode )
  306. {
  307. // node range [0...m_NodeCount)
  308. Assert( iNode >= 0 );
  309. Assert( iNode < m_nodes.Count() );
  310. // ( node index - 1 ) / 4
  311. return ( ( iNode - 1 ) >> 2 );
  312. }
  313. //-----------------------------------------------------------------------------
  314. // Purpose:
  315. // TODO: should make this a function - not a hardcoded set of statements!!!
  316. //-----------------------------------------------------------------------------
  317. inline int CDispCollTree::Nodes_GetLevel( int iNode )
  318. {
  319. // node range [0...m_NodeCount)
  320. Assert( iNode >= 0 );
  321. Assert( iNode < m_nodes.Count() );
  322. // level = 2^n + 1
  323. if ( iNode == 0 ) { return 1; }
  324. if ( iNode < 5 ) { return 2; }
  325. if ( iNode < 21 ) { return 3; }
  326. if ( iNode < 85 ) { return 4; }
  327. if ( iNode < 341 ) { return 5; }
  328. return -1;
  329. }
  330. //-----------------------------------------------------------------------------
  331. // Purpose:
  332. //-----------------------------------------------------------------------------
  333. inline int CDispCollTree::Nodes_GetIndexFromComponents( int x, int y )
  334. {
  335. int nIndex = 0;
  336. // Interleave bits from the x and y values to create the index
  337. int iShift;
  338. for( iShift = 0; x != 0; iShift += 2, x >>= 1 )
  339. {
  340. nIndex |= ( x & 1 ) << iShift;
  341. }
  342. for( iShift = 1; y != 0; iShift += 2, y >>= 1 )
  343. {
  344. nIndex |= ( y & 1 ) << iShift;
  345. }
  346. return nIndex;
  347. }
  348. //-----------------------------------------------------------------------------
  349. // Purpose:
  350. //-----------------------------------------------------------------------------
  351. inline void CDispCollTree::CalcClosestBoxPoint( const Vector &vecPlaneNormal, const Vector &vecBoxStart,
  352. const Vector &vecBoxExtents, Vector &vecBoxPoint )
  353. {
  354. vecBoxPoint = vecBoxStart;
  355. ( vecPlaneNormal[0] < 0.0f ) ? vecBoxPoint[0] += vecBoxExtents[0] : vecBoxPoint[0] -= vecBoxExtents[0];
  356. ( vecPlaneNormal[1] < 0.0f ) ? vecBoxPoint[1] += vecBoxExtents[1] : vecBoxPoint[1] -= vecBoxExtents[1];
  357. ( vecPlaneNormal[2] < 0.0f ) ? vecBoxPoint[2] += vecBoxExtents[2] : vecBoxPoint[2] -= vecBoxExtents[2];
  358. }
  359. //-----------------------------------------------------------------------------
  360. // Purpose:
  361. //-----------------------------------------------------------------------------
  362. inline void CDispCollTree::CalcClosestExtents( const Vector &vecPlaneNormal, const Vector &vecBoxExtents,
  363. Vector &vecBoxPoint )
  364. {
  365. ( vecPlaneNormal[0] < 0.0f ) ? vecBoxPoint[0] = vecBoxExtents[0] : vecBoxPoint[0] = -vecBoxExtents[0];
  366. ( vecPlaneNormal[1] < 0.0f ) ? vecBoxPoint[1] = vecBoxExtents[1] : vecBoxPoint[1] = -vecBoxExtents[1];
  367. ( vecPlaneNormal[2] < 0.0f ) ? vecBoxPoint[2] = vecBoxExtents[2] : vecBoxPoint[2] = -vecBoxExtents[2];
  368. }
  369. //=============================================================================
  370. // Global Helper Functions
  371. CDispCollTree *DispCollTrees_Alloc( int count );
  372. void DispCollTrees_Free( CDispCollTree *pTrees );
  373. #endif // DISPCOLL_COMMON_H