Irrlicht 3D Engine
ISceneNode.h
Go to the documentation of this file.
00001 // Copyright (C) 2002-2012 Nikolaus Gebhardt
00002 // This file is part of the "Irrlicht Engine".
00003 // For conditions of distribution and use, see copyright notice in irrlicht.h
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     typedef core::list<ISceneNode*> ISceneNodeList;
00030     typedef core::list<ISceneNodeAnimator*> ISceneNodeAnimatorList;
00031 
00033 
00040     class ISceneNode : virtual public io::IAttributeExchangingObject
00041     {
00042     public:
00043 
00045         ISceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id=-1,
00046                 const core::vector3df& position = core::vector3df(0,0,0),
00047                 const core::vector3df& rotation = core::vector3df(0,0,0),
00048                 const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f))
00049             : RelativeTranslation(position), RelativeRotation(rotation), RelativeScale(scale),
00050                 Parent(0), SceneManager(mgr), TriangleSelector(0), ID(id),
00051                 AutomaticCullingState(EAC_BOX), DebugDataVisible(EDS_OFF),
00052                 IsVisible(true), IsDebugObject(false)
00053         {
00054             if (parent)
00055                 parent->addChild(this);
00056 
00057             updateAbsolutePosition();
00058         }
00059 
00060 
00062         virtual ~ISceneNode()
00063         {
00064             // delete all children
00065             removeAll();
00066 
00067             // delete all animators
00068             ISceneNodeAnimatorList::Iterator ait = Animators.begin();
00069             for (; ait != Animators.end(); ++ait)
00070                 (*ait)->drop();
00071 
00072             if (TriangleSelector)
00073                 TriangleSelector->drop();
00074         }
00075 
00076 
00078 
00091         virtual void OnRegisterSceneNode()
00092         {
00093             if (IsVisible)
00094             {
00095                 ISceneNodeList::Iterator it = Children.begin();
00096                 for (; it != Children.end(); ++it)
00097                     (*it)->OnRegisterSceneNode();
00098             }
00099         }
00100 
00101 
00103 
00108         virtual void OnAnimate(u32 timeMs)
00109         {
00110             if (IsVisible)
00111             {
00112                 // animate this node with all animators
00113 
00114                 ISceneNodeAnimatorList::Iterator ait = Animators.begin();
00115                 while (ait != Animators.end())
00116                     {
00117                     // continue to the next node before calling animateNode()
00118                     // so that the animator may remove itself from the scene
00119                     // node without the iterator becoming invalid
00120                     ISceneNodeAnimator* anim = *ait;
00121                     ++ait;
00122                     anim->animateNode(this, timeMs);
00123                 }
00124 
00125                 // update absolute position
00126                 updateAbsolutePosition();
00127 
00128                 // perform the post render process on all children
00129 
00130                 ISceneNodeList::Iterator it = Children.begin();
00131                 for (; it != Children.end(); ++it)
00132                     (*it)->OnAnimate(timeMs);
00133             }
00134         }
00135 
00136 
00138         virtual void render() = 0;
00139 
00140 
00142 
00143         virtual const c8* getName() const
00144         {
00145             return Name.c_str();
00146         }
00147 
00148 
00150 
00151         virtual void setName(const c8* name)
00152         {
00153             Name = name;
00154         }
00155 
00156 
00158 
00159         virtual void setName(const core::stringc& name)
00160         {
00161             Name = name;
00162         }
00163 
00164 
00166 
00173         virtual const core::aabbox3d<f32>& getBoundingBox() const = 0;
00174 
00175 
00177 
00178         virtual const core::aabbox3d<f32> getTransformedBoundingBox() const
00179         {
00180             core::aabbox3d<f32> box = getBoundingBox();
00181             AbsoluteTransformation.transformBoxEx(box);
00182             return box;
00183         }
00184 
00185 
00187 
00193         virtual const core::matrix4& getAbsoluteTransformation() const
00194         {
00195             return AbsoluteTransformation;
00196         }
00197 
00198 
00200 
00204         virtual core::matrix4 getRelativeTransformation() const
00205         {
00206             core::matrix4 mat;
00207             mat.setRotationDegrees(RelativeRotation);
00208             mat.setTranslation(RelativeTranslation);
00209 
00210             if (RelativeScale != core::vector3df(1.f,1.f,1.f))
00211             {
00212                 core::matrix4 smat;
00213                 smat.setScale(RelativeScale);
00214                 mat *= smat;
00215             }
00216 
00217             return mat;
00218         }
00219 
00220 
00222 
00226         virtual bool isVisible() const
00227         {
00228             _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00229             return IsVisible;
00230         }
00231 
00233 
00235         virtual bool isTrulyVisible() const
00236         {
00237             _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00238             if(!IsVisible)
00239                 return false;
00240 
00241             if(!Parent)
00242                 return true;
00243 
00244             return Parent->isTrulyVisible();
00245         }
00246 
00248 
00252         virtual void setVisible(bool isVisible)
00253         {
00254             IsVisible = isVisible;
00255         }
00256 
00257 
00259 
00261         virtual s32 getID() const
00262         {
00263             return ID;
00264         }
00265 
00266 
00268 
00270         virtual void setID(s32 id)
00271         {
00272             ID = id;
00273         }
00274 
00275 
00277 
00280         virtual void addChild(ISceneNode* child)
00281         {
00282             if (child && (child != this))
00283             {
00284                 // Change scene manager?
00285                 if (SceneManager != child->SceneManager)
00286                     child->setSceneManager(SceneManager);
00287 
00288                 child->grab();
00289                 child->remove(); // remove from old parent
00290                 Children.push_back(child);
00291                 child->Parent = this;
00292             }
00293         }
00294 
00295 
00297 
00302         virtual bool removeChild(ISceneNode* child)
00303         {
00304             ISceneNodeList::Iterator it = Children.begin();
00305             for (; it != Children.end(); ++it)
00306                 if ((*it) == child)
00307                 {
00308                     (*it)->Parent = 0;
00309                     (*it)->drop();
00310                     Children.erase(it);
00311                     return true;
00312                 }
00313 
00314             _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00315             return false;
00316         }
00317 
00318 
00320 
00323         virtual void removeAll()
00324         {
00325             ISceneNodeList::Iterator it = Children.begin();
00326             for (; it != Children.end(); ++it)
00327             {
00328                 (*it)->Parent = 0;
00329                 (*it)->drop();
00330             }
00331 
00332             Children.clear();
00333         }
00334 
00335 
00337 
00339         virtual void remove()
00340         {
00341             if (Parent)
00342                 Parent->removeChild(this);
00343         }
00344 
00345 
00347 
00348         virtual void addAnimator(ISceneNodeAnimator* animator)
00349         {
00350             if (animator)
00351             {
00352                 Animators.push_back(animator);
00353                 animator->grab();
00354             }
00355         }
00356 
00357 
00359 
00360         const core::list<ISceneNodeAnimator*>& getAnimators() const
00361         {
00362             return Animators;
00363         }
00364 
00365 
00367 
00370         virtual void removeAnimator(ISceneNodeAnimator* animator)
00371         {
00372             ISceneNodeAnimatorList::Iterator it = Animators.begin();
00373             for (; it != Animators.end(); ++it)
00374             {
00375                 if ((*it) == animator)
00376                 {
00377                     (*it)->drop();
00378                     Animators.erase(it);
00379                     return;
00380                 }
00381             }
00382         }
00383 
00384 
00386 
00388         virtual void removeAnimators()
00389         {
00390             ISceneNodeAnimatorList::Iterator it = Animators.begin();
00391             for (; it != Animators.end(); ++it)
00392                 (*it)->drop();
00393 
00394             Animators.clear();
00395         }
00396 
00397 
00399 
00406         virtual video::SMaterial& getMaterial(u32 num)
00407         {
00408             return video::IdentityMaterial;
00409         }
00410 
00411 
00413 
00414         virtual u32 getMaterialCount() const
00415         {
00416             return 0;
00417         }
00418 
00419 
00421 
00425         void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
00426         {
00427             for (u32 i=0; i<getMaterialCount(); ++i)
00428                 getMaterial(i).setFlag(flag, newvalue);
00429         }
00430 
00431 
00433 
00436         void setMaterialTexture(u32 textureLayer, video::ITexture* texture)
00437         {
00438             if (textureLayer >= video::MATERIAL_MAX_TEXTURES)
00439                 return;
00440 
00441             for (u32 i=0; i<getMaterialCount(); ++i)
00442                 getMaterial(i).setTexture(textureLayer, texture);
00443         }
00444 
00445 
00447 
00448         void setMaterialType(video::E_MATERIAL_TYPE newType)
00449         {
00450             for (u32 i=0; i<getMaterialCount(); ++i)
00451                 getMaterial(i).MaterialType = newType;
00452         }
00453 
00454 
00456 
00460         virtual const core::vector3df& getScale() const
00461         {
00462             return RelativeScale;
00463         }
00464 
00465 
00467 
00468         virtual void setScale(const core::vector3df& scale)
00469         {
00470             RelativeScale = scale;
00471         }
00472 
00473 
00475 
00479         virtual const core::vector3df& getRotation() const
00480         {
00481             return RelativeRotation;
00482         }
00483 
00484 
00486 
00488         virtual void setRotation(const core::vector3df& rotation)
00489         {
00490             RelativeRotation = rotation;
00491         }
00492 
00493 
00495 
00498         virtual const core::vector3df& getPosition() const
00499         {
00500             return RelativeTranslation;
00501         }
00502 
00503 
00505 
00507         virtual void setPosition(const core::vector3df& newpos)
00508         {
00509             RelativeTranslation = newpos;
00510         }
00511 
00512 
00514 
00522         virtual core::vector3df getAbsolutePosition() const
00523         {
00524             return AbsoluteTransformation.getTranslation();
00525         }
00526 
00527 
00529 
00534         void setAutomaticCulling( u32 state)
00535         {
00536             AutomaticCullingState = state;
00537         }
00538 
00539 
00541 
00542         u32 getAutomaticCulling() const
00543         {
00544             return AutomaticCullingState;
00545         }
00546 
00547 
00549 
00552         virtual void setDebugDataVisible(u32 state)
00553         {
00554             DebugDataVisible = state;
00555         }
00556 
00558 
00560         u32 isDebugDataVisible() const
00561         {
00562             return DebugDataVisible;
00563         }
00564 
00565 
00567 
00569         void setIsDebugObject(bool debugObject)
00570         {
00571             IsDebugObject = debugObject;
00572         }
00573 
00574 
00576 
00579         bool isDebugObject() const
00580         {
00581             _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00582             return IsDebugObject;
00583         }
00584 
00585 
00587 
00588         const core::list<ISceneNode*>& getChildren() const
00589         {
00590             return Children;
00591         }
00592 
00593 
00595 
00596         virtual void setParent(ISceneNode* newParent)
00597         {
00598             grab();
00599             remove();
00600 
00601             Parent = newParent;
00602 
00603             if (Parent)
00604                 Parent->addChild(this);
00605 
00606             drop();
00607         }
00608 
00609 
00611 
00620         virtual ITriangleSelector* getTriangleSelector() const
00621         {
00622             return TriangleSelector;
00623         }
00624 
00625 
00627 
00635         virtual void setTriangleSelector(ITriangleSelector* selector)
00636         {
00637             if (TriangleSelector != selector)
00638             {
00639                 if (TriangleSelector)
00640                     TriangleSelector->drop();
00641 
00642                 TriangleSelector = selector;
00643                 if (TriangleSelector)
00644                     TriangleSelector->grab();
00645             }
00646         }
00647 
00648 
00650 
00652         virtual void updateAbsolutePosition()
00653         {
00654             if (Parent)
00655             {
00656                 AbsoluteTransformation =
00657                     Parent->getAbsoluteTransformation() * getRelativeTransformation();
00658             }
00659             else
00660                 AbsoluteTransformation = getRelativeTransformation();
00661         }
00662 
00663 
00665 
00666         scene::ISceneNode* getParent() const
00667         {
00668             return Parent;
00669         }
00670 
00671 
00673 
00674         virtual ESCENE_NODE_TYPE getType() const
00675         {
00676             return ESNT_UNKNOWN;
00677         }
00678 
00679 
00681 
00687         virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
00688         {
00689             if (!out)
00690                 return;
00691             out->addString  ("Name", Name.c_str());
00692             out->addInt ("Id", ID );
00693 
00694             out->addVector3d("Position", getPosition() );
00695             out->addVector3d("Rotation", getRotation() );
00696             out->addVector3d("Scale", getScale() );
00697 
00698             out->addBool    ("Visible", IsVisible );
00699             out->addInt ("AutomaticCulling", AutomaticCullingState);
00700             out->addInt ("DebugDataVisible", DebugDataVisible );
00701             out->addBool    ("IsDebugObject", IsDebugObject );
00702         }
00703 
00704 
00706 
00712         virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
00713         {
00714             if (!in)
00715                 return;
00716             Name = in->getAttributeAsString("Name");
00717             ID = in->getAttributeAsInt("Id");
00718 
00719             setPosition(in->getAttributeAsVector3d("Position"));
00720             setRotation(in->getAttributeAsVector3d("Rotation"));
00721             setScale(in->getAttributeAsVector3d("Scale"));
00722 
00723             IsVisible = in->getAttributeAsBool("Visible");
00724             s32 tmpState = in->getAttributeAsEnumeration("AutomaticCulling",
00725                     scene::AutomaticCullingNames);
00726             if (tmpState != -1)
00727                 AutomaticCullingState = (u32)tmpState;
00728             else
00729                 AutomaticCullingState = in->getAttributeAsInt("AutomaticCulling");
00730 
00731             DebugDataVisible = in->getAttributeAsInt("DebugDataVisible");
00732             IsDebugObject = in->getAttributeAsBool("IsDebugObject");
00733 
00734             updateAbsolutePosition();
00735         }
00736 
00738 
00741         virtual ISceneNode* clone(ISceneNode* newParent=0, ISceneManager* newManager=0)
00742         {
00743             return 0; // to be implemented by derived classes
00744         }
00745 
00747 
00748         virtual ISceneManager* getSceneManager(void) const { return SceneManager; }
00749 
00750     protected:
00751 
00753 
00757         void cloneMembers(ISceneNode* toCopyFrom, ISceneManager* newManager)
00758         {
00759             Name = toCopyFrom->Name;
00760             AbsoluteTransformation = toCopyFrom->AbsoluteTransformation;
00761             RelativeTranslation = toCopyFrom->RelativeTranslation;
00762             RelativeRotation = toCopyFrom->RelativeRotation;
00763             RelativeScale = toCopyFrom->RelativeScale;
00764             ID = toCopyFrom->ID;
00765             setTriangleSelector(toCopyFrom->TriangleSelector);
00766             AutomaticCullingState = toCopyFrom->AutomaticCullingState;
00767             DebugDataVisible = toCopyFrom->DebugDataVisible;
00768             IsVisible = toCopyFrom->IsVisible;
00769             IsDebugObject = toCopyFrom->IsDebugObject;
00770 
00771             if (newManager)
00772                 SceneManager = newManager;
00773             else
00774                 SceneManager = toCopyFrom->SceneManager;
00775 
00776             // clone children
00777 
00778             ISceneNodeList::Iterator it = toCopyFrom->Children.begin();
00779             for (; it != toCopyFrom->Children.end(); ++it)
00780                 (*it)->clone(this, newManager);
00781 
00782             // clone animators
00783 
00784             ISceneNodeAnimatorList::Iterator ait = toCopyFrom->Animators.begin();
00785             for (; ait != toCopyFrom->Animators.end(); ++ait)
00786             {
00787                 ISceneNodeAnimator* anim = (*ait)->createClone(this, SceneManager);
00788                 if (anim)
00789                 {
00790                     addAnimator(anim);
00791                     anim->drop();
00792                 }
00793             }
00794         }
00795 
00798         void setSceneManager(ISceneManager* newManager)
00799         {
00800             SceneManager = newManager;
00801 
00802             ISceneNodeList::Iterator it = Children.begin();
00803             for (; it != Children.end(); ++it)
00804                 (*it)->setSceneManager(newManager);
00805         }
00806 
00808         core::stringc Name;
00809 
00811         core::matrix4 AbsoluteTransformation;
00812 
00814         core::vector3df RelativeTranslation;
00815 
00817         core::vector3df RelativeRotation;
00818 
00820         core::vector3df RelativeScale;
00821 
00823         ISceneNode* Parent;
00824 
00826         core::list<ISceneNode*> Children;
00827 
00829         core::list<ISceneNodeAnimator*> Animators;
00830 
00832         ISceneManager* SceneManager;
00833 
00835         ITriangleSelector* TriangleSelector;
00836 
00838         s32 ID;
00839 
00841         u32 AutomaticCullingState;
00842 
00844         u32 DebugDataVisible;
00845 
00847         bool IsVisible;
00848 
00850         bool IsDebugObject;
00851     };
00852 
00853 
00854 } // end namespace scene
00855 } // end namespace irr
00856 
00857 #endif
00858