Team Fortress 2 Source Code as on 22/4/2020
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.

187 lines
4.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Read SMD and create DMX data
  4. //
  5. //=============================================================================
  6. #ifndef DMSMDSERIALIZER_H
  7. #define DMSMDSERIALIZER_H
  8. #if defined( _WIN32 )
  9. #pragma once
  10. #endif
  11. // Valve includes
  12. #include "datamodel/idatamodel.h"
  13. #include "tier1/utlbuffer.h"
  14. #include "tier1/utlstring.h"
  15. #include "tier1/utlvector.h"
  16. //-----------------------------------------------------------------------------
  17. // Forward declarations
  18. //-----------------------------------------------------------------------------
  19. class CDmeDag;
  20. class CDmeMesh;
  21. class CPolygonData;
  22. //-----------------------------------------------------------------------------
  23. // Serialization class for SMD files
  24. //-----------------------------------------------------------------------------
  25. class CDmSmdSerializer : public IDmSerializer
  26. {
  27. public:
  28. enum Axis_t
  29. {
  30. X_AXIS = 0,
  31. Y_AXIS = 1,
  32. Z_AXIS = 2
  33. };
  34. CDmSmdSerializer()
  35. : m_bOptAutoStripPrefix( false )
  36. , m_bOptImportSkeleton( true )
  37. , m_bOptAnimation( false )
  38. , m_flFrameRate( 30.0f )
  39. {
  40. SetUpAxis( Z_AXIS );
  41. }
  42. // Inherited from IDMSerializer
  43. virtual const char *GetName() const { return "smd"; }
  44. virtual const char *GetDescription() const { return "VALVe SMD"; }
  45. virtual bool IsBinaryFormat() const { return false; }
  46. virtual bool StoresVersionInFile() const { return true; }
  47. virtual int GetCurrentVersion() const { return 1; }
  48. virtual bool Serialize( CUtlBuffer &buf, CDmElement *pRoot ) { return false; } // No DMX -> SMD support
  49. virtual bool Unserialize(
  50. CUtlBuffer &utlBuf,
  51. const char *pszEncodingName,
  52. int nEncodingVersion,
  53. const char *pszSourceFormatName,
  54. int nSourceFormatVersion,
  55. DmFileId_t nDmFileId,
  56. DmConflictResolution_t nDmConflictResolution,
  57. CDmElement **ppDmRoot );
  58. // Methods used for importing (only should return non-NULL for serializers that return false from StoresVersionInFile)
  59. virtual const char *GetImportedFormat() const { return NULL; }
  60. virtual int GetImportedVersion() const { return 1; }
  61. // CDmSmdSerializer
  62. CDmElement *ReadSMD( const char *pszFilename, CDmeMesh **ppDmeMeshCreated = NULL );
  63. void SetUpAxis( Axis_t nUpAxis );
  64. Axis_t GetUpAxis() const { return m_nUpAxis; }
  65. void SetIsAnimation( bool bOptAnimation ) { m_bOptAnimation = bOptAnimation; }
  66. bool IsReadAnimation() const { return m_bOptAnimation; }
  67. void SetFrameRate( float flFrameRate ) { m_flFrameRate = MAX( 0.1f, flFrameRate ); } // Don't allow 0 or negative frame rate
  68. float GetFrameRate() const { return m_flFrameRate; }
  69. //-----------------------------------------------------------------------------
  70. //
  71. //-----------------------------------------------------------------------------
  72. struct SmdJoint_t
  73. {
  74. int m_nId; // The id parsed from the SMD file
  75. int m_nActualId; // The actual node id which is created after sorting and creating all joints in order with no gaps in numbering, corresponds to joitnIndex in DmeModel
  76. CUtlString m_sName;
  77. int m_nParentId;
  78. int m_nLineNumber;
  79. CDmeDag *m_pDmeDag;
  80. SmdJoint_t()
  81. : m_nId( -1 )
  82. , m_nActualId( -1 )
  83. , m_nParentId( -1 )
  84. , m_nLineNumber( -1 )
  85. , m_pDmeDag( NULL )
  86. {}
  87. };
  88. //-----------------------------------------------------------------------------
  89. //
  90. //-----------------------------------------------------------------------------
  91. typedef CUtlMap< int, SmdJoint_t > SmdJointMap_t;
  92. protected:
  93. void ParserGetNodeName( const char *pszBuf, CUtlString &sName ) const;
  94. bool ParserHandleSkeletonLine(
  95. const char *pszBuf,
  96. CUtlString &sName,
  97. int &nId,
  98. int &nParentId ) const;
  99. CDmElement *CDmSmdSerializer::ReadSMD(
  100. CUtlBuffer &inUtlBuf,
  101. DmFileId_t nDmFileId,
  102. const char *pszFilename,
  103. CDmeMesh **ppDmeMeshCreated );
  104. //-----------------------------------------------------------------------------
  105. //
  106. //-----------------------------------------------------------------------------
  107. class CNodeData
  108. {
  109. public:
  110. CNodeData()
  111. : m_nParentIndex( -1 )
  112. , m_bSkinned( false )
  113. , m_nInfluenceIndex( 0 )
  114. , m_pDmeDag( NULL )
  115. {
  116. }
  117. bool Valid() const
  118. {
  119. return m_pDmeDag != NULL;
  120. }
  121. void Reset()
  122. {
  123. m_pDmeDag = NULL;
  124. }
  125. int m_nParentIndex;
  126. bool m_bSkinned;
  127. int m_nInfluenceIndex;
  128. CDmeDag *m_pDmeDag;
  129. CUtlVector< Vector > m_positions;
  130. };
  131. void FixNodeName( CUtlString &sName ) const;
  132. void ParserSetJoint(
  133. const SmdJointMap_t &smdJointMap,
  134. int nFrame, int nId,
  135. const Vector &vPosition, const RadianEuler &eRadianEulerXYZ,
  136. const char *pszFilename, int nLineNumber );
  137. Axis_t m_nUpAxis; // 0 == X, 1 == Y, 2 == Z
  138. matrix3x4_t m_mAdj; // Matrix to adjust for SMD source orientation to DMX Y up
  139. matrix3x4_t m_mAdjNormal; // Matrix to adjust normals, inverse transpose of m_mAdj
  140. public:
  141. bool m_bOptImportSkeleton;
  142. bool m_bOptAutoStripPrefix;
  143. bool m_bOptAnimation;
  144. float m_flFrameRate;
  145. CUtlString m_sNodeDelPrefix;
  146. CUtlString m_sNodeAddPrefix;
  147. };
  148. #endif // DMSMDSERIALIZER_H