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
#include "Headers/Terrain.h"
#include "Headers/TerrainChunk.h"
#include "Headers/TerrainDescriptor.h"

#include "Quadtree/Headers/QuadtreeNode.h"

#include "Core/Headers/PlatformContext.h"
#include "Core/Math/Headers/Transform.h"
#include "Graphs/Headers/SceneGraphNode.h"
#include "Managers/Headers/SceneManager.h"
#include "Geometry/Shapes/Headers/Predefined/Patch3D.h"

#include "Geometry/Material/Headers/Material.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Video/Buffers/VertexBuffer/Headers/VertexBuffer.h"
#include "Platform/Video/Buffers/ShaderBuffer/Headers/ShaderBuffer.h"

namespace Divide {

namespace {
    size_t g_PlaneCommandIndex = 0;
};

namespace Test {
    bool treeDirty = true;

    typedef struct terrainNode_t terrainNode_t;
    struct terrainNode_t {
        vec3<F32> origin;
        vec2<F32> dimensions;
        U8 type; // 1, 2, 3, 4 -- the child # relative to its parent. (0 == root)

        // Tessellation scale
        F32 tscale_negx; // negative x edge
        F32 tscale_posx; // Positive x edge
        F32 tscale_negz; // Negative z edge
        F32 tscale_posz; // Positive z edge

        terrainNode_t *p;  // Parent
        terrainNode_t *c1; // Children
        terrainNode_t *c2;
        terrainNode_t *c3;
        terrainNode_t *c4;

        terrainNode_t *n; // Neighbor to north
        terrainNode_t *s; // Neighbor to south
        terrainNode_t *e; // Neighbor to east
        terrainNode_t *w; // Neighbor to west
    };

    // The size of a patch in meters at which point to
    // stop subdividing a terrain patch once it's width is
    // less than the cutoff
    #define VMB_TERRAIN_REC_CUTOFF 10

    terrainNode_t *terrainTree;
    terrainNode_t *terrainTreeTail;
    I32 numTerrainNodes = 0;

    /**
    * Allocates a new node in the terrain quadtree with the specified parameters.
    */
    terrainNode_t* createNode(terrainNode_t *parent, U8 type, F32 x, F32 y, F32 z, F32 width, F32 height)
    {
        if (numTerrainNodes >= Terrain::MAX_RENDER_NODES)
            return NULL;
        numTerrainNodes++;

        terrainTreeTail++;
        terrainTreeTail->type = type;
        terrainTreeTail->origin.set(x, y, z);
        terrainTreeTail->dimensions.set(width, height);
        terrainTreeTail->tscale_negx = 1.0;
        terrainTreeTail->tscale_negz = 1.0;
        terrainTreeTail->tscale_posx = 1.0;
        terrainTreeTail->tscale_posz = 1.0;
        terrainTreeTail->p = parent;
        terrainTreeTail->n = NULL;
        terrainTreeTail->s = NULL;
        terrainTreeTail->e = NULL;
        terrainTreeTail->w = NULL;
        return terrainTreeTail;
    }

    /**
    * Resets the terrain quadtree.
    */
    void terrain_clearTree()
    {
        terrainTreeTail = terrainTree;
        memset(terrainTree, 0, Terrain::MAX_RENDER_NODES * sizeof(terrainNode_t));
        numTerrainNodes = 0;
    }

    /**
    * Determines whether a node should be subdivided based on its distance to the camera.
    * Returns true if the node should be subdivided.
    */
    bool checkDivide(const vec3<F32>& camPos, terrainNode_t *node)
    {
        // Distance from current origin to camera
        F32 d = std::abs(std::sqrtf(std::pow(camPos.x - node->origin[0], 2.0f) + std::pow(camPos.z - node->origin[2], 2.0f)));

        // Check base case:
        // Distance to camera is greater than twice the length of the diagonal
        // from current origin to corner of current square.
        // OR
        // Max recursion level has been hit
        if (d > 2.5f * std::sqrtf(std::pow(0.5f * node->dimensions.width, 2.0f) + 
                                  std::pow(0.5f * node->dimensions.height, 2.0f)) ||
            node->dimensions.width < VMB_TERRAIN_REC_CUTOFF)
        {
            return false;
        }

        return true;
    }

