Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
node.h
1#ifndef OE_SCENE_NODE_H
2#define OE_SCENE_NODE_H
3
4#include "../common.h"
5
6#include <optional>
7#include <functional>
8#include <string>
9#include <list>
10#include <memory>
11#include <vector>
12
13#include "../util/non_copyable.h"
14#include "component/node.h"
15#include "material.h"
16#include "aabb.h"
17#include "transform.h"
18
19namespace oe::scene
20{
21 class Camera;
22 class Manager;
23
24 using NodeType = uint64_t;
25 const NodeType NodeTypeALL = 0;
26
30 class Node : public Transform, public util::NonCopyable, public oe::component::HasComponents<Node, NodeComponent>
31 {
32 public:
33 explicit Node();
34 virtual ~Node() {}
35
36 inline const Node* getParentNode() const noexcept
37 {
38 return _parent_node;
39 }
40
41 inline Node* getParentNode() noexcept
42 {
43 return _parent_node;
44 }
45
46 inline Manager* getSceneManager()
47 {
48 return _manager;
49 }
50
51 void addChild(Node& child);
52
53 inline const std::vector<oe::scene::Node*>& getChildren() noexcept
54 {
55 return _children;
56 }
57
58 inline const std::vector<oe::scene::Node*>& getChildren() const noexcept
59 {
60 return _children;
61 }
62
68 void setParent(Node& parent) noexcept;
69
75 void moveToRoot() noexcept;
76
80 void foreachChildren(void (*func)(Node&, void*), const bool recursion = false, void* user_data = nullptr);
81
86 void foreachChildren(std::function<void(Node&)> func, const bool recursion = false);
87
91 const std::string getAbsoluteName(const std::string& separator = "/") const noexcept;
92
99 template <typename T = Node>
100 T* search(const std::string path, const std::string separator = "/")
101 {
102 if (path == "")
103 return nullptr;
104
105 Node* rawresult = _internal_search(name + separator + path, separator);
106
107 return dynamic_cast<T*>(rawresult);
108 }
109
110 inline bool isVisible() const
111 {
112 return _is_visible;
113 }
114
115 void setVisible(const bool& is_visible = true)
116 {
117 _is_visible = is_visible;
118 }
119
120 bool canRender(const scene::Camera& camera) const;
121
127 const AABB& getBoundingBox() const;
128
134
141 std::vector<Node*> getAllParents() const;
142
147
151 glm::vec3 getAbsolutePosition() const;
152
156 const glm::mat4& getAbsoluteModelMatrix() const;
157
165 const glm::mat4 getAbsoluteInverseModelMatrix() const;
166
173 glm::quat getRotationNeededToLookAt(const glm::vec3& target, const glm::vec3& up = glm::vec3(0.f, 1.f, 0.f)) const;
174
181 void lookAt(const glm::vec3& target, const glm::vec3& up = glm::vec3(0.f, 1.f, 0.f));
182
193 virtual void update(const std::chrono::nanoseconds delta, const int8_t flags = 0);
194
200 void generateModel(const bool recursive = true) const;
201
203 std::string name = "";
204
207
208 protected:
209 mutable AABB _aabb;
210
211 private:
212 bool _is_visible = true;
213
214 Manager* _manager = nullptr;
215 Node* _parent_node = nullptr;
216
217 std::vector<oe::scene::Node*> _children;
218
219 void _remove_child(Node&) noexcept;
220
221 mutable glm::mat4 _model_matrix;
222
223 mutable glm::mat4 _absolute_model_matrix;
224
225 Node* _internal_search(const std::string& path, const std::string& separator) noexcept;
226
227 Node* _internal_search(
228 const std::vector<std::string>::iterator& begin,
229 const std::vector<std::string>::iterator& end
230 ) noexcept;
231
235 bool _has_dirty_parents() const;
236
237 Node* _fetch_dirty_parent() const;
238
239 // Only used to set '_manager', todo use the 'Passkey' design pattern
240 friend class Manager;
241 };
242}
243
244#endif
Util class to add components handling to an entity.
Definition component.h:312
Axis-aligned bounding box.
Definition aabb.h:13
Scene manager.
Definition manager.h:21
Definition node.h:31
std::string name
Name, used as identifier for search.
Definition node.h:203
void setParent(Node &parent) noexcept
Change the parent of this node.
const AABB getEnglobingBoundingBox() const
Compute englobing bounding box including all children ones.
T * search(const std::string path, const std::string separator="/")
Search for a node among children using a name path.
Definition node.h:100
void moveToRoot() noexcept
Detach the node from parent to make a root node.
void foreachChildren(void(*func)(Node &, void *), const bool recursion=false, void *user_data=nullptr)
Do actions on children.
Transform getAbsoluteTransform() const
Get Node transform relative from world origin.
void lookAt(const glm::vec3 &target, const glm::vec3 &up=glm::vec3(0.f, 1.f, 0.f))
Orient the node to stare at a specified target.
const glm::mat4 & getAbsoluteModelMatrix() const
Get absolute model matrix (by taking the parents into account)
void generateModel(const bool recursive=true) const
Force regeneration of Models Matrices.
glm::quat getRotationNeededToLookAt(const glm::vec3 &target, const glm::vec3 &up=glm::vec3(0.f, 1.f, 0.f)) const
Get the rotation needed to orient the node at a specified target.
glm::vec3 getAbsolutePosition() const
Get Node position from world origin.
virtual void update(const std::chrono::nanoseconds delta, const int8_t flags=0)
Update the node.
AABB raw_aabb
Raw bounding box (ie. without taking node transform)
Definition node.h:206
const AABB & getBoundingBox() const
Recompute and return bounding box from raw AABB.
const glm::mat4 getAbsoluteInverseModelMatrix() const
Get absolute inverse model matrix (by taking the parents into account)
const std::string getAbsoluteName(const std::string &separator="/") const noexcept
Get absolute name (from the hierarchy)
std::vector< Node * > getAllParents() const
Get all node parents hierarchy of the node.
Manage local Translation / Rotation / Scale of an entity in the world.
Definition transform.h:20
Prevent class to be copied.
Definition non_copyable.h:12
Scene related management (Render-agnostic Geometry, Manger, etc...)
Definition debug.h:19