00001
00002
00003
00004
00005 #ifndef __I_SCENE_NODE_H_INCLUDED__
00006 #define __I_SCENE_NODE_H_INCLUDED__
00007
00008 #include "IAttributeExchangingObject.h"
00009 #include "ESceneNodeTypes.h"
00010 #include "ECullingTypes.h"
00011 #include "EDebugSceneTypes.h"
00012 #include "ISceneNodeAnimator.h"
00013 #include "ITriangleSelector.h"
00014 #include "SMaterial.h"
00015 #include "irrString.h"
00016 #include "aabbox3d.h"
00017 #include "matrix4.h"
00018 #include "irrList.h"
00019 #include "IAttributes.h"
00020
00021 namespace irr
00022 {
00023 namespace scene
00024 {
00025 class ISceneManager;
00026
00028
00035 class ISceneNode : public io::IAttributeExchangingObject
00036 {
00037 public:
00038
00040 ISceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id=-1,
00041 const core::vector3df& position = core::vector3df(0,0,0),
00042 const core::vector3df& rotation = core::vector3df(0,0,0),
00043 const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f))
00044 : RelativeTranslation(position), RelativeRotation(rotation), RelativeScale(scale),
00045 Parent(parent), ID(id), SceneManager(mgr), TriangleSelector(0),
00046 AutomaticCullingState(EAC_BOX), IsVisible(true),
00047 DebugDataVisible(EDS_OFF), IsDebugObject(false)
00048 {
00049 if (Parent)
00050 Parent->addChild(this);
00051
00052 updateAbsolutePosition();
00053 }
00054
00055
00057 virtual ~ISceneNode()
00058 {
00059
00060 removeAll();
00061
00062
00063 core::list<ISceneNodeAnimator*>::Iterator ait = Animators.begin();
00064 for (; ait != Animators.end(); ++ait)
00065 (*ait)->drop();
00066
00067 if (TriangleSelector)
00068 TriangleSelector->drop();
00069 }
00070
00071
00073
00086 virtual void OnRegisterSceneNode()
00087 {
00088 if (IsVisible)
00089 {
00090 core::list<ISceneNode*>::Iterator it = Children.begin();
00091 for (; it != Children.end(); ++it)
00092 (*it)->OnRegisterSceneNode();
00093 }
00094 }
00095
00096
00098
00103 virtual void OnAnimate(u32 timeMs)
00104 {
00105 if (IsVisible)
00106 {
00107
00108
00109 core::list<ISceneNodeAnimator*>::Iterator ait = Animators.begin();
00110 for (; ait != Animators.end(); ++ait)
00111 (*ait)->animateNode(this, timeMs);
00112
00113
00114 updateAbsolutePosition();
00115
00116
00117
00118 core::list<ISceneNode*>::Iterator it = Children.begin();
00119 for (; it != Children.end(); ++it)
00120 (*it)->OnAnimate(timeMs);
00121 }
00122 }
00123
00124
00126 virtual void render() = 0;
00127
00128
00130
00131 virtual const c8* getName() const
00132 {
00133 return Name.c_str();
00134 }
00135
00136
00138
00139 virtual void setName(const c8* name)
00140 {
00141 Name = name;
00142 }
00143
00144
00146
00153 virtual const core::aabbox3d<f32>& getBoundingBox() const = 0;
00154
00155
00157
00158 virtual const core::aabbox3d<f32> getTransformedBoundingBox() const
00159 {
00160 core::aabbox3d<f32> box = getBoundingBox();
00161 AbsoluteTransformation.transformBox(box);
00162 return box;
00163 }
00164
00165
00168 const core::matrix4& getAbsoluteTransformation() const
00169 {
00170 return AbsoluteTransformation;
00171 }
00172
00173
00175
00179 virtual core::matrix4 getRelativeTransformation() const
00180 {
00181 core::matrix4 mat;
00182 mat.setRotationDegrees(RelativeRotation);
00183 mat.setTranslation(RelativeTranslation);
00184
00185 if (RelativeScale != core::vector3df(1.f,1.f,1.f))
00186 {
00187 core::matrix4 smat;
00188 smat.setScale(RelativeScale);
00189 mat *= smat;
00190 }
00191
00192 return mat;
00193 }
00194
00195
00197
00200 virtual bool isVisible() const
00201 {
00202 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00203 return IsVisible;
00204 }
00205
00206
00208
00211 virtual void setVisible(bool isVisible)
00212 {
00213 IsVisible = isVisible;
00214 }
00215
00216
00218
00220 virtual s32 getID() const
00221 {
00222 return ID;
00223 }
00224
00225
00227
00229 virtual void setID(s32 id)
00230 {
00231 ID = id;
00232 }
00233
00234
00236
00239 virtual void addChild(ISceneNode* child)
00240 {
00241 if (child && (child != this))
00242 {
00243 child->grab();
00244 child->remove();
00245 Children.push_back(child);
00246 child->Parent = this;
00247 }
00248 }
00249
00250
00252
00255 virtual bool removeChild(ISceneNode* child)
00256 {
00257 core::list<ISceneNode*>::Iterator it = Children.begin();
00258 for (; it != Children.end(); ++it)
00259 if ((*it) == child)
00260 {
00261 (*it)->Parent = 0;
00262 (*it)->drop();
00263 Children.erase(it);
00264 return true;
00265 }
00266
00267 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00268 return false;
00269 }
00270
00271
00273 virtual void removeAll()
00274 {
00275 core::list<ISceneNode*>::Iterator it = Children.begin();
00276 for (; it != Children.end(); ++it)
00277 {
00278 (*it)->Parent = 0;
00279 (*it)->drop();
00280 }
00281
00282 Children.clear();
00283 }
00284
00285
00287 virtual void remove()
00288 {
00289 if (Parent)
00290 Parent->removeChild(this);
00291 }
00292
00293
00295
00296 virtual void addAnimator(ISceneNodeAnimator* animator)
00297 {
00298 if (animator)
00299 {
00300 Animators.push_back(animator);
00301 animator->grab();
00302 }
00303 }
00304
00305
00307
00308 const core::list<ISceneNodeAnimator*>& getAnimators() const
00309 {
00310 return Animators;
00311 }
00312
00313
00315
00316 virtual void removeAnimator(ISceneNodeAnimator* animator)
00317 {
00318 core::list<ISceneNodeAnimator*>::Iterator it = Animators.begin();
00319 for (; it != Animators.end(); ++it)
00320 if ((*it) == animator)
00321 {
00322 (*it)->drop();
00323 Animators.erase(it);
00324 return;
00325 }
00326 }
00327
00328
00330 virtual void removeAnimators()
00331 {
00332 core::list<ISceneNodeAnimator*>::Iterator it = Animators.begin();
00333 for (; it != Animators.end(); ++it)
00334 (*it)->drop();
00335
00336 Animators.clear();
00337 }
00338
00339
00341
00348 virtual video::SMaterial& getMaterial(u32 num)
00349 {
00350 return *((video::SMaterial*)0);
00351 }
00352
00353
00355
00356 virtual u32 getMaterialCount() const
00357 {
00358 return 0;
00359 }
00360
00361
00363
00367 void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
00368 {
00369 for (u32 i=0; i<getMaterialCount(); ++i)
00370 getMaterial(i).setFlag(flag, newvalue);
00371 }
00372
00373
00375
00378 void setMaterialTexture(u32 textureLayer, video::ITexture* texture)
00379 {
00380 if (textureLayer >= video::MATERIAL_MAX_TEXTURES)
00381 return;
00382
00383 for (u32 i=0; i<getMaterialCount(); ++i)
00384 getMaterial(i).setTexture(textureLayer, texture);
00385 }
00386
00387
00389
00390 void setMaterialType(video::E_MATERIAL_TYPE newType)
00391 {
00392 for (u32 i=0; i<getMaterialCount(); ++i)
00393 getMaterial(i).MaterialType = newType;
00394 }
00395
00396
00398
00399 virtual core::vector3df getScale() const
00400 {
00401 return RelativeScale;
00402 }
00403
00404
00406
00407 virtual void setScale(const core::vector3df& scale)
00408 {
00409 RelativeScale = scale;
00410 }
00411
00412
00414
00416 virtual const core::vector3df& getRotation() const
00417 {
00418 return RelativeRotation;
00419 }
00420
00421
00423
00425 virtual void setRotation(const core::vector3df& rotation)
00426 {
00427 RelativeRotation = rotation;
00428 }
00429
00430
00432
00434 virtual const core::vector3df getPosition() const
00435 {
00436 return RelativeTranslation;
00437 }
00438
00439
00441
00443 virtual void setPosition(const core::vector3df& newpos)
00444 {
00445 RelativeTranslation = newpos;
00446 }
00447
00448
00450
00451 virtual core::vector3df getAbsolutePosition() const
00452 {
00453 return AbsoluteTransformation.getTranslation();
00454 }
00455
00456
00458
00463 void setAutomaticCulling( E_CULLING_TYPE state)
00464 {
00465 AutomaticCullingState = state;
00466 }
00467
00468
00470
00471 E_CULLING_TYPE getAutomaticCulling() const
00472 {
00473 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00474 return AutomaticCullingState;
00475 }
00476
00477
00479
00482 virtual void setDebugDataVisible(s32 state)
00483 {
00484 DebugDataVisible = state;
00485 }
00486
00488
00489 s32 isDebugDataVisible() const
00490 {
00491 return DebugDataVisible;
00492 }
00493
00494
00496
00498 void setIsDebugObject(bool debugObject)
00499 {
00500 IsDebugObject = debugObject;
00501 }
00502
00503
00505
00508 bool isDebugObject() const
00509 {
00510 _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00511 return IsDebugObject;
00512 }
00513
00514
00516
00517 const core::list<ISceneNode*>& getChildren() const
00518 {
00519 return Children;
00520 }
00521
00522
00524
00525 virtual void setParent(ISceneNode* newParent)
00526 {
00527 grab();
00528 remove();
00529
00530 Parent = newParent;
00531
00532 if (Parent)
00533 Parent->addChild(this);
00534
00535 drop();
00536 }
00537
00538
00540
00549 virtual ITriangleSelector* getTriangleSelector() const
00550 {
00551 return TriangleSelector;
00552 }
00553
00554
00556
00564 virtual void setTriangleSelector(ITriangleSelector* selector)
00565 {
00566 if (TriangleSelector)
00567 TriangleSelector->drop();
00568
00569 TriangleSelector = selector;
00570 if (TriangleSelector)
00571 TriangleSelector->grab();
00572 }
00573
00574
00576 virtual void updateAbsolutePosition()
00577 {
00578 if (Parent)
00579 {
00580 AbsoluteTransformation =
00581 Parent->getAbsoluteTransformation() * getRelativeTransformation();
00582 }
00583 else
00584 AbsoluteTransformation = getRelativeTransformation();
00585 }
00586
00587
00589
00590 scene::ISceneNode* getParent() const
00591 {
00592 return Parent;
00593 }
00594
00595
00597
00598 virtual ESCENE_NODE_TYPE getType() const
00599 {
00600 return ESNT_UNKNOWN;
00601 }
00602
00603
00605
00611 virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
00612 {
00613 if (!out)
00614 return;
00615 out->addString ("Name", Name.c_str());
00616 out->addInt ("Id", ID );
00617
00618 out->addVector3d("Position", getPosition() );
00619 out->addVector3d("Rotation", getRotation() );
00620 out->addVector3d("Scale", getScale() );
00621
00622 out->addBool ("Visible", IsVisible );
00623 out->addEnum ("AutomaticCulling", AutomaticCullingState, AutomaticCullingNames);
00624 out->addInt ("DebugDataVisible", DebugDataVisible );
00625 out->addBool ("IsDebugObject", IsDebugObject );
00626 }
00627
00628
00630
00636 virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
00637 {
00638 if (!in)
00639 return;
00640 Name = in->getAttributeAsString("Name");
00641 ID = in->getAttributeAsInt("Id");
00642
00643 setPosition(in->getAttributeAsVector3d("Position"));
00644 setRotation(in->getAttributeAsVector3d("Rotation"));
00645 setScale(in->getAttributeAsVector3d("Scale"));
00646
00647 IsVisible = in->getAttributeAsBool("Visible");
00648 AutomaticCullingState = (scene::E_CULLING_TYPE) in->getAttributeAsEnumeration("AutomaticCulling",
00649 scene::AutomaticCullingNames);
00650
00651 DebugDataVisible = in->getAttributeAsInt("DebugDataVisible");
00652 IsDebugObject = in->getAttributeAsBool("IsDebugObject");
00653
00654 updateAbsolutePosition();
00655 }
00656
00658
00661 virtual ISceneNode* clone(ISceneNode* newParent=0, ISceneManager* newManager=0)
00662 {
00663 return 0;
00664 }
00665
00666 protected:
00667
00669
00673 void cloneMembers(ISceneNode* toCopyFrom, ISceneManager* newManager)
00674 {
00675 Name = toCopyFrom->Name;
00676 AbsoluteTransformation = toCopyFrom->AbsoluteTransformation;
00677 RelativeTranslation = toCopyFrom->RelativeTranslation;
00678 RelativeRotation = toCopyFrom->RelativeRotation;
00679 RelativeScale = toCopyFrom->RelativeScale;
00680 ID = toCopyFrom->ID;
00681 setTriangleSelector(toCopyFrom->TriangleSelector);
00682 AutomaticCullingState = toCopyFrom->AutomaticCullingState;
00683 DebugDataVisible = toCopyFrom->DebugDataVisible;
00684 IsVisible = toCopyFrom->IsVisible;
00685 IsDebugObject = toCopyFrom->IsDebugObject;
00686
00687 if (newManager)
00688 SceneManager = newManager;
00689 else
00690 SceneManager = toCopyFrom->SceneManager;
00691
00692
00693
00694 core::list<ISceneNode*>::Iterator it = toCopyFrom->Children.begin();
00695 for (; it != toCopyFrom->Children.end(); ++it)
00696 (*it)->clone(this, newManager);
00697
00698
00699
00700 core::list<ISceneNodeAnimator*>::Iterator ait = toCopyFrom->Animators.begin();
00701 for (; ait != toCopyFrom->Animators.end(); ++ait)
00702 {
00703 ISceneNodeAnimator* anim = (*ait)->createClone(this, SceneManager);
00704 if (anim)
00705 {
00706 addAnimator(anim);
00707 anim->drop();
00708 }
00709 }
00710 }
00711
00713 core::stringc Name;
00714
00716 core::matrix4 AbsoluteTransformation;
00717
00719 core::vector3df RelativeTranslation;
00720
00722 core::vector3df RelativeRotation;
00723
00725 core::vector3df RelativeScale;
00726
00728 ISceneNode* Parent;
00729
00731 core::list<ISceneNode*> Children;
00732
00734 core::list<ISceneNodeAnimator*> Animators;
00735
00737 s32 ID;
00738
00740 ISceneManager* SceneManager;
00741
00743 ITriangleSelector* TriangleSelector;
00744
00746 E_CULLING_TYPE AutomaticCullingState;
00747
00749 bool IsVisible;
00750
00752 s32 DebugDataVisible;
00753
00755 bool IsDebugObject;
00756 };
00757
00758 }
00759 }
00760
00761 #endif
00762