Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
   Copyright (c) 2014 DIVIDE-Studio
   Copyright (c) 2009 Ionut Cava

   This file is part of DIVIDE Framework.

   Permission is hereby granted, free of charge, to any person obtaining a copy of this software
   and associated documentation files (the "Software"), to deal in the Software without restriction,
   including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

   */

#ifndef _SCENE_GRAPH_NODE_H_
#define _SCENE_GRAPH_NODE_H_

#define SCENE_GRAPH_PROCESS(pointer) DELEGATE_BIND(&SceneGraph::process(),pointer)

#include "SceneNode.h"
#include "Utility/Headers/StateTracker.h"
#include "Graphs/Components/Headers/SGNComponent.h"
#include "Graphs/Components/Headers/PhysicsComponent.h"
#include "Graphs/Components/Headers/AnimationComponent.h"
#include "Graphs/Components/Headers/NavigationComponent.h"
#include "Graphs/Components/Headers/RenderingComponent.h"

namespace Divide {
    class Transform;
    class SceneGraph;
    class SceneState;
    // This is the scene root node. All scene node's are added to it as child nodes
    class SceneRoot : public SceneNode {
    public:
        SceneRoot() : SceneNode("root", TYPE_ROOT)
        {
            _renderState.useDefaultMaterial(false);
            setState(RES_LOADED);
        }

        bool onDraw(SceneGraphNode* const sgn, const RenderStage& currentStage) { return true; }
        bool unload()                                      { return true; }
        bool load(const stringImpl& name)                 { return true; }
        bool computeBoundingBox(SceneGraphNode* const sgn);

    protected:
        void render(SceneGraphNode* const sgn, const SceneRenderState& sceneRenderState, const RenderStage& currentRenderStage) {}
        void postLoad(SceneGraphNode* const sgn)  { SceneNode::postLoad(sgn); }
        void getDrawCommands(SceneGraphNode* const sgn, 
                             const RenderStage& renderStage, 
                             SceneRenderState& sceneRenderState, 
                             vectorImpl<GenericDrawCommand>& drawCommandsOut) {}
    };

    // Add as many SceneTransform nodes are needed as parent nodes for any scenenode to create complex transforms in the scene
    class SceneTransform : public SceneNode {
    public:
        SceneTransform() : SceneNode(TYPE_TRANSFORM)
        {
            _renderState.useDefaultMaterial(false);
            setState(RES_LOADED);
        }

        void render(SceneGraphNode* const sgn, const SceneRenderState& sceneRenderState, const RenderStage& currentRenderStage) {}
        bool onDraw(const RenderStage& currentStage) { return true; }

        void postLoad(SceneGraphNode* const sgn)           { return; }
        bool unload()                                      { return true; }
        bool load(const stringImpl& name)                 { return true; }
        bool computeBoundingBox(SceneGraphNode* const sgn) { return true; }
    };

    class IMPrimitive;
    class SceneGraphNode : public GUIDWrapper, private NonCopyable {
    public:
        typedef hashMapImpl<stringImpl, SceneGraphNode*> NodeChildren;

        ///Usage context affects lighting, navigation, physics, etc
        enum UsageContext {
            NODE_DYNAMIC = 0,
            NODE_STATIC
        };

