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.

203 lines
6.1 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "cbase.h"
  7. #include "model_types.h"
  8. #include "vcollide.h"
  9. #include "vcollide_parse.h"
  10. #include "solidsetdefaults.h"
  11. #include "bone_setup.h"
  12. #include "engine/ivmodelinfo.h"
  13. #include "physics.h"
  14. #include "view.h"
  15. #include "c_physicsprop.h"
  16. #include "tier0/vprof.h"
  17. #include "ivrenderview.h"
  18. #if defined ( PORTAL2 )
  19. #include "portal2/portal_grabcontroller_shared.h"
  20. #endif
  21. // memdbgon must be the last include file in a .cpp file!!!
  22. #include "tier0/memdbgon.h"
  23. IMPLEMENT_CLIENTCLASS_DT(C_PhysicsProp, DT_PhysicsProp, CPhysicsProp)
  24. RecvPropBool( RECVINFO( m_bAwake ) ),
  25. RecvPropInt( RECVINFO( m_spawnflags ) ),
  26. END_RECV_TABLE()
  27. // We discard VVD data on consoles to save memory, so we cannot compute static lighting for models at run-time:
  28. #if defined( _GAMECONSOLE )
  29. ConVar r_PhysPropStaticLighting( "r_PhysPropStaticLighting", "0" );
  30. #else
  31. ConVar r_PhysPropStaticLighting( "r_PhysPropStaticLighting", "0" ); // Disabled for CS:GO on the PC
  32. #endif // _GAMECONSOLE
  33. // @MULTICORE (toml 9/18/2006): this visualization will need to be implemented elsewhere
  34. ConVar r_visualizeproplightcaching( "r_visualizeproplightcaching", "0" );
  35. //-----------------------------------------------------------------------------
  36. // Purpose:
  37. //-----------------------------------------------------------------------------
  38. C_PhysicsProp::C_PhysicsProp( void )
  39. {
  40. m_pPhysicsObject = NULL;
  41. m_takedamage = DAMAGE_YES;
  42. // default true so static lighting will get recomputed when we go to sleep
  43. m_bAwakeLastTime = true;
  44. m_bCanUseStaticLighting = false;
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose:
  48. //-----------------------------------------------------------------------------
  49. C_PhysicsProp::~C_PhysicsProp( void )
  50. {
  51. }
  52. void C_PhysicsProp::OnPreDataChanged( DataUpdateType_t updateType )
  53. {
  54. #ifdef PORTAL2
  55. // UNDONE: This was supposed to do something when the player had m_pHeldEntityClone set?
  56. // It was causing problems in single player where the cube would freeze in place whenever this was true
  57. /*
  58. C_Portal_Player *pPlayer = static_cast<C_Portal_Player *>( GetPlayerHoldingEntity( this ) );
  59. if ( pPlayer && pPlayer->IsUsingVMGrab() )
  60. {
  61. m_vecClientOrigin = GetAbsOrigin();
  62. m_vecClientAngles = GetAbsAngles();
  63. }
  64. */
  65. #endif
  66. }
  67. // Used to indicate if we can use the static lighting baking
  68. //-----------------------------------------------------------------------------
  69. void C_PhysicsProp::OnDataChanged( DataUpdateType_t type )
  70. {
  71. BaseClass::OnDataChanged( type );
  72. m_bCanUseStaticLighting = modelinfo->UsesStaticLighting( GetModel() );
  73. if ( m_bCanUseStaticLighting )
  74. {
  75. CreateModelInstance();
  76. }
  77. #ifdef PORTAL2
  78. // UNDONE: This was supposed to do something when the player had m_pHeldEntityClone set?
  79. // It was causing problems in single player where the cube would freeze in place whenever this was true
  80. /*
  81. // Put these values back to the client simulated ones if we're player held
  82. C_Portal_Player *pPlayer = static_cast<C_Portal_Player *>( GetPlayerHoldingEntity( this ) );
  83. if ( pPlayer && pPlayer->IsUsingVMGrab() )
  84. {
  85. SetAbsOrigin( m_vecClientOrigin );
  86. SetAbsAngles( m_vecClientAngles );
  87. }
  88. */
  89. #endif
  90. }
  91. //-----------------------------------------------------------------------------
  92. // Hooks for the fast model rendering path
  93. //-----------------------------------------------------------------------------
  94. IClientModelRenderable* C_PhysicsProp::GetClientModelRenderable()
  95. {
  96. // Can't do this debug mode through the fast path since it uses
  97. // color modulation, which we don't support yet.
  98. if ( !BaseClass::GetClientModelRenderable() )
  99. return NULL;
  100. if ( ( m_bAwakeLastTime != m_bAwake ) && r_visualizeproplightcaching.GetBool() )
  101. return NULL;
  102. // NOTE: This works because GetClientModelRenderable() is only queried
  103. // if the prop is known by the viewrender system to be opaque already
  104. // so we need no other opacity checks here.
  105. return this;
  106. }
  107. bool C_PhysicsProp::GetRenderData( void *pData, ModelDataCategory_t nCategory )
  108. {
  109. switch ( nCategory )
  110. {
  111. case MODEL_DATA_LIGHTING_MODEL:
  112. {
  113. // FIXME: Cache bCanBeStaticLit off in OnDataChanged?
  114. if ( !m_bCanUseStaticLighting || !r_PhysPropStaticLighting.GetBool() )
  115. {
  116. *(RenderableLightingModel_t*)pData = LIGHTING_MODEL_STANDARD;
  117. return true;
  118. }
  119. if ( m_bAwakeLastTime && !m_bAwake )
  120. {
  121. // transition to sleep, bake lighting now, once
  122. if ( !modelrender->RecomputeStaticLighting( GetModelInstance() ) )
  123. {
  124. // Failed to bake, don't indicate it's asleep yet, use normal model
  125. *(RenderableLightingModel_t*)pData = LIGHTING_MODEL_STANDARD;
  126. return true;
  127. }
  128. }
  129. *(RenderableLightingModel_t*)pData = m_bAwake ? LIGHTING_MODEL_STANDARD : LIGHTING_MODEL_PHYSICS_PROP;
  130. m_bAwakeLastTime = m_bAwake;
  131. }
  132. return true;
  133. default:
  134. return BaseClass::GetRenderData( pData, nCategory );
  135. }
  136. }
  137. //-----------------------------------------------------------------------------
  138. //-----------------------------------------------------------------------------
  139. // Purpose: Draws the object
  140. // Input : flags -
  141. //-----------------------------------------------------------------------------
  142. bool C_PhysicsProp::OnInternalDrawModel( ClientModelRenderInfo_t *pInfo )
  143. {
  144. if ( !m_bCanUseStaticLighting || !r_PhysPropStaticLighting.GetBool() )
  145. return true;
  146. if ( m_bAwakeLastTime != m_bAwake )
  147. {
  148. if ( m_bAwakeLastTime && !m_bAwake )
  149. {
  150. // transition to sleep, bake lighting now, once
  151. if ( !modelrender->RecomputeStaticLighting( GetModelInstance() ) )
  152. {
  153. // not valid for drawing
  154. return false;
  155. }
  156. if ( r_visualizeproplightcaching.GetBool() )
  157. {
  158. float color[] = { 0.0f, 1.0f, 0.0f, 1.0f };
  159. render->SetColorModulation( color );
  160. }
  161. }
  162. else if ( r_visualizeproplightcaching.GetBool() )
  163. {
  164. float color[] = { 1.0f, 0.0f, 0.0f, 1.0f };
  165. render->SetColorModulation( color );
  166. }
  167. }
  168. if ( !m_bAwake )
  169. {
  170. // going to sleep, have static lighting
  171. pInfo->flags |= STUDIO_STATIC_LIGHTING;
  172. }
  173. // track state
  174. m_bAwakeLastTime = m_bAwake;
  175. return true;
  176. }