123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- #pragma once
- #include "IReferenceCounted.h"
- #include "ESceneNodeTypes.h"
- #include "ECullingTypes.h"
- #include "EDebugSceneTypes.h"
- #include "SMaterial.h"
- #include "irrArray.h"
- #include "aabbox3d.h"
- #include "matrix4.h"
- #include <list>
- #include <optional>
- #include <string>
- namespace irr
- {
- namespace scene
- {
- class ISceneNode;
- class ISceneManager;
- typedef std::list<ISceneNode *> ISceneNodeList;
- class ISceneNode : virtual public IReferenceCounted
- {
- public:
-
- ISceneNode(ISceneNode *parent, ISceneManager *mgr, s32 id = -1,
- const core::vector3df &position = core::vector3df(0, 0, 0),
- const core::vector3df &rotation = core::vector3df(0, 0, 0),
- const core::vector3df &scale = core::vector3df(1.0f, 1.0f, 1.0f)) :
- RelativeTranslation(position),
- RelativeRotation(rotation), RelativeScale(scale),
- Parent(0), SceneManager(mgr), ID(id),
- AutomaticCullingState(EAC_BOX), DebugDataVisible(EDS_OFF),
- IsVisible(true), IsDebugObject(false)
- {
- if (parent)
- parent->addChild(this);
- updateAbsolutePosition();
- }
-
- virtual ~ISceneNode()
- {
-
- removeAll();
- }
-
-
- virtual void OnRegisterSceneNode()
- {
- if (IsVisible) {
- ISceneNodeList::iterator it = Children.begin();
- for (; it != Children.end(); ++it)
- (*it)->OnRegisterSceneNode();
- }
- }
-
-
- virtual void OnAnimate(u32 timeMs)
- {
- if (IsVisible) {
-
- updateAbsolutePosition();
-
- ISceneNodeList::iterator it = Children.begin();
- for (; it != Children.end(); ++it)
- (*it)->OnAnimate(timeMs);
- }
- }
-
- virtual void render() = 0;
-
-
- virtual const std::optional<std::string> &getName() const
- {
- return Name;
- }
-
-
- virtual void setName(const std::optional<std::string> &name)
- {
- Name = name;
- }
-
-
- virtual const core::aabbox3d<f32> &getBoundingBox() const = 0;
-
-
- virtual const core::aabbox3d<f32> getTransformedBoundingBox() const
- {
- core::aabbox3d<f32> box = getBoundingBox();
- AbsoluteTransformation.transformBoxEx(box);
- return box;
- }
-
-
-
- virtual void getTransformedBoundingBoxEdges(core::array<core::vector3d<f32>> &edges) const
- {
- edges.set_used(8);
- getBoundingBox().getEdges(edges.pointer());
- for (u32 i = 0; i < 8; ++i)
- AbsoluteTransformation.transformVect(edges[i]);
- }
-
-
- virtual const core::matrix4 &getAbsoluteTransformation() const
- {
- return AbsoluteTransformation;
- }
-
-
- virtual core::matrix4 getRelativeTransformation() const
- {
- core::matrix4 mat;
- mat.setRotationDegrees(RelativeRotation);
- mat.setTranslation(RelativeTranslation);
- if (RelativeScale != core::vector3df(1.f, 1.f, 1.f)) {
- core::matrix4 smat;
- smat.setScale(RelativeScale);
- mat *= smat;
- }
- return mat;
- }
-
-
- virtual bool isVisible() const
- {
- return IsVisible;
- }
-
-
- virtual bool isTrulyVisible() const
- {
- if (!IsVisible)
- return false;
- if (!Parent)
- return true;
- return Parent->isTrulyVisible();
- }
-
-
- virtual void setVisible(bool isVisible)
- {
- IsVisible = isVisible;
- }
-
-
- virtual s32 getID() const
- {
- return ID;
- }
-
-
- virtual void setID(s32 id)
- {
- ID = id;
- }
-
-
- virtual void addChild(ISceneNode *child)
- {
- if (child && (child != this)) {
-
- if (SceneManager != child->SceneManager)
- child->setSceneManager(SceneManager);
- child->grab();
- child->remove();
-
- child->ThisIterator = Children.insert(Children.end(), child);
- child->Parent = this;
- }
- }
-
-
- virtual bool removeChild(ISceneNode *child)
- {
- if (child->Parent != this)
- return false;
-
- _IRR_DEBUG_BREAK_IF(!child->ThisIterator.has_value());
- auto it = *child->ThisIterator;
- child->ThisIterator = std::nullopt;
- child->Parent = nullptr;
- child->drop();
- Children.erase(it);
- return true;
- }
-
-
- virtual void removeAll()
- {
- for (auto &child : Children) {
- child->Parent = nullptr;
- child->ThisIterator = std::nullopt;
- child->drop();
- }
- Children.clear();
- }
-
-
- virtual void remove()
- {
- if (Parent)
- Parent->removeChild(this);
- }
-
-
- virtual video::SMaterial &getMaterial(u32 num)
- {
- return video::IdentityMaterial;
- }
-
-
- virtual u32 getMaterialCount() const
- {
- return 0;
- }
-
-
- template <typename F>
- void forEachMaterial(F &&fn)
- {
- for (u32 i = 0; i < getMaterialCount(); i++) {
- fn(getMaterial(i));
- }
- }
-
-
- virtual const core::vector3df &getScale() const
- {
- return RelativeScale;
- }
-
-
- virtual void setScale(const core::vector3df &scale)
- {
- RelativeScale = scale;
- }
-
-
- virtual const core::vector3df &getRotation() const
- {
- return RelativeRotation;
- }
-
-
- virtual void setRotation(const core::vector3df &rotation)
- {
- RelativeRotation = rotation;
- }
-
-
- virtual const core::vector3df &getPosition() const
- {
- return RelativeTranslation;
- }
-
-
- virtual void setPosition(const core::vector3df &newpos)
- {
- RelativeTranslation = newpos;
- }
-
-
- virtual core::vector3df getAbsolutePosition() const
- {
- return AbsoluteTransformation.getTranslation();
- }
-
-
- void setAutomaticCulling(u32 state)
- {
- AutomaticCullingState = state;
- }
-
-
- u32 getAutomaticCulling() const
- {
- return AutomaticCullingState;
- }
-
-
- virtual void setDebugDataVisible(u32 state)
- {
- DebugDataVisible = state;
- }
-
-
- u32 isDebugDataVisible() const
- {
- return DebugDataVisible;
- }
-
-
- void setIsDebugObject(bool debugObject)
- {
- IsDebugObject = debugObject;
- }
-
-
- bool isDebugObject() const
- {
- return IsDebugObject;
- }
-
-
- const std::list<ISceneNode *> &getChildren() const
- {
- return Children;
- }
-
-
- virtual void setParent(ISceneNode *newParent)
- {
- grab();
- remove();
- if (newParent)
- newParent->addChild(this);
- drop();
- }
-
-
- virtual void updateAbsolutePosition()
- {
- if (Parent) {
- AbsoluteTransformation =
- Parent->getAbsoluteTransformation() * getRelativeTransformation();
- } else
- AbsoluteTransformation = getRelativeTransformation();
- }
-
-
- scene::ISceneNode *getParent() const
- {
- return Parent;
- }
-
-
- virtual ESCENE_NODE_TYPE getType() const
- {
- return ESNT_UNKNOWN;
- }
-
-
- virtual ISceneNode *clone(ISceneNode *newParent = 0, ISceneManager *newManager = 0)
- {
- return 0;
- }
-
-
- virtual ISceneManager *getSceneManager(void) const { return SceneManager; }
- protected:
-
-
- void cloneMembers(ISceneNode *toCopyFrom, ISceneManager *newManager)
- {
- Name = toCopyFrom->Name;
- AbsoluteTransformation = toCopyFrom->AbsoluteTransformation;
- RelativeTranslation = toCopyFrom->RelativeTranslation;
- RelativeRotation = toCopyFrom->RelativeRotation;
- RelativeScale = toCopyFrom->RelativeScale;
- ID = toCopyFrom->ID;
- AutomaticCullingState = toCopyFrom->AutomaticCullingState;
- DebugDataVisible = toCopyFrom->DebugDataVisible;
- IsVisible = toCopyFrom->IsVisible;
- IsDebugObject = toCopyFrom->IsDebugObject;
- if (newManager)
- SceneManager = newManager;
- else
- SceneManager = toCopyFrom->SceneManager;
-
- ISceneNodeList::iterator it = toCopyFrom->Children.begin();
- for (; it != toCopyFrom->Children.end(); ++it)
- (*it)->clone(this, newManager);
- }
-
-
- void setSceneManager(ISceneManager *newManager)
- {
- SceneManager = newManager;
- ISceneNodeList::iterator it = Children.begin();
- for (; it != Children.end(); ++it)
- (*it)->setSceneManager(newManager);
- }
-
- std::optional<std::string> Name;
-
- core::matrix4 AbsoluteTransformation;
-
- core::vector3df RelativeTranslation;
-
- core::vector3df RelativeRotation;
-
- core::vector3df RelativeScale;
-
- std::list<ISceneNode *> Children;
-
- std::optional<ISceneNodeList::iterator> ThisIterator;
-
- ISceneNode *Parent;
-
- ISceneManager *SceneManager;
-
- s32 ID;
-
- u32 AutomaticCullingState;
-
- u32 DebugDataVisible;
-
- bool IsVisible;
-
- bool IsDebugObject;
- };
- }
- }
|