Node.cpp 34 KB

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