Leviathan  0.8.0.0
Leviathan game engine
Leviathan::SceneNode Class Reference

A node in the Scene used to position renderables and other SceneNodes. More...

#include <SceneNode.h>

+ Inheritance diagram for Leviathan::SceneNode:

Public Member Functions

DLLEXPORT ~SceneNode ()
 
 SceneNode (const SceneNode &other)=delete
 
SceneNode operator= (const SceneNode &other)=delete
 
DLLEXPORT void AttachObject (const SceneAttachable::pointer &object)
 Attaches an object to this SceneNode. More...
 
DLLEXPORT bool DetachChild (SceneAttachable *child)
 Removes an added child, returns true if succeeded. More...
 
DLLEXPORT bool HasChild (SceneAttachable *child) const
 
void SetPosition (const Float3 &pos)
 
Float3 GetPosition () const
 
void SetOrientation (const Quaternion &orientation)
 
FORCE_INLINE void SetRotation (const Quaternion &orientation)
 Alias for SetOrientation. More...
 
Quaternion GetOrientation () const
 
void SetPositionAndOrientation (const Float3 &pos, const Quaternion &orientation)
 
void SetScale (const Float3 &scale)
 
void SetHidden (bool hidden)
 
bool IsHidden (bool hidden)
 
SceneGetScene () const
 
void MarkDirty ()
 
const TransformGetWorldTransform () const
 
bs::HSceneObject GetInternal ()
 
void AttachObjectWrapper (SceneAttachable *object)
 
void DetachObjectWrapper (SceneAttachable *object)
 
 REFERENCE_COUNTED_PTR_TYPE (SceneNode)
 
- Public Member Functions inherited from Leviathan::SceneAttachable
DLLEXPORT ~SceneAttachable ()
 
DLLEXPORT void DetachFromParent ()
 Detaches this from parent if attached. More...
 
SceneNodeGetParent () const
 
bool HasParent () const
 
 REFERENCE_COUNTED_PTR_TYPE (SceneAttachable)
 
- Public Member Functions inherited from Leviathan::ReferenceCounted
 ReferenceCounted (const ReferenceCounted &other)=delete
 
ReferenceCountedoperator= (const ReferenceCounted &other)=delete
 
FORCE_INLINE void AddRef () const
 
FORCE_INLINE void Release () const
 removes a reference and deletes the object if reference count reaches zero More...
 
FORCE_INLINE void AddRef ()
 
FORCE_INLINE void Release ()
 
int32_t GetRefCount () const
 Returns the reference count. More...
 

Protected Member Functions

DLLEXPORT SceneNode (SceneNode *parent, Scene *scene)
 
DLLEXPORT SceneNode (bs::HSceneObject node, Scene *scene)
 
DLLEXPORT void OnAttachedToParent (SceneNode &parent) override
 
DLLEXPORT void OnDetachedFromParent (SceneNode &oldparent) override
 
void ApplyWorldMatrixIfDirty ()
 
DLLEXPORT void PrepareToRender () override
 
- Protected Member Functions inherited from Leviathan::SceneAttachable
DLLEXPORT void NotifyDetachParent (SceneNode &oldparent)
 
DLLEXPORT void NotifyAttachParent (SceneNode &parent)
 
- Protected Member Functions inherited from Leviathan::ReferenceCounted
DLLEXPORT ReferenceCounted ()
 
virtual DLLEXPORT ~ReferenceCounted ()
 

Additional Inherited Members

- Public Types inherited from Leviathan::ReferenceCounted
using basepointer = boost::intrusive_ptr< ReferenceCounted >
 
using refcountedpointer = boost::intrusive_ptr< ReferenceCounted >
 
- Static Public Member Functions inherited from Leviathan::ReferenceCounted
template<class ActualType >
static boost::intrusive_ptr< ActualType > WrapPtr (ActualType *ptr)
 Creates an intrusive_ptr from raw pointer. More...
 
template<class ActualType , class... Args>
static boost::intrusive_ptr< ActualType > MakeShared (Args &&... args)
 Constructs a new instance and wraps it. More...
 

