Irrlicht 3D Engine
ISceneNode.h
Go to the documentation of this file.
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
5#ifndef __I_SCENE_NODE_H_INCLUDED__
6#define __I_SCENE_NODE_H_INCLUDED__
7
9#include "ESceneNodeTypes.h"
10#include "ECullingTypes.h"
11#include "EDebugSceneTypes.h"
12#include "ISceneNodeAnimator.h"
13#include "ITriangleSelector.h"
14#include "SMaterial.h"
15#include "irrString.h"
16#include "aabbox3d.h"
17#include "matrix4.h"
18#include "irrList.h"
19#include "IAttributes.h"
20
21namespace irr
22{
23namespace scene
24{
25 class ISceneManager;
26
31
33
41 {
42 public:
43
45 ISceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id=-1,
46 const core::vector3df& position = core::vector3df(0,0,0),
47 const core::vector3df& rotation = core::vector3df(0,0,0),
48 const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f))
49 : RelativeTranslation(position), RelativeRotation(rotation), RelativeScale(scale),
50 Parent(0), SceneManager(mgr), TriangleSelector(0), ID(id),
52 IsVisible(true), IsDebugObject(false)
53 {
54 if (parent)
55 parent->addChild(this);
56
58 }
59
60
62 virtual ~ISceneNode()
63 {
64 // delete all children
65 removeAll();
66
67 // delete all animators
69 for (; ait != Animators.end(); ++ait)
70 (*ait)->drop();
71
74 }
75
76
78
91 virtual void OnRegisterSceneNode()
92 {
93 if (IsVisible)
94 {
96 for (; it != Children.end(); ++it)
97 (*it)->OnRegisterSceneNode();
98 }
99 }
100
101
103
108 virtual void OnAnimate(u32 timeMs)
109 {
110 if (IsVisible)
111 {
112 // animate this node with all animators
113
115 while (ait != Animators.end())
116 {
117 // continue to the next node before calling animateNode()
118 // so that the animator may remove itself from the scene
119 // node without the iterator becoming invalid
120 ISceneNodeAnimator* anim = *ait;
121 ++ait;
122 anim->animateNode(this, timeMs);
123 }
124
125 // update absolute position
127
128 // perform the post render process on all children
129
131 for (; it != Children.end(); ++it)
132 (*it)->OnAnimate(timeMs);
133 }
134 }
135
136
138 virtual void render() = 0;
139
140
142
143 virtual const c8* getName() const
144 {
145 return Name.c_str();
146 }
147
148
150
151 virtual void setName(const c8* name)
152 {
153 Name = name;
154 }
155
156
158
159 virtual void setName(const core::stringc& name)
160 {
161 Name = name;
162 }
163
164
166
173 virtual const core::aabbox3d<f32>& getBoundingBox() const = 0;
174
175
177
179 {
182 return box;
183 }
184
185
187
194 {
196 }
197
198
200
205 {
206 core::matrix4 mat;
209
210 if (RelativeScale != core::vector3df(1.f,1.f,1.f))
211 {
212 core::matrix4 smat;
214 mat *= smat;
215 }
216
217 return mat;
218 }
219
220
222
226 virtual bool isVisible() const
227 {
229 return IsVisible;
230 }
231
233
235 virtual bool isTrulyVisible() const
236 {
238 if(!IsVisible)
239 return false;
240
241 if(!Parent)
242 return true;
243
244 return Parent->isTrulyVisible();
245 }
246
248
252 virtual void setVisible(bool isVisible)
253 {
255 }
256
257
259
261 virtual s32 getID() const
262 {
263 return ID;
264 }
265
266
268
270 virtual void setID(s32 id)
271 {
272 ID = id;
273 }
274
275
277
280 virtual void addChild(ISceneNode* child)
281 {
282 if (child && (child != this))
283 {
284 // Change scene manager?
285 if (SceneManager != child->SceneManager)
287
288 child->grab();
289 child->remove(); // remove from old parent
290 Children.push_back(child);
291 child->Parent = this;
292 }
293 }
294
295
297
302 virtual bool removeChild(ISceneNode* child)
303 {
305 for (; it != Children.end(); ++it)
306 if ((*it) == child)
307 {
308 (*it)->Parent = 0;
309 (*it)->drop();
310 Children.erase(it);
311 return true;
312 }
313
315 return false;
316 }
317
318
320
323 virtual void removeAll()
324 {
326 for (; it != Children.end(); ++it)
327 {
328 (*it)->Parent = 0;
329 (*it)->drop();
330 }
331
332 Children.clear();
333 }
334
335
337
339 virtual void remove()
340 {
341 if (Parent)
342 Parent->removeChild(this);
343 }
344
345
347
348 virtual void addAnimator(ISceneNodeAnimator* animator)
349 {
350 if (animator)
351 {
352 Animators.push_back(animator);
353 animator->grab();
354 }
355 }
356
357
359
361 {
362 return Animators;
363 }
364
365
367
370 virtual void removeAnimator(ISceneNodeAnimator* animator)
371 {
373 for (; it != Animators.end(); ++it)
374 {
375 if ((*it) == animator)
376 {
377 (*it)->drop();
378 Animators.erase(it);
379 return;
380 }
381 }
382 }
383
384
386
388 virtual void removeAnimators()
389 {
391 for (; it != Animators.end(); ++it)
392 (*it)->drop();
393
394 Animators.clear();
395 }
396
397
399
407 {
409 }
410
411
413
414 virtual u32 getMaterialCount() const
415 {
416 return 0;
417 }
418
419
421
425 void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
426 {
427 for (u32 i=0; i<getMaterialCount(); ++i)
428 getMaterial(i).setFlag(flag, newvalue);
429 }
430
431
433
436 void setMaterialTexture(u32 textureLayer, video::ITexture* texture)
437 {
438 if (textureLayer >= video::MATERIAL_MAX_TEXTURES)
439 return;
440
441 for (u32 i=0; i<getMaterialCount(); ++i)
442 getMaterial(i).setTexture(textureLayer, texture);
443 }
444
445
447
449 {
450 for (u32 i=0; i<getMaterialCount(); ++i)
451 getMaterial(i).MaterialType = newType;
452 }
453
454
456
460 virtual const core::vector3df& getScale() const
461 {
462 return RelativeScale;
463 }
464
465
467
468 virtual void setScale(const core::vector3df& scale)
469 {
470 RelativeScale = scale;
471 }
472
473
475
479 virtual const core::vector3df& getRotation() const
480 {
481 return RelativeRotation;
482 }
483
484
486
488 virtual void setRotation(const core::vector3df& rotation)
489 {
490 RelativeRotation = rotation;
491 }
492
493
495
498 virtual const core::vector3df& getPosition() const
499 {
500 return RelativeTranslation;
501 }
502
503
505
507 virtual void setPosition(const core::vector3df& newpos)
508 {
509 RelativeTranslation = newpos;
510 }
511
512
514
523 {
525 }
526
527
529
535 {
536 AutomaticCullingState = state;
537 }
538
539
541
543 {
545 }
546
547
549
552 virtual void setDebugDataVisible(u32 state)
553 {
554 DebugDataVisible = state;
555 }
556
558
561 {
562 return DebugDataVisible;
563 }
564
565
567
569 void setIsDebugObject(bool debugObject)
570 {
571 IsDebugObject = debugObject;
572 }
573
574
576
579 bool isDebugObject() const
580 {
582 return IsDebugObject;
583 }
584
585
587
589 {
590 return Children;
591 }
592
593
595
596 virtual void setParent(ISceneNode* newParent)
597 {
598 grab();
599 remove();
600
601 Parent = newParent;
602
603 if (Parent)
604 Parent->addChild(this);
605
606 drop();
607 }
608
609
611
621 {
622 return TriangleSelector;
623 }
624
625
627
636 {
637 if (TriangleSelector != selector)
638 {
641
642 TriangleSelector = selector;
645 }
646 }
647
648
650
653 {
654 if (Parent)
655 {
658 }
659 else
661 }
662
663
665
667 {
668 return Parent;
669 }
670
671
673
674 virtual ESCENE_NODE_TYPE getType() const
675 {
676 return ESNT_UNKNOWN;
677 }
678
679
681
688 {
689 if (!out)
690 return;
691 out->addString ("Name", Name.c_str());
692 out->addInt ("Id", ID );
693
694 out->addVector3d("Position", getPosition() );
695 out->addVector3d("Rotation", getRotation() );
696 out->addVector3d("Scale", getScale() );
697
698 out->addBool ("Visible", IsVisible );
699 out->addInt ("AutomaticCulling", AutomaticCullingState);
700 out->addInt ("DebugDataVisible", DebugDataVisible );
701 out->addBool ("IsDebugObject", IsDebugObject );
702 }
703
704
706
713 {
714 if (!in)
715 return;
716 Name = in->getAttributeAsString("Name");
717 ID = in->getAttributeAsInt("Id");
718
719 setPosition(in->getAttributeAsVector3d("Position"));
720 setRotation(in->getAttributeAsVector3d("Rotation"));
721 setScale(in->getAttributeAsVector3d("Scale"));
722
723 IsVisible = in->getAttributeAsBool("Visible");
724 s32 tmpState = in->getAttributeAsEnumeration("AutomaticCulling",
726 if (tmpState != -1)
727 AutomaticCullingState = (u32)tmpState;
728 else
729 AutomaticCullingState = in->getAttributeAsInt("AutomaticCulling");
730
731 DebugDataVisible = in->getAttributeAsInt("DebugDataVisible");
732 IsDebugObject = in->getAttributeAsBool("IsDebugObject");
733
735 }
736
738
741 virtual ISceneNode* clone(ISceneNode* newParent=0, ISceneManager* newManager=0)
742 {
743 return 0; // to be implemented by derived classes
744 }
745
747
748 virtual ISceneManager* getSceneManager(void) const { return SceneManager; }
749
750 protected:
751
753
757 void cloneMembers(ISceneNode* toCopyFrom, ISceneManager* newManager)
758 {
759 Name = toCopyFrom->Name;
763 RelativeScale = toCopyFrom->RelativeScale;
764 ID = toCopyFrom->ID;
768 IsVisible = toCopyFrom->IsVisible;
769 IsDebugObject = toCopyFrom->IsDebugObject;
770
771 if (newManager)
772 SceneManager = newManager;
773 else
774 SceneManager = toCopyFrom->SceneManager;
775
776 // clone children
777
778 ISceneNodeList::Iterator it = toCopyFrom->Children.begin();
779 for (; it != toCopyFrom->Children.end(); ++it)
780 (*it)->clone(this, newManager);
781
782 // clone animators
783
784 ISceneNodeAnimatorList::Iterator ait = toCopyFrom->Animators.begin();
785 for (; ait != toCopyFrom->Animators.end(); ++ait)
786 {
787 ISceneNodeAnimator* anim = (*ait)->createClone(this, SceneManager);
788 if (anim)
789 {
790 addAnimator(anim);
791 anim->drop();
792 }
793 }
794 }
795
799 {
800 SceneManager = newManager;
801
803 for (; it != Children.end(); ++it)
804 (*it)->setSceneManager(newManager);
805 }
806
809
812
815
818
821
824
827
830
833
836
839
842
845
848
851 };
852
853
854} // end namespace scene
855} // end namespace irr
856
857#endif
858
bool drop() const
Drops the object. Decrements the reference counter by one.
void grab() const
Grabs the object. Increments the reference counter by one.
4x4 matrix. Mostly used as transformation matrix for 3d calculations.
Definition: matrix4.h:46
void transformBoxEx(core::aabbox3d< f32 > &box) const
Transforms a axis aligned bounding box.
Definition: matrix4.h:1219
CMatrix4< T > & setScale(const vector3d< T > &scale)
Set Scale.
Definition: matrix4.h:775
vector3d< T > getTranslation() const
Gets the current translation.
Definition: matrix4.h:744
CMatrix4< T > & setRotationDegrees(const vector3d< T > &rotation)
Make a rotation matrix from Euler angles. The 4th row and column are unmodified.
Definition: matrix4.h:813
CMatrix4< T > & setTranslation(const vector3d< T > &translation)
Set the translation of the current matrix. Will erase any previous values.
Definition: matrix4.h:751
List iterator.
Definition: irrList.h:39
Doubly linked list template.
Definition: irrList.h:21
const T * c_str() const
Returns character string.
Definition: irrString.h:495
An object which is able to serialize and deserialize its attributes into an attributes object.
Provides a generic interface for attributes and their values and the possiblity to serialize them.
Definition: IAttributes.h:42
virtual void addString(const c8 *attributeName, const c8 *value)=0
Adds an attribute as string.
virtual bool getAttributeAsBool(const c8 *attributeName)=0
virtual void addBool(const c8 *attributeName, bool value)=0
Adds an attribute as bool.
virtual core::stringc getAttributeAsString(const c8 *attributeName)=0
virtual core::vector3df getAttributeAsVector3d(const c8 *attributeName)=0
virtual s32 getAttributeAsInt(const c8 *attributeName) const =0
virtual const c8 * getAttributeAsEnumeration(const c8 *attributeName)=0
virtual void addVector3d(const c8 *attributeName, core::vector3df value)=0
Adds an attribute as 3d vector.
virtual void addInt(const c8 *attributeName, s32 value)=0
Adds an attribute as integer.
The Scene Manager manages scene nodes, mesh recources, cameras and all the other stuff.
Animates a scene node. Can animate position, rotation, material, and so on.
virtual ISceneNodeAnimator * createClone(ISceneNode *node, ISceneManager *newManager=0)=0
Creates a clone of this animator.
virtual void animateNode(ISceneNode *node, u32 timeMs)=0
Animates a scene node.
Scene node interface.
Definition: ISceneNode.h:41
s32 ID
ID of the node.
Definition: ISceneNode.h:838
void cloneMembers(ISceneNode *toCopyFrom, ISceneManager *newManager)
A clone function for the ISceneNode members.
Definition: ISceneNode.h:757
ISceneManager * SceneManager
Pointer to the scene manager.
Definition: ISceneNode.h:832
u32 getAutomaticCulling() const
Gets the automatic culling state.
Definition: ISceneNode.h:542
u32 AutomaticCullingState
Automatic culling state.
Definition: ISceneNode.h:841
virtual core::vector3df getAbsolutePosition() const
Gets the absolute position of the node in world coordinates.
Definition: ISceneNode.h:522
void setMaterialTexture(u32 textureLayer, video::ITexture *texture)
Sets the texture of the specified layer in all materials of this scene node to the new texture.
Definition: ISceneNode.h:436
virtual void addAnimator(ISceneNodeAnimator *animator)
Adds an animator which should animate this node.
Definition: ISceneNode.h:348
ISceneNode * Parent
Pointer to the parent.
Definition: ISceneNode.h:823
virtual s32 getID() const
Get the id of the scene node.
Definition: ISceneNode.h:261
const core::list< ISceneNode * > & getChildren() const
Returns a const reference to the list of all children.
Definition: ISceneNode.h:588
core::list< ISceneNode * > Children
List of all children of this node.
Definition: ISceneNode.h:826
virtual void setScale(const core::vector3df &scale)
Sets the relative scale of the scene node.
Definition: ISceneNode.h:468
core::vector3df RelativeTranslation
Relative translation of the scene node.
Definition: ISceneNode.h:814
virtual const core::matrix4 & getAbsoluteTransformation() const
Get the absolute transformation of the node. Is recalculated every OnAnimate()-call.
Definition: ISceneNode.h:193
virtual void setName(const core::stringc &name)
Sets the name of the node.
Definition: ISceneNode.h:159
virtual void setPosition(const core::vector3df &newpos)
Sets the position of the node relative to its parent.
Definition: ISceneNode.h:507
u32 DebugDataVisible
Flag if debug data should be drawn, such as Bounding Boxes.
Definition: ISceneNode.h:844
core::stringc Name
Name of the scene node.
Definition: ISceneNode.h:808
virtual const core::vector3df & getScale() const
Gets the scale of the scene node relative to its parent.
Definition: ISceneNode.h:460
void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
Sets all material flags at once to a new value.
Definition: ISceneNode.h:425
virtual void removeAnimators()
Removes all animators from this scene node.
Definition: ISceneNode.h:388
scene::ISceneNode * getParent() const
Returns the parent of this scene node.
Definition: ISceneNode.h:666
virtual void setVisible(bool isVisible)
Sets if the node should be visible or not.
Definition: ISceneNode.h:252
virtual void remove()
Removes this scene node from the scene.
Definition: ISceneNode.h:339
void setMaterialType(video::E_MATERIAL_TYPE newType)
Sets the material type of all materials in this scene node to a new material type.
Definition: ISceneNode.h:448
bool isDebugObject() const
Returns if this scene node is a debug object.
Definition: ISceneNode.h:579
virtual ~ISceneNode()
Destructor.
Definition: ISceneNode.h:62
virtual void serializeAttributes(io::IAttributes *out, io::SAttributeReadWriteOptions *options=0) const
Writes attributes of the scene node.
Definition: ISceneNode.h:687
virtual const c8 * getName() const
Returns the name of the node.
Definition: ISceneNode.h:143
virtual ESCENE_NODE_TYPE getType() const
Returns type of the scene node.
Definition: ISceneNode.h:674
virtual void removeAll()
Removes all children of this scene node.
Definition: ISceneNode.h:323
virtual bool isTrulyVisible() const
Check whether the node is truly visible, taking into accounts its parents' visibility.
Definition: ISceneNode.h:235
void setSceneManager(ISceneManager *newManager)
Definition: ISceneNode.h:798
virtual void setParent(ISceneNode *newParent)
Changes the parent of the scene node.
Definition: ISceneNode.h:596
virtual const core::aabbox3d< f32 > getTransformedBoundingBox() const
Get the axis aligned, transformed and animated absolute bounding box of this node.
Definition: ISceneNode.h:178
virtual void deserializeAttributes(io::IAttributes *in, io::SAttributeReadWriteOptions *options=0)
Reads attributes of the scene node.
Definition: ISceneNode.h:712
void setAutomaticCulling(u32 state)
Enables or disables automatic culling based on the bounding box.
Definition: ISceneNode.h:534
virtual ISceneManager * getSceneManager(void) const
Retrieve the scene manager for this node.
Definition: ISceneNode.h:748
core::vector3df RelativeScale
Relative scale of the scene node.
Definition: ISceneNode.h:820
void setIsDebugObject(bool debugObject)
Sets if this scene node is a debug object.
Definition: ISceneNode.h:569
core::list< ISceneNodeAnimator * > Animators
List of all animator nodes.
Definition: ISceneNode.h:829
virtual void setName(const c8 *name)
Sets the name of the node.
Definition: ISceneNode.h:151
virtual bool removeChild(ISceneNode *child)
Removes a child from this scene node.
Definition: ISceneNode.h:302
core::vector3df RelativeRotation
Relative rotation of the scene node.
Definition: ISceneNode.h:817
virtual void setTriangleSelector(ITriangleSelector *selector)
Sets the triangle selector of the scene node.
Definition: ISceneNode.h:635
virtual u32 getMaterialCount() const
Get amount of materials used by this scene node.
Definition: ISceneNode.h:414
bool IsDebugObject
Is debug object?
Definition: ISceneNode.h:850
virtual const core::aabbox3d< f32 > & getBoundingBox() const =0
Get the axis aligned, not transformed bounding box of this node.
ISceneNode(ISceneNode *parent, ISceneManager *mgr, s32 id=-1, const core::vector3df &position=core::vector3df(0, 0, 0), const core::vector3df &rotation=core::vector3df(0, 0, 0), const core::vector3df &scale=core::vector3df(1.0f, 1.0f, 1.0f))
Constructor.
Definition: ISceneNode.h:45
virtual bool isVisible() const
Returns whether the node should be visible (if all of its parents are visible).
Definition: ISceneNode.h:226
virtual video::SMaterial & getMaterial(u32 num)
Returns the material based on the zero based index i.
Definition: ISceneNode.h:406
bool IsVisible
Is the node visible?
Definition: ISceneNode.h:847
virtual void removeAnimator(ISceneNodeAnimator *animator)
Removes an animator from this scene node.
Definition: ISceneNode.h:370
core::matrix4 AbsoluteTransformation
Absolute transformation of the node.
Definition: ISceneNode.h:811
virtual core::matrix4 getRelativeTransformation() const
Returns the relative transformation of the scene node.
Definition: ISceneNode.h:204
virtual void OnRegisterSceneNode()
This method is called just before the rendering process of the whole scene.
Definition: ISceneNode.h:91
virtual const core::vector3df & getPosition() const
Gets the position of the node relative to its parent.
Definition: ISceneNode.h:498
virtual void addChild(ISceneNode *child)
Adds a child to this scene node.
Definition: ISceneNode.h:280
u32 isDebugDataVisible() const
Returns if debug data like bounding boxes are drawn.
Definition: ISceneNode.h:560
const core::list< ISceneNodeAnimator * > & getAnimators() const
Get a list of all scene node animators.
Definition: ISceneNode.h:360
ITriangleSelector * TriangleSelector
Pointer to the triangle selector.
Definition: ISceneNode.h:835
virtual ITriangleSelector * getTriangleSelector() const
Returns the triangle selector attached to this scene node.
Definition: ISceneNode.h:620
virtual void setDebugDataVisible(u32 state)
Sets if debug data like bounding boxes should be drawn.
Definition: ISceneNode.h:552
virtual ISceneNode * clone(ISceneNode *newParent=0, ISceneManager *newManager=0)
Creates a clone of this scene node and its children.
Definition: ISceneNode.h:741
virtual void setRotation(const core::vector3df &rotation)
Sets the rotation of the node relative to its parent.
Definition: ISceneNode.h:488
virtual const core::vector3df & getRotation() const
Gets the rotation of the node relative to its parent.
Definition: ISceneNode.h:479
virtual void setID(s32 id)
Sets the id of the scene node.
Definition: ISceneNode.h:270
virtual void updateAbsolutePosition()
Updates the absolute position based on the relative and the parents position.
Definition: ISceneNode.h:652
virtual void OnAnimate(u32 timeMs)
OnAnimate() is called just before rendering the whole scene.
Definition: ISceneNode.h:108
virtual void render()=0
Renders the node.
Interface to return triangles with specific properties.
Interface of a Video Driver dependent Texture.
Definition: ITexture.h:99
Struct for holding parameters for a material renderer.
Definition: SMaterial.h:227
void setTexture(u32 i, ITexture *tex)
Sets the i-th texture.
Definition: SMaterial.h:482
void setFlag(E_MATERIAL_FLAG flag, bool value)
Sets the Material flag to the given value.
Definition: SMaterial.h:492
E_MATERIAL_TYPE MaterialType
Type of the material. Specifies how everything is blended together.
Definition: SMaterial.h:300
#define _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX
Defines a small statement to work around a microsoft compiler bug.
Definition: irrTypes.h:207
core::list< ISceneNodeAnimator * > ISceneNodeAnimatorList
Typedef for list of scene node animators.
Definition: ISceneNode.h:30
@ EDS_OFF
No Debug Data ( Default )
core::list< ISceneNode * > ISceneNodeList
Typedef for list of scene nodes.
Definition: ISceneNode.h:28
ESCENE_NODE_TYPE
An enumeration for all types of built-in scene nodes.
@ ESNT_UNKNOWN
Unknown scene node.
const c8 *const AutomaticCullingNames[]
Names for culling type.
Definition: ECullingTypes.h:26
IRRLICHT_API SMaterial IdentityMaterial
global const identity Material
E_MATERIAL_FLAG
Material flags.
E_MATERIAL_TYPE
Abstracted and easy to use fixed function/programmable pipeline material modes.
const u32 MATERIAL_MAX_TEXTURES
Maximum number of texture an SMaterial can have.
Definition: SMaterial.h:223
Everything in the Irrlicht Engine can be found in this namespace.
Definition: aabbox3d.h:13
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:58
char c8
8 bit character variable.
Definition: irrTypes.h:31
signed int s32
32 bit signed variable.
Definition: irrTypes.h:66
struct holding data describing options