Transform.cpp 24 KB

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