    /**
    * Returns true if node is sub-divided. False otherwise.
    */
    bool terrain_divideNode(const vec3<F32>& camPos, terrainNode_t *node)
    {
        // Subdivide
        F32 w_new = 0.5f * node->dimensions.width;
        F32 h_new = 0.5f * node->dimensions.height;

        // Create the child nodes
        node->c1 = createNode(node, 1u, node->origin.x - 0.5f * w_new, node->origin.y, node->origin.z - 0.5f * h_new, w_new, h_new);
        node->c2 = createNode(node, 2u, node->origin.x + 0.5f * w_new, node->origin.y, node->origin.z - 0.5f * h_new, w_new, h_new);
        node->c3 = createNode(node, 3u, node->origin.x + 0.5f * w_new, node->origin.y, node->origin.z + 0.5f * h_new, w_new, h_new);
        node->c4 = createNode(node, 4u, node->origin.x - 0.5f * w_new, node->origin.y, node->origin.z + 0.5f * h_new, w_new, h_new);

        // Assign neighbors
        if (node->type == 1)
        {
            node->e = node->p->c2;
            node->n = node->p->c4;
        }
        else if (node->type == 2)
        {
            node->w = node->p->c1;
            node->n = node->p->c3;
        }
        else if (node->type == 3)
        {
            node->s = node->p->c2;
            node->w = node->p->c4;
        }
        else if (node->type == 4)
        {
            node->s = node->p->c1;
            node->e = node->p->c3;
        }

        // Check if each of these four child nodes will be subdivided.
        bool div1, div2, div3, div4;
        div1 = checkDivide(camPos, node->c1);
        div2 = checkDivide(camPos, node->c2);
        div3 = checkDivide(camPos, node->c3);
        div4 = checkDivide(camPos, node->c4);

        if (div1) {
            terrain_divideNode(camPos, node->c1);
        }
        if (div2) {
            terrain_divideNode(camPos, node->c2);
        }
        if (div3) {
            terrain_divideNode(camPos, node->c3);
        }
        if (div4) {
            terrain_divideNode(camPos, node->c4);
        }

        return div1 || div2 || div3 || div4;
    }

    /**
    * Builds a terrain quadtree based on specified parameters and current camera position.
    */
    void terrain_createTree(const vec3<F32>& camPos, const vec3<F32>& origin, const vec2<U16>& terrainDimensions)
    {
        terrain_clearTree();

        terrainTree->type = 0u; // Root node
        terrainTree->origin.set(origin);
        terrainTree->dimensions.set(terrainDimensions.width, terrainDimensions.height);
        terrainTree->tscale_negx = 1.0;
        terrainTree->tscale_negz = 1.0;
        terrainTree->tscale_posx = 1.0;
        terrainTree->tscale_posz = 1.0;
        terrainTree->p = NULL;
        terrainTree->n = NULL;
        terrainTree->s = NULL;
        terrainTree->e = NULL;
        terrainTree->w = NULL;

        // Recursively subdivide the terrain
        terrain_divideNode(camPos, terrainTree);
    }

    /**
    * Reserves memory for the terrrain quadtree and initializes the data structure.
    */
    void terrain_init()
    {
        terrainTree = (terrainNode_t*)malloc(Terrain::MAX_RENDER_NODES * sizeof(terrainNode_t));
        terrain_clearTree();
    }

    /**
    * Frees memory for the terrain quadtree.
    */
    void terrain_shutdown()
    {
        free(terrainTree);
        terrainTree = NULL;
        terrainTreeTail = NULL;
    }

