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.

172 lines
4.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "c_baseanimating.h"
  9. #include "materialsystem/imaterialsystem.h"
  10. #include "model_types.h"
  11. #include "viewrender.h"
  12. #include "c_pixel_visibility.h"
  13. extern view_id_t CurrentViewID();
  14. extern bool IsMainView( view_id_t id );
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. class C_Prop_Hallucination : public C_BaseAnimating
  18. {
  19. public:
  20. DECLARE_CLASS( C_Prop_Hallucination, C_BaseAnimating );
  21. DECLARE_CLIENTCLASS();
  22. virtual void Spawn( void );
  23. virtual void UpdateOnRemove( void );
  24. //virtual bool ShouldDraw( void );
  25. virtual int DrawModel( int flags, const RenderableInstance_t &instance );
  26. virtual ShadowType_t ShadowCastType( void );
  27. virtual void OnDataChanged( DataUpdateType_t type );
  28. COcclusionQuerySet m_OcclusionSet;
  29. int m_iLastDrawCallFrame;
  30. bool m_bVisibleInAnyViewLastFrame;
  31. //float m_fVisibilityRemaining; //an ill defined measure of how much longer we're visible. When it reaches 0 we no longer show the hallucination until it's recharged to 1. Being occluded before reaching 0 is going to be up in the air on what to do.
  32. double m_fLastStateChangeTime;
  33. bool m_bVisibleEligible;
  34. bool m_bEnabled;
  35. float m_fVisibleTime;
  36. float m_fRechargeTime;
  37. static CMaterialReference sm_OcclusionProxyMaterial;
  38. };
  39. CMaterialReference C_Prop_Hallucination::sm_OcclusionProxyMaterial;
  40. IMPLEMENT_CLIENTCLASS_DT( C_Prop_Hallucination, DT_Prop_Hallucination, CProp_Hallucination )
  41. RecvPropBool( RECVINFO(m_bEnabled) ),
  42. RecvPropFloat( RECVINFO(m_fVisibleTime) ),
  43. RecvPropFloat( RECVINFO(m_fRechargeTime) ),
  44. END_RECV_TABLE()
  45. //ConVar cl_hallucination_dischargescale( "cl_hallucination_dischargescale", "3000.0", FCVAR_CHEAT, "(delta time) * (percentage of screen drawn to) * (this scale) is how much visibility charge (0.0-1.0) we use up per frame. When it's 0.0 the hallucination is invisible" );
  46. //ConVar cl_hallucination_visibletime( "cl_hallucination_visibletime", "0.215", FCVAR_CHEAT, "the maximum time (in seconds) from first visible frame to when we stop drawing the hallucination" );
  47. //ConVar cl_hallucination_rechargetime( "cl_hallucination_rechargetime", "2.0", FCVAR_CHEAT, "How much time must pass without drawing the hallucination before it's eligble to draw again" );
  48. void C_Prop_Hallucination::Spawn( void )
  49. {
  50. if( !sm_OcclusionProxyMaterial.IsValid() )
  51. {
  52. sm_OcclusionProxyMaterial.Init( "engine/occlusionproxy", TEXTURE_GROUP_CLIENT_EFFECTS );
  53. }
  54. BaseClass::Spawn();
  55. }
  56. void C_Prop_Hallucination::UpdateOnRemove( void )
  57. {
  58. BaseClass::UpdateOnRemove();
  59. }
  60. void C_Prop_Hallucination::OnDataChanged( DataUpdateType_t type )
  61. {
  62. if( !m_bEnabled )
  63. {
  64. if( m_bVisibleEligible && (m_fLastStateChangeTime != 0.0f) )
  65. {
  66. m_fLastStateChangeTime = Plat_FloatTime();
  67. }
  68. m_bVisibleEligible = false;
  69. }
  70. }
  71. int C_Prop_Hallucination::DrawModel( int flags, const RenderableInstance_t &instance )
  72. {
  73. switch( CurrentViewID() )
  74. {
  75. case VIEW_SHADOW_DEPTH_TEXTURE:
  76. if( m_bVisibleEligible )
  77. {
  78. return BaseClass::DrawModel( flags, instance );
  79. }
  80. default:
  81. if( IsMainView( CurrentViewID() ) ) //VIEW_MAIN and portal views are true
  82. {
  83. break;
  84. }
  85. else
  86. {
  87. return 0;
  88. }
  89. };
  90. bool bSameFrame = (m_iLastDrawCallFrame == gpGlobals->framecount);
  91. if( (flags & STUDIO_RENDER) && !bSameFrame )
  92. {
  93. bool bDrewLastFrame = (m_iLastDrawCallFrame == (gpGlobals->framecount - 1));
  94. if( m_bVisibleEligible )
  95. {
  96. if( m_fLastStateChangeTime == 0.0 ) //waiting for the first frame of actual visibility mark the state change
  97. {
  98. if( bDrewLastFrame && (m_OcclusionSet.QueryNumPixelsRenderedForAllViewsLastFrame() > 0) )
  99. {
  100. m_fLastStateChangeTime = Plat_FloatTime();// - gpGlobals->realtime; //state change occurred last frame
  101. }
  102. }
  103. else if( (Plat_FloatTime() - m_fLastStateChangeTime) > m_fVisibleTime )
  104. {
  105. m_bVisibleEligible = false;
  106. }
  107. }
  108. else
  109. {
  110. if( ((m_fLastStateChangeTime == 0.0f) || (Plat_FloatTime() - m_fLastStateChangeTime) > m_fRechargeTime) &&
  111. ((m_OcclusionSet.QueryNumPixelsRenderedForAllViewsLastFrame() == 0) || !bDrewLastFrame) )
  112. {
  113. m_bVisibleEligible = true;
  114. m_fLastStateChangeTime = 0.0f;
  115. }
  116. }
  117. }
  118. if( flags & STUDIO_RENDER )
  119. {
  120. if( !m_bVisibleEligible )
  121. {
  122. modelrender->ForcedMaterialOverride( sm_OcclusionProxyMaterial );
  123. }
  124. m_iLastDrawCallFrame = gpGlobals->framecount;
  125. m_OcclusionSet.BeginQueryDrawing();
  126. int iRetVal = BaseClass::DrawModel( flags, instance );
  127. m_OcclusionSet.EndQueryDrawing();
  128. if( !m_bVisibleEligible )
  129. {
  130. modelrender->ForcedMaterialOverride( NULL );
  131. }
  132. return iRetVal;
  133. }
  134. else
  135. {
  136. return BaseClass::DrawModel( flags, instance );
  137. }
  138. }
  139. ShadowType_t C_Prop_Hallucination::ShadowCastType( void )
  140. {
  141. return SHADOWS_NONE;
  142. }