Leviathan  0.8.0.0
Leviathan game engine
SceneNode.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2020 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
6 #include "Common/Matrix.h"
7 #include "Common/Quaternion.h"
9 #include "Common/Types.h"
10 
11 #include "bsfCore/BsCorePrerequisites.h"
12 #include "bsfCore/Scene/BsSceneObject.h"
13 
14 namespace Leviathan {
15 
16 class SceneNode;
17 class Scene;
18 
23 struct Transform {
24 public:
25  Float3 Translation = Float3(0.f, 0.f, 0.f);
26  Float3 Scale = Float3(1.f, 1.f, 1.f);
28 };
29 
30 
33  friend SceneNode;
34 
35 public:
37 
40 
41  inline SceneNode* GetParent() const
42  {
43  return Parent;
44  }
45 
46  inline bool HasParent() const
47  {
48  return Parent != nullptr;
49  }
50 
52 
53 protected:
54  // Callbacks for child classes
55  DLLEXPORT virtual void OnAttachedToParent(SceneNode& parent);
56  DLLEXPORT virtual void OnDetachedFromParent(SceneNode& oldparent);
57 
58  // Methods called by SceneNode
59  DLLEXPORT void NotifyDetachParent(SceneNode& oldparent);
61 
62  // Called by Scene when it is time to render
63  DLLEXPORT virtual void PrepareToRender();
64 
65 private:
66  SceneNode* Parent = nullptr;
67 };
68 
70 class SceneNode : public SceneAttachable {
71  friend Scene;
72 
73 protected:
74  // These are protected for only constructing properly initialized instances through a Scene
75  DLLEXPORT SceneNode(SceneNode* parent, Scene* scene);
76  DLLEXPORT SceneNode(bs::HSceneObject node, Scene* scene);
77 
78 public:
82 
83  SceneNode(const SceneNode& other) = delete;
84  SceneNode operator=(const SceneNode& other) = delete;
85 
90  DLLEXPORT void AttachObject(const SceneAttachable::pointer& object);
91 
94 
95  DLLEXPORT bool HasChild(SceneAttachable* child) const;
96 
97  inline void SetPosition(const Float3& pos)
98  {
99  Node->setPosition(pos);
100  LocalTransform.Translation = pos;
101  MarkDirty();
102  }
103 
104  inline Float3 GetPosition() const
105  {
106  return LocalTransform.Translation;
107  }
108 
109  inline void SetOrientation(const Quaternion& orientation)
110  {
111  Node->setRotation(orientation);
112  LocalTransform.Orientation = orientation;
113  MarkDirty();
114  }
115 
117  FORCE_INLINE void SetRotation(const Quaternion& orientation)
118  {
119  SetOrientation(orientation);
120  }
121 
122  inline Quaternion GetOrientation() const
123  {
124  return LocalTransform.Orientation;
125  }
126 
127  inline void SetPositionAndOrientation(const Float3& pos, const Quaternion& orientation)
128  {
129  SetPosition(pos);
130  SetOrientation(orientation);
131 
132  LocalTransform.Translation = pos;
133  LocalTransform.Orientation = orientation;
134  MarkDirty();
135  }
136 
137  inline void SetScale(const Float3& scale)
138  {
139  Node->setScale(scale);
140  LocalTransform.Scale = scale;
141  MarkDirty();
142  }
143 
144  void SetHidden(bool hidden)
145  {
146  if(Hidden == hidden)
147  return;
148 
149  Hidden = hidden;
150  Node->setActive(!Hidden);
151  }
152 
153  bool IsHidden(bool hidden)
154  {
155  return Hidden;
156  }
157 
158  Scene* GetScene() const
159  {
160  return ParentScene;
161  }
162 
163  inline void MarkDirty()
164  {
165  Dirty = true;
166  TransformDirty = true;
167  }
168 
169  const Transform& GetWorldTransform() const;
170 
171  inline bs::HSceneObject GetInternal()
172  {
173  return Node;
174  }
175 
177  {
178  return AttachObject(SceneAttachable::WrapPtr(object));
179  }
180 
182  {
183  // This is used to release the reference given to us from scripts
184  const auto wrapped = SceneAttachable::WrapPtr(object);
185  return AttachObject(object);
186  }
187 
189 
190 protected:
191  DLLEXPORT void OnAttachedToParent(SceneNode& parent) override;
192  DLLEXPORT void OnDetachedFromParent(SceneNode& oldparent) override;
193 
195 
196  // Called by Scene when it is time to render
197  DLLEXPORT void PrepareToRender() override;
198 
199 private:
200  bs::HSceneObject Node;
201 
202  Scene* ParentScene = nullptr;
203 
204  Transform LocalTransform;
205 
206  // These computations depend on getting the world transform from the parent
207  mutable Transform CachedWorldTransform;
208  mutable Matrix4 CachedFinalMatrix = Matrix4::IDENTITY;
209 
210  std::vector<SceneAttachable::pointer> Children;
211 
212  bool Hidden = false;
213 
215  mutable bool Dirty = true;
216 
218  mutable bool TransformDirty = true;
219 };
220 
221 } // namespace Leviathan
const Transform & GetWorldTransform() const
Definition: SceneNode.cpp:128
SceneNode operator=(const SceneNode &other)=delete
REFERENCE_COUNTED_PTR_TYPE(SceneAttachable)
bool IsHidden(bool hidden)
Definition: SceneNode.h:153
DLLEXPORT void OnDetachedFromParent(SceneNode &oldparent) override
Definition: SceneNode.cpp:118
DLLEXPORT void NotifyAttachParent(SceneNode &parent)
Definition: SceneNode.cpp:34
void AttachObjectWrapper(SceneAttachable *object)
Definition: SceneNode.h:176
DLLEXPORT void PrepareToRender() override
Definition: SceneNode.cpp:187
DLLEXPORT bool HasChild(SceneAttachable *child) const
Definition: SceneNode.cpp:101
Float3 GetPosition() const
Definition: SceneNode.h:104
void SetOrientation(const Quaternion &orientation)
Definition: SceneNode.h:109
World transform for an object.
Definition: SceneNode.h:23
void SetScale(const Float3 &scale)
Definition: SceneNode.h:137
#define FORCE_INLINE
Definition: Include.h:95
A node in the Scene used to position renderables and other SceneNodes.
Definition: SceneNode.h:70
void SetPosition(const Float3 &pos)
Definition: SceneNode.h:97
DLLEXPORT void OnAttachedToParent(SceneNode &parent) override
Definition: SceneNode.cpp:112
DLLEXPORT SceneNode(SceneNode *parent, Scene *scene)
Definition: SceneNode.cpp:49
DLLEXPORT void DetachFromParent()
Detaches this from parent if attached.
Definition: SceneNode.cpp:14
Scene * GetScene() const
Definition: SceneNode.h:158
Quaternion Orientation
Definition: SceneNode.h:27
DLLEXPORT bool DetachChild(SceneAttachable *child)
Removes an added child, returns true if succeeded.
Definition: SceneNode.cpp:85
virtual DLLEXPORT void OnAttachedToParent(SceneNode &parent)
Definition: SceneNode.cpp:20
virtual DLLEXPORT void PrepareToRender()
Definition: SceneNode.cpp:46
bs::HSceneObject GetInternal()
Definition: SceneNode.h:171
Quaternion GetOrientation() const
Definition: SceneNode.h:122
void SetPositionAndOrientation(const Float3 &pos, const Quaternion &orientation)
Definition: SceneNode.h:127
FORCE_INLINE void SetRotation(const Quaternion &orientation)
Alias for SetOrientation.
Definition: SceneNode.h:117
Base class for all objects attachable to a SceneNode.
Definition: SceneNode.h:32
DLLEXPORT ~SceneAttachable()
Definition: SceneNode.cpp:9
DLLEXPORT ~SceneNode()
Definition: SceneNode.cpp:62
A Quaternion type to make quaternion specific operations easier to access.
Definition: Quaternion.h:14
bool HasParent() const
Definition: SceneNode.h:46
DLLEXPORT void AttachObject(const SceneAttachable::pointer &object)
Attaches an object to this SceneNode.
Definition: SceneNode.cpp:68
virtual DLLEXPORT void OnDetachedFromParent(SceneNode &oldparent)
Definition: SceneNode.cpp:21
#define DLLEXPORT
Definition: Include.h:84
void DetachObjectWrapper(SceneAttachable *object)
Definition: SceneNode.h:181
REFERENCE_COUNTED_PTR_TYPE(SceneNode)
static const Quaternion IDENTITY
Definition: Quaternion.h:322
DLLEXPORT void NotifyDetachParent(SceneNode &oldparent)
Definition: SceneNode.cpp:23
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
void SetHidden(bool hidden)
Definition: SceneNode.h:144
static boost::intrusive_ptr< ActualType > WrapPtr(ActualType *ptr)
Creates an intrusive_ptr from raw pointer.
void ApplyWorldMatrixIfDirty()
Definition: SceneNode.cpp:159
SceneNode * GetParent() const
Definition: SceneNode.h:41
A 4x4 matrix type.
Definition: Matrix.h:379
static DLLEXPORT const Matrix4 IDENTITY
Definition: Matrix.h:873