    /**
    * Search for a node in the tree.
    * x, z == the point we are searching for (trying to find the node with an origin closest to that point)
    * n = the current node we are testing
    */
    terrainNode_t *find(terrainNode_t* n, F32 x, F32 z)
    {
        if (n->origin[0] == x && n->origin[2] == z)
            return n;

        if (n->c1 == NULL && n->c2 == NULL && n->c3 == NULL && n->c4 == NULL)
            return n;

        if (n->origin[0] >= x && n->origin[2] >= z && n->c1)
            return find(n->c1, x, z);
        else if (n->origin[0] <= x && n->origin[2] >= z && n->c2)
            return find(n->c2, x, z);
        else if (n->origin[0] <= x && n->origin[2] <= z && n->c3)
            return find(n->c3, x, z);
        else if (n->origin[0] >= x && n->origin[2] <= z && n->c4)
            return find(n->c4, x, z);

        return n;
    }

    /**
    * Calculate the tessellation scale factor for a node depending on the neighboring patches.
    */
    void calcTessScale(terrainNode_t *node)
    {
        terrainNode_t *t;

        // Positive Z (north)
        t = find(terrainTree, node->origin.x, node->origin.z + 1 + node->dimensions.width / 2.0f);
        if (t->dimensions.width > node->dimensions.width) {
            node->tscale_posz = 2.0;
        }

        // Positive X (east)
        t = find(terrainTree, node->origin.x + 1 + node->dimensions.width / 2.0f, node->origin.z);
        if (t->dimensions.width > node->dimensions.width) {
            node->tscale_posx = 2.0;
        }

        // Negative Z (south)
        t = find(terrainTree, node->origin.x, node->origin.z - 1 - node->dimensions.width / 2.0f);
        if (t->dimensions.width > node->dimensions.width) {
            node->tscale_negz = 2.0;
        }

        // Negative X (west)
        t = find(terrainTree, node->origin.x - 1 - node->dimensions.width / 2.0f, node->origin.z);
        if (t->dimensions.width > node->dimensions.width) {
            node->tscale_negx = 2.0;
        }
    }

    /**
    * Pushes a node (patch) to the GPU to be drawn.
    * note: height parameter is here but not used. currently only dealing with square terrains (width is used only)
    */

    struct NodeData {
        vec4<F32> _positionAndTileScale;
        vec4<F32> _tScale;
    };

    void terrain_renderNode(terrainNode_t *node, vectorImpl<NodeData>& data)
    {
        // Calculate the tess scale factor
        calcTessScale(node);

        data.push_back({ vec4<F32>(node->origin,
                                   0.5f * node->dimensions.width),
                         vec4<F32>(node->tscale_negx,
                                   node->tscale_negz,
                                   node->tscale_posx,
                                   node->tscale_posz)});
    }

    I32 maxRenderDepth = 1;
    I32 renderDepth = 0;

    /**
    * Traverses the terrain quadtree to draw nodes with no children.
    */
    void terrain_renderRecursive(terrainNode_t *node, vectorImpl<NodeData>& data)
    {
        //if (renderDepth >= maxRenderDepth)
        //	return;

        // If all children are null, render this node
        if (!node->c1 && !node->c2 && !node->c3 && !node->c4)
        {
            terrain_renderNode(node, data);
            renderDepth++;
            return;
        }

        // Otherwise, recruse to the children.
        // Note: we're checking if the child exists. Theoretically, with our algorithm,
        // either all the children are null or all the children are not null.
        // There shouldn't be any other cases, but we check here for safety.
        if (node->c1)
            terrain_renderRecursive(node->c1, data);
        if (node->c2)
            terrain_renderRecursive(node->c2, data);
        if (node->c3)
            terrain_renderRecursive(node->c3, data);
        if (node->c4)
            terrain_renderRecursive(node->c4, data);
    }

