Node.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. #include "Base.h"
  2. #include "Node.h"
  3. #include "Scene.h"
  4. #include "Joint.h"
  5. #include "PhysicsRigidBody.h"
  6. #include "PhysicsGhostObject.h"
  7. #include "PhysicsCharacter.h"
  8. #include "Game.h"
  9. // Node dirty flags
  10. #define NODE_DIRTY_WORLD 1
  11. #define NODE_DIRTY_BOUNDS 2
  12. #define NODE_DIRTY_ALL (NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS)
  13. // Node property flags
  14. #define NODE_FLAG_VISIBLE 1
  15. #define NODE_FLAG_TRANSPARENT 2
  16. #define NODE_FLAG_DYNAMIC 4
  17. namespace gameplay
  18. {
  19. Node::Node(const char* id)
  20. : _scene(NULL), _firstChild(NULL), _nextSibling(NULL), _prevSibling(NULL), _parent(NULL), _childCount(NULL),
  21. _nodeFlags(NODE_FLAG_VISIBLE), _camera(NULL), _light(NULL), _model(NULL), _form(NULL), _audioSource(NULL), _particleEmitter(NULL),
  22. _collisionObject(NULL), _dirtyBits(NODE_DIRTY_ALL), _notifyHierarchyChanged(true), _userData(NULL)
  23. {
  24. if (id)
  25. {
  26. _id = id;
  27. }
  28. }
  29. Node::~Node()
  30. {
  31. removeAllChildren();
  32. if (_model)
  33. _model->setNode(NULL);
  34. if (_audioSource)
  35. _audioSource->setNode(NULL);
  36. if (_particleEmitter)
  37. _particleEmitter->setNode(NULL);
  38. if (_form)
  39. _form->setNode(NULL);
  40. SAFE_RELEASE(_camera);
  41. SAFE_RELEASE(_light);
  42. SAFE_RELEASE(_model);
  43. SAFE_RELEASE(_audioSource);
  44. SAFE_RELEASE(_particleEmitter);
  45. SAFE_RELEASE(_form);
  46. SAFE_DELETE(_collisionObject);
  47. // Cleanup user data
  48. if (_userData)
  49. {
  50. // Call custom cleanup callback if specified
  51. if (_userData->cleanupCallback)
  52. _userData->cleanupCallback(_userData->pointer);
  53. SAFE_DELETE(_userData);
  54. }
  55. }
  56. Node* Node::create(const char* id)
  57. {
  58. return new Node(id);
  59. }
  60. const char* Node::getId() const
  61. {
  62. return _id.c_str();
  63. }
  64. void Node::setId(const char* id)
  65. {
  66. if (id)
  67. {
  68. _id = id;
  69. }
  70. }
  71. Node::Type Node::getType() const
  72. {
  73. return Node::NODE;
  74. }
  75. void Node::addChild(Node* child)
  76. {
  77. assert(child);
  78. if (child->_parent == this)
  79. {
  80. // This node is already present in our hierarchy
  81. return;
  82. }
  83. child->addRef();
  84. // If the item belongs to another hierarchy, remove it first.
  85. if (child->_parent)
  86. {
  87. child->_parent->removeChild(child);
  88. }
  89. else if (child->_scene)
  90. {
  91. child->_scene->removeNode(child);
  92. }
  93. // Order is irrelevant, so add to the beginning of the list.
  94. if (_firstChild)
  95. {
  96. _firstChild->_prevSibling = child;
  97. child->_nextSibling = _firstChild;
  98. _firstChild = child;
  99. }
  100. else
  101. {
  102. _firstChild = child;
  103. }
  104. child->_parent = this;
  105. ++_childCount;
  106. if (_notifyHierarchyChanged)
  107. {
  108. hierarchyChanged();
  109. }
  110. }
  111. void Node::removeChild(Node* child)
  112. {
  113. if (child == NULL || child->_parent != this)
  114. {
  115. // The child is not in our hierarchy.
  116. return;
  117. }
  118. // Call remove on the child.
  119. child->remove();
  120. SAFE_RELEASE(child);
  121. }
  122. void Node::removeAllChildren()
  123. {
  124. _notifyHierarchyChanged = false;
  125. while (_firstChild)
  126. {
  127. removeChild(_firstChild);
  128. }
  129. _notifyHierarchyChanged = true;
  130. hierarchyChanged();
  131. }
  132. void Node::remove()
  133. {
  134. // Re-link our neighbours.
  135. if (_prevSibling)
  136. {
  137. _prevSibling->_nextSibling = _nextSibling;
  138. }
  139. if (_nextSibling)
  140. {
  141. _nextSibling->_prevSibling = _prevSibling;
  142. }
  143. // Update our parent.
  144. Node* parent = _parent;
  145. if (parent)
  146. {
  147. if (this == parent->_firstChild)
  148. {
  149. parent->_firstChild = _nextSibling;
  150. }
  151. --parent->_childCount;
  152. }
  153. _nextSibling = NULL;
  154. _prevSibling = NULL;
  155. _parent = NULL;
  156. if (parent && parent->_notifyHierarchyChanged)
  157. {
  158. parent->hierarchyChanged();
  159. }
  160. }
  161. Node* Node::getFirstChild() const
  162. {
  163. return _firstChild;
  164. }
  165. Node* Node::getNextSibling() const
  166. {
  167. return _nextSibling;
  168. }
  169. Node* Node::getPreviousSibling() const
  170. {
  171. return _prevSibling;
  172. }
  173. Node* Node::getParent() const
  174. {
  175. return _parent;
  176. }
  177. bool Node::isVisible() const
  178. {
  179. return ((_nodeFlags & NODE_FLAG_VISIBLE) == NODE_FLAG_VISIBLE);
  180. }
  181. void Node::setVisible(bool visible)
  182. {
  183. if (visible)
  184. _nodeFlags |= NODE_FLAG_VISIBLE;
  185. else
  186. _nodeFlags &= ~NODE_FLAG_VISIBLE;
  187. }
  188. bool Node::isTransparent() const
  189. {
  190. return ((_nodeFlags & NODE_FLAG_TRANSPARENT) == NODE_FLAG_TRANSPARENT);
  191. }
  192. void Node::setTransparent(bool transparent)
  193. {
  194. if (transparent)
  195. _nodeFlags |= NODE_FLAG_TRANSPARENT;
  196. else
  197. _nodeFlags &= ~NODE_FLAG_TRANSPARENT;
  198. }
  199. bool Node::isDynamic() const
  200. {
  201. return ((_nodeFlags & NODE_FLAG_DYNAMIC) == NODE_FLAG_DYNAMIC);
  202. }
  203. void Node::setDynamic(bool dynamic)
  204. {
  205. if (dynamic)
  206. _nodeFlags |= NODE_FLAG_DYNAMIC;
  207. else
  208. _nodeFlags &= ~NODE_FLAG_DYNAMIC;
  209. }
  210. void* Node::getUserPointer() const
  211. {
  212. return (_userData ? _userData->pointer : NULL);
  213. }
  214. void Node::setUserPointer(void* pointer, void (*cleanupCallback)(void*))
  215. {
  216. // If existing user pointer is being changed, call cleanup function to free previous pointer
  217. if (_userData && _userData->pointer && _userData->cleanupCallback && pointer != _userData->pointer)
  218. {
  219. _userData->cleanupCallback(_userData->pointer);
  220. }
  221. if (pointer)
  222. {
  223. // Assign user pointer
  224. if (_userData == NULL)
  225. _userData = new UserData();
  226. _userData->pointer = pointer;
  227. _userData->cleanupCallback = cleanupCallback;
  228. }
  229. else
  230. {
  231. // Clear user pointer
  232. SAFE_DELETE(_userData);
  233. }
  234. }
  235. unsigned int Node::getChildCount() const
  236. {
  237. return _childCount;
  238. }
  239. Node* Node::findNode(const char* id, bool recursive, bool exactMatch) const
  240. {
  241. assert(id);
  242. // If the node has a model with a mesh skin, search the skin's hierarchy as well.
  243. Node* rootNode = NULL;
  244. if (_model != NULL && _model->getSkin() != NULL && (rootNode = _model->getSkin()->_rootNode) != NULL)
  245. {
  246. if ((exactMatch && rootNode->_id == id) || (!exactMatch && rootNode->_id.find(id) == 0))
  247. return rootNode;
  248. Node* match = rootNode->findNode(id, true, exactMatch);
  249. if (match)
  250. {
  251. return match;
  252. }
  253. }
  254. // Search immediate children first.
  255. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  256. {
  257. // Does this child's ID match?
  258. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  259. {
  260. return child;
  261. }
  262. }
  263. // Recurse.
  264. if (recursive)
  265. {
  266. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  267. {
  268. Node* match = child->findNode(id, true, exactMatch);
  269. if (match)
  270. {
  271. return match;
  272. }
  273. }
  274. }
  275. return NULL;
  276. }
  277. unsigned int Node::findNodes(const char* id, std::vector<Node*>& nodes, bool recursive, bool exactMatch) const
  278. {
  279. assert(id);
  280. unsigned int count = 0;
  281. // Search immediate children first.
  282. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  283. {
  284. // Does this child's ID match?
  285. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  286. {
  287. nodes.push_back(child);
  288. ++count;
  289. }
  290. }
  291. // Recurse.
  292. if (recursive)
  293. {
  294. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  295. {
  296. count += child->findNodes(id, nodes, true, exactMatch);
  297. }
  298. }
  299. return count;
  300. }
  301. Scene* Node::getScene() const
  302. {
  303. // Search for a scene in our parents.
  304. for (Node* n = const_cast<Node*>(this); n != NULL; n = n->getParent())
  305. {
  306. if (n->_scene)
  307. {
  308. return n->_scene;
  309. }
  310. }
  311. return NULL;
  312. }
  313. Node* Node::getRootNode() const
  314. {
  315. Node* n = const_cast<Node*>(this);
  316. while (n->getParent())
  317. {
  318. n = n->getParent();
  319. }
  320. return n;
  321. }
  322. const Matrix& Node::getWorldMatrix() const
  323. {
  324. if (_dirtyBits & NODE_DIRTY_WORLD)
  325. {
  326. // Clear our dirty flag immediately to prevent this block from being entered if our
  327. // parent calls our getWorldMatrix() method as a result of the following calculations.
  328. _dirtyBits &= ~NODE_DIRTY_WORLD;
  329. // If we have a parent, multiply our parent world transform by our local
  330. // transform to obtain our final resolved world transform.
  331. Node* parent = getParent();
  332. if (parent && (!_collisionObject || _collisionObject->isKinematic()))
  333. {
  334. Matrix::multiply(parent->getWorldMatrix(), getMatrix(), &_world);
  335. }
  336. else
  337. {
  338. _world = getMatrix();
  339. }
  340. // Our world matrix was just updated, so call getWorldMatrix() on all child nodes
  341. // to force their resolved world matrices to be updated.
  342. Node* node = getFirstChild();
  343. while (node)
  344. {
  345. node->getWorldMatrix();
  346. node = node->getNextSibling();
  347. }
  348. }
  349. return _world;
  350. }
  351. const Matrix& Node::getWorldViewMatrix() const
  352. {
  353. static Matrix worldView;
  354. Matrix::multiply(getViewMatrix(), getWorldMatrix(), &worldView);
  355. return worldView;
  356. }
  357. const Matrix& Node::getInverseTransposeWorldViewMatrix() const
  358. {
  359. static Matrix invTransWorldView;
  360. Matrix::multiply(getViewMatrix(), getWorldMatrix(), &invTransWorldView);
  361. invTransWorldView.invert();
  362. invTransWorldView.transpose();
  363. return invTransWorldView;
  364. }
  365. const Matrix& Node::getInverseTransposeWorldMatrix() const
  366. {
  367. static Matrix invTransWorld;
  368. invTransWorld = getWorldMatrix();
  369. invTransWorld.invert();
  370. invTransWorld.transpose();
  371. return invTransWorld;
  372. }
  373. const Matrix& Node::getViewMatrix() const
  374. {
  375. Scene* scene = getScene();
  376. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  377. if (camera)
  378. {
  379. return camera->getViewMatrix();
  380. }
  381. else
  382. {
  383. return Matrix::identity();
  384. }
  385. }
  386. const Matrix& Node::getInverseViewMatrix() const
  387. {
  388. Scene* scene = getScene();
  389. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  390. if (camera)
  391. {
  392. return camera->getInverseViewMatrix();
  393. }
  394. else
  395. {
  396. return Matrix::identity();
  397. }
  398. }
  399. const Matrix& Node::getProjectionMatrix() const
  400. {
  401. Scene* scene = getScene();
  402. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  403. if (camera)
  404. {
  405. return camera->getProjectionMatrix();
  406. }
  407. else
  408. {
  409. return Matrix::identity();
  410. }
  411. }
  412. const Matrix& Node::getViewProjectionMatrix() const
  413. {
  414. Scene* scene = getScene();
  415. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  416. if (camera)
  417. {
  418. return camera->getViewProjectionMatrix();
  419. }
  420. else
  421. {
  422. return Matrix::identity();
  423. }
  424. }
  425. const Matrix& Node::getInverseViewProjectionMatrix() const
  426. {
  427. Scene* scene = getScene();
  428. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  429. if (camera)
  430. {
  431. return camera->getInverseViewProjectionMatrix();
  432. }
  433. return Matrix::identity();
  434. }
  435. const Matrix& Node::getWorldViewProjectionMatrix() const
  436. {
  437. static Matrix worldViewProj;
  438. // Always re-calculate worldViewProjection matrix since it's extremely difficult
  439. // to track whether the camera has changed (it may frequently change every frame).
  440. Matrix::multiply(getViewProjectionMatrix(), getWorldMatrix(), &worldViewProj);
  441. return worldViewProj;
  442. }
  443. Vector3 Node::getTranslationWorld() const
  444. {
  445. Vector3 translation;
  446. getWorldMatrix().getTranslation(&translation);
  447. return translation;
  448. }
  449. Vector3 Node::getTranslationView() const
  450. {
  451. Vector3 translation;
  452. getWorldMatrix().getTranslation(&translation);
  453. getViewMatrix().transformPoint(&translation);
  454. return translation;
  455. }
  456. Vector3 Node::getForwardVectorWorld() const
  457. {
  458. Vector3 vector;
  459. getWorldMatrix().getForwardVector(&vector);
  460. return vector;
  461. }
  462. Vector3 Node::getForwardVectorView() const
  463. {
  464. Vector3 vector;
  465. getWorldMatrix().getForwardVector(&vector);
  466. getViewMatrix().transformVector(&vector);
  467. //getForwardVector(&vector);
  468. //getWorldViewMatrix().transformVector(&vector);
  469. return vector;
  470. }
  471. Vector3 Node::getActiveCameraTranslationWorld() const
  472. {
  473. Scene* scene = getScene();
  474. if (scene)
  475. {
  476. Camera* camera = scene->getActiveCamera();
  477. if (camera)
  478. {
  479. Node* cameraNode = camera->getNode();
  480. if (cameraNode)
  481. {
  482. return cameraNode->getTranslationWorld();
  483. }
  484. }
  485. }
  486. return Vector3::zero();
  487. }
  488. Vector3 Node::getActiveCameraTranslationView() const
  489. {
  490. Scene* scene = getScene();
  491. if (scene)
  492. {
  493. Camera* camera = scene->getActiveCamera();
  494. if (camera)
  495. {
  496. Node* cameraNode = camera->getNode();
  497. if (cameraNode)
  498. {
  499. return cameraNode->getTranslationView();
  500. }
  501. }
  502. }
  503. return Vector3::zero();
  504. }
  505. void Node::hierarchyChanged()
  506. {
  507. // When our hierarchy changes our world transform is affected, so we must dirty it.
  508. transformChanged();
  509. }
  510. void Node::transformChanged()
  511. {
  512. // Our local transform was changed, so mark our world matrices dirty.
  513. _dirtyBits |= NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS;
  514. // Notify our children that their transform has also changed (since transforms are inherited).
  515. Node* n = getFirstChild();
  516. while (n)
  517. {
  518. if (Transform::isTransformChangedSuspended())
  519. {
  520. // If the DIRTY_NOTIFY bit is not set
  521. if (!n->isDirty(Transform::DIRTY_NOTIFY))
  522. {
  523. n->transformChanged();
  524. suspendTransformChange(n);
  525. }
  526. }
  527. else
  528. {
  529. n->transformChanged();
  530. }
  531. n = n->getNextSibling();
  532. }
  533. Transform::transformChanged();
  534. }
  535. void Node::setBoundsDirty()
  536. {
  537. // Mark ourself and our parent nodes as dirty
  538. _dirtyBits |= NODE_DIRTY_BOUNDS;
  539. // Mark our parent bounds as dirty as well
  540. if (_parent)
  541. _parent->setBoundsDirty();
  542. }
  543. Animation* Node::getAnimation(const char* id) const
  544. {
  545. Animation* animation = ((AnimationTarget*)this)->getAnimation(id);
  546. if (animation)
  547. return animation;
  548. // See if this node has a model, then drill down.
  549. Model* model = this->getModel();
  550. if (model)
  551. {
  552. // Check to see if there's any animations with the ID on the joints.
  553. MeshSkin* skin = model->getSkin();
  554. if (skin)
  555. {
  556. Joint* rootJoint = skin->getRootJoint();
  557. if (rootJoint)
  558. {
  559. animation = rootJoint->getAnimation(id);
  560. if (animation)
  561. return animation;
  562. }
  563. }
  564. // Check to see if any of the model's material parameter's has an animation
  565. // with the given ID.
  566. Material* material = model->getMaterial();
  567. if (material)
  568. {
  569. // How to access material parameters? hidden on the Material::RenderState.
  570. std::vector<MaterialParameter*>::iterator itr = material->_parameters.begin();
  571. for (; itr != material->_parameters.end(); itr++)
  572. {
  573. animation = ((MaterialParameter*)(*itr))->getAnimation(id);
  574. if (animation)
  575. return animation;
  576. }
  577. }
  578. }
  579. // look through form for animations.
  580. Form* form = this->getForm();
  581. if (form)
  582. {
  583. animation = form->getAnimation(id);
  584. if (animation)
  585. return animation;
  586. }
  587. // Look through this node's children for an animation with the specified ID.
  588. unsigned int childCount = this->getChildCount();
  589. Node* child = this->getFirstChild();
  590. for (unsigned int i = 0; i < childCount; i++)
  591. {
  592. animation = child->getAnimation(id);
  593. if (animation)
  594. return animation;
  595. child = child->getNextSibling();
  596. }
  597. return NULL;
  598. }
  599. Camera* Node::getCamera() const
  600. {
  601. return _camera;
  602. }
  603. void Node::setCamera(Camera* camera)
  604. {
  605. if (_camera != camera)
  606. {
  607. if (_camera)
  608. {
  609. _camera->setNode(NULL);
  610. SAFE_RELEASE(_camera);
  611. }
  612. _camera = camera;
  613. if (_camera)
  614. {
  615. _camera->addRef();
  616. _camera->setNode(this);
  617. }
  618. }
  619. }
  620. Light* Node::getLight() const
  621. {
  622. return _light;
  623. }
  624. void Node::setLight(Light* light)
  625. {
  626. if (_light != light)
  627. {
  628. if (_light)
  629. {
  630. _light->setNode(NULL);
  631. SAFE_RELEASE(_light);
  632. }
  633. _light = light;
  634. if (_light)
  635. {
  636. _light->addRef();
  637. _light->setNode(this);
  638. }
  639. }
  640. }
  641. void Node::setModel(Model* model)
  642. {
  643. if (_model != model)
  644. {
  645. if (_model)
  646. {
  647. _model->setNode(NULL);
  648. SAFE_RELEASE(_model);
  649. }
  650. _model = model;
  651. if (_model)
  652. {
  653. _model->addRef();
  654. _model->setNode(this);
  655. }
  656. }
  657. }
  658. Model* Node::getModel() const
  659. {
  660. return _model;
  661. }
  662. void Node::setForm(Form* form)
  663. {
  664. if (_form != form)
  665. {
  666. if (_form)
  667. {
  668. _form->setNode(NULL);
  669. SAFE_RELEASE(_form);
  670. }
  671. _form = form;
  672. if (_form)
  673. {
  674. _form->addRef();
  675. _form->setNode(this);
  676. }
  677. }
  678. }
  679. Form* Node::getForm() const
  680. {
  681. return _form;
  682. }
  683. const BoundingSphere& Node::getBoundingSphere() const
  684. {
  685. if (_dirtyBits & NODE_DIRTY_BOUNDS)
  686. {
  687. _dirtyBits &= ~NODE_DIRTY_BOUNDS;
  688. const Matrix& worldMatrix = getWorldMatrix();
  689. // Start with our local bounding sphere
  690. // TODO: Incorporate bounds from entities other than mesh (i.e. emitters, audiosource, etc)
  691. bool empty = true;
  692. if (_model && _model->getMesh())
  693. {
  694. _bounds.set(_model->getMesh()->getBoundingSphere());
  695. empty = false;
  696. }
  697. else
  698. {
  699. // Empty bounding sphere, set the world translation with zero radius
  700. worldMatrix.getTranslation(&_bounds.center);
  701. _bounds.radius = 0;
  702. }
  703. // Transform the sphere (if not empty) into world space.
  704. if (!empty)
  705. {
  706. bool applyWorldTransform = true;
  707. if (_model && _model->getSkin())
  708. {
  709. // Special case: If the root joint of our mesh skin is parented by any nodes,
  710. // multiply the world matrix of the root joint's parent by this node's
  711. // world matrix. This computes a final world matrix used for transforming this
  712. // node's bounding volume. This allows us to store a much smaller bounding
  713. // volume approximation than would otherwise be possible for skinned meshes,
  714. // since joint parent nodes that are not in the matrix pallette do not need to
  715. // be considered as directly transforming vertices on the GPU (they can instead
  716. // be applied directly to the bounding volume transformation below).
  717. Node* jointParent = _model->getSkin()->getRootJoint()->getParent();
  718. if (jointParent)
  719. {
  720. // TODO: Should we protect against the case where joints are nested directly
  721. // in the node hierachy of the model (this is normally not the case)?
  722. Matrix boundsMatrix;
  723. Matrix::multiply(getWorldMatrix(), jointParent->getWorldMatrix(), &boundsMatrix);
  724. _bounds.transform(boundsMatrix);
  725. applyWorldTransform = false;
  726. }
  727. }
  728. if (applyWorldTransform)
  729. {
  730. _bounds.transform(getWorldMatrix());
  731. }
  732. }
  733. // Merge this world-space bounding sphere with our childrens' bounding volumes.
  734. for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
  735. {
  736. const BoundingSphere& childSphere = n->getBoundingSphere();
  737. if (!childSphere.isEmpty())
  738. {
  739. if (empty)
  740. {
  741. _bounds.set(childSphere);
  742. empty = false;
  743. }
  744. else
  745. {
  746. _bounds.merge(childSphere);
  747. }
  748. }
  749. }
  750. }
  751. return _bounds;
  752. }
  753. Node* Node::clone() const
  754. {
  755. NodeCloneContext context;
  756. return cloneRecursive(context);
  757. }
  758. Node* Node::cloneSingleNode(NodeCloneContext &context) const
  759. {
  760. Node* copy = Node::create(getId());
  761. context.registerClonedNode(this, copy);
  762. cloneInto(copy, context);
  763. return copy;
  764. }
  765. Node* Node::cloneRecursive(NodeCloneContext &context) const
  766. {
  767. Node* copy = cloneSingleNode(context);
  768. Node* lastChild = NULL;
  769. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  770. {
  771. lastChild = child;
  772. }
  773. // Loop through the nodes backwards because addChild adds the node to the front.
  774. for (Node* child = lastChild; child != NULL; child = child->getPreviousSibling())
  775. {
  776. Node* childCopy = child->cloneRecursive(context);
  777. copy->addChild(childCopy);
  778. childCopy->release();
  779. }
  780. return copy;
  781. }
  782. void Node::cloneInto(Node* node, NodeCloneContext &context) const
  783. {
  784. Transform::cloneInto(node, context);
  785. // TODO: Clone the rest of the node data.
  786. if (Camera* camera = getCamera())
  787. {
  788. Camera* cameraClone = camera->clone(context);
  789. node->setCamera(cameraClone);
  790. cameraClone->release();
  791. }
  792. if (Light* light = getLight())
  793. {
  794. Light* lightClone = light->clone(context);
  795. node->setLight(lightClone);
  796. lightClone->release();
  797. }
  798. if (AudioSource* audio = getAudioSource())
  799. {
  800. AudioSource* audioClone = audio->clone(context);
  801. node->setAudioSource(audioClone);
  802. audioClone->release();
  803. }
  804. if (Model* model = getModel())
  805. {
  806. Model* modelClone = model->clone(context);
  807. node->setModel(modelClone);
  808. modelClone->release();
  809. }
  810. node->_world = _world;
  811. node->_bounds = _bounds;
  812. }
  813. AudioSource* Node::getAudioSource() const
  814. {
  815. return _audioSource;
  816. }
  817. void Node::setAudioSource(AudioSource* audio)
  818. {
  819. if (_audioSource != audio)
  820. {
  821. if (_audioSource)
  822. {
  823. _audioSource->setNode(NULL);
  824. SAFE_RELEASE(_audioSource);
  825. }
  826. _audioSource = audio;
  827. if (_audioSource)
  828. {
  829. _audioSource->addRef();
  830. _audioSource->setNode(this);
  831. }
  832. }
  833. }
  834. ParticleEmitter* Node::getParticleEmitter() const
  835. {
  836. return _particleEmitter;
  837. }
  838. void Node::setParticleEmitter(ParticleEmitter* emitter)
  839. {
  840. if (_particleEmitter != emitter)
  841. {
  842. if (_particleEmitter)
  843. {
  844. _particleEmitter->setNode(NULL);
  845. SAFE_RELEASE(_particleEmitter);
  846. }
  847. _particleEmitter = emitter;
  848. if (_particleEmitter)
  849. {
  850. _particleEmitter->addRef();
  851. _particleEmitter->setNode(this);
  852. }
  853. }
  854. }
  855. PhysicsCollisionObject* Node::getCollisionObject() const
  856. {
  857. return _collisionObject;
  858. }
  859. PhysicsCollisionObject* Node::setCollisionObject(PhysicsCollisionObject::Type type, const PhysicsCollisionShape::Definition& shape, PhysicsRigidBody::Parameters* rigidBodyParameters)
  860. {
  861. SAFE_DELETE(_collisionObject);
  862. switch (type)
  863. {
  864. case PhysicsCollisionObject::RIGID_BODY:
  865. {
  866. _collisionObject = new PhysicsRigidBody(this, shape, rigidBodyParameters ? *rigidBodyParameters : PhysicsRigidBody::Parameters());
  867. }
  868. break;
  869. case PhysicsCollisionObject::GHOST_OBJECT:
  870. {
  871. _collisionObject = new PhysicsGhostObject(this, shape);
  872. }
  873. break;
  874. case PhysicsCollisionObject::CHARACTER:
  875. {
  876. _collisionObject = new PhysicsCharacter(this, shape, rigidBodyParameters ? rigidBodyParameters->mass : 1.0f);
  877. }
  878. break;
  879. }
  880. return _collisionObject;
  881. }
  882. PhysicsCollisionObject* Node::setCollisionObject(const char* url)
  883. {
  884. // Load the collision object properties from file.
  885. Properties* properties = Properties::create(url);
  886. assert(properties);
  887. if (properties == NULL)
  888. {
  889. WARN_VARG("Failed to load collision object file: %s", url);
  890. return NULL;
  891. }
  892. PhysicsCollisionObject* collisionObject = setCollisionObject((strlen(properties->getNamespace()) > 0) ? properties : properties->getNextNamespace());
  893. SAFE_DELETE(properties);
  894. return collisionObject;
  895. }
  896. PhysicsCollisionObject* Node::setCollisionObject(Properties* properties)
  897. {
  898. SAFE_DELETE(_collisionObject);
  899. // Check if the properties is valid.
  900. if (!properties ||
  901. !(strcmp(properties->getNamespace(), "character") == 0 ||
  902. strcmp(properties->getNamespace(), "ghostObject") == 0 ||
  903. strcmp(properties->getNamespace(), "rigidBody") == 0))
  904. {
  905. WARN("Failed to load collision object from properties object: must be non-null object and have namespace equal to \'character\', \'ghostObject\', or \'rigidBody\'.");
  906. return NULL;
  907. }
  908. if (strcmp(properties->getNamespace(), "character") == 0)
  909. {
  910. _collisionObject = PhysicsCharacter::create(this, properties);
  911. }
  912. else if (strcmp(properties->getNamespace(), "ghostObject") == 0)
  913. {
  914. _collisionObject = PhysicsGhostObject::create(this, properties);
  915. }
  916. else if (strcmp(properties->getNamespace(), "rigidBody") == 0)
  917. {
  918. _collisionObject = PhysicsRigidBody::create(this, properties);
  919. }
  920. return _collisionObject;
  921. }
  922. NodeCloneContext::NodeCloneContext()
  923. {
  924. }
  925. NodeCloneContext::~NodeCloneContext()
  926. {
  927. }
  928. Animation* NodeCloneContext::findClonedAnimation(const Animation* animation)
  929. {
  930. AnimationMap::iterator it = _clonedAnimations.find(animation);
  931. return it != _clonedAnimations.end() ? it->second : NULL;
  932. }
  933. void NodeCloneContext::registerClonedAnimation(const Animation* original, Animation* clone)
  934. {
  935. _clonedAnimations[original] = clone;
  936. }
  937. Node* NodeCloneContext::findClonedNode(const Node* node)
  938. {
  939. NodeMap::iterator it = _clonedNodes.find(node);
  940. return it != _clonedNodes.end() ? it->second : NULL;
  941. }
  942. void NodeCloneContext::registerClonedNode(const Node* original, Node* clone)
  943. {
  944. _clonedNodes[original] = clone;
  945. }
  946. }