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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#include "Headers/SceneGraph.h"

#include "Core/Headers/PlatformContext.h"
#include "Scenes/Headers/SceneState.h"
#include "Core/Math/Headers/Transform.h"
#include "Core/Time/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 {

SceneGraphNode::SceneGraphNode(SceneGraph& sceneGraph,
                               PhysicsGroup physicsGroup,
                               const SceneNode_ptr& node,
                               const stringImpl& name,
                               U32 componentMask)
    : GUIDWrapper(),
      _sceneGraph(sceneGraph),
      _updateTimer(Time::ElapsedMilliseconds()),
      _elapsedTime(0ULL),
      _node(node),
      _active(true),
      _visibilityLocked(false),
      _isSelectable(false),
      _selectionFlag(SelectionFlag::SELECTION_NONE),
      _usageContext(UsageContext::NODE_DYNAMIC),
      _relationshipCache(*this)
{
    assert(_node != nullptr);
    _children.resize(INITIAL_CHILD_COUNT);
    _childCount = 0;

    for (std::atomic_bool& flag : _updateFlags) {
        flag = false;
    }

    setName(name);
    _components.fill(nullptr);

    if (BitCompare(componentMask, to_uint(SGNComponent::ComponentType::ANIMATION))) {
        setComponent(SGNComponent::ComponentType::ANIMATION, new AnimationComponent(*this));
    }
    if (BitCompare(componentMask, to_uint(SGNComponent::ComponentType::INVERSE_KINEMATICS))) {
        setComponent(SGNComponent::ComponentType::INVERSE_KINEMATICS, new IKComponent(*this));
    }
    if (BitCompare(componentMask, to_uint(SGNComponent::ComponentType::NETWORKING))) {
        LocalClient& client = _sceneGraph.parentScene().platformContext().client();
        setComponent(SGNComponent::ComponentType::NETWORKING, new NetworkingComponent(*this, client));
    }
    if (BitCompare(componentMask, to_uint(SGNComponent::ComponentType::RAGDOLL))) {
        setComponent(SGNComponent::ComponentType::RAGDOLL, new RagdollComponent(*this));
    }
    if (BitCompare(componentMask, to_uint(SGNComponent::ComponentType::NAVIGATION))) {
        setComponent(SGNComponent::ComponentType::NAVIGATION, new NavigationComponent(*this));
    }
    if (BitCompare(componentMask, to_uint(SGNComponent::ComponentType::PHYSICS))) {
        STUBBED("Rigid body physics disabled for now - Ionut");
        physicsGroup = PhysicsGroup::GROUP_IGNORE;
        PXDevice& pxContext = _sceneGraph.parentScene().platformContext().pfx();
        setComponent(SGNComponent::ComponentType::PHYSICS, new PhysicsComponent(*this, physicsGroup, pxContext));

        PhysicsComponent* pComp = get<PhysicsComponent>();
        pComp->addTransformUpdateCbk([this]() { Attorney::SceneGraphSGN::onNodeTransform(_sceneGraph, *this); });
        pComp->addTransformUpdateCbk([this]() { onTransform(); });
    }

    if (BitCompare(componentMask, to_uint(SGNComponent::ComponentType::BOUNDS))) {
        setComponent(SGNComponent::ComponentType::BOUNDS, new BoundsComponent(*this));
    }

    if (BitCompare(componentMask, to_uint(SGNComponent::ComponentType::UNIT))) {
        setComponent(SGNComponent::ComponentType::UNIT, new UnitComponent(*this));
    }
    
    if (BitCompare(componentMask, to_uint(SGNComponent::ComponentType::RENDERING))) {
        GFXDevice& gfxContext = _sceneGraph.parentScene().platformContext().gfx();

        const Material_ptr& materialTpl = _node->getMaterialTpl();
        setComponent(SGNComponent::ComponentType::RENDERING, 
                     new RenderingComponent(gfxContext,
                                            materialTpl ? materialTpl->clone("_instance_" + name)
                                                        : nullptr,
                                            *this));
    }

    Attorney::SceneNodeSceneGraph::registerSGNParent(*_node, getGUID());
}

