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

#include "Graphs/Headers/SceneGraph.h"
#include "Core/Headers/ParamHandler.h"
#include "Core/Math/Headers/Transform.h"
#include "Managers/Headers/SceneManager.h"
#include "Geometry/Shapes/Headers/Mesh.h"
#include "Geometry/Material/Headers/Material.h"
#include "Environment/Vegetation/Headers/Vegetation.h"

namespace Divide {

U32 TerrainChunk::_chunkID = 0;

TerrainChunk::TerrainChunk(Terrain* const parentTerrain,
                           QuadtreeNode* const parentNode)
    : _parentNode(parentNode), _vegetation(nullptr) {
    _chunkID++;
    _xOffset = _yOffset = _sizeX = _sizeY = 0;
    _chunkIndOffset = 0;
    memset(_lodIndOffset, 0, Config::TERRAIN_CHUNKS_LOD * sizeof(U32));
    memset(_lodIndCount, 0, Config::TERRAIN_CHUNKS_LOD * sizeof(U32));

    _terrainVB = parentTerrain->getGeometryVB();

    VegetationDetails& vegDetails =
        TerrainChunkAttorney::vegetationDetails(*parentTerrain);
    vegDetails.name += "_chunk_" + std::to_string(_chunkID);
    _vegetation = MemoryManager_NEW Vegetation(
        vegDetails);  //<Deleted by the sceneGraph on "unload"
    _vegetation->renderState().useDefaultMaterial(false);
    _vegetation->setMaterialTpl(nullptr);
    assert(_vegetation != nullptr);
}

TerrainChunk::~TerrainChunk() {
    for (U8 i = 0; i < Config::TERRAIN_CHUNKS_LOD; i++) {
        _indice[i].clear();
    }

    memset(_lodIndOffset, 0, Config::TERRAIN_CHUNKS_LOD * sizeof(U32));
    memset(_lodIndCount, 0, Config::TERRAIN_CHUNKS_LOD * sizeof(U32));
    _chunkIndOffset = 0;
    _terrainVB = nullptr;
}

void TerrainChunk::Load(U8 depth, const vec2<U32>& pos, U32 minHMSize,
                        const vec2<U32>& HMsize, Terrain* const terrain) {
    _chunkIndOffset = _terrainVB->getIndexCount();

    _xOffset = (F32)pos.x;
    _yOffset = (F32)pos.y;
    _sizeX = (F32)minHMSize;
    _sizeY = (F32)minHMSize;

    for (U8 i = 0; i < Config::TERRAIN_CHUNKS_LOD; i++)
        ComputeIndicesArray(i, depth, pos, HMsize);

    const vectorImpl<vec3<F32>>& vertices = _terrainVB->getPosition();

    F32 tempMin = std::numeric_limits<F32>::max();
    F32 tempMax = std::numeric_limits<F32>::min();
    F32 height = 0.0f;
    for (U32 i = 0; i < _lodIndCount[0]; ++i) {
        U32 idx = _indice[0][i];
        if (idx == Config::PRIMITIVE_RESTART_INDEX_L) {
            continue;
        }
        height = vertices[idx].y;

        if (height > tempMax) {
            tempMax = height;
        }
        if (height < tempMin) {
            tempMin = height;
        }
    }

    _heightBounds.set(tempMin, tempMax);

    for (U8 i = 0; i < Config::TERRAIN_CHUNKS_LOD; i++) {
        _indice[i].clear();
    }

    _vegetation->initialize(this);

    TerrainChunkAttorney::registerTerrainChunk(*terrain, this);
}

void TerrainChunk::ComputeIndicesArray(I8 lod, U8 depth,
                                       const vec2<U32>& position,
                                       const vec2<U32>& heightMapSize) {
    assert(lod < Config::TERRAIN_CHUNKS_LOD);

    U32 offset = static_cast<U32>(std::pow(2.0f, static_cast<F32>(lod)));
    U32 div = static_cast<U32>(std::pow(2.0f, static_cast<F32>(depth + lod)));
    vec2<U32> heightmapDataSize = heightMapSize / (div);

    U32 nHMWidth = heightmapDataSize.x + 1;
    U32 nHMHeight = heightmapDataSize.y + 1;
    U32 nHMOffsetX = position.x;
    U32 nHMOffsetY = position.y;

    U32 nHMTotalWidth = heightMapSize.x;
    U32 nIndice = (nHMWidth) * (nHMHeight - 1) * 2;
    nIndice += (nHMHeight - 1);  //<Restart indices
    _indice[lod].reserve(nIndice);

    U32 iOffset = 0;
    U32 indexA = 0;
    U32 indexB = 0;
    U32 jOffset = 0;
    for (U16 j = 0; j < nHMHeight - 1; j++) {
        jOffset = j * (offset) + nHMOffsetY;
        for (U16 i = 0; i < nHMWidth; i++) {
            iOffset = i * (offset) + nHMOffsetX;
            indexA = iOffset + jOffset * nHMTotalWidth;
            indexB = iOffset + (jOffset + (offset)) * nHMTotalWidth;
            _indice[lod].push_back(indexA);
            _indice[lod].push_back(indexB);
            _terrainVB->addIndexL(indexA);
            _terrainVB->addIndexL(indexB);
        }
        _indice[lod].push_back(Config::PRIMITIVE_RESTART_INDEX_L);
        _terrainVB->addRestartIndex();
    }

    if (lod > 0) {
        _lodIndOffset[lod] =
            static_cast<U32>(_indice[lod - 1].size() + _lodIndOffset[lod - 1]);
    }

    _lodIndCount[lod] = (U32)_indice[lod].size();
    assert(nIndice == _lodIndCount[lod]);
}

void TerrainChunk::createDrawCommand(
    I8 lod, vectorImpl<GenericDrawCommand>& drawCommandsOut) {
    assert(lod < Config::TERRAIN_CHUNKS_LOD);
    if (lod > 0) {
        lod--;
    }
    GenericDrawCommand drawCommand(PrimitiveType::TRIANGLE_STRIP,
                                   _lodIndOffset[lod] + _chunkIndOffset,
                                   _lodIndCount[lod]);
    drawCommand.LoD(lod);
    drawCommandsOut.push_back(drawCommand);
}

U8 TerrainChunk::getLoD() const { return _parentNode->getLoD(); }
};

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

