More dynamic terrain generation (Still doing it wrong i think)

This commit is contained in:
Ben Kyd
2019-11-05 14:23:03 +00:00
parent 573ad69ee4
commit 84cece0825
3 changed files with 63 additions and 10 deletions

View File

@@ -84,7 +84,7 @@ void Game::Setup(int w, int h) {
m_cameras["Default"] = std::make_shared<Camera>(w, h);
m_activeCamera = m_cameras["Default"];
m_activeCamera->Position = { 0, 64, 0 };
m_activeCamera->Position = { 0, 70, 0 };
m_activeCamera->UpdateView();
std::shared_ptr<CBlockDictionary> BlockDictionary = CBlockDictionary::GetInstance();

View File

@@ -37,20 +37,71 @@ Chunk::Chunk(int x, int z, std::vector<uint8_t> voxels) {
Chunk::Chunk(int x, int z, std::shared_ptr<FastNoise> terrainGenerator) {
X = x, Z = z;
int y;
for (x = 0; x < CHUNK_WIDTH; x++)
for (int y = 0; y < CHUNK_HEIGHT; y++)
for (y = 0; y < CHUNK_HEIGHT; y++)
for (z = 0; z < CHUNK_DEPTH; z++) {
if (pow(y / (float)CHUNK_HEIGHT, 1.1024f) + terrainGenerator->GetValueFractal(x + (Z * CHUNK_WIDTH), y, z + (X * CHUNK_DEPTH)) * 0.60f < 0.3f) {
if (y == 0) {
Voxels.push_back((uint8_t)EBlockType::Bedrock);
continue;
}
if (y == 1 && (float)rand() / (float)RAND_MAX > 0.5f) {
Voxels.push_back((uint8_t)EBlockType::Bedrock);
continue;
}
if (pow(y / (float)CHUNK_HEIGHT, 1.1024f) + terrainGenerator->GetValueFractal(x + (Z * CHUNK_WIDTH), y, z + (X * CHUNK_DEPTH)) * 0.60f < 0.5f) {
Voxels.push_back((uint8_t)EBlockType::Grass);
} else {
Voxels.push_back((uint8_t)EBlockType::Air);
continue;
}
Voxels.push_back((uint8_t)EBlockType::Air);
}
for (x = 0; x < CHUNK_WIDTH; x++)
for (y = 0; y < CHUNK_HEIGHT; y++)
for (z = 0; z < CHUNK_DEPTH; z++) {
if (BlockAt(x, y, z) == EBlockType::Bedrock)
continue;
// No need for bounds checking as a closed loop
if (BlockAt(x, y + 1, z) == EBlockType::Grass)
Voxels[x + CHUNK_WIDTH * (y + CHUNK_HEIGHT * z)] = EBlockType::Dirt;
}
// Add stone 3 layers below dirt
for (x = 0; x < CHUNK_WIDTH; x++)
for (y = 0; y < CHUNK_HEIGHT; y++)
for (z = 0; z < CHUNK_DEPTH; z++) {
if (BlockAt(x, y, z) == EBlockType::Bedrock)
continue;
if (BlockAt(x, y + 1, z) == EBlockType::Dirt)
if (BlockAt(x, y + 2, z) == EBlockType::Dirt)
// if (BlockAt(x, y + 3, z) == EBlockType::Dirt)
Voxels[x + CHUNK_WIDTH * (y + CHUNK_HEIGHT * z)] = EBlockType::Stone;
}
// Add the rest of the stone
for (x = 0; x < CHUNK_WIDTH; x++)
for (y = 0; y < CHUNK_HEIGHT; y++)
for (z = 0; z < CHUNK_DEPTH; z++) {
if (BlockAt(x, y, z) == EBlockType::Bedrock)
continue;
if (BlockAt(x, y + 1, z) == EBlockType::Stone)
Voxels[x + CHUNK_WIDTH * (y + CHUNK_HEIGHT * z)] = EBlockType::Stone;
}

View File

@@ -17,12 +17,14 @@ void World::LoadWorld() {
m_shaders["Basic"]->Load(GameConfig.ResourceBase + "shaders/simple");
m_shaders["Basic"]->Link();
srand(time(NULL));
m_noiseGenerator = std::make_shared<FastNoise>();
m_noiseGenerator->SetSeed(rand());
m_noiseGenerator->SetNoiseType(FastNoise::SimplexFractal);
m_noiseGenerator->SetFractalOctaves(3);
m_noiseGenerator->SetFractalOctaves(5);
for (int x = -4; x < 10; x++)
for (int y = -4; y < 4; y++) {
@@ -31,7 +33,7 @@ void World::LoadWorld() {
}
//
// Spawn generator threads
for (int i = 0; i < 7; i++) {
m_generatorThreads.push_back(std::thread([&]() {