2
0

Node.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. #include "Base.h"
  2. #include "AudioSource.h"
  3. #include "Node.h"
  4. #include "Scene.h"
  5. #include "Joint.h"
  6. #include "PhysicsRigidBody.h"
  7. #include "PhysicsGhostObject.h"
  8. #include "PhysicsCharacter.h"
  9. #include "Game.h"
  10. // Node dirty flags
  11. #define NODE_DIRTY_WORLD 1
  12. #define NODE_DIRTY_BOUNDS 2
  13. #define NODE_DIRTY_ALL (NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS)
  14. namespace gameplay
  15. {
  16. Node::Node(const char* id)
  17. : _scene(NULL), _firstChild(NULL), _nextSibling(NULL), _prevSibling(NULL), _parent(NULL), _childCount(0),
  18. _tags(NULL), _camera(NULL), _light(NULL), _model(NULL), _form(NULL), _audioSource(NULL), _particleEmitter(NULL),
  19. _collisionObject(NULL), _agent(NULL), _dirtyBits(NODE_DIRTY_ALL), _notifyHierarchyChanged(true), _userData(NULL)
  20. {
  21. if (id)
  22. {
  23. _id = id;
  24. }
  25. }
  26. Node::~Node()
  27. {
  28. removeAllChildren();
  29. if (_model)
  30. _model->setNode(NULL);
  31. if (_audioSource)
  32. _audioSource->setNode(NULL);
  33. if (_particleEmitter)
  34. _particleEmitter->setNode(NULL);
  35. if (_form)
  36. _form->setNode(NULL);
  37. SAFE_RELEASE(_camera);
  38. SAFE_RELEASE(_light);
  39. SAFE_RELEASE(_model);
  40. SAFE_RELEASE(_audioSource);
  41. SAFE_RELEASE(_particleEmitter);
  42. SAFE_RELEASE(_form);
  43. SAFE_DELETE(_collisionObject);
  44. SAFE_DELETE(_tags);
  45. setAgent(NULL);
  46. // Cleanup user data
  47. if (_userData)
  48. {
  49. // Call custom cleanup callback if specified
  50. if (_userData->cleanupCallback)
  51. _userData->cleanupCallback(_userData->pointer);
  52. SAFE_DELETE(_userData);
  53. }
  54. }
  55. Node* Node::create(const char* id)
  56. {
  57. return new Node(id);
  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. // Order is irrelevant, so add to the beginning of the list.
  93. if (_firstChild)
  94. {
  95. _firstChild->_prevSibling = child;
  96. child->_nextSibling = _firstChild;
  97. _firstChild = child;
  98. }
  99. else
  100. {
  101. _firstChild = child;
  102. }
  103. child->_parent = this;
  104. ++_childCount;
  105. if (_notifyHierarchyChanged)
  106. {
  107. hierarchyChanged();
  108. }
  109. }
  110. void Node::removeChild(Node* child)
  111. {
  112. if (child == NULL || child->_parent != this)
  113. {
  114. // The child is not in our hierarchy.
  115. return;
  116. }
  117. // Call remove on the child.
  118. child->remove();
  119. SAFE_RELEASE(child);
  120. }
  121. void Node::removeAllChildren()
  122. {
  123. _notifyHierarchyChanged = false;
  124. while (_firstChild)
  125. {
  126. removeChild(_firstChild);
  127. }
  128. _notifyHierarchyChanged = true;
  129. hierarchyChanged();
  130. }
  131. void Node::remove()
  132. {
  133. // Re-link our neighbours.
  134. if (_prevSibling)
  135. {
  136. _prevSibling->_nextSibling = _nextSibling;
  137. }
  138. if (_nextSibling)
  139. {
  140. _nextSibling->_prevSibling = _prevSibling;
  141. }
  142. // Update our parent.
  143. Node* parent = _parent;
  144. if (parent)
  145. {
  146. if (this == parent->_firstChild)
  147. {
  148. parent->_firstChild = _nextSibling;
  149. }
  150. --parent->_childCount;
  151. }
  152. _nextSibling = NULL;
  153. _prevSibling = NULL;
  154. _parent = NULL;
  155. if (parent && parent->_notifyHierarchyChanged)
  156. {
  157. parent->hierarchyChanged();
  158. }
  159. }
  160. Node* Node::getFirstChild() const
  161. {
  162. return _firstChild;
  163. }
  164. Node* Node::getNextSibling() const
  165. {
  166. return _nextSibling;
  167. }
  168. Node* Node::getPreviousSibling() const
  169. {
  170. return _prevSibling;
  171. }
  172. Node* Node::getParent() const
  173. {
  174. return _parent;
  175. }
  176. bool Node::hasTag(const char* name) const
  177. {
  178. GP_ASSERT(name);
  179. return (_tags ? _tags->find(name) != _tags->end() : false);
  180. }
  181. const char* Node::getTag(const char* name) const
  182. {
  183. GP_ASSERT(name);
  184. if (!_tags)
  185. return NULL;
  186. std::map<std::string, std::string>::const_iterator itr = _tags->find(name);
  187. return (itr == _tags->end() ? NULL : itr->second.c_str());
  188. }
  189. void Node::setTag(const char* name, const char* value)
  190. {
  191. GP_ASSERT(name);
  192. if (value == NULL)
  193. {
  194. // Removing tag
  195. if (_tags)
  196. {
  197. _tags->erase(name);
  198. if (_tags->size() == 0)
  199. SAFE_DELETE(_tags);
  200. }
  201. }
  202. else
  203. {
  204. // Setting tag
  205. if (_tags == NULL)
  206. _tags = new std::map<std::string, std::string>();
  207. (*_tags)[name] = value;
  208. }
  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. AIAgent* Node::getAgent() const
  942. {
  943. return _agent;
  944. }
  945. void Node::setAgent(AIAgent* agent)
  946. {
  947. if (agent != _agent)
  948. {
  949. if (_agent)
  950. {
  951. Game::getInstance()->getAIController()->removeAgent(_agent);
  952. _agent->_node = NULL;
  953. SAFE_RELEASE(_agent);
  954. }
  955. _agent = agent;
  956. if (_agent)
  957. {
  958. _agent->addRef();
  959. _agent->_node = this;
  960. Game::getInstance()->getAIController()->addAgent(_agent);
  961. }
  962. }
  963. }
  964. NodeCloneContext::NodeCloneContext()
  965. {
  966. }
  967. NodeCloneContext::~NodeCloneContext()
  968. {
  969. }
  970. Animation* NodeCloneContext::findClonedAnimation(const Animation* animation)
  971. {
  972. GP_ASSERT(animation);
  973. std::map<const Animation*, Animation*>::iterator it = _clonedAnimations.find(animation);
  974. return it != _clonedAnimations.end() ? it->second : NULL;
  975. }
  976. void NodeCloneContext::registerClonedAnimation(const Animation* original, Animation* clone)
  977. {
  978. GP_ASSERT(original);
  979. GP_ASSERT(clone);
  980. _clonedAnimations[original] = clone;
  981. }
  982. Node* NodeCloneContext::findClonedNode(const Node* node)
  983. {
  984. GP_ASSERT(node);
  985. std::map<const Node*, Node*>::iterator it = _clonedNodes.find(node);
  986. return it != _clonedNodes.end() ? it->second : NULL;
  987. }
  988. void NodeCloneContext::registerClonedNode(const Node* original, Node* clone)
  989. {
  990. GP_ASSERT(original);
  991. GP_ASSERT(clone);
  992. _clonedNodes[original] = clone;
  993. }
  994. }