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.

215 lines
5.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "glow_overlay.h"
  9. #include "view.h"
  10. #include "c_pixel_visibility.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. class C_LightGlowOverlay : public CGlowOverlay
  14. {
  15. public:
  16. virtual void CalcSpriteColorAndSize( float flDot, CGlowSprite *pSprite, float *flHorzSize, float *flVertSize, Vector *vColor )
  17. {
  18. *flHorzSize = pSprite->m_flHorzSize;
  19. *flVertSize = pSprite->m_flVertSize;
  20. Vector viewDir = ( CurrentViewOrigin() - m_vecOrigin );
  21. float distToViewer = VectorNormalize( viewDir );
  22. if ( m_bOneSided )
  23. {
  24. if ( DotProduct( viewDir, m_vecDirection ) < 0.0f )
  25. {
  26. *vColor = Vector(0,0,0);
  27. return;
  28. }
  29. }
  30. float fade;
  31. // See if we're in the outer fade distance range
  32. if ( m_nOuterMaxDist > m_nMaxDist && distToViewer > m_nMaxDist )
  33. {
  34. fade = RemapValClamped( distToViewer, m_nMaxDist, m_nOuterMaxDist, 1.0f, 0.0f );
  35. }
  36. else
  37. {
  38. fade = RemapValClamped( distToViewer, m_nMinDist, m_nMaxDist, 0.0f, 1.0f );
  39. }
  40. *vColor = pSprite->m_vColor * fade * m_flGlowObstructionScale;
  41. }
  42. void SetOrigin( const Vector &origin ) { m_vecOrigin = origin; }
  43. void SetFadeDistances( int minDist, int maxDist, int outerMaxDist )
  44. {
  45. m_nMinDist = minDist;
  46. m_nMaxDist = maxDist;
  47. m_nOuterMaxDist = outerMaxDist;
  48. }
  49. void SetOneSided( bool state = true ) { m_bOneSided = state; }
  50. void SetModulateByDot( bool state = true ) { m_bModulateByDot = state; }
  51. void SetDirection( const Vector &dir ) { m_vecDirection = dir; VectorNormalize( m_vecDirection ); }
  52. protected:
  53. Vector m_vecOrigin;
  54. Vector m_vecDirection;
  55. int m_nMinDist;
  56. int m_nMaxDist;
  57. int m_nOuterMaxDist;
  58. bool m_bOneSided;
  59. bool m_bModulateByDot;
  60. };
  61. //-----------------------------------------------------------------------------
  62. // Purpose:
  63. //-----------------------------------------------------------------------------
  64. class C_LightGlow : public C_BaseEntity
  65. {
  66. public:
  67. DECLARE_CLASS( C_LightGlow, C_BaseEntity );
  68. DECLARE_CLIENTCLASS();
  69. C_LightGlow();
  70. // C_BaseEntity overrides.
  71. public:
  72. virtual void OnDataChanged( DataUpdateType_t updateType );
  73. virtual void Simulate( void );
  74. virtual void ClientThink( void );
  75. public:
  76. int m_nHorizontalSize;
  77. int m_nVerticalSize;
  78. int m_nMinDist;
  79. int m_nMaxDist;
  80. int m_nOuterMaxDist;
  81. int m_spawnflags;
  82. C_LightGlowOverlay m_Glow;
  83. float m_flGlowProxySize;
  84. };
  85. static void RecvProxy_HDRColorScale( const CRecvProxyData *pData, void *pStruct, void *pOut )
  86. {
  87. C_LightGlow *pLightGlow = ( C_LightGlow * )pStruct;
  88. pLightGlow->m_Glow.m_flHDRColorScale = pData->m_Value.m_Float;
  89. }
  90. IMPLEMENT_CLIENTCLASS_DT_NOBASE( C_LightGlow, DT_LightGlow, CLightGlow )
  91. RecvPropInt( RECVINFO(m_clrRender), 0, RecvProxy_IntToColor32 ),
  92. RecvPropInt( RECVINFO( m_nHorizontalSize ) ),
  93. RecvPropInt( RECVINFO( m_nVerticalSize ) ),
  94. RecvPropInt( RECVINFO( m_nMinDist ) ),
  95. RecvPropInt( RECVINFO( m_nMaxDist ) ),
  96. RecvPropInt( RECVINFO( m_nOuterMaxDist ) ),
  97. RecvPropInt( RECVINFO( m_spawnflags ) ),
  98. RecvPropVector( RECVINFO_NAME( m_vecNetworkOrigin, m_vecOrigin ) ),
  99. RecvPropQAngles( RECVINFO_NAME( m_angNetworkAngles, m_angRotation ) ),
  100. RecvPropInt( RECVINFO_NAME(m_hNetworkMoveParent, moveparent), 0, RecvProxy_IntToMoveParent ),
  101. RecvPropFloat(RECVINFO(m_flGlowProxySize)),
  102. RecvPropFloat("HDRColorScale", 0, SIZEOF_IGNORE, 0, RecvProxy_HDRColorScale),
  103. END_RECV_TABLE()
  104. //-----------------------------------------------------------------------------
  105. // Constructor
  106. //-----------------------------------------------------------------------------
  107. C_LightGlow::C_LightGlow() :
  108. m_nHorizontalSize( 0 ), m_nVerticalSize( 0 ), m_nMinDist( 0 ), m_nMaxDist( 0 )
  109. {
  110. m_Glow.m_bDirectional = false;
  111. m_Glow.m_bInSky = false;
  112. }
  113. void C_LightGlow::Simulate( void )
  114. {
  115. BaseClass::Simulate();
  116. m_Glow.m_vPos = GetAbsOrigin();
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose:
  120. // Input : updateType -
  121. //-----------------------------------------------------------------------------
  122. void C_LightGlow::OnDataChanged( DataUpdateType_t updateType )
  123. {
  124. BaseClass::OnDataChanged( updateType );
  125. m_Glow.m_vPos = GetAbsOrigin();
  126. if ( updateType == DATA_UPDATE_CREATED )
  127. {
  128. // Setup our flare.
  129. Vector vColor(
  130. m_clrRender->r / 255.0f,
  131. m_clrRender->g / 255.0f,
  132. m_clrRender->b / 255.0f );
  133. m_Glow.m_nSprites = 1;
  134. m_Glow.m_Sprites[0].m_flVertSize = (float) m_nVerticalSize;
  135. m_Glow.m_Sprites[0].m_flHorzSize = (float) m_nHorizontalSize;
  136. m_Glow.m_Sprites[0].m_vColor = vColor;
  137. m_Glow.SetOrigin( GetAbsOrigin() );
  138. m_Glow.SetFadeDistances( m_nMinDist, m_nMaxDist, m_nOuterMaxDist );
  139. m_Glow.m_flProxyRadius = m_flGlowProxySize;
  140. if ( m_spawnflags & SF_LIGHTGLOW_DIRECTIONAL )
  141. {
  142. m_Glow.SetOneSided();
  143. }
  144. SetNextClientThink( gpGlobals->curtime + RandomFloat(0,3.0) );
  145. }
  146. else if ( updateType == DATA_UPDATE_DATATABLE_CHANGED ) //Right now only color should change.
  147. {
  148. // Setup our flare.
  149. Vector vColor(
  150. m_clrRender->r / 255.0f,
  151. m_clrRender->g / 255.0f,
  152. m_clrRender->b / 255.0f );
  153. m_Glow.m_Sprites[0].m_vColor = vColor;
  154. }
  155. Vector forward;
  156. AngleVectors( GetAbsAngles(), &forward, NULL, NULL );
  157. m_Glow.SetDirection( forward );
  158. }
  159. //-----------------------------------------------------------------------------
  160. // Purpose:
  161. //-----------------------------------------------------------------------------
  162. void C_LightGlow::ClientThink( void )
  163. {
  164. Vector mins = GetAbsOrigin();
  165. if ( engine->IsBoxVisible( mins, mins ) )
  166. {
  167. m_Glow.Activate();
  168. }
  169. else
  170. {
  171. m_Glow.Deactivate();
  172. }
  173. SetNextClientThink( gpGlobals->curtime + RandomFloat(1.0,3.0) );
  174. }