Transform.cpp 22 KB

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