Home | Namespaces | Hierarchy | Alphabetical List | Class list | Files | Namespace Members | Class members | File members | Tutorials

ISceneNode.h

Go to the documentation of this file.
00001 // Copyright (C) 2002-2009 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 
00035         class ISceneNode : virtual 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(0), SceneManager(mgr), TriangleSelector(0), ID(id),
00046                                 AutomaticCullingState(EAC_BOX), DebugDataVisible(EDS_OFF),
00047                                 IsVisible(true), IsDebugObject(false)
00048                 {
00049                         if (parent)
00050                                 parent->addChild(this);
00051 
00052                         updateAbsolutePosition();
00053                 }
00054 
00055 
00057                 virtual ~ISceneNode()
00058                 {
00059                         // delete all children
00060                         removeAll();
00061 
00062                         // delete all animators
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                                 // animate this node with all animators
00108 
00109                                 core::list<ISceneNodeAnimator*>::Iterator ait = Animators.begin();
00110                                 while (ait != Animators.end())
00111                                         {
00112                                         // continue to the next node before calling animateNode()
00113                                         // so that the animator may remove itself from the scene
00114                                         // node without the iterator becoming invalid
00115                                         ISceneNodeAnimator* anim = *ait;
00116                                         ++ait;
00117                                         anim->animateNode(this, timeMs);
00118                                 } 
00119 
00120                                 // update absolute position
00121                                 updateAbsolutePosition();
00122 
00123                                 // perform the post render process on all children
00124 
00125                                 core::list<ISceneNode*>::Iterator it = Children.begin();
00126                                 for (; it != Children.end(); ++it)
00127                                         (*it)->OnAnimate(timeMs);
00128                         }
00129                 }
00130 
00131 
00133                 virtual void render() = 0;
00134 
00135 
00137 
00138                 virtual const c8* getName() const
00139                 {
00140                         return Name.c_str();
00141                 }
00142 
00143 
00145 
00146                 virtual void setName(const c8* name)
00147                 {
00148                         Name = name;
00149                 }
00150 
00151 
00153 
00154                 virtual void setName(const core::stringc& name)
00155                 {
00156                         Name = name;
00157                 }
00158 
00159 
00161 
00168                 virtual const core::aabbox3d<f32>& getBoundingBox() const = 0;
00169 
00170 
00172 
00173                 virtual const core::aabbox3d<f32> getTransformedBoundingBox() const
00174                 {
00175                         core::aabbox3d<f32> box = getBoundingBox();
00176                         AbsoluteTransformation.transformBoxEx(box);
00177                         return box;
00178                 }
00179 
00180 
00183                 virtual const core::matrix4& getAbsoluteTransformation() const
00184                 {
00185                         return AbsoluteTransformation;
00186                 }
00187 
00188 
00190 
00194                 virtual core::matrix4 getRelativeTransformation() const
00195                 {
00196                         core::matrix4 mat;
00197                         mat.setRotationDegrees(RelativeRotation);
00198                         mat.setTranslation(RelativeTranslation);
00199 
00200                         if (RelativeScale != core::vector3df(1.f,1.f,1.f))
00201                         {
00202                                 core::matrix4 smat;
00203                                 smat.setScale(RelativeScale);
00204                                 mat *= smat;
00205                         }
00206 
00207                         return mat;
00208                 }
00209 
00210 
00212 
00216                 virtual bool isVisible() const
00217                 {
00218                         _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00219                         return IsVisible;
00220                 }
00221 
00223 
00225                 virtual bool isTrulyVisible() const
00226                 {
00227                         _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00228                         if(!IsVisible)
00229                                 return false;
00230 
00231                         if(!Parent)
00232                                 return true;
00233 
00234                         return Parent->isTrulyVisible();
00235                 }
00236 
00238 
00242                 virtual void setVisible(bool isVisible)
00243                 {
00244                         IsVisible = isVisible;
00245                 }
00246 
00247 
00249 
00251                 virtual s32 getID() const
00252                 {
00253                         return ID;
00254                 }
00255 
00256 
00258 
00260                 virtual void setID(s32 id)
00261                 {
00262                         ID = id;
00263                 }
00264 
00265 
00267 
00270                 virtual void addChild(ISceneNode* child)
00271                 {
00272                         if (child && (child != this))
00273                         {
00274                                 // Change scene manager?
00275                                 if (SceneManager != child->SceneManager)
00276                                         child->setSceneManager(SceneManager);
00277 
00278                                 child->grab();
00279                                 child->remove(); // remove from old parent
00280                                 Children.push_back(child);
00281                                 child->Parent = this;
00282                         }
00283                 }
00284 
00285 
00287 
00292                 virtual bool removeChild(ISceneNode* child)
00293                 {
00294                         core::list<ISceneNode*>::Iterator it = Children.begin();
00295                         for (; it != Children.end(); ++it)
00296                                 if ((*it) == child)
00297                                 {
00298                                         (*it)->Parent = 0;
00299                                         (*it)->drop();
00300                                         Children.erase(it);
00301                                         return true;
00302                                 }
00303 
00304                         _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00305                         return false;
00306                 }
00307 
00308 
00310 
00313                 virtual void removeAll()
00314                 {
00315                         core::list<ISceneNode*>::Iterator it = Children.begin();
00316                         for (; it != Children.end(); ++it)
00317                         {
00318                                 (*it)->Parent = 0;
00319                                 (*it)->drop();
00320                         }
00321 
00322                         Children.clear();
00323                 }
00324 
00325 
00327 
00329                 virtual void remove()
00330                 {
00331                         if (Parent)
00332                                 Parent->removeChild(this);
00333                 }
00334 
00335 
00337 
00338                 virtual void addAnimator(ISceneNodeAnimator* animator)
00339                 {
00340                         if (animator)
00341                         {
00342                                 Animators.push_back(animator);
00343                                 animator->grab();
00344                         }
00345                 }
00346 
00347 
00349 
00350                 const core::list<ISceneNodeAnimator*>& getAnimators() const
00351                 {
00352                         return Animators;
00353                 }
00354 
00355 
00357 
00360                 virtual void removeAnimator(ISceneNodeAnimator* animator)
00361                 {
00362                         core::list<ISceneNodeAnimator*>::Iterator it = Animators.begin();
00363                         for (; it != Animators.end(); ++it)
00364                         {
00365                                 if ((*it) == animator)
00366                                 {
00367                                         (*it)->drop();
00368                                         Animators.erase(it);
00369                                         return;
00370                                 }
00371                         }
00372                 }
00373 
00374 
00376 
00378                 virtual void removeAnimators()
00379                 {
00380                         core::list<ISceneNodeAnimator*>::Iterator it = Animators.begin();
00381                         for (; it != Animators.end(); ++it)
00382                                 (*it)->drop();
00383 
00384                         Animators.clear();
00385                 }
00386 
00387 
00389 
00396                 virtual video::SMaterial& getMaterial(u32 num)
00397                 {
00398                         return video::IdentityMaterial;
00399                 }
00400 
00401 
00403 
00404                 virtual u32 getMaterialCount() const
00405                 {
00406                         return 0;
00407                 }
00408 
00409 
00411 
00415                 void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
00416                 {
00417                         for (u32 i=0; i<getMaterialCount(); ++i)
00418                                 getMaterial(i).setFlag(flag, newvalue);
00419                 }
00420 
00421 
00423 
00426                 void setMaterialTexture(u32 textureLayer, video::ITexture* texture)
00427                 {
00428                         if (textureLayer >= video::MATERIAL_MAX_TEXTURES)
00429                                 return;
00430 
00431                         for (u32 i=0; i<getMaterialCount(); ++i)
00432                                 getMaterial(i).setTexture(textureLayer, texture);
00433                 }
00434 
00435 
00437 
00438                 void setMaterialType(video::E_MATERIAL_TYPE newType)
00439                 {
00440                         for (u32 i=0; i<getMaterialCount(); ++i)
00441                                 getMaterial(i).MaterialType = newType;
00442                 }
00443 
00444 
00446 
00450                 virtual const core::vector3df& getScale() const
00451                 {
00452                         return RelativeScale;
00453                 }
00454 
00455 
00457 
00458                 virtual void setScale(const core::vector3df& scale)
00459                 {
00460                         RelativeScale = scale;
00461                 }
00462 
00463 
00465 
00469                 virtual const core::vector3df& getRotation() const
00470                 {
00471                         return RelativeRotation;
00472                 }
00473 
00474 
00476 
00478                 virtual void setRotation(const core::vector3df& rotation)
00479                 {
00480                         RelativeRotation = rotation;
00481                 }
00482 
00483 
00485 
00488                 virtual const core::vector3df& getPosition() const
00489                 {
00490                         return RelativeTranslation;
00491                 }
00492 
00493 
00495 
00497                 virtual void setPosition(const core::vector3df& newpos)
00498                 {
00499                         RelativeTranslation = newpos;
00500                 }
00501 
00502 
00504 
00507                 virtual core::vector3df getAbsolutePosition() const
00508                 {
00509                         return AbsoluteTransformation.getTranslation();
00510                 }
00511 
00512 
00514 
00519                 void setAutomaticCulling( E_CULLING_TYPE state)
00520                 {
00521                         AutomaticCullingState = state;
00522                 }
00523 
00524 
00526 
00527                 E_CULLING_TYPE getAutomaticCulling() const
00528                 {
00529                         _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00530                         return AutomaticCullingState;
00531                 }
00532 
00533 
00535 
00538                 virtual void setDebugDataVisible(s32 state)
00539                 {
00540                         DebugDataVisible = state;
00541                 }
00542 
00544 
00546                 s32 isDebugDataVisible() const
00547                 {
00548                         return DebugDataVisible;
00549                 }
00550 
00551 
00553 
00555                 void setIsDebugObject(bool debugObject)
00556                 {
00557                         IsDebugObject = debugObject;
00558                 }
00559 
00560 
00562 
00565                 bool isDebugObject() const
00566                 {
00567                         _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00568                         return IsDebugObject;
00569                 }
00570 
00571 
00573 
00574                 const core::list<ISceneNode*>& getChildren() const
00575                 {
00576                         return Children;
00577                 }
00578 
00579 
00581 
00582                 virtual void setParent(ISceneNode* newParent)
00583                 {
00584                         grab();
00585                         remove();
00586 
00587                         Parent = newParent;
00588 
00589                         if (Parent)
00590                                 Parent->addChild(this);
00591 
00592                         drop();
00593                 }
00594 
00595 
00597 
00606                 virtual ITriangleSelector* getTriangleSelector() const
00607                 {
00608                         return TriangleSelector;
00609                 }
00610 
00611 
00613 
00621                 virtual void setTriangleSelector(ITriangleSelector* selector)
00622                 {
00623                         if (TriangleSelector != selector)
00624                         {
00625                                 if (TriangleSelector)
00626                                         TriangleSelector->drop();
00627 
00628                                 TriangleSelector = selector;
00629                                 if (TriangleSelector)
00630                                         TriangleSelector->grab();
00631                         }
00632                 }
00633 
00634 
00636 
00638                 virtual void updateAbsolutePosition()
00639                 {
00640                         if (Parent)
00641                         {
00642                                 AbsoluteTransformation =
00643                                         Parent->getAbsoluteTransformation() * getRelativeTransformation();
00644                         }
00645                         else
00646                                 AbsoluteTransformation = getRelativeTransformation();
00647                 }
00648 
00649 
00651 
00652                 scene::ISceneNode* getParent() const
00653                 {
00654                         return Parent;
00655                 }
00656 
00657 
00659 
00660                 virtual ESCENE_NODE_TYPE getType() const
00661                 {
00662                         return ESNT_UNKNOWN;
00663                 }
00664 
00665 
00667 
00673                 virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
00674                 {
00675                         if (!out)
00676                                 return;
00677                         out->addString  ("Name", Name.c_str());
00678                         out->addInt     ("Id", ID );
00679 
00680                         out->addVector3d("Position", getPosition() );
00681                         out->addVector3d("Rotation", getRotation() );
00682                         out->addVector3d("Scale", getScale() );
00683 
00684                         out->addBool    ("Visible", IsVisible );
00685                         out->addEnum    ("AutomaticCulling", AutomaticCullingState, AutomaticCullingNames);
00686                         out->addInt     ("DebugDataVisible", DebugDataVisible );
00687                         out->addBool    ("IsDebugObject", IsDebugObject );
00688                 }
00689 
00690 
00692 
00698                 virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
00699                 {
00700                         if (!in)
00701                                 return;
00702                         Name = in->getAttributeAsString("Name");
00703                         ID = in->getAttributeAsInt("Id");
00704 
00705                         setPosition(in->getAttributeAsVector3d("Position"));
00706                         setRotation(in->getAttributeAsVector3d("Rotation"));
00707                         setScale(in->getAttributeAsVector3d("Scale"));
00708 
00709                         IsVisible = in->getAttributeAsBool("Visible");
00710                         AutomaticCullingState = (scene::E_CULLING_TYPE) in->getAttributeAsEnumeration("AutomaticCulling",
00711                                         scene::AutomaticCullingNames);
00712 
00713                         DebugDataVisible = in->getAttributeAsInt("DebugDataVisible");
00714                         IsDebugObject = in->getAttributeAsBool("IsDebugObject");
00715 
00716                         updateAbsolutePosition();
00717                 }
00718 
00720 
00723                 virtual ISceneNode* clone(ISceneNode* newParent=0, ISceneManager* newManager=0)
00724                 {
00725                         return 0; // to be implemented by derived classes
00726                 }
00727 
00729 
00730                 virtual ISceneManager* getSceneManager(void) const { return SceneManager; }
00731 
00732         protected:
00733 
00735 
00739                 void cloneMembers(ISceneNode* toCopyFrom, ISceneManager* newManager)
00740                 {
00741                         Name = toCopyFrom->Name;
00742                         AbsoluteTransformation = toCopyFrom->AbsoluteTransformation;
00743                         RelativeTranslation = toCopyFrom->RelativeTranslation;
00744                         RelativeRotation = toCopyFrom->RelativeRotation;
00745                         RelativeScale = toCopyFrom->RelativeScale;
00746                         ID = toCopyFrom->ID;
00747                         setTriangleSelector(toCopyFrom->TriangleSelector);
00748                         AutomaticCullingState = toCopyFrom->AutomaticCullingState;
00749                         DebugDataVisible = toCopyFrom->DebugDataVisible;
00750                         IsVisible = toCopyFrom->IsVisible;
00751                         IsDebugObject = toCopyFrom->IsDebugObject;
00752 
00753                         if (newManager)
00754                                 SceneManager = newManager;
00755                         else
00756                                 SceneManager = toCopyFrom->SceneManager;
00757 
00758                         // clone children
00759 
00760                         core::list<ISceneNode*>::Iterator it = toCopyFrom->Children.begin();
00761                         for (; it != toCopyFrom->Children.end(); ++it)
00762                                 (*it)->clone(this, newManager);
00763 
00764                         // clone animators
00765 
00766                         core::list<ISceneNodeAnimator*>::Iterator ait = toCopyFrom->Animators.begin();
00767                         for (; ait != toCopyFrom->Animators.end(); ++ait)
00768                         {
00769                                 ISceneNodeAnimator* anim = (*ait)->createClone(this, SceneManager);
00770                                 if (anim)
00771                                 {
00772                                         addAnimator(anim);
00773                                         anim->drop();
00774                                 }
00775                         }
00776                 }
00777 
00780                 void setSceneManager(ISceneManager* newManager)
00781                 {
00782                         SceneManager = newManager;
00783 
00784                         core::list<ISceneNode*>::Iterator it = Children.begin();
00785                         for (; it != Children.end(); ++it)
00786                                 (*it)->setSceneManager(newManager);
00787                 }
00788 
00790                 core::stringc Name;
00791 
00793                 core::matrix4 AbsoluteTransformation;
00794 
00796                 core::vector3df RelativeTranslation;
00797 
00799                 core::vector3df RelativeRotation;
00800 
00802                 core::vector3df RelativeScale;
00803 
00805                 ISceneNode* Parent;
00806 
00808                 core::list<ISceneNode*> Children;
00809 
00811                 core::list<ISceneNodeAnimator*> Animators;
00812 
00814                 ISceneManager* SceneManager;
00815 
00817                 ITriangleSelector* TriangleSelector;
00818 
00820                 s32 ID;
00821 
00823                 E_CULLING_TYPE AutomaticCullingState;
00824 
00826                 s32 DebugDataVisible;
00827 
00829                 bool IsVisible;
00830 
00832                 bool IsDebugObject;
00833         };
00834 
00835 } // end namespace scene
00836 } // end namespace irr
00837 
00838 #endif
00839 

The Irrlicht Engine
The Irrlicht Engine Documentation © 2003-2009 by Nikolaus Gebhardt. Generated on Sun Jan 10 09:24:04 2010 by Doxygen (1.5.6)