Node.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. #include "Base.h"
  2. #include "AudioSource.h"
  3. #include "Node.h"
  4. #include "Scene.h"
  5. #include "Joint.h"
  6. #include "PhysicsRigidBody.h"
  7. #include "PhysicsVehicle.h"
  8. #include "PhysicsVehicleWheel.h"
  9. #include "PhysicsGhostObject.h"
  10. #include "PhysicsCharacter.h"
  11. #include "Game.h"
  12. // Node dirty flags
  13. #define NODE_DIRTY_WORLD 1
  14. #define NODE_DIRTY_BOUNDS 2
  15. #define NODE_DIRTY_ALL (NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS)
  16. namespace gameplay
  17. {
  18. Node::Node(const char* id)
  19. : _scene(NULL), _firstChild(NULL), _nextSibling(NULL), _prevSibling(NULL), _parent(NULL), _childCount(0),
  20. _tags(NULL), _camera(NULL), _light(NULL), _model(NULL), _form(NULL), _audioSource(NULL), _particleEmitter(NULL),
  21. _collisionObject(NULL), _agent(NULL), _dirtyBits(NODE_DIRTY_ALL), _notifyHierarchyChanged(true), _userData(NULL)
  22. {
  23. if (id)
  24. {
  25. _id = id;
  26. }
  27. }
  28. Node::~Node()
  29. {
  30. removeAllChildren();
  31. if (_model)
  32. _model->setNode(NULL);
  33. if (_audioSource)
  34. _audioSource->setNode(NULL);
  35. if (_particleEmitter)
  36. _particleEmitter->setNode(NULL);
  37. if (_form)
  38. _form->setNode(NULL);
  39. SAFE_RELEASE(_camera);
  40. SAFE_RELEASE(_light);
  41. SAFE_RELEASE(_model);
  42. SAFE_RELEASE(_audioSource);
  43. SAFE_RELEASE(_particleEmitter);
  44. SAFE_RELEASE(_form);
  45. SAFE_DELETE(_collisionObject);
  46. SAFE_DELETE(_tags);
  47. setAgent(NULL);
  48. // Cleanup user data
  49. if (_userData)
  50. {
  51. // Call custom cleanup callback if specified
  52. if (_userData->cleanupCallback)
  53. _userData->cleanupCallback(_userData->pointer);
  54. SAFE_DELETE(_userData);
  55. }
  56. }
  57. Node* Node::create(const char* id)
  58. {
  59. return new Node(id);
  60. }
  61. const char* Node::getId() const
  62. {
  63. return _id.c_str();
  64. }
  65. void Node::setId(const char* id)
  66. {
  67. if (id)
  68. {
  69. _id = id;
  70. }
  71. }
  72. Node::Type Node::getType() const
  73. {
  74. return Node::NODE;
  75. }
  76. void Node::addChild(Node* child)
  77. {
  78. GP_ASSERT(child);
  79. if (child->_parent == this)
  80. {
  81. // This node is already present in our hierarchy
  82. return;
  83. }
  84. child->addRef();
  85. // If the item belongs to another hierarchy, remove it first.
  86. if (child->_parent)
  87. {
  88. child->_parent->removeChild(child);
  89. }
  90. else if (child->_scene)
  91. {
  92. child->_scene->removeNode(child);
  93. }
  94. // Order is irrelevant, so add to the beginning of the list.
  95. if (_firstChild)
  96. {
  97. _firstChild->_prevSibling = child;
  98. child->_nextSibling = _firstChild;
  99. _firstChild = child;
  100. }
  101. else
  102. {
  103. _firstChild = child;
  104. }
  105. child->_parent = this;
  106. ++_childCount;
  107. if (_notifyHierarchyChanged)
  108. {
  109. hierarchyChanged();
  110. }
  111. }
  112. void Node::removeChild(Node* child)
  113. {
  114. if (child == NULL || child->_parent != this)
  115. {
  116. // The child is not in our hierarchy.
  117. return;
  118. }
  119. // Call remove on the child.
  120. child->remove();
  121. SAFE_RELEASE(child);
  122. }
  123. void Node::removeAllChildren()
  124. {
  125. _notifyHierarchyChanged = false;
  126. while (_firstChild)
  127. {
  128. removeChild(_firstChild);
  129. }
  130. _notifyHierarchyChanged = true;
  131. hierarchyChanged();
  132. }
  133. void Node::remove()
  134. {
  135. // Re-link our neighbours.
  136. if (_prevSibling)
  137. {
  138. _prevSibling->_nextSibling = _nextSibling;
  139. }
  140. if (_nextSibling)
  141. {
  142. _nextSibling->_prevSibling = _prevSibling;
  143. }
  144. // Update our parent.
  145. Node* parent = _parent;
  146. if (parent)
  147. {
  148. if (this == parent->_firstChild)
  149. {
  150. parent->_firstChild = _nextSibling;
  151. }
  152. --parent->_childCount;
  153. }
  154. _nextSibling = NULL;
  155. _prevSibling = NULL;
  156. _parent = NULL;
  157. if (parent && parent->_notifyHierarchyChanged)
  158. {
  159. parent->hierarchyChanged();
  160. }
  161. }
  162. Node* Node::getFirstChild() const
  163. {
  164. return _firstChild;
  165. }
  166. Node* Node::getNextSibling() const
  167. {
  168. return _nextSibling;
  169. }
  170. Node* Node::getPreviousSibling() const
  171. {
  172. return _prevSibling;
  173. }
  174. Node* Node::getParent() const
  175. {
  176. return _parent;
  177. }
  178. bool Node::hasTag(const char* name) const
  179. {
  180. GP_ASSERT(name);
  181. return (_tags ? _tags->find(name) != _tags->end() : false);
  182. }
  183. const char* Node::getTag(const char* name) const
  184. {
  185. GP_ASSERT(name);
  186. if (!_tags)
  187. return NULL;
  188. std::map<std::string, std::string>::const_iterator itr = _tags->find(name);
  189. return (itr == _tags->end() ? NULL : itr->second.c_str());
  190. }
  191. void Node::setTag(const char* name, const char* value)
  192. {
  193. GP_ASSERT(name);
  194. if (value == NULL)
  195. {
  196. // Removing tag
  197. if (_tags)
  198. {
  199. _tags->erase(name);
  200. if (_tags->size() == 0)
  201. SAFE_DELETE(_tags);
  202. }
  203. }
  204. else
  205. {
  206. // Setting tag
  207. if (_tags == NULL)
  208. _tags = new std::map<std::string, std::string>();
  209. (*_tags)[name] = value;
  210. }
  211. }
  212. void* Node::getUserPointer() const
  213. {
  214. return (_userData ? _userData->pointer : NULL);
  215. }
  216. void Node::setUserPointer(void* pointer, void (*cleanupCallback)(void*))
  217. {
  218. // If existing user pointer is being changed, call cleanup function to free previous pointer
  219. if (_userData && _userData->pointer && _userData->cleanupCallback && pointer != _userData->pointer)
  220. {
  221. _userData->cleanupCallback(_userData->pointer);
  222. }
  223. if (pointer)
  224. {
  225. // Assign user pointer
  226. if (_userData == NULL)
  227. _userData = new UserData();
  228. _userData->pointer = pointer;
  229. _userData->cleanupCallback = cleanupCallback;
  230. }
  231. else
  232. {
  233. // Clear user pointer
  234. SAFE_DELETE(_userData);
  235. }
  236. }
  237. unsigned int Node::getChildCount() const
  238. {
  239. return _childCount;
  240. }
  241. Node* Node::findNode(const char* id, bool recursive, bool exactMatch) const
  242. {
  243. GP_ASSERT(id);
  244. // If the node has a model with a mesh skin, search the skin's hierarchy as well.
  245. Node* rootNode = NULL;
  246. if (_model != NULL && _model->getSkin() != NULL && (rootNode = _model->getSkin()->_rootNode) != NULL)
  247. {
  248. if ((exactMatch && rootNode->_id == id) || (!exactMatch && rootNode->_id.find(id) == 0))
  249. return rootNode;
  250. Node* match = rootNode->findNode(id, true, exactMatch);
  251. if (match)
  252. {
  253. return match;
  254. }
  255. }
  256. // Search immediate children first.
  257. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  258. {
  259. // Does this child's ID match?
  260. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  261. {
  262. return child;
  263. }
  264. }
  265. // Recurse.
  266. if (recursive)
  267. {
  268. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  269. {
  270. Node* match = child->findNode(id, true, exactMatch);
  271. if (match)
  272. {
  273. return match;
  274. }
  275. }
  276. }
  277. return NULL;
  278. }
  279. unsigned int Node::findNodes(const char* id, std::vector<Node*>& nodes, bool recursive, bool exactMatch) const
  280. {
  281. GP_ASSERT(id);
  282. unsigned int count = 0;
  283. // Search immediate children first.
  284. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  285. {
  286. // Does this child's ID match?
  287. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  288. {
  289. nodes.push_back(child);
  290. ++count;
  291. }
  292. }
  293. // Recurse.
  294. if (recursive)
  295. {
  296. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  297. {
  298. count += child->findNodes(id, nodes, true, exactMatch);
  299. }
  300. }
  301. return count;
  302. }
  303. Scene* Node::getScene() const
  304. {
  305. // Search for a scene in our parents.
  306. for (Node* n = const_cast<Node*>(this); n != NULL; n = n->getParent())
  307. {
  308. if (n->_scene)
  309. {
  310. return n->_scene;
  311. }
  312. }
  313. return NULL;
  314. }
  315. Node* Node::getRootNode() const
  316. {
  317. Node* n = const_cast<Node*>(this);
  318. while (n->getParent())
  319. {
  320. n = n->getParent();
  321. }
  322. return n;
  323. }
  324. const Matrix& Node::getWorldMatrix() const
  325. {
  326. if (_dirtyBits & NODE_DIRTY_WORLD)
  327. {
  328. // Clear our dirty flag immediately to prevent this block from being entered if our
  329. // parent calls our getWorldMatrix() method as a result of the following calculations.
  330. _dirtyBits &= ~NODE_DIRTY_WORLD;
  331. // If we have a parent, multiply our parent world transform by our local
  332. // transform to obtain our final resolved world transform.
  333. Node* parent = getParent();
  334. if (parent && (!_collisionObject || _collisionObject->isKinematic()))
  335. {
  336. Matrix::multiply(parent->getWorldMatrix(), getMatrix(), &_world);
  337. }
  338. else
  339. {
  340. _world = getMatrix();
  341. }
  342. // Our world matrix was just updated, so call getWorldMatrix() on all child nodes
  343. // to force their resolved world matrices to be updated.
  344. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  345. {
  346. child->getWorldMatrix();
  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. return vector;
  468. }
  469. Vector3 Node::getRightVectorWorld() const
  470. {
  471. Vector3 vector;
  472. getWorldMatrix().getRightVector(&vector);
  473. return vector;
  474. }
  475. Vector3 Node::getUpVectorWorld() const
  476. {
  477. Vector3 vector;
  478. getWorldMatrix().getUpVector(&vector);
  479. return vector;
  480. }
  481. Vector3 Node::getActiveCameraTranslationWorld() const
  482. {
  483. Scene* scene = getScene();
  484. if (scene)
  485. {
  486. Camera* camera = scene->getActiveCamera();
  487. if (camera)
  488. {
  489. Node* cameraNode = camera->getNode();
  490. if (cameraNode)
  491. {
  492. return cameraNode->getTranslationWorld();
  493. }
  494. }
  495. }
  496. return Vector3::zero();
  497. }
  498. Vector3 Node::getActiveCameraTranslationView() const
  499. {
  500. Scene* scene = getScene();
  501. if (scene)
  502. {
  503. Camera* camera = scene->getActiveCamera();
  504. if (camera)
  505. {
  506. Node* cameraNode = camera->getNode();
  507. if (cameraNode)
  508. {
  509. return cameraNode->getTranslationView();
  510. }
  511. }
  512. }
  513. return Vector3::zero();
  514. }
  515. void Node::hierarchyChanged()
  516. {
  517. // When our hierarchy changes our world transform is affected, so we must dirty it.
  518. transformChanged();
  519. }
  520. void Node::transformChanged()
  521. {
  522. // Our local transform was changed, so mark our world matrices dirty.
  523. _dirtyBits |= NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS;
  524. // Notify our children that their transform has also changed (since transforms are inherited).
  525. for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
  526. {
  527. if (Transform::isTransformChangedSuspended())
  528. {
  529. // If the DIRTY_NOTIFY bit is not set
  530. if (!n->isDirty(Transform::DIRTY_NOTIFY))
  531. {
  532. n->transformChanged();
  533. suspendTransformChange(n);
  534. }
  535. }
  536. else
  537. {
  538. n->transformChanged();
  539. }
  540. }
  541. Transform::transformChanged();
  542. }
  543. void Node::setBoundsDirty()
  544. {
  545. // Mark ourself and our parent nodes as dirty
  546. _dirtyBits |= NODE_DIRTY_BOUNDS;
  547. // Mark our parent bounds as dirty as well
  548. if (_parent)
  549. _parent->setBoundsDirty();
  550. }
  551. Animation* Node::getAnimation(const char* id) const
  552. {
  553. Animation* animation = ((AnimationTarget*)this)->getAnimation(id);
  554. if (animation)
  555. return animation;
  556. // See if this node has a model, then drill down.
  557. Model* model = this->getModel();
  558. if (model)
  559. {
  560. // Check to see if there's any animations with the ID on the joints.
  561. MeshSkin* skin = model->getSkin();
  562. if (skin)
  563. {
  564. Node* rootNode = skin->_rootNode;
  565. if (rootNode)
  566. {
  567. animation = rootNode->getAnimation(id);
  568. if (animation)
  569. return animation;
  570. }
  571. }
  572. // Check to see if any of the model's material parameter's has an animation
  573. // with the given ID.
  574. Material* material = model->getMaterial();
  575. if (material)
  576. {
  577. // How to access material parameters? hidden on the Material::RenderState.
  578. std::vector<MaterialParameter*>::iterator itr = material->_parameters.begin();
  579. for (; itr != material->_parameters.end(); itr++)
  580. {
  581. GP_ASSERT(*itr);
  582. animation = ((MaterialParameter*)(*itr))->getAnimation(id);
  583. if (animation)
  584. return animation;
  585. }
  586. }
  587. }
  588. // look through form for animations.
  589. Form* form = this->getForm();
  590. if (form)
  591. {
  592. animation = form->getAnimation(id);
  593. if (animation)
  594. return animation;
  595. }
  596. // Look through this node's children for an animation with the specified ID.
  597. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  598. {
  599. animation = child->getAnimation(id);
  600. if (animation)
  601. return animation;
  602. }
  603. return NULL;
  604. }
  605. Camera* Node::getCamera() const
  606. {
  607. return _camera;
  608. }
  609. void Node::setCamera(Camera* camera)
  610. {
  611. if (_camera != camera)
  612. {
  613. if (_camera)
  614. {
  615. _camera->setNode(NULL);
  616. SAFE_RELEASE(_camera);
  617. }
  618. _camera = camera;
  619. if (_camera)
  620. {
  621. _camera->addRef();
  622. _camera->setNode(this);
  623. }
  624. }
  625. }
  626. Light* Node::getLight() const
  627. {
  628. return _light;
  629. }
  630. void Node::setLight(Light* light)
  631. {
  632. if (_light != light)
  633. {
  634. if (_light)
  635. {
  636. _light->setNode(NULL);
  637. SAFE_RELEASE(_light);
  638. }
  639. _light = light;
  640. if (_light)
  641. {
  642. _light->addRef();
  643. _light->setNode(this);
  644. }
  645. }
  646. }
  647. void Node::setModel(Model* model)
  648. {
  649. if (_model != model)
  650. {
  651. if (_model)
  652. {
  653. _model->setNode(NULL);
  654. SAFE_RELEASE(_model);
  655. }
  656. _model = model;
  657. if (_model)
  658. {
  659. _model->addRef();
  660. _model->setNode(this);
  661. }
  662. }
  663. }
  664. Model* Node::getModel() const
  665. {
  666. return _model;
  667. }
  668. void Node::setForm(Form* form)
  669. {
  670. if (_form != form)
  671. {
  672. if (_form)
  673. {
  674. _form->setNode(NULL);
  675. SAFE_RELEASE(_form);
  676. }
  677. _form = form;
  678. if (_form)
  679. {
  680. _form->addRef();
  681. _form->setNode(this);
  682. }
  683. }
  684. }
  685. Form* Node::getForm() const
  686. {
  687. return _form;
  688. }
  689. const BoundingSphere& Node::getBoundingSphere() const
  690. {
  691. if (_dirtyBits & NODE_DIRTY_BOUNDS)
  692. {
  693. _dirtyBits &= ~NODE_DIRTY_BOUNDS;
  694. const Matrix& worldMatrix = getWorldMatrix();
  695. // Start with our local bounding sphere
  696. // TODO: Incorporate bounds from entities other than mesh (i.e. emitters, audiosource, etc)
  697. bool empty = true;
  698. if (_model && _model->getMesh())
  699. {
  700. _bounds.set(_model->getMesh()->getBoundingSphere());
  701. empty = false;
  702. }
  703. else
  704. {
  705. // Empty bounding sphere, set the world translation with zero radius
  706. worldMatrix.getTranslation(&_bounds.center);
  707. _bounds.radius = 0;
  708. }
  709. // Transform the sphere (if not empty) into world space.
  710. if (!empty)
  711. {
  712. bool applyWorldTransform = true;
  713. if (_model && _model->getSkin())
  714. {
  715. // Special case: If the root joint of our mesh skin is parented by any nodes,
  716. // multiply the world matrix of the root joint's parent by this node's
  717. // world matrix. This computes a final world matrix used for transforming this
  718. // node's bounding volume. This allows us to store a much smaller bounding
  719. // volume approximation than would otherwise be possible for skinned meshes,
  720. // since joint parent nodes that are not in the matrix palette do not need to
  721. // be considered as directly transforming vertices on the GPU (they can instead
  722. // be applied directly to the bounding volume transformation below).
  723. GP_ASSERT(_model->getSkin()->getRootJoint());
  724. Node* jointParent = _model->getSkin()->getRootJoint()->getParent();
  725. if (jointParent)
  726. {
  727. // TODO: Should we protect against the case where joints are nested directly
  728. // in the node hierachy of the model (this is normally not the case)?
  729. Matrix boundsMatrix;
  730. Matrix::multiply(getWorldMatrix(), jointParent->getWorldMatrix(), &boundsMatrix);
  731. _bounds.transform(boundsMatrix);
  732. applyWorldTransform = false;
  733. }
  734. }
  735. if (applyWorldTransform)
  736. {
  737. _bounds.transform(getWorldMatrix());
  738. }
  739. }
  740. // Merge this world-space bounding sphere with our childrens' bounding volumes.
  741. for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
  742. {
  743. const BoundingSphere& childSphere = n->getBoundingSphere();
  744. if (!childSphere.isEmpty())
  745. {
  746. if (empty)
  747. {
  748. _bounds.set(childSphere);
  749. empty = false;
  750. }
  751. else
  752. {
  753. _bounds.merge(childSphere);
  754. }
  755. }
  756. }
  757. }
  758. return _bounds;
  759. }
  760. Node* Node::clone() const
  761. {
  762. NodeCloneContext context;
  763. return cloneRecursive(context);
  764. }
  765. Node* Node::cloneSingleNode(NodeCloneContext &context) const
  766. {
  767. Node* copy = Node::create(getId());
  768. context.registerClonedNode(this, copy);
  769. cloneInto(copy, context);
  770. return copy;
  771. }
  772. Node* Node::cloneRecursive(NodeCloneContext &context) const
  773. {
  774. Node* copy = cloneSingleNode(context);
  775. GP_ASSERT(copy);
  776. Node* lastChild = NULL;
  777. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  778. {
  779. lastChild = child;
  780. }
  781. // Loop through the nodes backwards because addChild adds the node to the front.
  782. for (Node* child = lastChild; child != NULL; child = child->getPreviousSibling())
  783. {
  784. Node* childCopy = child->cloneRecursive(context);
  785. GP_ASSERT(childCopy);
  786. copy->addChild(childCopy);
  787. childCopy->release();
  788. }
  789. return copy;
  790. }
  791. void Node::cloneInto(Node* node, NodeCloneContext &context) const
  792. {
  793. GP_ASSERT(node);
  794. Transform::cloneInto(node, context);
  795. // TODO: Clone the rest of the node data.
  796. if (Camera* camera = getCamera())
  797. {
  798. Camera* cameraClone = camera->clone(context);
  799. node->setCamera(cameraClone);
  800. cameraClone->release();
  801. }
  802. if (Light* light = getLight())
  803. {
  804. Light* lightClone = light->clone(context);
  805. node->setLight(lightClone);
  806. lightClone->release();
  807. }
  808. if (AudioSource* audio = getAudioSource())
  809. {
  810. AudioSource* audioClone = audio->clone(context);
  811. node->setAudioSource(audioClone);
  812. audioClone->release();
  813. }
  814. if (Model* model = getModel())
  815. {
  816. Model* modelClone = model->clone(context);
  817. node->setModel(modelClone);
  818. modelClone->release();
  819. }
  820. node->_world = _world;
  821. node->_bounds = _bounds;
  822. if (_tags)
  823. {
  824. node->_tags = new std::map<std::string, std::string>(_tags->begin(), _tags->end());
  825. }
  826. }
  827. AudioSource* Node::getAudioSource() const
  828. {
  829. return _audioSource;
  830. }
  831. void Node::setAudioSource(AudioSource* audio)
  832. {
  833. if (_audioSource != audio)
  834. {
  835. if (_audioSource)
  836. {
  837. _audioSource->setNode(NULL);
  838. SAFE_RELEASE(_audioSource);
  839. }
  840. _audioSource = audio;
  841. if (_audioSource)
  842. {
  843. _audioSource->addRef();
  844. _audioSource->setNode(this);
  845. }
  846. }
  847. }
  848. ParticleEmitter* Node::getParticleEmitter() const
  849. {
  850. return _particleEmitter;
  851. }
  852. void Node::setParticleEmitter(ParticleEmitter* emitter)
  853. {
  854. if (_particleEmitter != emitter)
  855. {
  856. if (_particleEmitter)
  857. {
  858. _particleEmitter->setNode(NULL);
  859. SAFE_RELEASE(_particleEmitter);
  860. }
  861. _particleEmitter = emitter;
  862. if (_particleEmitter)
  863. {
  864. _particleEmitter->addRef();
  865. _particleEmitter->setNode(this);
  866. }
  867. }
  868. }
  869. PhysicsCollisionObject* Node::getCollisionObject() const
  870. {
  871. return _collisionObject;
  872. }
  873. PhysicsCollisionObject* Node::setCollisionObject(PhysicsCollisionObject::Type type, const PhysicsCollisionShape::Definition& shape, PhysicsRigidBody::Parameters* rigidBodyParameters)
  874. {
  875. SAFE_DELETE(_collisionObject);
  876. switch (type)
  877. {
  878. case PhysicsCollisionObject::RIGID_BODY:
  879. {
  880. _collisionObject = new PhysicsRigidBody(this, shape, rigidBodyParameters ? *rigidBodyParameters : PhysicsRigidBody::Parameters());
  881. }
  882. break;
  883. case PhysicsCollisionObject::GHOST_OBJECT:
  884. {
  885. _collisionObject = new PhysicsGhostObject(this, shape);
  886. }
  887. break;
  888. case PhysicsCollisionObject::CHARACTER:
  889. {
  890. _collisionObject = new PhysicsCharacter(this, shape, rigidBodyParameters ? rigidBodyParameters->mass : 1.0f);
  891. }
  892. break;
  893. case PhysicsCollisionObject::VEHICLE:
  894. {
  895. _collisionObject = new PhysicsVehicle(this, shape, rigidBodyParameters ? *rigidBodyParameters : PhysicsRigidBody::Parameters());
  896. }
  897. break;
  898. case PhysicsCollisionObject::VEHICLE_WHEEL:
  899. {
  900. //
  901. // PhysicsVehicleWheel is special because this call will traverse up the scene graph for the
  902. // first ancestor node that is shared with another node of collision type VEHICLE, and then
  903. // proceed to add itself as a wheel onto that vehicle. This is by design, and allows the
  904. // visual scene hierarchy to be the sole representation of the relationship between physics
  905. // objects rather than forcing that upon the otherwise-flat ".physics" (properties) file.
  906. //
  907. // IMPORTANT: The VEHICLE must come before the VEHICLE_WHEEL in the ".scene" (properties) file!
  908. //
  909. _collisionObject = new PhysicsVehicleWheel(this, shape, rigidBodyParameters ? *rigidBodyParameters : PhysicsRigidBody::Parameters());
  910. }
  911. break;
  912. case PhysicsCollisionObject::NONE:
  913. break; // Already deleted, Just don't add a new collision object back.
  914. }
  915. return _collisionObject;
  916. }
  917. PhysicsCollisionObject* Node::setCollisionObject(const char* url)
  918. {
  919. // Load the collision object properties from file.
  920. Properties* properties = Properties::create(url);
  921. if (properties == NULL)
  922. {
  923. GP_ERROR("Failed to load collision object file: %s", url);
  924. return NULL;
  925. }
  926. PhysicsCollisionObject* collisionObject = setCollisionObject((strlen(properties->getNamespace()) > 0) ? properties : properties->getNextNamespace());
  927. SAFE_DELETE(properties);
  928. return collisionObject;
  929. }
  930. PhysicsCollisionObject* Node::setCollisionObject(Properties* properties)
  931. {
  932. SAFE_DELETE(_collisionObject);
  933. // Check if the properties is valid.
  934. if (!properties || !(strcmp(properties->getNamespace(), "collisionObject") == 0))
  935. {
  936. GP_ERROR("Failed to load collision object from properties object: must be non-null object and have namespace equal to 'collisionObject'.");
  937. return NULL;
  938. }
  939. if (const char* type = properties->getString("type"))
  940. {
  941. if (strcmp(type, "CHARACTER") == 0)
  942. {
  943. _collisionObject = PhysicsCharacter::create(this, properties);
  944. }
  945. else if (strcmp(type, "GHOST_OBJECT") == 0)
  946. {
  947. _collisionObject = PhysicsGhostObject::create(this, properties);
  948. }
  949. else if (strcmp(type, "RIGID_BODY") == 0)
  950. {
  951. _collisionObject = PhysicsRigidBody::create(this, properties);
  952. }
  953. else if (strcmp(type, "VEHICLE") == 0)
  954. {
  955. _collisionObject = PhysicsVehicle::create(this, properties);
  956. }
  957. else if (strcmp(type, "VEHICLE_WHEEL") == 0)
  958. {
  959. //
  960. // PhysicsVehicleWheel is special because this call will traverse up the scene graph for the
  961. // first ancestor node that is shared with another node of collision type VEHICLE, and then
  962. // proceed to add itself as a wheel onto that vehicle. This is by design, and allows the
  963. // visual scene hierarchy to be the sole representation of the relationship between physics
  964. // objects rather than forcing that upon the otherwise-flat ".physics" (properties) file.
  965. //
  966. // IMPORTANT: The VEHICLE must come before the VEHICLE_WHEEL in the ".scene" (properties) file!
  967. //
  968. _collisionObject = PhysicsVehicleWheel::create(this, properties);
  969. }
  970. else
  971. {
  972. GP_ERROR("Unsupported collision object type '%s'.", type);
  973. return NULL;
  974. }
  975. }
  976. else
  977. {
  978. GP_ERROR("Failed to load collision object from properties object; required attribute 'type' is missing.");
  979. return NULL;
  980. }
  981. return _collisionObject;
  982. }
  983. unsigned int Node::getNumAdvertisedDescendants() const
  984. {
  985. return _advertisedDescendants.size();
  986. }
  987. Node* Node::getAdvertisedDescendant(unsigned int i) const
  988. {
  989. return _advertisedDescendants.at(i);
  990. }
  991. void Node::addAdvertisedDescendant(Node* node)
  992. {
  993. _advertisedDescendants.push_back(node);
  994. }
  995. AIAgent* Node::getAgent() const
  996. {
  997. return _agent;
  998. }
  999. void Node::setAgent(AIAgent* agent)
  1000. {
  1001. if (agent != _agent)
  1002. {
  1003. if (_agent)
  1004. {
  1005. Game::getInstance()->getAIController()->removeAgent(_agent);
  1006. _agent->_node = NULL;
  1007. SAFE_RELEASE(_agent);
  1008. }
  1009. _agent = agent;
  1010. if (_agent)
  1011. {
  1012. _agent->addRef();
  1013. _agent->_node = this;
  1014. Game::getInstance()->getAIController()->addAgent(_agent);
  1015. }
  1016. }
  1017. }
  1018. NodeCloneContext::NodeCloneContext()
  1019. {
  1020. }
  1021. NodeCloneContext::~NodeCloneContext()
  1022. {
  1023. }
  1024. Animation* NodeCloneContext::findClonedAnimation(const Animation* animation)
  1025. {
  1026. GP_ASSERT(animation);
  1027. std::map<const Animation*, Animation*>::iterator it = _clonedAnimations.find(animation);
  1028. return it != _clonedAnimations.end() ? it->second : NULL;
  1029. }
  1030. void NodeCloneContext::registerClonedAnimation(const Animation* original, Animation* clone)
  1031. {
  1032. GP_ASSERT(original);
  1033. GP_ASSERT(clone);
  1034. _clonedAnimations[original] = clone;
  1035. }
  1036. Node* NodeCloneContext::findClonedNode(const Node* node)
  1037. {
  1038. GP_ASSERT(node);
  1039. std::map<const Node*, Node*>::iterator it = _clonedNodes.find(node);
  1040. return it != _clonedNodes.end() ? it->second : NULL;
  1041. }
  1042. void NodeCloneContext::registerClonedNode(const Node* original, Node* clone)
  1043. {
  1044. GP_ASSERT(original);
  1045. GP_ASSERT(clone);
  1046. _clonedNodes[original] = clone;
  1047. }
  1048. }