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 Node* rawresult = _internalSearch(path, separator);
103
104 return dynamic_cast<T*>(rawresult);
105 }
106
107 inline bool isVisible() const
108 {
109 return _is_visible;
110 }
111
112 void setVisible(const bool& is_visible = true)
113 {
114 _is_visible = is_visible;
115 }
116
117 bool canRender(const scene::Camera& camera) const;
118
124 const AABB& getBoundingBox() const;
125
131
138 std::vector<Node*> getAllParents() const;
139
144
148 glm::vec3 getAbsolutePosition() const;
149
153 const glm::mat4& getAbsoluteModelMatrix() const;
154
162 const glm::mat4 getAbsoluteInverseModelMatrix() const;
163
170 glm::quat getRotationNeededToLookAt(const glm::vec3& target, const glm::vec3& up = glm::vec3(0.f, 1.f, 0.f)) const;
171
178 void lookAt(const glm::vec3& target, const glm::vec3& up = glm::vec3(0.f, 1.f, 0.f));
179
190 virtual void update(const double& delta, const int8_t flags = 0);
191
197 void generateModel(const bool recursive = true) const;
198
200 std::string name = "";
201
204
205 protected:
206 mutable AABB _aabb;
207
208 private:
209
210 bool _is_visible = true;
211
212 Manager* _manager = nullptr;
213 Node* _parent_node = nullptr;
214
215 std::vector<oe::scene::Node*> _children;
216
217 void _remove_child(Node&) noexcept;
218
219 mutable glm::mat4 _model_matrix;
220
221 mutable glm::mat4 _absolute_model_matrix;
222
223 Node* _internalSearch(const std::string& path, const std::string& separator) noexcept;
224
225 Node* _internalSearch(
226 const std::vector<std::string>::iterator& begin,
227 const std::vector<std::string>::iterator& end
228 ) noexcept;
229
233 bool _has_dirty_parents() const;
234
235 Node* _fetch_dirty_parent() const;
236
237 // Only used to set '_manager', todo use the 'Passkey' design pattern
238 friend class Manager;
239 };
240}
241
242#endif
Util class to add components handling to an entity.
Definition component.h:311
Axis-aligned bounding box.
Definition aabb.h:13
Scene manager.
Definition manager.h:21
Definition node.h:31
virtual void update(const double &delta, const int8_t flags=0)
Update the node.
std::string name
Name, used as identifier for search.
Definition node.h:200
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.
AABB raw_aabb
Raw bounding box (ie. without taking node transform)
Definition node.h:203
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