Terrain Generation
This commit is contained in:
@@ -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, 40, 0 };
|
||||
m_activeCamera->Position = { 0, 64, 0 };
|
||||
m_activeCamera->UpdateView();
|
||||
|
||||
std::shared_ptr<CBlockDictionary> BlockDictionary = CBlockDictionary::GetInstance();
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
#include "chunk.hpp"
|
||||
#include "voxel.hpp"
|
||||
|
||||
#include "../../renderer/shader.hpp"
|
||||
#include "../../renderer/camera.hpp"
|
||||
|
||||
#include "voxel.hpp"
|
||||
|
||||
#include "../block.hpp"
|
||||
|
||||
#include "../../util/fastnoise.hpp"
|
||||
|
||||
#include <random>
|
||||
|
||||
static std::default_random_engine generator;
|
||||
@@ -17,7 +18,7 @@ Chunk::Chunk() {
|
||||
|
||||
Chunk::Chunk(int x, int z) {
|
||||
|
||||
m_model = glm::translate(glm::mat4(1.0f), { x * CHUNK_WIDTH, 0, z * CHUNK_DEPTH });
|
||||
X = x, Z = z;
|
||||
|
||||
Load();
|
||||
|
||||
@@ -25,7 +26,7 @@ Chunk::Chunk(int x, int z) {
|
||||
|
||||
Chunk::Chunk(int x, int z, std::vector<uint8_t> voxels) {
|
||||
|
||||
m_model = glm::translate(glm::mat4(1.0f), { x * CHUNK_WIDTH, 0, z * CHUNK_DEPTH });
|
||||
X = x, Z = z;
|
||||
|
||||
Voxels = voxels;
|
||||
|
||||
@@ -33,16 +34,44 @@ 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;
|
||||
|
||||
for (x = 0; x < CHUNK_WIDTH; x++)
|
||||
for (int 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) {
|
||||
|
||||
Voxels.push_back((uint8_t)EBlockType::Grass);
|
||||
|
||||
} else {
|
||||
|
||||
Voxels.push_back((uint8_t)EBlockType::Air);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Load();
|
||||
|
||||
}
|
||||
|
||||
void Chunk::Load() {
|
||||
|
||||
if (Loaded)
|
||||
return;
|
||||
|
||||
m_model = glm::translate(glm::mat4(1.0f), { X * CHUNK_WIDTH, 0, Z * CHUNK_DEPTH });
|
||||
|
||||
if (!Voxels.empty()) {
|
||||
m_mesh();
|
||||
Loaded = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Generate a superflat chunk if nothing is there
|
||||
// [x + WIDTH * (y + HEIGHT * z)]
|
||||
for (int x = 0; x < CHUNK_WIDTH; x++)
|
||||
for (int y = 0; y < CHUNK_HEIGHT; y++)
|
||||
@@ -53,14 +82,6 @@ void Chunk::Load() {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::uniform_real_distribution<float> distribution(0, 1);
|
||||
float r = distribution(generator);
|
||||
|
||||
if (r > 0.9f) {
|
||||
Voxels.push_back((uint8_t)EBlockType::Air);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (y == 0)
|
||||
Voxels.push_back((uint8_t)EBlockType::Bedrock);
|
||||
else if (y < 28)
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
#define CHUNK_WIDTH 16
|
||||
#define CHUNK_DEPTH 16
|
||||
|
||||
class FastNoise;
|
||||
|
||||
class Camera;
|
||||
class Shader;
|
||||
|
||||
@@ -18,20 +20,17 @@ public:
|
||||
Chunk();
|
||||
Chunk(int x, int z);
|
||||
Chunk(int x, int z, std::vector<uint8_t> voxels);
|
||||
Chunk(int x, int z, std::shared_ptr<FastNoise> terrainGenerator);
|
||||
|
||||
void Load();
|
||||
|
||||
bool MeshReady = false;
|
||||
void UploadMesh();
|
||||
|
||||
bool MeshReady = false;
|
||||
|
||||
void Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> shader);
|
||||
|
||||
void Update(std::vector<uint8_t> voxels);
|
||||
|
||||
//bool Loaded = false;
|
||||
//bool Render = false;
|
||||
|
||||
uint8_t BlockAt(int x, int y, int z);
|
||||
|
||||
// Indexed sequentially [x + WIDTH * (y + HEIGHT * z)] = voxelID
|
||||
@@ -44,6 +43,9 @@ public:
|
||||
// To only be changed by render components
|
||||
bool ShouldRender = false;
|
||||
|
||||
// Chunk World pos
|
||||
int X,Z;
|
||||
|
||||
~Chunk();
|
||||
|
||||
private:
|
||||
|
||||
14
src/world/generator/chunkgenerator.cpp
Normal file
14
src/world/generator/chunkgenerator.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "../../util/fastnoise.hpp"
|
||||
|
||||
|
||||
void dp() {
|
||||
|
||||
FastNoise noise;
|
||||
noise.SetSeed(121212);
|
||||
|
||||
noise.SetNoiseType(FastNoise::SimplexFractal);
|
||||
|
||||
noise.SetFractalOctaves(3);
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "../renderer/shader.hpp"
|
||||
|
||||
#include "../config.hpp"
|
||||
#include "../util/fastnoise.hpp"
|
||||
|
||||
World::World() {
|
||||
|
||||
@@ -16,13 +17,21 @@ void World::LoadWorld() {
|
||||
m_shaders["Basic"]->Load(GameConfig.ResourceBase + "shaders/simple");
|
||||
m_shaders["Basic"]->Link();
|
||||
|
||||
for (int x = -4; x < 4; x++)
|
||||
m_noiseGenerator = std::make_shared<FastNoise>();
|
||||
m_noiseGenerator->SetSeed(rand());
|
||||
|
||||
m_noiseGenerator->SetNoiseType(FastNoise::SimplexFractal);
|
||||
|
||||
m_noiseGenerator->SetFractalOctaves(3);
|
||||
|
||||
for (int x = -4; x < 10; x++)
|
||||
for (int y = -4; y < 4; y++) {
|
||||
|
||||
m_chunkLoaderQueue.push({ x, y });
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
for (int i = 0; i < 7; i++) {
|
||||
|
||||
m_generatorThreads.push_back(std::thread([&]() {
|
||||
@@ -115,7 +124,7 @@ void World::m_loadChunks() {
|
||||
m_chunkMutex.unlock();
|
||||
|
||||
|
||||
std::shared_ptr<Chunk> loadingChunk = std::make_shared<Chunk>(coords.x, coords.y);
|
||||
std::shared_ptr<Chunk> loadingChunk = std::make_shared<Chunk>(coords.x, coords.y, m_noiseGenerator);
|
||||
std::cout << "Loaded chunk " << coords.x << ":" << coords.y << std::endl;
|
||||
|
||||
|
||||
@@ -130,6 +139,7 @@ void World::m_loadChunks() {
|
||||
while (m_chunkLoaderQueue.empty()) {
|
||||
|
||||
if (!m_generatorRunning) break;
|
||||
|
||||
static std::chrono::milliseconds dura(1);
|
||||
std::this_thread::sleep_for(dura);
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
|
||||
class FastNoise;
|
||||
|
||||
class Shader;
|
||||
|
||||
class World {
|
||||
@@ -61,6 +63,9 @@ private:
|
||||
|
||||
std::mutex m_chunkMutex;
|
||||
|
||||
// Generator
|
||||
std::shared_ptr<FastNoise> m_noiseGenerator;
|
||||
|
||||
|
||||
void m_loadChunks();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user