Transform.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. #include "Base.h"
  2. #include "Transform.h"
  3. #include "Game.h"
  4. #include "Node.h"
  5. namespace gameplay
  6. {
  7. int Transform::_suspendTransformChanged(0);
  8. std::vector<Transform*> Transform::_transformsChanged;
  9. Transform::Transform()
  10. : _matrixDirtyBits(0), _listeners(NULL)
  11. {
  12. _targetType = AnimationTarget::TRANSFORM;
  13. _scale.set(Vector3::one());
  14. }
  15. Transform::Transform(const Vector3& scale, const Quaternion& rotation, const Vector3& translation)
  16. : _matrixDirtyBits(0), _listeners(NULL)
  17. {
  18. _targetType = AnimationTarget::TRANSFORM;
  19. set(scale, rotation, translation);
  20. }
  21. Transform::Transform(const Vector3& scale, const Matrix& rotation, const Vector3& translation)
  22. : _matrixDirtyBits(0), _listeners(NULL)
  23. {
  24. _targetType = AnimationTarget::TRANSFORM;
  25. set(scale, rotation, translation);
  26. }
  27. Transform::Transform(const Transform& copy)
  28. : _matrixDirtyBits(0), _listeners(NULL)
  29. {
  30. _targetType = AnimationTarget::TRANSFORM;
  31. set(copy);
  32. }
  33. Transform::~Transform()
  34. {
  35. SAFE_DELETE(_listeners);
  36. }
  37. void Transform::suspendTransformChanged()
  38. {
  39. _suspendTransformChanged++;
  40. }
  41. void Transform::resumeTransformChanged()
  42. {
  43. if (_suspendTransformChanged == 0) // We haven't suspended transformChanged() calls, so do nothing.
  44. return;
  45. if (_suspendTransformChanged == 1)
  46. {
  47. // Call transformChanged() on all transforms in the list
  48. unsigned int transformCount = _transformsChanged.size();
  49. for (unsigned int i = 0; i < transformCount; i++)
  50. {
  51. Transform* t = _transformsChanged.at(i);
  52. t->transformChanged();
  53. }
  54. // Go through list and reset DIRTY_NOTIFY bit. The list could potentially be larger here if the
  55. // transforms we were delaying calls to transformChanged() have any child nodes.
  56. transformCount = _transformsChanged.size();
  57. for (unsigned int i = 0; i < transformCount; i++)
  58. {
  59. Transform* t = _transformsChanged.at(i);
  60. t->_matrixDirtyBits &= ~DIRTY_NOTIFY;
  61. }
  62. // empty list for next frame.
  63. _transformsChanged.clear();
  64. }
  65. _suspendTransformChanged--;
  66. }
  67. bool Transform::isTransformChangedSuspended()
  68. {
  69. return (_suspendTransformChanged > 0);
  70. }
  71. const Matrix& Transform::getMatrix() const
  72. {
  73. if (_matrixDirtyBits)
  74. {
  75. bool hasTranslation = !_translation.isZero();
  76. bool hasScale = !_scale.isOne();
  77. bool hasRotation = !_rotation.isIdentity();
  78. // Compose the matrix in TRS order since we use column-major matrices with column vectors and
  79. // multiply M*v (as opposed to XNA and DirectX that use row-major matrices with row vectors and multiply v*M).
  80. if (hasTranslation || (_matrixDirtyBits & DIRTY_TRANSLATION) == DIRTY_TRANSLATION)
  81. {
  82. Matrix::createTranslation(_translation, &_matrix);
  83. if (hasRotation || (_matrixDirtyBits & DIRTY_ROTATION) == DIRTY_ROTATION)
  84. {
  85. _matrix.rotate(_rotation);
  86. }
  87. if (hasScale || (_matrixDirtyBits & DIRTY_SCALE) == DIRTY_SCALE)
  88. {
  89. _matrix.scale(_scale);
  90. }
  91. }
  92. else if (hasRotation || (_matrixDirtyBits & DIRTY_ROTATION) == DIRTY_ROTATION)
  93. {
  94. Matrix::createRotation(_rotation, &_matrix);
  95. if (hasScale || (_matrixDirtyBits & DIRTY_SCALE) == DIRTY_SCALE)
  96. {
  97. _matrix.scale(_scale);
  98. }
  99. }
  100. else if (hasScale || (_matrixDirtyBits & DIRTY_SCALE) == DIRTY_SCALE)
  101. {
  102. Matrix::createScale(_scale, &_matrix);
  103. }
  104. _matrixDirtyBits &= ~DIRTY_TRANSLATION & ~DIRTY_ROTATION & ~DIRTY_SCALE;
  105. }
  106. return _matrix;
  107. }
  108. const Vector3& Transform::getScale() const
  109. {
  110. return _scale;
  111. }
  112. void Transform::getScale(Vector3* scale) const
  113. {
  114. scale->set(_scale);
  115. }
  116. float Transform::getScaleX() const
  117. {
  118. return _scale.x;
  119. }
  120. float Transform::getScaleY() const
  121. {
  122. return _scale.y;
  123. }
  124. float Transform::getScaleZ() const
  125. {
  126. return _scale.z;
  127. }
  128. const Quaternion& Transform::getRotation() const
  129. {
  130. return _rotation;
  131. }
  132. void Transform::getRotation(Quaternion* rotation) const
  133. {
  134. GP_ASSERT(rotation);
  135. rotation->set(_rotation);
  136. }
  137. void Transform::getRotation(Matrix* rotation) const
  138. {
  139. GP_ASSERT(rotation);
  140. Matrix::createRotation(_rotation, rotation);
  141. }
  142. float Transform::getRotation(Vector3* axis) const
  143. {
  144. GP_ASSERT(axis);
  145. return _rotation.toAxisAngle(axis);
  146. }
  147. const Vector3& Transform::getTranslation() const
  148. {
  149. return _translation;
  150. }
  151. void Transform::getTranslation(Vector3* translation) const
  152. {
  153. translation->set(_translation);
  154. }
  155. float Transform::getTranslationX() const
  156. {
  157. return _translation.x;
  158. }
  159. float Transform::getTranslationY() const
  160. {
  161. return _translation.y;
  162. }
  163. float Transform::getTranslationZ() const
  164. {
  165. return _translation.z;
  166. }
  167. Vector3 Transform::getForwardVector() const
  168. {
  169. Vector3 v;
  170. getForwardVector(&v);
  171. return v;
  172. }
  173. void Transform::getForwardVector(Vector3* dst) const
  174. {
  175. getMatrix().getForwardVector(dst);
  176. }
  177. Vector3 Transform::getBackVector() const
  178. {
  179. Vector3 v;
  180. getBackVector(&v);
  181. return v;
  182. }
  183. void Transform::getBackVector(Vector3* dst) const
  184. {
  185. getMatrix().getBackVector(dst);
  186. }
  187. Vector3 Transform::getUpVector() const
  188. {
  189. Vector3 v;
  190. getUpVector(&v);
  191. return v;
  192. }
  193. void Transform::getUpVector(Vector3* dst) const
  194. {
  195. getMatrix().getUpVector(dst);
  196. }
  197. Vector3 Transform::getDownVector() const
  198. {
  199. Vector3 v;
  200. getDownVector(&v);
  201. return v;
  202. }
  203. void Transform::getDownVector(Vector3* dst) const
  204. {
  205. getMatrix().getDownVector(dst);
  206. }
  207. Vector3 Transform::getLeftVector() const
  208. {
  209. Vector3 v;
  210. getLeftVector(&v);
  211. return v;
  212. }
  213. void Transform::getLeftVector(Vector3* dst) const
  214. {
  215. getMatrix().getLeftVector(dst);
  216. }
  217. Vector3 Transform::getRightVector() const
  218. {
  219. Vector3 v;
  220. getRightVector(&v);
  221. return v;
  222. }
  223. void Transform::getRightVector(Vector3* dst) const
  224. {
  225. getMatrix().getRightVector(dst);
  226. }
  227. void Transform::rotate(float qx, float qy, float qz, float qw)
  228. {
  229. Quaternion q(qx, qy, qz, qw);
  230. _rotation.multiply(q);
  231. dirty(DIRTY_ROTATION);
  232. }
  233. void Transform::rotate(const Quaternion& rotation)
  234. {
  235. _rotation.multiply(rotation);
  236. dirty(DIRTY_ROTATION);
  237. }
  238. void Transform::rotate(const Vector3& axis, float angle)
  239. {
  240. Quaternion rotationQuat;
  241. Quaternion::createFromAxisAngle(axis, angle, &rotationQuat);
  242. _rotation.multiply(rotationQuat);
  243. _rotation.normalize();
  244. dirty(DIRTY_ROTATION);
  245. }
  246. void Transform::rotate(const Matrix& rotation)
  247. {
  248. Quaternion rotationQuat;
  249. Quaternion::createFromRotationMatrix(rotation, &rotationQuat);
  250. _rotation.multiply(rotationQuat);
  251. dirty(DIRTY_ROTATION);
  252. }
  253. void Transform::rotateX(float angle)
  254. {
  255. Quaternion rotationQuat;
  256. Quaternion::createFromAxisAngle(Vector3::unitX(), angle, &rotationQuat);
  257. _rotation.multiply(rotationQuat);
  258. dirty(DIRTY_ROTATION);
  259. }
  260. void Transform::rotateY(float angle)
  261. {
  262. Quaternion rotationQuat;
  263. Quaternion::createFromAxisAngle(Vector3::unitY(), angle, &rotationQuat);
  264. _rotation.multiply(rotationQuat);
  265. dirty(DIRTY_ROTATION);
  266. }
  267. void Transform::rotateZ(float angle)
  268. {
  269. Quaternion rotationQuat;
  270. Quaternion::createFromAxisAngle(Vector3::unitZ(), angle, &rotationQuat);
  271. _rotation.multiply(rotationQuat);
  272. dirty(DIRTY_ROTATION);
  273. }
  274. void Transform::scale(float scale)
  275. {
  276. _scale.scale(scale);
  277. dirty(DIRTY_SCALE);
  278. }
  279. void Transform::scale(float sx, float sy, float sz)
  280. {
  281. _scale.x *= sx;
  282. _scale.y *= sy;
  283. _scale.z *= sz;
  284. dirty(DIRTY_SCALE);
  285. }
  286. void Transform::scale(const Vector3& scale)
  287. {
  288. _scale.x *= scale.x;
  289. _scale.y *= scale.y;
  290. _scale.z *= scale.z;
  291. dirty(DIRTY_SCALE);
  292. }
  293. void Transform::scaleX(float sx)
  294. {
  295. _scale.x *= sx;
  296. dirty(DIRTY_SCALE);
  297. }
  298. void Transform::scaleY(float sy)
  299. {
  300. _scale.y *= sy;
  301. dirty(DIRTY_SCALE);
  302. }
  303. void Transform::scaleZ(float sz)
  304. {
  305. _scale.z *= sz;
  306. dirty(DIRTY_SCALE);
  307. }
  308. void Transform::set(const Vector3& scale, const Quaternion& rotation, const Vector3& translation)
  309. {
  310. _scale.set(scale);
  311. _rotation.set(rotation);
  312. _translation.set(translation);
  313. dirty(DIRTY_TRANSLATION | DIRTY_ROTATION | DIRTY_SCALE);
  314. }
  315. void Transform::set(const Vector3& scale, const Matrix& rotation, const Vector3& translation)
  316. {
  317. _scale.set(scale);
  318. Quaternion rotationQuat;
  319. Quaternion::createFromRotationMatrix(rotation, &rotationQuat);
  320. _rotation.set(rotationQuat);
  321. _translation.set(translation);
  322. dirty(DIRTY_TRANSLATION | DIRTY_ROTATION | DIRTY_SCALE);
  323. }
  324. void Transform::set(const Vector3& scale, const Vector3& axis, float angle, const Vector3& translation)
  325. {
  326. _scale.set(scale);
  327. _rotation.set(axis, angle);
  328. _translation.set(translation);
  329. dirty(DIRTY_TRANSLATION | DIRTY_ROTATION | DIRTY_SCALE);
  330. }
  331. void Transform::set(const Transform& transform)
  332. {
  333. _scale.set(transform._scale);
  334. _rotation.set(transform._rotation);
  335. _translation.set(transform._translation);
  336. dirty(DIRTY_TRANSLATION | DIRTY_ROTATION | DIRTY_SCALE);
  337. }
  338. void Transform::setIdentity()
  339. {
  340. _scale.set(1.0f, 1.0f, 1.0f);
  341. _rotation.setIdentity();
  342. _translation.set(0.0f, 0.0f, 0.0f);
  343. dirty(DIRTY_TRANSLATION | DIRTY_ROTATION | DIRTY_SCALE);
  344. }
  345. void Transform::setScale(float scale)
  346. {
  347. _scale.set(scale, scale, scale);
  348. dirty(DIRTY_SCALE);
  349. }
  350. void Transform::setScale(float sx, float sy, float sz)
  351. {
  352. _scale.set(sx, sy, sz);
  353. dirty(DIRTY_SCALE);
  354. }
  355. void Transform::setScale(const Vector3& scale)
  356. {
  357. _scale.set(scale);
  358. dirty(DIRTY_SCALE);
  359. }
  360. void Transform::setScaleX(float sx)
  361. {
  362. _scale.x = sx;
  363. dirty(DIRTY_SCALE);
  364. }
  365. void Transform::setScaleY(float sy)
  366. {
  367. _scale.y = sy;
  368. dirty(DIRTY_SCALE);
  369. }
  370. void Transform::setScaleZ(float sz)
  371. {
  372. _scale.z = sz;
  373. dirty(DIRTY_SCALE);
  374. }
  375. void Transform::setRotation(const Quaternion& rotation)
  376. {
  377. _rotation.set(rotation);
  378. dirty(DIRTY_ROTATION);
  379. }
  380. void Transform::setRotation(float qx, float qy, float qz, float qw)
  381. {
  382. _rotation.set(qx, qy, qz, qw);
  383. dirty(DIRTY_ROTATION);
  384. }
  385. void Transform::setRotation(const Matrix& rotation)
  386. {
  387. Quaternion rotationQuat;
  388. Quaternion::createFromRotationMatrix(rotation, &rotationQuat);
  389. _rotation.set(rotationQuat);
  390. dirty(DIRTY_ROTATION);
  391. }
  392. void Transform::setRotation(const Vector3& axis, float angle)
  393. {
  394. _rotation.set(axis, angle);
  395. dirty(DIRTY_ROTATION);
  396. }
  397. void Transform::setTranslation(const Vector3& translation)
  398. {
  399. _translation.set(translation);
  400. dirty(DIRTY_TRANSLATION);
  401. }
  402. void Transform::setTranslation(float tx, float ty, float tz)
  403. {
  404. _translation.set(tx, ty, tz);
  405. dirty(DIRTY_TRANSLATION);
  406. }
  407. void Transform::setTranslationX(float tx)
  408. {
  409. _translation.x = tx;
  410. dirty(DIRTY_TRANSLATION);
  411. }
  412. void Transform::setTranslationY(float ty)
  413. {
  414. _translation.y = ty;
  415. dirty(DIRTY_TRANSLATION);
  416. }
  417. void Transform::setTranslationZ(float tz)
  418. {
  419. _translation.z = tz;
  420. dirty(DIRTY_TRANSLATION);
  421. }
  422. void Transform::translate(float tx, float ty, float tz)
  423. {
  424. _translation.x += tx;
  425. _translation.y += ty;
  426. _translation.z += tz;
  427. dirty(DIRTY_TRANSLATION);
  428. }
  429. void Transform::translate(const Vector3& translation)
  430. {
  431. _translation.x += translation.x;
  432. _translation.y += translation.y;
  433. _translation.z += translation.z;
  434. dirty(DIRTY_TRANSLATION);
  435. }
  436. void Transform::translateX(float tx)
  437. {
  438. _translation.x += tx;
  439. dirty(DIRTY_TRANSLATION);
  440. }
  441. void Transform::translateY(float ty)
  442. {
  443. _translation.y += ty;
  444. dirty(DIRTY_TRANSLATION);
  445. }
  446. void Transform::translateZ(float tz)
  447. {
  448. _translation.z += tz;
  449. dirty(DIRTY_TRANSLATION);
  450. }
  451. void Transform::translateLeft(float amount)
  452. {
  453. // Force the current transform matrix to be updated.
  454. getMatrix();
  455. Vector3 left;
  456. _matrix.getLeftVector(&left);
  457. left.normalize();
  458. left.scale(amount);
  459. translate(left);
  460. }
  461. void Transform::translateUp(float amount)
  462. {
  463. // Force the current transform matrix to be updated.
  464. getMatrix();
  465. Vector3 up;
  466. _matrix.getUpVector(&up);
  467. up.normalize();
  468. up.scale(amount);
  469. translate(up);
  470. }
  471. void Transform::translateForward(float amount)
  472. {
  473. // Force the current transform matrix to be updated.
  474. getMatrix();
  475. Vector3 forward;
  476. _matrix.getForwardVector(&forward);
  477. forward.normalize();
  478. forward.scale(amount);
  479. translate(forward);
  480. }
  481. void Transform::transformPoint(Vector3* point)
  482. {
  483. GP_ASSERT(point);
  484. getMatrix();
  485. _matrix.transformPoint(point);
  486. }
  487. void Transform::transformPoint(const Vector3& point, Vector3* dst)
  488. {
  489. GP_ASSERT(dst);
  490. getMatrix();
  491. _matrix.transformPoint(point, dst);
  492. }
  493. void Transform::transformVector(Vector3* normal)
  494. {
  495. GP_ASSERT(normal);
  496. getMatrix();
  497. _matrix.transformVector(normal);
  498. }
  499. void Transform::transformVector(const Vector3& normal, Vector3* dst)
  500. {
  501. getMatrix();
  502. _matrix.transformVector(normal, dst);
  503. }
  504. void Transform::transformVector(float x, float y, float z, float w, Vector3* dst)
  505. {
  506. getMatrix();
  507. _matrix.transformVector(x, y, z, w, dst);
  508. }
  509. unsigned int Transform::getAnimationPropertyComponentCount(int propertyId) const
  510. {
  511. switch (propertyId)
  512. {
  513. case ANIMATE_SCALE_UNIT:
  514. case ANIMATE_SCALE_X:
  515. case ANIMATE_SCALE_Y:
  516. case ANIMATE_SCALE_Z:
  517. case ANIMATE_TRANSLATE_X:
  518. case ANIMATE_TRANSLATE_Y:
  519. case ANIMATE_TRANSLATE_Z:
  520. return 1;
  521. case ANIMATE_SCALE:
  522. case ANIMATE_TRANSLATE:
  523. return 3;
  524. case ANIMATE_ROTATE:
  525. return 4;
  526. case ANIMATE_ROTATE_TRANSLATE:
  527. return 7;
  528. case ANIMATE_SCALE_ROTATE_TRANSLATE:
  529. return 10;
  530. default:
  531. return -1;
  532. }
  533. }
  534. void Transform::getAnimationPropertyValue(int propertyId, AnimationValue* value)
  535. {
  536. switch (propertyId)
  537. {
  538. case ANIMATE_SCALE_UNIT:
  539. value->setFloat(0, _scale.x);
  540. break;
  541. case ANIMATE_SCALE:
  542. value->setFloat(0, _scale.x);
  543. value->setFloat(1, _scale.y);
  544. value->setFloat(2, _scale.z);
  545. break;
  546. case ANIMATE_SCALE_X:
  547. value->setFloat(0, _scale.x);
  548. break;
  549. case ANIMATE_SCALE_Y:
  550. value->setFloat(0, _scale.y);
  551. break;
  552. case ANIMATE_SCALE_Z:
  553. value->setFloat(0, _scale.z);
  554. break;
  555. case ANIMATE_ROTATE:
  556. value->setFloat(0, _rotation.x);
  557. value->setFloat(1, _rotation.y);
  558. value->setFloat(2, _rotation.z);
  559. value->setFloat(3, _rotation.w);
  560. break;
  561. case ANIMATE_TRANSLATE:
  562. value->setFloat(0, _translation.x);
  563. value->setFloat(1, _translation.y);
  564. value->setFloat(2, _translation.z);
  565. break;
  566. case ANIMATE_TRANSLATE_X:
  567. value->setFloat(0, _translation.x);
  568. break;
  569. case ANIMATE_TRANSLATE_Y:
  570. value->setFloat(0, _translation.y);
  571. break;
  572. case ANIMATE_TRANSLATE_Z:
  573. value->setFloat(0, _translation.z);
  574. break;
  575. case ANIMATE_ROTATE_TRANSLATE:
  576. value->setFloat(0, _rotation.x);
  577. value->setFloat(1, _rotation.y);
  578. value->setFloat(2, _rotation.z);
  579. value->setFloat(3, _rotation.w);
  580. value->setFloat(4, _translation.x);
  581. value->setFloat(5, _translation.y);
  582. value->setFloat(6, _translation.z);
  583. break;
  584. case ANIMATE_SCALE_ROTATE_TRANSLATE:
  585. value->setFloat(0, _scale.x);
  586. value->setFloat(1, _scale.y);
  587. value->setFloat(2, _scale.z);
  588. value->setFloat(3, _rotation.x);
  589. value->setFloat(4, _rotation.y);
  590. value->setFloat(5, _rotation.z);
  591. value->setFloat(6, _rotation.w);
  592. value->setFloat(7, _translation.x);
  593. value->setFloat(8, _translation.y);
  594. value->setFloat(9, _translation.z);
  595. break;
  596. default:
  597. break;
  598. }
  599. }
  600. void Transform::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
  601. {
  602. GP_ASSERT(blendWeight >= 0.0f && blendWeight <= 1.0f);
  603. switch (propertyId)
  604. {
  605. case ANIMATE_SCALE_UNIT:
  606. {
  607. float scale = Curve::lerp(blendWeight, _scale.x, value->getFloat(0));
  608. setScale(scale);
  609. break;
  610. }
  611. case ANIMATE_SCALE:
  612. {
  613. setScale(Curve::lerp(blendWeight, _scale.x, value->getFloat(0)), Curve::lerp(blendWeight, _scale.y, value->getFloat(1)), Curve::lerp(blendWeight, _scale.z, value->getFloat(2)));
  614. break;
  615. }
  616. case ANIMATE_SCALE_X:
  617. {
  618. setScaleX(Curve::lerp(blendWeight, _scale.x, value->getFloat(0)));
  619. break;
  620. }
  621. case ANIMATE_SCALE_Y:
  622. {
  623. setScaleY(Curve::lerp(blendWeight, _scale.y, value->getFloat(0)));
  624. break;
  625. }
  626. case ANIMATE_SCALE_Z:
  627. {
  628. setScaleZ(Curve::lerp(blendWeight, _scale.z, value->getFloat(0)));
  629. break;
  630. }
  631. case ANIMATE_ROTATE:
  632. {
  633. applyAnimationValueRotation(value, 0, blendWeight);
  634. break;
  635. }
  636. case ANIMATE_TRANSLATE:
  637. {
  638. setTranslation(Curve::lerp(blendWeight, _translation.x, value->getFloat(0)), Curve::lerp(blendWeight, _translation.y, value->getFloat(1)), Curve::lerp(blendWeight, _translation.z, value->getFloat(2)));
  639. break;
  640. }
  641. case ANIMATE_TRANSLATE_X:
  642. {
  643. setTranslationX(Curve::lerp(blendWeight, _translation.x, value->getFloat(0)));
  644. break;
  645. }
  646. case ANIMATE_TRANSLATE_Y:
  647. {
  648. setTranslationY(Curve::lerp(blendWeight, _translation.y, value->getFloat(0)));
  649. break;
  650. }
  651. case ANIMATE_TRANSLATE_Z:
  652. {
  653. setTranslationZ(Curve::lerp(blendWeight, _translation.z, value->getFloat(0)));
  654. break;
  655. }
  656. case ANIMATE_ROTATE_TRANSLATE:
  657. {
  658. applyAnimationValueRotation(value, 0, blendWeight);
  659. setTranslation(Curve::lerp(blendWeight, _translation.x, value->getFloat(4)), Curve::lerp(blendWeight, _translation.y, value->getFloat(5)), Curve::lerp(blendWeight, _translation.z, value->getFloat(6)));
  660. break;
  661. }
  662. case ANIMATE_SCALE_ROTATE_TRANSLATE:
  663. {
  664. setScale(Curve::lerp(blendWeight, _scale.x, value->getFloat(0)), Curve::lerp(blendWeight, _scale.y, value->getFloat(1)), Curve::lerp(blendWeight, _scale.z, value->getFloat(2)));
  665. applyAnimationValueRotation(value, 3, blendWeight);
  666. setTranslation(Curve::lerp(blendWeight, _translation.x, value->getFloat(7)), Curve::lerp(blendWeight, _translation.y, value->getFloat(8)), Curve::lerp(blendWeight, _translation.z, value->getFloat(9)));
  667. break;
  668. }
  669. default:
  670. break;
  671. }
  672. }
  673. void Transform::dirty(char matrixDirtyBits)
  674. {
  675. _matrixDirtyBits |= matrixDirtyBits;
  676. if (isTransformChangedSuspended())
  677. {
  678. if (!isDirty(DIRTY_NOTIFY))
  679. {
  680. suspendTransformChange(this);
  681. }
  682. }
  683. else
  684. {
  685. transformChanged();
  686. }
  687. }
  688. bool Transform::isDirty(char matrixDirtyBits) const
  689. {
  690. return (_matrixDirtyBits & matrixDirtyBits) == matrixDirtyBits;
  691. }
  692. void Transform::suspendTransformChange(Transform* transform)
  693. {
  694. transform->_matrixDirtyBits |= DIRTY_NOTIFY;
  695. _transformsChanged.push_back(transform);
  696. }
  697. void Transform::addListener(Transform::Listener* listener, long cookie)
  698. {
  699. if (_listeners == NULL)
  700. _listeners = new std::list<TransformListener>();
  701. TransformListener l;
  702. l.listener = listener;
  703. l.cookie = cookie;
  704. _listeners->push_back(l);
  705. }
  706. void Transform::removeListener(Transform::Listener* listener)
  707. {
  708. if (_listeners)
  709. {
  710. for (std::list<TransformListener>::iterator itr = _listeners->begin(); itr != _listeners->end(); itr++)
  711. {
  712. if ((*itr).listener == listener)
  713. {
  714. _listeners->erase(itr);
  715. break;
  716. }
  717. }
  718. }
  719. }
  720. void Transform::transformChanged()
  721. {
  722. if (_listeners)
  723. {
  724. for (std::list<TransformListener>::iterator itr = _listeners->begin(); itr != _listeners->end(); itr++)
  725. {
  726. TransformListener& l = *itr;
  727. l.listener->transformChanged(this, l.cookie);
  728. }
  729. }
  730. }
  731. void Transform::cloneInto(Transform* transform, NodeCloneContext &context) const
  732. {
  733. AnimationTarget::cloneInto(transform, context);
  734. transform->_scale.set(_scale);
  735. transform->_rotation.set(_rotation);
  736. transform->_translation.set(_translation);
  737. }
  738. void Transform::applyAnimationValueRotation(AnimationValue* value, unsigned int index, float blendWeight)
  739. {
  740. Quaternion::slerp(_rotation.x, _rotation.y, _rotation.z, _rotation.w, value->getFloat(index), value->getFloat(index + 1), value->getFloat(index + 2), value->getFloat(index + 3), blendWeight,
  741. &_rotation.x, &_rotation.y, &_rotation.z, &_rotation.w);
  742. dirty(DIRTY_ROTATION);
  743. }
  744. }