Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
scene.h
1#ifndef OE_SCENE_MANAGER_H
2#define OE_SCENE_MANAGER_H
3
4#include <atomic>
5#include <vector>
6#include <map>
7
8#include "node.h"
9#include "node_ptr.h"
10#include "camera.h"
11
12namespace oe::scene
13{
17 class Scene
18 {
19 public:
20
27
35 Scene(const Camera& camera);
36
41 {
42 Node node({
43 .scene = this,
44 .id_node = _next_node_id++
45 });
46
47 _nodes.push_back(node);
48
49 return node.scene_data;
50 }
51
56 NodePtr addNamedNode(const std::string& name)
57 {
58 NodePtr result = addNode();
59 result->name = name;
60
61 return result;
62 }
63
71 Node* getNodeById(uint64_t id_node)
72 {
73 // Search into the node vector
74 // Todo ensure the node vector is always sorted
75 for (auto& it : _nodes)
76 {
77 if (it.scene_data.id_node == id_node)
78 {
79 return &it;
80 }
81 }
82
83 return nullptr;
84 }
85
93 const Node* getNodeById(uint64_t id_node) const
94 {
95 // Search into the node vector
96 // Todo ensure the node vector is always sorted
97 for (auto& it : _nodes)
98 {
99 if (it.scene_data.id_node == id_node)
100 {
101 return &it;
102 }
103 }
104
105 return nullptr;
106 }
107
108 /*
109 * @brief Remove a node and all of its children from the scene
110 *
111 * @param node the node to remove
112 */
113 void deleteNode(NodePtr node)
114 {
115 // Check if the node belong to this scene
116 if (node.scene != this) [[unlikely]]
117 {
118 throw std::invalid_argument("Node not part of the current scene!");
119 }
120
121 // Remove all node children
122 for (const auto child : node->children)
123 {
124 deleteNode(child);
125 }
126
128
129 _nodes.erase(std::remove_if(
130 _nodes.begin(),
131 _nodes.end(),
132 [&node](Node& n) { return n.scene_data == node; }
133 ), _nodes.end());
134 }
135
139 const std::vector<NodePtr> getAllNodes() const
140 {
141 std::vector<NodePtr> result;
142
143 result.reserve(_nodes.size());
144
145 for (auto& it: _nodes)
146 {
147 result.push_back(it.scene_data);
148 }
149
150 return result;
151 }
152
156 const std::vector<NodePtr> getRootNodes() const
157 {
158 std::vector<NodePtr> result;
159
160 for (const Node& node : _nodes)
161 {
162 if (!node.parent)
163 {
164 result.push_back(node.scene_data);
165 }
166 }
167
168 return result;
169 }
170
176 NodePtr search(const std::string path, const std::string separator = "/") const
177 {
178 if (path == "")
179 {
180 return {};
181 }
182
183 auto root_nodes = getRootNodes();
184
185 for (auto it : root_nodes)
186 {
187 NodePtr result = it->searchFromThisNode(path, separator);
188
189 if (result != false)
190 {
191 return result;
192 }
193 }
194
195 return {};
196 }
197
203 template <typename ComponentType>
205 {
206 setCamera(component.getCamera());
207 }
208
213 {
214 camera = &default_camera;
215 }
216
223 void addNodeInGroup(NodePtr node, const NodeType type)
224 {
225 if (node.scene != this) [[unlikely]]
226 {
227 throw std::invalid_argument("Node not part of the current scene!");
228 }
229
230 if (_node_types.count(type) == 0)
231 {
232 _node_types[type] = {};
233 }
234
235 _node_types[type].push_back(node);
236 }
237
241 std::vector<NodePtr> getNodesByType(const NodeType type) const
242 {
243 if (_node_types.count(type) == 0)
244 {
245 return {};
246 }
247
248 return _node_types.at(type);
249 }
250
256 void removeNodeFromGroup(NodePtr node, const NodeType type)
257 {
258 if (node.scene != this) [[unlikely]]
259 {
260 throw std::invalid_argument("Node not part of the current scene!");
261 }
262
263 if (_node_types.count(type) == 0)
264 {
265 return;
266 }
267
268 auto& nodes_by_this_type = _node_types[type];
269
270 auto it = std::find(nodes_by_this_type.begin(), nodes_by_this_type.end(), node);
271
272 if (it == nodes_by_this_type.end())
273 {
274 return;
275 }
276
277 nodes_by_this_type.erase(it);
278 }
279
286 {
287 for (auto& it : _node_types)
288 {
289 removeNodeFromGroup(node, it.first);
290 }
291 }
292
293 Camera default_camera;
294
295 /* TODO: remove this pointer and rename default_camera to camera */
296 Camera* camera = nullptr;
297
303 void* user_data = 0;
304
305 private:
306 std::map<NodeType, std::vector<NodePtr>> _node_types;
307 std::vector<Node> _nodes;
308
309 std::atomic<uint64_t> _next_node_id = 1;
310 };
311}
312
313#endif
The "eye of the scene".
Definition camera.h:31
Scene node.
Definition node.h:30
NodePtr searchFromThisNode(const std::string &path, const std::string &separator) noexcept
search starting from this node
std::string name
Name, used as identifier for search.
Definition node.h:256
NodePtr scene_data
Scene pointer to this node.
Definition node.h:239
const std::vector< NodePtr > getRootNodes() const
Get root nodes of the scene.
Definition scene.h:156
void resetCameraToDefault()
Reset scene camera to the default camera.
Definition scene.h:212
NodePtr search(const std::string path, const std::string separator="/") const
Search for a node using a name path.
Definition scene.h:176
void setCameraFromComponent(ComponentType &component)
Set scene camera using a component providing a camera.
Definition scene.h:204
void removeNodeFromAllGroups(NodePtr node)
Remove node from all of its groups.
Definition scene.h:285
NodePtr addNode()
Generate a new root node in the the scene.
Definition scene.h:40
void addNodeInGroup(NodePtr node, const NodeType type)
Add a node in a group (For example nodes that need to be in specific pass).
Definition scene.h:223
const Node * getNodeById(uint64_t id_node) const
Get pointer to a node from its id.
Definition scene.h:93
NodePtr addNamedNode(const std::string &name)
Generate a new root node in the the scene and set a name.
Definition scene.h:56
const std::vector< NodePtr > getAllNodes() const
Get all nodes in the scene.
Definition scene.h:139
Scene(const Camera &camera)
Constructor where Camera settings are filled from a reference one.
Node * getNodeById(uint64_t id_node)
Get pointer to a node from its id.
Definition scene.h:71
Scene()
Default Constructor.
void * user_data
Custom user data, you can use it to store flags / pointer / physics / etc... related to this scene.
Definition scene.h:303
void removeNodeFromGroup(NodePtr node, const NodeType type)
Remove node from specified group.
Definition scene.h:256
std::vector< NodePtr > getNodesByType(const NodeType type) const
Get nodes belonging to a type (For exemple nodes that need to be in specific pass).
Definition scene.h:241
Scene / node components (camera, lighting, ...).
Definition debug.h:17
Scene related management (Render-agnostic Geometry, Manger, etc...).
Definition debug.h:19
Wrapper to a node reference to use pointers to node even if the actual node moves in memory.
Definition node_ptr.h:19
SceneType * scene
Scene containing the Node.
Definition node_ptr.h:59