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
#include "stdafx.h"

#include "Headers/Terrain.h"
#include "Headers/TerrainLoader.h"
#include "Headers/TerrainDescriptor.h"
#include "Headers/TerrainTessellator.h"

#include "Core/Headers/PlatformContext.h"
#include "Platform/File/Headers/FileManagement.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Video/Headers/RenderStateBlock.h"

#include "Geometry/Material/Headers/Material.h"
#include "Managers/Headers/SceneManager.h"
#include "Geometry/Shapes/Predefined/Headers/Quad3D.h"
#include "Geometry/Shapes/Predefined/Headers/Patch3D.h"

namespace Divide {

bool TerrainLoader::loadTerrain(std::shared_ptr<Terrain> terrain,
                                const std::shared_ptr<TerrainDescriptor>& terrainDescriptor,
                                PlatformContext& context,
                                bool threadedLoading,
                                const DELEGATE_CBK<void, CachedResource_wptr>& onLoadCallback ) {
    const stringImpl& name = terrainDescriptor->getVariable("terrainName");
    const stringImpl& terrainMapLocation = terrainDescriptor->getVariable("textureLocation");

    // Blend map
    ResourceDescriptor textureBlendMap("Terrain Blend Map_" + name);
    textureBlendMap.setResourceLocation(terrainMapLocation);
    textureBlendMap.setFlag(true);

    // Albedo map
    ResourceDescriptor textureTileMaps("Terrain Tile Maps_" + name);
    textureTileMaps.setResourceLocation(terrainMapLocation);
    textureTileMaps.setFlag(true);

    // Normal map
    ResourceDescriptor textureNormalMaps("Terrain Normal Maps_" + name);
    textureNormalMaps.setResourceLocation(terrainMapLocation);
    textureNormalMaps.setFlag(true);

    //temp data
    stringImpl layerOffsetStr;
    stringImpl currentEntry;

    //texture data
    stringImpl blendMapArray;
    stringImpl albedoMapArray;
    stringImpl normalMapArray;
    U8 albedoCount = 0;
    U8 normalCount = 0;

    U8 layerCount = terrainDescriptor->getTextureLayerCount();
    TerrainTextureLayer* textureLayer = MemoryManager_NEW TerrainTextureLayer(layerCount);

    for (U8 i = 0; i < layerCount; ++i) {
        layerOffsetStr = to_stringImpl(i);
        
        blendMapArray += ((i != 0) ? (",") : ("")) + terrainDescriptor->getVariable("blendMap" + layerOffsetStr);
        
        currentEntry = terrainDescriptor->getVariable("redAlbedo" + layerOffsetStr);
        if (!currentEntry.empty()) {
            albedoMapArray += ((i != 0) ? (",") : ("")) + currentEntry;
            albedoCount++;

            textureLayer->setDiffuseScale(TerrainTextureLayer::TerrainTextureChannel::TEXTURE_RED_CHANNEL,
                                          i,
                                          terrainDescriptor->getVariablef("diffuseScaleR" + layerOffsetStr));
        }

        currentEntry = terrainDescriptor->getVariable("greenAlbedo" + layerOffsetStr);
        if (!currentEntry.empty()) {
            albedoMapArray += "," + currentEntry;
            albedoCount++;

            
            textureLayer->setDiffuseScale(TerrainTextureLayer::TerrainTextureChannel::TEXTURE_GREEN_CHANNEL,
                                          i,
                                          terrainDescriptor->getVariablef("diffuseScaleG" + layerOffsetStr));
        }

        currentEntry = terrainDescriptor->getVariable("blueAlbedo" + layerOffsetStr);
        if (!currentEntry.empty()) {
            albedoMapArray += "," + currentEntry;
            albedoCount++;
            
            textureLayer->setDiffuseScale(TerrainTextureLayer::TerrainTextureChannel::TEXTURE_BLUE_CHANNEL,
                                          i,
                                          terrainDescriptor->getVariablef("diffuseScaleB" + layerOffsetStr));
        }

        currentEntry = terrainDescriptor->getVariable("alphaAlbedo" + layerOffsetStr);
        if (!currentEntry.empty()) {
            albedoMapArray += "," + currentEntry;
            albedoCount++;
            
            textureLayer->setDiffuseScale(TerrainTextureLayer::TerrainTextureChannel::TEXTURE_ALPHA_CHANNEL,
                                          i,
                                          terrainDescriptor->getVariablef("diffuseScaleA" + layerOffsetStr));
        }


        currentEntry =  terrainDescriptor->getVariable("redDetail" + layerOffsetStr);
        if (!currentEntry.empty()) {
            normalMapArray += ((i != 0) ? (",") : ("")) + currentEntry;
            normalCount++;

            textureLayer->setDetailScale(TerrainTextureLayer::TerrainTextureChannel::TEXTURE_RED_CHANNEL,
                                         i,
                                         terrainDescriptor->getVariablef("detailScaleR" + layerOffsetStr));
        }

        currentEntry = terrainDescriptor->getVariable("greenDetail" + layerOffsetStr);
        if (!currentEntry.empty()) {
            normalMapArray += "," + currentEntry;
            normalCount++;

            textureLayer->setDetailScale(TerrainTextureLayer::TerrainTextureChannel::TEXTURE_GREEN_CHANNEL,
                                         i,
                                         terrainDescriptor->getVariablef("detailScaleG" + layerOffsetStr));
        }

        currentEntry = terrainDescriptor->getVariable("blueDetail" + layerOffsetStr);
        if (!currentEntry.empty()) {
            normalMapArray += "," + currentEntry;
            normalCount++;

            textureLayer->setDetailScale(TerrainTextureLayer::TerrainTextureChannel::TEXTURE_BLUE_CHANNEL,
                                         i,
                                         terrainDescriptor->getVariablef("detailScaleB" + layerOffsetStr));
        }

        currentEntry = terrainDescriptor->getVariable("alphaDetail" + layerOffsetStr);
        if (!currentEntry.empty()) {
            normalMapArray += "," + currentEntry;
            normalCount++;

            textureLayer->setDetailScale(TerrainTextureLayer::TerrainTextureChannel::TEXTURE_ALPHA_CHANNEL,
                                         i,
                                         terrainDescriptor->getVariablef("detailScaleA" + layerOffsetStr));
        }
    }

    SamplerDescriptor blendMapSampler;
    blendMapSampler.setWrapMode(TextureWrap::CLAMP);
    blendMapSampler.setAnisotropy(0);
    blendMapSampler.setMinFilter(TextureFilter::LINEAR);

    SamplerDescriptor albedoSampler;
    albedoSampler.setWrapMode(TextureWrap::REPEAT);
    albedoSampler.setAnisotropy(8);
    albedoSampler.toggleSRGBColourSpace(true);

    SamplerDescriptor normalSampler;
    normalSampler.setWrapMode(TextureWrap::REPEAT);
    normalSampler.setAnisotropy(8);

    SamplerDescriptor heightMapSampler;
    heightMapSampler.setWrapMode(TextureWrap::CLAMP);
    heightMapSampler.setAnisotropy(0);
    heightMapSampler.setMinFilter(TextureFilter::LINEAR);

    TextureDescriptor heightMapDescriptor(TextureType::TEXTURE_2D);
    heightMapDescriptor.setSampler(heightMapSampler);

    TextureDescriptor blendMapDescriptor(TextureType::TEXTURE_2D_ARRAY);
    blendMapDescriptor.setSampler(blendMapSampler);
    blendMapDescriptor._layerCount = layerCount;

    TextureDescriptor albedoDescriptor(TextureType::TEXTURE_2D_ARRAY);
    albedoDescriptor.setSampler(albedoSampler);
    albedoDescriptor._layerCount = albedoCount;

    TextureDescriptor normalDescriptor(TextureType::TEXTURE_2D_ARRAY);
    normalDescriptor.setSampler(normalSampler);
    normalDescriptor._layerCount = normalCount;

    textureBlendMap.setResourceName(blendMapArray);
    textureBlendMap.setPropertyDescriptor(blendMapDescriptor);

    textureTileMaps.setResourceName(albedoMapArray);
    textureTileMaps.setPropertyDescriptor(albedoDescriptor);
    
    textureNormalMaps.setResourceName(normalMapArray);
    textureNormalMaps.setPropertyDescriptor(normalDescriptor);

    textureLayer->setBlendMaps(CreateResource<Texture>(terrain->parentResourceCache(), textureBlendMap));
    textureLayer->setTileMaps(CreateResource<Texture>(terrain->parentResourceCache(), textureTileMaps));
    textureLayer->setNormalMaps(CreateResource<Texture>(terrain->parentResourceCache(), textureNormalMaps));
    Attorney::TerrainLoader::setTextureLayer(*terrain, textureLayer);

    ResourceDescriptor terrainMaterialDescriptor("terrainMaterial_" + name);
    Material_ptr terrainMaterial = CreateResource<Material>(terrain->parentResourceCache(), terrainMaterialDescriptor);

    const vec2<U16>& terrainDimensions = terrainDescriptor->getDimensions();
    const vec2<F32>& altitudeRange = terrainDescriptor->getAltitudeRange();

    Console::d_printfn(Locale::get(_ID("TERRAIN_INFO")), terrainDimensions.width, terrainDimensions.height);

    Attorney::TerrainLoader::dimensions(*terrain).set(terrainDimensions.width, terrainDimensions.height);
    Attorney::TerrainLoader::scaleFactor(*terrain).set(terrainDescriptor->getScale());
    Attorney::TerrainLoader::offsetPosition(*terrain).set(terrainDescriptor->getPosition());
    Attorney::TerrainLoader::altitudeRange(*terrain).set(altitudeRange);

    F32 underwaterDiffuseScale = terrainDescriptor->getVariablef("underwaterDiffuseScale");
    terrainMaterial->setDiffuse(FColour(DefaultColours::WHITE.rgb() * 0.5f, 1.0f));
    terrainMaterial->setSpecular(FColour(0.1f, 0.1f, 0.1f, 1.0f));
    terrainMaterial->setShininess(20.0f);
    terrainMaterial->setShadingMode(Material::ShadingMode::BLINN_PHONG);

    //terrainMaterial->setShaderLoadThreaded(false);
    //terrainMaterial->setShaderDefines("TOGGLE_WIREFRAME");
    terrainMaterial->setShaderDefines("COMPUTE_TBN");
    terrainMaterial->setShaderDefines("SKIP_TEXTURES");
    terrainMaterial->setShaderDefines("USE_SHADING_PHONG");
    terrainMaterial->setShaderDefines("MAX_TEXTURE_LAYERS " + to_stringImpl(Attorney::TerrainLoader::textureLayerCount(*terrain)));
    terrainMaterial->setShaderDefines("CURRENT_TEXTURE_COUNT " + to_stringImpl(albedoCount));
    terrainMaterial->setShaderDefines("TERRAIN_WIDTH " + to_stringImpl(terrainDimensions.width));
    terrainMaterial->setShaderDefines("TERRAIN_LENGTH " + to_stringImpl(terrainDimensions.height));
    terrainMaterial->setShaderDefines("TERRAIN_MIN_HEIGHT " + to_stringImpl(altitudeRange.x));
    terrainMaterial->setShaderDefines("TERRAIN_HEIGHT_RANGE " + to_stringImpl(altitudeRange.y - altitudeRange.x));
    terrainMaterial->setShaderDefines("UNDERWATER_DIFFUSE_SCALE " + to_stringImpl(underwaterDiffuseScale));
    terrainMaterial->setShaderProgram("terrainTess." + name, true);
    terrainMaterial->setShaderProgram("terrainTess.PrePass." + name, RenderPassType::DEPTH_PASS, true);
    terrainMaterial->setShaderProgram("terrainTess.Shadow." + name, RenderStage::SHADOW, true);

    TextureDescriptor miscTexDescriptor(TextureType::TEXTURE_2D);
    miscTexDescriptor.setSampler(albedoSampler);

    ResourceDescriptor textureWaterCaustics("Terrain Water Caustics_" + name);
    textureWaterCaustics.setResourceLocation(terrainMapLocation);
    textureWaterCaustics.setResourceName(terrainDescriptor->getVariable("waterCaustics"));
    textureWaterCaustics.setPropertyDescriptor(miscTexDescriptor);
    
    terrainMaterial->setTexture(ShaderProgram::TextureUsage::UNIT0, CreateResource<Texture>(terrain->parentResourceCache(), textureWaterCaustics));

    ResourceDescriptor underwaterAlbedoTexture("Terrain Underwater Albedo_" + name);
    underwaterAlbedoTexture.setResourceLocation(terrainMapLocation);
    underwaterAlbedoTexture.setResourceName(terrainDescriptor->getVariable("underwaterAlbedoTexture"));
    underwaterAlbedoTexture.setPropertyDescriptor(miscTexDescriptor);
    terrainMaterial->setTexture(ShaderProgram::TextureUsage::UNIT1, CreateResource<Texture>(terrain->parentResourceCache(), underwaterAlbedoTexture));

    ResourceDescriptor underwaterDetailTexture("Terrain Underwater Detail_" + name);
    underwaterDetailTexture.setResourceLocation(terrainMapLocation);
    underwaterDetailTexture.setResourceName(terrainDescriptor->getVariable("underwaterDetailTexture"));
    underwaterDetailTexture.setPropertyDescriptor(miscTexDescriptor);
    terrainMaterial->setTexture(ShaderProgram::TextureUsage::NORMALMAP, CreateResource<Texture>(terrain->parentResourceCache(), underwaterDetailTexture));

    ResourceDescriptor heightMapTexture("Terrain Heightmap_" + name);
    heightMapTexture.setResourceLocation(terrainDescriptor->getVariable("heightmapLocation"));
    heightMapTexture.setResourceName(terrainDescriptor->getVariable("heightmap"));
    heightMapTexture.setPropertyDescriptor(heightMapDescriptor);
    heightMapTexture.setFlag(true);

    terrainMaterial->setTexture(ShaderProgram::TextureUsage::OPACITY, CreateResource<Texture>(terrain->parentResourceCache(), heightMapTexture));

    terrainMaterial->dumpToFile(false);
    terrain->setMaterialTpl(terrainMaterial);

    // Generate a render state
    RenderStateBlock terrainRenderState;
    terrainRenderState.setCullMode(CullMode::CCW);
    // Generate a render state for drawing reflections
    RenderStateBlock terrainRenderStateReflection;
    terrainRenderStateReflection.setCullMode(CullMode::CCW);
    // Generate a shadow render state
    RenderStateBlock terrainRenderStateDepth;
    terrainRenderStateDepth.setCullMode(CullMode::CCW);
    // terrainDescDepth.setZBias(1.0f, 2.0f);
    terrainRenderStateDepth.setColourWrites(true, true, false, false);

    terrainMaterial->setRenderStateBlock(terrainRenderState.getHash());
    terrainMaterial->setRenderStateBlock(terrainRenderStateReflection.getHash(), RenderStage::REFLECTION);
    terrainMaterial->setRenderStateBlock(terrainRenderStateDepth.getHash(), RenderStage::SHADOW);

    return context.gfx().loadInContext(threadedLoading ? CurrentContext::GFX_LOADING_CTX : CurrentContext::GFX_RENDERING_CTX,
                                       [terrain, terrainDescriptor, onLoadCallback](const Task& parent) {
                                            loadThreadedResources(terrain, std::move(terrainDescriptor), std::move(onLoadCallback));
                                       });
}

bool TerrainLoader::loadThreadedResources(std::shared_ptr<Terrain> terrain,
                                          const std::shared_ptr<TerrainDescriptor> terrainDescriptor,
                                          DELEGATE_CBK<void, CachedResource_wptr> onLoadCallback) {

    ResourceDescriptor infinitePlane("infinitePlane");
    infinitePlane.setFlag(true);  // No default material

    Attorney::TerrainLoader::chunkSize(*terrain) = terrainDescriptor->getChunkSize();

    const stringImpl& terrainMapLocation = terrainDescriptor->getVariable("heightmapLocation");
    stringImpl terrainRawFile(terrainDescriptor->getVariable("heightmap"));

    const vec2<U16>& terrainDimensions = Attorney::TerrainLoader::dimensions(*terrain);
    
    F32 minAltitude = terrainDescriptor->getAltitudeRange().x;
    F32 maxAltitude = terrainDescriptor->getAltitudeRange().y;
    BoundingBox& terrainBB = Attorney::TerrainLoader::boundingBox(*terrain);
    terrainBB.set(vec3<F32>(-terrainDimensions.x * 0.5f, minAltitude, -terrainDimensions.y * 0.5f),
                  vec3<F32>( terrainDimensions.x * 0.5f, maxAltitude,  terrainDimensions.y * 0.5f));

    const vec3<F32>& bMin = terrainBB.getMin();
    const vec3<F32>& bMax = terrainBB.getMax();

    stringImpl cacheLocation(terrainMapLocation + terrainRawFile + ".cache");
    ByteBuffer terrainCache;
    if (!terrainCache.loadFromFile(cacheLocation)) {
        F32 altitudeRange = maxAltitude - minAltitude;

        vectorImpl<U16> heightValues;
        if (terrainDescriptor->is16Bit()) {
            assert(terrainDimensions.x != 0 && terrainDimensions.y != 0);
            // only raw files for 16 bit support
            assert(hasExtension(terrainRawFile, "raw"));
            // Read File Data

            vectorImpl<Byte> data;
            readFile(terrainMapLocation + terrainRawFile, data, FileType::BINARY);
            if (data.empty()) {
                return false;
            }

            U32 positionCount = to_U32(data.size());
            heightValues.reserve(positionCount / 2);
            for (U32 i = 0; i < positionCount + 1; i += 2) {
                heightValues.push_back((data[i + 1] << 8) | data[i]);
            }

        } else {
            ImageTools::ImageData img;
            //img.flip(true);
            ImageTools::ImageDataInterface::CreateImageData(terrainMapLocation + terrainRawFile, img);
            assert(terrainDimensions == img.dimensions());

            // data will be destroyed when img gets out of scope
            const U8* data = (const U8*)img.data();
            assert(data);
            heightValues.insert(std::end(heightValues), &data[0], &data[img.imageLayers().front()._data.size()]);
        }


        I32 terrainWidth = to_I32(terrainDimensions.x);
        I32 terrainHeight = to_I32(terrainDimensions.y);

        terrain->_physicsVerts.resize(terrainWidth * terrainHeight);

        // scale and translate all height by half to convert from 0-255 (0-65335) to -127 - 128 (-32767 - 32768)

        auto highDetailHeight = [minAltitude, altitudeRange](F32 height) -> F32 {
            constexpr F32 fMax = std::numeric_limits<U16>::max() + 1.0f;
            return minAltitude + altitudeRange * (height / fMax);
        };

        auto lowDetailHeight = [minAltitude, altitudeRange](F32 height) -> F32 {
            constexpr F32 byteMax = std::numeric_limits<U8>::max() + 1.0f;
            return minAltitude + altitudeRange * (height / byteMax);
        };
        
        F32 bXRange = bMax.x - bMin.x;
        F32 bZRange = bMax.z - bMin.z;

        #pragma omp parallel for
        for (I32 j = 0; j < terrainHeight; j++) {
            for (I32 i = 0; i < terrainWidth; i++) {
                U32 idxIMG = TER_COORD<U32>(i < to_I32(terrainDimensions.x) ? i : i - 1,
                                            j < to_I32(terrainDimensions.y) ? j : j - 1,
                                            terrainDimensions.x);

                F32 heightValue = ((heightValues[idxIMG * 3 + 0] +
                                    heightValues[idxIMG * 3 + 1] +
                                    heightValues[idxIMG * 3 + 2]) / 3.0f);

                vec3<F32> vertexData(bMin.x + (to_F32(i)) * bXRange / (terrainWidth - 1),          //X
                                     terrainDescriptor->is16Bit() ? highDetailHeight(heightValue)
                                                                  : lowDetailHeight(heightValue),  //Y
                                     bMin.z + (to_F32(j)) * (bZRange) / (terrainHeight - 1));      //Z

                    
                U32 targetCoord = TER_COORD(i, j, terrainWidth);

                #pragma omp critical
                {
                    terrain->_physicsVerts[targetCoord]._position.set(vertexData);
                }
            }
        }

        heightValues.clear();

        I32 offset = 2;
        U32 idx = 0, idx0 = 0, idx1 = 0;
    
        #pragma omp parallel for
        for (I32 j = offset; j < terrainHeight - offset; j++) {
            for (I32 i = offset; i < terrainWidth - offset; i++) {
                vec3<F32> vU, vV, vUV;

                idx = TER_COORD(i, j, terrainWidth);

                vU.set(terrain->_physicsVerts[TER_COORD(i + offset, j + 0, terrainWidth)]._position -
                       terrain->_physicsVerts[TER_COORD(i - offset, j + 0, terrainWidth)]._position);
                vV.set(terrain->_physicsVerts[TER_COORD(i + 0, j + offset, terrainWidth)]._position -
                       terrain->_physicsVerts[TER_COORD(i + 0, j - offset, terrainWidth)]._position);

                vUV.cross(vV, vU);
                vUV.normalize();
                vU = -vU;
                vU.normalize();
                #pragma omp critical
                {
                    terrain->_physicsVerts[idx]._normal = Util::PACK_VEC3(vUV);
                    terrain->_physicsVerts[idx]._tangent = Util::PACK_VEC3(vU);
                }
            }
        }
        
        vec3<F32> normal, tangent;
        for (I32 j = 0; j < offset; j++) {
            for (I32 i = 0; i < terrainWidth; i++) {
                idx0 = TER_COORD(i, j, terrainWidth);
                idx1 = TER_COORD(i, offset, terrainWidth);

                normal.set(Util::UNPACK_VEC3(terrain->_physicsVerts[idx1]._normal));
                tangent.set(Util::UNPACK_VEC3(terrain->_physicsVerts[idx1]._tangent));

                terrain->_physicsVerts[idx0]._normal = Util::PACK_VEC3(normal);
                terrain->_physicsVerts[idx0]._tangent = Util::PACK_VEC3(tangent);

                idx0 = TER_COORD(i, terrainHeight - 1 - j, terrainWidth);
                idx1 = TER_COORD(i, terrainHeight - 1 - offset, terrainWidth);

                normal.set(Util::UNPACK_VEC3(terrain->_physicsVerts[idx1]._normal));
                tangent.set(Util::UNPACK_VEC3(terrain->_physicsVerts[idx1]._tangent));

                terrain->_physicsVerts[idx0]._normal = Util::PACK_VEC3(normal);
                terrain->_physicsVerts[idx0]._tangent = Util::PACK_VEC3(tangent);
            }
        }

        for (I32 i = 0; i < offset; i++) {
            for (I32 j = 0; j < terrainHeight; j++) {
                idx0 = TER_COORD(i, j, terrainWidth);
                idx1 = TER_COORD(offset, j, terrainWidth);

                normal.set(Util::UNPACK_VEC3(terrain->_physicsVerts[idx1]._normal));
                tangent.set(Util::UNPACK_VEC3(terrain->_physicsVerts[idx1]._tangent));

                terrain->_physicsVerts[idx0]._normal = Util::PACK_VEC3(normal);
                terrain->_physicsVerts[idx0]._tangent = Util::PACK_VEC3(tangent);

                idx0 = TER_COORD(terrainWidth - 1 - i, j, terrainWidth);
                idx1 = TER_COORD(terrainWidth - 1 - offset, j, terrainWidth);

                normal.set(Util::UNPACK_VEC3(terrain->_physicsVerts[idx1]._normal));
                tangent.set(Util::UNPACK_VEC3(terrain->_physicsVerts[idx1]._tangent));

                terrain->_physicsVerts[idx0]._normal = Util::PACK_VEC3(normal);
                terrain->_physicsVerts[idx0]._tangent = Util::PACK_VEC3(tangent);
            }
        }
        terrainCache << terrain->_physicsVerts;
        terrainCache.dumpToFile(cacheLocation);
    } else {
        terrainCache >> terrain->_physicsVerts;
    }

    F32 underwaterDiffuseScale = terrainDescriptor->getVariablef("underwaterDiffuseScale");

    ResourceDescriptor planeShaderDesc("terrainPlane.Colour");
    planeShaderDesc.setPropertyList("UNDERWATER_DIFFUSE_SCALE " + to_stringImpl(underwaterDiffuseScale));
    ShaderProgram_ptr planeShader = CreateResource<ShaderProgram>(terrain->parentResourceCache(), planeShaderDesc);
    planeShaderDesc.setResourceName("terrainPlane.Depth");
    ShaderProgram_ptr planeDepthShader = CreateResource<ShaderProgram>(terrain->parentResourceCache(), planeShaderDesc);

    Quad3D_ptr plane = CreateResource<Quad3D>(terrain->parentResourceCache(), infinitePlane);
    // our bottom plane is placed at the bottom of our water entity
    F32 height = bMin.y;
    F32 farPlane = 2.0f * terrainBB.getWidth();

    plane->setCorner(Quad3D::CornerLocation::TOP_LEFT, vec3<F32>(-farPlane * 1.5f, height, -farPlane * 1.5f));
    plane->setCorner(Quad3D::CornerLocation::TOP_RIGHT, vec3<F32>(farPlane * 1.5f, height, -farPlane * 1.5f));
    plane->setCorner(Quad3D::CornerLocation::BOTTOM_LEFT, vec3<F32>(-farPlane * 1.5f, height, farPlane * 1.5f));
    plane->setCorner(Quad3D::CornerLocation::BOTTOM_RIGHT, vec3<F32>(farPlane * 1.5f, height, farPlane * 1.5f));

    VertexBuffer* vb = terrain->getGeometryVB();
    Attorney::TerrainTessellatorLoader::initTessellationPatch(vb);
    vb->keepData(false);
    vb->create(true);

    initializeVegetation(terrain, terrainDescriptor);
    Attorney::TerrainLoader::buildQuadtree(*terrain);
    Attorney::TerrainLoader::plane(*terrain, plane, planeShader, planeDepthShader);

    Console::printfn(Locale::get(_ID("TERRAIN_LOAD_END")), terrain->getName().c_str());
    return terrain->load(onLoadCallback);
}

void TerrainLoader::initializeVegetation(std::shared_ptr<Terrain> terrain,
                                         const std::shared_ptr<TerrainDescriptor> terrainDescriptor) {
    U8 textureCount = 0;
    stringImpl textureName;

    stringImpl currentImage = terrainDescriptor->getVariable("grassBillboard1");
    if (!currentImage.empty()) {
        textureName += currentImage;
        textureCount++;
    }

    currentImage = terrainDescriptor->getVariable("grassBillboard2");
    if (!currentImage.empty()) {
        textureName += "," + currentImage;
        textureCount++;
    }

    currentImage = terrainDescriptor->getVariable("grassBillboard3");
    if (!currentImage.empty()) {
        textureName += "," + currentImage;
        textureCount++;
    }

    currentImage = terrainDescriptor->getVariable("grassBillboard4");
    if (!currentImage.empty()) {
        textureName += "," + currentImage;
        textureCount++;
    }

    if (textureCount == 0){
        return;
    }

    SamplerDescriptor grassSampler;
    grassSampler.setAnisotropy(0);
    grassSampler.setWrapMode(TextureWrap::CLAMP);
    grassSampler.toggleSRGBColourSpace(true);

    TextureDescriptor grassTexDescriptor(TextureType::TEXTURE_2D_ARRAY);
    grassTexDescriptor._layerCount = textureCount;
    grassTexDescriptor.setSampler(grassSampler);

    ResourceDescriptor textureDetailMaps("Vegetation Billboards");
    textureDetailMaps.setResourceLocation(terrainDescriptor->getVariable("grassMapLocation"));
    textureDetailMaps.setResourceName(textureName);
    textureDetailMaps.setPropertyDescriptor(grassTexDescriptor);
    Texture_ptr grassBillboardArray = CreateResource<Texture>(terrain->parentResourceCache(), textureDetailMaps);

    VegetationDetails& vegDetails = Attorney::TerrainLoader::vegetationDetails(*terrain);
    vegDetails.billboardCount = textureCount;
    vegDetails.name = terrain->getName() + "_grass";
    vegDetails.grassDensity = terrainDescriptor->getGrassDensity();
    vegDetails.grassScale = terrainDescriptor->getGrassScale();
    vegDetails.treeDensity = terrainDescriptor->getTreeDensity();
    vegDetails.treeScale = terrainDescriptor->getTreeScale();
    vegDetails.grassBillboards = grassBillboardArray;
    vegDetails.grassShaderName = "grass";
    vegDetails.parentTerrain = terrain;

    vegDetails.map.reset(new ImageTools::ImageData);
    ImageTools::ImageDataInterface::CreateImageData(
        terrainDescriptor->getVariable("grassMapLocation") + 
        terrainDescriptor->getVariable("grassMap"),
        *vegDetails.map);
}

bool TerrainLoader::Save(const char* fileName) { return true; }

bool TerrainLoader::Load(const char* filename) { return true; }
};

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

