T3DTransform.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. #include "T3DTransform.h"
  23. #include "math/mPoint.h"
  24. #include "math/mQuat.h"
  25. //---------------------------------------------------------
  26. // T3DTransform
  27. //---------------------------------------------------------
  28. void Transform3D::setParentTransform(Transform3D * parent)
  29. {
  30. if (_parentTransform == parent)
  31. return;
  32. _flags |= Transform3D::ParentDirty;
  33. _parentTransform = parent;
  34. if (_dirtyListener != NULL)
  35. _dirtyListener->onTransformDirty();
  36. }
  37. bool Transform3D::hasObjectScale() const
  38. {
  39. if (hasLocalScale())
  40. return true;
  41. // check all parent transforms except last for scale
  42. Transform3D * walk = _parentTransform;
  43. while (walk != NULL && walk->_parentTransform != NULL)
  44. {
  45. if (walk->hasLocalScale())
  46. return true;
  47. walk = walk->getParentTransform();
  48. }
  49. return false;
  50. }
  51. bool Transform3D::hasWorldScale() const
  52. {
  53. if (hasLocalScale())
  54. return true;
  55. // check all parent transforms for scale
  56. Transform3D * walk = _parentTransform;
  57. while (walk != NULL)
  58. {
  59. if (walk->hasLocalScale())
  60. return true;
  61. walk = walk->getParentTransform();
  62. }
  63. return false;
  64. }
  65. void Transform3D::setWorldMatrix(const MatrixF & world)
  66. {
  67. if (_parentTransform != NULL)
  68. {
  69. MatrixF parentMatrix;
  70. _parentTransform->getWorldMatrix(parentMatrix, true);
  71. MatrixF parentMatrixInv = parentMatrix;
  72. parentMatrixInv.inverse();
  73. MatrixF localMat;
  74. localMat = parentMatrixInv * world;
  75. setLocalMatrix(localMat);
  76. }
  77. else
  78. setLocalMatrix(world);
  79. }
  80. void Transform3D::setObjectMatrix(const MatrixF & objMatrix)
  81. {
  82. if (_parentTransform != NULL)
  83. {
  84. MatrixF parentMatrix;
  85. _parentTransform->getObjectMatrix(parentMatrix, true);
  86. MatrixF parentMatrixInv = parentMatrix;
  87. parentMatrixInv.inverse();
  88. MatrixF localMat;
  89. localMat = parentMatrixInv * objMatrix;
  90. setLocalMatrix(localMat);
  91. }
  92. else
  93. setLocalMatrix(objMatrix);
  94. }
  95. bool Transform3D::isChildOf(Transform3D * parent, bool recursive) const
  96. {
  97. if (_parentTransform != NULL)
  98. {
  99. if (_parentTransform == parent)
  100. return true;
  101. else if (recursive)
  102. return _parentTransform->isChildOf(parent, true);
  103. else
  104. return false;
  105. }
  106. else
  107. {
  108. return false;
  109. }
  110. }
  111. //---------------------------------------------------------
  112. // Transform3DInPlace
  113. //---------------------------------------------------------
  114. Point3F Transform3DInPlace::getPosition() const
  115. {
  116. return _position;
  117. }
  118. void Transform3DInPlace::setPosition(const Point3F & position)
  119. {
  120. _position = position;
  121. _flags |= Transform3D::LocalPositionDirty;
  122. if (_dirtyListener != NULL)
  123. _dirtyListener->onTransformDirty();
  124. }
  125. QuatF Transform3DInPlace::getRotation() const
  126. {
  127. return _rotation;
  128. }
  129. void Transform3DInPlace::setRotation(const QuatF & rotation)
  130. {
  131. _rotation = rotation;
  132. _flags |= Transform3D::LocalRotationDirty;
  133. if (_dirtyListener != NULL)
  134. _dirtyListener->onTransformDirty();
  135. }
  136. Point3F Transform3DInPlace::getScale() const
  137. {
  138. return _scale;
  139. }
  140. void Transform3DInPlace::setScale(const Point3F & scale)
  141. {
  142. _scale = scale;
  143. _flags |= Transform3D::LocalScaleDirty;
  144. if (_dirtyListener != NULL)
  145. _dirtyListener->onTransformDirty();
  146. }
  147. void Transform3DInPlace::getWorldMatrix(MatrixF & worldMat, bool includeLocalScale) const
  148. {
  149. if (_parentTransform == NULL)
  150. getLocalMatrix(worldMat, includeLocalScale);
  151. else
  152. {
  153. MatrixF localMat, parentMat;
  154. getLocalMatrix(localMat, includeLocalScale);
  155. _parentTransform->getWorldMatrix(parentMat, true);
  156. worldMat = parentMat * localMat;
  157. }
  158. }
  159. void Transform3DInPlace::getObjectMatrix(MatrixF & objectMat, bool includeLocalScale) const
  160. {
  161. if (_parentTransform == NULL)
  162. objectMat = MatrixF::smIdentity;
  163. else if (_parentTransform->getParentTransform() == NULL)
  164. getLocalMatrix(objectMat, includeLocalScale);
  165. else
  166. {
  167. MatrixF localMat, parentMat;
  168. getLocalMatrix(localMat, includeLocalScale);
  169. _parentTransform->getObjectMatrix(parentMat, true);
  170. objectMat = parentMat * localMat;
  171. }
  172. }
  173. void Transform3DInPlace::getLocalMatrix(MatrixF & localMat, bool includeLocalScale) const
  174. {
  175. _rotation.setMatrix(&localMat);
  176. localMat.setColumn(3,_position);
  177. if (includeLocalScale)
  178. localMat.scale(_scale);
  179. }
  180. void Transform3DInPlace::setLocalMatrix(const MatrixF & localMat)
  181. {
  182. _rotation.set(localMat);
  183. _position = localMat.getPosition();
  184. _scale = localMat.getScale();
  185. _flags |= Transform3D::LocalScaleDirty | Transform3D::LocalRotationDirty | Transform3D::LocalPositionDirty;
  186. if (_dirtyListener != NULL)
  187. _dirtyListener->onTransformDirty();
  188. }
  189. //---------------------------------------------------------
  190. // TSTransform3D
  191. //---------------------------------------------------------
  192. TSTransform3D::TSTransform3D(TSShapeInstance * si, S32 nodeIndex)
  193. {
  194. _shapeInstance = si;
  195. _nodeIndex = nodeIndex;
  196. AssertFatal(_nodeIndex >= 0 && _nodeIndex < _shapeInstance->mNodeTransforms.size(), "TSTransform3D nodeIndex out of range");
  197. }
  198. Point3F TSTransform3D::getPosition() const
  199. {
  200. if (!doHandleLocal())
  201. {
  202. MatrixF mat;
  203. return getTSLocal(mat).getPosition();
  204. }
  205. return _position;
  206. }
  207. void TSTransform3D::setPosition(const Point3F & position)
  208. {
  209. setHandleLocal(true);
  210. _position = position;
  211. _flags |= Transform3D::LocalPositionDirty;
  212. if (_dirtyListener != NULL)
  213. _dirtyListener->onTransformDirty();
  214. }
  215. QuatF TSTransform3D::getRotation() const
  216. {
  217. if (!doHandleLocal())
  218. {
  219. MatrixF mat;
  220. return QuatF(getTSLocal(mat));
  221. }
  222. return _rotation;
  223. }
  224. void TSTransform3D::setRotation(const QuatF & rotation)
  225. {
  226. setHandleLocal(true);
  227. _rotation = rotation;
  228. _flags |= Transform3D::LocalRotationDirty;
  229. if (_dirtyListener != NULL)
  230. _dirtyListener->onTransformDirty();
  231. }
  232. Point3F TSTransform3D::getScale() const
  233. {
  234. if (!doHandleLocal())
  235. {
  236. MatrixF mat;
  237. return getTSLocal(mat).getScale();
  238. }
  239. return _scale;
  240. }
  241. void TSTransform3D::setScale(const Point3F & scale)
  242. {
  243. setHandleLocal(true);
  244. _scale = scale;
  245. _flags |= Transform3D::LocalScaleDirty;
  246. if (_dirtyListener != NULL)
  247. _dirtyListener->onTransformDirty();
  248. }
  249. void TSTransform3D::getWorldMatrix(MatrixF & worldMat, bool includeLocalScale) const
  250. {
  251. _shapeInstance->animate();
  252. if (_parentTransform == NULL)
  253. {
  254. worldMat = _shapeInstance->mNodeTransforms[_nodeIndex];
  255. }
  256. else
  257. {
  258. MatrixF parentMat;
  259. _parentTransform->getWorldMatrix(parentMat, true);
  260. worldMat = parentMat * _shapeInstance->mNodeTransforms[_nodeIndex];
  261. }
  262. }
  263. void TSTransform3D::getObjectMatrix(MatrixF & objectMat, bool includeLocalScale) const
  264. {
  265. if (_parentTransform == NULL)
  266. objectMat = MatrixF::smIdentity;
  267. else if (_parentTransform->getParentTransform() == NULL)
  268. {
  269. _shapeInstance->animate();
  270. objectMat = _shapeInstance->mNodeTransforms[_nodeIndex];
  271. }
  272. else
  273. {
  274. _shapeInstance->animate();
  275. MatrixF parentMat;
  276. _parentTransform->getObjectMatrix(parentMat, true);
  277. objectMat = parentMat * _shapeInstance->mNodeTransforms[_nodeIndex];
  278. }
  279. }
  280. void TSTransform3D::getLocalMatrix(MatrixF & localMat, bool includeLocalScale) const
  281. {
  282. if (doHandleLocal())
  283. {
  284. _rotation.setMatrix(&localMat);
  285. localMat.setPosition(_position);
  286. if (includeLocalScale)
  287. localMat.scale(_scale);
  288. }
  289. else
  290. {
  291. _shapeInstance->animate();
  292. localMat = _shapeInstance->mNodeTransforms[_nodeIndex];
  293. if (!includeLocalScale && (_flags & Transform3D::LocalHasScale) != Transform3D::None)
  294. {
  295. // reverse any scale on matrix -- this is a inconvenient, but not a common case
  296. Point3F scale = localMat.getScale();
  297. scale.x = 1.0f / scale.x;
  298. scale.y = 1.0f / scale.y;
  299. scale.z = 1.0f / scale.z;
  300. localMat.scale(scale);
  301. }
  302. }
  303. }
  304. void TSTransform3D::setLocalMatrix(const MatrixF & localMatrix)
  305. {
  306. setHandleLocal(true);
  307. _position = localMatrix.getPosition();
  308. _rotation.set(localMatrix);
  309. _scale = localMatrix.getScale();
  310. _flags |= Transform3D::LocalScaleDirty | Transform3D::LocalRotationDirty | Transform3D::LocalPositionDirty;
  311. if (_dirtyListener != NULL)
  312. _dirtyListener->onTransformDirty();
  313. }
  314. MatrixF & TSTransform3D::getTSLocal(MatrixF & mat) const
  315. {
  316. _shapeInstance->animate();
  317. // if node has no parent, easy enough to just grab the matrix of the node
  318. int parentIdx = _shapeInstance->getShape()->nodes[_nodeIndex].parentIndex;
  319. if (parentIdx < 0)
  320. {
  321. return _shapeInstance->mNodeTransforms[_nodeIndex];
  322. }
  323. // has parent, local is transform from this node to parent so get local matrix the hard way
  324. MatrixF parentMat = _shapeInstance->mNodeTransforms[parentIdx];
  325. parentMat.inverse();
  326. mat = parentMat * _shapeInstance->mNodeTransforms[_nodeIndex];
  327. return mat;
  328. }
  329. void TSTransform3D::setHandleLocal(bool handleLocal)
  330. {
  331. if (handleLocal == doHandleLocal())
  332. return;
  333. if (handleLocal)
  334. {
  335. _position = getPosition();
  336. _rotation = getRotation();
  337. _scale = getScale();
  338. _shapeInstance->setNodeAnimationState(_nodeIndex, 0, this);
  339. }
  340. else
  341. _shapeInstance->setNodeAnimationState(_nodeIndex, 0);
  342. _flags ^= TSTransform3D::HandleLocal;
  343. }
  344. void TSTransform3D::setNodeTransform(TSShapeInstance * si, S32 nodeIndex, MatrixF & localTransform)
  345. {
  346. AssertFatal(si == _shapeInstance,"TSTransform3D hooked up to wrong shape.");
  347. getLocalMatrix(localTransform, true);
  348. }