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.

217 lines
7.5 KiB

  1. //===== Copyright � 2005-2013, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: A higher level link library for general use in the game and tools.
  4. //
  5. //===========================================================================//
  6. #ifndef MDLUTILS_H
  7. #define MDLUTILS_H
  8. #if defined( _WIN32 )
  9. #pragma once
  10. #endif
  11. #include "datacache/imdlcache.h"
  12. #include "mathlib/vector.h"
  13. #include "color.h"
  14. #include "studio.h"
  15. #include "materialsystem/custommaterialowner.h"
  16. #include "materialsystem/MaterialSystemUtil.h"
  17. #include "irendertorthelperobject.h"
  18. //-----------------------------------------------------------------------------
  19. // Forward declarations
  20. //-----------------------------------------------------------------------------
  21. struct matrix3x4_t;
  22. class CMDLAttachmentData
  23. {
  24. public:
  25. matrix3x4_t m_AttachmentToWorld;
  26. bool m_bValid;
  27. };
  28. struct MDLSquenceLayer_t
  29. {
  30. int m_nSequenceIndex;
  31. float m_flWeight;
  32. };
  33. //-----------------------------------------------------------------------------
  34. // Class containing simplistic MDL state for use in rendering
  35. //-----------------------------------------------------------------------------
  36. class CMDL : public CCustomMaterialOwner, public IRenderToRTHelperObject
  37. {
  38. public:
  39. CMDL();
  40. virtual ~CMDL();
  41. void SetMDL( MDLHandle_t h );
  42. MDLHandle_t GetMDL() const;
  43. /// NOTE: This version of draw assumes you've filled in the bone to world
  44. /// matrix yourself by calling IStudioRender::LockBoneMatrices. The pointer
  45. /// returned by that method needs to be passed into here
  46. /// @param flags allows you to specify additional STUDIORENDER_ flags -- usually never necessary
  47. /// unless you need to (eg) forcibly disable shadows for some reason.
  48. void Draw( const matrix3x4_t& rootToWorld, const matrix3x4_t *pBoneToWorld, int flags = 0 );
  49. // from IRenderToRTHelperObject
  50. // Simple version of drawing; sets up bones for you
  51. void Draw( const matrix3x4_t& rootToWorld );
  52. bool GetBoundingSphere( Vector &vecCenter, float &flRadius );
  53. ITexture *GetEnvCubeMap();
  54. void SetUpBones( const matrix3x4_t& shapeToWorld, int nMaxBoneCount, matrix3x4_t *pOutputMatrices, const float *pPoseParameters = NULL, MDLSquenceLayer_t *pSequenceLayers = NULL, int nNumSequenceLayers = 0 );
  55. void SetupBonesWithBoneMerge( const CStudioHdr *pMergeHdr, matrix3x4_t *pMergeBoneToWorld,
  56. const CStudioHdr *pFollow, const matrix3x4_t *pFollowBoneToWorld, const matrix3x4_t &matModelToWorld );
  57. studiohdr_t *GetStudioHdr();
  58. virtual bool GetAttachment( int number, matrix3x4_t &matrix );
  59. bool GetAttachment( const char *szAttachment, matrix3x4_t& matrixOut );
  60. void AdjustTime( float flAmount );
  61. void SetSimpleMaterialOverride( IMaterial *pNewMaterial );
  62. private:
  63. void UnreferenceMDL();
  64. void SetupBones_AttachmentHelper( CStudioHdr *hdr, matrix3x4_t *pBoneToWorld );
  65. bool PutAttachment( int number, const matrix3x4_t &attachmentToWorld );
  66. CUtlVector<CMDLAttachmentData> m_Attachments;
  67. CMaterialReference m_pSimpleMaterialOverride;
  68. protected:
  69. CTextureReference m_DefaultEnvCubemap;
  70. CTextureReference m_DefaultHDREnvCubemap;
  71. public:
  72. MDLHandle_t m_MDLHandle;
  73. // Adding m_Padding to coax the PS3 compiler into generating working code.
  74. // Without it the m_Color field won't be 4-byte aligned and the PS3 compiler will try and vectorize
  75. // some of the color arithmetic and trash nearby memory.
  76. unsigned short m_Padding;
  77. Color m_Color;
  78. int m_nSkin;
  79. int m_nBody;
  80. int m_nSequence;
  81. int m_nLOD;
  82. float m_flPlaybackRate;
  83. float m_flTime;
  84. float m_flCurrentAnimEndTime;
  85. float m_pFlexControls[ MAXSTUDIOFLEXCTRL * 4 ];
  86. Vector m_vecViewTarget;
  87. bool m_bWorldSpaceViewTarget;
  88. bool m_bUseSequencePlaybackFPS;
  89. void *m_pProxyData;
  90. float m_flTimeBasisAdjustment;
  91. CUtlVector< int > m_arrSequenceFollowLoop;
  92. };
  93. struct MDLData_t
  94. {
  95. MDLData_t();
  96. CMDL m_MDL;
  97. matrix3x4_t m_MDLToWorld;
  98. bool m_bRequestBoneMergeTakeover;
  99. };
  100. class CMergedMDL : public IRenderToRTHelperObject
  101. {
  102. public:
  103. // constructor, destructor
  104. CMergedMDL();
  105. virtual ~CMergedMDL();
  106. // Sets the current mdl
  107. virtual void SetMDL( MDLHandle_t handle, CCustomMaterialOwner* pCustomMaterialOwner = NULL, void *pProxyData = NULL );
  108. virtual void SetMDL( const char *pMDLName, CCustomMaterialOwner* pCustomMaterialOwner = NULL, void *pProxyData = NULL );
  109. CMDL *GetMDL() { return &m_RootMDL.m_MDL; }
  110. // Sets the current sequence
  111. void SetSequence( int nSequence, bool bUseSequencePlaybackFPS );
  112. void AddSequenceFollowLoop( int nSequence, bool bUseSequencePlaybackFPS );
  113. void ClearSequenceFollowLoop();
  114. // Set the pose parameters
  115. void SetPoseParameters( const float *pPoseParameters, int nCount );
  116. // Set the overlay sequence layers
  117. void SetSequenceLayers( const MDLSquenceLayer_t *pSequenceLayers, int nCount );
  118. void SetSkin( int nSkin );
  119. // Bounds.
  120. bool GetBoundingBox( Vector &vecBoundsMin, Vector &vecBoundsMax );
  121. bool GetAttachment( const char *szAttachment, matrix3x4_t& matrixOut );
  122. bool GetAttachment( int iAttachmentNum, matrix3x4_t& matrixOut );
  123. void SetModelAnglesAndPosition( const QAngle &angRot, const Vector &vecPos );
  124. // Attached models.
  125. void SetMergeMDL( MDLHandle_t handle, CCustomMaterialOwner* pCustomMaterialOwner = NULL, void *pProxyData = NULL, bool bRequestBonemergeTakeover = false );
  126. MDLHandle_t SetMergeMDL( const char *pMDLName, CCustomMaterialOwner* pCustomMaterialOwner = NULL, void *pProxyData = NULL, bool bRequestBonemergeTakeover = false );
  127. int GetMergeMDLIndex( MDLHandle_t handle );
  128. CMDL *GetMergeMDL(MDLHandle_t handle );
  129. void ClearMergeMDLs( void );
  130. void Draw();
  131. void SetupBonesForAttachmentQueries( void );
  132. // from IRenderToRTHelperObject
  133. void Draw( const matrix3x4_t &rootToWorld );
  134. bool GetBoundingSphere( Vector &vecCenter, float &flRadius );
  135. ITexture *GetEnvCubeMap() { return m_RootMDL.m_MDL.GetEnvCubeMap(); }
  136. void UpdateModelCustomMaterials( MDLHandle_t handle, CCustomMaterialOwner* pCustomMaterialOwner );
  137. protected:
  138. virtual void OnPostSetUpBonesPreDraw() {}
  139. virtual void OnModelDrawPassStart( int iPass, CStudioHdr *pStudioHdr, int &nFlags ) {}
  140. virtual void OnModelDrawPassFinished( int iPass, CStudioHdr *pStudioHdr, int &nFlags ) {}
  141. protected:
  142. MDLData_t m_RootMDL;
  143. CUtlVector<MDLData_t> m_aMergeMDLs;
  144. float m_PoseParameters[ MAXSTUDIOPOSEPARAM ];
  145. private:
  146. static const int MAX_SEQUENCE_LAYERS = 8;
  147. int m_nNumSequenceLayers;
  148. MDLSquenceLayer_t m_SequenceLayers[ MAX_SEQUENCE_LAYERS ];
  149. };
  150. //-----------------------------------------------------------------------------
  151. // Returns the bounding box for the model
  152. //-----------------------------------------------------------------------------
  153. void GetMDLBoundingBox( Vector *pMins, Vector *pMaxs, MDLHandle_t h, int nSequence );
  154. //-----------------------------------------------------------------------------
  155. // Returns the radius of the model as measured from the origin
  156. //-----------------------------------------------------------------------------
  157. float GetMDLRadius( MDLHandle_t h, int nSequence );
  158. //-----------------------------------------------------------------------------
  159. // Returns a more accurate bounding sphere
  160. //-----------------------------------------------------------------------------
  161. void GetMDLBoundingSphere( Vector *pVecCenter, float *pRadius, MDLHandle_t h, int nSequence );
  162. //-----------------------------------------------------------------------------
  163. // Determines which pose parameters are used by the specified sequence
  164. //-----------------------------------------------------------------------------
  165. void FindSequencePoseParameters( CStudioHdr &hdr, int nSequence, bool *pPoseParameters, int nCount );
  166. #endif // MDLUTILS_H