Node.cpp 31 KB

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