Node.cpp 17 KB

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