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 
00290                 virtual bool removeChild(ISceneNode* child)
00291                 {
00292                         core::list<ISceneNode*>::Iterator it = Children.begin();
00293                         for (; it != Children.end(); ++it)
00294                                 if ((*it) == child)
00295                                 {
00296                                         (*it)->Parent = 0;
00297                                         (*it)->drop();
00298                                         Children.erase(it);
00299                                         return true;
00300                                 }
00301 
00302                         _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00303                         return false;
00304                 }
00305 
00306 
00308                 virtual void removeAll()
00309                 {
00310                         core::list<ISceneNode*>::Iterator it = Children.begin();
00311                         for (; it != Children.end(); ++it)
00312                         {
00313                                 (*it)->Parent = 0;
00314                                 (*it)->drop();
00315                         }
00316 
00317                         Children.clear();
00318                 }
00319 
00320 
00322                 virtual void remove()
00323                 {
00324                         if (Parent)
00325                                 Parent->removeChild(this);
00326                 }
00327 
00328 
00330 
00331                 virtual void addAnimator(ISceneNodeAnimator* animator)
00332                 {
00333                         if (animator)
00334                         {
00335                                 Animators.push_back(animator);
00336                                 animator->grab();
00337                         }
00338                 }
00339 
00340 
00342 
00343                 const core::list<ISceneNodeAnimator*>& getAnimators() const
00344                 {
00345                         return Animators;
00346                 }
00347 
00348 
00350 
00351                 virtual void removeAnimator(ISceneNodeAnimator* animator)
00352                 {
00353                         core::list<ISceneNodeAnimator*>::Iterator it = Animators.begin();
00354                         for (; it != Animators.end(); ++it)
00355                                 if ((*it) == animator)
00356                                 {
00357                                         (*it)->drop();
00358                                         Animators.erase(it);
00359                                         return;
00360                                 }
00361                 }
00362 
00363 
00365                 virtual void removeAnimators()
00366                 {
00367                         core::list<ISceneNodeAnimator*>::Iterator it = Animators.begin();
00368                         for (; it != Animators.end(); ++it)
00369                                 (*it)->drop();
00370 
00371                         Animators.clear();
00372                 }
00373 
00374 
00376 
00383                 virtual video::SMaterial& getMaterial(u32 num)
00384                 {
00385                         return video::IdentityMaterial;
00386                 }
00387 
00388 
00390 
00391                 virtual u32 getMaterialCount() const
00392                 {
00393                         return 0;
00394                 }
00395 
00396 
00398 
00402                 void setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
00403                 {
00404                         for (u32 i=0; i<getMaterialCount(); ++i)
00405                                 getMaterial(i).setFlag(flag, newvalue);
00406                 }
00407 
00408 
00410 
00413                 void setMaterialTexture(u32 textureLayer, video::ITexture* texture)
00414                 {
00415                         if (textureLayer >= video::MATERIAL_MAX_TEXTURES)
00416                                 return;
00417 
00418                         for (u32 i=0; i<getMaterialCount(); ++i)
00419                                 getMaterial(i).setTexture(textureLayer, texture);
00420                 }
00421 
00422 
00424 
00425                 void setMaterialType(video::E_MATERIAL_TYPE newType)
00426                 {
00427                         for (u32 i=0; i<getMaterialCount(); ++i)
00428                                 getMaterial(i).MaterialType = newType;
00429                 }
00430 
00431 
00433 
00437                 virtual const core::vector3df& getScale() const
00438                 {
00439                         return RelativeScale;
00440                 }
00441 
00442 
00444 
00445                 virtual void setScale(const core::vector3df& scale)
00446                 {
00447                         RelativeScale = scale;
00448                 }
00449 
00450 
00452 
00456                 virtual const core::vector3df& getRotation() const
00457                 {
00458                         return RelativeRotation;
00459                 }
00460 
00461 
00463 
00465                 virtual void setRotation(const core::vector3df& rotation)
00466                 {
00467                         RelativeRotation = rotation;
00468                 }
00469 
00470 
00472 
00475                 virtual const core::vector3df& getPosition() const
00476                 {
00477                         return RelativeTranslation;
00478                 }
00479 
00480 
00482 
00484                 virtual void setPosition(const core::vector3df& newpos)
00485                 {
00486                         RelativeTranslation = newpos;
00487                 }
00488 
00489 
00491 
00494                 virtual core::vector3df getAbsolutePosition() const
00495                 {
00496                         return AbsoluteTransformation.getTranslation();
00497                 }
00498 
00499 
00501 
00506                 void setAutomaticCulling( E_CULLING_TYPE state)
00507                 {
00508                         AutomaticCullingState = state;
00509                 }
00510 
00511 
00513 
00514                 E_CULLING_TYPE getAutomaticCulling() const
00515                 {
00516                         _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00517                         return AutomaticCullingState;
00518                 }
00519 
00520 
00522 
00525                 virtual void setDebugDataVisible(s32 state)
00526                 {
00527                         DebugDataVisible = state;
00528                 }
00529 
00531 
00533                 s32 isDebugDataVisible() const
00534                 {
00535                         return DebugDataVisible;
00536                 }
00537 
00538 
00540 
00542                 void setIsDebugObject(bool debugObject)
00543                 {
00544                         IsDebugObject = debugObject;
00545                 }
00546 
00547 
00549 
00552                 bool isDebugObject() const
00553                 {
00554                         _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
00555                         return IsDebugObject;
00556                 }
00557 
00558 
00560 
00561                 const core::list<ISceneNode*>& getChildren() const
00562                 {
00563                         return Children;
00564                 }
00565 
00566 
00568 
00569                 virtual void setParent(ISceneNode* newParent)
00570                 {
00571                         grab();
00572                         remove();
00573 
00574                         Parent = newParent;
00575 
00576                         if (Parent)
00577                                 Parent->addChild(this);
00578 
00579                         drop();
00580                 }
00581 
00582 
00584 
00593                 virtual ITriangleSelector* getTriangleSelector() const
00594                 {
00595                         return TriangleSelector;
00596                 }
00597 
00598 
00600 
00608                 virtual void setTriangleSelector(ITriangleSelector* selector)
00609                 {
00610                         if (TriangleSelector != selector)
00611                         {
00612                                 if (TriangleSelector)
00613                                         TriangleSelector->drop();
00614 
00615                                 TriangleSelector = selector;
00616                                 if (TriangleSelector)
00617                                         TriangleSelector->grab();
00618                         }
00619                 }
00620 
00621 
00623 
00625                 virtual void updateAbsolutePosition()
00626                 {
00627                         if (Parent)
00628                         {
00629                                 AbsoluteTransformation =
00630                                         Parent->getAbsoluteTransformation() * getRelativeTransformation();
00631                         }
00632                         else
00633                                 AbsoluteTransformation = getRelativeTransformation();
00634                 }
00635 
00636 
00638 
00639                 scene::ISceneNode* getParent() const
00640                 {
00641                         return Parent;
00642                 }
00643 
00644 
00646 
00647                 virtual ESCENE_NODE_TYPE getType() const
00648                 {
00649                         return ESNT_UNKNOWN;
00650                 }
00651 
00652 
00654 
00660                 virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
00661                 {
00662                         if (!out)
00663                                 return;
00664                         out->addString  ("Name", Name.c_str());
00665                         out->addInt     ("Id", ID );
00666 
00667                         out->addVector3d("Position", getPosition() );
00668                         out->addVector3d("Rotation", getRotation() );
00669                         out->addVector3d("Scale", getScale() );
00670 
00671                         out->addBool    ("Visible", IsVisible );
00672                         out->addEnum    ("AutomaticCulling", AutomaticCullingState, AutomaticCullingNames);
00673                         out->addInt     ("DebugDataVisible", DebugDataVisible );
00674                         out->addBool    ("IsDebugObject", IsDebugObject );
00675                 }
00676 
00677 
00679 
00685                 virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
00686                 {
00687                         if (!in)
00688                                 return;
00689                         Name = in->getAttributeAsString("Name");
00690                         ID = in->getAttributeAsInt("Id");
00691 
00692                         setPosition(in->getAttributeAsVector3d("Position"));
00693                         setRotation(in->getAttributeAsVector3d("Rotation"));
00694                         setScale(in->getAttributeAsVector3d("Scale"));
00695 
00696                         IsVisible = in->getAttributeAsBool("Visible");
00697                         AutomaticCullingState = (scene::E_CULLING_TYPE) in->getAttributeAsEnumeration("AutomaticCulling",
00698                                         scene::AutomaticCullingNames);
00699 
00700                         DebugDataVisible = in->getAttributeAsInt("DebugDataVisible");
00701                         IsDebugObject = in->getAttributeAsBool("IsDebugObject");
00702 
00703                         updateAbsolutePosition();
00704                 }
00705 
00707 
00710                 virtual ISceneNode* clone(ISceneNode* newParent=0, ISceneManager* newManager=0)
00711                 {
00712                         return 0; // to be implemented by derived classes
00713                 }
00714 
00716 
00717                 virtual ISceneManager* getSceneManager(void) const { return SceneManager; }
00718 
00719         protected:
00720 
00722 
00726                 void cloneMembers(ISceneNode* toCopyFrom, ISceneManager* newManager)
00727                 {
00728                         Name = toCopyFrom->Name;
00729                         AbsoluteTransformation = toCopyFrom->AbsoluteTransformation;
00730                         RelativeTranslation = toCopyFrom->RelativeTranslation;
00731                         RelativeRotation = toCopyFrom->RelativeRotation;
00732                         RelativeScale = toCopyFrom->RelativeScale;
00733                         ID = toCopyFrom->ID;
00734                         setTriangleSelector(toCopyFrom->TriangleSelector);
00735                         AutomaticCullingState = toCopyFrom->AutomaticCullingState;
00736                         DebugDataVisible = toCopyFrom->DebugDataVisible;
00737                         IsVisible = toCopyFrom->IsVisible;
00738                         IsDebugObject = toCopyFrom->IsDebugObject;
00739 
00740                         if (newManager)
00741                                 SceneManager = newManager;
00742                         else
00743                                 SceneManager = toCopyFrom->SceneManager;
00744 
00745                         // clone children
00746 
00747                         core::list<ISceneNode*>::Iterator it = toCopyFrom->Children.begin();
00748                         for (; it != toCopyFrom->Children.end(); ++it)
00749                                 (*it)->clone(this, newManager);
00750 
00751                         // clone animators
00752 
00753                         core::list<ISceneNodeAnimator*>::Iterator ait = toCopyFrom->Animators.begin();
00754                         for (; ait != toCopyFrom->Animators.end(); ++ait)
00755                         {
00756                                 ISceneNodeAnimator* anim = (*ait)->createClone(this, SceneManager);
00757                                 if (anim)
00758                                 {
00759                                         addAnimator(anim);
00760                                         anim->drop();
00761                                 }
00762                         }
00763                 }
00764 
00767                 void setSceneManager(ISceneManager* newManager)
00768                 {
00769                         SceneManager = newManager;
00770 
00771                         core::list<ISceneNode*>::Iterator it = Children.begin();
00772                         for (; it != Children.end(); ++it)
00773                                 (*it)->setSceneManager(newManager);
00774                 }
00775 
00777                 core::stringc Name;
00778 
00780                 core::matrix4 AbsoluteTransformation;
00781 
00783                 core::vector3df RelativeTranslation;
00784 
00786                 core::vector3df RelativeRotation;
00787 
00789                 core::vector3df RelativeScale;
00790 
00792                 ISceneNode* Parent;
00793 
00795                 core::list<ISceneNode*> Children;
00796 
00798                 core::list<ISceneNodeAnimator*> Animators;
00799 
00801                 ISceneManager* SceneManager;
00802 
00804                 ITriangleSelector* TriangleSelector;
00805 
00807                 s32 ID;
00808 
00810                 E_CULLING_TYPE AutomaticCullingState;
00811 
00813                 s32 DebugDataVisible;
00814 
00816                 bool IsVisible;
00817 
00819                 bool IsDebugObject;
00820         };
00821 
00822 } // end namespace scene
00823 } // end namespace irr
00824 
00825 #endif
00826 

The Irrlicht Engine
The Irrlicht Engine Documentation © 2003-2009 by Nikolaus Gebhardt. Generated on Mon Oct 26 12:48:36 2009 by Doxygen (1.5.6)