From 7ef94f4e55a0b798eecbcc7166a80b8364b21ea4 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 23 Mar 2015 23:28:41 +0100 Subject: [PATCH] use 3 octaves for solid generation noise also, that other perlin implementation sucked noodles --- TODO | 2 -- src/generator.cpp | 6 +++--- src/generator.hpp | 2 +- src/noise.cpp | 5 ++--- src/noise.hpp | 24 ++++++++++++++++++++++++ 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/TODO b/TODO index 244e938..363c635 100644 --- a/TODO +++ b/TODO @@ -79,5 +79,3 @@ entity/world collision better noise current simplex noise implementation repeats itself pretty quickly - also there seems to be a (imo) better implementation here: - http://flafla2.github.io/2014/08/09/perlinnoise.html diff --git a/src/generator.cpp b/src/generator.cpp index 43d932c..dc83b89 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -8,7 +8,7 @@ namespace blank { Generator::Generator(const Config &config) : solidNoise(config.solid_seed) , typeNoise(config.type_seed) -, stretch(config.stretch) +, stretch(1.0f/config.stretch) , solid_threshold(config.solid_threshold) , space(0) , light(0) @@ -25,8 +25,8 @@ void Generator::operator ()(Chunk &chunk) const { 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); + glm::vec3 gen_pos = (coords + block_pos) * stretch; + float val = OctaveNoise(solidNoise, coords + block_pos, 3, 0.5f, stretch, 2.0f); if (val > solid_threshold) { int type_val = int((typeNoise(gen_pos) + 1.0f) * solids.size()) % solids.size(); chunk.SetBlock(block_pos, Block(solids[type_val])); diff --git a/src/generator.hpp b/src/generator.hpp index 3d7ceaf..70c38f5 100644 --- a/src/generator.hpp +++ b/src/generator.hpp @@ -17,7 +17,7 @@ public: unsigned int solid_seed = 0; unsigned int type_seed = 0; float stretch = 64.0f; - float solid_threshold = 0.8f; + float solid_threshold = 0.5f; }; explicit Generator(const Config &); diff --git a/src/noise.cpp b/src/noise.cpp index 6d59224..8b71614 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -45,10 +45,9 @@ SimplexNoise::SimplexNoise(unsigned int seed) { 0.0f, 1.0f, -1.0f }, { 0.0f, -1.0f, -1.0f }, }) { - unsigned int val = seed; + GaloisLFSR random(seed ^ 0x0123456789ACBDEF); for (size_t i = 0; i < 256; ++i) { - val = 2346765 * val + 6446345; - perm[i] = val % 256; + random(perm[i]); perm[i + 256] = perm[i]; } } diff --git a/src/noise.hpp b/src/noise.hpp index 166a60a..26e3766 100644 --- a/src/noise.hpp +++ b/src/noise.hpp @@ -35,6 +35,7 @@ private: }; + /// (3D only) adaptation of Stefan Gustavson's SimplexNoise java class class SimplexNoise { @@ -68,6 +69,29 @@ private: }; + +template +float OctaveNoise( + const Noise &noise, + const glm::vec3 &in, + int num, + float persistence, + float frequency = 1.0f, + float amplitude = 1.0f, + float growth = 2.0f +) { + float total = 0.0f; + float max = 0.0f; + for (int i = 0; i < num; ++i) { + total += noise(in * frequency) * amplitude; + max += amplitude; + amplitude *= persistence; + frequency *= growth; + } + + return total / max; +} + } #endif -- 2.39.2