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.

194 lines
5.7 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef LIGHTCACHE_H
  8. #define LIGHTCACHE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "mathlib/vector.h"
  13. #include "tier2/tier2.h"
  14. #include "materialsystem/imaterialsystemhardwareconfig.h"
  15. #define MAXLOCALLIGHTS 4
  16. class IHandleEntity;
  17. struct dworldlight_t;
  18. FORWARD_DECLARE_HANDLE( LightCacheHandle_t );
  19. struct LightingState_t;
  20. typedef Vector AmbientCube_t[6];
  21. //-----------------------------------------------------------------------------
  22. // Flags to pass into LightIntensityAndDirectionAtPoint + LightIntensityAndDirectionInBox
  23. //-----------------------------------------------------------------------------
  24. enum LightIntensityFlags_t
  25. {
  26. LIGHT_NO_OCCLUSION_CHECK = 0x1,
  27. LIGHT_NO_RADIUS_CHECK = 0x2,
  28. LIGHT_OCCLUDE_VS_PROPS = 0x4,
  29. LIGHT_IGNORE_LIGHTSTYLE_VALUE = 0x8,
  30. };
  31. //-----------------------------------------------------------------------------
  32. // Adds a world light to the ambient cube
  33. //-----------------------------------------------------------------------------
  34. void AddWorldLightToAmbientCube( dworldlight_t* pWorldLight, const Vector &vecLightingOrigin, AmbientCube_t &ambientCube, bool bNoLightCull = false );
  35. struct LightingState_t
  36. {
  37. Vector r_boxcolor[6]; // ambient, and lights that aren't in locallight[]
  38. int numlights;
  39. dworldlight_t *locallight[MAXLOCALLIGHTS];
  40. void ZeroLightingState( void )
  41. {
  42. int i;
  43. for( i = 0; i < 6; i++ )
  44. {
  45. r_boxcolor[i].Init();
  46. }
  47. numlights = 0;
  48. }
  49. bool HasAmbientColors()
  50. {
  51. for ( int i = 0; i < 6; i++ )
  52. {
  53. if ( !r_boxcolor[i].IsZero(1e-4f) )
  54. return true;
  55. }
  56. return false;
  57. }
  58. void AddLocalLight( dworldlight_t *pLocalLight, const Vector &vecLightingOrigin )
  59. {
  60. if ( numlights >= MAXLOCALLIGHTS )
  61. return;
  62. for ( int i = 0; i < numlights; i++ )
  63. {
  64. if ( locallight[i] == pLocalLight )
  65. return;
  66. }
  67. // HACK for shipping: This isn't great to do in general,
  68. // but it's only used by code that is called to set decal lighting state.
  69. // It's not great since we may well be putting a strong light into the
  70. // ambient cube; a sort a higher level could fix this, but it's pretty complex
  71. // to do this and we want to limit risk
  72. // Prevent the total number of local lights from exceeding the max we can deal with
  73. extern ConVar r_worldlights;
  74. int nWorldLights = MIN( g_pMaterialSystemHardwareConfig->MaxNumLights(), r_worldlights.GetInt() );
  75. if ( numlights < nWorldLights )
  76. {
  77. locallight[numlights] = pLocalLight;
  78. numlights++;
  79. return;
  80. }
  81. AddWorldLightToAmbientCube( pLocalLight, vecLightingOrigin, r_boxcolor );
  82. }
  83. void AddAllLocalLights( const LightingState_t &src, const Vector &vecLightingOrigin )
  84. {
  85. for ( int i = 0; i < src.numlights; i++ )
  86. {
  87. AddLocalLight( src.locallight[i], vecLightingOrigin );
  88. }
  89. }
  90. void CopyLocalLights( const LightingState_t &src )
  91. {
  92. numlights = src.numlights;
  93. for ( int i = 0; i < src.numlights; i++ )
  94. {
  95. locallight[i] = src.locallight[i];
  96. }
  97. }
  98. LightingState_t()
  99. {
  100. ZeroLightingState();
  101. }
  102. };
  103. enum
  104. {
  105. LIGHTCACHEFLAGS_STATIC = 0x1,
  106. LIGHTCACHEFLAGS_DYNAMIC = 0x2,
  107. LIGHTCACHEFLAGS_LIGHTSTYLE = 0x4,
  108. LIGHTCACHEFLAGS_ALLOWFAST = 0x8,
  109. };
  110. class ITexture;
  111. // Called each frame to check for reinitializing the lightcache based on cvar changes.
  112. void R_StudioCheckReinitLightingCache();
  113. // static prop version
  114. #ifndef DEDICATED
  115. LightingState_t *LightcacheGetStatic( LightCacheHandle_t cache, ITexture **pEnvCubemap,
  116. unsigned int flags = ( LIGHTCACHEFLAGS_STATIC |
  117. LIGHTCACHEFLAGS_DYNAMIC |
  118. LIGHTCACHEFLAGS_LIGHTSTYLE ) );
  119. #endif
  120. // dynamic prop version
  121. struct LightcacheGetDynamic_Stats
  122. {
  123. bool m_bHasNonSwitchableLightStyles;
  124. bool m_bHasSwitchableLightStyles;
  125. bool m_bHasDLights;
  126. bool m_bNeedsSwitchableLightStyleUpdate;
  127. };
  128. #ifndef DEDICATED
  129. ITexture *LightcacheGetDynamic( const Vector& origin, LightingState_t& lightingState,
  130. LightcacheGetDynamic_Stats &stats, const IClientRenderable* pRenderable,
  131. unsigned int flags = ( LIGHTCACHEFLAGS_STATIC |
  132. LIGHTCACHEFLAGS_DYNAMIC |
  133. LIGHTCACHEFLAGS_LIGHTSTYLE ), bool bDebugModel=false );
  134. #endif
  135. // Reset the light cache.
  136. void R_StudioInitLightingCache( void );
  137. // force recomputation for static lighting cache entries
  138. void InvalidateStaticLightingCache(void);
  139. // Compute the comtribution of D- and E- lights at a point + normal
  140. void ComputeDynamicLighting( const Vector& pt, const Vector* pNormal, Vector& color );
  141. // Computes an average color (of sorts) at a particular point + optional normal
  142. void ComputeLighting( const Vector& pt, const Vector* pNormal, bool bClamp, bool bAddDynamicLightsToBox, Vector& color, Vector *pBoxColors );
  143. // Finds ambient lights
  144. dworldlight_t* FindAmbientLight();
  145. // Precache lighting
  146. LightCacheHandle_t CreateStaticLightingCache( const Vector& origin, const Vector& mins, const Vector& maxs );
  147. void ClearStaticLightingCache();
  148. // Computes the static vertex lighting term from a large number of spherical samples
  149. bool ComputeVertexLightingFromSphericalSamples( const Vector& vecVertex,
  150. const Vector &vecNormal, IHandleEntity *pIgnoreEnt, Vector *pLinearColor );
  151. bool StaticLightCacheAffectedByDynamicLight( LightCacheHandle_t handle );
  152. bool StaticLightCacheAffectedByAnimatedLightStyle( LightCacheHandle_t handle );
  153. bool StaticLightCacheNeedsSwitchableLightUpdate( LightCacheHandle_t handle );
  154. void InitDLightGlobals( int nMapVersion );
  155. // This is different for shipped HL2. . .
  156. extern float g_flMinLightingValue;
  157. #endif // LIGHTCACHE_H