Node.cpp 32 KB

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