2
0

ISceneNode.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. #pragma once
  5. #include "IReferenceCounted.h"
  6. #include "ESceneNodeTypes.h"
  7. #include "ECullingTypes.h"
  8. #include "EDebugSceneTypes.h"
  9. #include "SMaterial.h"
  10. #include "irrString.h"
  11. #include "aabbox3d.h"
  12. #include "matrix4.h"
  13. #include "IAttributes.h"
  14. #include <list>
  15. #include <optional>
  16. namespace irr
  17. {
  18. namespace scene
  19. {
  20. class ISceneNode;
  21. class ISceneManager;
  22. //! Typedef for list of scene nodes
  23. typedef std::list<ISceneNode *> ISceneNodeList;
  24. //! Scene node interface.
  25. /** A scene node is a node in the hierarchical scene graph. Every scene
  26. node may have children, which are also scene nodes. Children move
  27. relative to their parent's position. If the parent of a node is not
  28. visible, its children won't be visible either. In this way, it is for
  29. example easily possible to attach a light to a moving car, or to place
  30. a walking character on a moving platform on a moving ship.
  31. */
  32. class ISceneNode : virtual public IReferenceCounted
  33. {
  34. public:
  35. //! Constructor
  36. ISceneNode(ISceneNode *parent, ISceneManager *mgr, s32 id = -1,
  37. const core::vector3df &position = core::vector3df(0, 0, 0),
  38. const core::vector3df &rotation = core::vector3df(0, 0, 0),
  39. const core::vector3df &scale = core::vector3df(1.0f, 1.0f, 1.0f)) :
  40. RelativeTranslation(position),
  41. RelativeRotation(rotation), RelativeScale(scale),
  42. Parent(0), SceneManager(mgr), ID(id),
  43. AutomaticCullingState(EAC_BOX), DebugDataVisible(EDS_OFF),
  44. IsVisible(true), IsDebugObject(false)
  45. {
  46. if (parent)
  47. parent->addChild(this);
  48. updateAbsolutePosition();
  49. }
  50. //! Destructor
  51. virtual ~ISceneNode()
  52. {
  53. // delete all children
  54. removeAll();
  55. }
  56. //! This method is called just before the rendering process of the whole scene.
  57. /** Nodes may register themselves in the render pipeline during this call,
  58. precalculate the geometry which should be rendered, and prevent their
  59. children from being able to register themselves if they are clipped by simply
  60. not calling their OnRegisterSceneNode method.
  61. If you are implementing your own scene node, you should overwrite this method
  62. with an implementation code looking like this:
  63. \code
  64. if (IsVisible)
  65. SceneManager->registerNodeForRendering(this);
  66. ISceneNode::OnRegisterSceneNode();
  67. \endcode
  68. */
  69. virtual void OnRegisterSceneNode()
  70. {
  71. if (IsVisible) {
  72. ISceneNodeList::iterator it = Children.begin();
  73. for (; it != Children.end(); ++it)
  74. (*it)->OnRegisterSceneNode();
  75. }
  76. }
  77. //! OnAnimate() is called just before rendering the whole scene.
  78. /** Nodes may calculate or store animations here, and may do other useful things,
  79. depending on what they are. Also, OnAnimate() should be called for all
  80. child scene nodes here. This method will be called once per frame, independent
  81. of whether the scene node is visible or not.
  82. \param timeMs Current time in milliseconds. */
  83. virtual void OnAnimate(u32 timeMs)
  84. {
  85. if (IsVisible) {
  86. // update absolute position
  87. updateAbsolutePosition();
  88. // perform the post render process on all children
  89. ISceneNodeList::iterator it = Children.begin();
  90. for (; it != Children.end(); ++it)
  91. (*it)->OnAnimate(timeMs);
  92. }
  93. }
  94. //! Renders the node.
  95. virtual void render() = 0;
  96. //! Returns the name of the node.
  97. /** \return Name as character string. */
  98. virtual const std::optional<std::string> &getName() const
  99. {
  100. return Name;
  101. }
  102. //! Sets the name of the node.
  103. /** \param name New name of the scene node. */
  104. virtual void setName(const std::optional<std::string> &name)
  105. {
  106. Name = name;
  107. }
  108. //! Get the axis aligned, not transformed bounding box of this node.
  109. /** This means that if this node is an animated 3d character,
  110. moving in a room, the bounding box will always be around the
  111. origin. To get the box in real world coordinates, just
  112. transform it with the matrix you receive with
  113. getAbsoluteTransformation() or simply use
  114. getTransformedBoundingBox(), which does the same.
  115. \return The non-transformed bounding box. */
  116. virtual const core::aabbox3d<f32> &getBoundingBox() const = 0;
  117. //! Get the axis aligned, transformed and animated absolute bounding box of this node.
  118. /** Note: The result is still an axis-aligned bounding box, so it's size
  119. changes with rotation.
  120. \return The transformed bounding box. */
  121. virtual const core::aabbox3d<f32> getTransformedBoundingBox() const
  122. {
  123. core::aabbox3d<f32> box = getBoundingBox();
  124. AbsoluteTransformation.transformBoxEx(box);
  125. return box;
  126. }
  127. //! Get a the 8 corners of the original bounding box transformed and
  128. //! animated by the absolute transformation.
  129. /** Note: The result is _not_ identical to getTransformedBoundingBox().getEdges(),
  130. but getting an aabbox3d of these edges would then be identical.
  131. \param edges Receives an array with the transformed edges */
  132. virtual void getTransformedBoundingBoxEdges(core::array<core::vector3d<f32>> &edges) const
  133. {
  134. edges.set_used(8);
  135. getBoundingBox().getEdges(edges.pointer());
  136. for (u32 i = 0; i < 8; ++i)
  137. AbsoluteTransformation.transformVect(edges[i]);
  138. }
  139. //! Get the absolute transformation of the node. Is recalculated every OnAnimate()-call.
  140. /** NOTE: For speed reasons the absolute transformation is not
  141. automatically recalculated on each change of the relative
  142. transformation or by a transformation change of an parent. Instead the
  143. update usually happens once per frame in OnAnimate. You can enforce
  144. an update with updateAbsolutePosition().
  145. \return The absolute transformation matrix. */
  146. virtual const core::matrix4 &getAbsoluteTransformation() const
  147. {
  148. return AbsoluteTransformation;
  149. }
  150. //! Returns the relative transformation of the scene node.
  151. /** The relative transformation is stored internally as 3
  152. vectors: translation, rotation and scale. To get the relative
  153. transformation matrix, it is calculated from these values.
  154. \return The relative transformation matrix. */
  155. virtual core::matrix4 getRelativeTransformation() const
  156. {
  157. core::matrix4 mat;
  158. mat.setRotationDegrees(RelativeRotation);
  159. mat.setTranslation(RelativeTranslation);
  160. if (RelativeScale != core::vector3df(1.f, 1.f, 1.f)) {
  161. core::matrix4 smat;
  162. smat.setScale(RelativeScale);
  163. mat *= smat;
  164. }
  165. return mat;
  166. }
  167. //! Returns whether the node should be visible (if all of its parents are visible).
  168. /** This is only an option set by the user, but has nothing to
  169. do with geometry culling
  170. \return The requested visibility of the node, true means
  171. visible (if all parents are also visible). */
  172. virtual bool isVisible() const
  173. {
  174. return IsVisible;
  175. }
  176. //! Check whether the node is truly visible, taking into accounts its parents' visibility
  177. /** \return true if the node and all its parents are visible,
  178. false if this or any parent node is invisible. */
  179. virtual bool isTrulyVisible() const
  180. {
  181. if (!IsVisible)
  182. return false;
  183. if (!Parent)
  184. return true;
  185. return Parent->isTrulyVisible();
  186. }
  187. //! Sets if the node should be visible or not.
  188. /** All children of this node won't be visible either, when set
  189. to false. Invisible nodes are not valid candidates for selection by
  190. collision manager bounding box methods.
  191. \param isVisible If the node shall be visible. */
  192. virtual void setVisible(bool isVisible)
  193. {
  194. IsVisible = isVisible;
  195. }
  196. //! Get the id of the scene node.
  197. /** This id can be used to identify the node.
  198. \return The id. */
  199. virtual s32 getID() const
  200. {
  201. return ID;
  202. }
  203. //! Sets the id of the scene node.
  204. /** This id can be used to identify the node.
  205. \param id The new id. */
  206. virtual void setID(s32 id)
  207. {
  208. ID = id;
  209. }
  210. //! Adds a child to this scene node.
  211. /** If the scene node already has a parent it is first removed
  212. from the other parent.
  213. \param child A pointer to the new child. */
  214. virtual void addChild(ISceneNode *child)
  215. {
  216. if (child && (child != this)) {
  217. // Change scene manager?
  218. if (SceneManager != child->SceneManager)
  219. child->setSceneManager(SceneManager);
  220. child->grab();
  221. child->remove(); // remove from old parent
  222. // Note: This iterator is not invalidated until we erase it.
  223. child->ThisIterator = Children.insert(Children.end(), child);
  224. child->Parent = this;
  225. }
  226. }
  227. //! Removes a child from this scene node.
  228. /**
  229. \param child A pointer to the child which shall be removed.
  230. \return True if the child was removed, and false if not,
  231. e.g. because it belongs to a different parent or no parent. */
  232. virtual bool removeChild(ISceneNode *child)
  233. {
  234. if (child->Parent != this)
  235. return false;
  236. // The iterator must be set since the parent is not null.
  237. _IRR_DEBUG_BREAK_IF(!child->ThisIterator.has_value());
  238. auto it = *child->ThisIterator;
  239. child->ThisIterator = std::nullopt;
  240. child->Parent = nullptr;
  241. child->drop();
  242. Children.erase(it);
  243. return true;
  244. }
  245. //! Removes all children of this scene node
  246. /** The scene nodes found in the children list are also dropped
  247. and might be deleted if no other grab exists on them.
  248. */
  249. virtual void removeAll()
  250. {
  251. for (auto &child : Children) {
  252. child->Parent = nullptr;
  253. child->ThisIterator = std::nullopt;
  254. child->drop();
  255. }
  256. Children.clear();
  257. }
  258. //! Removes this scene node from the scene
  259. /** If no other grab exists for this node, it will be deleted.
  260. */
  261. virtual void remove()
  262. {
  263. if (Parent)
  264. Parent->removeChild(this);
  265. }
  266. //! Returns the material based on the zero based index i.
  267. /** To get the amount of materials used by this scene node, use
  268. getMaterialCount(). This function is needed for inserting the
  269. node into the scene hierarchy at an optimal position for
  270. minimizing renderstate changes, but can also be used to
  271. directly modify the material of a scene node.
  272. \param num Zero based index. The maximal value is getMaterialCount() - 1.
  273. \return The material at that index. */
  274. virtual video::SMaterial &getMaterial(u32 num)
  275. {
  276. return video::IdentityMaterial;
  277. }
  278. //! Get amount of materials used by this scene node.
  279. /** \return Current amount of materials of this scene node. */
  280. virtual u32 getMaterialCount() const
  281. {
  282. return 0;
  283. }
  284. //! Execute a function on all materials of this scene node.
  285. /** Useful for setting material properties, e.g. if you want the whole
  286. mesh to be affected by light. */
  287. template <typename F>
  288. void forEachMaterial(F &&fn)
  289. {
  290. for (u32 i = 0; i < getMaterialCount(); i++) {
  291. fn(getMaterial(i));
  292. }
  293. }
  294. //! Gets the scale of the scene node relative to its parent.
  295. /** This is the scale of this node relative to its parent.
  296. If you want the absolute scale, use
  297. getAbsoluteTransformation().getScale()
  298. \return The scale of the scene node. */
  299. virtual const core::vector3df &getScale() const
  300. {
  301. return RelativeScale;
  302. }
  303. //! Sets the relative scale of the scene node.
  304. /** \param scale New scale of the node, relative to its parent. */
  305. virtual void setScale(const core::vector3df &scale)
  306. {
  307. RelativeScale = scale;
  308. }
  309. //! Gets the rotation of the node relative to its parent.
  310. /** Note that this is the relative rotation of the node.
  311. If you want the absolute rotation, use
  312. getAbsoluteTransformation().getRotation()
  313. \return Current relative rotation of the scene node. */
  314. virtual const core::vector3df &getRotation() const
  315. {
  316. return RelativeRotation;
  317. }
  318. //! Sets the rotation of the node relative to its parent.
  319. /** This only modifies the relative rotation of the node.
  320. \param rotation New rotation of the node in degrees. */
  321. virtual void setRotation(const core::vector3df &rotation)
  322. {
  323. RelativeRotation = rotation;
  324. }
  325. //! Gets the position of the node relative to its parent.
  326. /** Note that the position is relative to the parent. If you want
  327. the position in world coordinates, use getAbsolutePosition() instead.
  328. \return The current position of the node relative to the parent. */
  329. virtual const core::vector3df &getPosition() const
  330. {
  331. return RelativeTranslation;
  332. }
  333. //! Sets the position of the node relative to its parent.
  334. /** Note that the position is relative to the parent.
  335. \param newpos New relative position of the scene node. */
  336. virtual void setPosition(const core::vector3df &newpos)
  337. {
  338. RelativeTranslation = newpos;
  339. }
  340. //! Gets the absolute position of the node in world coordinates.
  341. /** If you want the position of the node relative to its parent,
  342. use getPosition() instead.
  343. NOTE: For speed reasons the absolute position is not
  344. automatically recalculated on each change of the relative
  345. position or by a position change of an parent. Instead the
  346. update usually happens once per frame in OnAnimate. You can enforce
  347. an update with updateAbsolutePosition().
  348. \return The current absolute position of the scene node (updated on last call of updateAbsolutePosition). */
  349. virtual core::vector3df getAbsolutePosition() const
  350. {
  351. return AbsoluteTransformation.getTranslation();
  352. }
  353. //! Set a culling style or disable culling completely.
  354. /** Box cullling (EAC_BOX) is set by default. Note that not
  355. all SceneNodes support culling and that some nodes always cull
  356. their geometry because it is their only reason for existence,
  357. for example the OctreeSceneNode.
  358. \param state The culling state to be used. Check E_CULLING_TYPE for possible values.*/
  359. void setAutomaticCulling(u32 state)
  360. {
  361. AutomaticCullingState = state;
  362. }
  363. //! Gets the automatic culling state.
  364. /** \return The automatic culling state. */
  365. u32 getAutomaticCulling() const
  366. {
  367. return AutomaticCullingState;
  368. }
  369. //! Sets if debug data like bounding boxes should be drawn.
  370. /** A bitwise OR of the types from @ref irr::scene::E_DEBUG_SCENE_TYPE.
  371. Please note that not all scene nodes support all debug data types.
  372. \param state The debug data visibility state to be used. */
  373. virtual void setDebugDataVisible(u32 state)
  374. {
  375. DebugDataVisible = state;
  376. }
  377. //! Returns if debug data like bounding boxes are drawn.
  378. /** \return A bitwise OR of the debug data values from
  379. @ref irr::scene::E_DEBUG_SCENE_TYPE that are currently visible. */
  380. u32 isDebugDataVisible() const
  381. {
  382. return DebugDataVisible;
  383. }
  384. //! Sets if this scene node is a debug object.
  385. /** Debug objects have some special properties, for example they can be easily
  386. excluded from collision detection or from serialization, etc. */
  387. void setIsDebugObject(bool debugObject)
  388. {
  389. IsDebugObject = debugObject;
  390. }
  391. //! Returns if this scene node is a debug object.
  392. /** Debug objects have some special properties, for example they can be easily
  393. excluded from collision detection or from serialization, etc.
  394. \return If this node is a debug object, true is returned. */
  395. bool isDebugObject() const
  396. {
  397. return IsDebugObject;
  398. }
  399. //! Returns a const reference to the list of all children.
  400. /** \return The list of all children of this node. */
  401. const std::list<ISceneNode *> &getChildren() const
  402. {
  403. return Children;
  404. }
  405. //! Changes the parent of the scene node.
  406. /** \param newParent The new parent to be used. */
  407. virtual void setParent(ISceneNode *newParent)
  408. {
  409. grab();
  410. remove();
  411. if (newParent)
  412. newParent->addChild(this);
  413. drop();
  414. }
  415. //! Updates the absolute position based on the relative and the parents position
  416. /** Note: This does not recursively update the parents absolute positions, so if you have a deeper
  417. hierarchy you might want to update the parents first.*/
  418. virtual void updateAbsolutePosition()
  419. {
  420. if (Parent) {
  421. AbsoluteTransformation =
  422. Parent->getAbsoluteTransformation() * getRelativeTransformation();
  423. } else
  424. AbsoluteTransformation = getRelativeTransformation();
  425. }
  426. //! Returns the parent of this scene node
  427. /** \return A pointer to the parent. */
  428. scene::ISceneNode *getParent() const
  429. {
  430. return Parent;
  431. }
  432. //! Returns type of the scene node
  433. /** \return The type of this node. */
  434. virtual ESCENE_NODE_TYPE getType() const
  435. {
  436. return ESNT_UNKNOWN;
  437. }
  438. //! Creates a clone of this scene node and its children.
  439. /** \param newParent An optional new parent.
  440. \param newManager An optional new scene manager.
  441. \return The newly created clone of this node. */
  442. virtual ISceneNode *clone(ISceneNode *newParent = 0, ISceneManager *newManager = 0)
  443. {
  444. return 0; // to be implemented by derived classes
  445. }
  446. //! Retrieve the scene manager for this node.
  447. /** \return The node's scene manager. */
  448. virtual ISceneManager *getSceneManager(void) const { return SceneManager; }
  449. protected:
  450. //! A clone function for the ISceneNode members.
  451. /** This method can be used by clone() implementations of
  452. derived classes
  453. \param toCopyFrom The node from which the values are copied
  454. \param newManager The new scene manager. */
  455. void cloneMembers(ISceneNode *toCopyFrom, ISceneManager *newManager)
  456. {
  457. Name = toCopyFrom->Name;
  458. AbsoluteTransformation = toCopyFrom->AbsoluteTransformation;
  459. RelativeTranslation = toCopyFrom->RelativeTranslation;
  460. RelativeRotation = toCopyFrom->RelativeRotation;
  461. RelativeScale = toCopyFrom->RelativeScale;
  462. ID = toCopyFrom->ID;
  463. AutomaticCullingState = toCopyFrom->AutomaticCullingState;
  464. DebugDataVisible = toCopyFrom->DebugDataVisible;
  465. IsVisible = toCopyFrom->IsVisible;
  466. IsDebugObject = toCopyFrom->IsDebugObject;
  467. if (newManager)
  468. SceneManager = newManager;
  469. else
  470. SceneManager = toCopyFrom->SceneManager;
  471. // clone children
  472. ISceneNodeList::iterator it = toCopyFrom->Children.begin();
  473. for (; it != toCopyFrom->Children.end(); ++it)
  474. (*it)->clone(this, newManager);
  475. }
  476. //! Sets the new scene manager for this node and all children.
  477. //! Called by addChild when moving nodes between scene managers
  478. void setSceneManager(ISceneManager *newManager)
  479. {
  480. SceneManager = newManager;
  481. ISceneNodeList::iterator it = Children.begin();
  482. for (; it != Children.end(); ++it)
  483. (*it)->setSceneManager(newManager);
  484. }
  485. //! Name of the scene node.
  486. std::optional<std::string> Name;
  487. //! Absolute transformation of the node.
  488. core::matrix4 AbsoluteTransformation;
  489. //! Relative translation of the scene node.
  490. core::vector3df RelativeTranslation;
  491. //! Relative rotation of the scene node.
  492. core::vector3df RelativeRotation;
  493. //! Relative scale of the scene node.
  494. core::vector3df RelativeScale;
  495. //! List of all children of this node
  496. std::list<ISceneNode *> Children;
  497. //! Iterator pointing to this node in the parent's child list.
  498. std::optional<ISceneNodeList::iterator> ThisIterator;
  499. //! Pointer to the parent
  500. ISceneNode *Parent;
  501. //! Pointer to the scene manager
  502. ISceneManager *SceneManager;
  503. //! ID of the node.
  504. s32 ID;
  505. //! Automatic culling state
  506. u32 AutomaticCullingState;
  507. //! Flag if debug data should be drawn, such as Bounding Boxes.
  508. u32 DebugDataVisible;
  509. //! Is the node visible?
  510. bool IsVisible;
  511. //! Is debug object?
  512. bool IsDebugObject;
  513. };
  514. } // end namespace scene
  515. } // end namespace irr