Node.cpp 28 KB

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