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

#include "Headers/Vegetation.h"

#include "Managers/Headers/SceneManager.h"
#include "Graphs/Headers/SceneGraphNode.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Video/Headers/RenderStateBlock.h"
#include "Geometry/Material/Headers/Material.h"
#include "Environment/Terrain/Headers/Terrain.h"
#include "Environment/Terrain/Headers/TerrainChunk.h"
#include "Environment/Terrain/Quadtree/Headers/Quadtree.h"
#include "Environment/Terrain/Quadtree/Headers/QuadtreeNode.h"
#include "Platform/Video/Buffers/VertexBuffer/Headers/VertexBuffer.h"
#include "Platform/Video/Buffers/VertexBuffer/GenericBuffer/Headers/GenericVertexData.h"

namespace Divide {

bool Vegetation::_staticDataUpdated = false;

Vegetation::Vegetation(GFXDevice& context, ResourceCache& parentCache, size_t descriptorHash, U32 chunkID, const VegetationDetails& details)
    : SceneNode(parentCache, descriptorHash + chunkID, details.name + "_" + to_stringImpl(chunkID), SceneNodeType::TYPE_VEGETATION_GRASS),
      _context(context),
      _billboardCount(details.billboardCount),
      _grassDensity(details.grassDensity),
      _grassScale(details.grassScale),
      _treeScale(details.treeScale),
      _treeDensity(details.treeDensity),
      _grassBillboards(details.grassBillboards),
      _terrain(details.parentTerrain),
      _render(false),
      _success(false),
      _culledFinal(false),
      _shadowMapped(true),
      _terrainChunk(nullptr),
      _instanceCountGrass(0),
      _instanceCountTrees(0),
      _grassStateBlockHash(0),
      _stateRefreshIntervalBufferUS(0ULL),
      _stateRefreshIntervalUS(Time::SecondsToMicroseconds(1))  ///<Every second?
{
    _threadedLoadComplete = false;
    _stopLoadingRequest = false;
    _readBuffer = 1;
    _writeBuffer = 0;
    _parentLoD = 0;

    _map = details.map;
    _grassShaderName = details.grassShaderName;
    _grassGPUBuffer[0] = _context.newGVD(3);
    _grassGPUBuffer[1] = _context.newGVD(3);
    _treeGPUBuffer[0] = _context.newGVD(1);
    _treeGPUBuffer[1] = _context.newGVD(1);

    _cullDrawCommand = GenericDrawCommand(PrimitiveType::API_POINTS, 0, 1);

    _instanceRoutineIdx.fill(0);

    auto setShaderData = [this](Resource_wptr res) {
        ShaderProgram_ptr shader = std::dynamic_pointer_cast<ShaderProgram>(res.lock());
        _instanceRoutineIdx[to_base(CullType::PASS_THROUGH)] = shader->GetSubroutineIndex(ShaderType::VERTEX, "PassThrough");
        _instanceRoutineIdx[to_base(CullType::INSTANCE_CLOUD_REDUCTION)] = shader->GetSubroutineIndex(ShaderType::VERTEX, "InstanceCloudReduction");
        _instanceRoutineIdx[to_base(CullType::HI_Z_CULL)] = shader->GetSubroutineIndex(ShaderType::VERTEX, "HiZOcclusionCull");
    };

    ResourceDescriptor instanceCullShader("instanceCull");
    instanceCullShader.setThreadedLoading(true);
    instanceCullShader.setID(3);
    instanceCullShader.setOnLoadCallback(setShaderData);
    _cullShader = CreateResource<ShaderProgram>(parentCache, instanceCullShader);
}

Vegetation::~Vegetation()
{
    Console::printfn(Locale::get(_ID("UNLOAD_VEGETATION_BEGIN")), getName().c_str());
    _stopLoadingRequest = true;
    U32 timer = 0;
    while (!_threadedLoadComplete) {
        // wait for the loading thread to finish first;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
        timer += 10;
        if (timer > 3000) {
            break;
        }
    }
    assert(_threadedLoadComplete);
    Console::printfn(Locale::get(_ID("UNLOAD_VEGETATION_END")));
}

void Vegetation::initialize(TerrainChunk* const terrainChunk) {
    assert(terrainChunk);
    assert(_map->data() != nullptr);

    _terrainChunk = terrainChunk;

    RenderStateBlock transparentRenderState;
    transparentRenderState.setCullMode(CullMode::CW);
    _grassStateBlockHash = transparentRenderState.getHash();

    ResourceDescriptor vegetationMaterial("vegetationMaterial" + getName());
    Material_ptr vegMaterial = CreateResource<Material>(_parentCache, vegetationMaterial);

    //vegMaterial->setShaderLoadThreaded(false);
    vegMaterial->setDiffuse(DefaultColours::WHITE);
    vegMaterial->setSpecular(FColour(0.1f, 0.1f, 0.1f, 1.0f));
    vegMaterial->setShininess(5.0f);
    vegMaterial->setShadingMode(Material::ShadingMode::BLINN_PHONG);
    vegMaterial->setShaderDefines("SKIP_TEXTURES");
    vegMaterial->setShaderProgram(_grassShaderName, RenderStage::DISPLAY, true);
    vegMaterial->setShaderProgram(_grassShaderName + ".PrePass", RenderPassType::DEPTH_PASS, true);
    vegMaterial->setShaderProgram(_grassShaderName + ".Shadow", RenderStage::SHADOW, true);
    vegMaterial->setTexture(ShaderProgram::TextureUsage::UNIT0, _grassBillboards);
    vegMaterial->dumpToFile(false);
    setMaterialTpl(vegMaterial);

    CreateTask(_context.parent().platformContext(),
               DELEGATE_BIND(&Vegetation::generateGrass, this, std::placeholders::_1),
               DELEGATE_BIND(&Vegetation::uploadGrassData, this))
               .startTask(Task::TaskPriority::LOW);
    setState(ResourceState::RES_LOADED);
}

namespace {
enum class BufferUsage : U32 {
    UnculledPositionBuffer = 0,
    CulledPositionBuffer = 1,
    UnculledSizeBuffer = 2,
    CulledSizeBuffer = 3,
    CulledInstanceBuffer = 4,
    COUNT
};

const U32 posLocation = 10;
const U32 scaleLocation = 11;
const U32 instLocation = 12;
const U32 instanceDiv = 1;
};

void Vegetation::uploadGrassData() {
    if (_grassPositions.empty()) {
        _threadedLoadComplete = true;
        return;
    }

    static const vec2<F32> pos000(cosf(Angle::to_RADIANS(0.000f)),
                                  sinf(Angle::to_RADIANS(0.000f)));
    static const vec2<F32> pos120(cosf(Angle::to_RADIANS(120.0f)),
                                  sinf(Angle::to_RADIANS(120.0f)));
    static const vec2<F32> pos240(cosf(Angle::to_RADIANS(240.0f)),
                                  sinf(Angle::to_RADIANS(240.0f)));

    static const vec3<F32> vertices[] = {vec3<F32>(-pos000.x, 0.0f, -pos000.y),
                                         vec3<F32>(-pos000.x, 1.0f, -pos000.y),
                                         vec3<F32>(pos000.x, 1.0f, pos000.y),
                                         vec3<F32>(pos000.x, 0.0f, pos000.y),

                                         vec3<F32>(-pos120.x, 0.0f, -pos120.y),
                                         vec3<F32>(-pos120.x, 1.0f, -pos120.y),
                                         vec3<F32>(pos120.x, 1.0f, pos120.y),
                                         vec3<F32>(pos120.x, 0.0f, pos120.y),

                                         vec3<F32>(-pos240.x, 0.0f, -pos240.y),
                                         vec3<F32>(-pos240.x, 1.0f, -pos240.y),
                                         vec3<F32>(pos240.x, 1.0f, pos240.y),
                                         vec3<F32>(pos240.x, 0.0f, pos240.y)};

    static const U32 indices[] = {2, 1, 0, 2, 0, 1, 2, 0, 3, 2, 3, 0};

    static const vec2<F32> texcoords[] = {vec2<F32>(0.0f, 0.99f),
                                          vec2<F32>(0.0f, 0.01f),
                                          vec2<F32>(1.0f, 0.01f),
                                          vec2<F32>(1.0f, 0.99f)};



    if (_grassBlades.empty()) {
        U32 indexOffset = 0;
        for (U8 j = 0; j < 3; ++j) {
            indexOffset = (j * 4);
            for (U8 l = 0; l < 12; ++l) {
                _grassBlades.push_back(vertices[indices[l] + indexOffset]);
                _texCoord.push_back(texcoords[(indices[l] + indexOffset) % 4]);
            }
        }
    }

    if (_rotationMatrices.empty()) {
        vectorImpl<F32> angles;
        angles.resize(18, 0.0f);
        for (U8 i = 0; i < 18; ++i) {
            F32 temp = Random(360.0f);
            while (std::find(std::begin(angles), std::end(angles), temp) !=
                   std::end(angles)) {
                temp = Random(360.0f);
            }
            angles[i] = temp;
        }

        mat3<F32> temp;
        for (U8 i = 0; i < 18; ++i) {
            temp.identity();
            temp.fromYRotation(angles[i]);
            _rotationMatrices.push_back(temp);
        }
    }

    for (U8 i = 0; i < 2; ++i) {
        GenericVertexData* buffer = _grassGPUBuffer[i];

        buffer->create(to_U8(BufferUsage::COUNT), 3);
        // position culled will be generated using transform feedback using
        // shader output 'posLocation'
        // writing to buffer "CulledPositionBuffer"
        buffer->setFeedbackBuffer(to_base(BufferUsage::CulledPositionBuffer), 0);
        buffer->setFeedbackBuffer(to_base(BufferUsage::CulledSizeBuffer), 1);
        buffer->setFeedbackBuffer(to_base(BufferUsage::CulledInstanceBuffer), 2);

        buffer->setBuffer(to_base(BufferUsage::UnculledPositionBuffer), _instanceCountGrass,
                          sizeof(vec4<F32>), true, &_grassPositions[0], BufferUpdateFrequency::OCASSIONAL);
        buffer->setBuffer(to_base(BufferUsage::UnculledSizeBuffer), _instanceCountGrass,
                          sizeof(F32), true, &_grassScales[0], BufferUpdateFrequency::OCASSIONAL);
        buffer->setBuffer(to_base(BufferUsage::CulledPositionBuffer), _instanceCountGrass * 3,
                          sizeof(vec4<F32>), true, NULL, BufferUpdateFrequency::OCASSIONAL);
        buffer->setBuffer(to_base(BufferUsage::CulledSizeBuffer), _instanceCountGrass * 3,
                          sizeof(F32), true, NULL, BufferUpdateFrequency::OCASSIONAL);
        buffer->setBuffer(to_base(BufferUsage::CulledInstanceBuffer), _instanceCountGrass * 3,
                          sizeof(I32), true, NULL, BufferUpdateFrequency::OCASSIONAL);

        buffer->attribDescriptor(posLocation)
            .set(to_base(BufferUsage::CulledPositionBuffer),
                 4,
                 GFXDataFormat::FLOAT_32,
                 false,
                 0,
                 instanceDiv);

        buffer->attribDescriptor(scaleLocation)
            .set(to_base(BufferUsage::CulledSizeBuffer),
                 1,
                 GFXDataFormat::FLOAT_32,
                 false,
                 0,
                 instanceDiv);

        buffer->attribDescriptor(instLocation)
            .set(to_base(BufferUsage::CulledInstanceBuffer), 
                 1,
                 GFXDataFormat::SIGNED_INT,
                 false,
                 0,
                 instanceDiv);
        buffer->fdbkAttribDescriptor(posLocation)
            .set(to_base(BufferUsage::UnculledPositionBuffer),
                 4,
                 GFXDataFormat::FLOAT_32,
                 false,
                 0,
                 instanceDiv);
        buffer->fdbkAttribDescriptor(scaleLocation)
            .set(to_base(BufferUsage::UnculledSizeBuffer),
                 1,
                 GFXDataFormat::FLOAT_32,
                 false,
                 0,
                 instanceDiv);

        buffer->toggleDoubleBufferedQueries(false);
    }

    _grassPositions.clear();
    _grassScales.clear();

    _render = _threadedLoadComplete = true;
}

void Vegetation::sceneUpdate(const U64 deltaTimeUS,
                             SceneGraphNode& sgn,
                             SceneState& sceneState) {
    static const Task updateTask;

    const SceneRenderState& renderState = sceneState.renderState();
    if (_threadedLoadComplete && !_success) {
        generateTrees(updateTask);
        Camera::addUpdateListener([this, &renderState](const Camera& cam) {
            gpuCull(renderState, cam);
        });
        _success = true;
    }

    if (_render && _success) {
        // Query shadow state every "_stateRefreshInterval" microseconds
        if (_stateRefreshIntervalBufferUS >= _stateRefreshIntervalUS) {
            if (!_staticDataUpdated) {
                _windX = sceneState.windDirX();
                _windZ = sceneState.windDirZ();
                _windS = sceneState.windSpeed();
                _stateRefreshIntervalBufferUS -= _stateRefreshIntervalUS;
                _staticDataUpdated = true;
            }
        }
        _stateRefreshIntervalBufferUS += deltaTimeUS;

        _writeBuffer = (_writeBuffer + 1) % 2;
        _readBuffer = (_writeBuffer + 1) % 2;
        _culledFinal = false;
    }

    SceneNode::sceneUpdate(deltaTimeUS, sgn, sceneState);
}

U32 Vegetation::getQueryID() {
    switch (_context.getRenderStage().stage()) {
        case RenderStage::SHADOW:
            return 0;
        case RenderStage::REFLECTION:
            return 1;
        case RenderStage::REFRACTION:
            return 2;
        default:
            return 3;
    };
}

void Vegetation::gpuCull(const SceneRenderState& sceneRenderState, const Camera& cam) {
    U32 queryID = getQueryID();

    bool draw = false;
    switch (queryID) {
        case 0 /*SHADOW*/: {
            draw = _context.getRenderStage() != _context.getPrevRenderStage();
            _culledFinal = false;
        } break;
        case 1 /*REFLECTION*/: {
            draw = true;
            _culledFinal = false;
        } break;
        default: {
            draw = !_culledFinal;
            _culledFinal = true;
        } break;
    }
    _parentLoD = _terrainChunk->getLoD(cam.getEye());
    if (draw && _threadedLoadComplete && _parentLoD == 0) {
        GenericVertexData* buffer = _grassGPUBuffer[_writeBuffer];
        //_cullShader->SetSubroutine(VERTEX,
        //_instanceRoutineIdx[HI_Z_CULL]);

        buffer->bindFeedbackBufferRange(to_base(BufferUsage::CulledPositionBuffer),
                                        _instanceCountGrass * queryID,
                                        _instanceCountGrass);
        buffer->bindFeedbackBufferRange(to_base(BufferUsage::CulledSizeBuffer),
                                        _instanceCountGrass * queryID,
                                        _instanceCountGrass);
        buffer->bindFeedbackBufferRange(to_base(BufferUsage::CulledInstanceBuffer),
                                        _instanceCountGrass * queryID,
                                        _instanceCountGrass);

        PipelineDescriptor pipeDesc;
        pipeDesc._shaderProgram = _cullShader;

        _cullDrawCommand.cmd().primCount = _instanceCountGrass;
        _cullDrawCommand.enableOption(GenericDrawCommand::RenderOptions::RENDER_NO_RASTERIZE);
        _cullDrawCommand.drawToBuffer(to_U8(queryID));
        _cullDrawCommand.sourceBuffer(buffer);
        buffer->incQueryQueue();

        GFX::ScopedCommandBuffer sBuffer = GFX::allocateScopedCommandBuffer();
        GFX::CommandBuffer& cmdBuffer = sBuffer();

        Texture_ptr depthTex = _context.renderTargetPool().renderTarget(RenderTargetID(RenderTargetUsage::SCREEN)).getAttachment(RTAttachmentType::Depth, 0).texture();

        GFX::BindDescriptorSetsCommand descriptorSetCmd;
        descriptorSetCmd._set._textureData.addTexture(depthTex->getData(), to_U8(ShaderProgram::TextureUsage::UNIT0));
        GFX::BindDescriptorSets(cmdBuffer, descriptorSetCmd);

        GFX::BindPipelineCommand pipelineCmd;
        pipelineCmd._pipeline = &_context.newPipeline(pipeDesc);
        GFX::BindPipeline(cmdBuffer, pipelineCmd);

        GFX::SendPushConstantsCommand pushConstantsCommand;
        pushConstantsCommand._constants.set("ObjectExtent", PushConstantType::VEC3, vec3<F32>(1.0f, 1.0f, 1.0f));
        pushConstantsCommand._constants.set("dvd_visibilityDistance", PushConstantType::FLOAT, sceneRenderState.grassVisibility());
        pushConstantsCommand._constants.set("cullType", PushConstantType::UINT, /*queryID*/ to_base(CullType::INSTANCE_CLOUD_REDUCTION));
        GFX::SendPushConstants(cmdBuffer, pushConstantsCommand);

        GFX::DrawCommand drawCmd;
        drawCmd._drawCommands.push_back(_cullDrawCommand);
        GFX::AddDrawCommands(cmdBuffer, drawCmd);

        _context.flushAndClearCommandBuffer(cmdBuffer);

        //_cullDrawCommand.setInstanceCount(_instanceCountTrees);
        //_cullDrawCommand.sourceBuffer(_treeGPUBuffer);
    }
}

void Vegetation::buildDrawCommands(SceneGraphNode& sgn,
                                   const RenderStagePass& renderStagePass,
                                   RenderPackage& pkgInOut) {

    GenericDrawCommand cmd;
    cmd.primitiveType(PrimitiveType::TRIANGLE_STRIP);
    cmd.cmd().firstIndex = 0;
    cmd.cmd().indexCount = 12 * 3;
    cmd.LoD(1);
    GFX::DrawCommand drawCommand;
    drawCommand._drawCommands.push_back(cmd);
    pkgInOut.addDrawCommand(drawCommand);

    const Pipeline* pipeline = pkgInOut.pipeline(0);
    PipelineDescriptor pipeDesc = pipeline->descriptor();
    pipeDesc._stateHash = _grassStateBlockHash;
    pkgInOut.pipeline(0, _context.newPipeline(pipeDesc));

    PushConstants constants = pkgInOut.pushConstants(0);
    constants.set("grassScale", PushConstantType::FLOAT, 1.0f);
    constants.set("positionOffsets", PushConstantType::VEC3, _grassBlades);
    constants.set("texCoordOffsets", PushConstantType::VEC2, _texCoord);
    constants.set("rotationMatrices", PushConstantType::MAT3, _rotationMatrices, true);
    constants.set("lod_metric", PushConstantType::FLOAT, 100.0f);
    pkgInOut.pushConstants(0, constants);

    SceneNode::buildDrawCommands(sgn, renderStagePass, pkgInOut);
}

bool Vegetation::onRender(SceneGraphNode& sgn,
                          const SceneRenderState& sceneRenderState,
                          const RenderStagePass& renderStagePass) {
    RenderingComponent* renderComp = sgn.get<RenderingComponent>();
    RenderPackage& pkg = renderComp->getDrawPackage(renderStagePass);

    ACKNOWLEDGE_UNUSED(sceneRenderState);
    GenericVertexData* buffer = _grassGPUBuffer[_readBuffer];
    U32 queryID = getQueryID();
    gpuCull(sceneRenderState, *sceneRenderState.parentScene().playerCamera());

    buffer->attribDescriptor(posLocation).strideInBytes(sizeof(vec4<F32>) * _instanceCountGrass * queryID);
    buffer->attribDescriptor(scaleLocation).strideInBytes(sizeof(F32) * _instanceCountGrass * queryID);
    buffer->attribDescriptor(instLocation).strideInBytes(sizeof(I32) * _instanceCountGrass * queryID);

    GenericDrawCommand cmd = pkg.drawCommand(0, 0);
    cmd.cmd().primCount = buffer->getFeedbackPrimitiveCount(to_U8(queryID));
    cmd.sourceBuffer(buffer);
    pkg.drawCommand(0, 0, cmd);
    
    _staticDataUpdated = false;
    return !(!_render || !_success || !_threadedLoadComplete ||
             _parentLoD > 0 ||
             (renderStagePass == _context.getPrevRenderStage() &&
              renderStagePass.stage() == RenderStage::SHADOW));
}

void Vegetation::generateTrees(const Task& parentTask) {
}

void Vegetation::generateGrass(const Task& parentTask) {
    
    //const vec2<F32>& chunkPos = _terrainChunk->getOffsetAndSize().xy();
    const vec2<F32>& chunkSize = _terrainChunk->getOffsetAndSize().zw();
    const F32 waterLevel = _terrainChunk->waterHeight(); //< ToDo: make this dynamic! (cull underwater points later on?)
    const I32 currentCount = std::min((I32)_billboardCount, 4);
    const U16 mapWidth = _map->dimensions().width;
    const U16 mapHeight = _map->dimensions().height;
    const U32 grassElements = to_U32(_grassDensity * chunkSize.x * chunkSize.y);

    Console::printfn(Locale::get(_ID("CREATE_GRASS_BEGIN")), grassElements);

    STUBBED("We really need blue noise or casey muratori's special circle/hex for a nice distribution. Use Poisson disk sampling and optimise from there? -Ionut");
    /*
    ref: https://github.com/corporateshark/poisson-disk-generator/blob/master/Poisson.cpp
    ref: http://yutingye.info/OtherProjects_files/report.pdf
    ref: http://mollyrocket.com/casey/stream_0016.html
    THIS:

    generate the first random point p0
    insert p0 into active list
    place the index of p0 (zero) to the background grid cell
    while active list is not empty do
        choose a random point p from active list
        for attemp = 1:maxAttemp
            get new sample p? around p between r and 2r
            for each non-empty neighbor cell around p?
                if p? is closer than r
                    break
                end if
            end for
            if p? is far from all neighbors
                insert p? to active list
                place the index of p? to background grid cell
                break
            end if
        end for
        if maxAttemp exceed
            remove p from active list
        end if
    end while
    return

    OR THIS:
    real32 PointRadius = 0.025f;
    v2 Ac = { -2, -2 }; // Center of circle A
    real32 ArBase = 1.0f; // Starting radius of circle A
    v2 Bc = { -2, 2 }; // Center of circle B
    real32 BrBase = 1.0f; // Starting radius of circle B
    real32 dR = 2.5f*PointRadius; // Distance between concentric rings
    for (int32x RadiusStepA = 0; RadiusStepA < 128; ++RadiusStepA) {
        real32 Ar = ArBase + dR*(real32)RadiusStepA;
        for (int32x RadiusStepB = 0; RadiusStepB < 128; ++RadiusStepB) {
            real32 Br = BrBase + dR*(real32)RadiusStepB;
            real32 UseAr = Ar + ((RadiusStepB % 3) ? 0.0f : 0.3f*dR);
            real32 UseBr = Br + ((RadiusStepA % 3) ? 0.0f : 0.3f*dR);

            // Intersect circle Ac,UseAr and Bc,UseBr
            // Add the resulting points if they are within the pattern bounds
            // (the bounds were [-1,1] on both axes for all prior screenshots)
        }
    }
    */
    /*
    _grassPositions.reserve(grassElements);
    F32 densityFactor = 1.0f / _grassDensity;
#pragma omp parallel for
    for (I32 index = 0; index < currentCount; ++index) {
        densityFactor += 0.1f;
        for (F32 width = 0; width < chunkSize.x - densityFactor;
             width += densityFactor) {
            for (F32 height = 0; height < chunkSize.y - densityFactor;
                 height += densityFactor) {
                if (_stopLoadingRequest || stopRequested) {
                    continue;
                }
                F32 x = width + Random(densityFactor) + chunkPos.x;
                F32 y = height + Random(densityFactor) + chunkPos.y;
                CLAMP<F32>(x, 0.0f, to_F32(mapWidth) - 1.0f);
                CLAMP<F32>(y, 0.0f, to_F32(mapHeight) - 1.0f);
                F32 x_fac = x / _map->dimensions().width;
                F32 y_fac = y / _map->dimensions().height;

                I32 map_colour = _map->getColour((U16)x, (U16)y)[index];
                if (map_colour < 150) {
                    continue;
                }
                const vec3<F32>& P = _terrain->getPosition(x_fac, y_fac);
                if (P.y < waterLevel) {
                    continue;
                }
                const vec3<F32>& N = _terrain->getNormal(x_fac, y_fac);
                if (N.y < 0.7f) {
                    continue;
                }
#pragma omp critical
                {
                    position.set(P);
                    //mat4<F32> matRot1;
                    //matRot1.scale(vec3<F32>(grassScale));
                    //mat4<F32> matRot2;
                    //matRot2.fromYRotation(Random(360.0f));
                    //_grassMatricesTemp.push_back(matRot1 * matRot2 *
                    //rotationFromVToU(WORLD_Y_AXIS, N).getMatrix());

                    _grassPositions.push_back(vec4<F32>(P, to_F32(index)));
                    _grassScales.push_back(((map_colour + 1) / 256.0f));
                    _instanceCountGrass++;
                }
            }
        }
    }
    */
    Console::printfn(Locale::get(_ID("CREATE_GRASS_END")));
}
};

