Node.cpp 32 KB

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