Node.cpp 33 KB

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