Node.cpp 24 KB

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