2
0

ISceneNode.h 18 KB

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