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.

218 lines
6.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Functionality to render a glowing outline around client renderable objects.
  4. //
  5. //===============================================================================
  6. #ifndef GLOW_OUTLINE_EFFECT_H
  7. #define GLOW_OUTLINE_EFFECT_H
  8. #if defined( COMPILER_MSVC )
  9. #pragma once
  10. #endif
  11. #include "utlvector.h"
  12. #include "mathlib/vector.h"
  13. #ifdef GLOWS_ENABLE
  14. class C_BaseEntity;
  15. class CViewSetup;
  16. class CMatRenderContextPtr;
  17. static const int GLOW_FOR_ALL_SPLIT_SCREEN_SLOTS = -1;
  18. class CGlowObjectManager
  19. {
  20. public:
  21. CGlowObjectManager() :
  22. m_nFirstFreeSlot( GlowObjectDefinition_t::END_OF_FREE_LIST )
  23. {
  24. }
  25. int RegisterGlowObject( C_BaseEntity *pEntity, const Vector &vGlowColor, float flGlowAlpha, bool bRenderWhenOccluded, bool bRenderWhenUnoccluded, int nSplitScreenSlot )
  26. {
  27. int nIndex;
  28. if ( m_nFirstFreeSlot == GlowObjectDefinition_t::END_OF_FREE_LIST )
  29. {
  30. nIndex = m_GlowObjectDefinitions.AddToTail();
  31. }
  32. else
  33. {
  34. nIndex = m_nFirstFreeSlot;
  35. m_nFirstFreeSlot = m_GlowObjectDefinitions[nIndex].m_nNextFreeSlot;
  36. }
  37. m_GlowObjectDefinitions[nIndex].m_hEntity = pEntity;
  38. m_GlowObjectDefinitions[nIndex].m_vGlowColor = vGlowColor;
  39. m_GlowObjectDefinitions[nIndex].m_flGlowAlpha = flGlowAlpha;
  40. m_GlowObjectDefinitions[nIndex].m_bRenderWhenOccluded = bRenderWhenOccluded;
  41. m_GlowObjectDefinitions[nIndex].m_bRenderWhenUnoccluded = bRenderWhenUnoccluded;
  42. m_GlowObjectDefinitions[nIndex].m_nSplitScreenSlot = nSplitScreenSlot;
  43. m_GlowObjectDefinitions[nIndex].m_nNextFreeSlot = GlowObjectDefinition_t::ENTRY_IN_USE;
  44. return nIndex;
  45. }
  46. void UnregisterGlowObject( int nGlowObjectHandle )
  47. {
  48. Assert( !m_GlowObjectDefinitions[nGlowObjectHandle].IsUnused() );
  49. m_GlowObjectDefinitions[nGlowObjectHandle].m_nNextFreeSlot = m_nFirstFreeSlot;
  50. m_GlowObjectDefinitions[nGlowObjectHandle].m_hEntity = NULL;
  51. m_nFirstFreeSlot = nGlowObjectHandle;
  52. }
  53. void SetEntity( int nGlowObjectHandle, C_BaseEntity *pEntity )
  54. {
  55. Assert( !m_GlowObjectDefinitions[nGlowObjectHandle].IsUnused() );
  56. m_GlowObjectDefinitions[nGlowObjectHandle].m_hEntity = pEntity;
  57. }
  58. void SetColor( int nGlowObjectHandle, const Vector &vGlowColor )
  59. {
  60. Assert( !m_GlowObjectDefinitions[nGlowObjectHandle].IsUnused() );
  61. m_GlowObjectDefinitions[nGlowObjectHandle].m_vGlowColor = vGlowColor;
  62. }
  63. void SetAlpha( int nGlowObjectHandle, float flAlpha )
  64. {
  65. Assert( !m_GlowObjectDefinitions[nGlowObjectHandle].IsUnused() );
  66. m_GlowObjectDefinitions[nGlowObjectHandle].m_flGlowAlpha = flAlpha;
  67. }
  68. void SetRenderFlags( int nGlowObjectHandle, bool bRenderWhenOccluded, bool bRenderWhenUnoccluded )
  69. {
  70. Assert( !m_GlowObjectDefinitions[nGlowObjectHandle].IsUnused() );
  71. m_GlowObjectDefinitions[nGlowObjectHandle].m_bRenderWhenOccluded = bRenderWhenOccluded;
  72. m_GlowObjectDefinitions[nGlowObjectHandle].m_bRenderWhenUnoccluded = bRenderWhenUnoccluded;
  73. }
  74. bool IsRenderingWhenOccluded( int nGlowObjectHandle ) const
  75. {
  76. Assert( !m_GlowObjectDefinitions[nGlowObjectHandle].IsUnused() );
  77. return m_GlowObjectDefinitions[nGlowObjectHandle].m_bRenderWhenOccluded;
  78. }
  79. bool IsRenderingWhenUnoccluded( int nGlowObjectHandle ) const
  80. {
  81. Assert( !m_GlowObjectDefinitions[nGlowObjectHandle].IsUnused() );
  82. return m_GlowObjectDefinitions[nGlowObjectHandle].m_bRenderWhenUnoccluded;
  83. }
  84. bool HasGlowEffect( C_BaseEntity *pEntity ) const
  85. {
  86. for ( int i = 0; i < m_GlowObjectDefinitions.Count(); ++ i )
  87. {
  88. if ( !m_GlowObjectDefinitions[i].IsUnused() && m_GlowObjectDefinitions[i].m_hEntity.Get() == pEntity )
  89. {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. void RenderGlowEffects( const CViewSetup *pSetup, int nSplitScreenSlot );
  96. private:
  97. void RenderGlowModels( const CViewSetup *pSetup, int nSplitScreenSlot, CMatRenderContextPtr &pRenderContext );
  98. void ApplyEntityGlowEffects( const CViewSetup *pSetup, int nSplitScreenSlot, CMatRenderContextPtr &pRenderContext, float flBloomScale, int x, int y, int w, int h );
  99. struct GlowObjectDefinition_t
  100. {
  101. bool ShouldDraw( int nSlot ) const
  102. {
  103. return m_hEntity.Get() &&
  104. ( m_nSplitScreenSlot == GLOW_FOR_ALL_SPLIT_SCREEN_SLOTS || m_nSplitScreenSlot == nSlot ) &&
  105. ( m_bRenderWhenOccluded || m_bRenderWhenUnoccluded ) &&
  106. m_hEntity->ShouldDraw() &&
  107. !m_hEntity->IsDormant();
  108. }
  109. bool IsUnused() const { return m_nNextFreeSlot != GlowObjectDefinition_t::ENTRY_IN_USE; }
  110. void DrawModel();
  111. EHANDLE m_hEntity;
  112. Vector m_vGlowColor;
  113. float m_flGlowAlpha;
  114. bool m_bRenderWhenOccluded;
  115. bool m_bRenderWhenUnoccluded;
  116. int m_nSplitScreenSlot;
  117. // Linked list of free slots
  118. int m_nNextFreeSlot;
  119. // Special values for GlowObjectDefinition_t::m_nNextFreeSlot
  120. static const int END_OF_FREE_LIST = -1;
  121. static const int ENTRY_IN_USE = -2;
  122. };
  123. CUtlVector< GlowObjectDefinition_t > m_GlowObjectDefinitions;
  124. int m_nFirstFreeSlot;
  125. };
  126. extern CGlowObjectManager g_GlowObjectManager;
  127. class CGlowObject
  128. {
  129. public:
  130. CGlowObject( C_BaseEntity *pEntity, const Vector &vGlowColor = Vector( 1.0f, 1.0f, 1.0f ), float flGlowAlpha = 1.0f, bool bRenderWhenOccluded = false, bool bRenderWhenUnoccluded = false, int nSplitScreenSlot = GLOW_FOR_ALL_SPLIT_SCREEN_SLOTS )
  131. {
  132. m_nGlowObjectHandle = g_GlowObjectManager.RegisterGlowObject( pEntity, vGlowColor, flGlowAlpha, bRenderWhenOccluded, bRenderWhenUnoccluded, nSplitScreenSlot );
  133. }
  134. ~CGlowObject()
  135. {
  136. g_GlowObjectManager.UnregisterGlowObject( m_nGlowObjectHandle );
  137. }
  138. void SetEntity( C_BaseEntity *pEntity )
  139. {
  140. g_GlowObjectManager.SetEntity( m_nGlowObjectHandle, pEntity );
  141. }
  142. void SetColor( const Vector &vGlowColor )
  143. {
  144. g_GlowObjectManager.SetColor( m_nGlowObjectHandle, vGlowColor );
  145. }
  146. void SetAlpha( float flAlpha )
  147. {
  148. g_GlowObjectManager.SetAlpha( m_nGlowObjectHandle, flAlpha );
  149. }
  150. void SetRenderFlags( bool bRenderWhenOccluded, bool bRenderWhenUnoccluded )
  151. {
  152. g_GlowObjectManager.SetRenderFlags( m_nGlowObjectHandle, bRenderWhenOccluded, bRenderWhenUnoccluded );
  153. }
  154. bool IsRenderingWhenOccluded() const
  155. {
  156. return g_GlowObjectManager.IsRenderingWhenOccluded( m_nGlowObjectHandle );
  157. }
  158. bool IsRenderingWhenUnoccluded() const
  159. {
  160. return g_GlowObjectManager.IsRenderingWhenUnoccluded( m_nGlowObjectHandle );
  161. }
  162. bool IsRendering() const
  163. {
  164. return IsRenderingWhenOccluded() || IsRenderingWhenUnoccluded();
  165. }
  166. // Add more accessors/mutators here as needed
  167. private:
  168. int m_nGlowObjectHandle;
  169. // Assignment & copy-construction disallowed
  170. CGlowObject( const CGlowObject &other );
  171. CGlowObject& operator=( const CGlowObject &other );
  172. };
  173. #endif // GLOWS_ENABLE
  174. #endif // GLOW_OUTLINE_EFFECT_H