Counter Strike : Global Offensive Source Code
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.

208 lines
6.3 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Directional lighting with cascaded shadow mapping entity.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef C_ENV_CASCADE_LIGHT_H
  8. #define C_ENV_CASCADE_LIGHT_H
  9. #include "c_baseplayer.h"
  10. #include "csm_parallel_split.h"
  11. //------------------------------------------------------------------------------
  12. // Purpose: Directional lighting with cascaded shadow mapping entity.
  13. //------------------------------------------------------------------------------
  14. class C_CascadeLight : public C_BaseEntity
  15. {
  16. public:
  17. DECLARE_CLASS( C_CascadeLight, C_BaseEntity );
  18. DECLARE_CLIENTCLASS();
  19. C_CascadeLight();
  20. virtual ~C_CascadeLight();
  21. static C_CascadeLight *Get() { return m_pCascadeLight; }
  22. virtual void Spawn();
  23. virtual void Release();
  24. virtual bool ShouldDraw();
  25. virtual void ClientThink();
  26. inline const Vector &GetShadowDirection() const { return m_shadowDirection; }
  27. inline const Vector &GetEnvLightShadowDirection() const { return m_envLightShadowDirection; }
  28. inline bool IsEnabled() const { return m_bEnabled; }
  29. inline color32 GetColor() const { return m_LightColor; }
  30. inline int GetColorScale() const { return m_LightColorScale; }
  31. inline bool UseLightEnvAngles() const { return m_bUseLightEnvAngles; }
  32. float GetMaxShadowDist() const { return m_flMaxShadowDist; }
  33. private:
  34. static C_CascadeLight *m_pCascadeLight;
  35. Vector m_shadowDirection;
  36. Vector m_envLightShadowDirection;
  37. bool m_bEnabled;
  38. bool m_bUseLightEnvAngles;
  39. color32 m_LightColor;
  40. int m_LightColorScale;
  41. float m_flMaxShadowDist;
  42. };
  43. class CDebugPrimRenderer2D
  44. {
  45. public:
  46. CDebugPrimRenderer2D();
  47. void Clear();
  48. // Normalized [0,1] coords, where (0,0) is upper left
  49. void AddNormalizedLine2D( float sx, float sy, float ex, float ey, uint r, uint g, uint b );
  50. // Screenspace (pixel) coords
  51. void AddScreenspaceLine2D( float sx, float sy, float ex, float ey, uint r, uint g, uint b );
  52. void AddScreenspaceRect2D( float sx, float sy, float ex, float ey, uint r, uint g, uint b );
  53. void AddScreenspaceLineList2D( uint nCount, const Vector2D *pVerts, const VertexColor_t &color );
  54. void AddScreenspaceWireframeFrustum2D( const VMatrix &xform, const VertexColor_t &color, bool bShowAxes );
  55. void Render2D( );
  56. void RenderScreenspaceDepthTexture( float sx, float sy, float ex, float ey, float su, float sv, float eu, float ev, CTextureReference &depthTex, float zLo, float zHi );
  57. private:
  58. class CDebugLine
  59. {
  60. public:
  61. inline CDebugLine() { }
  62. inline CDebugLine( const Vector2D &start, const Vector2D &end, uint r = 255, uint g = 255, uint b = 255, uint a = 255 ) { Init( start, end, r, g, b, a ); }
  63. inline void Init( const Vector2D &start, const Vector2D &end, uint r = 255, uint g = 255, uint b = 255, uint a = 255 ) { m_EndPoints[0] = start; m_EndPoints[1] = end; m_nColor[0] = r; m_nColor[1] = g; m_nColor[2] = b; m_nColor[3] = a; }
  64. Vector2D m_EndPoints[2]; // normalized [0,1] coordinates, (0,0) is top-left
  65. uint8 m_nColor[4];
  66. };
  67. CUtlVector< CDebugLine > m_debugLines;
  68. void RenderDebugLines2D( uint nNumLines, const CDebugLine *pLines );
  69. };
  70. class CCascadeLightManager : public CAutoGameSystemPerFrame
  71. {
  72. public:
  73. CCascadeLightManager();
  74. virtual ~CCascadeLightManager();
  75. bool InitRenderTargets();
  76. void ShutdownRenderTargets();
  77. void LevelInitPreEntity();
  78. void LevelInitPostEntity();
  79. void LevelShutdownPreEntity();
  80. void LevelShutdownPostEntity();
  81. void Shutdown();
  82. bool IsEnabled() const;
  83. bool IsEnabledAndActive() const;
  84. virtual void PreRender ();
  85. void ComputeShadowDepthTextures( const CViewSetup &viewSetup, bool bSetup = false );
  86. void UnlockAllShadowDepthTextures();
  87. void BeginViewModelRendering();
  88. void EndViewModelRendering();
  89. void BeginReflectionView();
  90. void EndReflectionView();
  91. void Draw3DDebugInfo();
  92. void Draw2DDebugInfo();
  93. void DumpStatus();
  94. CSMQualityMode_t GetCSMQualityMode();
  95. public:
  96. static void RotXPlusDown( const CCommand &args );
  97. static void RotXPlusUp( const CCommand &args );
  98. static void RotXNegDown( const CCommand &args );
  99. static void RotXNegUp( const CCommand &args );
  100. static void RotYPlusDown( const CCommand &args );
  101. static void RotYPlusUp( const CCommand &args );
  102. static void RotYNegDown( const CCommand &args );
  103. static void RotYNegUp( const CCommand &args );
  104. private:
  105. class CFullCSMState
  106. {
  107. public:
  108. CFullCSMState()
  109. {
  110. Clear();
  111. }
  112. inline void Clear()
  113. {
  114. V_memset( &m_sceneFrustum, 0, sizeof( m_sceneFrustum ) );
  115. m_flSceneAspectRatio = 0.0f;
  116. m_sceneWorldToView.Identity();
  117. m_sceneViewToProj.Identity();
  118. m_sceneWorldToProj.Identity();
  119. m_CSMParallelSplit.Clear();
  120. m_shadowDir.Init( 0, 0, -1.0f );
  121. m_flMaxShadowDist = 0.0f;
  122. m_flMaxVisibleDist = 0.0f;
  123. m_nMaxCascadeSize = 0;
  124. m_bValid = false;
  125. }
  126. void Update( const CViewSetup &viewSetup, const Vector &shadowDir, const color32 lightColor, const int lightColorScale, float flMaxShadowDist, float flMaxVisibleDist, uint nMaxCascadeSize, uint nAtlasFirstCascadeIndex, int nCSMQualityLevel, bool bSetAllCascadesToFirst );
  127. bool IsValid() const { return m_CSMParallelSplit.IsValid() && m_CSMParallelSplit.GetLightState().m_nShadowCascadeSize; }
  128. void Reset() { m_CSMParallelSplit.Reset(); }
  129. CFrustum m_sceneFrustum;
  130. float m_flSceneAspectRatio;
  131. VMatrix m_sceneWorldToView, m_sceneViewToProj, m_sceneWorldToProj;
  132. Vector m_shadowDir;
  133. float m_flMaxShadowDist;
  134. float m_flMaxVisibleDist;
  135. uint m_nMaxCascadeSize;
  136. CCSMParallelSplit m_CSMParallelSplit;
  137. bool m_bValid;
  138. };
  139. void RenderViews( CFullCSMState &state, bool bIncludeViewModels );
  140. int m_nDepthTextureResolution;
  141. CSMQualityMode_t m_nCurRenderTargetQualityMode;
  142. CFullCSMState m_curState;
  143. CFullCSMState m_curViewModelState;
  144. CFullCSMState m_capturedState;
  145. CDebugPrimRenderer2D m_debugPrimRenderer;
  146. bool m_bRenderTargetsAllocated;
  147. CTextureReference m_ShadowDepthTexture;
  148. CTextureReference m_DummyColorTexture;
  149. float m_flRotX[2], m_flRotY[2];
  150. bool m_bCSMIsActive;
  151. bool m_bStateIsValid;
  152. void DeinitRenderTargets();
  153. void DrawTextDebugInfo();
  154. Vector GetShadowDirection();
  155. inline CFullCSMState &GetActiveState() { return m_capturedState.m_CSMParallelSplit.IsValid() ? m_capturedState : m_curState; }
  156. };
  157. extern CCascadeLightManager g_CascadeLightManager;
  158. #endif // C_ENV_CASCADE_LIGHT_H