Node.cpp 34 KB

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