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.

244 lines
7.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef PHYSICS_TRACE_H
  8. #define PHYSICS_TRACE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. class Vector;
  13. class QAngle;
  14. class CGameTrace;
  15. class CTraceRay;
  16. class IVP_Compact_Surface;
  17. typedef CGameTrace trace_t;
  18. struct Ray_t;
  19. class IVP_Compact_Surface;
  20. class IVP_Compact_Mopp;
  21. class IConvexInfo;
  22. enum
  23. {
  24. COLLIDE_POLY = 0,
  25. COLLIDE_MOPP = 1,
  26. COLLIDE_BALL = 2,
  27. COLLIDE_VIRTUAL = 3,
  28. };
  29. class IPhysCollide
  30. {
  31. public:
  32. virtual ~IPhysCollide() {}
  33. //virtual void AddReference() = 0;
  34. //virtual void ReleaseReference() = 0;
  35. // get a surface manager
  36. virtual IVP_SurfaceManager *CreateSurfaceManager( short & ) const = 0;
  37. virtual void GetAllLedges( IVP_U_BigVector<IVP_Compact_Ledge> &ledges ) const = 0;
  38. virtual unsigned int GetSerializationSize() const = 0;
  39. virtual unsigned int SerializeToBuffer( char *pDest, bool bSwap = false ) const = 0;
  40. virtual int GetVCollideIndex() const = 0;
  41. virtual Vector GetMassCenter() const = 0;
  42. virtual void SetMassCenter( const Vector &massCenter ) = 0;
  43. virtual Vector GetOrthographicAreas() const = 0;
  44. virtual void SetOrthographicAreas( const Vector &areas ) = 0;
  45. virtual float GetSphereRadius() const = 0;
  46. virtual void OutputDebugInfo() const = 0;
  47. };
  48. #define LEAFMAP_HAS_CUBEMAP 0x0001
  49. #define LEAFMAP_HAS_SINGLE_VERTEX_SPAN 0x0002
  50. #define LEAFMAP_HAS_MULTIPLE_VERTEX_SPANS 0x0004
  51. struct leafmap_t
  52. {
  53. void *pLeaf;
  54. unsigned short vertCount;
  55. byte flags;
  56. byte spanCount;
  57. unsigned short startVert[8];
  58. void SetHasCubemap()
  59. {
  60. flags = LEAFMAP_HAS_CUBEMAP;
  61. }
  62. void SetSingleVertexSpan( int startVertIndex, int vertCountIn )
  63. {
  64. flags = 0;
  65. flags |= LEAFMAP_HAS_SINGLE_VERTEX_SPAN;
  66. startVert[0] = startVertIndex;
  67. vertCount = vertCountIn;
  68. }
  69. int MaxSpans()
  70. {
  71. return sizeof(startVert) - sizeof(startVert[0]);
  72. }
  73. const byte *GetSpans() const
  74. {
  75. return reinterpret_cast<const byte *>(&startVert[1]);
  76. }
  77. byte *GetSpans()
  78. {
  79. return reinterpret_cast<byte *>(&startVert[1]);
  80. }
  81. void SetRLESpans( int startVertIndex, int spanCountIn, byte *pSpans )
  82. {
  83. flags = 0;
  84. if ( spanCountIn > MaxSpans() )
  85. return;
  86. if ( spanCountIn == 1 )
  87. {
  88. SetSingleVertexSpan( startVertIndex, pSpans[0] );
  89. return;
  90. }
  91. // write out a run length encoded list of verts to include in this model
  92. flags |= LEAFMAP_HAS_MULTIPLE_VERTEX_SPANS;
  93. startVert[0] = startVertIndex;
  94. vertCount = 0;
  95. spanCount = spanCountIn;
  96. byte *pSpanOut = GetSpans();
  97. for ( int i = 0; i < spanCountIn; i++ )
  98. {
  99. pSpanOut[i] = pSpans[i];
  100. if ( !(i & 1) )
  101. {
  102. vertCount += pSpans[i];
  103. }
  104. }
  105. }
  106. inline bool HasSpans() const { return (flags & (LEAFMAP_HAS_SINGLE_VERTEX_SPAN|LEAFMAP_HAS_MULTIPLE_VERTEX_SPANS)) ? true : false; }
  107. inline bool HasCubemap() const { return (flags & LEAFMAP_HAS_CUBEMAP) ? true : false; }
  108. inline bool HasSingleVertexSpan() const { return (flags & LEAFMAP_HAS_SINGLE_VERTEX_SPAN) ? true : false; }
  109. inline bool HasRLESpans() const { return (flags & LEAFMAP_HAS_MULTIPLE_VERTEX_SPANS) ? true : false; }
  110. };
  111. struct collidemap_t
  112. {
  113. int leafCount;
  114. leafmap_t leafmap[1];
  115. };
  116. extern void InitLeafmap( IVP_Compact_Ledge *pLeaf, leafmap_t *pLeafmapOut );
  117. class CPhysCollide : public IPhysCollide
  118. {
  119. public:
  120. static CPhysCollide *UnserializeFromBuffer( const char *pBuffer, unsigned int size, int index, bool swap = false );
  121. virtual const IVP_Compact_Surface *GetCompactSurface() const { return NULL; }
  122. virtual Vector GetOrthographicAreas() const { return Vector(1,1,1); }
  123. virtual float GetSphereRadius() const { return 0; }
  124. virtual void ComputeOrthographicAreas( float epsilon ) {}
  125. virtual void SetOrthographicAreas( const Vector &areas ) {}
  126. virtual const collidemap_t *GetCollideMap() const { return NULL; }
  127. };
  128. class ITraceObject
  129. {
  130. public:
  131. virtual int SupportMap( const Vector &dir, Vector *pOut ) const = 0;
  132. virtual Vector GetVertByIndex( int index ) const = 0;
  133. virtual float Radius( void ) const = 0;
  134. };
  135. // This is the size of the vertex hash
  136. #define CONVEX_HASH_SIZE 512
  137. // The little hashing trick below allows 64K verts per hash entry
  138. #define MAX_CONVEX_VERTS ((CONVEX_HASH_SIZE * (1<<16))-1)
  139. class CPhysicsTrace
  140. {
  141. public:
  142. CPhysicsTrace();
  143. ~CPhysicsTrace();
  144. // Calculate the intersection of a swept box (mins/maxs) against an IVP object. All coords are in HL space.
  145. void SweepBoxIVP( const Vector &start, const Vector &end, const Vector &mins, const Vector &maxs, const CPhysCollide *pSurface, const Vector &surfaceOrigin, const QAngle &surfaceAngles, trace_t *ptr );
  146. void SweepBoxIVP( const Ray_t &raySrc, unsigned int contentsMask, IConvexInfo *pConvexInfo, const CPhysCollide *pSurface, const Vector &surfaceOrigin, const QAngle &surfaceAngles, trace_t *ptr );
  147. // Calculate the intersection of a swept compact surface against another compact surface. All coords are in HL space.
  148. // NOTE: BUGBUG: swept surface must be single convex!!!
  149. void SweepIVP( const Vector &start, const Vector &end, const CPhysCollide *pSweptSurface, const QAngle &sweptAngles, const CPhysCollide *pSurface, const Vector &surfaceOrigin, const QAngle &surfaceAngles, trace_t *ptr );
  150. // get an AABB for an oriented collide
  151. void GetAABB( Vector *pMins, Vector *pMaxs, const CPhysCollide *pCollide, const Vector &collideOrigin, const QAngle &collideAngles );
  152. // get the support map/extent for a collide along the axis given by "direction"
  153. Vector GetExtent( const CPhysCollide *pCollide, const Vector &collideOrigin, const QAngle &collideAngles, const Vector &direction );
  154. bool IsBoxIntersectingCone( const Vector &boxAbsMins, const Vector &boxAbsMaxs, const truncatedcone_t &cone );
  155. };
  156. class CVisitHash
  157. {
  158. public:
  159. CVisitHash();
  160. inline unsigned short VertIndexToID( int vertIndex );
  161. inline void VisitVert( int vertIndex );
  162. inline bool WasVisited( int vertIndex );
  163. inline void NewVisit( void );
  164. private:
  165. // Store the current increment and the vertex ID (rotating hash) to guarantee no collisions
  166. struct vertmarker_t
  167. {
  168. unsigned short visitID;
  169. unsigned short vertID;
  170. };
  171. vertmarker_t m_vertVisit[CONVEX_HASH_SIZE];
  172. unsigned short m_vertVisitID;
  173. unsigned short m_isInUse;
  174. };
  175. // Calculate the intersection of a swept box (mins/maxs) against an IVP object. All coords are in HL space.
  176. inline unsigned short CVisitHash::VertIndexToID( int vertIndex )
  177. {
  178. // A little hashing trick here:
  179. // rotate the hash key each time you wrap around at 64K
  180. // That way, the index will not collide until you've hit 64K # hash entries times
  181. int high = vertIndex >> 16;
  182. return (unsigned short) ((vertIndex + high) & 0xFFFF);
  183. }
  184. inline void CVisitHash::VisitVert( int vertIndex )
  185. {
  186. int index = vertIndex & (CONVEX_HASH_SIZE-1);
  187. m_vertVisit[index].visitID = m_vertVisitID;
  188. m_vertVisit[index].vertID = VertIndexToID(vertIndex);
  189. }
  190. inline bool CVisitHash::WasVisited( int vertIndex )
  191. {
  192. unsigned short hashIndex = vertIndex & (CONVEX_HASH_SIZE-1);
  193. unsigned short id = VertIndexToID(vertIndex);
  194. if ( m_vertVisit[hashIndex].visitID == m_vertVisitID && m_vertVisit[hashIndex].vertID == id )
  195. return true;
  196. return false;
  197. }
  198. inline void CVisitHash::NewVisit( void )
  199. {
  200. m_vertVisitID++;
  201. if ( m_vertVisitID == 0 )
  202. {
  203. memset( m_vertVisit, 0, sizeof(m_vertVisit) );
  204. }
  205. }
  206. extern IVP_SurfaceManager *CreateSurfaceManager( const CPhysCollide *pCollisionModel, short &collideType );
  207. extern void OutputCollideDebugInfo( const CPhysCollide *pCollisionModel );
  208. #endif // PHYSICS_TRACE_H