/// If we are destroying the current graph node
SceneGraphNode::~SceneGraphNode()
{
    if (getParent().lock()) {
        Attorney::SceneGraphSGN::onNodeDestroy(_sceneGraph, *this);
    }
    Console::printfn(Locale::get(_ID("REMOVE_SCENEGRAPH_NODE")),
        getName().c_str(), _node->getName().c_str());

    if (Config::Build::IS_DEBUG_BUILD) {
        for (U32 i = 0; i < getChildCount(); ++i) {
            DIVIDE_ASSERT(_children[i].unique(), "SceneGraphNode::~SceneGraphNode error: child still in use!");
        }
    }

    Attorney::SceneNodeSceneGraph::unregisterSGNParent(*_node, getGUID());

    if (isHelperNode(*_node)) {
        if (Attorney::SceneNodeSceneGraph::parentCount(*_node) == 0) {
            _node.reset();
        }
    }

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

void SceneGraphNode::setComponent(SGNComponent::ComponentType type, SGNComponent* component) {
    // We have a component registered for the specified slot
    if (getComponent(type) != nullptr) {
        if (component != nullptr) {
            // We want to replace the existing entry, so keep the same index
        } else {
            // We want to delete the existing entry, so destroy the index as well
        }
    } else {
        // We are adding a new component type
    }

    MemoryManager::SAFE_UPDATE(_components[getComponentIdx(type)], component);
}

void SceneGraphNode::usageContext(const UsageContext& newContext) {
    Attorney::SceneGraphSGN::onNodeUsageChange(_sceneGraph,
                                               *this,
                                               _usageContext,
                                               newContext);
    _usageContext = newContext;
}

/// Change current SceneGraphNode's parent
void SceneGraphNode::setParent(SceneGraphNode& parent) {
    assert(parent.getGUID() != getGUID());
    SceneGraphNode_ptr parentPtr = _parent.lock();
    if (parentPtr) {
        if (*parentPtr == parent) {
            return;
        }
        // Remove us from the old parent's children map
        parentPtr->removeNode(*this, false);
    }
    // Set the parent pointer to the new parent
    _parent = parent.shared_from_this();
    // Add ourselves in the new parent's children map
    parent.registerNode(shared_from_this());
    invalidateRelationshipCache();
    // That's it. Parent Transforms will be updated in the next render pass;
}

SceneGraphNode_ptr SceneGraphNode::registerNode(SceneGraphNode_ptr node) {
    // Time to add it to the children vector
    SceneGraphNode_ptr child = findChild(node->getName()).lock();
    if (child) {
        removeNode(*child, true);
    }
    
    Attorney::SceneGraphSGN::onNodeAdd(_sceneGraph, *node);
    
    WriteLock w_lock(_childLock);
    if (_childCount == _children.size()) {
        _children.push_back(node);
    } else {
        _children[_childCount] = node;
    }
    _childCount = _childCount + 1;

    return node;
}

/// Add a new SceneGraphNode to the current node's child list based on a SceneNode
SceneGraphNode_ptr SceneGraphNode::addNode(const SceneNode_ptr& node, U32 componentMask, PhysicsGroup physicsGroup, const stringImpl& name) {
    assert(node);
    // 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_ptr sceneGraphNode = 
        std::make_shared<SceneGraphNode>(_sceneGraph, 
                                         physicsGroup,
                                         node,
                                         name.empty() ? node->getName()
                                                      : name,
                                         componentMask);
    // Set the current node as the new node's parent
    sceneGraphNode->setParent(*this);
    if (node->getState() == ResourceState::RES_LOADED) {
        // 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
        Attorney::SceneNodeSceneGraph::postLoad(*node, *sceneGraphNode);
    } else if (node->getState() == ResourceState::RES_LOADING) {
        node->setStateCallback(ResourceState::RES_LOADED,
            [&node, sceneGraphNode]() {
                Attorney::SceneNodeSceneGraph::postLoad(*node, *sceneGraphNode);
            }
        );
    }
    invalidateRelationshipCache();
    // return the newly created node
    return sceneGraphNode;
}

bool SceneGraphNode::removeNode(SceneGraphNode& node, bool recursive) {
    UpgradableReadLock ur_lock(_childLock);
    
    U32 childCount = getChildCount();
    for (U32 i = 0; i < childCount; ++i) {
        if (_children[i]->getGUID() == node.getGUID()) {
            {
                UpgradeToWriteLock w_lock(ur_lock);
                _children[i].reset();
                _childCount = _childCount - 1;
                std::swap(_children[_childCount], _children[i]);
            }
            invalidateRelationshipCache();
            return true;
        }
    }
    

    bool success = false;
    if (recursive) {
        // If this didn't finish, it means that we found our node
        success = !forEachChildInterruptible([&node](SceneGraphNode& child) {
                        if (child.removeNode(node)) {
                            return false;
                        }
                        return true;
                    });
    }

    // Beware. Removing a node, does no delete it!
    // Call delete on the SceneGraphNode's pointer to do that
    return success;
}

void SceneGraphNode::postLoad() {
    for (SGNComponent* component : _components) {
        if (component != nullptr) {
            component->postLoad();
        }
    }
}

bool SceneGraphNode::isChildOfType(U32 typeMask, bool ignoreRoot) const {
    if (ignoreRoot) {
        ClearBit(typeMask, to_const_uint(SceneNodeType::TYPE_ROOT));
    }
    SceneGraphNode_ptr parent = getParent().lock();
    while (parent != nullptr) {
        if (BitCompare(typeMask, to_uint(parent->getNode<>()->getType()))) {
            return true;
        }
        parent = parent->getParent().lock();
    }

    return false;
}

bool SceneGraphNode::isRelated(const SceneGraphNode& target) const {
    I64 targetGUID = target.getGUID();

    SGNRelationshipCache::RelationShipType type = _relationshipCache.clasifyNode(targetGUID);

    // We also ignore grandparents as this will usually be the root;
    return type != SGNRelationshipCache::RelationShipType::COUNT;
}

bool SceneGraphNode::isChild(const SceneGraphNode& target, bool recursive) const {
    I64 targetGUID = target.getGUID();

    SGNRelationshipCache::RelationShipType type = _relationshipCache.clasifyNode(targetGUID);
    if (type == SGNRelationshipCache::RelationShipType::GRANDCHILD && recursive) {
        return true;
    }

    return type == SGNRelationshipCache::RelationShipType::CHILD;
}

SceneGraphNode_wptr SceneGraphNode::findChild(I64 GUID, bool recursive) {
    U32 childCount = getChildCount();
    ReadLock r_lock(_childLock);
    for (U32 i = 0; i < childCount; ++i) {
        SceneGraphNode& child = *_children[i];
        if (child.getGUID() == GUID) {
            return child.shared_from_this();
        } else {
            if (recursive) {
                SceneGraphNode_wptr recChild = child.findChild(GUID, recursive);
                if (recChild.lock()) {
                    return recChild;
                }
            }
        }
    }
    // no children's name matches or there are no more children
    // so return nullptr, indicating that the node was not found yet
    return SceneGraphNode_wptr();
}

SceneGraphNode_wptr SceneGraphNode::findChild(const stringImpl& name, bool sceneNodeName, bool recursive) {
    U32 childCount = getChildCount();
    ReadLock r_lock(_childLock);
    for (U32 i = 0; i < childCount; ++i) {
        SceneGraphNode& child = *_children[i];
        if (sceneNodeName ? child.getNode()->getName().compare(name) == 0
                          : child.getName().compare(name) == 0)
        {
            return child.shared_from_this();
        } else {
            if (recursive) {
                SceneGraphNode_wptr recChild = child.findChild(name, sceneNodeName, recursive);
                if (recChild.lock()) {
                    return recChild;
                }
            }
        }
    }
    // no children's name matches or there are no more children
    // so return nullptr, indicating that the node was not found yet
    return SceneGraphNode_wptr();
}

// 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_wptr SceneGraphNode::findNode(const stringImpl& name,  bool sceneNodeName) {
    // 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 shared_from_this();
        }

        // The current node isn't the one we want, so check children
        SceneGraphNode_wptr child = findChild(name, sceneNodeName);
        if (!child.expired()) {
            return child;
        }

        // The node we want isn't one of the children, so recursively check each of them
        U32 childCount = getChildCount();
        ReadLock r_lock(_childLock);
        for (U32 i = 0; i < childCount; ++i) {
            SceneGraphNode_wptr returnValue = _children[i]->findNode(name, sceneNodeName);
            // if it is not nullptr it is the node we are looking for so just pass it through
            if (!returnValue.expired()) {
                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 SceneGraphNode_wptr();
}

void SceneGraphNode::intersect(const Ray& ray,
                               F32 start,
                               F32 end,
                               vectorImpl<SceneGraphNode_cwptr>& selectionHits,
                               bool recursive) const {

    if (isSelectable()) {
        if (get<BoundsComponent>()->getBoundingBox().intersect(ray, start, end)) {
            selectionHits.push_back(shared_from_this());
        }
    }

    if (recursive) {
        forEachChild([&ray, start, end, &selectionHits](const SceneGraphNode& child) {
            child.intersect(ray, start, end, selectionHits);
        });
    }
}

void SceneGraphNode::setSelectionFlag(SelectionFlag flag) {
    if (_selectionFlag != flag) {
        _selectionFlag = flag;
        forEachChild([flag](SceneGraphNode& child) {
            child.setSelectionFlag(flag);
        });
    }
}

void SceneGraphNode::setSelectable(const bool state) {
    if (_isSelectable != state) {
        _isSelectable = state;
        forEachChild([state](SceneGraphNode& child) {
            child.setSelectable(state);
        });
    }
}

void SceneGraphNode::lockVisibility(const bool state) {
    if (_visibilityLocked != state) {
        _visibilityLocked = state;
        forEachChild([state](SceneGraphNode& child) {
            child.lockVisibility(state);
        });
    }
}

void SceneGraphNode::setActive(const bool state) {
    if (_active != state) {
        _active = state;
        for (SGNComponent* component : _components) {
            if (component != nullptr) {
                component->setActive(state);
            }
        }

        forEachChild([state](SceneGraphNode& child) {
            child.setActive(state);
        });
    }
}

void SceneGraphNode::getOrderedNodeList(vectorImpl<SceneGraphNode*>& nodeList) {
    // Compute from leaf to root to ensure proper calculations
    forEachChild([&nodeList](SceneGraphNode& child) {
        child.getOrderedNodeList(nodeList);
    });

    nodeList.push_back(this);
}

void SceneGraphNode::sgnUpdate(const U64 deltaTime, SceneState& sceneState) {
    Attorney::SceneNodeSceneGraph::sgnUpdate(*_node, deltaTime, *this, sceneState);
}

/// Please call in MAIN THREAD! Nothing is thread safe here (for now) -Ionut
void SceneGraphNode::sceneUpdate(const U64 deltaTime, SceneState& sceneState) {
    ResourceState nodeState = _node->getState();
    assert(nodeState == ResourceState::RES_LOADED || nodeState == ResourceState::RES_LOADING);

    // Node is not fully loaded. Skip.
    if (nodeState == ResourceState::RES_LOADING) {
        return;
    }

    // update local time
    _elapsedTime += deltaTime;

    // update all of the internal components (animation, physics, etc)
    for (SGNComponent* component : _components) {
        if (component != nullptr) {
            component->update(deltaTime);
         }
    }

    if (!_relationshipCache.isValid()) {
        _relationshipCache.rebuild();
    }

    Attorney::SceneNodeSceneGraph::sceneUpdate(*_node, deltaTime, *this, sceneState);
}

void SceneGraphNode::onTransform() {
    forEachChild([](SceneGraphNode& child) {
        child.onTransform();
    });

    PhysicsComponent* pComp = get<PhysicsComponent>();
    BoundsComponent* bComp = get<BoundsComponent>();
    if (pComp != nullptr && bComp != nullptr) {
        bComp->onTransform(pComp->getWorldMatrix());
    }
}

bool SceneGraphNode::prepareDraw(const SceneRenderState& sceneRenderState,
                                 RenderStage renderStage) {
    for (SGNComponent* component : _components) {
        if (component && !component->onRender(renderStage)) {
            return false;
        }
    }

    return true;
}

void SceneGraphNode::frameEnded() {
    forEachChild([](SceneGraphNode& child) {
        child.frameEnded();
    });
}

void SceneGraphNode::onCameraUpdate(const I64 cameraGUID,
                                    const vec3<F32>& posOffset,
                                    const mat4<F32>& rotationOffset) {

    forEachChild([cameraGUID, &posOffset, &rotationOffset](SceneGraphNode& child) {
        child.onCameraUpdate(cameraGUID, posOffset, rotationOffset);
    });

    PhysicsComponent* pComp = get<PhysicsComponent>();
    if (pComp && pComp->ignoreView(cameraGUID)) {
        pComp->setViewOffset(posOffset, rotationOffset);
    }
    
    Attorney::SceneNodeSceneGraph::onCameraUpdate(*this, *_node, cameraGUID, posOffset, rotationOffset);
}

void SceneGraphNode::onCameraChange(const Camera& cam) {
    forEachChild([&cam](SceneGraphNode& child) {
        child.onCameraChange(cam);
    });

    Attorney::SceneNodeSceneGraph::onCameraChange(*this, *_node, cam);
}

void SceneGraphNode::onNetworkSend(U32 frameCount) {
    forEachChild([frameCount](SceneGraphNode& child) {
        child.onNetworkSend(frameCount);
    });

    NetworkingComponent* net = get<NetworkingComponent>();
    if (net) {
        net->onNetworkSend(frameCount);
    }
}

bool SceneGraphNode::filterCollission(const SceneGraphNode& node) {
    // filter by mask, type, etc
    return true;
}

void SceneGraphNode::onCollision(SceneGraphNode_cwptr collider) {
    //handle collision
    if (_collisionCbk) {
        SceneGraphNode_cptr node = collider.lock();
        if (node && filterCollission(*node)) {
            assert(getGUID() != node->getGUID());
            _collisionCbk(node);
        }
    }
}

bool SceneGraphNode::cullNode(const Camera& currentCamera,
                              F32 maxDistanceFromCamera,
                              RenderStage currentStage,
                              Frustum::FrustCollision& collisionTypeOut) const {

    collisionTypeOut = Frustum::FrustCollision::FRUSTUM_IN;
    if (visibilityLocked()) {
        return false;
    }

    bool distanceCheck = currentStage != RenderStage::SHADOW;
    const BoundingBox& boundingBox = get<BoundsComponent>()->getBoundingBox();
    const BoundingSphere& sphere = get<BoundsComponent>()->getBoundingSphere();

    F32 radius = sphere.getRadius();
    const vec3<F32>& eye = currentCamera.getEye();
    const vec3<F32>& center = sphere.getCenter();
    F32 visibilityDistanceSq = std::pow(maxDistanceFromCamera + radius, 2);

    if (distanceCheck && center.distanceSquared(eye) > visibilityDistanceSq) {
        if (boundingBox.nearestDistanceFromPointSquared(eye) >
            std::min(visibilityDistanceSq, std::pow(currentCamera.getZPlanes().y, 2))) {
            return true;
        }
    }

    if (!boundingBox.containsPoint(eye)) {
        const Frustum& frust = currentCamera.getFrustum();
        collisionTypeOut = frust.ContainsSphere(center, radius);
        if (collisionTypeOut == Frustum::FrustCollision::FRUSTUM_INTERSECT) {
            collisionTypeOut = frust.ContainsBoundingBox(boundingBox);
        }
    }

    return collisionTypeOut == Frustum::FrustCollision::FRUSTUM_OUT;
}

void SceneGraphNode::invalidateRelationshipCache() {
    _relationshipCache.invalidate();

    SceneGraphNode_ptr parentPtr = _parent.lock();
    if (parentPtr && parentPtr->getParent().lock()) {
        parentPtr->invalidateRelationshipCache();

        forEachChild([](SceneGraphNode& child) {
            child.invalidateRelationshipCache();
        });
    }
}

void SceneGraphNode::forEachChild(const DELEGATE_CBK<void, SceneGraphNode&>& callback) {
    ReadLock r_lock(_childLock);
    //Using childCount instead of a range-based for loops means that we can skip child null check
    //as childCount should always be updated to reflect the actual number of node children
    U32 childCount = _childCount;
    for (U32 i = 0; i < childCount; ++i) {
        callback(*_children[i]);
    }
}

void SceneGraphNode::forEachChild(const DELEGATE_CBK<void, const SceneGraphNode&>& callback) const {
    ReadLock r_lock(_childLock);
    U32 childCount = _childCount;
    for (U32 i = 0; i < childCount; ++i) {
        callback(*_children[i]);
    }
}

bool SceneGraphNode::forEachChildInterruptible(const DELEGATE_CBK<bool, SceneGraphNode&>& callback) {
    ReadLock r_lock(_childLock);
    U32 childCount = _childCount;
    for (U32 i = 0; i < childCount; ++i) {
        if (!callback(*_children[i])) {
            return false;
        }
    }

    return true;
}

bool SceneGraphNode::forEachChildInterruptible(const DELEGATE_CBK<bool, const SceneGraphNode&>& callback) const {
    ReadLock r_lock(_childLock);
    U32 childCount = _childCount;
    for (U32 i = 0; i < childCount; ++i) {
        if (!callback(*_children[i])) {
            return false;
        }
    }

    return true;
}

void SceneGraphNode::forEachChild(const DELEGATE_CBK<void, SceneGraphNode&, I32>& callback, U32 start, U32 end) {
    ReadLock r_lock(_childLock);
    U32 childCount = _childCount;
    CLAMP<U32>(end, 0, childCount);
    assert(start < end);

    for (U32 i = start; i < end; ++i) {
        callback(*_children[i], i);
    }
}

void SceneGraphNode::forEachChild(const DELEGATE_CBK<void, const SceneGraphNode&, I32>& callback, U32 start, U32 end) const {
    ReadLock r_lock(_childLock);
    U32 childCount = _childCount;
    CLAMP<U32>(end, 0, childCount);
    assert(start < end);

    for (U32 i = start; i < end; ++i) {
        callback(*_children[i], i);
    }
}

bool SceneGraphNode::forEachChildInterruptible(const DELEGATE_CBK<bool, SceneGraphNode&, I32>& callback, U32 start, U32 end) {
    ReadLock r_lock(_childLock);
    U32 childCount = _childCount;
    CLAMP<U32>(end, 0, childCount);
    assert(start < end);

    for (U32 i = start; i < end; ++i) {
        if (!callback(*_children[i], i)) {
            return false;
        }
    }

    return true;
}

bool SceneGraphNode::forEachChildInterruptible(const DELEGATE_CBK<bool, const SceneGraphNode&, I32>& callback, U32 start, U32 end) const {
    ReadLock r_lock(_childLock);
    U32 childCount = _childCount;
    CLAMP<U32>(end, 0, childCount);
    assert(start < end);

    for (U32 i = start; i < end; ++i) {
        if (!callback(*_children[i], i)) {
            return false;
        }
    }

    return true;
}
};

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