    /**
    * Draw the terrrain.
    */
    void terrain_render(vectorImpl<NodeData>& data)
    {
        renderDepth = 0;

        terrain_renderRecursive(terrainTree, data);
    }

};

Terrain::Terrain(GFXDevice& context, ResourceCache& parentCache, size_t descriptorHash, const stringImpl& name)
    : Object3D(context, parentCache, descriptorHash, name, ObjectType::TERRAIN, ObjectFlag::OBJECT_FLAG_NONE),
      _plane(nullptr),
      _shaderData(nullptr),
      _drawBBoxes(false),
      _underwaterDiffuseScale(100.0f),
      _chunkSize(1),
      _waterHeight(0.0f)
{
}

Terrain::~Terrain()
{
}

bool Terrain::unload() {
    MemoryManager::DELETE_VECTOR(_terrainTextures);
    Test::terrain_shutdown();
    return Object3D::unload();
}

void Terrain::postLoad(SceneGraphNode& sgn) {
    static const U32 normalMask = to_base(SGNComponent::ComponentType::NAVIGATION) |
                                  to_base(SGNComponent::ComponentType::PHYSICS) |
                                  to_base(SGNComponent::ComponentType::BOUNDS) |
                                  to_base(SGNComponent::ComponentType::RENDERING) |
                                  to_base(SGNComponent::ComponentType::NETWORKING);

    SceneGraphNode_ptr planeSGN(sgn.addNode(_plane, normalMask, PhysicsGroup::GROUP_STATIC));
    planeSGN->setActive(false);
    for (TerrainChunk* chunk : _terrainChunks) {
        //SceneGraphNode_ptr vegetation = sgn.addNode(Attorney::TerrainChunkTerrain::getVegetation(*chunk), normalMask);
        //vegetation->lockVisibility(true);
    }
    // Skip Object3D::load() to avoid triangle list computation (extremely expensive)!!!

    ShaderBufferParams params;
    params._primitiveCount = Terrain::MAX_RENDER_NODES;
    params._primitiveSizeInBytes = sizeof(Test::NodeData);
    params._ringBufferLength = 1;
    params._unbound = true;
    params._updateFrequency = BufferUpdateFrequency::OFTEN;

    _shaderData = _context.newSB(params);
    sgn.get<RenderingComponent>()->registerShaderBuffer(ShaderBufferLocation::TERRAIN_DATA,
                                                        vec2<U32>(0, Terrain::MAX_RENDER_NODES),
                                                        *_shaderData);

    Test::terrain_init();
    SceneNode::postLoad(sgn);
}

void Terrain::buildQuadtree() {
    reserveTriangleCount((_terrainDimensions.x - 1) *
                         (_terrainDimensions.y - 1) * 2);
    _terrainQuadtree.Build(
        _context,
        _boundingBox,
        vec2<U32>(_terrainDimensions.x, _terrainDimensions.y),
        _chunkSize, this);

    // The terrain's final bounding box is the QuadTree's root bounding box
    _boundingBox.set(_terrainQuadtree.computeBoundingBox());

    const Material_ptr& mat = getMaterialTpl();

    const vec3<F32>& bbMin = _boundingBox.getMin();
    const vec3<F32>& bbExtent = _boundingBox.getExtent();

    for (U8 pass = 0; pass < to_base(RenderPassType::COUNT); ++pass) {
        for (U32 i = 0; i < to_base(RenderStage::COUNT); ++i) {
            RenderStage stage = static_cast<RenderStage>(i);

            const ShaderProgram_ptr& drawShader = mat->getShaderInfo(RenderStagePass(stage, static_cast<RenderPassType>(pass))).getProgram();

            drawShader->Uniform("bbox_min", bbMin);
            drawShader->Uniform("bbox_extent", bbExtent);
            drawShader->Uniform("underwaterDiffuseScale", _underwaterDiffuseScale);

            U8 textureOffset = to_U8(ShaderProgram::TextureUsage::COUNT);
            U8 layerOffset = 0;
            stringImpl layerIndex;
            for (U8 k = 0; k < _terrainTextures.size(); ++k) {
                layerOffset = k * 3 + textureOffset;
                layerIndex = to_stringImpl(k);
                TerrainTextureLayer* textureLayer = _terrainTextures[k];

                drawShader->Uniform(("texBlend[" + layerIndex + "]").c_str(), layerOffset);
                drawShader->Uniform(("texTileMaps[" + layerIndex + "]").c_str(), layerOffset + 1);
                drawShader->Uniform(("texNormalMaps[" + layerIndex + "]").c_str(), layerOffset + 2);
                drawShader->Uniform(("diffuseScale[" + layerIndex + "]").c_str(), textureLayer->getDiffuseScales());
                drawShader->Uniform(("detailScale[" + layerIndex + "]").c_str(), textureLayer->getDetailScales());

                if (pass == 0 && i == 0 ) {
                    getMaterialTpl()->addCustomTexture(textureLayer->blendMap(), layerOffset);
                    getMaterialTpl()->addCustomTexture(textureLayer->tileMaps(), layerOffset + 1);
                    getMaterialTpl()->addCustomTexture(textureLayer->normalMaps(), layerOffset + 2);
                }
            }
        }
    }
}

void Terrain::sceneUpdate(const U64 deltaTime,
                          SceneGraphNode& sgn,
                          SceneState& sceneState) {
    _waterHeight = sceneState.waterLevel();
    _terrainQuadtree.sceneUpdate(deltaTime, sgn, sceneState);
    Object3D::sceneUpdate(deltaTime, sgn, sceneState);
}

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