Commits for Divide-Framework/trunk/Source Code/Environment/Vegetation/Vegetation.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.

1001 Diff Diff IonutCava picture IonutCava Sun 17 Dec, 2017 20:55:45 +0000

[Ionut]
- Fix IMGUI rendering with GenericVertexData
- Fix persistent buffer locking issue (lock after every draw call, even if it overlaps)

999 Diff Diff IonutCava picture IonutCava Thu 14 Dec, 2017 00:27:08 +0000

[Ionut]
- Simply GenericVertexData class a lot
- Move IMGUI to GenericVertexData based rendering (still has rendering issues)
- Fix some crash issues on shutdown if the rendering API failed to initialize
- Disable some GL debugging flags as they seem to cause issues with the new 17.12.1 AMD driver

968 Diff Diff IonutCava picture IonutCava Tue 07 Nov, 2017 17:32:31 +0000

[Ionut]
- Consistent naming between timing variables

953 Diff Diff IonutCava picture IonutCava Sat 28 Oct, 2017 21:57:10 +0000

[Ionut]
- Fix blinking issue by caching and hashing pipelines to that we don’t overwrite pipelines used by other elements

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

949 Diff Diff IonutCava picture IonutCava Thu 19 Oct, 2017 21:48:37 +0000

[Ionut]
- More command buffer refactoring

948 Diff Diff IonutCava picture IonutCava Tue 17 Oct, 2017 22:41:09 +0000

[Ionut]
- Move rendering data caching outside of CommandBuffer and into RenderPackage for better control.

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.

943 IonutCava picture IonutCava Tue 12 Sep, 2017 11:38:52 +0000

[IonutCava]
- Rework ThreadPool/Task system for clearer parent<->child relationship between tasks
— Disable Prio pool since it doesn’t work properly with the newest boost libraries
— Modify the C++11 threadpool to remove the boost dependency
- Reduce memory usage by allocating command buffers on use instead of on creation