T3DTransform.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _T3DTRANSFORM_H_
  23. #define _T3DTRANSFORM_H_
  24. #include "ts/tsShapeInstance.h"
  25. #include "math/mMatrix.h"
  26. //---------------------------------------------------------
  27. // T3DTransform
  28. //---------------------------------------------------------
  29. class Transform3D
  30. {
  31. public:
  32. class IDirtyListener
  33. {
  34. public:
  35. virtual void onTransformDirty() = 0;
  36. };
  37. // local/object/world matrix access
  38. void setWorldMatrix(const MatrixF & world);
  39. void setObjectMatrix(const MatrixF & objMatrix);
  40. virtual void setLocalMatrix(const MatrixF & localMatrix) = 0;
  41. virtual void getWorldMatrix(MatrixF & worldMatrix, bool includeLocalScale) const = 0;
  42. virtual void getObjectMatrix(MatrixF & objectMatrix, bool includeLocalScale) const = 0;
  43. virtual void getLocalMatrix(MatrixF & localMatrix, bool includeLocalScale) const = 0;
  44. MatrixF getWorldMatrix() const
  45. {
  46. MatrixF world;
  47. getWorldMatrix(world, true);
  48. return world;
  49. }
  50. MatrixF getObjectMatrix() const
  51. {
  52. MatrixF objMatrix;
  53. getObjectMatrix(objMatrix, true);
  54. return objMatrix;
  55. }
  56. MatrixF getLocalMatrix() const
  57. {
  58. MatrixF loc;
  59. getLocalMatrix(loc, true);
  60. return loc;
  61. }
  62. // local position/rotation/scale
  63. virtual Point3F getPosition() const = 0;
  64. virtual void setPosition(const Point3F & position) = 0;
  65. virtual QuatF getRotation() const = 0;
  66. virtual void setRotation(const QuatF & rotation) = 0;
  67. virtual Point3F getScale() const = 0;
  68. virtual void setScale(const Point3F & scale) = 0;
  69. // scale tests
  70. bool hasLocalScale() const
  71. {
  72. return (_flags & Transform3D::LocalHasScale) != Transform3D::None;
  73. }
  74. bool hasObjectScale() const;
  75. bool hasWorldScale() const;
  76. // parent/child methods
  77. Transform3D * getParentTransform() const
  78. {
  79. return _parentTransform;
  80. }
  81. void setParentTransform(Transform3D * parent);
  82. IDirtyListener * getDirtyListener() const
  83. {
  84. return _dirtyListener;
  85. }
  86. void setDirtyListener(IDirtyListener * dirtyListener)
  87. {
  88. _dirtyListener = dirtyListener;
  89. }
  90. bool isChildOf(Transform3D * parent, bool recursive) const;
  91. protected:
  92. enum TransformFlags
  93. {
  94. None = 0,
  95. LocalHasScale = 1 << 0,
  96. LocalPositionDirty = 1 << 1,
  97. LocalRotationDirty = 1 << 2,
  98. LocalScaleDirty = 1 << 3,
  99. LocalDirty = LocalPositionDirty | LocalRotationDirty | LocalScaleDirty,
  100. ParentDirty = 1 << 4,
  101. LastFlag = 1 << 4
  102. };
  103. Transform3D * _parentTransform;
  104. IDirtyListener * _dirtyListener;
  105. U32 _flags;
  106. };
  107. //---------------------------------------------------------
  108. // Transform3DInPlace
  109. //---------------------------------------------------------
  110. class Transform3DInPlace : public Transform3D
  111. {
  112. public:
  113. Transform3DInPlace() : _position(0,0,0), _rotation(0,0,0,1), _scale(1,1,1)
  114. {
  115. }
  116. Point3F getPosition() const;
  117. void setPosition(const Point3F & position);
  118. QuatF getRotation() const;
  119. void setRotation(const QuatF & rotation);
  120. Point3F getScale() const;
  121. void setScale(const Point3F & scale);
  122. void getWorldMatrix(MatrixF & worldMat, bool includeLocalScale) const;
  123. void getObjectMatrix(MatrixF & objectMat, bool includeLocalScale) const;
  124. void getLocalMatrix(MatrixF & localMat, bool includeLocalScale) const;
  125. void setLocalMatrix(const MatrixF & localMat);
  126. protected:
  127. Point3F _position;
  128. QuatF _rotation;
  129. Point3F _scale;
  130. };
  131. //---------------------------------------------------------
  132. // TSTransform3D
  133. //---------------------------------------------------------
  134. class TSTransform3D : public Transform3D, public TSCallback
  135. {
  136. public:
  137. TSTransform3D(TSShapeInstance * si, S32 nodeIndex);
  138. Point3F getPosition() const;
  139. void setPosition(const Point3F & position);
  140. QuatF getRotation() const;
  141. void setRotation(const QuatF & rotation);
  142. Point3F getScale() const;
  143. void setScale(const Point3F & scale);
  144. void getWorldMatrix(MatrixF & worldMat, bool includeLocalScale) const;
  145. void getObjectMatrix(MatrixF & objectMat, bool includeLocalScale) const;
  146. void getLocalMatrix(MatrixF & localMat, bool includeLocalScale) const;
  147. void setLocalMatrix(const MatrixF & localMatrix);
  148. // Define TSCallback interface
  149. void setNodeTransform(TSShapeInstance * si, S32 nodeIndex, MatrixF & localTransform);
  150. protected:
  151. enum TSTransformFlags
  152. {
  153. HandleLocal = Transform3D::LastFlag << 1,
  154. LastFlag = Transform3D::LastFlag << 1
  155. };
  156. bool doHandleLocal() const
  157. {
  158. return (_flags & (TransformFlags)TSTransform3D::HandleLocal) != Transform3D::None;
  159. }
  160. void setHandleLocal(bool handleLocal);
  161. MatrixF & getTSLocal(MatrixF & mat) const;
  162. TSShapeInstance * _shapeInstance;
  163. int _nodeIndex;
  164. Point3F _position;
  165. QuatF _rotation;
  166. Point3F _scale;
  167. };
  168. #endif // _T3DTRANSFORM_H_