Diff revisions: vs.
Revision Author Commited Message
842 Diff Diff IonutCava picture IonutCava Wed 01 Feb, 2017 17:25:15 +0000

[IonutCava]
- Start to implement scripting support via ChaiScript: http://chaiscript.com/
- Cleanup DELEGATE_CBK alias

839 Diff Diff IonutCava picture IonutCava Tue 31 Jan, 2017 17:28:13 +0000

[IonutCava]
- Add simplefilewatcher libary: https://github.com/jameswynn/simplefilewatcher
- Use simplefilewatcher to automatically recompile shaders after save without restarting the app!
— Shaders can now be edited on the fly with automatic rebuild if they used one of the modified atoms.

838 Diff Diff IonutCava picture IonutCava Mon 30 Jan, 2017 17:28:35 +0000

[IonutCava]
- Added a NetworkingComponent to SGNs
- Started cleaning up Client / Server architecture

821 Diff Diff IonutCava picture IonutCava Tue 17 Jan, 2017 17:15:45 +0000

[IonutCava]
- Profile guided optimizations

817 Diff Diff IonutCava picture IonutCava Sun 15 Jan, 2017 21:29:59 +0000

[IonutCava]
- Add a new Unit component to SGNs that holds NPC/Player/etc. objects
- Add support for multiple keyboard+mouse combos and joysticks and add mapping between these and players

