Node.cpp 34 KB

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