        bool unload();
        /// Update bounding boxes
        void checkBoundingBoxes();
        /// Apply current transform to the node's BB. Return true if the bounding extents changed
        bool updateBoundingBoxTransform(const mat4<F32>& transform);
        /// Called from SceneGraph "sceneUpdate"
        void sceneUpdate(const U64 deltaTime, SceneState& sceneState);
        /// Called when the camera updates the view matrix and/or the projection matrix
        void onCameraChange();
        /*Node Management*/
        ///Always use the level of redirection needed to reduce virtual function overhead
        ///Use getNode<SceneNode> if you need material properties for ex. or getNode<SubMesh> for animation transforms
        template<typename T = SceneNode>
        inline T* getNode() const { assert(_node != nullptr); return dynamic_cast<T*>(_node); }
        ///Create node never increments the node's ref counter (used for scene loading)
        SceneGraphNode* createNode(SceneNode* const node, const stringImpl& name = "");
        ///Add node increments the node's ref counter if the node was already added to the scene graph
        SceneGraphNode* addNode(SceneNode* const node, const stringImpl& name = "");
        void            removeNode(SceneGraphNode* node);
        inline void     deleteNode(SceneGraphNode*& node) {
            removeNode(node);
            MemoryManager::DELETE(node);
        }
        ///Find a node in the graph based on the SceneGraphNode's name
        ///If sceneNodeName = true, find a node in the graph based on the SceneNode's name
        SceneGraphNode* findNode(const stringImpl& name, bool sceneNodeName = false);
        ///Find the graph nodes whom's bounding boxes intersects the given ray
        void Intersect(const Ray& ray, F32 start, F32 end, vectorImpl<SceneGraphNode* >& selectionHits);

        ///Selection helper functions
        void setSelected(const bool state);
        inline bool isSelected()                    const { return _selected; }
        inline bool isSelectable()                  const { return _isSelectable; }
        inline void setSelectable(const bool state)       { _isSelectable = state; }

        const  stringImpl& getName() const { return _name; }
        /*Node Management*/

        /*Parent <-> Children*/
        SceneGraphNode*  getRoot()        const;
        inline      SceneGraphNode*  getParent()      const { return _parent; }
        inline      NodeChildren&    getChildren()          { return _children; }
        void             setParent(SceneGraphNode* const parent);

        /// Alias for changing parent
        inline void attachToNode(SceneGraphNode* const target) {
            setParent(target);
        }

        inline void attachToRoot() {
            setParent(getRoot());
        }
        /*Parent <-> Children*/

        /*Bounding Box Management*/
        void setInitialBoundingBox(const BoundingBox& initialBoundingBox);

        inline       BoundingBox&     getBoundingBox()                { return _boundingBox; }
        inline const BoundingBox&     getBoundingBoxConst()    const  { return _boundingBox; }
        inline       BoundingSphere&  getBoundingSphere()             { return _boundingSphere; }
        inline const BoundingSphere&  getBoundingSphereConst() const  { return _boundingSphere; }
        inline const BoundingBox&     getInitialBoundingBox()  const  { return _initialBoundingBox; }

        void getBBoxes(vectorImpl<BoundingBox >& boxes) const;
        /*Bounding Box Management*/

        inline void silentDispose(const bool state)       { _silentDispose = state; }
        inline void useDefaultTransform(const bool state) { getComponent<PhysicsComponent>()->useDefaultTransform(!state); }

        /*Node State*/
        inline void setActive(const bool state) { _wasActive = _active; _active = state; }
        inline void restoreActive()       { _active = _wasActive; }
        inline void    scheduleDeletion()    { _shouldDelete = true; }

        inline bool inView()   const { return _inView; }
        inline bool isActive() const { return _active; }

        inline U32  getInstanceID() const { return _instanceID; }
        inline U32  getChildQueue() const { return _childQueue; }
        inline void incChildQueue()       { _childQueue++; }
        inline void decChildQueue()       { _childQueue--; }

        inline const UsageContext& usageContext()                 const { return _usageContext; }
        inline void  usageContext(const UsageContext& newContext)       { _usageContext = newContext; }

        void addBoundingBox(const BoundingBox& bb, const SceneNodeType& type);
        void setBBExclusionMask(U32 bbExclusionMask) { _bbAddExclusionList = bbExclusionMask; }

        inline U64 getElapsedTime() const { return _elapsedTime; }

        inline void setComponent(SGNComponent::ComponentType type, SGNComponent* component) {
            MemoryManager::SAFE_UPDATE(_components[type], component);
        }

