ISceneNode.h 28 KB

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