Node.cpp 18 KB

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