Node.cpp 33 KB

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