summaryrefslogtreecommitdiff
path: root/src/chunk.cpp
blob: f9ddb54a4df6579e3d04c5ff48918ef77cd01aaa (plain)
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
#include "chunk.hpp"

namespace app {

//
//  CHUNK CONSTRUCTORS AND DECONSTUCTORS
//

Chunk::Chunk(int32_t gridX, int32_t gridZ, uint32_t world_seed) 
  : world_seed{world_seed},
    chunk_seed{(world_seed * gridX) + (world_seed * gridZ) / 2},
    gridX{gridX},
    gridZ{gridZ} {
  chunkMesh = nullptr;
  generate();
}

Chunk::~Chunk() {
  if(chunkMesh != nullptr)
    delete chunkMesh;
  if(worker.joinable())
    worker.join();
}

//
//  CHUNK CREATION AND DELETION
//

static std::map<std::pair<int32_t, int32_t>, Chunk*> chunks{};

Chunk* Chunk::newChunk(int32_t gridX, int32_t gridZ, uint32_t world_seed) {
  Chunk* chunk = new Chunk(gridX, gridZ, world_seed);
  chunks[{gridX, gridZ}] = std::move(chunk);
  return chunks[{gridX, gridZ}];
}

Chunk* Chunk::getChunk(int32_t gridX, int32_t gridZ) {
  if(chunks.count({gridX, gridZ})) {
    return chunks[{gridX, gridZ}];
  } else {
    return NULL;
  }
}

//
//  CHUNK TEXTURE AND BLOCK LOADING
//

static std::map<uint8_t, Block> blocks{};
static std::map<std::string, uint32_t> texturesIds{};
static std::vector<xe::Image*> textures{};

void loadTexture(const std::string& filePath) {
  xe::Image* image = xe::Engine::getInstance()->loadImageFromFile(filePath, false);
  texturesIds[filePath] = static_cast<uint32_t>(textures.size());
  textures.push_back(image);
}

uint32_t getTexture(const std::string& filePath) {
  return texturesIds[filePath];
}

std::vector<xe::Image*>& Chunk::getTextures() {
  return textures;
}

void Chunk::load() {
  loadTexture(DIRT_TEXTURE);
  loadTexture(GRASS_TEXTURE);
  loadTexture(GRASS_TOP_TEXTURE);
  blocks[DIRT] = {{getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE)}};
  blocks[GRASS] = {{getTexture(GRASS_TEXTURE), getTexture(GRASS_TEXTURE), getTexture(GRASS_TOP_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(GRASS_TEXTURE), getTexture(GRASS_TEXTURE)}};
}

void Chunk::unload() {
  for(const auto &image: textures) {
    delete image;
  }
  for(const auto &[key, chunk]: chunks) {
    delete chunk;
  }
  chunks.clear();
  textures.clear();
}

//
//  CHUNK MESH CREATION FOR BOTH SYNC AND ASYNC
//

void Chunk::createMeshAsync(Chunk* c) {
  if(c->working) return;
  c->worker = std::thread(createMesh, c);
}

void Chunk::createMesh(Chunk* c) {
  c->working = true;
  c->vertexData.data.clear();
  for(int32_t x=0;x<16;x++) {
    for(int32_t y=0; y<256; y++) {
      for(int32_t z=0; z<16; z++) {
        uint8_t block = c->getBlock(x,y,z);
        if(block == AIR) continue;
        if(c->getBlock(x+1,y,z) == AIR) {
          c->addVerticies(c, 0, x, y, z, block);
        }
        if(c->getBlock(x-1,y,z) == AIR) {
          c->addVerticies(c, 1, x, y, z, block);
        }
        if(c->getBlock(x,y+1,z) == AIR) {
          c->addVerticies(c, 2, x, y, z, block);
        }
        if(c->getBlock(x,y-1,z) == AIR) {
          c->addVerticies(c, 3, x, y, z, block);
        }
        if(c->getBlock(x,y,z+1) == AIR) {
          c->addVerticies(c, 4, x, y, z, block);
        }
        if(c->getBlock(x,y,z-1) == AIR) {
          c->addVerticies(c, 5, x, y, z, block);
        }
      }
    }
  }
  c->working = false;
  c->reloadRequired = true;
}

void Chunk::addVerticies(Chunk* c, uint8_t side, int32_t x, int32_t y, int32_t z, uint8_t block) {
  for(int i = 0; i < 6; i ++) {
    c->vertexData.write<float>(px[side * 6 + i][0] + x);
    c->vertexData.write<float>(px[side * 6 + i][1] + y);
    c->vertexData.write<float>(px[side * 6 + i][2] + z);
    c->vertexData.write<float>(nm[side][0]);
    c->vertexData.write<float>(nm[side][1]);
    c->vertexData.write<float>(nm[side][2]);
    c->vertexData.write<float>(uv[i][0]);
    c->vertexData.write<float>(uv[i][1]);
    c->vertexData.write<uint32_t>(static_cast<uint32_t>(blocks[block].textures[side]));
  }
}

//
//  CHUNK GETTERS AND SETTORS
//

xe::Model* Chunk::getMesh() {
  if(reloadRequired) {
    if(chunkMesh != nullptr)
      delete chunkMesh;
    if(worker.joinable())
      worker.join();
    xe::Model::Builder builder{};
    builder.vertexData = vertexData;
    builder.vertexSize = 36;
    chunkMesh = new xe::Model(xe::Engine::getInstance()->getDevice(), builder);
    reloadRequired = false;
  }
  return chunkMesh;
}

uint8_t Chunk::getBlock(int32_t x, int32_t y, int32_t z) {
  if(y > 256) return AIR;
  if(y < 0) return INVALID;
  int chunkX = gridX;
  int chunkZ = gridZ;
  if(x < 0) {
    chunkX--;
  } else if(x > 15) {
    chunkX++;
  }
  if(z < 0) {
    chunkZ--;
  } else if(z > 15) {
    chunkZ++;
  }
  x = (x+16)%16;
  z = (z+16)%16;
  if(chunkX == gridX && chunkZ == gridZ) {
    int index = x + (z * 16) + (y * 256);
    return cubes[index];
  } else {
    Chunk* chunk = getChunk(chunkX, chunkZ);
    if(chunk == NULL) {
      return INVALID;
    } else {
      int index = x + (z * 16) + (y * 256);
      return chunk->cubes[index];
    }
  }
}

void Chunk::setBlock(int32_t x, int32_t y, int32_t z, uint8_t block) {
  int index = x + (z * 16) + (y * 256);
  cubes[index] = block;
}

//
//  CHUNK GENERATION
//

void Chunk::generate() {
  cubes.resize(16*16*256);
  
  const PerlinNoise perlin{world_seed};

  for(int x = 0; x < 16; x++) {
    for(int z = 0; z < 16; z++) {
      int height = perlin.octave2D_01((( x + gridX * 16) * 0.01), ((z + gridZ * 16) * 0.01), 4) * 10;
      for(int y = 0; y < 256; y++) {
        if(y == height){
          setBlock(x, y, z, GRASS);
        } else if(y < height)
          setBlock(x, y, z, DIRT);
        else
          setBlock(x, y, z, AIR);
      }
    }
  }
}

}