Node.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. Animation* Node::getAnimation(const char* id) const
  509. {
  510. Animation* animation = ((AnimationTarget*)this)->getAnimation(id);
  511. if (animation)
  512. return animation;
  513. // See if this node has a model, then drill down.
  514. Model* model = this->getModel();
  515. if (model)
  516. {
  517. // Check to see if there's any animations with the ID on the joints.
  518. MeshSkin* skin = model->getSkin();
  519. if (skin)
  520. {
  521. Joint* rootJoint = skin->getRootJoint();
  522. if (rootJoint)
  523. {
  524. animation = rootJoint->getAnimation(id);
  525. if (animation)
  526. return animation;
  527. }
  528. }
  529. // Check to see if any of the model's material parameter's has an animation
  530. // with the given ID.
  531. Material* material = model->getMaterial();
  532. if (material)
  533. {
  534. // How to access material parameters? hidden on the Material::RenderState.
  535. std::vector<MaterialParameter*>::iterator itr = material->_parameters.begin();
  536. for (; itr != material->_parameters.end(); itr++)
  537. {
  538. animation = ((MaterialParameter*)(*itr))->getAnimation(id);
  539. if (animation)
  540. return animation;
  541. }
  542. }
  543. }
  544. // look through form for animations.
  545. Form* form = this->getForm();
  546. if (form)
  547. {
  548. animation = form->getAnimation(id);
  549. if (animation)
  550. return animation;
  551. }
  552. // Look through this node's children for an animation with the specified ID.
  553. unsigned int childCount = this->getChildCount();
  554. Node* child = this->getFirstChild();
  555. for (unsigned int i = 0; i < childCount; i++)
  556. {
  557. animation = child->getAnimation(id);
  558. if (animation)
  559. return animation;
  560. child = child->getNextSibling();
  561. }
  562. return NULL;
  563. }
  564. Camera* Node::getCamera() const
  565. {
  566. return _camera;
  567. }
  568. void Node::setCamera(Camera* camera)
  569. {
  570. if (_camera != camera)
  571. {
  572. if (_camera)
  573. {
  574. _camera->setNode(NULL);
  575. SAFE_RELEASE(_camera);
  576. }
  577. _camera = camera;
  578. if (_camera)
  579. {
  580. _camera->addRef();
  581. _camera->setNode(this);
  582. }
  583. }
  584. }
  585. Light* Node::getLight() const
  586. {
  587. return _light;
  588. }
  589. void Node::setLight(Light* light)
  590. {
  591. if (_light != light)
  592. {
  593. if (_light)
  594. {
  595. _light->setNode(NULL);
  596. SAFE_RELEASE(_light);
  597. }
  598. _light = light;
  599. if (_light)
  600. {
  601. _light->addRef();
  602. _light->setNode(this);
  603. }
  604. }
  605. }
  606. void Node::setModel(Model* model)
  607. {
  608. if (_model != model)
  609. {
  610. if (_model)
  611. {
  612. _model->setNode(NULL);
  613. SAFE_RELEASE(_model);
  614. }
  615. _model = model;
  616. if (_model)
  617. {
  618. _model->addRef();
  619. _model->setNode(this);
  620. }
  621. }
  622. }
  623. Model* Node::getModel() const
  624. {
  625. return _model;
  626. }
  627. void Node::setForm(Form* form)
  628. {
  629. if (_form != form)
  630. {
  631. if (_form)
  632. {
  633. _form->setNode(NULL);
  634. SAFE_RELEASE(_form);
  635. }
  636. _form = form;
  637. if (_form)
  638. {
  639. _form->addRef();
  640. _form->setNode(this);
  641. }
  642. }
  643. }
  644. Form* Node::getForm() const
  645. {
  646. return _form;
  647. }
  648. const BoundingSphere& Node::getBoundingSphere() const
  649. {
  650. if (_dirtyBits & NODE_DIRTY_BOUNDS)
  651. {
  652. _dirtyBits &= ~NODE_DIRTY_BOUNDS;
  653. const Matrix& worldMatrix = getWorldMatrix();
  654. // Start with our local bounding sphere
  655. // TODO: Incorporate bounds from entities other than mesh (i.e. emitters, audiosource, etc)
  656. bool empty = true;
  657. if (_model && _model->getMesh())
  658. {
  659. _bounds.set(_model->getMesh()->getBoundingSphere());
  660. empty = false;
  661. }
  662. else
  663. {
  664. // Empty bounding sphere, set the world translation with zero radius
  665. worldMatrix.getTranslation(&_bounds.center);
  666. _bounds.radius = 0;
  667. }
  668. // Transform the sphere (if not empty) into world space.
  669. if (!empty)
  670. {
  671. bool applyWorldTransform = true;
  672. if (_model && _model->getSkin())
  673. {
  674. // Special case: If the root joint of our mesh skin is parented by any nodes,
  675. // multiply the world matrix of the root joint's parent by this node's
  676. // world matrix. This computes a final world matrix used for transforming this
  677. // node's bounding volume. This allows us to store a much smaller bounding
  678. // volume approximation than would otherwise be possible for skinned meshes,
  679. // since joint parent nodes that are not in the matrix pallette do not need to
  680. // be considered as directly transforming vertices on the GPU (they can instead
  681. // be applied directly to the bounding volume transformation below).
  682. Node* jointParent = _model->getSkin()->getRootJoint()->getParent();
  683. if (jointParent)
  684. {
  685. // TODO: Should we protect against the case where joints are nested directly
  686. // in the node hierachy of the model (this is normally not the case)?
  687. Matrix boundsMatrix;
  688. Matrix::multiply(getWorldMatrix(), jointParent->getWorldMatrix(), &boundsMatrix);
  689. _bounds.transform(boundsMatrix);
  690. applyWorldTransform = false;
  691. }
  692. }
  693. if (applyWorldTransform)
  694. {
  695. _bounds.transform(getWorldMatrix());
  696. }
  697. }
  698. // Merge this world-space bounding sphere with our childrens' bounding volumes.
  699. for (Node* n = getFirstChild(); n != NULL; n = n->getNextSibling())
  700. {
  701. const BoundingSphere& childSphere = n->getBoundingSphere();
  702. if (!childSphere.isEmpty())
  703. {
  704. if (empty)
  705. {
  706. _bounds.set(childSphere);
  707. empty = false;
  708. }
  709. else
  710. {
  711. _bounds.merge(childSphere);
  712. }
  713. }
  714. }
  715. }
  716. return _bounds;
  717. }
  718. Node* Node::clone() const
  719. {
  720. NodeCloneContext context;
  721. return cloneRecursive(context);
  722. }
  723. Node* Node::cloneSingleNode(NodeCloneContext &context) const
  724. {
  725. Node* copy = Node::create(getId());
  726. context.registerClonedNode(this, copy);
  727. cloneInto(copy, context);
  728. return copy;
  729. }
  730. Node* Node::cloneRecursive(NodeCloneContext &context) const
  731. {
  732. Node* copy = cloneSingleNode(context);
  733. for (Node* child = getFirstChild(); child != NULL; child = child->getNextSibling())
  734. {
  735. Node* childCopy = child->cloneRecursive(context);
  736. copy->addChild(childCopy); // TODO: Does child order matter?
  737. childCopy->release();
  738. }
  739. return copy;
  740. }
  741. void Node::cloneInto(Node* node, NodeCloneContext &context) const
  742. {
  743. Transform::cloneInto(node, context);
  744. // TODO: Clone the rest of the node data.
  745. //node->setCamera(getCamera());
  746. //node->setLight(getLight());
  747. if (Camera* camera = getCamera())
  748. {
  749. Camera* cameraClone = camera->clone(context);
  750. node->setCamera(cameraClone);
  751. cameraClone->release();
  752. }
  753. if (Light* light = getLight())
  754. {
  755. Light* lightClone = light->clone(context);
  756. node->setLight(lightClone);
  757. lightClone->release();
  758. }
  759. if (AudioSource* audio = getAudioSource())
  760. {
  761. AudioSource* audioClone = audio->clone(context);
  762. node->setAudioSource(audioClone);
  763. audioClone->release();
  764. }
  765. if (Model* model = getModel())
  766. {
  767. Model* modelClone = model->clone(context);
  768. node->setModel(modelClone);
  769. modelClone->release();
  770. }
  771. node->_world = _world;
  772. node->_bounds = _bounds;
  773. }
  774. AudioSource* Node::getAudioSource() const
  775. {
  776. return _audioSource;
  777. }
  778. void Node::setAudioSource(AudioSource* audio)
  779. {
  780. if (_audioSource != audio)
  781. {
  782. if (_audioSource)
  783. {
  784. _audioSource->setNode(NULL);
  785. SAFE_RELEASE(_audioSource);
  786. }
  787. _audioSource = audio;
  788. if (_audioSource)
  789. {
  790. _audioSource->addRef();
  791. _audioSource->setNode(this);
  792. }
  793. }
  794. }
  795. ParticleEmitter* Node::getParticleEmitter() const
  796. {
  797. return _particleEmitter;
  798. }
  799. void Node::setParticleEmitter(ParticleEmitter* emitter)
  800. {
  801. if (_particleEmitter != emitter)
  802. {
  803. if (_particleEmitter)
  804. {
  805. _particleEmitter->setNode(NULL);
  806. SAFE_RELEASE(_particleEmitter);
  807. }
  808. _particleEmitter = emitter;
  809. if (_particleEmitter)
  810. {
  811. _particleEmitter->addRef();
  812. _particleEmitter->setNode(this);
  813. }
  814. }
  815. }
  816. PhysicsCollisionObject* Node::getCollisionObject() const
  817. {
  818. return _collisionObject;
  819. }
  820. PhysicsCollisionObject* Node::setCollisionObject(PhysicsCollisionObject::Type type, const PhysicsCollisionShape::Definition& shape, PhysicsRigidBody::Parameters* rigidBodyParameters)
  821. {
  822. SAFE_DELETE(_collisionObject);
  823. switch (type)
  824. {
  825. case PhysicsCollisionObject::RIGID_BODY:
  826. {
  827. _collisionObject = new PhysicsRigidBody(this, shape, rigidBodyParameters ? *rigidBodyParameters : PhysicsRigidBody::Parameters());
  828. }
  829. break;
  830. case PhysicsCollisionObject::GHOST_OBJECT:
  831. {
  832. _collisionObject = new PhysicsGhostObject(this, shape);
  833. }
  834. break;
  835. case PhysicsCollisionObject::CHARACTER:
  836. {
  837. _collisionObject = new PhysicsCharacter(this, shape, rigidBodyParameters ? rigidBodyParameters->mass : 1.0f);
  838. }
  839. break;
  840. }
  841. return _collisionObject;
  842. }
  843. PhysicsCollisionObject* Node::setCollisionObject(const char* filePath)
  844. {
  845. // Load the collision object properties from file.
  846. Properties* properties = Properties::create(filePath);
  847. assert(properties);
  848. if (properties == NULL)
  849. {
  850. WARN_VARG("Failed to load collision object file: %s", filePath);
  851. return NULL;
  852. }
  853. PhysicsCollisionObject* collisionObject = setCollisionObject(properties->getNextNamespace());
  854. SAFE_DELETE(properties);
  855. return collisionObject;
  856. }
  857. PhysicsCollisionObject* Node::setCollisionObject(Properties* properties)
  858. {
  859. SAFE_DELETE(_collisionObject);
  860. // Check if the properties is valid.
  861. if (!properties ||
  862. !(strcmp(properties->getNamespace(), "character") == 0 ||
  863. strcmp(properties->getNamespace(), "ghost") == 0 ||
  864. strcmp(properties->getNamespace(), "rigidbody") == 0))
  865. {
  866. WARN("Failed to load collision object from properties object: must be non-null object and have namespace equal to \'character\', \'ghost\', or \'rigidbody\'.");
  867. return NULL;
  868. }
  869. if (strcmp(properties->getNamespace(), "character") == 0)
  870. {
  871. _collisionObject = PhysicsCharacter::create(this, properties);
  872. }
  873. else if (strcmp(properties->getNamespace(), "ghost") == 0)
  874. {
  875. _collisionObject = PhysicsGhostObject::create(this, properties);
  876. }
  877. else if (strcmp(properties->getNamespace(), "rigidbody") == 0)
  878. {
  879. _collisionObject = PhysicsRigidBody::create(this, properties);
  880. }
  881. return _collisionObject;
  882. }
  883. NodeCloneContext::NodeCloneContext()
  884. {
  885. }
  886. NodeCloneContext::~NodeCloneContext()
  887. {
  888. }
  889. Animation* NodeCloneContext::findClonedAnimation(const Animation* animation)
  890. {
  891. AnimationMap::iterator it = _clonedAnimations.find(animation);
  892. return it != _clonedAnimations.end() ? it->second : NULL;
  893. }
  894. void NodeCloneContext::registerClonedAnimation(const Animation* original, Animation* clone)
  895. {
  896. _clonedAnimations[original] = clone;
  897. }
  898. Node* NodeCloneContext::findClonedNode(const Node* node)
  899. {
  900. NodeMap::iterator it = _clonedNodes.find(node);
  901. return it != _clonedNodes.end() ? it->second : NULL;
  902. }
  903. void NodeCloneContext::registerClonedNode(const Node* original, Node* clone)
  904. {
  905. _clonedNodes[original] = clone;
  906. }
  907. }