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 Engine::Ecs
9{
10 struct Transform;
11} // namespace Engine::Ecs
12
13namespace Engine::Entities {
18 class SceneNode : public std::enable_shared_from_this<SceneNode> {
19 friend class SceneGraph;
20 friend class NodeFactory;
21 protected:
28 explicit SceneNode(entt::registry& registry, bool disableCaching = false);
29
30 public:
32
40
45
51 entt::entity getEntity() const;
52
58 void setWorldPosition(const glm::vec3& newLocation);
59
65 void setWorldRotation(const glm::vec3& newRotation);
66
72 void setWorldRotation(const glm::quat& newRotation);
73
80 void rotateWorld(const glm::vec3& deltaRotation);
81
87 void rotateWorld(const glm::quat& deltaRotation);
88
94 void setWorldScale(const glm::vec3& newScale);
95
100 void setLocalModelMatrix(const glm::mat4& newModelMatrix);
101
106 void setWorldModelMatrix(const glm::mat4& newModelMatrix);
107
113 void addChild(const std::shared_ptr<SceneNode> &newChild);
114
120 void addChildren(std::vector<std::shared_ptr<SceneNode>> newChildren);
121
126
132 void removeChild(const std::shared_ptr<SceneNode> &child);
133
138
144 const glm::mat4& getWorldMatrix() const;
145
151 bool isWorldMatrixDirty() const;
152
158 bool isRoot() const;
159
165 std::vector<std::weak_ptr<SceneNode>> getChildren() const;
166
170 void markDirtyRecursive() const;
171 private:
172
173 void updateWorldMatrix() const;
174
175 std::weak_ptr<SceneNode> parent_;
176 std::vector<std::shared_ptr<SceneNode>> children_;
177 size_t indexInParent_ = 0;
178
179 entt::entity entity_;
180 entt::registry* registry;
181
182 mutable glm::mat4 worldMatrix;
183 bool disableCaching = false;
184 };
185
189 class RootNode : public SceneNode
190 {
191 public:
192 explicit RootNode(entt::registry& registry);
193 };
194
200 std::shared_ptr<SceneNode> root_;
201 public:
205 explicit SceneGraph(const std::shared_ptr<SceneNode> &root);
210 [[nodiscard]] const std::shared_ptr<SceneNode>& getRoot() const { return root_; }
211
215 void reserveRootChildren(size_t additionalChildren);
216
226 std::shared_ptr<SceneNode> createNode(const Ecs::Transform& transform,
227 const std::shared_ptr<SceneNode> & parent,
228 bool disableCaching = false);
229 };
230
236 public:
245 static std::shared_ptr<SceneNode> createNode(const std::shared_ptr<SceneNode>& parent, bool disableCaching = false);
246
256 static std::vector<std::shared_ptr<SceneNode>> createNodes(
257 const std::shared_ptr<SceneNode>& parent,
258 size_t count,
259 bool disableCaching = false);
260
269 static std::shared_ptr<RootNode> createRoot(bool disableCaching = false) {
270 auto shared = std::make_shared<RootNode>(Ecs::RegistryManager::get());
271 shared->registerWithEcs();
272 return shared;
273 }
274 };
275
276}
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:235
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:269
static std::shared_ptr< SceneNode > createNode(const std::shared_ptr< SceneNode > &parent, bool disableCaching=false)
Creates a new SceneNode with the specified parent.
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 > root_
Definition SceneGraph.h:200
void reserveRootChildren(size_t additionalChildren)
Reserves space for additional root children to avoid reallocations during bulk spawning.
std::shared_ptr< SceneNode > createNode(const Ecs::Transform &transform, const 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:210
bool isRoot() const
Checks if this node is the root node.
void setWorldModelMatrix(const glm::mat4 &newModelMatrix)
Overwrites all components of this transform.
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...
void rotateWorld(const glm::quat &deltaRotation)
Applies an incremental rotation to the current world rotation using quaternion multiplication.
const glm::mat4 & getWorldMatrix() const
get the world matrix as a raw glm matrix
void setLocalModelMatrix(const glm::mat4 &newModelMatrix)
Overwrites all components of the transform.
void addChildren(std::vector< std::shared_ptr< SceneNode > > newChildren)
adds new children to this node
void clearChildren()
Removes all children from this node.
void detachFromParent()
Detaches this node from its parent, transferring ownership away from the scene graph.
entt::registry * registry
Definition SceneGraph.h:180
SceneNode(entt::registry &registry, bool disableCaching=false)
Constructs a new Scene Node object.
entt::entity getEntity() const
Gets the entity associated with this scene node.
const Ecs::Transform & getLocalTransform() const
std::vector< std::shared_ptr< SceneNode > > children_
Definition SceneGraph.h:176
void markDirtyRecursive() const
Marks this node and its children as dirty.
void setWorldRotation(const glm::quat &newRotation)
Sets a new world rotation with a quaternion on this node.
void setWorldRotation(const glm::vec3 &newRotation)
Sets the world rotation of this node with euler angles.
Ecs::Transform & getLocalTransform()
Gets the local transform of the scene node. The local transform is relative to the parent node.
bool isWorldMatrixDirty() const
Checks if the world matrix has to be recalculated.
void rotateWorld(const glm::vec3 &deltaRotation)
Applies an incremental rotation to the current world rotation using quaternion multiplication....
void removeChild(const std::shared_ptr< SceneNode > &child)
Removes a single child from this node.
void registerWithEcs()
Registers this scene node and its transform data in the ECS registry.
void addChild(const std::shared_ptr< SceneNode > &newChild)
Adds a child node to this node.
std::weak_ptr< SceneNode > parent_
Definition SceneGraph.h:175
std::vector< std::weak_ptr< SceneNode > > getChildren() const
Get a list of all children of this node.