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 "Hardware/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
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 Diff Diff 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]]

268 Diff Diff IonutCava picture IonutCava Wed 04 Jun, 2014 10:20:27 +0000

[IonutCava] [[BR]]
- Terrain and vegetation updates: [[BR]]
— Use standard phong lighting model [[BR]]
— Separate grass patches per chunk [[BR]]
— Reduce number of required texture bind points for terrain by adding both textures and normal maps in a single texture array [[BR]]
— Draw terrain with 2 drawcalls (using glMultiDrawElements twice, once for LoD == 0 and once for LoD > 0) [[BR]]
[[BR]]
- Buffer management updates: [[BR]]
— Unified buffer binding system [[BR]]
— Made Uniform buffer objects visible to all classes and moved lightUBO to LightManager, matricesUBO to GFXDevice etc [[BR]]
— Added initial support for Shader Buffer Objects [[BR]]
— Moved viewport management to GFXDevice class [[BR]]
[[BR]]
- Rendering improvements: [[BR]]
— Added Hi-Z occlusion culling to instanceCull shader (using Hi-Z depth buffer constructed in GFXDevice class) [[BR]]
— Resolved issues with shadow maps overriding texture bind locations [[[BR]]
— Improved shader and shader program loading [[BR]]
— Improved material transparency support [[BR]]
— Added buffer lock manager class from “Approaching Zero Driver Overhead in OpenGL (Presented by NVIDIA)” speech at GDC14 [[BR]]

264 k1ngp1n picture k1ngp1n Tue 27 May, 2014 13:04:38 +0000

[Ionut] [[BR]]
- New camera system: [[BR]]
— Stack based (push/pop active camera) [[BR]]
— Per camera frustum [[BR]]
— Per camera projection [[BR]]
— Shadow mapping now uses the new camera system (each light has a shadow camera) [[BR]]
[[BR]]
- New terrain, water and vegetation system: [[BR]]
— Direct import of terrain from L3DT both as 8-bit grayscale and 16-bit raw formats [[BR]]
— Multiple alpha maps for texture splatting support [[BR]]
— Per texture detail map support [[BR]]
— Transform feedback based visibility culling for vegetation [[BR]]
— Alpha-map based vegetation placing on terrain [[BR]]
— Water now uses a refraction map instead of alpha blending for better refraction effects [[BR]]
[[BR]]
- SceneGraph improvements: [[BR]]
— Better bounding box updates from child to parent [[BR]]
— Better onDraw / postDraw implementation [[BR]]
[[BR]]
- Performance improvements: [[BR]]
— Batch 2D rendering tasks together as much as possible[[BR]]
— Improve renderStateBlock hash generation for better sorting [[BR]]
— Move most state related gfx changes from API implementation to the GFXDevice class [[BR]]
— Move viewport stack to the GFXDevice class [[BR]]
— Added various performance counters (frame duration with round-robin query system, draw call count, etc) [[BR]]
— Fixed and improved clip plane management [[BR]]
— Delete client-side texture data after GPU upload [[BR]]
[[BR]]
- New features: [[BR]]
— Added callback support to the Task class so that a specific task may call a function in the main thread upon completion [[BR]]
— Added mouse-wheel zoom support to Orbit/ThirdPerson cameras [[BR]]
— Added 2D Texture Array support (e.g. used for terrain textures) [[BR]]