Transform.cpp 23 KB

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