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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "Headers/SceneGraph.h"

#include "Scenes/Headers/SceneState.h"
#include "Core/Math/Headers/Transform.h"
#include "Core/Headers/ApplicationTimer.h"
#include "Managers/Headers/SceneManager.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Geometry/Shapes/Headers/Object3D.h"
#include "Geometry/Material/Headers/Material.h"
#include "Geometry/Shapes/Headers/SkinnedSubMesh.h"
#include "Environment/Water/Headers/Water.h"
#include "Environment/Terrain/Headers/Terrain.h"
#include "Platform/Video/Shaders/Headers/ShaderProgram.h"

namespace Divide {

bool SceneRoot::computeBoundingBox(SceneGraphNode* const sgn) {
    BoundingBox& bb = sgn->getBoundingBox();
    bb.reset();
    for (const SceneGraphNode::NodeChildren::value_type& s :
         sgn->getChildren()) {
        sgn->addBoundingBox(s.second->getBoundingBoxConst(),
                            s.second->getNode()->getType());
    }
    bb.setComputed(true);
    return SceneNode::computeBoundingBox(sgn);
}

SceneGraphNode::SceneGraphNode(SceneNode* const node, const stringImpl& name)
    : GUIDWrapper(),
      _node(node),
      _elapsedTime(0ULL),
      _parent(nullptr),
      _loaded(true),
      _wasActive(true),
      _active(true),
      _inView(false),
      _selected(false),
      _isSelectable(false),
      _sorted(false),
      _silentDispose(false),
      _boundingBoxDirty(true),
      _shouldDelete(false),
      _updateTimer(Time::ElapsedMilliseconds()),
      _childQueue(0),
      _bbAddExclusionList(0),
      _usageContext(NODE_DYNAMIC) {
    assert(_node != nullptr);

    setName(name);
    _instanceID = (node->GetRef() - 1);
    Material* const materialTpl = _node->getMaterialTpl();
    _components[SGNComponent::SGN_COMP_ANIMATION] = nullptr;
    _components[SGNComponent::SGN_COMP_NAVIGATION] =
        MemoryManager_NEW NavigationComponent(this);
    _components[SGNComponent::SGN_COMP_PHYSICS] =
        MemoryManager_NEW PhysicsComponent(this);
    _components[SGNComponent::SGN_COMP_RENDERING] =
        MemoryManager_NEW RenderingComponent(
            materialTpl != nullptr ? materialTpl->clone("_instance_" + name)
                                   : nullptr,
            this);
}

/// If we are destroying the current graph node
SceneGraphNode::~SceneGraphNode() {
    unload();

    Console::printfn(Locale::get("DELETE_SCENEGRAPH_NODE"), getName().c_str());
    // delete child nodes recursively
    MemoryManager::DELETE_HASHMAP(_children);

    for (SGNComponent*& component : _components) {
        MemoryManager::DELETE(component);
    }
}

void SceneGraphNode::addBoundingBox(const BoundingBox& bb,
                                    const SceneNodeType& type) {
    if (!bitCompare(_bbAddExclusionList, type)) {
        _boundingBox.Add(bb);
        if (_parent) {
            _parent->getBoundingBox().setComputed(false);
        }
    }
}

void SceneGraphNode::getBBoxes(vectorImpl<BoundingBox>& boxes) const {
    for (const NodeChildren::value_type& it : _children) {
        it.second->getBBoxes(boxes);
    }

    boxes.push_back(_boundingBox);
}

/// When unloading the current graph node
bool SceneGraphNode::unload() {
    if (!_loaded) {
        return true;
    }
    // Unload every sub node recursively
    for (NodeChildren::value_type& it : _children) {
        it.second->unload();
    }
    // Some debug output ...
    if (!_silentDispose && getParent()) {
        Console::printfn(Locale::get("REMOVE_SCENEGRAPH_NODE"),
                         _node->getName().c_str(), getName().c_str());
    }

    // if not root
    if (getParent()) {
        RemoveResource(_node);
    }

    _loaded = false;

    for (DELEGATE_CBK<>& cbk : _deletionCallbacks) {
        cbk();
    }
    return true;
}

/// Change current SceneGraphNode's parent
void SceneGraphNode::setParent(SceneGraphNode* const parent) {
    DIVIDE_ASSERT(parent != nullptr,
                  "SceneGraphNode error: Can't add a new node to a null "
                  "parent. Top level allowed is ROOT");
    assert(parent->getGUID() != getGUID());

    if (_parent) {
        if (*_parent == *parent) {
            return;
        }
        // Remove us from the old parent's children map
        NodeChildren::iterator it = _parent->getChildren().find(getName());
        if (it != std::end(_parent->getChildren())) {
            _parent->getChildren().erase(it);
        }
    }
    // Set the parent pointer to the new parent
    _parent = parent;
    // Add ourselves in the new parent's children map
    // Time to add it to the children map
    hashAlg::pair<hashMapImpl<stringImpl, SceneGraphNode*>::iterator, bool>
        result;
    // Try and add it to the map
    result = hashAlg::insert(_parent->getChildren(),
                             hashAlg::makePair(getName(), this));
    // If we had a collision (same name?)
    if (!result.second) {
        /// delete the old SceneGraphNode and add this one instead
        MemoryManager::SAFE_UPDATE((result.first)->second, this);
    }
    // That's it. Parent Transforms will be updated in the next render pass;
}

SceneGraphNode* SceneGraphNode::addNode(SceneNode* const node,
                                        const stringImpl& name) {
    STUBBED(
        "SceneGraphNode: This add/create node system is an ugly HACK so it "
        "should probably be removed soon! -Ionut")

    if (node->hasSGNParent()) {
        node->AddRef();
    }
    return createNode(node, name);
}

/// Add a new SceneGraphNode to the current node's child list based on a
/// SceneNode
SceneGraphNode* SceneGraphNode::createNode(SceneNode* const node,
                                           const stringImpl& name) {
    // assert(node != nullptr && FindResourceImpl<Resource>(node->getName()) !=
    // nullptr);
    // Create a new SceneGraphNode with the SceneNode's info
    // We need to name the new SceneGraphNode
    // If we did not supply a custom name use the SceneNode's name
    SceneGraphNode* sceneGraphNode = MemoryManager_NEW SceneGraphNode(
        node, name.empty() ? node->getName() : name);
    // Set the current node as the new node's parent
    sceneGraphNode->setParent(this);
    // Do all the post load operations on the SceneNode
    // Pass a reference to the newly created SceneGraphNode in case we need
    // transforms or bounding boxes
    node->postLoad(sceneGraphNode);
    // return the newly created node
    return sceneGraphNode;
}

// Remove a child node from this Node
void SceneGraphNode::removeNode(SceneGraphNode* node) {
    // find the node in the children map
    NodeChildren::iterator it = _children.find(node->getName());
    // If we found the node we are looking for
    if (it != std::end(_children)) {
        // Remove it from the map
        _children.erase(it);
    } else {
        for (U8 i = 0; i < _children.size(); ++i) {
            removeNode(node);
        }
    }
    // Beware. Removing a node, does no unload it!
    // Call delete on the SceneGraphNode's pointer to do that
}

// Finding a node based on the name of the SceneGraphNode or the SceneNode it
// holds
// Switching is done by setting sceneNodeName to false if we pass a
// SceneGraphNode name
// or to true if we search by a SceneNode's name
SceneGraphNode* SceneGraphNode::findNode(const stringImpl& name,
                                         bool sceneNodeName) {
    // Null return value as default
    SceneGraphNode* returnValue = nullptr;
    // Make sure a name exists
    if (!name.empty()) {
        // check if it is the name we are looking for
        if ((sceneNodeName && _node->getName().compare(name) == 0) ||
            getName().compare(name) == 0) {
            // We got the node!
            return this;
        }

        // The current node isn't the one we want, so recursively check all
        // children
        for (const NodeChildren::value_type& it : _children) {
            returnValue = it.second->findNode(name);
            // if it is not nullptr it is the node we are looking for so just
            // pass it through
            if (returnValue != nullptr) {
                return returnValue;
            }
        }
    }

    // no children's name matches or there are no more children
    // so return nullptr, indicating that the node was not found yet
    return nullptr;
}

void SceneGraphNode::intersect(const Ray& ray, F32 start, F32 end,
                               vectorImpl<SceneGraphNode*>& selectionHits) {
    if (isSelectable() && _boundingBox.Intersect(ray, start, end)) {
        selectionHits.push_back(this);
    }

    for (const NodeChildren::value_type& it : _children) {
        it.second->intersect(ray, start, end, selectionHits);
    }
}

void SceneGraphNode::setSelected(const bool state) {
    _selected = state;
    for (NodeChildren::value_type& it : _children) {
        it.second->setSelected(_selected);
    }
}

bool SceneGraphNode::updateBoundingBoxTransform(const mat4<F32>& transform) {
    if (_boundingBox.Transform(
            _initialBoundingBox, transform,
            !_initialBoundingBox.Compare(_initialBoundingBoxCache))) {
        _initialBoundingBoxCache = _initialBoundingBox;
        _boundingSphere.fromBoundingBox(_boundingBox);
        return true;
    }

    return false;
}

void SceneGraphNode::setInitialBoundingBox(
    const BoundingBox& initialBoundingBox) {
    if (!initialBoundingBox.Compare(getInitialBoundingBox())) {
        _initialBoundingBox = initialBoundingBox;
        _initialBoundingBox.setComputed(true);
        _boundingBoxDirty = true;
    }
}

void SceneGraphNode::onCameraChange() {
    for (NodeChildren::value_type& it : _children) {
        it.second->onCameraChange();
    }
    _node->onCameraChange(this);
}

/// Please call in MAIN THREAD! Nothing is thread safe here (for now) -Ionut
void SceneGraphNode::sceneUpdate(const U64 deltaTime, SceneState& sceneState) {
    // Compute from leaf to root to ensure proper calculations
    for (NodeChildren::value_type& it : _children) {
        assert(it.second);
        it.second->sceneUpdate(deltaTime, sceneState);
    }
    // update local time
    _elapsedTime += deltaTime;
    // update all of the internal components (animation, physics, etc)
    for (U8 i = 0; i < SGNComponent::ComponentType_PLACEHOLDER; ++i) {
        if (_components[i]) {
            _components[i]->update(deltaTime);
        }
    }

    if (getComponent<PhysicsComponent>()->transformUpdated()) {
        _boundingBoxDirty = true;
        for (NodeChildren::value_type& it : _children) {
            it.second->getComponent<PhysicsComponent>()->transformUpdated(true);
        }
    }
    assert(_node->getState() == RES_LOADED);
    // Update order is very important! e.g. Mesh BB is composed of SubMesh BB's.

    // Compute the BoundingBox if it isn't already
    if (!_boundingBox.isComputed()) {
        _node->computeBoundingBox(this);
        assert(_boundingBox.isComputed());
        _boundingBoxDirty = true;
    }

    if (_boundingBoxDirty) {
        if (updateBoundingBoxTransform(
                getComponent<PhysicsComponent>()->getWorldMatrix())) {
            if (_parent) {
                _parent->getBoundingBox().setComputed(false);
            }
        }
        _boundingBoxDirty = false;
    }

    getComponent<PhysicsComponent>()->transformUpdated(false);

    _node->sceneUpdate(deltaTime, this, sceneState);

    if (_shouldDelete) {
        GET_ACTIVE_SCENEGRAPH().addToDeletionQueue(this);
    }
}

bool SceneGraphNode::prepareDraw(const SceneRenderState& sceneRenderState,
                                 const RenderStage& renderStage) {
    if (_reset[renderStage]) {
        _reset[renderStage] = false;
        if (getParent() && !GFX_DEVICE.isCurrentRenderStage(DEPTH_STAGE)) {
            for (SceneGraphNode::NodeChildren::value_type& it :
                 getParent()->getChildren()) {
                if (it.second->getComponent<AnimationComponent>()) {
                    it.second->getComponent<AnimationComponent>()
                        ->resetTimers();
                }
            }
        }
    }

    for (U8 i = 0; i < SGNComponent::ComponentType_PLACEHOLDER; ++i) {
        if (_components[i]) {
            if (!_components[i]->onDraw(renderStage)) {
                return false;
            }
        }
    }

    return true;
}

void SceneGraphNode::inView(const bool state) {
    _inView = state;
    if (state && getComponent<RenderingComponent>()) {
        getComponent<RenderingComponent>()->inViewCallback();
    }
}
};

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

