Node.cpp 31 KB

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