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.

249 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "baseentity.h"
  8. #include "entityoutput.h"
  9. #include "convar.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. ConVar mat_hdr_tonemapscale( "mat_hdr_tonemapscale", "1.0", FCVAR_CHEAT, "The HDR tonemap scale. 1 = Use autoexposure, 0 = eyes fully closed, 16 = eyes wide open." );
  13. // 0 - eyes fully closed / fully black
  14. // 1 - nominal
  15. // 16 - eyes wide open / fully white
  16. //-----------------------------------------------------------------------------
  17. // Purpose: Entity that controls player's tonemap
  18. //-----------------------------------------------------------------------------
  19. class CEnvTonemapController : public CPointEntity
  20. {
  21. DECLARE_CLASS( CEnvTonemapController, CPointEntity );
  22. public:
  23. DECLARE_DATADESC();
  24. DECLARE_SERVERCLASS();
  25. void Spawn( void );
  26. int UpdateTransmitState( void );
  27. void UpdateTonemapScaleBlend( void );
  28. // Inputs
  29. void InputSetTonemapScale( inputdata_t &inputdata );
  30. void InputBlendTonemapScale( inputdata_t &inputdata );
  31. void InputSetTonemapRate( inputdata_t &inputdata );
  32. void InputSetAutoExposureMin( inputdata_t &inputdata );
  33. void InputSetAutoExposureMax( inputdata_t &inputdata );
  34. void InputUseDefaultAutoExposure( inputdata_t &inputdata );
  35. void InputSetBloomScale( inputdata_t &inputdata );
  36. void InputUseDefaultBloomScale( inputdata_t &inputdata );
  37. void InputSetBloomScaleRange( inputdata_t &inputdata );
  38. private:
  39. float m_flBlendTonemapStart; // HDR Tonemap at the start of the blend
  40. float m_flBlendTonemapEnd; // Target HDR Tonemap at the end of the blend
  41. float m_flBlendEndTime; // Time at which the blend ends
  42. float m_flBlendStartTime; // Time at which the blend started
  43. CNetworkVar( bool, m_bUseCustomAutoExposureMin );
  44. CNetworkVar( bool, m_bUseCustomAutoExposureMax );
  45. CNetworkVar( bool, m_bUseCustomBloomScale );
  46. CNetworkVar( float, m_flCustomAutoExposureMin );
  47. CNetworkVar( float, m_flCustomAutoExposureMax );
  48. CNetworkVar( float, m_flCustomBloomScale);
  49. CNetworkVar( float, m_flCustomBloomScaleMinimum);
  50. };
  51. LINK_ENTITY_TO_CLASS( env_tonemap_controller, CEnvTonemapController );
  52. BEGIN_DATADESC( CEnvTonemapController )
  53. DEFINE_FIELD( m_flBlendTonemapStart, FIELD_FLOAT ),
  54. DEFINE_FIELD( m_flBlendTonemapEnd, FIELD_FLOAT ),
  55. DEFINE_FIELD( m_flBlendEndTime, FIELD_TIME ),
  56. DEFINE_FIELD( m_flBlendStartTime, FIELD_TIME ),
  57. DEFINE_FIELD( m_bUseCustomAutoExposureMin, FIELD_BOOLEAN ),
  58. DEFINE_FIELD( m_bUseCustomAutoExposureMax, FIELD_BOOLEAN ),
  59. DEFINE_FIELD( m_flCustomAutoExposureMin, FIELD_FLOAT ),
  60. DEFINE_FIELD( m_flCustomAutoExposureMax, FIELD_FLOAT ),
  61. DEFINE_FIELD( m_flCustomBloomScale, FIELD_FLOAT ),
  62. DEFINE_FIELD( m_flCustomBloomScaleMinimum, FIELD_FLOAT ),
  63. DEFINE_FIELD( m_bUseCustomBloomScale, FIELD_BOOLEAN ),
  64. DEFINE_THINKFUNC( UpdateTonemapScaleBlend ),
  65. // Inputs
  66. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetTonemapScale", InputSetTonemapScale ),
  67. DEFINE_INPUTFUNC( FIELD_STRING, "BlendTonemapScale", InputBlendTonemapScale ),
  68. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetTonemapRate", InputSetTonemapRate ),
  69. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetAutoExposureMin", InputSetAutoExposureMin ),
  70. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetAutoExposureMax", InputSetAutoExposureMax ),
  71. DEFINE_INPUTFUNC( FIELD_VOID, "UseDefaultAutoExposure", InputUseDefaultAutoExposure ),
  72. DEFINE_INPUTFUNC( FIELD_VOID, "UseDefaultBloomScale", InputUseDefaultBloomScale ),
  73. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetBloomScale", InputSetBloomScale ),
  74. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetBloomScaleRange", InputSetBloomScaleRange ),
  75. END_DATADESC()
  76. IMPLEMENT_SERVERCLASS_ST( CEnvTonemapController, DT_EnvTonemapController )
  77. SendPropInt( SENDINFO(m_bUseCustomAutoExposureMin), 1, SPROP_UNSIGNED ),
  78. SendPropInt( SENDINFO(m_bUseCustomAutoExposureMax), 1, SPROP_UNSIGNED ),
  79. SendPropInt( SENDINFO(m_bUseCustomBloomScale), 1, SPROP_UNSIGNED ),
  80. SendPropFloat( SENDINFO(m_flCustomAutoExposureMin), 0, SPROP_NOSCALE),
  81. SendPropFloat( SENDINFO(m_flCustomAutoExposureMax), 0, SPROP_NOSCALE),
  82. SendPropFloat( SENDINFO(m_flCustomBloomScale), 0, SPROP_NOSCALE),
  83. SendPropFloat( SENDINFO(m_flCustomBloomScaleMinimum), 0, SPROP_NOSCALE),
  84. END_SEND_TABLE()
  85. //-----------------------------------------------------------------------------
  86. // Purpose:
  87. //-----------------------------------------------------------------------------
  88. void CEnvTonemapController::Spawn( void )
  89. {
  90. SetSolid( SOLID_NONE );
  91. SetMoveType( MOVETYPE_NONE );
  92. }
  93. //-----------------------------------------------------------------------------
  94. // Purpose:
  95. //-----------------------------------------------------------------------------
  96. int CEnvTonemapController::UpdateTransmitState()
  97. {
  98. return SetTransmitState( FL_EDICT_ALWAYS );
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Purpose: Set the tonemap scale to the specified value
  102. //-----------------------------------------------------------------------------
  103. void CEnvTonemapController::InputSetTonemapScale( inputdata_t &inputdata )
  104. {
  105. float flRemapped = inputdata.value.Float();
  106. mat_hdr_tonemapscale.SetValue( flRemapped );
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Purpose: Blend the tonemap scale to the specified value
  110. //-----------------------------------------------------------------------------
  111. void CEnvTonemapController::InputBlendTonemapScale( inputdata_t &inputdata )
  112. {
  113. char parseString[255];
  114. Q_strncpy(parseString, inputdata.value.String(), sizeof(parseString));
  115. // Get the target tonemap scale
  116. char *pszParam = strtok(parseString," ");
  117. if ( !pszParam || !pszParam[0] )
  118. {
  119. Warning("%s (%s) received BlendTonemapScale input without a target tonemap scale. Syntax: <target tonemap scale> <blend time>\n", GetClassname(), GetDebugName() );
  120. return;
  121. }
  122. m_flBlendTonemapEnd = atof( pszParam );
  123. // Get the blend time
  124. pszParam = strtok(NULL," ");
  125. if ( !pszParam || !pszParam[0] )
  126. {
  127. Warning("%s (%s) received BlendTonemapScale input without a blend time. Syntax: <target tonemap scale> <blend time>\n", GetClassname(), GetDebugName() );
  128. return;
  129. }
  130. m_flBlendEndTime = gpGlobals->curtime + atof( pszParam );
  131. m_flBlendStartTime = gpGlobals->curtime;
  132. m_flBlendTonemapStart = mat_hdr_tonemapscale.GetFloat();
  133. // Start thinking
  134. SetNextThink( gpGlobals->curtime + 0.1 );
  135. SetThink( &CEnvTonemapController::UpdateTonemapScaleBlend );
  136. }
  137. //-----------------------------------------------------------------------------
  138. // Purpose: set a base and minimum bloom scale
  139. //-----------------------------------------------------------------------------
  140. void CEnvTonemapController::InputSetBloomScaleRange( inputdata_t &inputdata )
  141. {
  142. float bloom_max=1, bloom_min=1;
  143. int nargs=sscanf("%f %f",inputdata.value.String(), bloom_max, bloom_min );
  144. if (nargs != 2)
  145. {
  146. Warning("%s (%s) received SetBloomScaleRange input without 2 arguments. Syntax: <max bloom> <min bloom>\n", GetClassname(), GetDebugName() );
  147. return;
  148. }
  149. m_flCustomBloomScale=bloom_max;
  150. m_flCustomBloomScaleMinimum=bloom_min;
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Purpose:
  154. //-----------------------------------------------------------------------------
  155. void CEnvTonemapController::InputSetTonemapRate( inputdata_t &inputdata )
  156. {
  157. // TODO: There should be a better way to do this.
  158. ConVarRef mat_hdr_manual_tonemap_rate( "mat_hdr_manual_tonemap_rate" );
  159. if ( mat_hdr_manual_tonemap_rate.IsValid() )
  160. {
  161. float flTonemapRate = inputdata.value.Float();
  162. mat_hdr_manual_tonemap_rate.SetValue( flTonemapRate );
  163. }
  164. }
  165. //-----------------------------------------------------------------------------
  166. // Purpose: Blend the tonemap scale to the specified value
  167. //-----------------------------------------------------------------------------
  168. void CEnvTonemapController::UpdateTonemapScaleBlend( void )
  169. {
  170. float flRemapped = RemapValClamped( gpGlobals->curtime, m_flBlendStartTime, m_flBlendEndTime, m_flBlendTonemapStart, m_flBlendTonemapEnd );
  171. mat_hdr_tonemapscale.SetValue( flRemapped );
  172. //Msg("Setting tonemap scale to %f (curtime %f, %f -> %f)\n", flRemapped, gpGlobals->curtime, m_flBlendStartTime, m_flBlendEndTime );
  173. // Stop when we're out of the blend range
  174. if ( gpGlobals->curtime >= m_flBlendEndTime )
  175. return;
  176. SetNextThink( gpGlobals->curtime + 0.1 );
  177. }
  178. //-----------------------------------------------------------------------------
  179. // Purpose: Set the auto exposure min to the specified value
  180. //-----------------------------------------------------------------------------
  181. void CEnvTonemapController::InputSetAutoExposureMin( inputdata_t &inputdata )
  182. {
  183. m_flCustomAutoExposureMin = inputdata.value.Float();
  184. m_bUseCustomAutoExposureMin = true;
  185. }
  186. //-----------------------------------------------------------------------------
  187. // Purpose: Set the auto exposure max to the specified value
  188. //-----------------------------------------------------------------------------
  189. void CEnvTonemapController::InputSetAutoExposureMax( inputdata_t &inputdata )
  190. {
  191. m_flCustomAutoExposureMax = inputdata.value.Float();
  192. m_bUseCustomAutoExposureMax = true;
  193. }
  194. //-----------------------------------------------------------------------------
  195. // Purpose:
  196. //-----------------------------------------------------------------------------
  197. void CEnvTonemapController::InputUseDefaultAutoExposure( inputdata_t &inputdata )
  198. {
  199. m_bUseCustomAutoExposureMin = false;
  200. m_bUseCustomAutoExposureMax = false;
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Purpose:
  204. //-----------------------------------------------------------------------------
  205. void CEnvTonemapController::InputSetBloomScale( inputdata_t &inputdata )
  206. {
  207. m_flCustomBloomScale = inputdata.value.Float();
  208. m_flCustomBloomScaleMinimum = m_flCustomBloomScale;
  209. m_bUseCustomBloomScale = true;
  210. }
  211. //-----------------------------------------------------------------------------
  212. // Purpose:
  213. //-----------------------------------------------------------------------------
  214. void CEnvTonemapController::InputUseDefaultBloomScale( inputdata_t &inputdata )
  215. {
  216. m_bUseCustomBloomScale = false;
  217. }