Node.cpp 33 KB

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