ISceneNode.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __I_SCENE_NODE_H_INCLUDED__
  5. #define __I_SCENE_NODE_H_INCLUDED__
  6. #include "IAttributeExchangingObject.h"
  7. #include "ESceneNodeTypes.h"
  8. #include "ECullingTypes.h"
  9. #include "EDebugSceneTypes.h"
  10. #include "ISceneNodeAnimator.h"
  11. #include "ITriangleSelector.h"
  12. #include "SMaterial.h"
  13. #include "irrString.h"
  14. #include "aabbox3d.h"
  15. #include "matrix4.h"
  16. #include "irrList.h"
  17. #include "IAttributes.h"
  18. namespace irr
  19. {
  20. namespace scene
  21. {
  22. class ISceneManager;
  23. //! Typedef for list of scene nodes
  24. typedef core::list<ISceneNode*> ISceneNodeList;
  25. //! Typedef for list of scene node animators
  26. typedef core::list<ISceneNodeAnimator*> ISceneNodeAnimatorList;
  27. //! Scene node interface.
  28. /** A scene node is a node in the hierarchical scene graph. Every scene
  29. node may have children, which are also scene nodes. Children move
  30. relative to their parent's position. If the parent of a node is not
  31. visible, its children won't be visible either. In this way, it is for
  32. example easily possible to attach a light to a moving car, or to place
  33. a walking character on a moving platform on a moving ship.
  34. */
  35. class ISceneNode : virtual public io::IAttributeExchangingObject
  36. {
  37. public:
  38. //! Constructor
  39. ISceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id=-1,
  40. const core::vector3df& position = core::vector3df(0,0,0),
  41. const core::vector3df& rotation = core::vector3df(0,0,0),
  42. const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f))
  43. : RelativeTranslation(position), RelativeRotation(rotation), RelativeScale(scale),
  44. Parent(0), SceneManager(mgr), TriangleSelector(0), ID(id),
  45. AutomaticCullingState(EAC_BOX), DebugDataVisible(EDS_OFF),
  46. IsVisible(true), IsDebugObject(false)
  47. {
  48. if (parent)
  49. parent->addChild(this);
  50. updateAbsolutePosition();
  51. }
  52. //! Destructor
  53. virtual ~ISceneNode()
  54. {
  55. // delete all children
  56. removeAll();
  57. // delete all animators
  58. ISceneNodeAnimatorList::Iterator ait = Animators.begin();
  59. for (; ait != Animators.end(); ++ait)
  60. (*ait)->drop();
  61. if (TriangleSelector)
  62. TriangleSelector->drop();
  63. }
  64. //! This method is called just before the rendering process of the whole scene.
  65. /** Nodes may register themselves in the render pipeline during this call,
  66. precalculate the geometry which should be renderered, and prevent their
  67. children from being able to register themselves if they are clipped by simply
  68. not calling their OnRegisterSceneNode method.
  69. If you are implementing your own scene node, you should overwrite this method
  70. with an implementation code looking like this:
  71. \code
  72. if (IsVisible)
  73. SceneManager->registerNodeForRendering(this);
  74. ISceneNode::OnRegisterSceneNode();
  75. \endcode
  76. */
  77. virtual void OnRegisterSceneNode()
  78. {
  79. if (IsVisible)
  80. {
  81. ISceneNodeList::Iterator it = Children.begin();
  82. for (; it != Children.end(); ++it)
  83. (*it)->OnRegisterSceneNode();
  84. }
  85. }
  86. //! OnAnimate() is called just before rendering the whole scene.
  87. /** Nodes may calculate or store animations here, and may do other useful things,
  88. depending on what they are. Also, OnAnimate() should be called for all
  89. child scene nodes here. This method will be called once per frame, independent
  90. of whether the scene node is visible or not.
  91. \param timeMs Current time in milliseconds. */
  92. virtual void OnAnimate(u32 timeMs)
  93. {
  94. if (IsVisible)
  95. {
  96. // animate this node with all animators
  97. ISceneNodeAnimatorList::Iterator ait = Animators.begin();
  98. while (ait != Animators.end())
  99. {
  100. // continue to the next node before calling animateNode()
  101. // so that the animator may remove itself from the scene
  102. // node without the iterator becoming invalid
  103. ISceneNodeAnimator* anim = *ait;
  104. ++ait;
  105. if ( anim->isEnabled() )
  106. {
  107. anim->animateNode(this, timeMs);
  108. }
  109. }
  110. // update absolute position
  111. updateAbsolutePosition();
  112. // perform the post render process on all children
  113. ISceneNodeList::Iterator it = Children.begin();
  114. for (; it != Children.end(); ++it)
  115. (*it)->OnAnimate(timeMs);
  116. }
  117. }
  118. //! Renders the node.
  119. virtual void render() = 0;
  120. //! Returns the name of the node.
  121. /** \return Name as character string. */
  122. virtual const c8* getName() const
  123. {
  124. return Name.c_str();
  125. }
  126. //! Sets the name of the node.
  127. /** \param name New name of the scene node. */
  128. virtual void setName(const c8* name)
  129. {
  130. Name = name;
  131. }
  132. //! Sets the name of the node.
  133. /** \param name New name of the scene node. */
  134. virtual void setName(const core::stringc& name)
  135. {
  136. Name = name;
  137. }
  138. //! Get the axis aligned, not transformed bounding box of this node.
  139. /** This means that if this node is an animated 3d character,
  140. moving in a room, the bounding box will always be around the
  141. origin. To get the box in real world coordinates, just
  142. transform it with the matrix you receive with
  143. getAbsoluteTransformation() or simply use
  144. getTransformedBoundingBox(), which does the same.
  145. \return The non-transformed bounding box. */
  146. virtual const core::aabbox3d<f32>& getBoundingBox() const = 0;
  147. //! Get the axis aligned, transformed and animated absolute bounding box of this node.
  148. /** Note: The result is still an axis-aligned bounding box, so it's size
  149. changes with rotation.
  150. \return The transformed bounding box. */
  151. virtual const core::aabbox3d<f32> getTransformedBoundingBox() const
  152. {
  153. core::aabbox3d<f32> box = getBoundingBox();
  154. AbsoluteTransformation.transformBoxEx(box);
  155. return box;
  156. }
  157. //! Get a the 8 corners of the original bounding box transformed and
  158. //! animated by the absolute transformation.
  159. /** Note: The result is _not_ identical to getTransformedBoundingBox().getEdges(),
  160. but getting an aabbox3d of these edges would then be identical.
  161. \param edges Receives an array with the transformed edges */
  162. virtual void getTransformedBoundingBoxEdges(core::array< core::vector3d<f32> >& edges) const
  163. {
  164. edges.set_used(8);
  165. getBoundingBox().getEdges( edges.pointer() );
  166. for ( u32 i=0; i<8; ++i )
  167. AbsoluteTransformation.transformVect( edges[i] );
  168. }
  169. //! Get the absolute transformation of the node. Is recalculated every OnAnimate()-call.
  170. /** NOTE: For speed reasons the absolute transformation is not
  171. automatically recalculated on each change of the relative
  172. transformation or by a transformation change of an parent. Instead the
  173. update usually happens once per frame in OnAnimate. You can enforce
  174. an update with updateAbsolutePosition().
  175. \return The absolute transformation matrix. */
  176. virtual const core::matrix4& getAbsoluteTransformation() const
  177. {
  178. return AbsoluteTransformation;
  179. }
  180. //! Returns the relative transformation of the scene node.
  181. /** The relative transformation is stored internally as 3
  182. vectors: translation, rotation and scale. To get the relative
  183. transformation matrix, it is calculated from these values.
  184. \return The relative transformation matrix. */
  185. virtual core::matrix4 getRelativeTransformation() const
  186. {
  187. core::matrix4 mat;
  188. mat.setRotationDegrees(RelativeRotation);
  189. mat.setTranslation(RelativeTranslation);
  190. if (RelativeScale != core::vector3df(1.f,1.f,1.f))
  191. {
  192. core::matrix4 smat;
  193. smat.setScale(RelativeScale);
  194. mat *= smat;
  195. }
  196. return mat;
  197. }
  198. //! Returns whether the node should be visible (if all of its parents are visible).
  199. /** This is only an option set by the user, but has nothing to
  200. do with geometry culling
  201. \return The requested visibility of the node, true means
  202. visible (if all parents are also visible). */
  203. virtual bool isVisible() const
  204. {
  205. return IsVisible;
  206. }
  207. //! Check whether the node is truly visible, taking into accounts its parents' visibility
  208. /** \return true if the node and all its parents are visible,
  209. false if this or any parent node is invisible. */
  210. virtual bool isTrulyVisible() const
  211. {
  212. if(!IsVisible)
  213. return false;
  214. if(!Parent)
  215. return true;
  216. return Parent->isTrulyVisible();
  217. }
  218. //! Sets if the node should be visible or not.
  219. /** All children of this node won't be visible either, when set
  220. to false. Invisible nodes are not valid candidates for selection by
  221. collision manager bounding box methods.
  222. \param isVisible If the node shall be visible. */
  223. virtual void setVisible(bool isVisible)
  224. {
  225. IsVisible = isVisible;
  226. }
  227. //! Get the id of the scene node.
  228. /** This id can be used to identify the node.
  229. \return The id. */
  230. virtual s32 getID() const
  231. {
  232. return ID;
  233. }
  234. //! Sets the id of the scene node.
  235. /** This id can be used to identify the node.
  236. \param id The new id. */
  237. virtual void setID(s32 id)
  238. {
  239. ID = id;
  240. }
  241. //! Adds a child to this scene node.
  242. /** If the scene node already has a parent it is first removed
  243. from the other parent.
  244. \param child A pointer to the new child. */
  245. virtual void addChild(ISceneNode* child)
  246. {
  247. if (child && (child != this))
  248. {
  249. // Change scene manager?
  250. if (SceneManager != child->SceneManager)
  251. child->setSceneManager(SceneManager);
  252. child->grab();
  253. child->remove(); // remove from old parent
  254. Children.push_back(child);
  255. child->Parent = this;
  256. }
  257. }
  258. //! Removes a child from this scene node.
  259. /** If found in the children list, the child pointer is also
  260. dropped and might be deleted if no other grab exists.
  261. \param child A pointer to the child which shall be removed.
  262. \return True if the child was removed, and false if not,
  263. e.g. because it couldn't be found in the children list. */
  264. virtual bool removeChild(ISceneNode* child)
  265. {
  266. ISceneNodeList::Iterator it = Children.begin();
  267. for (; it != Children.end(); ++it)
  268. if ((*it) == child)
  269. {
  270. (*it)->Parent = 0;
  271. (*it)->drop();
  272. Children.erase(it);
  273. return true;
  274. }
  275. return false;
  276. }
  277. //! Removes all children of this scene node
  278. /** The scene nodes found in the children list are also dropped
  279. and might be deleted if no other grab exists on them.
  280. */
  281. virtual void removeAll()
  282. {
  283. ISceneNodeList::Iterator it = Children.begin();
  284. for (; it != Children.end(); ++it)
  285. {
  286. (*it)->Parent = 0;
  287. (*it)->drop();
  288. }
  289. Children.clear();
  290. }
  291. //! Removes this scene node from the scene
  292. /** If no other grab exists for this node, it will be deleted.
  293. */
  294. virtual void remove()
  295. {
  296. if (Parent)
  297. Parent->removeChild(this);
  298. }
  299. //! Adds an animator which should animate this node.
  300. /** \param animator A pointer to the new animator. */
  301. virtual void addAnimator(ISceneNodeAnimator* animator)
  302. {
  303. if (animator)
  304. {
  305. Animators.push_back(animator);
  306. animator->grab();
  307. }
  308. }
  309. //! Get a list of all scene node animators.
  310. /** \return The list of animators attached to this node. */
  311. const core::list<ISceneNodeAnimator*>& getAnimators() const
  312. {
  313. return Animators;
  314. }
  315. //! Removes an animator from this scene node.
  316. /** If the animator is found, it is also dropped and might be
  317. deleted if not other grab exists for it.
  318. \param animator A pointer to the animator to be deleted. */
  319. virtual void removeAnimator(ISceneNodeAnimator* animator)
  320. {
  321. ISceneNodeAnimatorList::Iterator it = Animators.begin();
  322. for (; it != Animators.end(); ++it)
  323. {
  324. if ((*it) == animator)
  325. {
  326. (*it)->drop();
  327. Animators.erase(it);
  328. return;
  329. }
  330. }
  331. }
  332. //! Removes all animators from this scene node.
  333. /** The animators might also be deleted if no other grab exists
  334. for them. */
  335. virtual void removeAnimators()
  336. {
  337. ISceneNodeAnimatorList::Iterator it = Animators.begin();
  338. for (; it != Animators.end(); ++it)
  339. (*it)->drop();
  340. Animators.clear();
  341. }
  342. //! Returns the material based on the zero based index i.
  343. /** To get the amount of materials used by this scene node, use
  344. getMaterialCount(). This function is needed for inserting the
  345. node into the scene hierarchy at an optimal position for
  346. minimizing renderstate changes, but can also be used to
  347. directly modify the material of a scene node.
  348. \param num Zero based index. The maximal value is getMaterialCount() - 1.
  349. \return The material at that index. */
  350. virtual video::SMaterial& getMaterial(u32 num)
  351. {
  352. return video::IdentityMaterial;
  353. }
  354. //! Get amount of materials used by this scene node.
  355. /** \return Current amount of materials of this scene node. */
  356. virtual u32 getMaterialCount() const
  357. {
  358. return 0;
  359. }
  360. //! Sets all material flags at once to a new value.
  361. /** Useful, for example, if you want the whole mesh to be
  362. affected by light.
  363. \param flag Which flag of all materials to be set.
  364. \param newvalue New value of that flag. */
  365. void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
  366. {
  367. for (u32 i=0; i<getMaterialCount(); ++i)
  368. getMaterial(i).setFlag(flag, newvalue);
  369. }
  370. //! Sets the texture of the specified layer in all materials of this scene node to the new texture.
  371. /** \param textureLayer Layer of texture to be set. Must be a
  372. value smaller than MATERIAL_MAX_TEXTURES.
  373. \param texture New texture to be used. */
  374. void setMaterialTexture(u32 textureLayer, video::ITexture* texture)
  375. {
  376. if (textureLayer >= video::MATERIAL_MAX_TEXTURES)
  377. return;
  378. for (u32 i=0; i<getMaterialCount(); ++i)
  379. getMaterial(i).setTexture(textureLayer, texture);
  380. }
  381. //! Sets the material type of all materials in this scene node to a new material type.
  382. /** \param newType New type of material to be set. */
  383. void setMaterialType(video::E_MATERIAL_TYPE newType)
  384. {
  385. for (u32 i=0; i<getMaterialCount(); ++i)
  386. getMaterial(i).MaterialType = newType;
  387. }
  388. //! Gets the scale of the scene node relative to its parent.
  389. /** This is the scale of this node relative to its parent.
  390. If you want the absolute scale, use
  391. getAbsoluteTransformation().getScale()
  392. \return The scale of the scene node. */
  393. virtual const core::vector3df& getScale() const
  394. {
  395. return RelativeScale;
  396. }
  397. //! Sets the relative scale of the scene node.
  398. /** \param scale New scale of the node, relative to its parent. */
  399. virtual void setScale(const core::vector3df& scale)
  400. {
  401. RelativeScale = scale;
  402. }
  403. //! Gets the rotation of the node relative to its parent.
  404. /** Note that this is the relative rotation of the node.
  405. If you want the absolute rotation, use
  406. getAbsoluteTransformation().getRotation()
  407. \return Current relative rotation of the scene node. */
  408. virtual const core::vector3df& getRotation() const
  409. {
  410. return RelativeRotation;
  411. }
  412. //! Sets the rotation of the node relative to its parent.
  413. /** This only modifies the relative rotation of the node.
  414. \param rotation New rotation of the node in degrees. */
  415. virtual void setRotation(const core::vector3df& rotation)
  416. {
  417. RelativeRotation = rotation;
  418. }
  419. //! Gets the position of the node relative to its parent.
  420. /** Note that the position is relative to the parent. If you want
  421. the position in world coordinates, use getAbsolutePosition() instead.
  422. \return The current position of the node relative to the parent. */
  423. virtual const core::vector3df& getPosition() const
  424. {
  425. return RelativeTranslation;
  426. }
  427. //! Sets the position of the node relative to its parent.
  428. /** Note that the position is relative to the parent.
  429. \param newpos New relative position of the scene node. */
  430. virtual void setPosition(const core::vector3df& newpos)
  431. {
  432. RelativeTranslation = newpos;
  433. }
  434. //! Gets the absolute position of the node in world coordinates.
  435. /** If you want the position of the node relative to its parent,
  436. use getPosition() instead.
  437. NOTE: For speed reasons the absolute position is not
  438. automatically recalculated on each change of the relative
  439. position or by a position change of an parent. Instead the
  440. update usually happens once per frame in OnAnimate. You can enforce
  441. an update with updateAbsolutePosition().
  442. \return The current absolute position of the scene node (updated on last call of updateAbsolutePosition). */
  443. virtual core::vector3df getAbsolutePosition() const
  444. {
  445. return AbsoluteTransformation.getTranslation();
  446. }
  447. //! Set a culling style or disable culling completely.
  448. /** Box cullling (EAC_BOX) is set by default. Note that not
  449. all SceneNodes support culling and that some nodes always cull
  450. their geometry because it is their only reason for existence,
  451. for example the OctreeSceneNode.
  452. \param state The culling state to be used. Check E_CULLING_TYPE for possible values.*/
  453. void setAutomaticCulling( u32 state)
  454. {
  455. AutomaticCullingState = state;
  456. }
  457. //! Gets the automatic culling state.
  458. /** \return The automatic culling state. */
  459. u32 getAutomaticCulling() const
  460. {
  461. return AutomaticCullingState;
  462. }
  463. //! Sets if debug data like bounding boxes should be drawn.
  464. /** A bitwise OR of the types from @ref irr::scene::E_DEBUG_SCENE_TYPE.
  465. Please note that not all scene nodes support all debug data types.
  466. \param state The debug data visibility state to be used. */
  467. virtual void setDebugDataVisible(u32 state)
  468. {
  469. DebugDataVisible = state;
  470. }
  471. //! Returns if debug data like bounding boxes are drawn.
  472. /** \return A bitwise OR of the debug data values from
  473. @ref irr::scene::E_DEBUG_SCENE_TYPE that are currently visible. */
  474. u32 isDebugDataVisible() const
  475. {
  476. return DebugDataVisible;
  477. }
  478. //! Sets if this scene node is a debug object.
  479. /** Debug objects have some special properties, for example they can be easily
  480. excluded from collision detection or from serialization, etc. */
  481. void setIsDebugObject(bool debugObject)
  482. {
  483. IsDebugObject = debugObject;
  484. }
  485. //! Returns if this scene node is a debug object.
  486. /** Debug objects have some special properties, for example they can be easily
  487. excluded from collision detection or from serialization, etc.
  488. \return If this node is a debug object, true is returned. */
  489. bool isDebugObject() const
  490. {
  491. return IsDebugObject;
  492. }
  493. //! Returns a const reference to the list of all children.
  494. /** \return The list of all children of this node. */
  495. const core::list<ISceneNode*>& getChildren() const
  496. {
  497. return Children;
  498. }
  499. //! Changes the parent of the scene node.
  500. /** \param newParent The new parent to be used. */
  501. virtual void setParent(ISceneNode* newParent)
  502. {
  503. grab();
  504. remove();
  505. Parent = newParent;
  506. if (Parent)
  507. Parent->addChild(this);
  508. drop();
  509. }
  510. //! Returns the triangle selector attached to this scene node.
  511. /** The Selector can be used by the engine for doing collision
  512. detection. You can create a TriangleSelector with
  513. ISceneManager::createTriangleSelector() or
  514. ISceneManager::createOctreeTriangleSelector and set it with
  515. ISceneNode::setTriangleSelector(). If a scene node got no triangle
  516. selector, but collision tests should be done with it, a triangle
  517. selector is created using the bounding box of the scene node.
  518. \return A pointer to the TriangleSelector or 0, if there
  519. is none. */
  520. virtual ITriangleSelector* getTriangleSelector() const
  521. {
  522. return TriangleSelector;
  523. }
  524. //! Sets the triangle selector of the scene node.
  525. /** The Selector can be used by the engine for doing collision
  526. detection. You can create a TriangleSelector with
  527. ISceneManager::createTriangleSelector() or
  528. ISceneManager::createOctreeTriangleSelector(). Some nodes may
  529. create their own selector by default, so it would be good to
  530. check if there is already a selector in this node by calling
  531. ISceneNode::getTriangleSelector().
  532. \param selector New triangle selector for this scene node. */
  533. virtual void setTriangleSelector(ITriangleSelector* selector)
  534. {
  535. if (TriangleSelector != selector)
  536. {
  537. if (TriangleSelector)
  538. TriangleSelector->drop();
  539. TriangleSelector = selector;
  540. if (TriangleSelector)
  541. TriangleSelector->grab();
  542. }
  543. }
  544. //! Updates the absolute position based on the relative and the parents position
  545. /** Note: This does not recursively update the parents absolute positions, so if you have a deeper
  546. hierarchy you might want to update the parents first.*/
  547. virtual void updateAbsolutePosition()
  548. {
  549. if (Parent)
  550. {
  551. AbsoluteTransformation =
  552. Parent->getAbsoluteTransformation() * getRelativeTransformation();
  553. }
  554. else
  555. AbsoluteTransformation = getRelativeTransformation();
  556. }
  557. //! Returns the parent of this scene node
  558. /** \return A pointer to the parent. */
  559. scene::ISceneNode* getParent() const
  560. {
  561. return Parent;
  562. }
  563. //! Returns type of the scene node
  564. /** \return The type of this node. */
  565. virtual ESCENE_NODE_TYPE getType() const
  566. {
  567. return ESNT_UNKNOWN;
  568. }
  569. //! Writes attributes of the scene node.
  570. /** Implement this to expose the attributes of your scene node
  571. for scripting languages, editors, debuggers or xml
  572. serialization purposes.
  573. \param out The attribute container to write into.
  574. \param options Additional options which might influence the
  575. serialization. */
  576. virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const _IRR_OVERRIDE_
  577. {
  578. if (!out)
  579. return;
  580. out->addString("Name", Name.c_str());
  581. out->addInt("Id", ID );
  582. out->addVector3d("Position", getPosition() );
  583. out->addVector3d("Rotation", getRotation() );
  584. out->addVector3d("Scale", getScale() );
  585. out->addBool("Visible", IsVisible );
  586. out->addInt("AutomaticCulling", AutomaticCullingState);
  587. out->addInt("DebugDataVisible", DebugDataVisible );
  588. out->addBool("IsDebugObject", IsDebugObject );
  589. }
  590. //! Reads attributes of the scene node.
  591. /** Implement this to set the attributes of your scene node for
  592. scripting languages, editors, debuggers or xml deserialization
  593. purposes.
  594. \param in The attribute container to read from.
  595. \param options Additional options which might influence the
  596. deserialization. */
  597. virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0) _IRR_OVERRIDE_
  598. {
  599. if (!in)
  600. return;
  601. Name = in->getAttributeAsString("Name", Name);
  602. ID = in->getAttributeAsInt("Id", ID);
  603. setPosition(in->getAttributeAsVector3d("Position", RelativeTranslation));
  604. setRotation(in->getAttributeAsVector3d("Rotation", RelativeRotation));
  605. setScale(in->getAttributeAsVector3d("Scale", RelativeScale));
  606. IsVisible = in->getAttributeAsBool("Visible", IsVisible);
  607. if (in->existsAttribute("AutomaticCulling"))
  608. {
  609. s32 tmpState = in->getAttributeAsEnumeration("AutomaticCulling",
  610. scene::AutomaticCullingNames);
  611. if (tmpState != -1)
  612. AutomaticCullingState = (u32)tmpState;
  613. else
  614. AutomaticCullingState = in->getAttributeAsInt("AutomaticCulling");
  615. }
  616. DebugDataVisible = in->getAttributeAsInt("DebugDataVisible", DebugDataVisible);
  617. IsDebugObject = in->getAttributeAsBool("IsDebugObject", IsDebugObject);
  618. updateAbsolutePosition();
  619. }
  620. //! Creates a clone of this scene node and its children.
  621. /** \param newParent An optional new parent.
  622. \param newManager An optional new scene manager.
  623. \return The newly created clone of this node. */
  624. virtual ISceneNode* clone(ISceneNode* newParent=0, ISceneManager* newManager=0)
  625. {
  626. return 0; // to be implemented by derived classes
  627. }
  628. //! Retrieve the scene manager for this node.
  629. /** \return The node's scene manager. */
  630. virtual ISceneManager* getSceneManager(void) const { return SceneManager; }
  631. protected:
  632. //! A clone function for the ISceneNode members.
  633. /** This method can be used by clone() implementations of
  634. derived classes
  635. \param toCopyFrom The node from which the values are copied
  636. \param newManager The new scene manager. */
  637. void cloneMembers(ISceneNode* toCopyFrom, ISceneManager* newManager)
  638. {
  639. Name = toCopyFrom->Name;
  640. AbsoluteTransformation = toCopyFrom->AbsoluteTransformation;
  641. RelativeTranslation = toCopyFrom->RelativeTranslation;
  642. RelativeRotation = toCopyFrom->RelativeRotation;
  643. RelativeScale = toCopyFrom->RelativeScale;
  644. ID = toCopyFrom->ID;
  645. setTriangleSelector(toCopyFrom->TriangleSelector);
  646. AutomaticCullingState = toCopyFrom->AutomaticCullingState;
  647. DebugDataVisible = toCopyFrom->DebugDataVisible;
  648. IsVisible = toCopyFrom->IsVisible;
  649. IsDebugObject = toCopyFrom->IsDebugObject;
  650. if (newManager)
  651. SceneManager = newManager;
  652. else
  653. SceneManager = toCopyFrom->SceneManager;
  654. // clone children
  655. ISceneNodeList::Iterator it = toCopyFrom->Children.begin();
  656. for (; it != toCopyFrom->Children.end(); ++it)
  657. (*it)->clone(this, newManager);
  658. // clone animators
  659. ISceneNodeAnimatorList::Iterator ait = toCopyFrom->Animators.begin();
  660. for (; ait != toCopyFrom->Animators.end(); ++ait)
  661. {
  662. ISceneNodeAnimator* anim = (*ait)->createClone(this, SceneManager);
  663. if (anim)
  664. {
  665. addAnimator(anim);
  666. anim->drop();
  667. }
  668. }
  669. }
  670. //! Sets the new scene manager for this node and all children.
  671. //! Called by addChild when moving nodes between scene managers
  672. void setSceneManager(ISceneManager* newManager)
  673. {
  674. SceneManager = newManager;
  675. ISceneNodeList::Iterator it = Children.begin();
  676. for (; it != Children.end(); ++it)
  677. (*it)->setSceneManager(newManager);
  678. }
  679. //! Name of the scene node.
  680. core::stringc Name;
  681. //! Absolute transformation of the node.
  682. core::matrix4 AbsoluteTransformation;
  683. //! Relative translation of the scene node.
  684. core::vector3df RelativeTranslation;
  685. //! Relative rotation of the scene node.
  686. core::vector3df RelativeRotation;
  687. //! Relative scale of the scene node.
  688. core::vector3df RelativeScale;
  689. //! Pointer to the parent
  690. ISceneNode* Parent;
  691. //! List of all children of this node
  692. core::list<ISceneNode*> Children;
  693. //! List of all animator nodes
  694. core::list<ISceneNodeAnimator*> Animators;
  695. //! Pointer to the scene manager
  696. ISceneManager* SceneManager;
  697. //! Pointer to the triangle selector
  698. ITriangleSelector* TriangleSelector;
  699. //! ID of the node.
  700. s32 ID;
  701. //! Automatic culling state
  702. u32 AutomaticCullingState;
  703. //! Flag if debug data should be drawn, such as Bounding Boxes.
  704. u32 DebugDataVisible;
  705. //! Is the node visible?
  706. bool IsVisible;
  707. //! Is debug object?
  708. bool IsDebugObject;
  709. };
  710. } // end namespace scene
  711. } // end namespace irr
  712. #endif