Node.cpp 33 KB

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