    Test::treeDirty = true;
}

void Terrain::onCameraChange(SceneGraphNode& sgn,
                             const Camera& cam) {
    ACKNOWLEDGE_UNUSED(sgn);
    ACKNOWLEDGE_UNUSED(cam);

    Test::treeDirty = true;
}

void Terrain::initialiseDrawCommands(SceneGraphNode& sgn,
                                     const RenderStagePass& renderStagePass,
                                     GenericDrawCommands& drawCommandsInOut) {
   
    GenericDrawCommand cmd;
    cmd.primitiveType(PrimitiveType::PATCH);
    cmd.enableOption(GenericDrawCommand::RenderOptions::RENDER_TESSELLATED);
    cmd.sourceBuffer(getGeometryVB());
    cmd.cmd().indexCount = getGeometryVB()->getIndexCount();
    drawCommandsInOut.push_back(cmd);

    g_PlaneCommandIndex = drawCommandsInOut.size();
    if (renderStagePass._stage == RenderStage::DISPLAY) {
        //infinite plane
        GenericDrawCommand planeCmd;
        planeCmd.primitiveType(PrimitiveType::TRIANGLE_STRIP);
        planeCmd.cmd().firstIndex = 0;
        planeCmd.cmd().indexCount = _plane->getGeometryVB()->getIndexCount();
        planeCmd.LoD(0);
        planeCmd.sourceBuffer(_plane->getGeometryVB());
        planeCmd.shaderProgram(renderStagePass._passType == RenderPassType::DEPTH_PASS
                                                          ? _planeDepthShader
                                                          : _planeShader);
        drawCommandsInOut.push_back(planeCmd);

        //BoundingBoxes
        GenericDrawCommands commands;
        commands.reserve(getQuadtree().getChunkCount());

        _terrainQuadtree.drawBBox(_context, commands);
        for (const GenericDrawCommand& crtCmd : commands) {
            drawCommandsInOut.push_back(crtCmd);
        }
    }

    Object3D::initialiseDrawCommands(sgn, renderStagePass, drawCommandsInOut);
}

