Transform.cpp 22 KB

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