804 Diff Diff IonutCava picture IonutCava Thu 01 Dec, 2016 17:20:59 +0000

[IonutCava]
- Singleton elimination update Part I: get it to compile
— The following classes are no longer Singletons: GFXDevice, GL_API, DX_API, SFXWrapper, FmodWrapper, SDLWrapper, ALWrapper, PXDevice, InputInterface, RenderPassManager, SceneManager and ResourceManager;
— Refactor system to a Context / Component based implementation (Pass relevant context to objects: e.g. GFXDevice object to Textures, GUI to GUIElements, etc)
— Make devices and managers components of the kernel
— Allow multiple Resource caches to co-exist. This may prove useful for later when a more fragmented memory model is need (per frame / per scene / global caches / etc)

  • next steps: part II – cleanup/refactor new code, part III – optimise code, part IV – remove remaining Singletons (e.g. Application, ParamHandler, FrameListenerManager, Recast, PostFX and DebugInterface)
800 Diff Diff IonutCava picture IonutCava Fri 25 Nov, 2016 17:04:02 +0000

[IonutCava]
- More profile-guided optimizations
- Added GLSL normal map blending methods from: http://blog.selfshadow.com/sandbox/normals.html
- Added more profile timers

795 Diff Diff IonutCava picture IonutCava Wed 23 Nov, 2016 17:14:33 +0000

[IonutCava]
- Remove ULL. Use U64 instead
- Various small bug fixes and typos.

794 Diff Diff IonutCava picture IonutCava Tue 22 Nov, 2016 16:44:26 +0000

[IonutCava]
- Change SDL_GL_Context management from a pool-based system to a thread_local variable type System
- Reworked SceneGraphNode child access for safer and faster iteration.
— Added a forEachChild with multiple overloads to apply a lambda expression over every child of the target SGN
- Slight optimization to Text rendering by batching all labels due for rendering and submitting them at all at once

784 IonutCava picture IonutCava Mon 17 Oct, 2016 16:20:49 +0000

[IonutCava]
- Removed the CameraManager and move most of its functionality into static members of the Camera class
- Removed Camera push/pop system in favour of a stateless ptr based system to better fit the RenderPass structure