void Terrain::updateDrawCommands(SceneGraphNode& sgn,
                                 const RenderStagePass& renderStagePass,
                                 const SceneRenderState& sceneRenderState,
                                 GenericDrawCommands& drawCommandsInOut) {

    //_context.setClipPlane(ClipPlaneIndex::CLIP_PLANE_0, Plane<F32>(WORLD_Y_AXIS, _waterHeight));
    //drawCommandsInOut.front().shaderProgram()->Uniform("dvd_waterHeight", _waterHeight);
    drawCommandsInOut.front().shaderProgram()->Uniform("TerrainOrigin", vec3<F32>(-to_F32(_terrainDimensions.width) / 2.0f, -1024.0f / 2.0f, -to_F32(_terrainDimensions.height) / 2.0f));
    drawCommandsInOut.front().shaderProgram()->Uniform("TerrainHeightOffset", 1024.0f);
    drawCommandsInOut.front().shaderProgram()->Uniform("TerrainLength", to_F32(_terrainDimensions.height));
    drawCommandsInOut.front().shaderProgram()->Uniform("TerrainWidth", to_F32(_terrainDimensions.width));
    if (Test::treeDirty) {
        Test::terrain_createTree(Camera::activeCamera()->getEye(),
                                 vec3<F32>(0),
                                 _terrainDimensions);
        Test::treeDirty = false;
    }

    vectorImpl<Test::NodeData> data;
    Test::terrain_render(data);
    drawCommandsInOut.front().drawCount(data.size());

    _shaderData->updateData(0, data.size(), data.data());

    if (renderStagePass._stage == RenderStage::DISPLAY) {
        // draw infinite plane
       assert(drawCommandsInOut[g_PlaneCommandIndex].drawCount() == 1);

        _planeShader->Uniform("underwaterDiffuseScale", _underwaterDiffuseScale);
        _planeDepthShader->Uniform("underwaterDiffuseScale", _underwaterDiffuseScale);
        drawCommandsInOut[g_PlaneCommandIndex].shaderProgram(
            renderStagePass._passType == RenderPassType::DEPTH_PASS
                                       ? _planeDepthShader
                                       : _planeShader);
        size_t i = g_PlaneCommandIndex + 1;

        if (_drawBBoxes) {
            GenericDrawCommands commands;
            commands.reserve(getQuadtree().getChunkCount());
            _terrainQuadtree.drawBBox(_context, commands);

            for (const GenericDrawCommand& bbCmd : commands) {
                drawCommandsInOut[i++] = bbCmd;
            }

        } else {
            std::for_each(std::begin(drawCommandsInOut) + i,
                          std::end(drawCommandsInOut),
                          [](GenericDrawCommand& bbCmd) {
                                bbCmd.drawCount(0);
                          });
        }
    }

    Object3D::updateDrawCommands(sgn, renderStagePass, sceneRenderState, drawCommandsInOut);
}

vec3<F32> Terrain::getPositionFromGlobal(F32 x, F32 z) const {
    x -= _boundingBox.getCenter().x;
    z -= _boundingBox.getCenter().z;
    F32 xClamp = (0.5f * _terrainDimensions.x) + x;
    F32 zClamp = (0.5f * _terrainDimensions.y) - z;
    xClamp /= _terrainDimensions.x;
    zClamp /= _terrainDimensions.y;
    zClamp = 1 - zClamp;
    vec3<F32> temp = getPosition(xClamp, zClamp);

    return temp;
}

vec3<F32> Terrain::getPosition(F32 x_clampf, F32 z_clampf) const {
    assert(!(x_clampf < .0f || z_clampf < .0f || x_clampf > 1.0f ||
             z_clampf > 1.0f));

    vec2<F32> posF(x_clampf * _terrainDimensions.x,
                   z_clampf * _terrainDimensions.y);
    vec2<I32> posI(to_I32(posF.x), to_I32(posF.y));
    vec2<F32> posD(posF.x - posI.x, posF.y - posI.y);

    if (posI.x >= (I32)_terrainDimensions.x - 1)
        posI.x = _terrainDimensions.x - 2;
    if (posI.y >= (I32)_terrainDimensions.y - 1)
        posI.y = _terrainDimensions.y - 2;

    assert(posI.x >= 0 && posI.x < to_I32(_terrainDimensions.x) - 1 &&
           posI.y >= 0 && posI.y < to_I32(_terrainDimensions.y) - 1);

    vec3<F32> pos(
        _boundingBox.getMin().x +
            x_clampf * (_boundingBox.getMax().x - _boundingBox.getMin().x),
        0.0f,
        _boundingBox.getMin().z +
            z_clampf * (_boundingBox.getMax().z - _boundingBox.getMin().z));

    auto height = [&](U32 index) -> F32 {
        return _physicsVerts[index]._position.y;
    };

    pos.y = height(TER_COORD(posI.x, posI.y, to_I32(_terrainDimensions.x))) *
            (1.0f - posD.x) * (1.0f - posD.y) + 
            height(TER_COORD(posI.x + 1, posI.y, to_I32(_terrainDimensions.x))) *
            posD.x * (1.0f - posD.y) +
            height(TER_COORD(posI.x, posI.y + 1, to_I32(_terrainDimensions.x))) *
            (1.0f - posD.x) * posD.y +
            height(TER_COORD(posI.x + 1, posI.y + 1,to_I32(_terrainDimensions.x))) *
            posD.x * posD.y;

    return pos;
}

