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.

158 lines
4.3 KiB

  1. //========= Copyright 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. #define MAXLOCALLIGHTS 4
  14. class IHandleEntity;
  15. struct dworldlight_t;
  16. FORWARD_DECLARE_HANDLE( LightCacheHandle_t );
  17. struct LightingState_t;
  18. typedef Vector AmbientCube_t[6];
  19. struct LightingState_t
  20. {
  21. Vector r_boxcolor[6]; // ambient, and lights that aren't in locallight[]
  22. int numlights;
  23. dworldlight_t *locallight[MAXLOCALLIGHTS];
  24. void ZeroLightingState( void )
  25. {
  26. int i;
  27. for( i = 0; i < 6; i++ )
  28. {
  29. r_boxcolor[i].Init();
  30. }
  31. numlights = 0;
  32. }
  33. bool HasAmbientColors()
  34. {
  35. for ( int i = 0; i < 6; i++ )
  36. {
  37. if ( !r_boxcolor[i].IsZero(1e-4f) )
  38. return true;
  39. }
  40. return false;
  41. }
  42. void AddLocalLight( dworldlight_t *pLocalLight )
  43. {
  44. if ( numlights >= MAXLOCALLIGHTS )
  45. return;
  46. for ( int i = 0; i < numlights; i++ )
  47. {
  48. if ( locallight[i] == pLocalLight )
  49. return;
  50. }
  51. locallight[numlights] = pLocalLight;
  52. numlights++;
  53. }
  54. void AddAllLocalLights( const LightingState_t &src )
  55. {
  56. for ( int i = 0; i < src.numlights; i++ )
  57. {
  58. AddLocalLight( src.locallight[i] );
  59. }
  60. }
  61. void CopyLocalLights( const LightingState_t &src )
  62. {
  63. numlights = src.numlights;
  64. for ( int i = 0; i < src.numlights; i++ )
  65. {
  66. locallight[i] = src.locallight[i];
  67. }
  68. }
  69. LightingState_t()
  70. {
  71. ZeroLightingState();
  72. }
  73. };
  74. enum
  75. {
  76. LIGHTCACHEFLAGS_STATIC = 0x1,
  77. LIGHTCACHEFLAGS_DYNAMIC = 0x2,
  78. LIGHTCACHEFLAGS_LIGHTSTYLE = 0x4,
  79. LIGHTCACHEFLAGS_ALLOWFAST = 0x8,
  80. };
  81. class ITexture;
  82. // Called each frame to check for reinitializing the lightcache based on cvar changes.
  83. void R_StudioCheckReinitLightingCache();
  84. // static prop version
  85. LightingState_t *LightcacheGetStatic( LightCacheHandle_t cache, ITexture **pEnvCubemap,
  86. unsigned int flags = ( LIGHTCACHEFLAGS_STATIC |
  87. LIGHTCACHEFLAGS_DYNAMIC |
  88. LIGHTCACHEFLAGS_LIGHTSTYLE ) );
  89. // dynamic prop version
  90. struct LightcacheGetDynamic_Stats
  91. {
  92. bool m_bHasNonSwitchableLightStyles;
  93. bool m_bHasSwitchableLightStyles;
  94. bool m_bHasDLights;
  95. bool m_bNeedsSwitchableLightStyleUpdate;
  96. };
  97. ITexture *LightcacheGetDynamic( const Vector& origin, LightingState_t& lightingState,
  98. LightcacheGetDynamic_Stats &stats,
  99. unsigned int flags = ( LIGHTCACHEFLAGS_STATIC |
  100. LIGHTCACHEFLAGS_DYNAMIC |
  101. LIGHTCACHEFLAGS_LIGHTSTYLE ), bool bDebugModel=false );
  102. // Reset the light cache.
  103. void R_StudioInitLightingCache( void );
  104. // force recomputation for static lighting cache entries
  105. void InvalidateStaticLightingCache(void);
  106. // Compute the comtribution of D- and E- lights at a point + normal
  107. void ComputeDynamicLighting( const Vector& pt, const Vector* pNormal, Vector& color );
  108. // Computes an average color (of sorts) at a particular point + optional normal
  109. void ComputeLighting( const Vector& pt, const Vector* pNormal, bool bClamp, Vector& color, Vector *pBoxColors );
  110. // Finds ambient lights
  111. dworldlight_t* FindAmbientLight();
  112. // Precache lighting
  113. LightCacheHandle_t CreateStaticLightingCache( const Vector& origin, const Vector& mins, const Vector& maxs );
  114. void ClearStaticLightingCache();
  115. // Computes the static vertex lighting term from a large number of spherical samples
  116. bool ComputeVertexLightingFromSphericalSamples( const Vector& vecVertex,
  117. const Vector &vecNormal, IHandleEntity *pIgnoreEnt, Vector *pLinearColor );
  118. bool StaticLightCacheAffectedByDynamicLight( LightCacheHandle_t handle );
  119. bool StaticLightCacheAffectedByAnimatedLightStyle( LightCacheHandle_t handle );
  120. bool StaticLightCacheNeedsSwitchableLightUpdate( LightCacheHandle_t handle );
  121. //-----------------------------------------------------------------------------
  122. // Adds a world light to the ambient cube
  123. //-----------------------------------------------------------------------------
  124. void AddWorldLightToAmbientCube( dworldlight_t* pWorldLight, const Vector &vecLightingOrigin, AmbientCube_t &ambientCube );
  125. void InitDLightGlobals( int nMapVersion );
  126. // This is different for shipped HL2. . .
  127. extern float g_flMinLightingValue;
  128. #endif // LIGHTCACHE_H