Diff revisions: vs.
Revision Author Commited Message
350 Diff Diff IonutCava picture IonutCava Tue 10 Feb, 2015 16:25:39 +0000

[Ionut]
- Simplified some Util-namespace level functions by using STL algorithms where possible
- Formatted the entire codebase using clang-format with the following style: "{BasedOnStyle: Google, IndentWidth: 4}"
- Attempted to make ProfileTimer thread-safe (temp fix until std::chrono works properly in Visual Studio)

348 Diff Diff IonutCava picture IonutCava Thu 05 Feb, 2015 17:13:01 +0000

[Ionut]
- Replaced some raw pointers with smart pointers or direct data members
- Removed redundant SceneGraph pointer from SceneGraphNode

347 Diff Diff IonutCava picture IonutCava Thu 05 Feb, 2015 13:39:09 +0000

[Ionut]
- Changed all begin/end calls on containers to non-member versions
- Replaced all useless “auto” keywords from code. (“auto” only makes sense for return values for delegate functions)
- Replaced useless code in Util::Mat4::Multiply with something that makes sense

343 Diff Diff IonutCava picture IonutCava Thu 15 Jan, 2015 17:30:12 +0000

[Ionut]
- Refactored 2 singletons into static classes (Console and TerrainLoader)
- Changed some C-casts to static_casts
- Updated copyright notices

