Node.cpp 19 KB

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