Node.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. #include "Base.h"
  2. #include "Node.h"
  3. #include "Scene.h"
  4. #include "Joint.h"
  5. #define NODE_DIRTY_WORLD 1
  6. #define NODE_DIRTY_BOUNDS 2
  7. #define NODE_DIRTY_ALL (NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS)
  8. namespace gameplay
  9. {
  10. Node::Node(const char* id)
  11. : _scene(NULL), _firstChild(NULL), _nextSibling(NULL), _prevSibling(NULL), _parent(NULL), _childCount(NULL),
  12. _camera(NULL), _light(NULL), _model(NULL), _form(NULL), _audioSource(NULL), _particleEmitter(NULL), _physicsRigidBody(NULL),
  13. _dirtyBits(NODE_DIRTY_ALL), _notifyHierarchyChanged(true)
  14. {
  15. if (id)
  16. {
  17. _id = id;
  18. }
  19. }
  20. Node::Node(const Node& node)
  21. {
  22. // hidden
  23. }
  24. Node::~Node()
  25. {
  26. removeAllChildren();
  27. if (_model)
  28. _model->setNode(NULL);
  29. if (_audioSource)
  30. _audioSource->setNode(NULL);
  31. if (_particleEmitter)
  32. _particleEmitter->setNode(NULL);
  33. SAFE_RELEASE(_camera);
  34. SAFE_RELEASE(_light);
  35. SAFE_RELEASE(_model);
  36. SAFE_RELEASE(_audioSource);
  37. SAFE_RELEASE(_particleEmitter);
  38. SAFE_DELETE(_physicsRigidBody);
  39. }
  40. Node* Node::create(const char* id)
  41. {
  42. return new Node(id);
  43. }
  44. const char* Node::getId() const
  45. {
  46. return _id.c_str();
  47. }
  48. void Node::setId(const char* id)
  49. {
  50. if (id)
  51. {
  52. _id = id;
  53. }
  54. }
  55. Node::Type Node::getType() const
  56. {
  57. return Node::NODE;
  58. }
  59. void Node::addChild(Node* child)
  60. {
  61. assert(child);
  62. if (child->_parent == this)
  63. {
  64. // This node is already present in our hierarchy
  65. return;
  66. }
  67. child->addRef();
  68. // If the item belongs to another hierarchy, remove it first.
  69. if (child->_parent)
  70. {
  71. child->_parent->removeChild(child);
  72. }
  73. else if (child->_scene)
  74. {
  75. child->_scene->removeNode(child);
  76. }
  77. // Order is irrelevant, so add to the beginning of the list.
  78. if (_firstChild)
  79. {
  80. _firstChild->_prevSibling = child;
  81. child->_nextSibling = _firstChild;
  82. _firstChild = child;
  83. }
  84. else
  85. {
  86. _firstChild = child;
  87. }
  88. child->_parent = this;
  89. ++_childCount;
  90. if (_notifyHierarchyChanged)
  91. {
  92. hierarchyChanged();
  93. }
  94. }
  95. void Node::removeChild(Node* child)
  96. {
  97. if (child == NULL || child->_parent != this)
  98. {
  99. // The child is not in our hierarchy.
  100. return;
  101. }
  102. // Call remove on the child.
  103. child->remove();
  104. SAFE_RELEASE(child);
  105. }
  106. void Node::removeAllChildren()
  107. {
  108. _notifyHierarchyChanged = false;
  109. while (_firstChild)
  110. {
  111. removeChild(_firstChild);
  112. }
  113. _notifyHierarchyChanged = true;
  114. hierarchyChanged();
  115. }
  116. void Node::remove()
  117. {
  118. // Re-link our neighbours.
  119. if (_prevSibling)
  120. {
  121. _prevSibling->_nextSibling = _nextSibling;
  122. }
  123. if (_nextSibling)
  124. {
  125. _nextSibling->_prevSibling = _prevSibling;
  126. }
  127. // Update our parent.
  128. Node* parent = _parent;
  129. if (parent)
  130. {
  131. if (this == parent->_firstChild)
  132. {
  133. parent->_firstChild = _nextSibling;
  134. }
  135. --parent->_childCount;
  136. }
  137. _nextSibling = NULL;
  138. _prevSibling = NULL;
  139. _parent = NULL;
  140. if (parent && parent->_notifyHierarchyChanged)
  141. {
  142. parent->hierarchyChanged();
  143. }
  144. }
  145. Node* Node::getFirstChild() const
  146. {
  147. return _firstChild;
  148. }
  149. Node* Node::getNextSibling() const
  150. {
  151. return _nextSibling;
  152. }
  153. Node* Node::getPreviousSibling() const
  154. {
  155. return _prevSibling;
  156. }
  157. Node* Node::getParent() const
  158. {
  159. return _parent;
  160. }
  161. unsigned int Node::getChildCount() const
  162. {
  163. return _childCount;
  164. }
  165. Node* Node::findNode(const char* id, bool recursive, bool exactMatch)
  166. {
  167. assert(id);
  168. // Search immediate children first.
  169. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  170. {
  171. // Does this child's ID match?
  172. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  173. {
  174. return child;
  175. }
  176. }
  177. // Recurse.
  178. if (recursive)
  179. {
  180. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  181. {
  182. Node* match = child->findNode(id, true, exactMatch);
  183. if (match)
  184. {
  185. return match;
  186. }
  187. }
  188. }
  189. return NULL;
  190. }
  191. unsigned int Node::findNodes(const char* id, std::vector<Node*>& nodes, bool recursive, bool exactMatch)
  192. {
  193. assert(id);
  194. unsigned int count = 0;
  195. // Search immediate children first.
  196. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  197. {
  198. // Does this child's ID match?
  199. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  200. {
  201. nodes.push_back(child);
  202. ++count;
  203. }
  204. }
  205. // Recurse.
  206. if (recursive)
  207. {
  208. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  209. {
  210. count += child->findNodes(id, nodes, true, exactMatch);
  211. }
  212. }
  213. return count;
  214. }
  215. Scene* Node::getScene() const
  216. {
  217. // Search for a scene in our parents.
  218. for (Node* n = const_cast<Node*>(this); n != NULL; n = n->getParent())
  219. {
  220. if (n->_scene)
  221. {
  222. return n->_scene;
  223. }
  224. }
  225. return NULL;
  226. }
  227. Node* Node::getRootNode() const
  228. {
  229. Node* n = const_cast<Node*>(this);
  230. while (n->getParent())
  231. {
  232. n = n->getParent();
  233. }
  234. return n;
  235. }
  236. const Matrix& Node::getWorldMatrix() const
  237. {
  238. if (_dirtyBits & NODE_DIRTY_WORLD)
  239. {
  240. // Clear our dirty flag immediately to prevent this block from being entered if our
  241. // parent calls our getWorldMatrix() method as a result of the following calculations.
  242. _dirtyBits &= ~NODE_DIRTY_WORLD;
  243. // If we have a parent, multiply our parent world transform by our local
  244. // transform to obtain our final resolved world transform.
  245. Node* parent = getParent();
  246. if (parent && (!_physicsRigidBody || _physicsRigidBody->isKinematic()) )
  247. {
  248. Matrix::multiply(parent->getWorldMatrix(), getMatrix(), &_world);
  249. }
  250. else
  251. {
  252. _world = getMatrix();
  253. }
  254. // Our world matrix was just updated, so call getWorldMatrix() on all child nodes
  255. // to force their resolved world matrices to be updated.
  256. Node* node = getFirstChild();
  257. while (node)
  258. {
  259. node->getWorldMatrix();
  260. node = node->getNextSibling();
  261. }
  262. }
  263. return _world;
  264. }
  265. const Matrix& Node::getWorldViewMatrix() const
  266. {
  267. static Matrix worldView;
  268. Matrix::multiply(getViewMatrix(), getWorldMatrix(), &worldView);
  269. return worldView;
  270. }
  271. const Matrix& Node::getInverseTransposeWorldViewMatrix() const
  272. {
  273. static Matrix invTransWorldView;
  274. Matrix::multiply(getViewMatrix(), getWorldMatrix(), &invTransWorldView);
  275. invTransWorldView.invert();
  276. invTransWorldView.transpose();
  277. return invTransWorldView;
  278. }
  279. const Matrix& Node::getInverseTransposeWorldMatrix() const
  280. {
  281. static Matrix invTransWorld;
  282. invTransWorld = getWorldMatrix();
  283. invTransWorld.invert();
  284. invTransWorld.transpose();
  285. return invTransWorld;
  286. }
  287. const Matrix& Node::getViewMatrix() const
  288. {
  289. Scene* scene = getScene();
  290. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  291. if (camera)
  292. {
  293. return camera->getViewMatrix();
  294. }
  295. else
  296. {
  297. return Matrix::identity();
  298. }
  299. }
  300. const Matrix& Node::getInverseViewMatrix() const
  301. {
  302. Scene* scene = getScene();
  303. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  304. if (camera)
  305. {
  306. return camera->getInverseViewMatrix();
  307. }
  308. else
  309. {
  310. return Matrix::identity();
  311. }
  312. }
  313. const Matrix& Node::getProjectionMatrix() const
  314. {
  315. Scene* scene = getScene();
  316. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  317. if (camera)
  318. {
  319. return camera->getProjectionMatrix();
  320. }
  321. else
  322. {
  323. return Matrix::identity();
  324. }
  325. }
  326. const Matrix& Node::getViewProjectionMatrix() const
  327. {
  328. Scene* scene = getScene();
  329. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  330. if (camera)
  331. {
  332. return camera->getViewProjectionMatrix();
  333. }
  334. else
  335. {
  336. return Matrix::identity();
  337. }
  338. }
  339. const Matrix& Node::getInverseViewProjectionMatrix() const
  340. {
  341. Scene* scene = getScene();
  342. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  343. if (camera)
  344. {
  345. return camera->getInverseViewProjectionMatrix();
  346. }
  347. return Matrix::identity();
  348. }
  349. const Matrix& Node::getWorldViewProjectionMatrix() const
  350. {
  351. static Matrix worldViewProj;
  352. // Always re-calculate worldViewProjection matrix since it's extremely difficult
  353. // to track whether the camera has changed (it may frequently change every frame).
  354. Matrix::multiply(getViewProjectionMatrix(), getWorldMatrix(), &worldViewProj);
  355. return worldViewProj;
  356. }
  357. Vector3 Node::getTranslationWorld() const
  358. {
  359. Vector3 translation;
  360. getWorldMatrix().getTranslation(&translation);
  361. return translation;
  362. }
  363. Vector3 Node::getTranslationView() const
  364. {
  365. Vector3 translation;
  366. getWorldMatrix().getTranslation(&translation);
  367. getViewMatrix().transformPoint(&translation);
  368. return translation;
  369. }
  370. Vector3 Node::getForwardVectorWorld() const
  371. {
  372. Vector3 vector;
  373. getWorldMatrix().getForwardVector(&vector);
  374. return vector;
  375. }
  376. Vector3 Node::getForwardVectorView() const
  377. {
  378. Vector3 vector;
  379. getWorldMatrix().getForwardVector(&vector);
  380. getViewMatrix().transformVector(&vector);
  381. //getForwardVector(&vector);
  382. //getWorldViewMatrix().transformVector(&vector);
  383. return vector;
  384. }
  385. Vector3 Node::getActiveCameraTranslationWorld() const
  386. {
  387. Scene* scene = getScene();
  388. if (scene)
  389. {
  390. Camera* camera = scene->getActiveCamera();
  391. if (camera)
  392. {
  393. Node* cameraNode = camera->getNode();
  394. if (cameraNode)
  395. {
  396. return cameraNode->getTranslationWorld();
  397. }
  398. }
  399. }
  400. return Vector3::zero();
  401. }
  402. Vector3 Node::getActiveCameraTranslationView() const
  403. {
  404. Scene* scene = getScene();
  405. if (scene)
  406. {
  407. Camera* camera = scene->getActiveCamera();
  408. if (camera)
  409. {
  410. Node* cameraNode = camera->getNode();
  411. if (cameraNode)
  412. {
  413. return cameraNode->getTranslationView();
  414. }
  415. }
  416. }
  417. return Vector3::zero();
  418. }
  419. void Node::hierarchyChanged()
  420. {
  421. // When our hierarchy changes our world transform is affected, so we must dirty it.
  422. transformChanged();
  423. }
  424. void Node::transformChanged()
  425. {
  426. // Our local transform was changed, so mark our world matrices dirty.
  427. _dirtyBits |= NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS;
  428. // Notify our children that their transform has also changed (since transforms are inherited).
  429. Joint* rootJoint = NULL;
  430. Node* n = getFirstChild();
  431. while (n)
  432. {
  433. n->transformChanged();
  434. n = n->getNextSibling();
  435. }
  436. Transform::transformChanged();
  437. }
  438. void Node::setBoundsDirty()
  439. {
  440. // Mark ourself and our parent nodes as dirty
  441. _dirtyBits |= NODE_DIRTY_BOUNDS;
  442. // Mark our parent bounds as dirty as well
  443. if (_parent)
  444. _parent->setBoundsDirty();
  445. }
  446. Camera* Node::getCamera() const
  447. {
  448. return _camera;
  449. }
  450. void Node::setCamera(Camera* camera)
  451. {
  452. if (_camera != camera)
  453. {
  454. if (_camera)
  455. {
  456. _camera->setNode(NULL);
  457. SAFE_RELEASE(_camera);
  458. }
  459. _camera = camera;
  460. if (_camera)
  461. {
  462. _camera->addRef();
  463. _camera->setNode(this);
  464. }
  465. }
  466. }
  467. Light* Node::getLight() const
  468. {
  469. return _light;
  470. }
  471. void Node::setLight(Light* light)
  472. {
  473. if (_light != light)
  474. {
  475. if (_light)
  476. {
  477. _light->setNode(NULL);
  478. SAFE_RELEASE(_light);
  479. }
  480. _light = light;
  481. if (_light)
  482. {
  483. _light->addRef();
  484. _light->setNode(this);
  485. }
  486. }
  487. }
  488. void Node::setModel(Model* model)
  489. {
  490. if (_model != model)
  491. {
  492. if (_model)
  493. {
  494. _model->setNode(NULL);
  495. SAFE_RELEASE(_model);
  496. }
  497. _model = model;
  498. if (_model)
  499. {
  500. _model->addRef();
  501. _model->setNode(this);
  502. }
  503. }
  504. }
  505. Model* Node::getModel() const
  506. {
  507. return _model;
  508. }
  509. void Node::setForm(Form* form)
  510. {
  511. if (_form != form)
  512. {
  513. if (_form)
  514. {
  515. _form->setNode(NULL);
  516. SAFE_RELEASE(_form);
  517. }
  518. _form = form;
  519. if (_form)
  520. {
  521. _form->addRef();
  522. _form->setNode(this);
  523. }
  524. }
  525. }
  526. Form* Node::getForm() const
  527. {
  528. return _form;
  529. }
  530. const BoundingSphere& Node::getBoundingSphere() const
  531. {
  532. if (_dirtyBits & NODE_DIRTY_BOUNDS)
  533. {
  534. _dirtyBits &= ~NODE_DIRTY_BOUNDS;
  535. const Matrix& worldMatrix = getWorldMatrix();
  536. // Start with our local bounding sphere
  537. // TODO: Incorporate bounds from entities other than mesh (i.e. emitters, audiosource, etc)
  538. bool empty = true;
  539. if (_model && _model->getMesh())
  540. {
  541. _bounds.set(_model->getMesh()->getBoundingSphere());
  542. empty = false;
  543. }
  544. else
  545. {
  546. // Empty bounding sphere, set the world translation with zero radius
  547. worldMatrix.getTranslation(&_bounds.center);
  548. _bounds.radius = 0;
  549. }
  550. // Transform the sphere (if not empty) into world space.
  551. if (!empty)
  552. {
  553. bool applyWorldTransform = true;
  554. if (_model && _model->getSkin())
  555. {
  556. // Special case: If the root joint of our mesh skin is parented by any nodes,
  557. // multiply the world matrix of the root joint's parent by this node's
  558. // world matrix. This computes a final world matrix used for transforming this
  559. // node's bounding volume. This allows us to store a much smaller bounding
  560. // volume approximation than would otherwise be possible for skinned meshes,
  561. // since joint parent nodes that are not in the matrix pallette do not need to
  562. // be considered as directly transforming vertices on the GPU (they can instead
  563. // be applied directly to the bounding volume transformation below).
  564. Node* jointParent = _model->getSkin()->getRootJoint()->getParent();
  565. if (jointParent)
  566. {
  567. // TODO: Should we protect against the case where joints are nested directly
  568. // in the node hierachy of the model (this is normally not the case)?
  569. Matrix boundsMatrix;
  570. Matrix::multiply(getWorldMatrix(), jointParent->getWorldMatrix(), &boundsMatrix);
  571. _bounds.transform(boundsMatrix);
  572. applyWorldTransform = false;
  573. }
  574. }
  575. if (applyWorldTransform)
  576. {
  577. _bounds.transform(getWorldMatrix());
  578. }
  579. }
  580. // Merge this world-space bounding sphere with our childrens' bounding volumes.
  581. for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
  582. {
  583. const BoundingSphere& childSphere = n->getBoundingSphere();
  584. if (!childSphere.isEmpty())
  585. {
  586. if (empty)
  587. {
  588. _bounds.set(childSphere);
  589. empty = false;
  590. }
  591. else
  592. {
  593. _bounds.merge(childSphere);
  594. }
  595. }
  596. }
  597. }
  598. return _bounds;
  599. }
  600. AudioSource* Node::getAudioSource() const
  601. {
  602. return _audioSource;
  603. }
  604. void Node::setAudioSource(AudioSource* audio)
  605. {
  606. if (_audioSource != audio)
  607. {
  608. if (_audioSource)
  609. {
  610. _audioSource->setNode(NULL);
  611. SAFE_RELEASE(_audioSource);
  612. }
  613. _audioSource = audio;
  614. if (_audioSource)
  615. {
  616. _audioSource->addRef();
  617. _audioSource->setNode(this);
  618. }
  619. }
  620. }
  621. ParticleEmitter* Node::getParticleEmitter() const
  622. {
  623. return _particleEmitter;
  624. }
  625. void Node::setParticleEmitter(ParticleEmitter* emitter)
  626. {
  627. if (_particleEmitter != emitter)
  628. {
  629. if (_particleEmitter)
  630. {
  631. _particleEmitter->setNode(NULL);
  632. SAFE_RELEASE(_particleEmitter);
  633. }
  634. _particleEmitter = emitter;
  635. if (_particleEmitter)
  636. {
  637. _particleEmitter->addRef();
  638. _particleEmitter->setNode(this);
  639. }
  640. }
  641. }
  642. PhysicsRigidBody* Node::getRigidBody() const
  643. {
  644. return _physicsRigidBody;
  645. }
  646. void Node::setRigidBody(PhysicsRigidBody::Type type, float mass, float friction,
  647. float restitution, float linearDamping, float angularDamping)
  648. {
  649. SAFE_DELETE(_physicsRigidBody);
  650. if (type != PhysicsRigidBody::SHAPE_NONE)
  651. _physicsRigidBody = new PhysicsRigidBody(this, type, mass, friction, restitution, linearDamping, angularDamping);
  652. }
  653. void Node::setRigidBody(const char* filePath)
  654. {
  655. SAFE_DELETE(_physicsRigidBody);
  656. _physicsRigidBody = PhysicsRigidBody::create(this, filePath);
  657. }
  658. void Node::setRigidBody(Properties* properties)
  659. {
  660. SAFE_DELETE(_physicsRigidBody);
  661. _physicsRigidBody = PhysicsRigidBody::create(this, properties);
  662. }
  663. }