Node.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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. node = node->_parent;
  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. bool Node::isStatic() const
  372. {
  373. return (_collisionObject && _collisionObject->isStatic());
  374. }
  375. const Matrix& Node::getWorldMatrix() const
  376. {
  377. if (_dirtyBits & NODE_DIRTY_WORLD)
  378. {
  379. // Clear our dirty flag immediately to prevent this block from being entered if our
  380. // parent calls our getWorldMatrix() method as a result of the following calculations.
  381. _dirtyBits &= ~NODE_DIRTY_WORLD;
  382. if (!isStatic())
  383. {
  384. // If we have a parent, multiply our parent world transform by our local
  385. // transform to obtain our final resolved world transform.
  386. Node* parent = getParent();
  387. if (parent && (!_collisionObject || _collisionObject->isKinematic()))
  388. {
  389. Matrix::multiply(parent->getWorldMatrix(), getMatrix(), &_world);
  390. }
  391. else
  392. {
  393. _world = getMatrix();
  394. }
  395. // Our world matrix was just updated, so call getWorldMatrix() on all child nodes
  396. // to force their resolved world matrices to be updated.
  397. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  398. {
  399. child->getWorldMatrix();
  400. }
  401. }
  402. }
  403. return _world;
  404. }
  405. const Matrix& Node::getWorldViewMatrix() const
  406. {
  407. static Matrix worldView;
  408. Matrix::multiply(getViewMatrix(), getWorldMatrix(), &worldView);
  409. return worldView;
  410. }
  411. const Matrix& Node::getInverseTransposeWorldViewMatrix() const
  412. {
  413. static Matrix invTransWorldView;
  414. Matrix::multiply(getViewMatrix(), getWorldMatrix(), &invTransWorldView);
  415. invTransWorldView.invert();
  416. invTransWorldView.transpose();
  417. return invTransWorldView;
  418. }
  419. const Matrix& Node::getInverseTransposeWorldMatrix() const
  420. {
  421. static Matrix invTransWorld;
  422. invTransWorld = getWorldMatrix();
  423. invTransWorld.invert();
  424. invTransWorld.transpose();
  425. return invTransWorld;
  426. }
  427. const Matrix& Node::getViewMatrix() const
  428. {
  429. Scene* scene = getScene();
  430. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  431. if (camera)
  432. {
  433. return camera->getViewMatrix();
  434. }
  435. else
  436. {
  437. return Matrix::identity();
  438. }
  439. }
  440. const Matrix& Node::getInverseViewMatrix() const
  441. {
  442. Scene* scene = getScene();
  443. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  444. if (camera)
  445. {
  446. return camera->getInverseViewMatrix();
  447. }
  448. else
  449. {
  450. return Matrix::identity();
  451. }
  452. }
  453. const Matrix& Node::getProjectionMatrix() const
  454. {
  455. Scene* scene = getScene();
  456. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  457. if (camera)
  458. {
  459. return camera->getProjectionMatrix();
  460. }
  461. else
  462. {
  463. return Matrix::identity();
  464. }
  465. }
  466. const Matrix& Node::getViewProjectionMatrix() const
  467. {
  468. Scene* scene = getScene();
  469. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  470. if (camera)
  471. {
  472. return camera->getViewProjectionMatrix();
  473. }
  474. else
  475. {
  476. return Matrix::identity();
  477. }
  478. }
  479. const Matrix& Node::getInverseViewProjectionMatrix() const
  480. {
  481. Scene* scene = getScene();
  482. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  483. if (camera)
  484. {
  485. return camera->getInverseViewProjectionMatrix();
  486. }
  487. return Matrix::identity();
  488. }
  489. const Matrix& Node::getWorldViewProjectionMatrix() const
  490. {
  491. static Matrix worldViewProj;
  492. // Always re-calculate worldViewProjection matrix since it's extremely difficult
  493. // to track whether the camera has changed (it may frequently change every frame).
  494. Matrix::multiply(getViewProjectionMatrix(), getWorldMatrix(), &worldViewProj);
  495. return worldViewProj;
  496. }
  497. Vector3 Node::getTranslationWorld() const
  498. {
  499. Vector3 translation;
  500. getWorldMatrix().getTranslation(&translation);
  501. return translation;
  502. }
  503. Vector3 Node::getTranslationView() const
  504. {
  505. Vector3 translation;
  506. getWorldMatrix().getTranslation(&translation);
  507. getViewMatrix().transformPoint(&translation);
  508. return translation;
  509. }
  510. Vector3 Node::getForwardVectorWorld() const
  511. {
  512. Vector3 vector;
  513. getWorldMatrix().getForwardVector(&vector);
  514. return vector;
  515. }
  516. Vector3 Node::getForwardVectorView() const
  517. {
  518. Vector3 vector;
  519. getWorldMatrix().getForwardVector(&vector);
  520. getViewMatrix().transformVector(&vector);
  521. return vector;
  522. }
  523. Vector3 Node::getRightVectorWorld() const
  524. {
  525. Vector3 vector;
  526. getWorldMatrix().getRightVector(&vector);
  527. return vector;
  528. }
  529. Vector3 Node::getUpVectorWorld() const
  530. {
  531. Vector3 vector;
  532. getWorldMatrix().getUpVector(&vector);
  533. return vector;
  534. }
  535. Vector3 Node::getActiveCameraTranslationWorld() const
  536. {
  537. Scene* scene = getScene();
  538. if (scene)
  539. {
  540. Camera* camera = scene->getActiveCamera();
  541. if (camera)
  542. {
  543. Node* cameraNode = camera->getNode();
  544. if (cameraNode)
  545. {
  546. return cameraNode->getTranslationWorld();
  547. }
  548. }
  549. }
  550. return Vector3::zero();
  551. }
  552. Vector3 Node::getActiveCameraTranslationView() const
  553. {
  554. Scene* scene = getScene();
  555. if (scene)
  556. {
  557. Camera* camera = scene->getActiveCamera();
  558. if (camera)
  559. {
  560. Node* cameraNode = camera->getNode();
  561. if (cameraNode)
  562. {
  563. return cameraNode->getTranslationView();
  564. }
  565. }
  566. }
  567. return Vector3::zero();
  568. }
  569. void Node::hierarchyChanged()
  570. {
  571. // When our hierarchy changes our world transform is affected, so we must dirty it.
  572. transformChanged();
  573. }
  574. void Node::transformChanged()
  575. {
  576. // Our local transform was changed, so mark our world matrices dirty.
  577. _dirtyBits |= NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS;
  578. // Notify our children that their transform has also changed (since transforms are inherited).
  579. for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
  580. {
  581. if (Transform::isTransformChangedSuspended())
  582. {
  583. // If the DIRTY_NOTIFY bit is not set
  584. if (!n->isDirty(Transform::DIRTY_NOTIFY))
  585. {
  586. n->transformChanged();
  587. suspendTransformChange(n);
  588. }
  589. }
  590. else
  591. {
  592. n->transformChanged();
  593. }
  594. }
  595. Transform::transformChanged();
  596. }
  597. void Node::setBoundsDirty()
  598. {
  599. // Mark ourself and our parent nodes as dirty
  600. _dirtyBits |= NODE_DIRTY_BOUNDS;
  601. // Mark our parent bounds as dirty as well
  602. if (_parent)
  603. _parent->setBoundsDirty();
  604. }
  605. Animation* Node::getAnimation(const char* id) const
  606. {
  607. Animation* animation = ((AnimationTarget*)this)->getAnimation(id);
  608. if (animation)
  609. return animation;
  610. // See if this node has a model, then drill down.
  611. Model* model = this->getModel();
  612. if (model)
  613. {
  614. // Check to see if there's any animations with the ID on the joints.
  615. MeshSkin* skin = model->getSkin();
  616. if (skin)
  617. {
  618. Node* rootNode = skin->_rootNode;
  619. if (rootNode)
  620. {
  621. animation = rootNode->getAnimation(id);
  622. if (animation)
  623. return animation;
  624. }
  625. }
  626. // Check to see if any of the model's material parameter's has an animation
  627. // with the given ID.
  628. Material* material = model->getMaterial();
  629. if (material)
  630. {
  631. // How to access material parameters? hidden on the Material::RenderState.
  632. std::vector<MaterialParameter*>::iterator itr = material->_parameters.begin();
  633. for (; itr != material->_parameters.end(); itr++)
  634. {
  635. GP_ASSERT(*itr);
  636. animation = ((MaterialParameter*)(*itr))->getAnimation(id);
  637. if (animation)
  638. return animation;
  639. }
  640. }
  641. }
  642. // look through form for animations.
  643. Form* form = this->getForm();
  644. if (form)
  645. {
  646. animation = form->getAnimation(id);
  647. if (animation)
  648. return animation;
  649. }
  650. // Look through this node's children for an animation with the specified ID.
  651. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  652. {
  653. animation = child->getAnimation(id);
  654. if (animation)
  655. return animation;
  656. }
  657. return NULL;
  658. }
  659. Camera* Node::getCamera() const
  660. {
  661. return _camera;
  662. }
  663. void Node::setCamera(Camera* camera)
  664. {
  665. if (_camera != camera)
  666. {
  667. if (_camera)
  668. {
  669. _camera->setNode(NULL);
  670. SAFE_RELEASE(_camera);
  671. }
  672. _camera = camera;
  673. if (_camera)
  674. {
  675. _camera->addRef();
  676. _camera->setNode(this);
  677. }
  678. }
  679. }
  680. Light* Node::getLight() const
  681. {
  682. return _light;
  683. }
  684. void Node::setLight(Light* light)
  685. {
  686. if (_light != light)
  687. {
  688. if (_light)
  689. {
  690. _light->setNode(NULL);
  691. SAFE_RELEASE(_light);
  692. }
  693. _light = light;
  694. if (_light)
  695. {
  696. _light->addRef();
  697. _light->setNode(this);
  698. }
  699. setBoundsDirty();
  700. }
  701. }
  702. Model* Node::getModel() const
  703. {
  704. return _model;
  705. }
  706. void Node::setModel(Model* model)
  707. {
  708. if (_model != model)
  709. {
  710. if (_model)
  711. {
  712. _model->setNode(NULL);
  713. SAFE_RELEASE(_model);
  714. }
  715. _model = model;
  716. if (_model)
  717. {
  718. _model->addRef();
  719. _model->setNode(this);
  720. }
  721. }
  722. }
  723. Terrain* Node::getTerrain() const
  724. {
  725. return _terrain;
  726. }
  727. void Node::setTerrain(Terrain* terrain)
  728. {
  729. if (_terrain != terrain)
  730. {
  731. if (_terrain)
  732. {
  733. _terrain->setNode(NULL);
  734. SAFE_RELEASE(_terrain);
  735. }
  736. _terrain = terrain;
  737. if (_terrain)
  738. {
  739. _terrain->addRef();
  740. _terrain->setNode(this);
  741. }
  742. setBoundsDirty();
  743. }
  744. }
  745. Form* Node::getForm() const
  746. {
  747. return _form;
  748. }
  749. void Node::setForm(Form* form)
  750. {
  751. if (_form != form)
  752. {
  753. if (_form)
  754. {
  755. _form->setNode(NULL);
  756. SAFE_RELEASE(_form);
  757. }
  758. _form = form;
  759. if (_form)
  760. {
  761. _form->addRef();
  762. _form->setNode(this);
  763. }
  764. }
  765. }
  766. const BoundingSphere& Node::getBoundingSphere() const
  767. {
  768. if (_dirtyBits & NODE_DIRTY_BOUNDS)
  769. {
  770. _dirtyBits &= ~NODE_DIRTY_BOUNDS;
  771. const Matrix& worldMatrix = getWorldMatrix();
  772. // Start with our local bounding sphere
  773. // TODO: Incorporate bounds from entities other than mesh (i.e. emitters, audiosource, etc)
  774. bool empty = true;
  775. if (_terrain)
  776. {
  777. _bounds.set(_terrain->getBoundingBox());
  778. empty = false;
  779. }
  780. if (_model && _model->getMesh())
  781. {
  782. if (empty)
  783. {
  784. _bounds.set(_model->getMesh()->getBoundingSphere());
  785. empty = false;
  786. }
  787. else
  788. {
  789. _bounds.merge(_model->getMesh()->getBoundingSphere());
  790. }
  791. }
  792. if (_light)
  793. {
  794. switch (_light->getLightType())
  795. {
  796. case Light::POINT:
  797. if (empty)
  798. {
  799. _bounds.set(Vector3::zero(), _light->getRange());
  800. empty = false;
  801. }
  802. else
  803. {
  804. _bounds.merge(BoundingSphere(Vector3::zero(), _light->getRange()));
  805. }
  806. break;
  807. case Light::SPOT:
  808. // TODO: Implement spot light bounds
  809. break;
  810. }
  811. }
  812. if (empty)
  813. {
  814. // Empty bounding sphere, set the world translation with zero radius
  815. worldMatrix.getTranslation(&_bounds.center);
  816. _bounds.radius = 0;
  817. }
  818. // Transform the sphere (if not empty) into world space.
  819. if (!empty)
  820. {
  821. bool applyWorldTransform = true;
  822. if (_model && _model->getSkin())
  823. {
  824. // Special case: If the root joint of our mesh skin is parented by any nodes,
  825. // multiply the world matrix of the root joint's parent by this node's
  826. // world matrix. This computes a final world matrix used for transforming this
  827. // node's bounding volume. This allows us to store a much smaller bounding
  828. // volume approximation than would otherwise be possible for skinned meshes,
  829. // since joint parent nodes that are not in the matrix palette do not need to
  830. // be considered as directly transforming vertices on the GPU (they can instead
  831. // be applied directly to the bounding volume transformation below).
  832. GP_ASSERT(_model->getSkin()->getRootJoint());
  833. Node* jointParent = _model->getSkin()->getRootJoint()->getParent();
  834. if (jointParent)
  835. {
  836. // TODO: Should we protect against the case where joints are nested directly
  837. // in the node hierachy of the model (this is normally not the case)?
  838. Matrix boundsMatrix;
  839. Matrix::multiply(getWorldMatrix(), jointParent->getWorldMatrix(), &boundsMatrix);
  840. _bounds.transform(boundsMatrix);
  841. applyWorldTransform = false;
  842. }
  843. }
  844. if (applyWorldTransform)
  845. {
  846. _bounds.transform(getWorldMatrix());
  847. }
  848. }
  849. // Merge this world-space bounding sphere with our childrens' bounding volumes.
  850. for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
  851. {
  852. const BoundingSphere& childSphere = n->getBoundingSphere();
  853. if (!childSphere.isEmpty())
  854. {
  855. if (empty)
  856. {
  857. _bounds.set(childSphere);
  858. empty = false;
  859. }
  860. else
  861. {
  862. _bounds.merge(childSphere);
  863. }
  864. }
  865. }
  866. }
  867. return _bounds;
  868. }
  869. Node* Node::clone() const
  870. {
  871. NodeCloneContext context;
  872. return cloneRecursive(context);
  873. }
  874. Node* Node::cloneSingleNode(NodeCloneContext &context) const
  875. {
  876. Node* copy = Node::create(getId());
  877. context.registerClonedNode(this, copy);
  878. cloneInto(copy, context);
  879. return copy;
  880. }
  881. Node* Node::cloneRecursive(NodeCloneContext &context) const
  882. {
  883. Node* copy = cloneSingleNode(context);
  884. GP_ASSERT(copy);
  885. // Add child nodes
  886. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  887. {
  888. Node* childCopy = child->cloneRecursive(context);
  889. GP_ASSERT(childCopy);
  890. copy->addChild(childCopy);
  891. childCopy->release();
  892. }
  893. return copy;
  894. }
  895. void Node::cloneInto(Node* node, NodeCloneContext &context) const
  896. {
  897. GP_ASSERT(node);
  898. Transform::cloneInto(node, context);
  899. // TODO: Clone the rest of the node data.
  900. if (Camera* camera = getCamera())
  901. {
  902. Camera* cameraClone = camera->clone(context);
  903. node->setCamera(cameraClone);
  904. cameraClone->release();
  905. }
  906. if (Light* light = getLight())
  907. {
  908. Light* lightClone = light->clone(context);
  909. node->setLight(lightClone);
  910. lightClone->release();
  911. }
  912. if (AudioSource* audio = getAudioSource())
  913. {
  914. AudioSource* audioClone = audio->clone(context);
  915. node->setAudioSource(audioClone);
  916. audioClone->release();
  917. }
  918. if (Model* model = getModel())
  919. {
  920. Model* modelClone = model->clone(context);
  921. node->setModel(modelClone);
  922. modelClone->release();
  923. }
  924. if (ParticleEmitter* emitter = getParticleEmitter())
  925. {
  926. ParticleEmitter* emitterClone = emitter->clone();
  927. node->setParticleEmitter(emitterClone);
  928. emitterClone->release();
  929. }
  930. node->_world = _world;
  931. node->_bounds = _bounds;
  932. // Note: Do not clone _userData - we can't make any assumptions about its content and how it's managed,
  933. // so it's the caller's responsibility to clone user data if needed.
  934. if (_tags)
  935. {
  936. node->_tags = new std::map<std::string, std::string>(_tags->begin(), _tags->end());
  937. }
  938. }
  939. AudioSource* Node::getAudioSource() const
  940. {
  941. return _audioSource;
  942. }
  943. void Node::setAudioSource(AudioSource* audio)
  944. {
  945. if (_audioSource != audio)
  946. {
  947. if (_audioSource)
  948. {
  949. _audioSource->setNode(NULL);
  950. SAFE_RELEASE(_audioSource);
  951. }
  952. _audioSource = audio;
  953. if (_audioSource)
  954. {
  955. _audioSource->addRef();
  956. _audioSource->setNode(this);
  957. }
  958. }
  959. }
  960. ParticleEmitter* Node::getParticleEmitter() const
  961. {
  962. return _particleEmitter;
  963. }
  964. void Node::setParticleEmitter(ParticleEmitter* emitter)
  965. {
  966. if (_particleEmitter != emitter)
  967. {
  968. if (_particleEmitter)
  969. {
  970. _particleEmitter->setNode(NULL);
  971. SAFE_RELEASE(_particleEmitter);
  972. }
  973. _particleEmitter = emitter;
  974. if (_particleEmitter)
  975. {
  976. _particleEmitter->addRef();
  977. _particleEmitter->setNode(this);
  978. }
  979. }
  980. }
  981. PhysicsCollisionObject* Node::getCollisionObject() const
  982. {
  983. return _collisionObject;
  984. }
  985. PhysicsCollisionObject* Node::setCollisionObject(PhysicsCollisionObject::Type type, const PhysicsCollisionShape::Definition& shape, PhysicsRigidBody::Parameters* rigidBodyParameters, int group, int mask)
  986. {
  987. SAFE_DELETE(_collisionObject);
  988. switch (type)
  989. {
  990. case PhysicsCollisionObject::RIGID_BODY:
  991. {
  992. _collisionObject = new PhysicsRigidBody(this, shape, rigidBodyParameters ? *rigidBodyParameters : PhysicsRigidBody::Parameters(), group, mask);
  993. }
  994. break;
  995. case PhysicsCollisionObject::GHOST_OBJECT:
  996. {
  997. _collisionObject = new PhysicsGhostObject(this, shape, group, mask);
  998. }
  999. break;
  1000. case PhysicsCollisionObject::CHARACTER:
  1001. {
  1002. _collisionObject = new PhysicsCharacter(this, shape, rigidBodyParameters ? rigidBodyParameters->mass : 1.0f);
  1003. }
  1004. break;
  1005. case PhysicsCollisionObject::VEHICLE:
  1006. {
  1007. _collisionObject = new PhysicsVehicle(this, shape, rigidBodyParameters ? *rigidBodyParameters : PhysicsRigidBody::Parameters());
  1008. }
  1009. break;
  1010. case PhysicsCollisionObject::VEHICLE_WHEEL:
  1011. {
  1012. //
  1013. // PhysicsVehicleWheel is special because this call will traverse up the scene graph for the
  1014. // first ancestor node that is shared with another node of collision type VEHICLE, and then
  1015. // proceed to add itself as a wheel onto that vehicle. This is by design, and allows the
  1016. // visual scene hierarchy to be the sole representation of the relationship between physics
  1017. // objects rather than forcing that upon the otherwise-flat ".physics" (properties) file.
  1018. //
  1019. // IMPORTANT: The VEHICLE must come before the VEHICLE_WHEEL in the ".scene" (properties) file!
  1020. //
  1021. _collisionObject = new PhysicsVehicleWheel(this, shape, rigidBodyParameters ? *rigidBodyParameters : PhysicsRigidBody::Parameters());
  1022. }
  1023. break;
  1024. case PhysicsCollisionObject::NONE:
  1025. break; // Already deleted, Just don't add a new collision object back.
  1026. }
  1027. return _collisionObject;
  1028. }
  1029. PhysicsCollisionObject* Node::setCollisionObject(const char* url)
  1030. {
  1031. // Load the collision object properties from file.
  1032. Properties* properties = Properties::create(url);
  1033. if (properties == NULL)
  1034. {
  1035. GP_ERROR("Failed to load collision object file: %s", url);
  1036. return NULL;
  1037. }
  1038. PhysicsCollisionObject* collisionObject = setCollisionObject((strlen(properties->getNamespace()) > 0) ? properties : properties->getNextNamespace());
  1039. SAFE_DELETE(properties);
  1040. return collisionObject;
  1041. }
  1042. PhysicsCollisionObject* Node::setCollisionObject(Properties* properties)
  1043. {
  1044. SAFE_DELETE(_collisionObject);
  1045. // Check if the properties is valid.
  1046. if (!properties || !(strcmp(properties->getNamespace(), "collisionObject") == 0))
  1047. {
  1048. GP_ERROR("Failed to load collision object from properties object: must be non-null object and have namespace equal to 'collisionObject'.");
  1049. return NULL;
  1050. }
  1051. if (const char* type = properties->getString("type"))
  1052. {
  1053. if (strcmp(type, "CHARACTER") == 0)
  1054. {
  1055. _collisionObject = PhysicsCharacter::create(this, properties);
  1056. }
  1057. else if (strcmp(type, "GHOST_OBJECT") == 0)
  1058. {
  1059. _collisionObject = PhysicsGhostObject::create(this, properties);
  1060. }
  1061. else if (strcmp(type, "RIGID_BODY") == 0)
  1062. {
  1063. _collisionObject = PhysicsRigidBody::create(this, properties);
  1064. }
  1065. else if (strcmp(type, "VEHICLE") == 0)
  1066. {
  1067. _collisionObject = PhysicsVehicle::create(this, properties);
  1068. }
  1069. else if (strcmp(type, "VEHICLE_WHEEL") == 0)
  1070. {
  1071. //
  1072. // PhysicsVehicleWheel is special because this call will traverse up the scene graph for the
  1073. // first ancestor node that is shared with another node of collision type VEHICLE, and then
  1074. // proceed to add itself as a wheel onto that vehicle. This is by design, and allows the
  1075. // visual scene hierarchy to be the sole representation of the relationship between physics
  1076. // objects rather than forcing that upon the otherwise-flat ".physics" (properties) file.
  1077. //
  1078. // IMPORTANT: The VEHICLE must come before the VEHICLE_WHEEL in the ".scene" (properties) file!
  1079. //
  1080. _collisionObject = PhysicsVehicleWheel::create(this, properties);
  1081. }
  1082. else
  1083. {
  1084. GP_ERROR("Unsupported collision object type '%s'.", type);
  1085. return NULL;
  1086. }
  1087. }
  1088. else
  1089. {
  1090. GP_ERROR("Failed to load collision object from properties object; required attribute 'type' is missing.");
  1091. return NULL;
  1092. }
  1093. return _collisionObject;
  1094. }
  1095. AIAgent* Node::getAgent() const
  1096. {
  1097. return _agent;
  1098. }
  1099. void Node::setAgent(AIAgent* agent)
  1100. {
  1101. if (agent != _agent)
  1102. {
  1103. if (_agent)
  1104. {
  1105. Game::getInstance()->getAIController()->removeAgent(_agent);
  1106. _agent->_node = NULL;
  1107. SAFE_RELEASE(_agent);
  1108. }
  1109. _agent = agent;
  1110. if (_agent)
  1111. {
  1112. _agent->addRef();
  1113. _agent->_node = this;
  1114. Game::getInstance()->getAIController()->addAgent(_agent);
  1115. }
  1116. }
  1117. }
  1118. NodeCloneContext::NodeCloneContext()
  1119. {
  1120. }
  1121. NodeCloneContext::~NodeCloneContext()
  1122. {
  1123. }
  1124. Animation* NodeCloneContext::findClonedAnimation(const Animation* animation)
  1125. {
  1126. GP_ASSERT(animation);
  1127. std::map<const Animation*, Animation*>::iterator it = _clonedAnimations.find(animation);
  1128. return it != _clonedAnimations.end() ? it->second : NULL;
  1129. }
  1130. void NodeCloneContext::registerClonedAnimation(const Animation* original, Animation* clone)
  1131. {
  1132. GP_ASSERT(original);
  1133. GP_ASSERT(clone);
  1134. _clonedAnimations[original] = clone;
  1135. }
  1136. Node* NodeCloneContext::findClonedNode(const Node* node)
  1137. {
  1138. GP_ASSERT(node);
  1139. std::map<const Node*, Node*>::iterator it = _clonedNodes.find(node);
  1140. return it != _clonedNodes.end() ? it->second : NULL;
  1141. }
  1142. void NodeCloneContext::registerClonedNode(const Node* original, Node* clone)
  1143. {
  1144. GP_ASSERT(original);
  1145. GP_ASSERT(clone);
  1146. _clonedNodes[original] = clone;
  1147. }
  1148. }