Irrlicht 3D Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
21 namespace irr
22 {
23 namespace scene
24 {
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
68  ISceneNodeAnimatorList::Iterator ait = Animators.begin();
69  for (; ait != Animators.end(); ++ait)
70  (*ait)->drop();
71 
72  if (TriangleSelector)
74  }
75 
76 
78 
91  virtual void OnRegisterSceneNode()
92  {
93  if (IsVisible)
94  {
95  ISceneNodeList::Iterator it = Children.begin();
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 
114  ISceneNodeAnimatorList::Iterator ait = Animators.begin();
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 
130  ISceneNodeList::Iterator it = Children.begin();
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  {
195  return AbsoluteTransformation;
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;
213  smat.setScale(RelativeScale);
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  {
304  ISceneNodeList::Iterator it = Children.begin();
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  {
325  ISceneNodeList::Iterator it = Children.begin();
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  {
372  ISceneNodeAnimatorList::Iterator it = Animators.begin();
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  {
390  ISceneNodeAnimatorList::Iterator it = Animators.begin();
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  {
544  return AutomaticCullingState;
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 
635  virtual void setTriangleSelector(ITriangleSelector* selector)
636  {
637  if (TriangleSelector != selector)
638  {
639  if (TriangleSelector)
641 
642  TriangleSelector = selector;
643  if (TriangleSelector)
645  }
646  }
647 
648 
650 
652  virtual void updateAbsolutePosition()
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;
762  RelativeRotation = toCopyFrom->RelativeRotation;
763  RelativeScale = toCopyFrom->RelativeScale;
764  ID = toCopyFrom->ID;
767  DebugDataVisible = toCopyFrom->DebugDataVisible;
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 
798  void setSceneManager(ISceneManager* newManager)
799  {
800  SceneManager = newManager;
801 
802  ISceneNodeList::Iterator it = Children.begin();
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 
847  bool IsVisible;
848 
851  };
852 
853 
854 } // end namespace scene
855 } // end namespace irr
856 
857 #endif
858