Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
SceneGraph.h
Go to the documentation of this file.
1#pragma once
3#include <glm/glm.hpp>
4#include <glm/gtc/quaternion.hpp>
5#include <memory>
6#include <vector>
7
8namespace Ecs {
9 struct LocalTransform;
10}
11
12namespace EngineCore {
13 class Transform;
14}
15
16namespace EngineCore {
24 class SceneNode : public std::enable_shared_from_this<SceneNode> {
25 friend class SceneGraph;
26 friend class NodeFactory;
27 protected:
37 explicit SceneNode(entt::registry& registry, bool disableCaching = false);
38
39 public:
40 ~SceneNode();
41
52
56 void registerWithEcs();
57
66 entt::entity getEntity() const;
67
76 void setWorldPosition(const glm::vec3& newLocation);
77
86 void setWorldRotation(const glm::vec3& newRotation);
87
96 void setWorldRotation(const glm::quat& newRotation);
97
107 void rotateWorld(const glm::vec3& deltaRotation);
108
117 void rotateWorld(const glm::quat& deltaRotation);
118
127 void setWorldScale(const glm::vec3& newScale);
128
136 void setLocalModelMatrix(const glm::mat4& newModelMatrix);
137
145 void setWorldModelMatrix(const glm::mat4& newModelMatrix);
146
155 void addChild(const std::shared_ptr<SceneNode> &newChild);
156
165 void addChildren(std::vector<std::shared_ptr<SceneNode>> newChildren);
166
173 void clearChildren();
174
183 void removeChild(const std::shared_ptr<SceneNode> &child);
184
193 const glm::mat4& getWorldMatrix() const;
194
203 bool isWorldMatrixDirty() const;
204
213 bool isRoot() const;
214
223 std::vector<std::weak_ptr<SceneNode>> getChildren() const;
224
231 void markDirtyRecursive();
232 private:
233
234 void updateWorldMatrix() const;
235
236 std::shared_ptr<SceneNode> parent = nullptr;
237 std::vector<std::shared_ptr<SceneNode>> children;
238 size_t indexInParent = 0;
239
240 entt::entity entity;
241 entt::registry* registry;
242
243 mutable glm::mat4 worldMatrix;
244 bool disableCaching = false;
245 };
246
253 class RootNode : public SceneNode
254 {
255 public:
256 explicit RootNode(entt::registry& registry);
257 };
258
267 private:
268 std::shared_ptr<SceneNode> root;
269 public:
276 explicit SceneGraph(const std::shared_ptr<SceneNode> &root);
285 const std::shared_ptr<SceneNode>& getRoot() const { return root; }
286
299 std::shared_ptr<SceneNode> createNode(const Ecs::LocalTransform& transform, std::shared_ptr<SceneNode> parent,
300 bool disableCaching = false);
301 };
302
311 public:
320 static std::shared_ptr<SceneNode> createNode(const std::shared_ptr<SceneNode>& parent, const bool disableCaching = false);
321
331 static std::vector<std::shared_ptr<SceneNode>> createNodes(
332 const std::shared_ptr<SceneNode>& parent,
333 size_t count,
334 bool disableCaching = false);
335
344 static std::shared_ptr<RootNode> createRoot(bool disableCaching = false) {
345 auto shared = std::make_shared<RootNode>(Ecs::RegistryManager::get());
346 shared->registerWithEcs();
347 return shared;
348 }
349 };
350
351}
352
353
static entt::registry & get()
Gets the registry for all components.
Factory class for creating SceneNodes with proper parent-child relationships. This ensures that nodes...
Definition SceneGraph.h:310
static std::shared_ptr< SceneNode > createNode(const std::shared_ptr< SceneNode > &parent, const bool disableCaching=false)
Creates a new SceneNode with the specified parent.
static std::vector< std::shared_ptr< SceneNode > > createNodes(const std::shared_ptr< SceneNode > &parent, size_t count, bool disableCaching=false)
Creates multiple nodes under the same parent.
static std::shared_ptr< RootNode > createRoot(bool disableCaching=false)
Creates a new root node for a scene graph. This is the only way to create a parentless node,...
Definition SceneGraph.h:344
RootNode(entt::registry &registry)
SceneGraph(const std::shared_ptr< SceneNode > &root)
Constructs a new Scene Graph object, initializing the root node.
std::shared_ptr< SceneNode > createNode(const Ecs::LocalTransform &transform, std::shared_ptr< SceneNode > parent, bool disableCaching=false)
Creates a node which tells you where an object is in the scene.
const std::shared_ptr< SceneNode > & getRoot() const
Gets the root node of the scene graph.
Definition SceneGraph.h:285
std::shared_ptr< SceneNode > root
Definition SceneGraph.h:268
entt::entity entity
Definition SceneGraph.h:240
std::vector< std::shared_ptr< SceneNode > > children
Definition SceneGraph.h:237
friend class SceneGraph
Definition SceneGraph.h:25
void removeChild(const std::shared_ptr< SceneNode > &child)
Removes a single child from this node.
std::vector< std::weak_ptr< SceneNode > > getChildren() const
Get a list of all children of this node.
void setWorldScale(const glm::vec3 &newScale)
sets the scale for this node
void setWorldPosition(const glm::vec3 &newLocation)
Sets the world position of the scene node. This will calculate the corresponding local position based...
bool isWorldMatrixDirty() const
Checks if the world matrix has to be recalculated.
void setWorldRotation(const glm::vec3 &newRotation)
Sets the world rotation of this node with euler angles.
void addChild(const std::shared_ptr< SceneNode > &newChild)
Adds a child node to this node.
void clearChildren()
Removes all children from this node.
bool isRoot() const
Checks if this node is the root node.
const glm::mat4 & getWorldMatrix() const
get the world matrix as a raw glm matrix
void updateWorldMatrix() const
void rotateWorld(const glm::vec3 &deltaRotation)
Applies an incremental rotation to the current world rotation using quaternion multiplication....
void markDirtyRecursive()
Marks this node and its children as dirty.
void setLocalModelMatrix(const glm::mat4 &newModelMatrix)
Overwrites all components of the transform.
SceneNode(entt::registry &registry, bool disableCaching=false)
Constructs a new Scene Node object.
entt::registry * registry
Definition SceneGraph.h:241
Ecs::LocalTransform & getLocalTransform()
Gets the local transform of the scene node. The local transform is relative to the parent node.
std::shared_ptr< SceneNode > parent
Definition SceneGraph.h:236
void setWorldModelMatrix(const glm::mat4 &newModelMatrix)
Overwrites all components of this transform.
void addChildren(std::vector< std::shared_ptr< SceneNode > > newChildren)
adds new children to this node
entt::entity getEntity() const
Gets the entity associated with this scene node.
friend class NodeFactory
Definition SceneGraph.h:26
Data structs for the Entity Component System.
Log category system implementation.