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.

364 lines
13 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "env_tonemap_controller.h"
  8. #include "baseentity.h"
  9. #include "entityoutput.h"
  10. #include "convar.h"
  11. #include "triggers.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. // Spawn Flags
  15. #define SF_TONEMAP_MASTER 0x0001
  16. // 0 - eyes fully closed / fully black
  17. // 1 - nominal
  18. // 16 - eyes wide open / fully white
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Entity that controls player's tonemap
  21. //-----------------------------------------------------------------------------
  22. class CEnvTonemapController : public CPointEntity
  23. {
  24. DECLARE_CLASS( CEnvTonemapController, CPointEntity );
  25. public:
  26. DECLARE_DATADESC();
  27. DECLARE_SERVERCLASS();
  28. CEnvTonemapController();
  29. void Spawn( void );
  30. int UpdateTransmitState( void );
  31. bool IsMaster( void ) const { return HasSpawnFlags( SF_TONEMAP_MASTER ); }
  32. // Inputs
  33. void InputSetTonemapRate( inputdata_t &inputdata );
  34. void InputSetAutoExposureMin( inputdata_t &inputdata );
  35. void InputSetAutoExposureMax( inputdata_t &inputdata );
  36. void InputUseDefaultAutoExposure( inputdata_t &inputdata );
  37. void InputSetBloomScale( inputdata_t &inputdata );
  38. void InputUseDefaultBloomScale( inputdata_t &inputdata );
  39. void InputSetBloomScaleRange( inputdata_t &inputdata );
  40. void InputSetBloomExponent( inputdata_t &inputdata );
  41. void InputSetBloomSaturation( inputdata_t &inputdata );
  42. void InputSetTonemapPercentTarget( inputdata_t &inputdata );
  43. void InputSetTonemapPercentBrightPixels( inputdata_t &inputdata );
  44. void InputSetTonemapMinAvgLum( inputdata_t &inputdata );
  45. public:
  46. CNetworkVar( bool, m_bUseCustomAutoExposureMin );
  47. CNetworkVar( bool, m_bUseCustomAutoExposureMax );
  48. CNetworkVar( bool, m_bUseCustomBloomScale );
  49. CNetworkVar( float, m_flCustomAutoExposureMin );
  50. CNetworkVar( float, m_flCustomAutoExposureMax );
  51. CNetworkVar( float, m_flCustomBloomScale);
  52. CNetworkVar( float, m_flCustomBloomScaleMinimum);
  53. CNetworkVar( float, m_flBloomExponent);
  54. CNetworkVar( float, m_flBloomSaturation);
  55. CNetworkVar( float, m_flTonemapPercentTarget );
  56. CNetworkVar( float, m_flTonemapPercentBrightPixels );
  57. CNetworkVar( float, m_flTonemapMinAvgLum );
  58. CNetworkVar( float, m_flTonemapRate );
  59. };
  60. LINK_ENTITY_TO_CLASS( env_tonemap_controller, CEnvTonemapController );
  61. BEGIN_DATADESC( CEnvTonemapController )
  62. DEFINE_FIELD( m_bUseCustomAutoExposureMin, FIELD_BOOLEAN ),
  63. DEFINE_FIELD( m_bUseCustomAutoExposureMax, FIELD_BOOLEAN ),
  64. DEFINE_FIELD( m_flCustomAutoExposureMin, FIELD_FLOAT ),
  65. DEFINE_FIELD( m_flCustomAutoExposureMax, FIELD_FLOAT ),
  66. DEFINE_FIELD( m_flCustomBloomScale, FIELD_FLOAT ),
  67. DEFINE_FIELD( m_flCustomBloomScaleMinimum, FIELD_FLOAT ),
  68. DEFINE_FIELD( m_bUseCustomBloomScale, FIELD_BOOLEAN ),
  69. DEFINE_FIELD( m_flTonemapPercentTarget, FIELD_FLOAT ),
  70. DEFINE_FIELD( m_flTonemapPercentBrightPixels, FIELD_FLOAT ),
  71. DEFINE_FIELD( m_flTonemapMinAvgLum, FIELD_FLOAT ),
  72. DEFINE_FIELD( m_flTonemapRate, FIELD_FLOAT ),
  73. DEFINE_FIELD( m_flBloomExponent, FIELD_FLOAT ),
  74. DEFINE_FIELD( m_flBloomSaturation, FIELD_FLOAT ),
  75. // Inputs
  76. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetTonemapRate", InputSetTonemapRate ),
  77. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetAutoExposureMin", InputSetAutoExposureMin ),
  78. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetAutoExposureMax", InputSetAutoExposureMax ),
  79. DEFINE_INPUTFUNC( FIELD_VOID, "UseDefaultAutoExposure", InputUseDefaultAutoExposure ),
  80. DEFINE_INPUTFUNC( FIELD_VOID, "UseDefaultBloomScale", InputUseDefaultBloomScale ),
  81. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetBloomScale", InputSetBloomScale ),
  82. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetBloomScaleRange", InputSetBloomScaleRange ),
  83. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetBloomExponent", InputSetBloomExponent ),
  84. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetBloomSaturation", InputSetBloomSaturation ),
  85. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetTonemapPercentTarget", InputSetTonemapPercentTarget ),
  86. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetTonemapPercentBrightPixels", InputSetTonemapPercentBrightPixels ),
  87. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetTonemapMinAvgLum", InputSetTonemapMinAvgLum ),
  88. END_DATADESC()
  89. IMPLEMENT_SERVERCLASS_ST( CEnvTonemapController, DT_EnvTonemapController )
  90. SendPropInt( SENDINFO(m_bUseCustomAutoExposureMin), 1, SPROP_UNSIGNED ),
  91. SendPropInt( SENDINFO(m_bUseCustomAutoExposureMax), 1, SPROP_UNSIGNED ),
  92. SendPropInt( SENDINFO(m_bUseCustomBloomScale), 1, SPROP_UNSIGNED ),
  93. SendPropFloat( SENDINFO(m_flCustomAutoExposureMin), 0, SPROP_NOSCALE),
  94. SendPropFloat( SENDINFO(m_flCustomAutoExposureMax), 0, SPROP_NOSCALE),
  95. SendPropFloat( SENDINFO(m_flCustomBloomScale), 0, SPROP_NOSCALE),
  96. SendPropFloat( SENDINFO(m_flCustomBloomScaleMinimum), 0, SPROP_NOSCALE),
  97. SendPropFloat( SENDINFO(m_flBloomExponent), 0, SPROP_NOSCALE),
  98. SendPropFloat( SENDINFO(m_flBloomSaturation), 0, SPROP_NOSCALE),
  99. SendPropFloat( SENDINFO(m_flTonemapPercentTarget), 0, SPROP_NOSCALE ),
  100. SendPropFloat( SENDINFO(m_flTonemapPercentBrightPixels), 0, SPROP_NOSCALE ),
  101. SendPropFloat( SENDINFO(m_flTonemapMinAvgLum), 0, SPROP_NOSCALE ),
  102. SendPropFloat( SENDINFO(m_flTonemapRate), 0, SPROP_NOSCALE ),
  103. END_SEND_TABLE()
  104. //-----------------------------------------------------------------------------
  105. // Purpose:
  106. //-----------------------------------------------------------------------------
  107. CEnvTonemapController::CEnvTonemapController()
  108. {
  109. m_flBloomExponent = 2.5f;
  110. m_flBloomSaturation = 1.0f;
  111. m_flTonemapPercentTarget = 65.0f;
  112. m_flTonemapPercentBrightPixels = 2.0f;
  113. m_flTonemapMinAvgLum = 3.0f;
  114. m_flTonemapRate = 1.0f;
  115. }
  116. //-----------------------------------------------------------------------------
  117. // Purpose:
  118. //-----------------------------------------------------------------------------
  119. void CEnvTonemapController::Spawn( void )
  120. {
  121. SetSolid( SOLID_NONE );
  122. SetMoveType( MOVETYPE_NONE );
  123. }
  124. //-----------------------------------------------------------------------------
  125. // Purpose:
  126. //-----------------------------------------------------------------------------
  127. int CEnvTonemapController::UpdateTransmitState()
  128. {
  129. return SetTransmitState( FL_EDICT_ALWAYS );
  130. }
  131. //-----------------------------------------------------------------------------
  132. // Purpose: set a base and minimum bloom scale
  133. //-----------------------------------------------------------------------------
  134. void CEnvTonemapController::InputSetBloomScaleRange( inputdata_t &inputdata )
  135. {
  136. float bloom_max=1, bloom_min=1;
  137. int nargs=sscanf("%f %f",inputdata.value.String(), bloom_max, bloom_min );
  138. if (nargs != 2)
  139. {
  140. Warning("%s (%s) received SetBloomScaleRange input without 2 arguments. Syntax: <max bloom> <min bloom>\n", GetClassname(), GetDebugName() );
  141. return;
  142. }
  143. m_flCustomBloomScale=bloom_max;
  144. m_flCustomBloomScale=bloom_min;
  145. }
  146. //-----------------------------------------------------------------------------
  147. // Purpose: Set the auto exposure min to the specified value
  148. //-----------------------------------------------------------------------------
  149. void CEnvTonemapController::InputSetAutoExposureMin( inputdata_t &inputdata )
  150. {
  151. m_flCustomAutoExposureMin = inputdata.value.Float();
  152. m_bUseCustomAutoExposureMin = true;
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose: Set the auto exposure max to the specified value
  156. //-----------------------------------------------------------------------------
  157. void CEnvTonemapController::InputSetAutoExposureMax( inputdata_t &inputdata )
  158. {
  159. m_flCustomAutoExposureMax = inputdata.value.Float();
  160. m_bUseCustomAutoExposureMax = true;
  161. }
  162. //-----------------------------------------------------------------------------
  163. // Purpose:
  164. //-----------------------------------------------------------------------------
  165. void CEnvTonemapController::InputUseDefaultAutoExposure( inputdata_t &inputdata )
  166. {
  167. m_bUseCustomAutoExposureMin = false;
  168. m_bUseCustomAutoExposureMax = false;
  169. }
  170. //-----------------------------------------------------------------------------
  171. // Purpose:
  172. //-----------------------------------------------------------------------------
  173. void CEnvTonemapController::InputSetBloomScale( inputdata_t &inputdata )
  174. {
  175. m_flCustomBloomScale = inputdata.value.Float();
  176. m_flCustomBloomScaleMinimum = m_flCustomBloomScale;
  177. m_bUseCustomBloomScale = true;
  178. }
  179. //-----------------------------------------------------------------------------
  180. // Purpose:
  181. //-----------------------------------------------------------------------------
  182. void CEnvTonemapController::InputUseDefaultBloomScale( inputdata_t &inputdata )
  183. {
  184. m_bUseCustomBloomScale = false;
  185. }
  186. //-----------------------------------------------------------------------------
  187. // Purpose:
  188. //-----------------------------------------------------------------------------
  189. void CEnvTonemapController::InputSetBloomExponent( inputdata_t &inputdata )
  190. {
  191. m_flBloomExponent = inputdata.value.Float();
  192. }
  193. //-----------------------------------------------------------------------------
  194. // Purpose:
  195. //-----------------------------------------------------------------------------
  196. void CEnvTonemapController::InputSetBloomSaturation( inputdata_t &inputdata )
  197. {
  198. m_flBloomSaturation = inputdata.value.Float();
  199. }
  200. //-----------------------------------------------------------------------------
  201. // Purpose:
  202. //-----------------------------------------------------------------------------
  203. void CEnvTonemapController::InputSetTonemapPercentTarget( inputdata_t &inputdata )
  204. {
  205. m_flTonemapPercentTarget = inputdata.value.Float();
  206. }
  207. //-----------------------------------------------------------------------------
  208. // Purpose:
  209. //-----------------------------------------------------------------------------
  210. void CEnvTonemapController::InputSetTonemapPercentBrightPixels( inputdata_t &inputdata )
  211. {
  212. m_flTonemapPercentBrightPixels= inputdata.value.Float();
  213. }
  214. //-----------------------------------------------------------------------------
  215. // Purpose:
  216. //-----------------------------------------------------------------------------
  217. void CEnvTonemapController::InputSetTonemapMinAvgLum( inputdata_t &inputdata )
  218. {
  219. m_flTonemapMinAvgLum = inputdata.value.Float();
  220. }
  221. //-----------------------------------------------------------------------------
  222. // Purpose:
  223. //-----------------------------------------------------------------------------
  224. void CEnvTonemapController::InputSetTonemapRate( inputdata_t &inputdata )
  225. {
  226. m_flTonemapRate = inputdata.value.Float();
  227. }
  228. //--------------------------------------------------------------------------------------------------------
  229. LINK_ENTITY_TO_CLASS( trigger_tonemap, CTonemapTrigger );
  230. BEGIN_DATADESC( CTonemapTrigger )
  231. DEFINE_KEYFIELD( m_tonemapControllerName, FIELD_STRING, "TonemapName" ),
  232. END_DATADESC()
  233. //--------------------------------------------------------------------------------------------------------
  234. void CTonemapTrigger::Spawn( void )
  235. {
  236. AddSpawnFlags( SF_TRIGGER_ALLOW_CLIENTS );
  237. BaseClass::Spawn();
  238. InitTrigger();
  239. m_hTonemapController = gEntList.FindEntityByName( NULL, m_tonemapControllerName );
  240. }
  241. //--------------------------------------------------------------------------------------------------------
  242. void CTonemapTrigger::StartTouch( CBaseEntity *other )
  243. {
  244. if ( !PassesTriggerFilters( other ) )
  245. return;
  246. BaseClass::StartTouch( other );
  247. CBasePlayer *player = ToBasePlayer( other );
  248. if ( !player )
  249. return;
  250. player->OnTonemapTriggerStartTouch( this );
  251. }
  252. //--------------------------------------------------------------------------------------------------------
  253. void CTonemapTrigger::EndTouch( CBaseEntity *other )
  254. {
  255. if ( !PassesTriggerFilters( other ) )
  256. return;
  257. BaseClass::EndTouch( other );
  258. CBasePlayer *player = ToBasePlayer( other );
  259. if ( !player )
  260. return;
  261. player->OnTonemapTriggerEndTouch( this );
  262. }
  263. //-----------------------------------------------------------------------------
  264. // Purpose: Clear out the tonemap controller.
  265. //-----------------------------------------------------------------------------
  266. void CTonemapSystem::LevelInitPreEntity( void )
  267. {
  268. m_hMasterController = NULL;
  269. }
  270. //-----------------------------------------------------------------------------
  271. // Purpose: On level load find the master tonemap controller. If no controller is
  272. // set as Master, use the first tonemap controller found.
  273. //-----------------------------------------------------------------------------
  274. void CTonemapSystem::LevelInitPostEntity( void )
  275. {
  276. // Overall master controller
  277. CEnvTonemapController *pTonemapController = NULL;
  278. do
  279. {
  280. pTonemapController = static_cast<CEnvTonemapController*>( gEntList.FindEntityByClassname( pTonemapController, "env_tonemap_controller" ) );
  281. if ( pTonemapController )
  282. {
  283. if ( m_hMasterController == NULL )
  284. {
  285. m_hMasterController = pTonemapController;
  286. }
  287. else
  288. {
  289. if ( pTonemapController->IsMaster() )
  290. {
  291. m_hMasterController = pTonemapController;
  292. }
  293. }
  294. }
  295. } while ( pTonemapController );
  296. }
  297. //--------------------------------------------------------------------------------------------------------
  298. CTonemapSystem s_TonemapSystem( "TonemapSystem" );
  299. //--------------------------------------------------------------------------------------------------------
  300. CTonemapSystem *TheTonemapSystem( void )
  301. {
  302. return &s_TonemapSystem;
  303. }
  304. //--------------------------------------------------------------------------------------------------------