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.

245 lines
5.6 KiB

  1. //=========== Copyright Valve Corporation, All rights reserved. ===============//
  2. //
  3. // Purpose:
  4. //=============================================================================//
  5. #ifndef TRANSFORMATIONS_H
  6. #define TRANSFORMATIONS_H
  7. #ifdef _WIN32
  8. #pragma once
  9. #endif
  10. #include "mathlib/mathlib.h"
  11. #include "mathlib/vmatrix.h"
  12. #include "tier1/utlbuffer.h"
  13. #include "panorama.h"
  14. #include "panoramatypes.h"
  15. #include "layout/uilength.h"
  16. namespace panorama
  17. {
  18. class CTransform3D
  19. {
  20. public:
  21. virtual ~CTransform3D() {}
  22. virtual ETransform3DType GetType() const = 0;
  23. virtual VMatrix GetTransformMatrix( float flParentWidth, float flParentHeight ) const = 0;
  24. virtual CTransform3D *Clone() const = 0;
  25. virtual void ScaleLengthValues( float flScaleFactor ) = 0;
  26. virtual bool BOnlyImpacts2DValues() = 0;
  27. virtual bool operator==( const CTransform3D &rhs ) const = 0;
  28. bool operator!=( const CTransform3D &rhs ) const { return !(*this == rhs); }
  29. };
  30. // Helpers for interpolating 3d transform matrixes
  31. struct DecomposedMatrix_t
  32. {
  33. DecomposedMatrix_t()
  34. {
  35. m_flTranslationXYZ[0] = 0.0f;
  36. m_flTranslationXYZ[1] = 0.0f;
  37. m_flTranslationXYZ[2] = 0.0f;
  38. m_flScaleXYZ[0] = 1.0;
  39. m_flScaleXYZ[1] = 1.0;
  40. m_flScaleXYZ[2] = 1.0;
  41. m_flSkewXY = 0.0f;
  42. m_flSkewXZ = 0.0f;
  43. m_flSkewYZ = 0.0f;
  44. m_quatTransform.Init();
  45. }
  46. float m_flTranslationXYZ[3];
  47. float m_flScaleXYZ[3];
  48. float m_flSkewXY;
  49. float m_flSkewXZ;
  50. float m_flSkewYZ;
  51. Quaternion m_quatTransform;
  52. };
  53. DecomposedMatrix_t DecomposeTransformMatrix( VMatrix matrix );
  54. VMatrix RecomposeTransformMatrix( const DecomposedMatrix_t &decomposed );
  55. VMatrix InterpolateTransformMatrix( VMatrix from, VMatrix to, float flTimeProgress );
  56. class CTransformRotate3D : public CTransform3D
  57. {
  58. public:
  59. // Transform in 3D space:
  60. // PITCH: Clockwise rotation around the Y axis.
  61. // YAW: Counterclockwise rotation around the Z axis.
  62. // ROLL: Counterclockwise rotation around the X axis.
  63. CTransformRotate3D( float flDegreesPitch, float flDegreesYaw, float flDegreesRoll )
  64. {
  65. QAngle angle( flDegreesPitch, flDegreesYaw, flDegreesRoll );
  66. AngleQuaternion( angle, m_quatTransform );
  67. }
  68. ETransform3DType GetType() const { return k_ETransform3DRotate; }
  69. VMatrix GetTransformMatrix( float flParentWidth, float flParentHeight ) const
  70. {
  71. matrix3x4_t mat;
  72. QuaternionMatrix( m_quatTransform, mat );
  73. return VMatrix( mat );
  74. }
  75. bool BOnlyImpacts2DValues()
  76. {
  77. return false;
  78. }
  79. virtual CTransform3D *Clone() const
  80. {
  81. CTransformRotate3D *pRet = new CTransformRotate3D();
  82. pRet->m_quatTransform = m_quatTransform;
  83. return pRet;
  84. }
  85. QAngle GetAngles() const
  86. {
  87. QAngle angle;
  88. QuaternionAngles( m_quatTransform, angle );
  89. return angle;
  90. }
  91. virtual void ScaleLengthValues( float flScaleFactor ) { }
  92. virtual bool operator==( const CTransform3D &other ) const
  93. {
  94. if ( other.GetType() != k_ETransform3DRotate )
  95. return false;
  96. const CTransformRotate3D &rhs = (const CTransformRotate3D &)other;
  97. return (m_quatTransform == rhs.m_quatTransform);
  98. }
  99. protected:
  100. CTransformRotate3D() {}
  101. private:
  102. Quaternion m_quatTransform;
  103. };
  104. class CTransformTranslate3D : public CTransform3D
  105. {
  106. public:
  107. // Translate in 3D space
  108. CTransformTranslate3D( float x, float y, float z )
  109. {
  110. m_x.SetLength( x );
  111. m_y.SetLength( y );
  112. m_z.SetLength( z );
  113. }
  114. CTransformTranslate3D( const CUILength &x, const CUILength &y, const CUILength &z )
  115. {
  116. m_x = x;
  117. m_y = y;
  118. m_z = z;
  119. }
  120. bool BOnlyImpacts2DValues()
  121. {
  122. if( fabs( m_z.GetValueAsLength( 100 ) - 0.0f ) < 0.0001f )
  123. return true;
  124. return false;
  125. }
  126. ETransform3DType GetType() const { return k_ETransform3DTranslate; }
  127. VMatrix GetTransformMatrix( float flParentWidth, float flParentHeight ) const
  128. {
  129. VMatrix mat = VMatrix::GetIdentityMatrix();
  130. mat.SetTranslation( Vector( m_x.GetValueAsLength( flParentWidth ), m_y.GetValueAsLength( flParentHeight ), m_z.GetValueAsLength( 0 ) ) );
  131. return mat;
  132. }
  133. virtual CTransform3D *Clone() const
  134. {
  135. return new CTransformTranslate3D( m_x, m_y, m_z );
  136. }
  137. virtual void ScaleLengthValues( float flScaleFactor )
  138. {
  139. m_x.ScaleLengthValue( flScaleFactor );
  140. m_y.ScaleLengthValue( flScaleFactor );
  141. m_z.ScaleLengthValue( flScaleFactor );
  142. }
  143. CUILength GetX() const { return m_x; }
  144. CUILength GetY() const { return m_y; }
  145. CUILength GetZ() const { return m_z; }
  146. virtual bool operator==( const CTransform3D &other ) const
  147. {
  148. if ( other.GetType() != k_ETransform3DTranslate )
  149. return false;
  150. const CTransformTranslate3D &rhs = (const CTransformTranslate3D &)other;
  151. return ( m_x == rhs.m_x && m_y == rhs.m_y && m_z == rhs.m_z );
  152. }
  153. private:
  154. CUILength m_x;
  155. CUILength m_y;
  156. CUILength m_z;
  157. };
  158. class CTransformScale3D : public CTransform3D
  159. {
  160. public:
  161. // Scale in 3D space
  162. CTransformScale3D( float x, float y, float z ) : m_VecScale( x, y, z )
  163. {
  164. }
  165. ETransform3DType GetType() const { return k_ETransform3DScale; }
  166. bool BOnlyImpacts2DValues()
  167. {
  168. return false;
  169. }
  170. VMatrix GetTransformMatrix( float flParentWidth, float flParentHeight ) const
  171. {
  172. VMatrix mat;
  173. MatrixBuildScale( mat, m_VecScale.x, m_VecScale.y, m_VecScale.z );
  174. return mat;
  175. }
  176. virtual CTransform3D *Clone() const
  177. {
  178. return new CTransformScale3D( m_VecScale.x, m_VecScale.y, m_VecScale.z );
  179. }
  180. virtual void ScaleLengthValues( float flScaleFactor ) { }
  181. float GetX() const { return m_VecScale.x; }
  182. float GetY() const { return m_VecScale.y; }
  183. float GetZ() const { return m_VecScale.z; }
  184. virtual bool operator==( const CTransform3D &other ) const
  185. {
  186. if ( other.GetType() != k_ETransform3DScale )
  187. return false;
  188. const CTransformScale3D &rhs = (const CTransformScale3D &)other;
  189. return ( m_VecScale == rhs.m_VecScale );
  190. }
  191. private:
  192. Vector m_VecScale;
  193. };
  194. } // namespace panorama
  195. #endif // TRANSFORMATIONS_H