vec3<F32> Terrain::getNormal(F32 x_clampf, F32 z_clampf) const {
    assert(!(x_clampf < .0f || z_clampf < .0f || x_clampf > 1.0f ||
             z_clampf > 1.0f));

    vec2<F32> posF(x_clampf * _terrainDimensions.x,
                   z_clampf * _terrainDimensions.y);
    vec2<I32> posI(to_I32(x_clampf * _terrainDimensions.x),
                   to_I32(z_clampf * _terrainDimensions.y));
    vec2<F32> posD(posF.x - posI.x, posF.y - posI.y);

    if (posI.x >= to_I32(_terrainDimensions.x) - 1) {
        posI.x = to_I32(_terrainDimensions.x) - 2;
    }
    if (posI.y >= to_I32(_terrainDimensions.y) - 1) {
        posI.y = to_I32(_terrainDimensions.y) - 2;
    }
    assert(posI.x >= 0 && posI.x < to_I32(_terrainDimensions.x) - 1 &&
           posI.y >= 0 && posI.y < to_I32(_terrainDimensions.y) - 1);

    vec3<F32> normals[4];

    Util::UNPACK_VEC3(_physicsVerts[TER_COORD(posI.x, posI.y, to_I32(_terrainDimensions.x))]._normal, normals[0]);
    Util::UNPACK_VEC3(_physicsVerts[TER_COORD(posI.x + 1, posI.y, to_I32(_terrainDimensions.x))]._normal, normals[1]);
    Util::UNPACK_VEC3(_physicsVerts[TER_COORD(posI.x, posI.y + 1, to_I32(_terrainDimensions.x))]._normal, normals[2]);
    Util::UNPACK_VEC3(_physicsVerts[TER_COORD(posI.x + 1, posI.y + 1, to_I32(_terrainDimensions.x))]._normal, normals[3]);

    return normals[0] * (1.0f - posD.x) * (1.0f - posD.y) +
           normals[1] * posD.x * (1.0f - posD.y) +
           normals[2] * (1.0f - posD.x) * posD.y + 
           normals[3] * posD.x * posD.y;
}

vec3<F32> Terrain::getTangent(F32 x_clampf, F32 z_clampf) const {
    assert(!(x_clampf < .0f || z_clampf < .0f || x_clampf > 1.0f ||
             z_clampf > 1.0f));

    vec2<F32> posF(x_clampf * _terrainDimensions.x,
                   z_clampf * _terrainDimensions.y);
    vec2<I32> posI(to_I32(x_clampf * _terrainDimensions.x),
                   to_I32(z_clampf * _terrainDimensions.y));
    vec2<F32> posD(posF.x - posI.x, posF.y - posI.y);

    if (posI.x >= to_I32(_terrainDimensions.x) - 1) {
        posI.x = to_I32(_terrainDimensions.x) - 2;
    }
    if (posI.y >= to_I32(_terrainDimensions.y) - 1) {
        posI.y = to_I32(_terrainDimensions.y) - 2;
    }
    assert(posI.x >= 0 && posI.x < to_I32(_terrainDimensions.x) - 1 &&
           posI.y >= 0 && posI.y < to_I32(_terrainDimensions.y) - 1);

    vec3<F32> tangents[4];

    Util::UNPACK_VEC3(_physicsVerts[TER_COORD(posI.x, posI.y, to_I32(_terrainDimensions.x))]._tangent, tangents[0]);
    Util::UNPACK_VEC3(_physicsVerts[TER_COORD(posI.x + 1, posI.y, to_I32(_terrainDimensions.x))]._tangent, tangents[1]);
    Util::UNPACK_VEC3(_physicsVerts[TER_COORD(posI.x, posI.y + 1, to_I32(_terrainDimensions.x))]._tangent, tangents[2]);
    Util::UNPACK_VEC3(_physicsVerts[TER_COORD(posI.x + 1, posI.y + 1, to_I32(_terrainDimensions.x))]._tangent, tangents[3]);

    return tangents[0] * (1.0f - posD.x) * (1.0f - posD.y) +
           tangents[1] * posD.x * (1.0f - posD.y) +
           tangents[2] * (1.0f - posD.x) * posD.y +
           tangents[3] * posD.x * posD.y;
}