Diff revisions: vs.
Revision Author Commited Message
1004 Diff Diff IonutCava picture IonutCava Wed 20 Dec, 2017 17:16:32 +0000

[Ionut]
- More GUI cleanup (added multiple GUIButton events)
- Typedef vec4<U8> and vec4<F32> to UColour and FColour respectively for easier to read code.

950 Diff Diff IonutCava picture IonutCava Sun 22 Oct, 2017 22:25:05 +0000

[Ionut]
- Revamp camera management
- More work on improving the command buffer system:
— Performance tweaks
— Validation
— Debug features
- Some code cleanup for Object3D stuff (preRender/onRender changes)

Still haven’t fixed random flashing issue

947 Diff Diff IonutCava picture IonutCava Sun 08 Oct, 2017 22:36:54 +0000

[Ionut]
- Refactor command buffer pooling
- Remove all static commands in an attempt to fix rendering issues. Profile and rework later.

914 Diff Diff IonutCava picture IonutCava Thu 06 Jul, 2017 21:50:27 +0000

[Ionut]
- Terrain:
— Properly use terrain world matrix
— Make ToggleWireframe a compile time define since it’s rarely used
- Material:
— Do not assign ShaderProgram::defaultShader() to material shaderInfo slots by default (helps track bugs)
— Make sure we load shadow shaders in terrain and vegetation after pre-pass shader (to not override them)
- Wait for resources to fully load before unloading them

