Node.cpp 31 KB

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