Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
TerrainChunk.cpp
Go to the documentation of this file.
1
2
4#include "Headers/Terrain.h"
6
10
11namespace Divide {
12
14
15TerrainChunk::TerrainChunk(Terrain* const parentTerrain,
16 QuadtreeNode& parentNode) noexcept
17 : _id(_chunkID++)
18 , _quadtreeNode(parentNode)
19 , _parentTerrain(parentTerrain)
20{
21}
22
23void TerrainChunk::load(const U8 depth, const vec2<U32> pos, const U32 targetChunkDimension, const vec2<U32> HMSize, BoundingBox& bbInOut)
24{
25 _xOffset = to_F32(pos.x) - HMSize.x * 0.5f;
26 _yOffset = to_F32(pos.y) - HMSize.y * 0.5f;
27 _sizeX = _sizeY = to_F32(targetChunkDimension);
28
29 F32 tempMin = F32_MAX;
30 F32 tempMax = F32_LOWEST;
31
32 const U32 offset = to_U32(std::pow(2.0f, to_F32(0.0f)));
33 const U32 div = to_U32(std::pow(2.0f, to_F32(depth)));
34
35 const vec2<U32> heightmapDataSize = HMSize / div;
36 const U32 nHMWidth = heightmapDataSize.x;
37 const U32 nHMHeight = heightmapDataSize.y;
38
39 const vector<VertexBuffer::Vertex>& verts = _parentTerrain->_physicsVerts;
40
41 for (U16 j = 0; j < nHMHeight - 1; ++j) {
42 const U32 jOffset = j * offset+pos.y;
43 for (U16 i = 0; i < nHMWidth; ++i) {
44 const U32 iOffset = i * offset+pos.x;
45 U32 idx = iOffset + jOffset * HMSize.x;
46 F32 height = verts[idx]._position.y;
47
48 if (height > tempMax) {
49 tempMax = height;
50 }
51 if (height < tempMin) {
52 tempMin = height;
53 }
54
55
56 idx = iOffset + (jOffset + offset) * HMSize.x;
57 height = verts[idx]._position.y;
58
59 if (height > tempMax) {
60 tempMax = height;
61 }
62 if (height < tempMin) {
63 tempMin = height;
64 }
65 }
66 }
67
68 bbInOut.setMin(bbInOut.getMin().x, tempMin, bbInOut.getMin().z);
69 bbInOut.setMax(bbInOut.getMax().x, tempMax, bbInOut.getMax().z);
70
72}
73
74const BoundingBox& TerrainChunk::bounds() const noexcept
75{
77}
78
79U8 TerrainChunk::LoD() const noexcept
80{
81 return _quadtreeNode.LoD();
82}
83
85{
86 _quadtreeNode.drawBBox(context);
87}
88
90{
91 _vegetation = std::make_unique<VegetationInstance>( context, handle, this );
92 _vegetation->computeTransforms();
93}
94} //namespace Divide
static void registerTerrainChunk(Terrain &terrain, Divide::TerrainChunk *const chunk)
Definition: Terrain.h:202
void setMin(const vec3< F32 > &min) noexcept
const vec3< F32 > & getMax() const noexcept
const vec3< F32 > & getMin() const noexcept
void setMax(const vec3< F32 > &max) noexcept
Rough around the edges Adapter pattern abstracting the actual rendering API and access to the GPU.
Definition: GFXDevice.h:215
void drawBBox(GFXDevice &context)
U8 LoD() const noexcept
Definition: QuadtreeNode.h:83
const BoundingBox & getBoundingBox() const noexcept
Definition: QuadtreeNode.h:85
QuadtreeNode & _quadtreeNode
Definition: TerrainChunk.h:86
void initVegetation(PlatformContext &context, Handle< Vegetation > handle)
TerrainChunk(Terrain *parentTerrain, QuadtreeNode &parentNode) noexcept
const BoundingBox & bounds() const noexcept
void load(U8 depth, const vec2< U32 > pos, U32 targetChunkDimension, vec2< U32 > HMSize, BoundingBox &bbInOut)
Terrain * _parentTerrain
Definition: TerrainChunk.h:92
U8 LoD() const noexcept
void drawBBox(GFXDevice &context) const
VegetationInstance_uptr _vegetation
Definition: TerrainChunk.h:93
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr U32 to_U32(const T value)
uint8_t U8
constexpr F32 to_F32(const T value)
constexpr F32 F32_LOWEST
eastl::vector< Type > vector
Definition: Vector.h:42
constexpr F32 F32_MAX
uint16_t U16
uint32_t U32