Detailed Description

A node in the Scene used to position renderables and other SceneNodes.

Definition at line 70 of file SceneNode.h.

Constructor & Destructor Documentation

◆ SceneNode() [1/3]

DLLEXPORT SceneNode::SceneNode ( SceneNode parent,
Scene scene 
)
protected

Definition at line 49 of file SceneNode.cpp.

49  :
50  Node(bs::SceneObject::create("")), ParentScene(scene)
51 {
52  LEVIATHAN_ASSERT(Node, "bs SceneObject creation failed");
53 
54  parent->AttachObject(this);
55 }
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:100
DLLEXPORT void AttachObject(const SceneAttachable::pointer &object)
Attaches an object to this SceneNode.
Definition: SceneNode.cpp:68

◆ SceneNode() [2/3]

DLLEXPORT SceneNode::SceneNode ( bs::HSceneObject  node,
Scene scene 
)
protected

Definition at line 58 of file SceneNode.cpp.

58  :
59  Node(node), ParentScene(scene)
60 {}

◆ ~SceneNode()

DLLEXPORT SceneNode::~SceneNode ( )
Note
As the scene nodes recursively get detached some care needs to be taken to not have very deep hierarchies to avoid a stack overflow

Definition at line 62 of file SceneNode.cpp.

63 {
64  for(auto child : Children)
65  child->NotifyDetachParent(*this);
66 }

◆ SceneNode() [3/3]

Leviathan::SceneNode::SceneNode ( const SceneNode other)
delete

Member Function Documentation

◆ ApplyWorldMatrixIfDirty()

void SceneNode::ApplyWorldMatrixIfDirty ( )
protected

Definition at line 159 of file SceneNode.cpp.

160 {
161  if(!TransformDirty)
162  return;
163 
164  const auto& transform = GetWorldTransform();
165 
166  CachedFinalMatrix =
167  Matrix4::FromTRS(transform.Translation, transform.Orientation, transform.Scale);
168 
169  // LOG_WRITE("final props: pos: " + Convert::ToString(transform.Translation) +
170  // " scale: " + Convert::ToString(transform.Scale));
171 
172  // Apply to bsf
173  // Can't use the Matrix here, but at least it can be verified that the individual parts are
174  // right
175  // This doesn't work for some reason. BSF doesn't want to work without using its parenting
176  // method, but the debug output numbers looked good
177  // Node->setPosition(transform.Translation);
178  // Node->setRotation(transform.Orientation);
179  // Node->setScale(transform.Scale);
180  // Node->setWorldPosition(transform.Translation);
181  // Node->setWorldRotation(transform.Orientation);
182  // Node->setWorldScale(transform.Scale);
183 
184  TransformDirty = false;
185 }
const Transform & GetWorldTransform() const
Definition: SceneNode.cpp:128
static DLLEXPORT Matrix4 FromTRS(const Float3 &translation, const Quaternion &rotation, const Float3 &scale)
Definition: Matrix.cpp:1215

◆ AttachObject()

DLLEXPORT void SceneNode::AttachObject ( const SceneAttachable::pointer &  object)

Attaches an object to this SceneNode.

And object can be attached only to one node at a time. This will detach the object first if it is attached

Definition at line 68 of file SceneNode.cpp.

69 {
70  if(!object || object.get() == this)
71  return;
72 
73  // Detach the object automatically if it is already attached
74  if(object->HasParent()) {
75  if(object->GetParent() == this)
76  return;
77 
78  object->DetachFromParent();
79  }
80 
81  Children.push_back(object);
82  object->NotifyAttachParent(*this);
83 }

◆ AttachObjectWrapper()

void Leviathan::SceneNode::AttachObjectWrapper ( SceneAttachable object)
inline

Definition at line 176 of file SceneNode.h.

177  {
178  return AttachObject(SceneAttachable::WrapPtr(object));
179  }
DLLEXPORT void AttachObject(const SceneAttachable::pointer &object)
Attaches an object to this SceneNode.
Definition: SceneNode.cpp:68
static boost::intrusive_ptr< ActualType > WrapPtr(ActualType *ptr)
Creates an intrusive_ptr from raw pointer.