907 Diff Diff IonutCava picture IonutCava Wed 28 Jun, 2017 23:16:40 +0000

[Ionut]
- Rework texture loading, mip map generation, sampler usage, etc
- ResourceDescriptor for textures now ONLY takes a TextureDescriptor as a property descriptor. Sampler must be assigned to the tex descriptor:
— ToDo: Separate samplers completely from textures and assign them from a pool to textures at bind time

899 Diff Diff IonutCava picture IonutCava Fri 23 Jun, 2017 15:37:44 +0000

[IonutCava]
- Move sRGB conversion and fog calculation to post-processing stages (fog in postProcessing using the depth buffer and sRGB in the presentToScreen step)
- Split the bloom.glsl file into multiple shaders: bloom, tonemap and luminanceCalc for ease of use purposes
- Rework RenderBin sorting: sort translucent/transparent nodes by material in depth passes
- Sort order is no longer part of RenderBin for better control

894 Diff Diff IonutCava picture IonutCava Sun 18 Jun, 2017 19:58:02 +0000

[Ionut]
- Fix terrain sphere-based LoD tessellation.
- Merge all terrain texture layers into a single texture array system to free up texture slots and speed up lookups.

893 Diff Diff IonutCava picture IonutCava Sun 18 Jun, 2017 17:33:07 +0000

[Ionut]
- Initial implementation of a PipelineStateObject (holds shader program, rasterizer state, etc)
- Rework PCH implementation a bit because VS2017 no longer has a /ZM option

890 Diff Diff IonutCava picture IonutCava Sun 14 May, 2017 20:54:59 +0000

[Ionut]
- Add pre-compiled header support
- Initial code for per-stage tessellation computation

885 IonutCava picture IonutCava Mon 27 Mar, 2017 21:16:49 +0000

[Ionut]
- Match terrain collision mesh with rendered mesh