Diff revisions: vs.
Revision Author Commited Message
365 Diff Diff IonutCava picture IonutCava Sun 08 Mar, 2015 21:26:10 +0000

[Ionut]
- More enum-related type safety changes

364 Diff Diff IonutCava picture IonutCava Fri 06 Mar, 2015 16:55:21 +0000

[Ionut]
- Part 1 of type safe enum replacement (does not compile)

351 Diff Diff IonutCava picture IonutCava Wed 11 Feb, 2015 16:39:15 +0000

[Ionut]
- Large amount of decoupling between classes by using Attorney-style classes where friendship was required.

350 Diff Diff IonutCava picture IonutCava Tue 10 Feb, 2015 16:25:39 +0000

[Ionut]
- Simplified some Util-namespace level functions by using STL algorithms where possible
- Formatted the entire codebase using clang-format with the following style: "{BasedOnStyle: Google, IndentWidth: 4}"
- Attempted to make ProfileTimer thread-safe (temp fix until std::chrono works properly in Visual Studio)

343 Diff Diff IonutCava picture IonutCava Thu 15 Jan, 2015 17:30:12 +0000

[Ionut]
- Refactored 2 singletons into static classes (Console and TerrainLoader)
- Changed some C-casts to static_casts
- Updated copyright notices

333 Diff Diff IonutCava picture IonutCava Tue 09 Dec, 2014 16:15:44 +0000

[Ionut]
- Removed “core.h” and moved everything to their appropriate header files
- Renamed MathClasses to MathMatrices (finally)
- Renamed “-Inl.h” files to ".inl"
- Some String wrapper refactoring
- Update “InstallLibraries.bat” so that it still offers valid options

331 Diff Diff IonutCava picture IonutCava Sat 06 Dec, 2014 20:53:45 +0000

[Ionut]
- Limited line length to 132 characters to improve readability and diff-comparisons
- Refactored memory allocation/deallocation functions
- Fixed a few compatibility issues with HashMap.h
- Fixed a bug in GPU Skinning shaders (cast a float to int)

330 Diff Diff IonutCava picture IonutCava Fri 10 Oct, 2014 17:19:11 +0000

[Ionut]
- New rendering algorithm :
— Perform second pass for visible nodes gathering all render commands and uploading them to an indirect draw buffer
— Render geometry with indirect draw commands
— Removed per-node range binding of the node buffer. Instead, bind the entire buffer once and index data in shaders
— Use “baseInstance” parameter from IndirectDrawCommand as a node buffer index
— Prefer DSA extensions for updating buffer data where applicable
- Moved all rendering specific code from SceneGraphNode to a new RenderingComponent
- Optimized “toString” utility function

328 Diff Diff IonutCava picture IonutCava Sat 04 Oct, 2014 18:03:36 +0000

[Ionut]
- Added per SceneGraphNode material instances. (Cloned from the SceneNode’s material)
- Improved shader loading for materials

325 IonutCava picture IonutCava Wed 17 Sep, 2014 20:06:13 +0000

[Ionut]
- Reworked transform system:
— Parent transform chain system moved from Transform class to the PhysicsComponent
— PhysicsComponent now returns all transform values needed both global and local (still needs heavy optimization and caching)

- Reworked SceneGraph node management:
— Proper ref counting of SceneNodes and proper removing of resource (ResourceCache is now empty on destruction, as it should be)
— Removed parent transform tracking as that’s the PhysicsComponent’s responsibility
— Only nodes loaded via the ResourceCache are allowed to be added to the SceneGraph (added proper loaders for Impostors, Sky, etc)

- Optimized some of the math classes (mat4, mat3)