Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
SceneGraph.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_SCENE_GRAPH_H_
34#define DVD_SCENE_GRAPH_H_
35
36#include "SceneNode.h"
37#include "IntersectionRecord.h"
40
41namespace ECS {
42 class ECSEngine;
43};
44
45namespace Divide {
46class SceneState;
47class SceneGraphNode;
48
49struct Ray;
50struct SGNRayResult;
51
52namespace Attorney
53{
54 class SceneGraphSGN;
55};
56
57struct SceneGraphNodeDescriptor;
58
60
61class SceneGraph final : NonCopyable,
62 public FrameListener,
63 public SceneComponent
64{
65
67
68 public:
69 explicit SceneGraph(Scene& parentScene);
70 ~SceneGraph() override;
71
72 void load();
73 void unload();
74
75 const SceneGraphNode* getRoot() const noexcept { return _root; }
76 SceneGraphNode* getRoot() noexcept { return _root; }
77
78 SceneGraphNode* findNode(const Str<128>& name, bool sceneNodeName = false) const;
79 SceneGraphNode* findNode(U64 nameHash, bool sceneNodeName = false) const;
80 SceneGraphNode* findNode(I64 guid) const;
81
83 void sceneUpdate(U64 deltaTimeUS, SceneState& sceneState);
84
85 bool intersect(const SGNIntersectionParams& params, vector<SGNRayResult>& intersectionsOut) const;
86
88
89 void destroySceneGraphNode(SceneGraphNode*& node, bool inPlace = true);
90 void addToDeleteQueue(SceneGraphNode* node, size_t childIdx);
91
92 // If this function returns true, the node was successfully removed (or queued for removal)
93 bool removeNode(I64 guid);
94 bool removeNode(SceneGraphNode* node);
95 // If this function returns true, nodes of the specified type were successfully removed (or queued for removal)
96 bool removeNodesByType(SceneNodeType nodeType);
97
99
100 void getNodesByType(std::initializer_list<SceneNodeType> types, vector<SceneGraphNode*>& nodesOut) const
101 {
102 efficient_clear( nodesOut );
103 for (const SceneNodeType type : types)
104 {
105 const vector<SceneGraphNode*>& nodes = getNodesByType(type);
106 nodesOut.insert(cend(nodesOut), cbegin(nodes), cend(nodes));
107 }
108 }
109
110 size_t getTotalNodeCount() const noexcept;
111
112 void onNetworkSend(U32 frameCount);
113
114 void postLoad();
115
116 bool saveNodeToXML(const SceneGraphNode* node) const;
117 bool loadNodeFromXML( const ResourcePath& assetsFile, SceneGraphNode* node) const;
118
119 void saveToXML( const ResourcePath& assetsFile, DELEGATE<void, std::string_view> msgCallback ) const;
120 void loadFromXML( const ResourcePath& assetsFile);
121
123 const ECSManager& GetECSManager() const { return *_ecsManager; }
124
129
130 bool saveCache(ByteBuffer& outputBuffer) const;
131 bool loadCache(ByteBuffer& inputBuffer);
132
133 ECS::ECSEngine& GetECSEngine() noexcept { return _ecsEngine; }
134 const ECS::ECSEngine& GetECSEngine() const noexcept { return _ecsEngine; }
135
136
139 [[nodiscard]] static BoundingSphere GetBounds( const SceneGraphNode* sgn );
140
141 protected:
142 void onNodeMoved(const SceneGraphNode& node);
143 void onNodeDestroy(SceneGraphNode* oldNode);
144 void onNodeAdd(SceneGraphNode* newNode);
145 void onNodeUpdated(const SceneGraphNode& node);
146 void onNodeSpatialChange(const SceneGraphNode& node);
147
148 bool frameStarted(const FrameEvent& evt) override;
149 bool frameEnded(const FrameEvent& evt) override;
150
152
153 static void HandleIntersection( const IntersectionRecord& intersection );
154
155 private:
157 ECSManager_uptr _ecsManager;
158 bool _nodeListChanged = false;
159
164 std::array<vector<SceneGraphNode*>, to_base(SceneNodeType::COUNT)> _nodesByType;
165
170
172 eastl::fixed_vector<SceneGraphNode*, 1024, true> _nodeEventQueue;
173
175 eastl::fixed_vector<SceneGraphNode*, 256, true> _nodeParentChangeQueue;
176};
177
179
180namespace Attorney {
182 static void onNodeAdd(Divide::SceneGraph* sceneGraph, SceneGraphNode* newNode) {
183 sceneGraph->onNodeAdd(newNode);
184 }
185
186 static void onNodeDestroy(Divide::SceneGraph* sceneGraph, SceneGraphNode* oldNode) {
187 sceneGraph->onNodeDestroy(oldNode);
188 }
189
190 static void onNodeMoved(Divide::SceneGraph* sceneGraph, const SceneGraphNode& node) {
191 sceneGraph->onNodeMoved(node);
192 }
193
194 static void onNodeShaderReady(Divide::SceneGraph* sceneGraph, const SceneGraphNode& node) {
195 sceneGraph->onNodeUpdated(node);
196 }
197
198 static void onNodeSpatialChange(Divide::SceneGraph* sceneGraph, const SceneGraphNode& node) {
199 sceneGraph->onNodeSpatialChange(node);
200 }
201
202 static void onNodeEvent(Divide::SceneGraph* sceneGraph, SceneGraphNode* node) {
203 LockGuard<Mutex> w_lock(sceneGraph->_nodeEventLock);
204 insert_unique(sceneGraph->_nodeEventQueue, node);
205 }
206
207 static void onNodeParentChange(Divide::SceneGraph* sceneGraph, SceneGraphNode* node) {
208 LockGuard<Mutex> w_lock(sceneGraph->_nodeParentChangeLock);
209 insert_unique(sceneGraph->_nodeParentChangeQueue, node);
210 }
211
213};
214
215}; // namespace Attorney
216
217}; // namespace Divide
218
219#endif //DVD_SCENE_GRAPH_H_
#define FWD_DECLARE_MANAGED_CLASS(T)
static void onNodeParentChange(Divide::SceneGraph *sceneGraph, SceneGraphNode *node)
Definition: SceneGraph.h:207
static void onNodeAdd(Divide::SceneGraph *sceneGraph, SceneGraphNode *newNode)
Definition: SceneGraph.h:182
static void onNodeDestroy(Divide::SceneGraph *sceneGraph, SceneGraphNode *oldNode)
Definition: SceneGraph.h:186
static void onNodeMoved(Divide::SceneGraph *sceneGraph, const SceneGraphNode &node)
Definition: SceneGraph.h:190
static void onNodeEvent(Divide::SceneGraph *sceneGraph, SceneGraphNode *node)
Definition: SceneGraph.h:202
static void onNodeShaderReady(Divide::SceneGraph *sceneGraph, const SceneGraphNode &node)
Definition: SceneGraph.h:194
static void onNodeSpatialChange(Divide::SceneGraph *sceneGraph, const SceneGraphNode &node)
Definition: SceneGraph.h:198
Scene & parentScene() noexcept
IntersectionContainer _intersectionsCache
Definition: SceneGraph.h:163
bool loadNodeFromXML(const ResourcePath &assetsFile, SceneGraphNode *node) const
Definition: SceneGraph.cpp:756
void addToDeleteQueue(SceneGraphNode *node, size_t childIdx)
Definition: SceneGraph.cpp:102
SceneGraphNode * createSceneGraphNode(PlatformContext &context, SceneGraph *sceneGraph, const SceneGraphNodeDescriptor &descriptor)
Definition: SceneGraph.cpp:70
SceneGraphNode * _root
Definition: SceneGraph.h:160
SceneGraphNode * getRoot() noexcept
Definition: SceneGraph.h:76
eastl::fixed_vector< SceneGraphNode *, 1024, true > _nodeEventQueue
Definition: SceneGraph.h:172
ECS::ECSEngine _ecsEngine
Definition: SceneGraph.h:156
~SceneGraph() override
Definition: SceneGraph.cpp:65
void onNodeMoved(const SceneGraphNode &node)
Definition: SceneGraph.cpp:145
ECS::EntityManager * GetEntityManager()
Definition: SceneGraph.cpp:483
SharedMutex _pendingDeletionLock
Definition: SceneGraph.h:168
size_t getTotalNodeCount() const noexcept
Definition: SceneGraph.cpp:464
const ECS::ECSEngine & GetECSEngine() const noexcept
Definition: SceneGraph.h:134
void getNodesByType(std::initializer_list< SceneNodeType > types, vector< SceneGraphNode * > &nodesOut) const
Definition: SceneGraph.h:100
static BoundingSphere GetBounds(const SceneGraphNode *sgn)
Definition: SceneGraph.cpp:36
vector< SceneGraphNode * > _nodeList
Definition: SceneGraph.h:161
hashMap< SceneGraphNode *, vector< size_t > > _pendingDeletion
Definition: SceneGraph.h:169
void saveToXML(const ResourcePath &assetsFile, DELEGATE< void, std::string_view > msgCallback) const
Definition: SceneGraph.cpp:647
const ECSManager & GetECSManager() const
Definition: SceneGraph.h:123
void loadFromXML(const ResourcePath &assetsFile)
Definition: SceneGraph.cpp:686
static void HandleIntersection(const IntersectionRecord &intersection)
Definition: SceneGraph.cpp:365
bool frameEnded(const FrameEvent &evt) override
frameEnded is called after the buffers have been swapped
Definition: SceneGraph.cpp:248
bool saveCache(ByteBuffer &outputBuffer) const
Definition: SceneGraph.cpp:530
Mutex _intersectionsLock
Definition: SceneGraph.h:162
bool loadCache(ByteBuffer &inputBuffer)
Definition: SceneGraph.cpp:572
const SceneGraphNode * getRoot() const noexcept
Definition: SceneGraph.h:75
void onNodeAdd(SceneGraphNode *newNode)
Definition: SceneGraph.cpp:190
void onNodeSpatialChange(const SceneGraphNode &node)
Definition: SceneGraph.cpp:128
bool frameStarted(const FrameEvent &evt) override
Definition: SceneGraph.cpp:228
ECS::ECSEngine & GetECSEngine() noexcept
Definition: SceneGraph.h:133
bool removeNode(I64 guid)
Definition: SceneGraph.cpp:204
SharedMutex _nodesByTypeLock
Definition: SceneGraph.h:167
Mutex _nodeParentChangeLock
Definition: SceneGraph.h:174
bool saveNodeToXML(const SceneGraphNode *node) const
Definition: SceneGraph.cpp:750
void destroySceneGraphNode(SceneGraphNode *&node, bool inPlace=true)
Definition: SceneGraph.cpp:448
void sceneUpdate(U64 deltaTimeUS, SceneState &sceneState)
Update all nodes. Called from "updateSceneState" from class Scene.
Definition: SceneGraph.cpp:282
void checkCollisions(BoundsComponent *bComp)
const vector< SceneGraphNode * > & getNodesByType(SceneNodeType type) const
Definition: SceneGraph.cpp:477
ECS::ComponentManager * GetComponentManager()
Definition: SceneGraph.cpp:493
SceneGraphNode * findNode(const Str< 128 > &name, bool sceneNodeName=false) const
Definition: SceneGraph.cpp:503
void onNodeDestroy(SceneGraphNode *oldNode)
Definition: SceneGraph.cpp:151
void onNodeUpdated(const SceneGraphNode &node)
Definition: SceneGraph.cpp:112
ECSManager & GetECSManager()
Definition: SceneGraph.h:122
ECSManager_uptr _ecsManager
Definition: SceneGraph.h:157
eastl::fixed_vector< SceneGraphNode *, 256, true > _nodeParentChangeQueue
Definition: SceneGraph.h:175
void onNetworkSend(U32 frameCount)
Definition: SceneGraph.cpp:389
std::array< vector< SceneGraphNode * >, to_base(SceneNodeType::COUNT)> _nodesByType
Definition: SceneGraph.h:164
bool removeNodesByType(SceneNodeType nodeType)
Definition: SceneGraph.cpp:199
bool intersect(const SGNIntersectionParams &params, vector< SGNRayResult > &intersectionsOut) const
Definition: SceneGraph.cpp:394
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
DELEGATE_STD< Ret, Args... > DELEGATE
std::lock_guard< mutex > LockGuard
Definition: SharedMutex.h:55
eastl::fixed_vector< IntersectionRecord, 32u, true > IntersectionContainer
std::mutex Mutex
Definition: SharedMutex.h:40
SceneNodeType
ToDo: Move particle emitter to components (it will make them way more dynamic) - Ionut.
Definition: SceneNodeFwd.h:47
std::shared_mutex SharedMutex
Definition: SharedMutex.h:43
eastl::vector< Type > vector
Definition: Vector.h:42
hashAlg::unordered_map< K, V, HashFun, Predicate > hashMap
Definition: HashMap.h:55
void efficient_clear(eastl::fixed_vector< T, nodeCount, bEnableOverflow, OverflowAllocator > &fixed_vector)
Definition: Vector.h:52
void insert_unique(eastl::vector< T, A > &target, const T &item)
Definition: Vector.h:77
int64_t I64
uint32_t U32
uint64_t U64
constexpr auto to_base(const Type value) -> Type