From: Daniel Karbach Date: Tue, 10 Mar 2015 17:40:06 +0000 (+0100) Subject: separate file for world generation X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=8acc3f990f1a5ee00471f72c150b407164149f2d;p=blank.git separate file for world generation --- diff --git a/src/generator.cpp b/src/generator.cpp new file mode 100644 index 0000000..10cb668 --- /dev/null +++ b/src/generator.cpp @@ -0,0 +1,38 @@ +#include "generator.hpp" + +#include + + +namespace blank { + +Generator::Generator(unsigned int seed) +: solidNoise(seed) +, typeNoise(seed + 1) +, stretch(64.0f) +, solid_threshold(0.8f) +, solids() { + +} + + +void Generator::operator ()(Chunk &chunk) const { + chunk.Allocate(); + Chunk::Pos pos(chunk.Position()); + glm::vec3 coords(pos * Chunk::Extent()); + for (int z = 0; z < Chunk::Depth(); ++z) { + for (int y = 0; y < Chunk::Height(); ++y) { + for (int x = 0; x < Chunk::Width(); ++x) { + Block::Pos block_pos(x, y, z); + glm::vec3 gen_pos = (coords + block_pos) / stretch; + float val = solidNoise(gen_pos); + if (val > solid_threshold) { + int type_val = int((typeNoise(gen_pos) + 1.0f) * solids.size()) % solids.size(); + chunk.BlockAt(block_pos) = Block(solids[type_val]); + } + } + } + } + chunk.Invalidate(); +} + +} diff --git a/src/generator.hpp b/src/generator.hpp new file mode 100644 index 0000000..a40184b --- /dev/null +++ b/src/generator.hpp @@ -0,0 +1,35 @@ +#ifndef BLANK_GENERATOR_HPP_ +#define BLANK_GENERATOR_HPP_ + +#include "block.hpp" +#include "chunk.hpp" +#include "noise.hpp" + +#include + + +namespace blank { + +class Generator { + +public: + explicit Generator(unsigned int seed); + + void operator ()(Chunk &) const; + + void Solids(const std::vector &s) { solids = s; } + +private: + SimplexNoise solidNoise; + SimplexNoise typeNoise; + + float stretch; + float solid_threshold; + + std::vector solids; + +}; + +} + +#endif diff --git a/src/world.cpp b/src/world.cpp index e041115..03e0ebb 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -11,8 +11,7 @@ World::World() , blockShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }}) , stairShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }}, { 0.0f, 0.0f }) , slabShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }}) -, blockNoise(0) -, colorNoise(1) +, generate(0) , player() , player_chunk(0, 0, 0) , loaded() @@ -31,6 +30,8 @@ World::World() blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &stairShape }); // blue stair blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &slabShape }); // blue slab + generate.Solids({ 1, 4, 7, 10 }); + player.Position({ 4.0f, 4.0f, 4.0f }); } @@ -59,7 +60,7 @@ void World::Generate(const Chunk::Pos &from, const Chunk::Pos &to) { } else if (x == 0 && y == 0 && z == 0) { loaded.emplace_back(blockType); loaded.back().Position(pos); - Generate(loaded.back()); + generate(loaded.back()); } else { to_generate.emplace_back(blockType); to_generate.back().Position(pos); @@ -70,34 +71,6 @@ void World::Generate(const Chunk::Pos &from, const Chunk::Pos &to) { to_generate.sort(ChunkLess); } -void World::Generate(Chunk &chunk) { - chunk.Allocate(); - Chunk::Pos pos(chunk.Position()); - glm::vec3 coords(pos * Chunk::Extent()); - if (pos.x == 0 && pos.y == 0 && pos.z == 0) { - for (size_t i = 1; i < blockType.Size(); ++i) { - chunk.BlockAt(i) = Block(i); - chunk.BlockAt(i + 257) = Block(i); - chunk.BlockAt(i + 514) = Block(i); - } - } else { - for (int z = 0; z < Chunk::Depth(); ++z) { - for (int y = 0; y < Chunk::Height(); ++y) { - for (int x = 0; x < Chunk::Width(); ++x) { - Block::Pos block_pos{float(x), float(y), float(z)}; - glm::vec3 gen_pos = (coords + block_pos) / 64.0f; - float val = blockNoise(gen_pos); - if (val > 0.8f) { - int col_val = int((colorNoise(gen_pos) + 1.0f) * 2.0f) % 4; - chunk.BlockAt(block_pos) = Block(col_val * 3 + 1); - } - } - } - } - } - chunk.Invalidate(); -} - bool World::Intersection( const Ray &ray, const glm::mat4 &M, @@ -175,13 +148,13 @@ Chunk &World::Next(const Chunk &to, const glm::tvec3 &dir) { chunk = ChunkQueued(tgt_pos); if (chunk) { - Generate(*chunk); + generate(*chunk); return *chunk; } loaded.emplace_back(blockType); loaded.back().Position(tgt_pos); - Generate(loaded.back()); + generate(loaded.back()); return loaded.back(); } @@ -225,7 +198,7 @@ void World::CheckChunkGeneration() { } if (!to_generate.empty()) { - Generate(to_generate.front()); + generate(to_generate.front()); loaded.splice(loaded.end(), to_generate, to_generate.begin()); } diff --git a/src/world.hpp b/src/world.hpp index 461da56..4823c47 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -4,7 +4,7 @@ #include "block.hpp" #include "chunk.hpp" #include "entity.hpp" -#include "noise.hpp" +#include "generator.hpp" #include "shader.hpp" #include "shape.hpp" @@ -44,17 +44,13 @@ public: void Render(DirectionalLighting &); -private: - void Generate(Chunk &); - private: BlockTypeRegistry blockType; CuboidShape blockShape; StairShape stairShape; CuboidShape slabShape; - SimplexNoise blockNoise; - SimplexNoise colorNoise; + Generator generate; Entity player; Chunk::Pos player_chunk;