338 Diff Diff IonutCava picture IonutCava Thu 08 Jan, 2015 14:17:33 +0000

[Ionut]
- Added Metric, Time and Angle namespaces and moved all related functions to the appropriate one.
- Reworked most dynamic_cast calls to static_cast (with static_assert where possible)
- Moved ProfileTimer and ApplicationTimer to Time namespace
- More .h/.cpp/.inl splitting in core files (mostly Math files)

334 Diff Diff IonutCava picture IonutCava Wed 10 Dec, 2014 17:21:22 +0000

[Ionut]
- Reorganized the Hardware project and renamed to "Platform"
- Reorganized the Server project

333 Diff Diff IonutCava picture IonutCava Tue 09 Dec, 2014 16:15:44 +0000

[Ionut]
- Removed “core.h” and moved everything to their appropriate header files
- Renamed MathClasses to MathMatrices (finally)
- Renamed “-Inl.h” files to ".inl"
- Some String wrapper refactoring
- Update “InstallLibraries.bat” so that it still offers valid options

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

329 IonutCava picture IonutCava Tue 07 Oct, 2014 12:51:37 +0000

[Ionut]
- Modified material class and shaders to support multiple BRDFs (currently, only Flat, Phong and Blinn-Phong supported with Cook-Torrance and Oren-Nayar in future commits)
- Optimized transform updates and queries
- Fixed an indirect draw command (no buffer was bound to GL_DRAW_INDIRECT_BUFFER, violating a core requirement)