Node.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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. GP_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. GP_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. GP_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. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  343. {
  344. child->getWorldMatrix();
  345. }
  346. }
  347. return _world;
  348. }
  349. const Matrix& Node::getWorldViewMatrix() const
  350. {
  351. static Matrix worldView;
  352. Matrix::multiply(getViewMatrix(), getWorldMatrix(), &worldView);
  353. return worldView;
  354. }
  355. const Matrix& Node::getInverseTransposeWorldViewMatrix() const
  356. {
  357. static Matrix invTransWorldView;
  358. Matrix::multiply(getViewMatrix(), getWorldMatrix(), &invTransWorldView);
  359. invTransWorldView.invert();
  360. invTransWorldView.transpose();
  361. return invTransWorldView;
  362. }
  363. const Matrix& Node::getInverseTransposeWorldMatrix() const
  364. {
  365. static Matrix invTransWorld;
  366. invTransWorld = getWorldMatrix();
  367. invTransWorld.invert();
  368. invTransWorld.transpose();
  369. return invTransWorld;
  370. }
  371. const Matrix& Node::getViewMatrix() const
  372. {
  373. Scene* scene = getScene();
  374. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  375. if (camera)
  376. {
  377. return camera->getViewMatrix();
  378. }
  379. else
  380. {
  381. return Matrix::identity();
  382. }
  383. }
  384. const Matrix& Node::getInverseViewMatrix() const
  385. {
  386. Scene* scene = getScene();
  387. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  388. if (camera)
  389. {
  390. return camera->getInverseViewMatrix();
  391. }
  392. else
  393. {
  394. return Matrix::identity();
  395. }
  396. }
  397. const Matrix& Node::getProjectionMatrix() const
  398. {
  399. Scene* scene = getScene();
  400. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  401. if (camera)
  402. {
  403. return camera->getProjectionMatrix();
  404. }
  405. else
  406. {
  407. return Matrix::identity();
  408. }
  409. }
  410. const Matrix& Node::getViewProjectionMatrix() const
  411. {
  412. Scene* scene = getScene();
  413. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  414. if (camera)
  415. {
  416. return camera->getViewProjectionMatrix();
  417. }
  418. else
  419. {
  420. return Matrix::identity();
  421. }
  422. }
  423. const Matrix& Node::getInverseViewProjectionMatrix() const
  424. {
  425. Scene* scene = getScene();
  426. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  427. if (camera)
  428. {
  429. return camera->getInverseViewProjectionMatrix();
  430. }
  431. return Matrix::identity();
  432. }
  433. const Matrix& Node::getWorldViewProjectionMatrix() const
  434. {
  435. static Matrix worldViewProj;
  436. // Always re-calculate worldViewProjection matrix since it's extremely difficult
  437. // to track whether the camera has changed (it may frequently change every frame).
  438. Matrix::multiply(getViewProjectionMatrix(), getWorldMatrix(), &worldViewProj);
  439. return worldViewProj;
  440. }
  441. Vector3 Node::getTranslationWorld() const
  442. {
  443. Vector3 translation;
  444. getWorldMatrix().getTranslation(&translation);
  445. return translation;
  446. }
  447. Vector3 Node::getTranslationView() const
  448. {
  449. Vector3 translation;
  450. getWorldMatrix().getTranslation(&translation);
  451. getViewMatrix().transformPoint(&translation);
  452. return translation;
  453. }
  454. Vector3 Node::getForwardVectorWorld() const
  455. {
  456. Vector3 vector;
  457. getWorldMatrix().getForwardVector(&vector);
  458. return vector;
  459. }
  460. Vector3 Node::getForwardVectorView() const
  461. {
  462. Vector3 vector;
  463. getWorldMatrix().getForwardVector(&vector);
  464. getViewMatrix().transformVector(&vector);
  465. return vector;
  466. }
  467. Vector3 Node::getActiveCameraTranslationWorld() const
  468. {
  469. Scene* scene = getScene();
  470. if (scene)
  471. {
  472. Camera* camera = scene->getActiveCamera();
  473. if (camera)
  474. {
  475. Node* cameraNode = camera->getNode();
  476. if (cameraNode)
  477. {
  478. return cameraNode->getTranslationWorld();
  479. }
  480. }
  481. }
  482. return Vector3::zero();
  483. }
  484. Vector3 Node::getActiveCameraTranslationView() const
  485. {
  486. Scene* scene = getScene();
  487. if (scene)
  488. {
  489. Camera* camera = scene->getActiveCamera();
  490. if (camera)
  491. {
  492. Node* cameraNode = camera->getNode();
  493. if (cameraNode)
  494. {
  495. return cameraNode->getTranslationView();
  496. }
  497. }
  498. }
  499. return Vector3::zero();
  500. }
  501. void Node::hierarchyChanged()
  502. {
  503. // When our hierarchy changes our world transform is affected, so we must dirty it.
  504. transformChanged();
  505. }
  506. void Node::transformChanged()
  507. {
  508. // Our local transform was changed, so mark our world matrices dirty.
  509. _dirtyBits |= NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS;
  510. // Notify our children that their transform has also changed (since transforms are inherited).
  511. for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
  512. {
  513. if (Transform::isTransformChangedSuspended())
  514. {
  515. // If the DIRTY_NOTIFY bit is not set
  516. if (!n->isDirty(Transform::DIRTY_NOTIFY))
  517. {
  518. n->transformChanged();
  519. suspendTransformChange(n);
  520. }
  521. }
  522. else
  523. {
  524. n->transformChanged();
  525. }
  526. }
  527. Transform::transformChanged();
  528. }
  529. void Node::setBoundsDirty()
  530. {
  531. // Mark ourself and our parent nodes as dirty
  532. _dirtyBits |= NODE_DIRTY_BOUNDS;
  533. // Mark our parent bounds as dirty as well
  534. if (_parent)
  535. _parent->setBoundsDirty();
  536. }
  537. Animation* Node::getAnimation(const char* id) const
  538. {
  539. Animation* animation = ((AnimationTarget*)this)->getAnimation(id);
  540. if (animation)
  541. return animation;
  542. // See if this node has a model, then drill down.
  543. Model* model = this->getModel();
  544. if (model)
  545. {
  546. // Check to see if there's any animations with the ID on the joints.
  547. MeshSkin* skin = model->getSkin();
  548. if (skin)
  549. {
  550. Node* rootNode = skin->_rootNode;
  551. if (rootNode)
  552. {
  553. animation = rootNode->getAnimation(id);
  554. if (animation)
  555. return animation;
  556. }
  557. }
  558. // Check to see if any of the model's material parameter's has an animation
  559. // with the given ID.
  560. Material* material = model->getMaterial();
  561. if (material)
  562. {
  563. // How to access material parameters? hidden on the Material::RenderState.
  564. std::vector<MaterialParameter*>::iterator itr = material->_parameters.begin();
  565. for (; itr != material->_parameters.end(); itr++)
  566. {
  567. GP_ASSERT(*itr);
  568. animation = ((MaterialParameter*)(*itr))->getAnimation(id);
  569. if (animation)
  570. return animation;
  571. }
  572. }
  573. }
  574. // look through form for animations.
  575. Form* form = this->getForm();
  576. if (form)
  577. {
  578. animation = form->getAnimation(id);
  579. if (animation)
  580. return animation;
  581. }
  582. // Look through this node's children for an animation with the specified ID.
  583. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  584. {
  585. animation = child->getAnimation(id);
  586. if (animation)
  587. return animation;
  588. }
  589. return NULL;
  590. }
  591. Camera* Node::getCamera() const
  592. {
  593. return _camera;
  594. }
  595. void Node::setCamera(Camera* camera)
  596. {
  597. if (_camera != camera)
  598. {
  599. if (_camera)
  600. {
  601. _camera->setNode(NULL);
  602. SAFE_RELEASE(_camera);
  603. }
  604. _camera = camera;
  605. if (_camera)
  606. {
  607. _camera->addRef();
  608. _camera->setNode(this);
  609. }
  610. }
  611. }
  612. Light* Node::getLight() const
  613. {
  614. return _light;
  615. }
  616. void Node::setLight(Light* light)
  617. {
  618. if (_light != light)
  619. {
  620. if (_light)
  621. {
  622. _light->setNode(NULL);
  623. SAFE_RELEASE(_light);
  624. }
  625. _light = light;
  626. if (_light)
  627. {
  628. _light->addRef();
  629. _light->setNode(this);
  630. }
  631. }
  632. }
  633. void Node::setModel(Model* model)
  634. {
  635. if (_model != model)
  636. {
  637. if (_model)
  638. {
  639. _model->setNode(NULL);
  640. SAFE_RELEASE(_model);
  641. }
  642. _model = model;
  643. if (_model)
  644. {
  645. _model->addRef();
  646. _model->setNode(this);
  647. }
  648. }
  649. }
  650. Model* Node::getModel() const
  651. {
  652. return _model;
  653. }
  654. void Node::setForm(Form* form)
  655. {
  656. if (_form != form)
  657. {
  658. if (_form)
  659. {
  660. _form->setNode(NULL);
  661. SAFE_RELEASE(_form);
  662. }
  663. _form = form;
  664. if (_form)
  665. {
  666. _form->addRef();
  667. _form->setNode(this);
  668. }
  669. }
  670. }
  671. Form* Node::getForm() const
  672. {
  673. return _form;
  674. }
  675. const BoundingSphere& Node::getBoundingSphere() const
  676. {
  677. if (_dirtyBits & NODE_DIRTY_BOUNDS)
  678. {
  679. _dirtyBits &= ~NODE_DIRTY_BOUNDS;
  680. const Matrix& worldMatrix = getWorldMatrix();
  681. // Start with our local bounding sphere
  682. // TODO: Incorporate bounds from entities other than mesh (i.e. emitters, audiosource, etc)
  683. bool empty = true;
  684. if (_model && _model->getMesh())
  685. {
  686. _bounds.set(_model->getMesh()->getBoundingSphere());
  687. empty = false;
  688. }
  689. else
  690. {
  691. // Empty bounding sphere, set the world translation with zero radius
  692. worldMatrix.getTranslation(&_bounds.center);
  693. _bounds.radius = 0;
  694. }
  695. // Transform the sphere (if not empty) into world space.
  696. if (!empty)
  697. {
  698. bool applyWorldTransform = true;
  699. if (_model && _model->getSkin())
  700. {
  701. // Special case: If the root joint of our mesh skin is parented by any nodes,
  702. // multiply the world matrix of the root joint's parent by this node's
  703. // world matrix. This computes a final world matrix used for transforming this
  704. // node's bounding volume. This allows us to store a much smaller bounding
  705. // volume approximation than would otherwise be possible for skinned meshes,
  706. // since joint parent nodes that are not in the matrix pallette do not need to
  707. // be considered as directly transforming vertices on the GPU (they can instead
  708. // be applied directly to the bounding volume transformation below).
  709. GP_ASSERT(_model->getSkin()->getRootJoint());
  710. Node* jointParent = _model->getSkin()->getRootJoint()->getParent();
  711. if (jointParent)
  712. {
  713. // TODO: Should we protect against the case where joints are nested directly
  714. // in the node hierachy of the model (this is normally not the case)?
  715. Matrix boundsMatrix;
  716. Matrix::multiply(getWorldMatrix(), jointParent->getWorldMatrix(), &boundsMatrix);
  717. _bounds.transform(boundsMatrix);
  718. applyWorldTransform = false;
  719. }
  720. }
  721. if (applyWorldTransform)
  722. {
  723. _bounds.transform(getWorldMatrix());
  724. }
  725. }
  726. // Merge this world-space bounding sphere with our childrens' bounding volumes.
  727. for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
  728. {
  729. const BoundingSphere& childSphere = n->getBoundingSphere();
  730. if (!childSphere.isEmpty())
  731. {
  732. if (empty)
  733. {
  734. _bounds.set(childSphere);
  735. empty = false;
  736. }
  737. else
  738. {
  739. _bounds.merge(childSphere);
  740. }
  741. }
  742. }
  743. }
  744. return _bounds;
  745. }
  746. Node* Node::clone() const
  747. {
  748. NodeCloneContext context;
  749. return cloneRecursive(context);
  750. }
  751. Node* Node::cloneSingleNode(NodeCloneContext &context) const
  752. {
  753. Node* copy = Node::create(getId());
  754. context.registerClonedNode(this, copy);
  755. cloneInto(copy, context);
  756. return copy;
  757. }
  758. Node* Node::cloneRecursive(NodeCloneContext &context) const
  759. {
  760. Node* copy = cloneSingleNode(context);
  761. GP_ASSERT(copy);
  762. Node* lastChild = NULL;
  763. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  764. {
  765. lastChild = child;
  766. }
  767. // Loop through the nodes backwards because addChild adds the node to the front.
  768. for (Node* child = lastChild; child != NULL; child = child->getPreviousSibling())
  769. {
  770. Node* childCopy = child->cloneRecursive(context);
  771. GP_ASSERT(childCopy);
  772. copy->addChild(childCopy);
  773. childCopy->release();
  774. }
  775. return copy;
  776. }
  777. void Node::cloneInto(Node* node, NodeCloneContext &context) const
  778. {
  779. GP_ASSERT(node);
  780. Transform::cloneInto(node, context);
  781. // TODO: Clone the rest of the node data.
  782. if (Camera* camera = getCamera())
  783. {
  784. Camera* cameraClone = camera->clone(context);
  785. node->setCamera(cameraClone);
  786. cameraClone->release();
  787. }
  788. if (Light* light = getLight())
  789. {
  790. Light* lightClone = light->clone(context);
  791. node->setLight(lightClone);
  792. lightClone->release();
  793. }
  794. if (AudioSource* audio = getAudioSource())
  795. {
  796. AudioSource* audioClone = audio->clone(context);
  797. node->setAudioSource(audioClone);
  798. audioClone->release();
  799. }
  800. if (Model* model = getModel())
  801. {
  802. Model* modelClone = model->clone(context);
  803. node->setModel(modelClone);
  804. modelClone->release();
  805. }
  806. node->_world = _world;
  807. node->_bounds = _bounds;
  808. }
  809. AudioSource* Node::getAudioSource() const
  810. {
  811. return _audioSource;
  812. }
  813. void Node::setAudioSource(AudioSource* audio)
  814. {
  815. if (_audioSource != audio)
  816. {
  817. if (_audioSource)
  818. {
  819. _audioSource->setNode(NULL);
  820. SAFE_RELEASE(_audioSource);
  821. }
  822. _audioSource = audio;
  823. if (_audioSource)
  824. {
  825. _audioSource->addRef();
  826. _audioSource->setNode(this);
  827. }
  828. }
  829. }
  830. ParticleEmitter* Node::getParticleEmitter() const
  831. {
  832. return _particleEmitter;
  833. }
  834. void Node::setParticleEmitter(ParticleEmitter* emitter)
  835. {
  836. if (_particleEmitter != emitter)
  837. {
  838. if (_particleEmitter)
  839. {
  840. _particleEmitter->setNode(NULL);
  841. SAFE_RELEASE(_particleEmitter);
  842. }
  843. _particleEmitter = emitter;
  844. if (_particleEmitter)
  845. {
  846. _particleEmitter->addRef();
  847. _particleEmitter->setNode(this);
  848. }
  849. }
  850. }
  851. PhysicsCollisionObject* Node::getCollisionObject() const
  852. {
  853. return _collisionObject;
  854. }
  855. PhysicsCollisionObject* Node::setCollisionObject(PhysicsCollisionObject::Type type, const PhysicsCollisionShape::Definition& shape, PhysicsRigidBody::Parameters* rigidBodyParameters)
  856. {
  857. SAFE_DELETE(_collisionObject);
  858. switch (type)
  859. {
  860. case PhysicsCollisionObject::RIGID_BODY:
  861. {
  862. _collisionObject = new PhysicsRigidBody(this, shape, rigidBodyParameters ? *rigidBodyParameters : PhysicsRigidBody::Parameters());
  863. }
  864. break;
  865. case PhysicsCollisionObject::GHOST_OBJECT:
  866. {
  867. _collisionObject = new PhysicsGhostObject(this, shape);
  868. }
  869. break;
  870. case PhysicsCollisionObject::CHARACTER:
  871. {
  872. _collisionObject = new PhysicsCharacter(this, shape, rigidBodyParameters ? rigidBodyParameters->mass : 1.0f);
  873. }
  874. break;
  875. case PhysicsCollisionObject::NONE:
  876. break; // Already deleted, Just don't add a new collision object back.
  877. }
  878. return _collisionObject;
  879. }
  880. PhysicsCollisionObject* Node::setCollisionObject(const char* url)
  881. {
  882. // Load the collision object properties from file.
  883. Properties* properties = Properties::create(url);
  884. if (properties == NULL)
  885. {
  886. GP_ERROR("Failed to load collision object file: %s", url);
  887. return NULL;
  888. }
  889. PhysicsCollisionObject* collisionObject = setCollisionObject((strlen(properties->getNamespace()) > 0) ? properties : properties->getNextNamespace());
  890. SAFE_DELETE(properties);
  891. return collisionObject;
  892. }
  893. PhysicsCollisionObject* Node::setCollisionObject(Properties* properties)
  894. {
  895. SAFE_DELETE(_collisionObject);
  896. // Check if the properties is valid.
  897. if (!properties ||
  898. !(strcmp(properties->getNamespace(), "character") == 0 ||
  899. strcmp(properties->getNamespace(), "ghostObject") == 0 ||
  900. strcmp(properties->getNamespace(), "rigidBody") == 0))
  901. {
  902. GP_ERROR("Failed to load collision object from properties object: must be non-null object and have namespace equal to 'character', 'ghostObject', or 'rigidBody'.");
  903. return NULL;
  904. }
  905. if (strcmp(properties->getNamespace(), "character") == 0)
  906. {
  907. _collisionObject = PhysicsCharacter::create(this, properties);
  908. }
  909. else if (strcmp(properties->getNamespace(), "ghostObject") == 0)
  910. {
  911. _collisionObject = PhysicsGhostObject::create(this, properties);
  912. }
  913. else if (strcmp(properties->getNamespace(), "rigidBody") == 0)
  914. {
  915. _collisionObject = PhysicsRigidBody::create(this, properties);
  916. }
  917. return _collisionObject;
  918. }
  919. NodeCloneContext::NodeCloneContext()
  920. {
  921. }
  922. NodeCloneContext::~NodeCloneContext()
  923. {
  924. }
  925. Animation* NodeCloneContext::findClonedAnimation(const Animation* animation)
  926. {
  927. GP_ASSERT(animation);
  928. AnimationMap::iterator it = _clonedAnimations.find(animation);
  929. return it != _clonedAnimations.end() ? it->second : NULL;
  930. }
  931. void NodeCloneContext::registerClonedAnimation(const Animation* original, Animation* clone)
  932. {
  933. GP_ASSERT(original);
  934. GP_ASSERT(clone);
  935. _clonedAnimations[original] = clone;
  936. }
  937. Node* NodeCloneContext::findClonedNode(const Node* node)
  938. {
  939. GP_ASSERT(node);
  940. NodeMap::iterator it = _clonedNodes.find(node);
  941. return it != _clonedNodes.end() ? it->second : NULL;
  942. }
  943. void NodeCloneContext::registerClonedNode(const Node* original, Node* clone)
  944. {
  945. GP_ASSERT(original);
  946. GP_ASSERT(clone);
  947. _clonedNodes[original] = clone;
  948. }
  949. }