        template<typename T>
        inline T* getComponent() const { assert(false && "INVALID COMPONENT"); return nullptr; }
        template<>
        inline AnimationComponent*  getComponent() const { 
            return static_cast<AnimationComponent*>(_components[SGNComponent::SGN_COMP_ANIMATION]); 
        }
        template<>
        inline NavigationComponent* getComponent() const { 
            return static_cast<NavigationComponent*>(_components[SGNComponent::SGN_COMP_NAVIGATION]); 
        }
        template<>
        inline PhysicsComponent*    getComponent() const { 
            return static_cast<PhysicsComponent*>(_components[SGNComponent::SGN_COMP_PHYSICS]); 
        }
        template<>
        inline RenderingComponent*  getComponent() const { 
            return static_cast<RenderingComponent*>(_components[SGNComponent::SGN_COMP_RENDERING]); 
        }

        inline StateTracker<bool>& getTrackedBools() { return _trackedBools; }

        inline void registerDeletionCallback(const DELEGATE_CBK<>& cbk) {
            _deletionCallbacks.push_back(cbk);
        }

        bool operator==(const SceneGraphNode& other) const {
            return this->getGUID() == other.getGUID();
        }

        bool operator!=(const SceneGraphNode& other) const {
            return this->getGUID() != other.getGUID();
        }

    protected:
        SET_DELETE_FRIEND
        SET_SAFE_UPDATE_FRIEND
        SET_DELETE_VECTOR_FRIEND
        SET_DELETE_HASHMAP_FRIEND

        friend class SceneGraph;
        friend vectorImpl<SceneGraphNode>;
        explicit SceneGraphNode(SceneGraph* const sg, SceneNode* const node, const stringImpl& name);
        ~SceneGraphNode();

    protected:
        friend class RenderPassCuller;
        void inView(const bool state);

    protected:
        friend class RenderingComponent;
        bool prepareDraw(const SceneRenderState& sceneRenderState, const RenderStage& currentRenderStage);

    private:
        inline void setName(const stringImpl& name){ _name = name; }
        inline void scheduleReset(RenderStage currentStage) { _reset[currentStage] = true; }

     private:
        SceneNode* _node;
        NodeChildren _children;
        SceneGraphNode *_parent;
        SceneGraph     *_sceneGraph;
        std::atomic<bool> _active;
        std::atomic<bool> _loaded;
        std::atomic<bool> _inView;
        std::atomic<bool> _boundingBoxDirty;
        U32 _bbAddExclusionList;
        bool _selected;
        bool _isSelectable;
        bool _wasActive;
        bool _sorted;
        bool _silentDispose;
        bool _shouldDelete;
        ///_initialBoundingBox is a copy of the initialy calculate BB for transformation
        ///it should be copied in every computeBoungingBox call;
        BoundingBox _initialBoundingBox, _initialBoundingBoxCache;
        BoundingBox _boundingBox;
        BoundingSphere _boundingSphere; ///<For faster visibility culling

        U32 _instanceID;
        U32 _childQueue;
        D32 _updateTimer;
        U64 _elapsedTime;//< the total amount of time that passed since this node was created
        stringImpl _name;

        UsageContext _usageContext;
        SGNComponent* _components[SGNComponent::ComponentType_PLACEHOLDER];
        vectorImpl<DELEGATE_CBK<> > _deletionCallbacks;
        hashMapImpl<RenderStage, bool, hashAlg::hash<I32>> _reset;

        StateTracker<bool> _trackedBools;

    };
}; //namespace Divide
#endif

Commits for Divide-Framework/trunk/Source Code/Graphs/Headers/SceneGraphNode.h

Diff revisions: vs.
Revision Author Commited Message
337 Diff Diff IonutCava picture IonutCava Wed 17 Dec, 2014 17:25:16 +0000

[Ionut]
- Replaced all tabs with 4 spaces (some spacing may be off now. no way to check all the files.)
- Moved implementation details for ParamHandler, BoundingBox, BoundingSphere and ProfileTimer to proper .inl and .cpp files. (more to follow)
- Update all forward declared enums with the required storage type (as requested by the C++11 standard)