◆ DetachChild()

DLLEXPORT bool SceneNode::DetachChild ( SceneAttachable child)

Removes an added child, returns true if succeeded.

Definition at line 85 of file SceneNode.cpp.

86 {
87  if(!child)
88  return false;
89 
90  for(auto iter = Children.begin(); iter != Children.end(); ++iter) {
91  if(*iter == child) {
92  (*iter)->NotifyDetachParent(*this);
93  Children.erase(iter);
94  return true;
95  }
96  }
97 
98  return false;
99 }

◆ DetachObjectWrapper()

void Leviathan::SceneNode::DetachObjectWrapper ( SceneAttachable object)
inline

Definition at line 181 of file SceneNode.h.

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  }
DLLEXPORT void AttachObject(const SceneAttachable::pointer &object)
Attaches an object to this SceneNode.
Definition: SceneNode.cpp:68
static boost::intrusive_ptr< ActualType > WrapPtr(ActualType *ptr)
Creates an intrusive_ptr from raw pointer.

◆ GetInternal()

bs::HSceneObject Leviathan::SceneNode::GetInternal ( )
inline

Definition at line 171 of file SceneNode.h.

172  {
173  return Node;
174  }

◆ GetOrientation()

Quaternion Leviathan::SceneNode::GetOrientation ( ) const
inline

Definition at line 122 of file SceneNode.h.

123  {
124  return LocalTransform.Orientation;
125  }
Quaternion Orientation
Definition: SceneNode.h:27

◆ GetPosition()

Float3 Leviathan::SceneNode::GetPosition ( ) const
inline

Definition at line 104 of file SceneNode.h.

105  {
106  return LocalTransform.Translation;
107  }

◆ GetScene()

Scene* Leviathan::SceneNode::GetScene ( ) const
inline

Definition at line 158 of file SceneNode.h.

159  {
160  return ParentScene;
161  }

◆ GetWorldTransform()

const Transform & SceneNode::GetWorldTransform ( ) const

Definition at line 128 of file SceneNode.cpp.

129 {
130  if(!Dirty)
131  return CachedWorldTransform;
132 
133  if(!Parent) {
134 
135  CachedWorldTransform = LocalTransform;
136 
137  return CachedWorldTransform;
138  }
139 
140  const auto& parentTransform = Parent->GetWorldTransform();
141 
142  // Calculate each 3 Transform member separately
143  CachedWorldTransform.Orientation =
144  parentTransform.Orientation * LocalTransform.Orientation;
145 
146  CachedWorldTransform.Scale = parentTransform.Scale * LocalTransform.Scale;
147 
148  // Adjust the local translation based on parent's orientation and scale
149  CachedWorldTransform.Translation =
150  parentTransform.Orientation * (parentTransform.Scale * LocalTransform.Translation);
151 
152  // Add parent position
153  CachedWorldTransform.Translation += parentTransform.Translation;
154 
155  Dirty = false;
156  return CachedWorldTransform;
157 }
Quaternion Orientation
Definition: SceneNode.h:27

◆ HasChild()

DLLEXPORT bool SceneNode::HasChild ( SceneAttachable child) const

Definition at line 101 of file SceneNode.cpp.

102 {
103  for(auto iter = Children.begin(); iter != Children.end(); ++iter) {
104  if(*iter == child) {
105  return true;
106  }
107  }
108 
109  return false;
110 }

◆ IsHidden()

bool Leviathan::SceneNode::IsHidden ( bool  hidden)
inline

Definition at line 153 of file SceneNode.h.

154  {
155  return Hidden;
156  }

◆ MarkDirty()

void Leviathan::SceneNode::MarkDirty ( )
inline

Definition at line 163 of file SceneNode.h.

164  {
165  Dirty = true;
166  TransformDirty = true;
167  }

◆ OnAttachedToParent()

