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.

211 lines
8.6 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =====//
  2. //
  3. // Dme version of a skeletal model (gets compiled into a MDL)
  4. //
  5. //===========================================================================//
  6. #ifndef DMEMODEL_H
  7. #define DMEMODEL_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier1/utlstack.h"
  12. #include "tier1/utlstringtoken.h"
  13. #include "movieobjects/dmejoint.h"
  14. #include "movieobjects/dmetransformlist.h"
  15. #include "movieobjects/dmeaxissystem.h"
  16. class CDmeDrawSettings;
  17. class CDmeMesh;
  18. //-----------------------------------------------------------------------------
  19. // A class representing a skeletal model
  20. //-----------------------------------------------------------------------------
  21. class CDmeModel : public CDmeDag
  22. {
  23. DEFINE_ELEMENT( CDmeModel, CDmeDag );
  24. public:
  25. // Add joint
  26. CDmeJoint *AddJoint( const char *pJointName, CDmeDag *pParent = NULL );
  27. int AddJoint( CDmeDag *pJoint );
  28. // Returns the number of joint transforms we know about
  29. int GetJointCount() const;
  30. // Returns the DmeDag for the specified joint index
  31. CDmeDag *GetJoint( int nIndex );
  32. const CDmeDag *GetJoint( int nIndex ) const;
  33. // Returns the DmeTransform for the specified joint index
  34. CDmeTransform *GetJointTransform( int nIndex );
  35. const CDmeTransform *GetJointTransform( int nIndex ) const;
  36. // Determines joint index of a given a joint
  37. int GetJointIndex( CDmeDag *pJoint ) const;
  38. int GetJointIndex( const char *pJointName ) const;
  39. int GetJointIndex( CUtlStringToken nJointNameHash ) const;
  40. // Captures the current joint transforms into a base state
  41. void CaptureJointsToBaseState( const char *pBaseStateName );
  42. // Sets the joint transforms to the values in the specified base state, if it exists
  43. void PushBaseStateToJoints( const char *pBaseStateName );
  44. // Finds a base state by name, returns NULL if not found
  45. CDmeTransformList *FindBaseState( const char *pBaseStateName );
  46. // Recursively render the Dag hierarchy
  47. virtual void Draw( CDmeDrawSettings *pDrawSettings = NULL );
  48. // NOTE: See comment for m_upAxis attribute below
  49. // Set if Z is the up axis of the model
  50. // Set to true if the data under the DmeModel is AS_VALVE_ENGINE space( Up: Z, Fwd: X, Left: Y )
  51. // Set to false if the data under the DmeModel is AS_MAYA_YUP space ( Up: Y, Fwd: Z, Left: X )
  52. // See SetAxisSystem() for anything other than AS_VALVE_ENGINE or AS_MAYA_YUP
  53. void ZUp( bool bZUp );
  54. // Returns true if the DmeModel is Z Up.
  55. bool IsZUp() const;
  56. // Returns the matrix that moves DmeModel data to engine space
  57. void GetModelToEngineMat( matrix3x4_t &modelToEngineMat );
  58. // Returns the matrix that moves engine data to DmeModel space
  59. void GetEngineToModelMat( matrix3x4_t &engineToModelMat );
  60. // Replace all instances of a material with a different material
  61. // Using a NULL material name will replace *all* materials with the new material
  62. void ReplaceMaterial( const char *pOldMaterialName, const char *pNewMaterialName );
  63. // Collapses all joints below the specified joint name, reskinning any meshes
  64. // referring to collapsed joints to use the specified joint instead
  65. void CollapseJoints( const char *pJointName );
  66. // Gets the joint with the specified name
  67. CDmeDag *GetJoint( const char *pJointName );
  68. // Reskin all meshes under the DmeModel
  69. void ReskinMeshes( const int *pJointTransformIndexRemap );
  70. // Sets the DmeAxisSystem, no reorientation is done, see ConvertToAxisSystem
  71. bool SetAxisSystem( CDmeAxisSystem::PredefinedAxisSystem ePredefAxisSystem );
  72. bool SetAxisSystem( CDmeAxisSystem::Axis_t eUpAxis, CDmeAxisSystem::ForwardParity_t eForwardParity, CDmeAxisSystem::CoordSys_t eCoordSys = CDmeAxisSystem::AS_RIGHT_HANDED );
  73. // Sets the DmeAxisSystem and reorients all of the data under the DmeModel
  74. // as necessary to change from the current DmeAxisSystem to the specified DmeAxisSystem
  75. // Currently private, will be made public soon, upAxis is still authoritative
  76. bool ConvertToAxisSystem( CDmeAxisSystem::PredefinedAxisSystem ePredefAxisSystem );
  77. bool ConvertToAxisSystem( CDmeAxisSystem::Axis_t eUpAxis, CDmeAxisSystem::ForwardParity_t eForwardParity, CDmeAxisSystem::CoordSys_t eCoordSys = CDmeAxisSystem::AS_RIGHT_HANDED );
  78. // Returns the matrix to convert data from the current axisSystem to the specified axisSystem
  79. bool GetConversionMatrix( matrix3x4a_t &mConversion, CDmeAxisSystem::PredefinedAxisSystem eToPredefinedAxisSystem );
  80. // Freeze child meshes, pushes transform into vertices of mesh, used to be part of Reorient
  81. void FreezeChildMeshes();
  82. // TransformScene
  83. void TransformScene( const Vector &vScale = Vector( 1.0f, 1.0f, 1.0f ), const Vector &vTranslate = vec3_origin, const DegreeEuler &eRotation = DegreeEuler( 0.0f, 0.0f, 0.0f ), float flEps = 1.0e-4 );
  84. void ScaleScene( const Vector &vScale );
  85. // Updates all base states by adding missing joints
  86. void UpdateBaseStates();
  87. private:
  88. // Reskin meshes based on bone collapse
  89. void ReskinMeshes( CDmeDag *pDag, const int *pJointTransformIndexRemap );
  90. // Remove joints
  91. void RemoveJoints( int nNewJointCount, const int *pInvJointRemap );
  92. // Removes all children from this joint, moving shapes to be
  93. void RemoveAllChildren( CDmeDag *pDag, CDmeDag *pSubtreeRoot, const matrix3x4_t &jointToSubtreeRoot );
  94. void RemoveAllChildren( CDmeDag *pSubtreeRoot );
  95. // Helper functions for ReorientToEngineSpace() and ReorientToDCCToolSpace()
  96. static void GetReorientData( matrix3x4_t &m, Quaternion &q, bool bMakeZUp );
  97. static void ReorientDmeAnimation( CDmeDag *pDmeDag, const matrix3x4_t &mOrient, const Quaternion &qOrient );
  98. static void ReorientDmeTransform( CDmeTransform *pDmeTransform, const matrix3x4_t &mOrient, const Quaternion &qOrient );
  99. static void ReorientDmeModelChildren( CDmeModel *pDmeModel, const matrix3x4_t &mOrient, const Quaternion &qOrient );
  100. static void ReorientChildDmeMeshes_R( CDmeDag *pDmeDag );
  101. static void ReorientDmeMesh( CDmeMesh *pDmeMesh, matrix3x4_t absMat );
  102. protected:
  103. // The order in which the joint transform names appear in this list
  104. // indicates the joint index for each dag
  105. CDmaElementArray< CDmeDag > m_JointList;
  106. // Stores a list of base poses for all the joint transforms
  107. CDmaElementArray<CDmeTransformList> m_BaseStates;
  108. // Stores the up axis of the model
  109. // One of Y or Z
  110. //
  111. // NOTE: Y up is Maya's typical coordinate system. Setting the up axis to Z
  112. // won't quite give you the engine's coordinate system (even though it
  113. // has an Up Axis of Z as well). The difference is the forward axis.
  114. //
  115. // A DmeModel with m_UpAxis = Y gives the data in a coordinate system
  116. // that is Maya's typical coordinate system. Y Up, Z Foward, X Right.
  117. //
  118. // A DmeModel with m_UpAxis = Z gives the data in a coordinate system
  119. // that is compatible with typical SMD data. This is data that can
  120. // be compiled in studiomdl without any special options.
  121. //
  122. // Neither Y up or Z up is engine space. Z up is closer but still
  123. // requires another 90 degree rotation to align things. This can be
  124. // changed in the future if required but Z up was added for legacy
  125. // SMD compatibility.
  126. //
  127. // In this table, missing entries mean it's not required or isn't
  128. // supported
  129. //
  130. // m_UpAxis Desc Up Forward Right studiomdl
  131. // -------- ------------- ------- ------- ------- ---------
  132. // Y Maya Y Up Y Z X $upaxis Y
  133. // Z Maya Z Up/XSI Z -Y X
  134. // Engine Z X Y $origin 0 0 0 0
  135. //-----------------------------------------------------------------------------
  136. CDmaString m_UpAxis;
  137. CDmaElement< CDmeAxisSystem > m_eAxisSystem;
  138. CDmaElement< CDmeModel > m_eModel;
  139. CDmaElement< CDmeModel > m_eSkeleton;
  140. private:
  141. enum SetupBoneRetval_t
  142. {
  143. NO_SKIN_DATA = 0,
  144. TOO_MANY_BONES,
  145. BONES_SET_UP
  146. };
  147. // Sets up the render state for the model
  148. SetupBoneRetval_t SetupBoneMatrixState( const matrix3x4_t& shapeToWorld, bool bForceSoftwareSkin );
  149. // Loads up joint transforms for this model
  150. void LoadJointTransform( CDmeDag *pJoint, CDmeTransformList *pBindPose, const matrix3x4_t &parentToWorld, const matrix3x4_t &parentToBindPose, bool bSetHardwareState );
  151. // Implementation of ReplaceMaterial
  152. void ReplaceMaterial( CDmeDag *pDag, const char *pOldMaterialName, const char *pNewMaterialName );
  153. // Sets up the render state for the model
  154. static matrix3x4_t *SetupModelRenderState( const matrix3x4_t& shapeToWorld, bool bHasSkinningData, bool bForceSoftwareSkin );
  155. static void CleanupModelRenderState();
  156. // Stack of DmeModels currently being rendered. Used to set up render state
  157. static CUtlStack< CDmeModel * > s_ModelStack;
  158. friend class CDmeMesh;
  159. friend bool SetMeshFromSkeleton( CDmeMesh *pDmeMesh ); // dmx edit function - to do skin weights
  160. };
  161. #endif // DMEMODEL_H