331 Diff Diff IonutCava picture IonutCava Sat 06 Dec, 2014 20:53:45 +0000

[Ionut]
- Limited line length to 132 characters to improve readability and diff-comparisons
- Refactored memory allocation/deallocation functions
- Fixed a few compatibility issues with HashMap.h
- Fixed a bug in GPU Skinning shaders (cast a float to int)

330 Diff Diff IonutCava picture IonutCava Fri 10 Oct, 2014 17:19:11 +0000

[Ionut]
- New rendering algorithm :
— Perform second pass for visible nodes gathering all render commands and uploading them to an indirect draw buffer
— Render geometry with indirect draw commands
— Removed per-node range binding of the node buffer. Instead, bind the entire buffer once and index data in shaders
— Use “baseInstance” parameter from IndirectDrawCommand as a node buffer index
— Prefer DSA extensions for updating buffer data where applicable
- Moved all rendering specific code from SceneGraphNode to a new RenderingComponent
- Optimized “toString” utility function

328 Diff Diff IonutCava picture IonutCava Sat 04 Oct, 2014 18:03:36 +0000

[Ionut]
- Added per SceneGraphNode material instances. (Cloned from the SceneNode’s material)
- Improved shader loading for materials

325 Diff Diff IonutCava picture IonutCava Wed 17 Sep, 2014 20:06:13 +0000

[Ionut]
- Reworked transform system:
— Parent transform chain system moved from Transform class to the PhysicsComponent
— PhysicsComponent now returns all transform values needed both global and local (still needs heavy optimization and caching)

- Reworked SceneGraph node management:
— Proper ref counting of SceneNodes and proper removing of resource (ResourceCache is now empty on destruction, as it should be)
— Removed parent transform tracking as that’s the PhysicsComponent’s responsibility
— Only nodes loaded via the ResourceCache are allowed to be added to the SceneGraph (added proper loaders for Impostors, Sky, etc)

- Optimized some of the math classes (mat4, mat3)

324 Diff Diff IonutCava picture IonutCava Sun 14 Sep, 2014 14:01:39 +0000

[Ionut]
- New memory management system (no custom allocators in this commit)
— Memory tracer to detect memory leaks on shutdown (from: https://www.relisoft.com/book/tech/9new.html)
— SAFE_DELETE_XYZ are now inline template functions not macros
— ASSERTION system now supports logging and message boxes and continue on error configurable for each build type
— Fixed a lot of memory leaks detected by the new system (some still remain)

- Added SRGB support (part 1)
— Albedo textures are loaded as SRGB (make sure resources are SRGB)
— Shaders output gamma corrected colors
— Moved terrain textures back to 2 array system (albedo and normal maps, albedo maps using a SRGB sampler)

- Sky is now loaded via the ResourceCache system
- Added include safeguards in shader atoms

322 Diff Diff IonutCava picture IonutCava Sat 06 Sep, 2014 20:33:47 +0000

[Ionut]
- Refactored most of Boost related code to be C++11 based
— Boost is only used for ThreadPool, Mutex handling, XML parsing and networking (ASIO)
— Function binds, threads, regex, NonCopyable base, Hashing algorithms and more are now using C++11 libraries
- Replaced all FOR_EACH calls with standard, range, “for” calls

321 Diff Diff IonutCava picture IonutCava Wed 03 Sep, 2014 22:05:15 +0000

[Ionut]
- Added support (and enabled by default) for the EASTL library (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html / https://github.com/paulhodge/EASTL)
— Strings, vectors and hash_maps can be combined from EASTL, STL and Boost (not all combinations work. e.g. EASTL strings with STL/Boost containers due to the lack of a proper hash function)

320 Diff Diff IonutCava picture IonutCava Sun 31 Aug, 2014 21:45:33 +0000

[Ionut]
- More AI work (entity captures flag and returns it to base)

319 IonutCava picture IonutCava Sat 30 Aug, 2014 21:37:57 +0000

[Ionut]
- Performance tweaks based on profiling data