Node.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. * Node.cpp
  3. */
  4. #include "Base.h"
  5. #include "Node.h"
  6. #include "Scene.h"
  7. #define NODE_DIRTY_WORLD 1
  8. #define NODE_DIRTY_BOUNDS 2
  9. #define NODE_DIRTY_ALL (NODE_DIRTY_WORLD | NODE_DIRTY_BOUNDS)
  10. namespace gameplay
  11. {
  12. Node::Node(const char* id)
  13. : _scene(NULL), _firstChild(NULL), _nextSibling(NULL), _prevSibling(NULL), _parent(NULL), _childCount(NULL),
  14. _camera(NULL), _light(NULL), _model(NULL), _audioSource(NULL), _particleEmitter(NULL), _physicsRigidBody(NULL),
  15. _dirtyBits(NODE_DIRTY_ALL), _notifyHierarchyChanged(true), _boundsType(NONE)
  16. {
  17. if (id)
  18. {
  19. _id = id;
  20. }
  21. memset(&_bounds, 0, sizeof(_bounds));
  22. }
  23. Node::Node(const Node& node)
  24. {
  25. // hidden
  26. }
  27. Node::~Node()
  28. {
  29. removeAllChildren();
  30. // Free bounding volume.
  31. switch (_boundsType)
  32. {
  33. case BOX:
  34. SAFE_DELETE(_bounds.box);
  35. break;
  36. case SPHERE:
  37. SAFE_DELETE(_bounds.sphere);
  38. break;
  39. }
  40. SAFE_RELEASE(_camera);
  41. SAFE_RELEASE(_light);
  42. SAFE_RELEASE(_model);
  43. SAFE_RELEASE(_audioSource);
  44. SAFE_RELEASE(_particleEmitter);
  45. SAFE_DELETE(_physicsRigidBody);
  46. }
  47. Node* Node::create(const char* id)
  48. {
  49. return new Node(id);
  50. }
  51. const char* Node::getId() const
  52. {
  53. return _id.c_str();
  54. }
  55. void Node::setId(const char* id)
  56. {
  57. if (id)
  58. {
  59. _id = id;
  60. }
  61. }
  62. Node::Type Node::getType() const
  63. {
  64. return Node::NODE;
  65. }
  66. void Node::addChild(Node* child)
  67. {
  68. assert(child);
  69. if (child->_parent == this)
  70. {
  71. // This node is already present in our hierarchy
  72. return;
  73. }
  74. child->addRef();
  75. // If the item belongs to another hierarchy, remove it first.
  76. if (child->_parent)
  77. {
  78. child->_parent->removeChild(child);
  79. }
  80. else if (child->_scene)
  81. {
  82. child->_scene->removeNode(child);
  83. }
  84. // Order is irrelevant, so add to the beginning of the list.
  85. if (_firstChild)
  86. {
  87. _firstChild->_prevSibling = child;
  88. child->_nextSibling = _firstChild;
  89. _firstChild = child;
  90. }
  91. else
  92. {
  93. _firstChild = child;
  94. }
  95. child->_parent = this;
  96. ++_childCount;
  97. if (_notifyHierarchyChanged)
  98. {
  99. hierarchyChanged();
  100. }
  101. }
  102. void Node::removeChild(Node* child)
  103. {
  104. if (child == NULL || child->_parent != this)
  105. {
  106. // The child is not in our hierarchy.
  107. return;
  108. }
  109. // Call remove on the child.
  110. child->remove();
  111. SAFE_RELEASE(child);
  112. }
  113. void Node::removeAllChildren()
  114. {
  115. _notifyHierarchyChanged = false;
  116. while (_firstChild)
  117. {
  118. removeChild(_firstChild);
  119. }
  120. _notifyHierarchyChanged = true;
  121. hierarchyChanged();
  122. }
  123. void Node::remove()
  124. {
  125. // Re-link our neighbours.
  126. if (_prevSibling)
  127. {
  128. _prevSibling->_nextSibling = _nextSibling;
  129. }
  130. if (_nextSibling)
  131. {
  132. _nextSibling->_prevSibling = _prevSibling;
  133. }
  134. // Update our parent.
  135. Node* parent = _parent;
  136. if (parent)
  137. {
  138. if (this == parent->_firstChild)
  139. {
  140. parent->_firstChild = _nextSibling;
  141. }
  142. --parent->_childCount;
  143. }
  144. _nextSibling = NULL;
  145. _prevSibling = NULL;
  146. _parent = NULL;
  147. if (parent && parent->_notifyHierarchyChanged)
  148. {
  149. parent->hierarchyChanged();
  150. }
  151. }
  152. Node* Node::getFirstChild() const
  153. {
  154. return _firstChild;
  155. }
  156. Node* Node::getNextSibling() const
  157. {
  158. return _nextSibling;
  159. }
  160. Node* Node::getPreviousSibling() const
  161. {
  162. return _prevSibling;
  163. }
  164. Node* Node::getParent() const
  165. {
  166. return _parent;
  167. }
  168. unsigned int Node::getChildCount() const
  169. {
  170. return _childCount;
  171. }
  172. Node* Node::findNode(const char* id, bool recursive, bool exactMatch)
  173. {
  174. assert(id);
  175. // Search immediate children first.
  176. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  177. {
  178. // Does this child's ID match?
  179. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  180. {
  181. return child;
  182. }
  183. }
  184. // Recurse.
  185. if (recursive)
  186. {
  187. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  188. {
  189. Node* match = child->findNode(id, true, exactMatch);
  190. if (match)
  191. {
  192. return match;
  193. }
  194. }
  195. }
  196. return NULL;
  197. }
  198. unsigned int Node::findNodes(const char* id, std::vector<Node*>& nodes, bool recursive, bool exactMatch)
  199. {
  200. assert(id);
  201. unsigned int count = 0;
  202. // Search immediate children first.
  203. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  204. {
  205. // Does this child's ID match?
  206. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  207. {
  208. nodes.push_back(child);
  209. ++count;
  210. }
  211. }
  212. // Recurse.
  213. if (recursive)
  214. {
  215. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  216. {
  217. count += child->findNodes(id, nodes, true, exactMatch);
  218. }
  219. }
  220. return count;
  221. }
  222. Scene* Node::getScene() const
  223. {
  224. // Search for a scene in our parents.
  225. for (Node* n = const_cast<Node*>(this); n != NULL; n = n->getParent())
  226. {
  227. if (n->_scene)
  228. {
  229. return n->_scene;
  230. }
  231. }
  232. return NULL;
  233. }
  234. Node* Node::getRootNode() const
  235. {
  236. Node* n = const_cast<Node*>(this);
  237. while (n->getParent())
  238. {
  239. n = n->getParent();
  240. }
  241. return n;
  242. }
  243. const Matrix& Node::getWorldMatrix() const
  244. {
  245. if (_dirtyBits & NODE_DIRTY_WORLD)
  246. {
  247. // Clear our dirty flag immediately to prevent this block from being entered if our
  248. // parent calls our getWorldMatrix() method as a result of the following calculations.
  249. _dirtyBits &= ~NODE_DIRTY_WORLD;
  250. // If we have a parent, multiply our parent world transform by our local
  251. // transform to obtain our final resolved world transform.
  252. Node* parent = getParent();
  253. if (parent && !_physicsRigidBody)
  254. {
  255. Matrix::multiply(parent->getWorldMatrix(), getMatrix(), &_world);
  256. }
  257. else
  258. {
  259. _world = getMatrix();
  260. }
  261. // Our world matrix was just updated, so call getWorldMatrix() on all child nodes
  262. // to force their resolved world matrices to be updated.
  263. Node* node = getFirstChild();
  264. while (node)
  265. {
  266. node->getWorldMatrix();
  267. node = node->getNextSibling();
  268. }
  269. }
  270. return _world;
  271. }
  272. const Matrix& Node::getWorldViewMatrix() const
  273. {
  274. static Matrix worldView;
  275. Matrix::multiply(getViewMatrix(), getWorldMatrix(), &worldView);
  276. return worldView;
  277. }
  278. const Matrix& Node::getInverseTransposeWorldViewMatrix() const
  279. {
  280. static Matrix invTransWorldView;
  281. // Assume the matrix is always dirty since the camera is moving
  282. // almost every frame in most games.
  283. //
  284. // TODO: Optimize here to NOT calculate the inverse transpose if the matrix is orthogonal.
  285. Matrix::multiply(getViewMatrix(), getWorldMatrix(), &invTransWorldView);
  286. invTransWorldView.invert();
  287. invTransWorldView.transpose();
  288. return invTransWorldView;
  289. }
  290. const Matrix& Node::getViewMatrix() const
  291. {
  292. Scene* scene = getScene();
  293. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  294. if (camera)
  295. {
  296. return camera->getViewMatrix();
  297. }
  298. else
  299. {
  300. return Matrix::identity();
  301. }
  302. }
  303. const Matrix& Node::getInverseViewMatrix() const
  304. {
  305. Scene* scene = getScene();
  306. Camera* camera = scene ? scene->getActiveCamera() : NULL;
  307. if (camera)
  308. {
  309. return camera->getInverseViewMatrix();
  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. Node* n = getFirstChild();
  401. while (n)
  402. {
  403. n->transformChanged();
  404. n = n->getNextSibling();
  405. }
  406. Transform::transformChanged();
  407. }
  408. Camera* Node::getCamera() const
  409. {
  410. return _camera;
  411. }
  412. void Node::setCamera(Camera* camera)
  413. {
  414. if (_camera != camera)
  415. {
  416. if (_camera)
  417. {
  418. _camera->setNode(NULL);
  419. SAFE_RELEASE(_camera);
  420. }
  421. _camera = camera;
  422. if (_camera)
  423. {
  424. _camera->addRef();
  425. _camera->setNode(this);
  426. }
  427. }
  428. }
  429. Light* Node::getLight() const
  430. {
  431. return _light;
  432. }
  433. void Node::setLight(Light* light)
  434. {
  435. if (_light != light)
  436. {
  437. if (_light)
  438. {
  439. _light->setNode(NULL);
  440. SAFE_RELEASE(_light);
  441. }
  442. _light = light;
  443. if (_light)
  444. {
  445. _light->addRef();
  446. _light->setNode(this);
  447. }
  448. }
  449. }
  450. void Node::setModel(Model* model)
  451. {
  452. if (_model != model)
  453. {
  454. if (_model)
  455. {
  456. _model->setNode(NULL);
  457. SAFE_RELEASE(_model);
  458. }
  459. _model = model;
  460. if (_model)
  461. {
  462. _model->addRef();
  463. _model->setNode(this);
  464. }
  465. }
  466. }
  467. Model* Node::getModel() const
  468. {
  469. return _model;
  470. }
  471. const BoundingBox& Node::getBoundingBox() const
  472. {
  473. if (_boundsType != BOX)
  474. {
  475. return BoundingBox::empty();
  476. }
  477. if (_dirtyBits & NODE_DIRTY_BOUNDS)
  478. {
  479. _dirtyBits &= ~NODE_DIRTY_BOUNDS;
  480. if (_model && _model->getMesh())
  481. {
  482. // Use the bounding volume of our model's mesh.
  483. Mesh* mesh = _model->getMesh();
  484. _bounds.box->set(mesh->getBoundingBox());
  485. }
  486. else
  487. {
  488. _bounds.box->set(Vector3::zero(), Vector3::zero());
  489. }
  490. bool empty = _bounds.box->isEmpty();
  491. if (!empty)
  492. {
  493. // Transform the box into world space.
  494. _bounds.box->transform(getWorldMatrix());
  495. }
  496. // Merge this world-space bounding box with our childrens' bounding volumes.
  497. for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
  498. {
  499. switch (n->_boundsType)
  500. {
  501. case BOX:
  502. {
  503. const BoundingBox& childBox = n->getBoundingBox();
  504. if (!childBox.isEmpty())
  505. {
  506. if (empty)
  507. {
  508. _bounds.box->set(childBox);
  509. empty = false;
  510. }
  511. else
  512. {
  513. _bounds.box->merge(childBox);
  514. }
  515. }
  516. }
  517. break;
  518. case SPHERE:
  519. {
  520. const BoundingSphere& childSphere = n->getBoundingSphere();
  521. if (!childSphere.isEmpty())
  522. {
  523. if (empty)
  524. {
  525. _bounds.box->set(childSphere);
  526. empty = false;
  527. }
  528. else
  529. {
  530. _bounds.box->merge(childSphere);
  531. }
  532. }
  533. }
  534. break;
  535. }
  536. }
  537. }
  538. return *_bounds.box;
  539. }
  540. const BoundingSphere& Node::getBoundingSphere() const
  541. {
  542. if (_boundsType != SPHERE)
  543. {
  544. return BoundingSphere::empty();
  545. }
  546. if (_dirtyBits & NODE_DIRTY_BOUNDS)
  547. {
  548. _dirtyBits &= ~NODE_DIRTY_BOUNDS;
  549. if (_model && _model->getMesh())
  550. {
  551. // Use the bounding volume of our model's mesh.
  552. Mesh* mesh = _model->getMesh();
  553. _bounds.sphere->set(mesh->getBoundingSphere());
  554. }
  555. else
  556. {
  557. _bounds.sphere->set(Vector3::zero(), 0);
  558. }
  559. bool empty = _bounds.sphere->isEmpty();
  560. if (!empty)
  561. {
  562. // Transform the sphere into world space.
  563. _bounds.sphere->transform(getWorldMatrix());
  564. }
  565. // Merge this world-space bounding sphere with our childrens' bounding volumes.
  566. for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
  567. {
  568. switch (n->getBoundsType())
  569. {
  570. case BOX:
  571. {
  572. const BoundingBox& childBox = n->getBoundingBox();
  573. if (!childBox.isEmpty())
  574. {
  575. if (empty)
  576. {
  577. _bounds.sphere->set(childBox);
  578. empty = false;
  579. }
  580. else
  581. {
  582. _bounds.sphere->merge(childBox);
  583. }
  584. }
  585. }
  586. break;
  587. case SPHERE:
  588. {
  589. const BoundingSphere& childSphere = n->getBoundingSphere();
  590. if (!childSphere.isEmpty())
  591. {
  592. if (empty)
  593. {
  594. _bounds.sphere->set(childSphere);
  595. empty = false;
  596. }
  597. else
  598. {
  599. _bounds.sphere->merge(childSphere);
  600. }
  601. }
  602. }
  603. break;
  604. }
  605. }
  606. }
  607. return *_bounds.sphere;
  608. }
  609. Node::BoundsType Node::getBoundsType() const
  610. {
  611. return _boundsType;
  612. }
  613. void Node::setBoundsType(Node::BoundsType type)
  614. {
  615. if (type != _boundsType)
  616. {
  617. switch (_boundsType)
  618. {
  619. case BOX:
  620. SAFE_DELETE(_bounds.box);
  621. break;
  622. case SPHERE:
  623. SAFE_DELETE(_bounds.sphere);
  624. break;
  625. }
  626. memset(&_bounds, 0, sizeof(_bounds));
  627. _boundsType = type;
  628. switch (_boundsType)
  629. {
  630. case BOX:
  631. _bounds.box = new BoundingBox();
  632. break;
  633. case SPHERE:
  634. _bounds.sphere = new BoundingSphere();
  635. break;
  636. }
  637. // We need to dirty the bounds for the parents in our hierarchy when
  638. // our bounding volume type changes.
  639. Node* parent = getParent();
  640. while (parent)
  641. {
  642. parent->_dirtyBits |= NODE_DIRTY_BOUNDS;
  643. parent = parent->getParent();
  644. }
  645. }
  646. }
  647. AudioSource* Node::getAudioSource() const
  648. {
  649. return _audioSource;
  650. }
  651. void Node::setAudioSource(AudioSource* audio)
  652. {
  653. if (_audioSource != audio)
  654. {
  655. if (_audioSource)
  656. {
  657. _audioSource->setNode(NULL);
  658. SAFE_RELEASE(_audioSource);
  659. }
  660. _audioSource = audio;
  661. if (_audioSource)
  662. {
  663. _audioSource->addRef();
  664. _audioSource->setNode(this);
  665. }
  666. }
  667. }
  668. ParticleEmitter* Node::getParticleEmitter() const
  669. {
  670. return _particleEmitter;
  671. }
  672. void Node::setParticleEmitter(ParticleEmitter* emitter)
  673. {
  674. if (_particleEmitter != emitter)
  675. {
  676. if (_particleEmitter)
  677. {
  678. _particleEmitter->setNode(NULL);
  679. SAFE_RELEASE(_particleEmitter);
  680. }
  681. _particleEmitter = emitter;
  682. if (_particleEmitter)
  683. {
  684. _particleEmitter->addRef();
  685. _particleEmitter->setNode(this);
  686. }
  687. }
  688. }
  689. PhysicsRigidBody* Node::getPhysicsRigidBody() const
  690. {
  691. return _physicsRigidBody;
  692. }
  693. void Node::setPhysicsRigidBody(PhysicsRigidBody::Type type, float mass, float friction,
  694. float restitution, float linearDamping, float angularDamping)
  695. {
  696. SAFE_DELETE(_physicsRigidBody);
  697. if (type != PhysicsRigidBody::SHAPE_NONE)
  698. _physicsRigidBody = new PhysicsRigidBody(this, type, mass, friction, restitution, linearDamping, angularDamping);
  699. }
  700. }