TerrainTextureLayer::~TerrainTextureLayer()
{
}
};

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

Diff revisions: vs.
Revision Author Commited Message
881 Diff Diff IonutCava picture IonutCava Tue 21 Mar, 2017 22:07:37 +0000

[IonutCava]
- Multi draw command bug fix: drawCount > 1 should properly register all commands in the command buffer
- More tessellated terrain updates and fixes (still Work In Progress)

880 Diff Diff IonutCava picture IonutCava Mon 20 Mar, 2017 17:27:13 +0000

[IonutCava]
- New tessellated terrain system based on this: http://victorbush.com/2015/01/tessellated-terrain/
— Work in progress: hardcoded values, new code is in Test namespace
- Split terrain geometry in two:
— tessellated rendering data
— physics verts + indices (used for collisions, vegetation, etc)
- Old Chunk system is used only for culling objects and speeding up collision detection

874 Diff Diff IonutCava picture IonutCava Mon 13 Mar, 2017 17:29:45 +0000

[IonutCava]
- Fix boundingbox skewing bug.
- Optimise frustum collision checks by adding a plane-cache system.
— ToDo: solve mutable status of _frustPlaneCache
— ToDo: TRIPLE check results to avoid false-positives
- Speedup terrain by not stack-allocating QuadtreeNodes in a QuadtreeChildren container instead of new-ing each one of them.

872 Diff Diff IonutCava picture IonutCava Mon 06 Mar, 2017 23:23:20 +0000

[IonutCava]
- Optimise static casts

867 Diff Diff IonutCava picture IonutCava Mon 20 Feb, 2017 17:01:12 +0000

[IonutCava]
- Rename to_XNN and to_const_XNN functions to reflect the target data type they are converting to.
- Cleanup Angle namespace and remove all “inDegrees” parameters from rotation functions. Use aliases to differentiate between Degree and Radian parameters.
- Profile guided optimizations.

866 Diff Diff IonutCava picture IonutCava Mon 20 Feb, 2017 13:57:52 +0000

[IonutCava]
- Cleanup terrain chunk generation

863 Diff Diff IonutCava picture IonutCava Wed 15 Feb, 2017 16:27:26 +0000

[IonutCava]
- Split Resource class into Resource and CachedResource
— CachedResource is the only resource held in a ResourceCache and loaded from a ResourceDescriptor
— CachedResource is mapped by its creation descriptor’s hash value, instead of the resource name
-— Multiple cached resources can now exist with the same name if the have different properties, for example.

859 Diff Diff IonutCava picture IonutCava Mon 13 Feb, 2017 17:18:06 +0000

[IonutCava]
- Multi-threaded mesh load
- ByteBuffer fixes and unit test

858 Diff Diff IonutCava picture IonutCava Sun 12 Feb, 2017 20:55:05 +0000

[IonutCava]
- More resource loading updates

856 IonutCava picture IonutCava Fri 10 Feb, 2017 17:19:06 +0000

[IonutCava]
- Attempt to improve resource creation mechanism by adding a callback for when the resource finishes loading
— This is useful for multi-threaded loading: shaders, textures, terrain, etc