From 1121e49e7cad94ce4efd0a02c72a44469c81494e Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Mon, 1 Jun 2015 10:04:30 +0200 Subject: [PATCH] be smart about other stuff ^^ --- src/chunk.cpp | 2 +- src/noise.cpp | 115 +++++++++++++++++++++++++++++--------------------- src/noise.hpp | 14 +++--- 3 files changed, 76 insertions(+), 55 deletions(-) diff --git a/src/chunk.cpp b/src/chunk.cpp index c31926b..f172143 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -471,7 +471,7 @@ Block::FaceSet Chunk::Obstructed(int idx) const noexcept { } glm::mat4 Chunk::ToTransform(int idx) const noexcept { - return glm::translate(glm::mat4(1.0f), ToCoords(idx)) * blocks[idx].Transform(); + return glm::translate(ToCoords(idx)) * blocks[idx].Transform(); } diff --git a/src/noise.cpp b/src/noise.cpp index c66153b..d653d5d 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -46,8 +46,10 @@ SimplexNoise::SimplexNoise(unsigned int seed) noexcept { 0.0f, -1.0f, -1.0f }, }) { GaloisLFSR random(seed ^ 0x0123456789ACBDEF); + unsigned char value; for (size_t i = 0; i < 256; ++i) { - random(perm[i]); + perm[i] = random(value); + perm[i] &= 0xFF; perm[i + 256] = perm[i]; perm12[i] = perm[i] % 12; perm12[i + 256] = perm12[i]; @@ -62,73 +64,92 @@ float SimplexNoise::operator ()(const glm::vec3 &in) const noexcept { float tr = (skewed.x + skewed.y + skewed.z) * one_sixth; glm::vec3 unskewed(skewed - tr); - glm::vec3 offset[4]; - offset[0] = in - unskewed; + glm::vec3 relative(in - unskewed); glm::vec3 second, third; - if (offset[0].x >= offset[0].y) { - if (offset[0].y >= offset[0].z) { - second = { 1.0f, 0.0f, 0.0f }; - third = { 1.0f, 1.0f, 0.0f }; - } else if (offset[0].x >= offset[0].z) { - second = { 1.0f, 0.0f, 0.0f }; - third = { 1.0f, 0.0f, 1.0f }; + if (relative.x >= relative.y) { + if (relative.y >= relative.z) { + second = { 1, 0, 0 }; + third = { 1, 1, 0 }; + } else if (relative.x >= relative.z) { + second = { 1, 0, 0 }; + third = { 1, 0, 1 }; } else { - second = { 0.0f, 0.0f, 1.0f }; - third = { 1.0f, 0.0f, 1.0f }; + second = { 0, 0, 1 }; + third = { 1, 0, 1 }; } - } else if (offset[0].y < offset[0].z) { - second = { 0.0f, 0.0f, 1.0f }; - third = { 0.0f, 1.0f, 1.0f }; - } else if (offset[0].x < offset[0].z) { - second = { 0.0f, 1.0f, 0.0f }; - third = { 0.0f, 1.0f, 1.0f }; + } else if (relative.y < relative.z) { + second = { 0, 0, 1 }; + third = { 0, 1, 1 }; + } else if (relative.x < relative.z) { + second = { 0, 1, 0 }; + third = { 0, 1, 1 }; } else { - second = { 0.0f, 1.0f, 0.0f }; - third = { 1.0f, 1.0f, 0.0f }; + second = { 0, 1, 0 }; + third = { 1, 1, 0 }; } - offset[1] = offset[0] - second + one_sixth; - offset[2] = offset[0] - third + one_third; - offset[3] = offset[0] - 0.5f; - - unsigned char index[3] = { - (unsigned char)(skewed.x), - (unsigned char)(skewed.y), - (unsigned char)(skewed.z), + glm::vec3 offset[4] = { + in - unskewed, + relative - second + one_sixth, + relative - third + one_third, + relative - 0.5f, }; - unsigned char corner[4] = { - Perm12(index[0] + Perm(index[1] + Perm(index[2]))), - Perm12(index[0] + int(second.x) + Perm(index[1] + int(second.y) + Perm(index[2] + int(second.z)))), - Perm12(index[0] + int(third.x) + Perm(index[1] + int(third.y) + Perm(index[2] + int(third.z)))), - Perm12(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1))), + + int index[3] = { + (int)(skewed.x) & 0xFF, + (int)(skewed.y) & 0xFF, + (int)(skewed.z) & 0xFF, }; - float n[4]; - float t[4]; - for (size_t i = 0; i < 4; ++i) { - t[i] = 0.6f - dot(offset[i], offset[i]); - if (t[i] < 0.0f) { - n[i] = 0.0f; - } else { - t[i] *= t[i]; - n[i] = t[i] * t[i] * dot(Grad(corner[i]), offset[i]); - } + + float n = 0.0f; + + // 0 + float t = 0.6f - dot(offset[0], offset[0]); + if (t > 0.0f) { + t *= t; + int corner = Perm12(index[0] + Perm(index[1] + Perm(index[2]))); + n += t * t * dot(Grad(corner), offset[0]); + } + + // 1 + t = 0.6f - dot(offset[1], offset[1]); + if (t > 0.0f) { + t *= t; + int corner = Perm12(index[0] + int(second.x) + Perm(index[1] + int(second.y) + Perm(index[2] + int(second.z)))); + n += t * t * dot(Grad(corner), offset[1]); + } + + // 2 + t = 0.6f - dot(offset[2], offset[2]); + if (t > 0.0f) { + t *= t; + int corner = Perm12(index[0] + int(third.x) + Perm(index[1] + int(third.y) + Perm(index[2] + int(third.z)))); + n += t * t * dot(Grad(corner), offset[2]); + } + + // 3 + t = 0.6f - dot(offset[3], offset[3]); + if (t > 0.0f) { + t *= t; + int corner = Perm12(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1))); + n += t * t * dot(Grad(corner), offset[3]); } - return 32.0f * (n[0] + n[1] + n[2] + n[3]); + return 32.0f * n; } -unsigned char SimplexNoise::Perm(size_t idx) const noexcept { +int SimplexNoise::Perm(int idx) const noexcept { return perm[idx]; } -unsigned char SimplexNoise::Perm12(size_t idx) const noexcept { +int SimplexNoise::Perm12(int idx) const noexcept { return perm12[idx]; } -const glm::vec3 &SimplexNoise::Grad(unsigned char idx) const noexcept { +const glm::vec3 &SimplexNoise::Grad(int idx) const noexcept { return grad[idx]; } diff --git a/src/noise.hpp b/src/noise.hpp index 3c4609e..cbfd48e 100644 --- a/src/noise.hpp +++ b/src/noise.hpp @@ -18,14 +18,14 @@ public: bool operator ()() noexcept; template - void operator ()(T &out) noexcept { + T operator ()(T &out) noexcept { constexpr int num_bits = std::numeric_limits::digits + std::numeric_limits::is_signed; for (int i = 0; i < num_bits; ++i) { operator ()(); } - out = static_cast(state); + return out = static_cast(state); } private: @@ -45,13 +45,13 @@ public: float operator ()(const glm::vec3 &) const noexcept; private: - unsigned char Perm(size_t idx) const noexcept; - unsigned char Perm12(size_t idx) const noexcept; - const glm::vec3 &Grad(unsigned char idx) const noexcept; + int Perm(int idx) const noexcept; + int Perm12(int idx) const noexcept; + const glm::vec3 &Grad(int idx) const noexcept; private: - unsigned char perm[512]; - unsigned char perm12[512]; + int perm[512]; + int perm12[512]; glm::vec3 grad[12]; }; -- 2.39.2