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.

100 lines
3.2 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. //
  3. // Functionality to render object motion blur
  4. //
  5. //===============================================================================
  6. #ifndef OBJECT_MOTION_BLUR_EFFECT_H
  7. #define OBJECT_MOTION_BLUR_EFFECT_H
  8. #if defined( COMPILER_MSVC )
  9. #pragma once
  10. #endif
  11. #include "utlvector.h"
  12. //-----------------------------------------------------------------------------
  13. // Central registry for objects that want motion blur applied to them,
  14. // responsible for tracking and drawing models.
  15. //
  16. // Currently requires C_BaseAnimating objects, could be made to work with
  17. // other object types as long as they can 1) be scaled and 2) be rendered.
  18. //-----------------------------------------------------------------------------
  19. class CObjectMotionBlurManager
  20. {
  21. public:
  22. CObjectMotionBlurManager() :
  23. m_nFirstFreeSlot( ObjectMotionBlurDefinition_t::END_OF_FREE_LIST )
  24. {
  25. }
  26. // Registers an entity with the object manager and returns an integer handle which must be unregistered before the object gdies
  27. int RegisterObject( C_BaseAnimating *pEntity, float flVelocityScale );
  28. // Unregisters a previously registered entity
  29. void UnregisterObject( int nObjectHandle );
  30. void SetVelocityScale( int nObjectHandle, float flVelocityScale )
  31. {
  32. Assert( !m_ObjectMotionBlurDefinitions[nObjectHandle].IsUnused() );
  33. m_ObjectMotionBlurDefinitions[nObjectHandle].m_flVelocityScale = flVelocityScale;
  34. }
  35. // Iterates through all valid registered objects and calls ObjectMotionBlurDefinition_t::DrawModel() on them
  36. void DrawObjects();
  37. int GetDrawableObjectCount();
  38. private:
  39. struct ObjectMotionBlurDefinition_t
  40. {
  41. bool ShouldDraw() const { return m_pEntity && m_pEntity->ShouldDraw() && m_flVelocityScale > 0; }
  42. bool IsUnused() const { return m_nNextFreeSlot != ObjectMotionBlurDefinition_t::ENTRY_IN_USE; }
  43. void DrawModel();
  44. C_BaseAnimating *m_pEntity;
  45. float m_flVelocityScale;
  46. // Linked list of free slots
  47. int m_nNextFreeSlot;
  48. // Special values for ObjectMotionBlurDefinition_t::m_nNextFreeSlot
  49. static const int END_OF_FREE_LIST = -1;
  50. static const int ENTRY_IN_USE = -2;
  51. };
  52. CUtlVector< ObjectMotionBlurDefinition_t > m_ObjectMotionBlurDefinitions;
  53. int m_nFirstFreeSlot;
  54. };
  55. extern CObjectMotionBlurManager g_ObjectMotionBlurManager;
  56. //-----------------------------------------------------------------------------
  57. // A scope-based auto-registration class for object motion blur
  58. //-----------------------------------------------------------------------------
  59. class CMotionBlurObject
  60. {
  61. public:
  62. CMotionBlurObject( C_BaseAnimating *pEntity, float flVelocityScale = 0.2f )
  63. {
  64. m_nObjectHandle = g_ObjectMotionBlurManager.RegisterObject( pEntity, flVelocityScale );
  65. }
  66. ~CMotionBlurObject()
  67. {
  68. g_ObjectMotionBlurManager.UnregisterObject( m_nObjectHandle );
  69. }
  70. void SetVelocityScale( float flVelocityScale )
  71. {
  72. g_ObjectMotionBlurManager.SetVelocityScale( m_nObjectHandle, flVelocityScale );
  73. }
  74. private:
  75. int m_nObjectHandle;
  76. // Assignment & copy-construction disallowed
  77. CMotionBlurObject( const CMotionBlurObject &other );
  78. CMotionBlurObject& operator=( const CMotionBlurObject &other );
  79. };
  80. #endif // OBJECT_MOTION_BLUR_EFFECT_H