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.

398 lines
12 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #ifndef DISPINFO_H
  10. #define DISPINFO_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. //=============================================================================
  15. #include <assert.h>
  16. #include "idispinfo.h"
  17. #include "bspfile.h"
  18. #include "mathlib/vmatrix.h"
  19. #include "dispnode.h"
  20. #include "builddisp.h"
  21. #include "utlvector.h"
  22. #include "disp_helpers.h"
  23. #include "tier0/fasttimer.h"
  24. #include "dlight.h"
  25. #include "utllinkedlist.h"
  26. #include "zone.h"
  27. struct model_t;
  28. class IMesh;
  29. class CMeshBuilder;
  30. struct ShadowInfo_t;
  31. class CDecalNodeSetupCache;
  32. class CDispInfo : public IDispInfo, public CDispUtilsHelper
  33. {
  34. // IDispInfo overrides.
  35. public:
  36. virtual ~CDispInfo();
  37. virtual void GetIntersectingSurfaces( GetIntersectingSurfaces_Struct *pStruct );
  38. virtual void RenderWireframeInLightmapPage( int pageId );
  39. virtual void GetBoundingBox( Vector& bbMin, Vector& bbMax );
  40. virtual void SetParent( SurfaceHandle_t surfID );
  41. virtual SurfaceHandle_t GetParent(); // returns surfID
  42. // add the dlights on this surface to the lightmap buffer for updload
  43. virtual void AddDynamicLights( dlight_t *pLights, unsigned int lightMask );
  44. // compute which dlights affect this surface
  45. virtual unsigned int ComputeDynamicLightMask( dlight_t *pLights );
  46. virtual DispDecalHandle_t NotifyAddDecal( decal_t *pDecal, float flSize );
  47. virtual void NotifyRemoveDecal( DispDecalHandle_t h );
  48. virtual DispShadowHandle_t AddShadowDecal( ShadowHandle_t shadowHandle );
  49. virtual void RemoveShadowDecal( DispShadowHandle_t handle );
  50. // Compute shadow fragments for a particular shadow
  51. virtual bool ComputeShadowFragments( DispShadowHandle_t h, int& vertexCount, int& indexCount );
  52. virtual bool GetTag();
  53. virtual void SetTag();
  54. public:
  55. //=========================================================================
  56. //
  57. // Construction/Decontruction
  58. //
  59. CDispInfo();
  60. // Used for indexing displacements.
  61. CDispInfo* GetDispByIndex( int index ) { return index == 0xFFFF ? 0 : &m_pDispArray->m_pDispInfos[index]; }
  62. // Get this displacement's index into the main array.
  63. int GetDispIndex() { return this - m_pDispArray->m_pDispInfos; }
  64. //=========================================================================
  65. //
  66. // Flags
  67. //
  68. void SetTouched( bool bTouched );
  69. bool IsTouched( void );
  70. // Rendering.
  71. void ClearLOD();
  72. void DrawDispAxes();
  73. bool Render( CGroupMesh *pGroup, bool bAllowDebugModes );
  74. // Add in the contribution of a dynamic light.
  75. void AddSingleDynamicLight( dlight_t& dl );
  76. void AddSingleDynamicLightBumped( dlight_t& dl );
  77. // Add in the contribution of a dynamic alpha light.
  78. void AddSingleDynamicAlphaLight( dlight_t& dl );
  79. // Cast a ray against this surface
  80. bool TestRay( Ray_t const& ray, float start, float end, float& dist,
  81. Vector2D* lightmapUV, Vector2D* texureUV );
  82. // CDispUtilsHelper implementation.
  83. public:
  84. virtual const CPowerInfo* GetPowerInfo() const;
  85. virtual CDispNeighbor* GetEdgeNeighbor( int index );
  86. virtual CDispCornerNeighbors* GetCornerNeighbors( int index );
  87. virtual CDispUtilsHelper* GetDispUtilsByIndex( int index );
  88. // Initialization functions.
  89. public:
  90. // These are used to mess with indices.
  91. int VertIndex( int x, int y ) const;
  92. int VertIndex( CVertIndex const &vert ) const;
  93. CVertIndex IndexToVert( int index ) const;
  94. // Helpers to test decal intersection bit on decals.
  95. void SetNodeIntersectsDecal( CDispDecal *pDispDecal, CVertIndex const &nodeIndex );
  96. int GetNodeIntersectsDecal( CDispDecal *pDispDecal, CVertIndex const &nodeIndex );
  97. public:
  98. // Copy data from a ddispinfo_t.
  99. void CopyMapDispData( const ddispinfo_t *pBuildDisp );
  100. // This is called from CreateStaticBuffers_All after the CCoreDispInfo is fully
  101. // initialized. It just copies the data that it needs.
  102. bool CopyCoreDispData(
  103. model_t *pWorld,
  104. const MaterialSystem_SortInfo_t *pSortInfos,
  105. const CCoreDispInfo *pInfo,
  106. bool bRestoring );
  107. // called by CopyCoreDispData, just copies the vert data
  108. void CopyCoreDispVertData( const CCoreDispInfo *pInfo, float bumpSTexCoordOffset );
  109. // Checks the SURFDRAW_BUMPLIGHT flag and returns NUM_BUMP_VECTS+1 if it's set
  110. // and 1 if not.
  111. int NumLightMaps();
  112. // This calculates the vertex's position on the base surface.
  113. // (Same result as CCoreDisp::GetFlatVert).
  114. Vector GetFlatVert( int iVertex );
  115. // Rendering functions.
  116. public:
  117. // Set m_BBoxMin and m_BBoxMax. Uses g_TempDispVerts and assumes m_LODs have been validated.
  118. void UpdateBoundingBox();
  119. // Number of verts per side.
  120. int GetSideLength() const;
  121. // Return the total number of vertices.
  122. int NumVerts() const;
  123. // Figure out the vector's projection in the decal's space.
  124. void DecalProjectVert( Vector const &vPos, CDispDecalBase *pDispDecal, ShadowInfo_t const* pInfo, Vector &out );
  125. void CullDecals(
  126. int iNodeBit,
  127. CDispDecal **decals,
  128. int nDecals,
  129. CDispDecal **childDecals,
  130. int &nChildDecals );
  131. void TesselateDisplacement();
  132. // Pass all the mesh data in to the material system.
  133. void SpecifyDynamicMesh();
  134. void SpecifyWalkableDynamicMesh( void );
  135. void SpecifyBuildableDynamicMesh( void );
  136. // Clear all active verts except the corner verts.
  137. void InitializeActiveVerts();
  138. // Returns a particular vertex
  139. CDispRenderVert* GetVertex( int i );
  140. // Methods to compute lightmap coordinates, texture coordinates,
  141. // and lightmap color based on displacement u,v
  142. void ComputeLightmapAndTextureCoordinate( RayDispOutput_t const& output,
  143. Vector2D* luv, Vector2D* tuv );
  144. // This little beastie generate decal fragments
  145. void GenerateDecalFragments( CVertIndex const &nodeIndex,
  146. int iNodeBitIndex, unsigned short decalHandle, CDispDecalBase *pDispDecal );
  147. private:
  148. // Two functions for adding decals
  149. void TestAddDecalTri( int iIndexStart, unsigned short decalHandle, CDispDecal *pDispDecal );
  150. void TestAddDecalTri( int iIndexStart, unsigned short decalHandle, CDispShadowDecal *pDispDecal );
  151. // Allocates fragments...
  152. CDispDecalFragment* AllocateDispDecalFragment( DispDecalHandle_t h, int nVerts = 6);
  153. // Clears decal fragment lists
  154. void ClearDecalFragments( DispDecalHandle_t h );
  155. void ClearAllDecalFragments();
  156. // Allocates fragments...
  157. CDispShadowFragment* AllocateShadowDecalFragment( DispShadowHandle_t h, int nCount );
  158. // Clears shadow decal fragment lists
  159. void ClearShadowDecalFragments( DispShadowHandle_t h );
  160. void ClearAllShadowDecalFragments();
  161. // Used by GenerateDecalFragments
  162. void GenerateDecalFragments_R( CVertIndex const &nodeIndex,
  163. int iNodeBitIndex, unsigned short decalHandle, CDispDecalBase *pDispDecal, int iLevel );
  164. // Used to create a bitfield to help cull decal tests
  165. void SetupDecalNodeIntersect( CVertIndex const &nodeIndex, int iNodeBitIndex,
  166. CDispDecalBase *pDispDecal, ShadowInfo_t const* pInfo );
  167. // Used by SetupDecalNodeIntersect
  168. bool SetupDecalNodeIntersect_R( CVertIndex const &nodeIndex, int iNodeBitIndex,
  169. CDispDecalBase *pDispDecal, ShadowInfo_t const* pInfo, int iLevel, CDecalNodeSetupCache* pCache );
  170. // Vertex/index data access.
  171. public:
  172. // bounding box
  173. Vector m_BBoxMin;
  174. Vector m_BBoxMax;
  175. int m_nIndices; // The actual # of indices being used (it can be less than m_Indices.Count() if
  176. // our LOD is reducing the triangle count).
  177. int m_iIndexOffset;
  178. // Used to get material..
  179. CGroupMesh *m_pMesh;
  180. int m_iVertOffset;
  181. float m_BumpSTexCoordOffset;
  182. // This can be used to access the vertex data in a platform-independent way.
  183. // XBOX will get them directly out of the static vertex buffer.
  184. // PC gets them out of CDispRenderVerts.
  185. CMeshReader m_MeshReader;
  186. // List of all indices in the displacement in the current tesselation.
  187. // These indices are straight into the static buffer (ie: they're not relative
  188. // to m_iVertOffset).
  189. CUtlVector<unsigned short> m_Indices;
  190. CUtlVector<CDispRenderVert, CHunkMemory<CDispRenderVert> > m_Verts; // vectors that define the surface (size is NumVerts()).
  191. public:
  192. // These bits tell which vertices and nodes are currently active.
  193. // These start out the same as m_ErrorVerts but have verts added if
  194. // a neighbor needs to activate some verts.
  195. CBitVec<CCoreDispInfo::MAX_VERT_COUNT> m_ActiveVerts;
  196. // These are set to 1 if the vert is allowed to become active.
  197. // This is what takes care of different-sized neighbors.
  198. CBitVec<CCoreDispInfo::MAX_VERT_COUNT> m_AllowedVerts;
  199. int m_idLMPage; // lightmap page id
  200. SurfaceHandle_t m_ParentSurfID; // parent surfaceID
  201. int m_iPointStart; // starting point (orientation) on base face
  202. int m_iLightmapAlphaStart;
  203. int m_Contents; // surface contents
  204. public:
  205. bool m_bTouched; // touched flag
  206. int m_fSurfProp; // surface properties flag - bump-mapping, etc.
  207. int m_Power; // surface size (sides are 2^n+1).
  208. unsigned short *m_pTags; // property tags
  209. // Position and texcoordinates at the four corner points
  210. Vector m_BaseSurfacePositions[4];
  211. Vector2D m_BaseSurfaceTexCoords[4];
  212. // Precalculated data for displacements of this size.
  213. const CPowerInfo *m_pPowerInfo;
  214. // Neighbor info for each side, indexed by NEIGHBOREDGE_ enums.
  215. // First 4 are edge neighbors, the rest are corners.
  216. CDispNeighbor m_EdgeNeighbors[4];
  217. CDispCornerNeighbors m_CornerNeighbors[4];
  218. // Copied from the ddispinfo. Speciifes where in g_DispLightmapSamplePositions the (compressed)
  219. // lightmap sample positions start.
  220. int m_iLightmapSamplePositionStart;
  221. // The current triangulation for visualizing tag data.
  222. int m_nWalkIndexCount;
  223. unsigned short *m_pWalkIndices;
  224. int m_nBuildIndexCount;
  225. unsigned short *m_pBuildIndices;
  226. // This here's a bunch of per-node information
  227. DispNodeInfo_t *m_pNodeInfo;
  228. // Where the viewer was when we last tesselated.
  229. // When the viewer moves out of the sphere, UpdateLOD is called.
  230. Vector m_ViewerSphereCenter;
  231. // Used to make sure it doesn't activate verts in the wrong dispinfos.
  232. bool m_bInUse;
  233. // Decals + Shadow decals
  234. DispDecalHandle_t m_FirstDecal;
  235. DispShadowHandle_t m_FirstShadowDecal;
  236. unsigned short m_Index; // helps in debugging
  237. // Current tag value.
  238. unsigned short m_Tag;
  239. CDispArray *m_pDispArray;
  240. };
  241. extern int g_nDispTris;
  242. extern CCycleCount g_DispRenderTime;
  243. extern bool DispInfoRenderDebugModes();
  244. // --------------------------------------------------------------------------------- //
  245. // CDispInfo functions.
  246. // --------------------------------------------------------------------------------- //
  247. inline int CDispInfo::GetSideLength() const
  248. {
  249. return m_pPowerInfo->m_SideLength;
  250. }
  251. inline int CDispInfo::NumVerts() const
  252. {
  253. Assert( m_Verts.Count() == m_pPowerInfo->m_MaxVerts );
  254. return m_pPowerInfo->m_MaxVerts;
  255. }
  256. //-----------------------------------------------------------------------------
  257. // Returns a particular vertex
  258. //-----------------------------------------------------------------------------
  259. inline CDispRenderVert* CDispInfo::GetVertex( int i )
  260. {
  261. Assert( i < NumVerts() );
  262. return &m_Verts[i];
  263. }
  264. //-----------------------------------------------------------------------------
  265. //-----------------------------------------------------------------------------
  266. inline void CDispInfo::SetTouched( bool bTouched )
  267. {
  268. m_bTouched = bTouched;
  269. }
  270. //-----------------------------------------------------------------------------
  271. //-----------------------------------------------------------------------------
  272. inline bool CDispInfo::IsTouched( void )
  273. {
  274. return m_bTouched;
  275. }
  276. inline int CDispInfo::VertIndex( int x, int y ) const
  277. {
  278. Assert( x >= 0 && x < GetSideLength() && y >= 0 && y < GetSideLength() );
  279. return y * GetSideLength() + x;
  280. }
  281. inline int CDispInfo::VertIndex( CVertIndex const &vert ) const
  282. {
  283. Assert( vert.x >= 0 && vert.x < GetSideLength() && vert.y >= 0 && vert.y < GetSideLength() );
  284. return vert.y * GetSideLength() + vert.x;
  285. }
  286. void DispInfo_BatchDecals( CDispInfo **pVisibleDisps, int nVisibleDisps );
  287. void DispInfo_DrawDecals( CDispInfo **pVisibleDisps, int nVisibleDisps );
  288. #endif // DISPINFO_H