1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #ifndef __I_SCENE_NODE_H_INCLUDED__
\r
6 #define __I_SCENE_NODE_H_INCLUDED__
\r
8 #include "IReferenceCounted.h"
\r
9 #include "ESceneNodeTypes.h"
\r
10 #include "ECullingTypes.h"
\r
11 #include "EDebugSceneTypes.h"
\r
12 #include "SMaterial.h"
\r
13 #include "irrString.h"
\r
14 #include "aabbox3d.h"
\r
15 #include "matrix4.h"
\r
16 #include "IAttributes.h"
\r
24 class ISceneManager;
\r
26 //! Typedef for list of scene nodes
\r
27 typedef std::list<ISceneNode*> ISceneNodeList;
\r
29 //! Scene node interface.
\r
30 /** A scene node is a node in the hierarchical scene graph. Every scene
\r
31 node may have children, which are also scene nodes. Children move
\r
32 relative to their parent's position. If the parent of a node is not
\r
33 visible, its children won't be visible either. In this way, it is for
\r
34 example easily possible to attach a light to a moving car, or to place
\r
35 a walking character on a moving platform on a moving ship.
\r
37 class ISceneNode : virtual public IReferenceCounted
\r
42 ISceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id=-1,
\r
43 const core::vector3df& position = core::vector3df(0,0,0),
\r
44 const core::vector3df& rotation = core::vector3df(0,0,0),
\r
45 const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f))
\r
46 : RelativeTranslation(position), RelativeRotation(rotation), RelativeScale(scale),
\r
47 Parent(0), SceneManager(mgr), ID(id),
\r
48 AutomaticCullingState(EAC_BOX), DebugDataVisible(EDS_OFF),
\r
49 IsVisible(true), IsDebugObject(false)
\r
52 parent->addChild(this);
\r
54 updateAbsolutePosition();
\r
59 virtual ~ISceneNode()
\r
61 // delete all children
\r
66 //! This method is called just before the rendering process of the whole scene.
\r
67 /** Nodes may register themselves in the render pipeline during this call,
\r
68 precalculate the geometry which should be renderered, and prevent their
\r
69 children from being able to register themselves if they are clipped by simply
\r
70 not calling their OnRegisterSceneNode method.
\r
71 If you are implementing your own scene node, you should overwrite this method
\r
72 with an implementation code looking like this:
\r
75 SceneManager->registerNodeForRendering(this);
\r
77 ISceneNode::OnRegisterSceneNode();
\r
80 virtual void OnRegisterSceneNode()
\r
84 ISceneNodeList::iterator it = Children.begin();
\r
85 for (; it != Children.end(); ++it)
\r
86 (*it)->OnRegisterSceneNode();
\r
91 //! OnAnimate() is called just before rendering the whole scene.
\r
92 /** Nodes may calculate or store animations here, and may do other useful things,
\r
93 depending on what they are. Also, OnAnimate() should be called for all
\r
94 child scene nodes here. This method will be called once per frame, independent
\r
95 of whether the scene node is visible or not.
\r
96 \param timeMs Current time in milliseconds. */
\r
97 virtual void OnAnimate(u32 timeMs)
\r
101 // update absolute position
\r
102 updateAbsolutePosition();
\r
104 // perform the post render process on all children
\r
106 ISceneNodeList::iterator it = Children.begin();
\r
107 for (; it != Children.end(); ++it)
\r
108 (*it)->OnAnimate(timeMs);
\r
113 //! Renders the node.
\r
114 virtual void render() = 0;
\r
117 //! Returns the name of the node.
\r
118 /** \return Name as character string. */
\r
119 virtual const c8* getName() const
\r
121 return Name.c_str();
\r
125 //! Sets the name of the node.
\r
126 /** \param name New name of the scene node. */
\r
127 virtual void setName(const c8* name)
\r
133 //! Sets the name of the node.
\r
134 /** \param name New name of the scene node. */
\r
135 virtual void setName(const core::stringc& name)
\r
141 //! Get the axis aligned, not transformed bounding box of this node.
\r
142 /** This means that if this node is an animated 3d character,
\r
143 moving in a room, the bounding box will always be around the
\r
144 origin. To get the box in real world coordinates, just
\r
145 transform it with the matrix you receive with
\r
146 getAbsoluteTransformation() or simply use
\r
147 getTransformedBoundingBox(), which does the same.
\r
148 \return The non-transformed bounding box. */
\r
149 virtual const core::aabbox3d<f32>& getBoundingBox() const = 0;
\r
152 //! Get the axis aligned, transformed and animated absolute bounding box of this node.
\r
153 /** Note: The result is still an axis-aligned bounding box, so it's size
\r
154 changes with rotation.
\r
155 \return The transformed bounding box. */
\r
156 virtual const core::aabbox3d<f32> getTransformedBoundingBox() const
\r
158 core::aabbox3d<f32> box = getBoundingBox();
\r
159 AbsoluteTransformation.transformBoxEx(box);
\r
163 //! Get a the 8 corners of the original bounding box transformed and
\r
164 //! animated by the absolute transformation.
\r
165 /** Note: The result is _not_ identical to getTransformedBoundingBox().getEdges(),
\r
166 but getting an aabbox3d of these edges would then be identical.
\r
167 \param edges Receives an array with the transformed edges */
\r
168 virtual void getTransformedBoundingBoxEdges(core::array< core::vector3d<f32> >& edges) const
\r
171 getBoundingBox().getEdges( edges.pointer() );
\r
172 for ( u32 i=0; i<8; ++i )
\r
173 AbsoluteTransformation.transformVect( edges[i] );
\r
176 //! Get the absolute transformation of the node. Is recalculated every OnAnimate()-call.
\r
177 /** NOTE: For speed reasons the absolute transformation is not
\r
178 automatically recalculated on each change of the relative
\r
179 transformation or by a transformation change of an parent. Instead the
\r
180 update usually happens once per frame in OnAnimate. You can enforce
\r
181 an update with updateAbsolutePosition().
\r
182 \return The absolute transformation matrix. */
\r
183 virtual const core::matrix4& getAbsoluteTransformation() const
\r
185 return AbsoluteTransformation;
\r
189 //! Returns the relative transformation of the scene node.
\r
190 /** The relative transformation is stored internally as 3
\r
191 vectors: translation, rotation and scale. To get the relative
\r
192 transformation matrix, it is calculated from these values.
\r
193 \return The relative transformation matrix. */
\r
194 virtual core::matrix4 getRelativeTransformation() const
\r
197 mat.setRotationDegrees(RelativeRotation);
\r
198 mat.setTranslation(RelativeTranslation);
\r
200 if (RelativeScale != core::vector3df(1.f,1.f,1.f))
\r
202 core::matrix4 smat;
\r
203 smat.setScale(RelativeScale);
\r
211 //! Returns whether the node should be visible (if all of its parents are visible).
\r
212 /** This is only an option set by the user, but has nothing to
\r
213 do with geometry culling
\r
214 \return The requested visibility of the node, true means
\r
215 visible (if all parents are also visible). */
\r
216 virtual bool isVisible() const
\r
221 //! Check whether the node is truly visible, taking into accounts its parents' visibility
\r
222 /** \return true if the node and all its parents are visible,
\r
223 false if this or any parent node is invisible. */
\r
224 virtual bool isTrulyVisible() const
\r
232 return Parent->isTrulyVisible();
\r
235 //! Sets if the node should be visible or not.
\r
236 /** All children of this node won't be visible either, when set
\r
237 to false. Invisible nodes are not valid candidates for selection by
\r
238 collision manager bounding box methods.
\r
239 \param isVisible If the node shall be visible. */
\r
240 virtual void setVisible(bool isVisible)
\r
242 IsVisible = isVisible;
\r
246 //! Get the id of the scene node.
\r
247 /** This id can be used to identify the node.
\r
249 virtual s32 getID() const
\r
255 //! Sets the id of the scene node.
\r
256 /** This id can be used to identify the node.
\r
257 \param id The new id. */
\r
258 virtual void setID(s32 id)
\r
264 //! Adds a child to this scene node.
\r
265 /** If the scene node already has a parent it is first removed
\r
266 from the other parent.
\r
267 \param child A pointer to the new child. */
\r
268 virtual void addChild(ISceneNode* child)
\r
270 if (child && (child != this))
\r
272 // Change scene manager?
\r
273 if (SceneManager != child->SceneManager)
\r
274 child->setSceneManager(SceneManager);
\r
277 child->remove(); // remove from old parent
\r
278 Children.push_back(child);
\r
279 child->Parent = this;
\r
284 //! Removes a child from this scene node.
\r
285 /** If found in the children list, the child pointer is also
\r
286 dropped and might be deleted if no other grab exists.
\r
287 \param child A pointer to the child which shall be removed.
\r
288 \return True if the child was removed, and false if not,
\r
289 e.g. because it couldn't be found in the children list. */
\r
290 virtual bool removeChild(ISceneNode* child)
\r
292 ISceneNodeList::iterator it = Children.begin();
\r
293 for (; it != Children.end(); ++it)
\r
294 if ((*it) == child)
\r
298 Children.erase(it);
\r
306 //! Removes all children of this scene node
\r
307 /** The scene nodes found in the children list are also dropped
\r
308 and might be deleted if no other grab exists on them.
\r
310 virtual void removeAll()
\r
312 ISceneNodeList::iterator it = Children.begin();
\r
313 for (; it != Children.end(); ++it)
\r
323 //! Removes this scene node from the scene
\r
324 /** If no other grab exists for this node, it will be deleted.
\r
326 virtual void remove()
\r
329 Parent->removeChild(this);
\r
333 //! Returns the material based on the zero based index i.
\r
334 /** To get the amount of materials used by this scene node, use
\r
335 getMaterialCount(). This function is needed for inserting the
\r
336 node into the scene hierarchy at an optimal position for
\r
337 minimizing renderstate changes, but can also be used to
\r
338 directly modify the material of a scene node.
\r
339 \param num Zero based index. The maximal value is getMaterialCount() - 1.
\r
340 \return The material at that index. */
\r
341 virtual video::SMaterial& getMaterial(u32 num)
\r
343 return video::IdentityMaterial;
\r
347 //! Get amount of materials used by this scene node.
\r
348 /** \return Current amount of materials of this scene node. */
\r
349 virtual u32 getMaterialCount() const
\r
355 //! Sets all material flags at once to a new value.
\r
356 /** Useful, for example, if you want the whole mesh to be
\r
358 \param flag Which flag of all materials to be set.
\r
359 \param newvalue New value of that flag. */
\r
360 void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
\r
362 for (u32 i=0; i<getMaterialCount(); ++i)
\r
363 getMaterial(i).setFlag(flag, newvalue);
\r
367 //! Sets the texture of the specified layer in all materials of this scene node to the new texture.
\r
368 /** \param textureLayer Layer of texture to be set. Must be a
\r
369 value smaller than MATERIAL_MAX_TEXTURES.
\r
370 \param texture New texture to be used. */
\r
371 void setMaterialTexture(u32 textureLayer, video::ITexture* texture)
\r
373 if (textureLayer >= video::MATERIAL_MAX_TEXTURES)
\r
376 for (u32 i=0; i<getMaterialCount(); ++i)
\r
377 getMaterial(i).setTexture(textureLayer, texture);
\r
381 //! Sets the material type of all materials in this scene node to a new material type.
\r
382 /** \param newType New type of material to be set. */
\r
383 void setMaterialType(video::E_MATERIAL_TYPE newType)
\r
385 for (u32 i=0; i<getMaterialCount(); ++i)
\r
386 getMaterial(i).MaterialType = newType;
\r
390 //! Gets the scale of the scene node relative to its parent.
\r
391 /** This is the scale of this node relative to its parent.
\r
392 If you want the absolute scale, use
\r
393 getAbsoluteTransformation().getScale()
\r
394 \return The scale of the scene node. */
\r
395 virtual const core::vector3df& getScale() const
\r
397 return RelativeScale;
\r
401 //! Sets the relative scale of the scene node.
\r
402 /** \param scale New scale of the node, relative to its parent. */
\r
403 virtual void setScale(const core::vector3df& scale)
\r
405 RelativeScale = scale;
\r
409 //! Gets the rotation of the node relative to its parent.
\r
410 /** Note that this is the relative rotation of the node.
\r
411 If you want the absolute rotation, use
\r
412 getAbsoluteTransformation().getRotation()
\r
413 \return Current relative rotation of the scene node. */
\r
414 virtual const core::vector3df& getRotation() const
\r
416 return RelativeRotation;
\r
420 //! Sets the rotation of the node relative to its parent.
\r
421 /** This only modifies the relative rotation of the node.
\r
422 \param rotation New rotation of the node in degrees. */
\r
423 virtual void setRotation(const core::vector3df& rotation)
\r
425 RelativeRotation = rotation;
\r
429 //! Gets the position of the node relative to its parent.
\r
430 /** Note that the position is relative to the parent. If you want
\r
431 the position in world coordinates, use getAbsolutePosition() instead.
\r
432 \return The current position of the node relative to the parent. */
\r
433 virtual const core::vector3df& getPosition() const
\r
435 return RelativeTranslation;
\r
439 //! Sets the position of the node relative to its parent.
\r
440 /** Note that the position is relative to the parent.
\r
441 \param newpos New relative position of the scene node. */
\r
442 virtual void setPosition(const core::vector3df& newpos)
\r
444 RelativeTranslation = newpos;
\r
448 //! Gets the absolute position of the node in world coordinates.
\r
449 /** If you want the position of the node relative to its parent,
\r
450 use getPosition() instead.
\r
451 NOTE: For speed reasons the absolute position is not
\r
452 automatically recalculated on each change of the relative
\r
453 position or by a position change of an parent. Instead the
\r
454 update usually happens once per frame in OnAnimate. You can enforce
\r
455 an update with updateAbsolutePosition().
\r
456 \return The current absolute position of the scene node (updated on last call of updateAbsolutePosition). */
\r
457 virtual core::vector3df getAbsolutePosition() const
\r
459 return AbsoluteTransformation.getTranslation();
\r
463 //! Set a culling style or disable culling completely.
\r
464 /** Box cullling (EAC_BOX) is set by default. Note that not
\r
465 all SceneNodes support culling and that some nodes always cull
\r
466 their geometry because it is their only reason for existence,
\r
467 for example the OctreeSceneNode.
\r
468 \param state The culling state to be used. Check E_CULLING_TYPE for possible values.*/
\r
469 void setAutomaticCulling( u32 state)
\r
471 AutomaticCullingState = state;
\r
475 //! Gets the automatic culling state.
\r
476 /** \return The automatic culling state. */
\r
477 u32 getAutomaticCulling() const
\r
479 return AutomaticCullingState;
\r
483 //! Sets if debug data like bounding boxes should be drawn.
\r
484 /** A bitwise OR of the types from @ref irr::scene::E_DEBUG_SCENE_TYPE.
\r
485 Please note that not all scene nodes support all debug data types.
\r
486 \param state The debug data visibility state to be used. */
\r
487 virtual void setDebugDataVisible(u32 state)
\r
489 DebugDataVisible = state;
\r
492 //! Returns if debug data like bounding boxes are drawn.
\r
493 /** \return A bitwise OR of the debug data values from
\r
494 @ref irr::scene::E_DEBUG_SCENE_TYPE that are currently visible. */
\r
495 u32 isDebugDataVisible() const
\r
497 return DebugDataVisible;
\r
501 //! Sets if this scene node is a debug object.
\r
502 /** Debug objects have some special properties, for example they can be easily
\r
503 excluded from collision detection or from serialization, etc. */
\r
504 void setIsDebugObject(bool debugObject)
\r
506 IsDebugObject = debugObject;
\r
510 //! Returns if this scene node is a debug object.
\r
511 /** Debug objects have some special properties, for example they can be easily
\r
512 excluded from collision detection or from serialization, etc.
\r
513 \return If this node is a debug object, true is returned. */
\r
514 bool isDebugObject() const
\r
516 return IsDebugObject;
\r
520 //! Returns a const reference to the list of all children.
\r
521 /** \return The list of all children of this node. */
\r
522 const std::list<ISceneNode*>& getChildren() const
\r
528 //! Changes the parent of the scene node.
\r
529 /** \param newParent The new parent to be used. */
\r
530 virtual void setParent(ISceneNode* newParent)
\r
535 Parent = newParent;
\r
538 Parent->addChild(this);
\r
544 //! Updates the absolute position based on the relative and the parents position
\r
545 /** Note: This does not recursively update the parents absolute positions, so if you have a deeper
\r
546 hierarchy you might want to update the parents first.*/
\r
547 virtual void updateAbsolutePosition()
\r
551 AbsoluteTransformation =
\r
552 Parent->getAbsoluteTransformation() * getRelativeTransformation();
\r
555 AbsoluteTransformation = getRelativeTransformation();
\r
559 //! Returns the parent of this scene node
\r
560 /** \return A pointer to the parent. */
\r
561 scene::ISceneNode* getParent() const
\r
567 //! Returns type of the scene node
\r
568 /** \return The type of this node. */
\r
569 virtual ESCENE_NODE_TYPE getType() const
\r
571 return ESNT_UNKNOWN;
\r
574 //! Creates a clone of this scene node and its children.
\r
575 /** \param newParent An optional new parent.
\r
576 \param newManager An optional new scene manager.
\r
577 \return The newly created clone of this node. */
\r
578 virtual ISceneNode* clone(ISceneNode* newParent=0, ISceneManager* newManager=0)
\r
580 return 0; // to be implemented by derived classes
\r
583 //! Retrieve the scene manager for this node.
\r
584 /** \return The node's scene manager. */
\r
585 virtual ISceneManager* getSceneManager(void) const { return SceneManager; }
\r
589 //! A clone function for the ISceneNode members.
\r
590 /** This method can be used by clone() implementations of
\r
592 \param toCopyFrom The node from which the values are copied
\r
593 \param newManager The new scene manager. */
\r
594 void cloneMembers(ISceneNode* toCopyFrom, ISceneManager* newManager)
\r
596 Name = toCopyFrom->Name;
\r
597 AbsoluteTransformation = toCopyFrom->AbsoluteTransformation;
\r
598 RelativeTranslation = toCopyFrom->RelativeTranslation;
\r
599 RelativeRotation = toCopyFrom->RelativeRotation;
\r
600 RelativeScale = toCopyFrom->RelativeScale;
\r
601 ID = toCopyFrom->ID;
\r
602 AutomaticCullingState = toCopyFrom->AutomaticCullingState;
\r
603 DebugDataVisible = toCopyFrom->DebugDataVisible;
\r
604 IsVisible = toCopyFrom->IsVisible;
\r
605 IsDebugObject = toCopyFrom->IsDebugObject;
\r
608 SceneManager = newManager;
\r
610 SceneManager = toCopyFrom->SceneManager;
\r
614 ISceneNodeList::iterator it = toCopyFrom->Children.begin();
\r
615 for (; it != toCopyFrom->Children.end(); ++it)
\r
616 (*it)->clone(this, newManager);
\r
619 //! Sets the new scene manager for this node and all children.
\r
620 //! Called by addChild when moving nodes between scene managers
\r
621 void setSceneManager(ISceneManager* newManager)
\r
623 SceneManager = newManager;
\r
625 ISceneNodeList::iterator it = Children.begin();
\r
626 for (; it != Children.end(); ++it)
\r
627 (*it)->setSceneManager(newManager);
\r
630 //! Name of the scene node.
\r
631 core::stringc Name;
\r
633 //! Absolute transformation of the node.
\r
634 core::matrix4 AbsoluteTransformation;
\r
636 //! Relative translation of the scene node.
\r
637 core::vector3df RelativeTranslation;
\r
639 //! Relative rotation of the scene node.
\r
640 core::vector3df RelativeRotation;
\r
642 //! Relative scale of the scene node.
\r
643 core::vector3df RelativeScale;
\r
645 //! Pointer to the parent
\r
646 ISceneNode* Parent;
\r
648 //! List of all children of this node
\r
649 std::list<ISceneNode*> Children;
\r
651 //! Pointer to the scene manager
\r
652 ISceneManager* SceneManager;
\r
654 //! ID of the node.
\r
657 //! Automatic culling state
\r
658 u32 AutomaticCullingState;
\r
660 //! Flag if debug data should be drawn, such as Bounding Boxes.
\r
661 u32 DebugDataVisible;
\r
663 //! Is the node visible?
\r
666 //! Is debug object?
\r
667 bool IsDebugObject;
\r
671 } // end namespace scene
\r
672 } // end namespace irr
\r