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
#include "Headers/QuadtreeNode.h"

#include "Managers/Headers/SceneManager.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Environment/Terrain/Headers/Terrain.h"
#include "Environment/Terrain/Headers/TerrainChunk.h"
#include "Core/Math/BoundingVolumes/Headers/BoundingBox.h"

namespace Divide {

QuadtreeNode::QuadtreeNode()
{
    _children[CHILD_NW] = nullptr;
    _children[CHILD_NE] = nullptr;
    _children[CHILD_SW] = nullptr;
    _children[CHILD_SE] = nullptr;
    _terrainChunk = nullptr;
    _LOD = 0;
    _terLoDOffset = 0.0f;
    _minHMSize = 0;
}

QuadtreeNode::~QuadtreeNode()
{
    MemoryManager::DELETE( _children[CHILD_NW] );
    MemoryManager::DELETE( _children[CHILD_NE] );
    MemoryManager::DELETE( _children[CHILD_SW] );
    MemoryManager::DELETE( _children[CHILD_SE] );
    MemoryManager::DELETE( _terrainChunk );
}

void QuadtreeNode::Build(U8 depth, 
                         const vec2<U32>& pos, 
                         const vec2<U32>& HMsize, 
                         U32 minHMSize, 
                         Terrain* const terrain, 
                         U32& chunkCount ) {
    _LOD = 0;
    _minHMSize = minHMSize;
    U32 div = (U32)std::pow(2.0f, (F32)depth);
    vec2<U32> nodesize = HMsize/(div);
    if(nodesize.x%2==0) nodesize.x++;
    if(nodesize.y%2==0) nodesize.y++;
    vec2<U32> newsize = nodesize/2;

    _terLoDOffset = (_minHMSize * 5.0f) / 100.0f;

    if (std::max(newsize.x, newsize.y) < _minHMSize)    {
        _terrainChunk = MemoryManager_NEW TerrainChunk(terrain, this);
        _terrainChunk->Load(depth, pos, _minHMSize, HMsize, terrain);
        chunkCount++;
        return;
    }

    // Create 4 children
    _children[CHILD_NW] = MemoryManager_NEW QuadtreeNode();
    _children[CHILD_NE] = MemoryManager_NEW QuadtreeNode();
    _children[CHILD_SW] = MemoryManager_NEW QuadtreeNode();
    _children[CHILD_SE] = MemoryManager_NEW QuadtreeNode();

    // Compute children bounding boxes
    const vec3<F32>& center = _boundingBox.getCenter();
    _children[CHILD_NW]->setBoundingBox(BoundingBox(_boundingBox.getMin(), center));
    _children[CHILD_NE]->setBoundingBox(BoundingBox(vec3<F32>(center.x, 0.0f, _boundingBox.getMin().z),
                                                    vec3<F32>(_boundingBox.getMax().x, 0.0f, center.z)));
    _children[CHILD_SW]->setBoundingBox(BoundingBox(vec3<F32>(_boundingBox.getMin().x, 0.0f, center.z), 
                                                    vec3<F32>(center.x, 0.0f, _boundingBox.getMax().z)));
    _children[CHILD_SE]->setBoundingBox(BoundingBox(center, _boundingBox.getMax()));
    // Compute children positions
    vec2<U32> tNewHMpos[4];
    tNewHMpos[CHILD_NW] = pos + vec2<U32>(0, 0);
    tNewHMpos[CHILD_NE] = pos + vec2<U32>(newsize.x, 0);
    tNewHMpos[CHILD_SW] = pos + vec2<U32>(0, newsize.y);
    tNewHMpos[CHILD_SE] = pos + vec2<U32>(newsize.x, newsize.y);
    _children[CHILD_NW]->Build( depth + 1, tNewHMpos[CHILD_NW], HMsize, _minHMSize, terrain, chunkCount );
    _children[CHILD_NE]->Build( depth + 1, tNewHMpos[CHILD_NE], HMsize, _minHMSize, terrain, chunkCount );
    _children[CHILD_SW]->Build( depth + 1, tNewHMpos[CHILD_SW], HMsize, _minHMSize, terrain, chunkCount );
    _children[CHILD_SE]->Build( depth + 1, tNewHMpos[CHILD_SE], HMsize, _minHMSize, terrain, chunkCount );
}

bool QuadtreeNode::computeBoundingBox(){
    if(_terrainChunk != nullptr) {
        _boundingBox.setMin(vec3<F32>(_boundingBox.getMin().x, _terrainChunk->getMinHeight(),_boundingBox.getMin().z));
        _boundingBox.setMax(vec3<F32>(_boundingBox.getMax().x, _terrainChunk->getMaxHeight(),_boundingBox.getMax().z));
    }

    if (!isALeaf()) {
        for(I8 i = 0; i < 4; i++) {
            _children[i]->computeBoundingBox();

            if (_boundingBox.getMin().y > _children[i]->_boundingBox.getMin().y){
                _boundingBox.setMin(vec3<F32>(_boundingBox.getMin().x, 
                                              _children[i]->_boundingBox.getMin().y,
                                              _boundingBox.getMin().z));
            }

            if (_boundingBox.getMax().y < _children[i]->_boundingBox.getMax().y){
                _boundingBox.setMax(vec3<F32>(_boundingBox.getMax().x,
                                              _children[i]->_boundingBox.getMax().y,
                                              _boundingBox.getMax().z));
            }
        }
    }

    _boundingBox.setComputed(true);
    _boundingSphere.fromBoundingBox(_boundingBox);
    return true;
}

void QuadtreeNode::sceneUpdate(const U64 deltaTime, SceneGraphNode* const sgn, SceneState& sceneState) {
    
    F32 camDistance = _boundingSphere.getCenter().distance(sceneState.getRenderState().getCameraConst().getEye()) - _terLoDOffset;
    F32 sphereRadius = _boundingSphere.getRadius();
    _LOD = camDistance >= sphereRadius ? (camDistance >= (sphereRadius * 2) ? 2 : 1) : 0;

    if (!isALeaf()) {
        _children[CHILD_NW]->sceneUpdate(deltaTime, sgn, sceneState);
        _children[CHILD_NE]->sceneUpdate(deltaTime, sgn, sceneState);
        _children[CHILD_SW]->sceneUpdate(deltaTime, sgn, sceneState);
        _children[CHILD_SE]->sceneUpdate(deltaTime, sgn, sceneState);
    }
}

bool QuadtreeNode::isInView(U32 options, const SceneRenderState& sceneRenderState) const {
    if(bitCompare(options, CHUNK_BIT_TESTCHILDREN)) {
        const Camera& cam = sceneRenderState.getCameraConst();
        if (!bitCompare(options, CHUNK_BIT_SHADOWMAP)) {
            const vec3<F32>& eye = cam.getEye();
            F32 visibilityDistance = GET_ACTIVE_SCENE()->state().getGeneralVisibility() + _boundingSphere.getRadius();
            if (_boundingSphere.getCenter().distance(eye) > visibilityDistance){
                if (_boundingBox.nearestDistanceFromPointSquared(eye) - _terLoDOffset > 
                    std::min(visibilityDistance, sceneRenderState.getCameraConst().getZPlanes().y))
                    return false;
            }
        }
        if (!_boundingBox.ContainsPoint(cam.getEye()))    {
            const Frustum& frust = cam.getFrustumConst();
            switch (frust.ContainsSphere(_boundingSphere.getCenter(), _boundingSphere.getRadius())) {
                case Frustum::FRUSTUM_OUT:    return false;
                case Frustum::FRUSTUM_IN:    options &= ~CHUNK_BIT_TESTCHILDREN;    break;
                case Frustum::FRUSTUM_INTERSECT:    {
                    switch (frust.ContainsBoundingBox(_boundingBox)) {
                        case Frustum::FRUSTUM_IN: options &= ~CHUNK_BIT_TESTCHILDREN; break;
                        case Frustum::FRUSTUM_OUT: return false;
                    };//inner switch
                };//case FRUSTUM_INTERSECT
            };//outer case
        }//if
    }//CHUNK_BIT_TESTCHILDREN option
    
    return true;
}


void QuadtreeNode::drawBBox() const {
    GFX_DEVICE.drawBox3D(_boundingBox.getMin(), _boundingBox.getMax(), vec4<U8>(0, 128, 255, 255));

    if (!isALeaf()){
        _children[CHILD_NW]->drawBBox();
        _children[CHILD_NE]->drawBBox();
        _children[CHILD_SW]->drawBBox();
        _children[CHILD_SE]->drawBBox();
    }
}

void QuadtreeNode::createDrawCommand(U32 options, 
                                     const SceneRenderState& sceneRenderState,
                                     vectorImpl<GenericDrawCommand>& drawCommandsOut) {
    if (!isInView(options, sceneRenderState)) {
        return;
    }

    if (isALeaf()) {
        assert(_terrainChunk);
        _terrainChunk->createDrawCommand(bitCompare(options, CHUNK_BIT_WATERREFLECTION) ? Config::TERRAIN_CHUNKS_LOD - 1 : 
                                                                                          _LOD,
                                         drawCommandsOut);
    }else{
        _children[CHILD_NW]->createDrawCommand(options, sceneRenderState, drawCommandsOut);
        _children[CHILD_NE]->createDrawCommand(options, sceneRenderState, drawCommandsOut);
        _children[CHILD_SW]->createDrawCommand(options, sceneRenderState, drawCommandsOut);
        _children[CHILD_SE]->createDrawCommand(options, sceneRenderState, drawCommandsOut);
    }
}

};

