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.

146 lines
3.8 KiB

  1. //====== Copyright � Valve Corporation, All rights reserved. =======
  2. #ifndef MODELLIB_CLOTHHELPERS
  3. #define MODELLIB_CLOTHHELPERS
  4. #include "tier1/utlstring.h"
  5. #include "tier1/utlhashtable.h"
  6. #include "tier1/utlvector.h"
  7. class CClothBoneMap
  8. {
  9. public:
  10. CClothBoneMap();
  11. ~CClothBoneMap();
  12. void RegisterBone( const char *pName, int nBone );
  13. public:
  14. struct Patch_t
  15. {
  16. Patch_t(): m_nColumnCount( 0 ), m_nRowCount( 0 ) {}
  17. int m_nColumnCount;
  18. int m_nRowCount;
  19. CUtlString m_Name;
  20. public:
  21. void Insert( int nRow, int nColumn, int nBone );
  22. int GetBone( int nRow, int nColumn );
  23. protected:
  24. CUtlHashtable< uint, int > m_Bones; // ( Row << 16 ) | Column => Bone name
  25. };
  26. public:
  27. Patch_t *GetPatch( int nPatch ) { return m_Patches[nPatch]; }
  28. int GetPatchCount() const { return m_Patches.Count(); }
  29. protected:
  30. CUtlVector< Patch_t* > m_Patches;
  31. protected:
  32. Patch_t *GetPatch( const char *pName, const char *pNameEnd );
  33. };
  34. class CReverseParser
  35. {
  36. public:
  37. CReverseParser( const char *pString ): m_pString( pString ), m_pEnd( pString + V_strlen( pString ) ) {}
  38. int MatchInt();
  39. char ReadChar( );
  40. bool IsValid() { return m_pEnd != NULL; }
  41. const char *GetEnd() const { return m_pEnd; }
  42. CUtlString GetRemainder()const;
  43. protected:
  44. const char * m_pString;
  45. const char * m_pEnd;
  46. };
  47. inline Vector GetScaleVector( const matrix3x4_t &tm )
  48. {
  49. Vector vScale( tm.GetColumn( X_AXIS ).Length( ), tm.GetColumn( Y_AXIS ).Length( ), tm.GetColumn( Z_AXIS ).Length( ) );
  50. return vScale;
  51. }
  52. inline bool IsGoodWorldTransform( const matrix3x4_t &tm, float flExpectedScale = 1.0f, float flTolerance = 0.001f )
  53. {
  54. Vector vScale = GetScaleVector( tm );
  55. return tm.IsValid( ) && ( vScale - Vector( flExpectedScale, flExpectedScale, flExpectedScale ) ).Length( ) < flTolerance && tm.GetOrthogonalityError() < flTolerance;
  56. }
  57. //-----------------------------------------------------------------------------
  58. // functions for dealing with scale
  59. //-----------------------------------------------------------------------------
  60. inline matrix3x4_t ScaleMatrix3x3( const matrix3x4_t &transform, float flScale )
  61. {
  62. if ( flScale == 1.0f )
  63. return transform;
  64. matrix3x4_t out;
  65. for ( int i = 0; i < 3; ++i )
  66. {
  67. for ( int j = 0; j < 3; ++j )
  68. {
  69. out[ i ][ j ] = transform[ i ][ j ] * flScale;
  70. }
  71. out[ i ][ 3 ] = transform[ i ][ 3 ];
  72. }
  73. return out;
  74. }
  75. inline void Set3x3( matrix3x4a_t &dest, const matrix3x4a_t &src )
  76. {
  77. dest.m_flMatVal[ 0 ][ 0 ] = src.m_flMatVal[ 0 ][ 0 ];
  78. dest.m_flMatVal[ 0 ][ 1 ] = src.m_flMatVal[ 0 ][ 1 ];
  79. dest.m_flMatVal[ 0 ][ 2 ] = src.m_flMatVal[ 0 ][ 2 ];
  80. dest.m_flMatVal[ 1 ][ 0 ] = src.m_flMatVal[ 1 ][ 0 ];
  81. dest.m_flMatVal[ 1 ][ 1 ] = src.m_flMatVal[ 1 ][ 1 ];
  82. dest.m_flMatVal[ 1 ][ 2 ] = src.m_flMatVal[ 1 ][ 2 ];
  83. dest.m_flMatVal[ 2 ][ 0 ] = src.m_flMatVal[ 2 ][ 0 ];
  84. dest.m_flMatVal[ 2 ][ 1 ] = src.m_flMatVal[ 2 ][ 1 ];
  85. dest.m_flMatVal[ 2 ][ 2 ] = src.m_flMatVal[ 2 ][ 2 ];
  86. }
  87. inline void Set3x3( matrix3x4a_t &dest, const matrix3x4a_t &src, const Vector &vNewOrigin )
  88. {
  89. Set3x3( dest, src );
  90. dest.SetOrigin( vNewOrigin );
  91. }
  92. inline matrix3x4_t AlignX( matrix3x4a_t &tm, const Vector &vNewX, const Vector &vOrigin )
  93. {
  94. float flNewXLen = vNewX.Length( );
  95. if ( flNewXLen > 0.03f ) // if the new X axis is not well-defined, it makes little sense to adjust the base
  96. {
  97. Quaternion q = RotateBetween( tm.GetColumn( X_AXIS ).Normalized( ), vNewX / flNewXLen );
  98. matrix3x4_t rot = QuaternionMatrix( q ), rotated = rot * tm;
  99. AssertDbg( CrossProduct( rotated.GetColumn( X_AXIS ), vNewX ).Length( ) < 0.002f * vNewX.Length() );
  100. AssertDbg( rotated.GetOrthogonalityError() < 0.001f );
  101. rotated.SetOrigin( vOrigin );
  102. return rotated;
  103. }
  104. else
  105. {
  106. return tm;
  107. }
  108. }
  109. inline matrix3x4_t Descale( const matrix3x4a_t &tm )
  110. {
  111. float flInvScale = 1.0f / tm.GetColumn( X_AXIS ).Length();
  112. return ScaleMatrix3x3( tm, flInvScale );
  113. }
  114. #endif