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.

248 lines
7.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef DISPCOLL_H
  8. #define DISPCOLL_H
  9. #pragma once
  10. #include "mathlib/vector.h"
  11. class CCoreDispInfo;
  12. //=============================================================================
  13. //
  14. // Displacement Collision Triangle Data
  15. //
  16. class CDispCollTri
  17. {
  18. public:
  19. void Init( void );
  20. inline void SetPoint( int index, Vector const& vert );
  21. inline void SetPointNormal( int index, Vector const& normal );
  22. void CalcPlane( void );
  23. inline void SetIntersect( bool bIntersect );
  24. inline bool IsIntersect( void );
  25. Vector m_Points[3]; // polygon points
  26. Vector m_PointNormals[3]; // polygon point normals
  27. Vector m_Normal; // plane normal
  28. float m_Distance; // plane distance
  29. short m_ProjAxes[2]; // projection axes (2 minor axes)
  30. bool m_bIntersect; // intersected triangle???
  31. };
  32. //=============================================================================
  33. //
  34. // Displacement Collision Node Data
  35. //
  36. class CDispCollNode
  37. {
  38. public:
  39. CDispCollNode();
  40. inline bool IsLeaf( void );
  41. inline void SetBounds( Vector const &bMin, Vector const &bMax );
  42. inline void GetBounds( Vector &bMin, Vector &bMax );
  43. Vector m_Bounds[2]; // node minimum and maximum
  44. bool m_bIsLeaf; // is the node a leaf? ( may have to make this an int for alignment!)
  45. CDispCollTri m_Tris[2]; // two triangles contained in leaf node
  46. };
  47. //=============================================================================
  48. //
  49. // Displacement Collision Data
  50. //
  51. class CDispCollData
  52. {
  53. public:
  54. Vector m_StartPos;
  55. Vector m_EndPos;
  56. Vector m_Extents;
  57. float m_Fraction;
  58. int m_Contents;
  59. Vector m_Normal;
  60. float m_Distance;
  61. bool m_bOcclude;
  62. };
  63. // HACKHACK: JAY: Moved this out of CDispCollTree to be thread safe in vrad
  64. enum { TRILIST_CACHE_SIZE = 128 };
  65. class CDispCollTreeTempData
  66. {
  67. public:
  68. //
  69. // temps
  70. //
  71. int m_TriListCount;
  72. CDispCollTri *m_ppTriList[TRILIST_CACHE_SIZE];
  73. // collision tree node cache
  74. float m_AABBDistances[6];
  75. };
  76. //=============================================================================
  77. //
  78. // Displacement Collision Tree
  79. //
  80. class CDispCollTree
  81. {
  82. public:
  83. static const float COLLISION_EPSILON;
  84. static const float ONE_MINUS_COLLISION_EPSILON;
  85. //=========================================================================
  86. //
  87. // Creation/Destruction
  88. //
  89. CDispCollTree();
  90. ~CDispCollTree();
  91. virtual bool Create( CCoreDispInfo *pDisp );
  92. //=========================================================================
  93. //
  94. // Collision Functions
  95. //
  96. bool RayTest( CDispCollData *pData );
  97. bool RayTestAllTris( CDispCollData *pData, int power );
  98. bool AABBIntersect( CDispCollData *pData );
  99. bool AABBSweep( CDispCollData *pData );
  100. //=========================================================================
  101. //
  102. // Attrib Functions
  103. //
  104. inline void SetPower( int power );
  105. inline int GetPower( void );
  106. inline void SetCheckCount( int count );
  107. inline int GetCheckCount( void );
  108. inline void GetBounds( Vector& boundMin, Vector& boundMax );
  109. protected:
  110. int m_Power;
  111. int m_NodeCount;
  112. CDispCollNode *m_pNodes;
  113. int m_CheckCount;
  114. // collision tree node cache
  115. Vector m_AABBNormals[6];
  116. //=========================================================================
  117. //
  118. // Creation/Destruction
  119. //
  120. void InitAABBData( void );
  121. void InitLeaves( CCoreDispInfo *pDisp );
  122. void CreateNodes( CCoreDispInfo *pDisp );
  123. void CreateNodes_r( CCoreDispInfo *pDisp, int nodeIndex, int termLevel );
  124. void CalcBounds( CDispCollNode *pNode, int nodeIndex );
  125. //=========================================================================
  126. //
  127. // Collision Functions
  128. //
  129. void CreatePlanesFromBounds( CDispCollTreeTempData *pTemp, Vector const &bbMin, Vector const &bbMax );
  130. // void RayNodeTest_r( int nodeIndex, Vector &rayStart, Vector &rayEnd );
  131. void RayNodeTest_r( CDispCollTreeTempData *pTemp, int nodeIndex, Vector rayStart, Vector rayEnd );
  132. bool RayAABBTest( CDispCollTreeTempData *pTemp, Vector &rayStart, Vector &rayEnd );
  133. bool RayTriListTest( CDispCollTreeTempData *pTemp, CDispCollData *pData );
  134. bool RayTriTest( Vector const &rayStart, Vector const &rayDir, float const rayLength, CDispCollTri const *pTri, float *fraction );
  135. void BuildTriList_r( CDispCollTreeTempData *pTemp, int nodeIndex, Vector &rayStart, Vector &rayEnd, Vector &extents, bool bIntersect );
  136. bool IntersectAABBAABBTest( CDispCollTreeTempData *pTemp, const Vector &pos, const Vector &extents );
  137. bool SweptAABBAABBTest( CDispCollTreeTempData *pTemp, const Vector &rayStart, const Vector &rayEnd, const Vector &extents );
  138. bool CullTriList( CDispCollTreeTempData *pTemp, Vector &rayStart, Vector &rayEnd, Vector &extents, bool bIntersect );
  139. bool SweptAABBTriTest( Vector &rayStart, Vector &rayEnd, Vector &extents, CDispCollTri const *pTri );
  140. bool AABBTriIntersect( CDispCollTreeTempData *pTemp, CDispCollData *pData );
  141. bool IntersectAABBTriTest( Vector &rayStart, Vector &extents, CDispCollTri const *pTri );
  142. bool SweptAABBTriIntersect( Vector &rayStart, Vector &rayEnd, Vector &extents,
  143. CDispCollTri const *pTri, Vector &plNormal, float *plDist,
  144. float *fraction );
  145. //=========================================================================
  146. //
  147. // Memory Functions
  148. //
  149. bool AllocNodes( int nodeCount );
  150. void FreeNodes( void );
  151. //=========================================================================
  152. //
  153. // Utility Functions
  154. //
  155. inline int CalcNodeCount( int power );
  156. inline int GetParentNode( int nodeIndex );
  157. inline int GetChildNode( int nodeIndex, int direction );
  158. inline int GetNodeLevel( int nodeIndex );
  159. int GetNodeIndexFromComponents( int x, int y );
  160. };
  161. //-----------------------------------------------------------------------------
  162. //-----------------------------------------------------------------------------
  163. inline void CDispCollTree::SetPower( int power )
  164. {
  165. m_Power = power;
  166. }
  167. //-----------------------------------------------------------------------------
  168. //-----------------------------------------------------------------------------
  169. inline int CDispCollTree::GetPower( void )
  170. {
  171. return m_Power;
  172. }
  173. //-----------------------------------------------------------------------------
  174. //-----------------------------------------------------------------------------
  175. inline void CDispCollTree::SetCheckCount( int count )
  176. {
  177. m_CheckCount = count;
  178. }
  179. //-----------------------------------------------------------------------------
  180. //-----------------------------------------------------------------------------
  181. inline int CDispCollTree::GetCheckCount( void )
  182. {
  183. return m_CheckCount;
  184. }
  185. //-----------------------------------------------------------------------------
  186. //-----------------------------------------------------------------------------
  187. inline void CDispCollTree::GetBounds( Vector& boundMin, Vector& boundMax )
  188. {
  189. boundMin[0] = m_pNodes[0].m_Bounds[0].x;
  190. boundMin[1] = m_pNodes[0].m_Bounds[0].y;
  191. boundMin[2] = m_pNodes[0].m_Bounds[0].z;
  192. boundMax[0] = m_pNodes[0].m_Bounds[1].x;
  193. boundMax[1] = m_pNodes[0].m_Bounds[1].y;
  194. boundMax[2] = m_pNodes[0].m_Bounds[1].z;
  195. }
  196. #endif // DISPCOLL_H