Commits for Divide-Framework/trunk/Source Code/Environment/Terrain/Quadtree/QuadtreeNode.cpp

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)

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

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

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)

318 Diff Diff IonutCava picture IonutCava Sat 30 Aug, 2014 17:35:53 +0000

[Ionut]
- Wrapped the entire code in a “Divide” namespace
- VertexBuffers now call “shrink_to_fit” on all internal data storage
- Improved some vector performance by preferring “emplace_back” instead of “push_back” + proepr usage of reserve / resize
- Wrapped OIS specific types and classes in Divide::Input namespace
- Added the messageBox.layout file (forgot it in the previous few commits)

304 Diff Diff IonutCava picture IonutCava Sun 10 Aug, 2014 21:10:54 +0000

[Ionut]
- Redesigned the WorldEditor (dummy functions added for every button/option)
- Improved debug rendering performance by using static IMPrimitives for axis gizmo rendering
- Simplified skeleton rendering

299 Diff Diff IonutCava picture IonutCava Mon 04 Aug, 2014 22:09:22 +0000

[Ionut]
- Disabled shader binary dump/load on non-nVidia GPUs (caused issues)
- More code cleanup/comments/formating (GFXDevice.cpp)

278 Diff Diff IonutCava picture IonutCava Thu 19 Jun, 2014 18:09:59 +0000

[Ionut][[BR]]
- Preliminary work on generic shader function selection infrastructure [[BR]]

276 IonutCava picture IonutCava Fri 13 Jun, 2014 18:23:32 +0000

[Ionut][[BR]]
- Terrain now inherits from Object3D (instead of directly from SceneNode) [[BR]]
- Restructured RenderInstance to support multiple draw commands [[BR]]
— Multiple draw commands per instance are separated to different LoD batches automatically on render [[BR]]