DLLEXPORT void SceneNode::OnAttachedToParent ( SceneNode parent)
overrideprotectedvirtual

Reimplemented from Leviathan::SceneAttachable.

Definition at line 112 of file SceneNode.cpp.

113 {
114  MarkDirty();
115  GetInternal()->setParent(parent.GetInternal(), false);
116 }
bs::HSceneObject GetInternal()
Definition: SceneNode.h:171

◆ OnDetachedFromParent()

DLLEXPORT void SceneNode::OnDetachedFromParent ( SceneNode oldparent)
overrideprotectedvirtual

Reimplemented from Leviathan::SceneAttachable.

Definition at line 118 of file SceneNode.cpp.

119 {
120  if(!ParentScene || !ParentScene->GetRootSceneNode())
121  return;
122 
123  MarkDirty();
124 
125  GetInternal()->setParent(ParentScene->GetRootSceneNode()->GetInternal(), false);
126 }
SceneNode::pointer GetRootSceneNode()
Definition: Scene.h:33
bs::HSceneObject GetInternal()
Definition: SceneNode.h:171

◆ operator=()

SceneNode Leviathan::SceneNode::operator= ( const SceneNode other)
delete

◆ PrepareToRender()

DLLEXPORT void SceneNode::PrepareToRender ( )
overrideprotectedvirtual

Reimplemented from Leviathan::SceneAttachable.

Definition at line 187 of file SceneNode.cpp.

188 {
189  if(TransformDirty) {
191  }
192 
193  for(const auto& child : Children) {
194  child->PrepareToRender();
195  }
196 }
void ApplyWorldMatrixIfDirty()
Definition: SceneNode.cpp:159

◆ REFERENCE_COUNTED_PTR_TYPE()

Leviathan::SceneNode::REFERENCE_COUNTED_PTR_TYPE ( SceneNode  )

◆ SetHidden()

void Leviathan::SceneNode::SetHidden ( bool  hidden)
inline

Definition at line 144 of file SceneNode.h.

145  {
146  if(Hidden == hidden)
147  return;
148 
149  Hidden = hidden;
150  Node->setActive(!Hidden);
151  }

◆ SetOrientation()

void Leviathan::SceneNode::SetOrientation ( const Quaternion orientation)
inline

Definition at line 109 of file SceneNode.h.

110  {
111  Node->setRotation(orientation);
112  LocalTransform.Orientation = orientation;
113  MarkDirty();
114  }
Quaternion Orientation
Definition: SceneNode.h:27

◆ SetPosition()

void Leviathan::SceneNode::SetPosition ( const Float3 pos)
inline

Definition at line 97 of file SceneNode.h.

98  {
99  Node->setPosition(pos);
100  LocalTransform.Translation = pos;
101  MarkDirty();
102  }

◆ SetPositionAndOrientation()

void Leviathan::SceneNode::SetPositionAndOrientation ( const Float3 pos,
const Quaternion orientation 
)
inline

Definition at line 127 of file SceneNode.h.

128  {
129  SetPosition(pos);
130  SetOrientation(orientation);
131 
132  LocalTransform.Translation = pos;
133  LocalTransform.Orientation = orientation;
134  MarkDirty();
135  }
void SetOrientation(const Quaternion &orientation)
Definition: SceneNode.h:109
void SetPosition(const Float3 &pos)
Definition: SceneNode.h:97
Quaternion Orientation
Definition: SceneNode.h:27

◆ SetRotation()

FORCE_INLINE void Leviathan::SceneNode::SetRotation ( const Quaternion orientation)
inline

Alias for SetOrientation.

Definition at line 117 of file SceneNode.h.

118  {
119  SetOrientation(orientation);
120  }
void SetOrientation(const Quaternion &orientation)
Definition: SceneNode.h:109

◆ SetScale()

void Leviathan::SceneNode::SetScale ( const Float3 scale)
inline

Definition at line 137 of file SceneNode.h.

138  {
139  Node->setScale(scale);
140  LocalTransform.Scale = scale;
141  MarkDirty();
142  }

The documentation for this class was generated from the following files: