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.

116 lines
3.6 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. //
  3. // Functionality to render object motion blur
  4. //
  5. //===============================================================================
  6. #include "cbase.h"
  7. #include "object_motion_blur_effect.h"
  8. #include "model_types.h"
  9. ConVar mat_object_motion_blur_enable( "mat_object_motion_blur_enable", "0" );
  10. ConVar mat_object_motion_blur_model_scale( "mat_object_motion_blur_model_scale", "1.2" );
  11. CObjectMotionBlurManager g_ObjectMotionBlurManager;
  12. int CObjectMotionBlurManager::RegisterObject( C_BaseAnimating *pEntity, float flVelocityScale )
  13. {
  14. int nIndex;
  15. if ( m_nFirstFreeSlot == ObjectMotionBlurDefinition_t::END_OF_FREE_LIST )
  16. {
  17. nIndex = m_ObjectMotionBlurDefinitions.AddToTail();
  18. }
  19. else
  20. {
  21. nIndex = m_nFirstFreeSlot;
  22. m_nFirstFreeSlot = m_ObjectMotionBlurDefinitions[nIndex].m_nNextFreeSlot;
  23. }
  24. m_ObjectMotionBlurDefinitions[nIndex].m_pEntity = pEntity;
  25. m_ObjectMotionBlurDefinitions[nIndex].m_flVelocityScale = flVelocityScale;
  26. m_ObjectMotionBlurDefinitions[nIndex].m_nNextFreeSlot = ObjectMotionBlurDefinition_t::ENTRY_IN_USE;
  27. return nIndex;
  28. }
  29. void CObjectMotionBlurManager::UnregisterObject( int nObjectHandle )
  30. {
  31. Assert( !m_ObjectMotionBlurDefinitions[nObjectHandle].IsUnused() );
  32. m_ObjectMotionBlurDefinitions[nObjectHandle].m_nNextFreeSlot = m_nFirstFreeSlot;
  33. m_ObjectMotionBlurDefinitions[nObjectHandle].m_pEntity = NULL;
  34. m_nFirstFreeSlot = nObjectHandle;
  35. }
  36. void CObjectMotionBlurManager::DrawObjects()
  37. {
  38. for ( int i = 0; i < m_ObjectMotionBlurDefinitions.Count(); ++ i )
  39. {
  40. if ( m_ObjectMotionBlurDefinitions[i].ShouldDraw() )
  41. {
  42. m_ObjectMotionBlurDefinitions[i].DrawModel();
  43. }
  44. }
  45. }
  46. int CObjectMotionBlurManager::GetDrawableObjectCount()
  47. {
  48. int nCount = 0;
  49. for ( int i = 0; i < m_ObjectMotionBlurDefinitions.Count(); ++ i )
  50. {
  51. if ( m_ObjectMotionBlurDefinitions[i].ShouldDraw() )
  52. {
  53. nCount++;
  54. }
  55. }
  56. return nCount;
  57. }
  58. void CObjectMotionBlurManager::ObjectMotionBlurDefinition_t::DrawModel()
  59. {
  60. Vector vVelocity;
  61. m_pEntity->EstimateAbsVelocity( vVelocity );
  62. float flR = ( m_flVelocityScale * vVelocity.x + 128.0f ) / 256.0f;
  63. float flG = ( m_flVelocityScale * vVelocity.y + 128.0f ) / 256.0f;
  64. float flColor[3] = { flR, flG, 0.0f };
  65. render->SetColorModulation( flColor );
  66. C_BaseEntity *pAttachment;
  67. RenderableInstance_t instance;
  68. instance.m_nAlpha = 255;
  69. if ( mat_object_motion_blur_model_scale.GetFloat() != 1.0f )
  70. {
  71. m_pEntity->SetModelScale( mat_object_motion_blur_model_scale.GetFloat() );
  72. m_pEntity->InvalidateBoneCache();
  73. m_pEntity->DrawModel( STUDIO_RENDER | STUDIO_SKIP_FLEXES | STUDIO_DONOTMODIFYSTENCILSTATE | STUDIO_NOLIGHTING_OR_CUBEMAP | STUDIO_SKIP_DECALS, instance );
  74. pAttachment = m_pEntity->FirstMoveChild();
  75. while ( pAttachment != NULL )
  76. {
  77. if ( pAttachment->ShouldDraw() )
  78. {
  79. pAttachment->DrawModel( STUDIO_RENDER | STUDIO_SKIP_FLEXES | STUDIO_DONOTMODIFYSTENCILSTATE | STUDIO_NOLIGHTING_OR_CUBEMAP | STUDIO_SKIP_DECALS, instance );
  80. }
  81. pAttachment = pAttachment->NextMovePeer();
  82. }
  83. m_pEntity->SetModelScale( 1.0f );
  84. m_pEntity->InvalidateBoneCache();
  85. }
  86. flColor[2] = 1.0f;
  87. render->SetColorModulation( flColor );
  88. m_pEntity->DrawModel( STUDIO_RENDER | STUDIO_SKIP_FLEXES | STUDIO_DONOTMODIFYSTENCILSTATE | STUDIO_NOLIGHTING_OR_CUBEMAP | STUDIO_SKIP_DECALS, instance );
  89. pAttachment = m_pEntity->FirstMoveChild();
  90. while ( pAttachment != NULL )
  91. {
  92. if ( pAttachment->ShouldDraw() )
  93. {
  94. pAttachment->DrawModel( STUDIO_RENDER | STUDIO_SKIP_FLEXES | STUDIO_DONOTMODIFYSTENCILSTATE | STUDIO_NOLIGHTING_OR_CUBEMAP | STUDIO_SKIP_DECALS, instance );
  95. }
  96. pAttachment = pAttachment->NextMovePeer();
  97. }
  98. }