From 48decb42046bfd1ad56528ad80b6f586d118a161 Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Sat, 31 May 2025 12:29:51 +0200 Subject: [PATCH] small refactor --- Application/Noise.cpp | 116 ++++++++++++++++---------------- Application/Noise.h | 29 ++++---- Application/NoiseCollection.cpp | 20 +----- Application/NoiseCollection.h | 17 +++-- 4 files changed, 88 insertions(+), 94 deletions(-) diff --git a/Application/Noise.cpp b/Application/Noise.cpp index c497590..b3536b7 100644 --- a/Application/Noise.cpp +++ b/Application/Noise.cpp @@ -26,36 +26,36 @@ void Noise::InitPermutation() std::shuffle(permutations.begin(), permutations.end(), m_seed); for(int i = 0; i < 256; ++i) { - this->perms[i] = permutations[i]; - this->perms[i + 256] = permutations[i]; + m_perms[i] = permutations[i]; + m_perms[i + 256] = permutations[i]; } } -[[nodiscard]] const float Noise::fade(float t) noexcept +[[nodiscard]] const float Noise::Fade(float t) noexcept { return t * t * t * (t * (t * 6 - 15) + 10); } const float Noise::Perlin2D(float x, float y) noexcept { - int xi = static_cast(floor(x)) & 255; - int yi = static_cast(floor(y)) & 255; + int xi = static_cast(std::floor(x)) & 255; + int yi = static_cast(std::floor(y)) & 255; - float xf = x - floor(x); - float yf = y - floor(y); + float xf = x - std::floor(x); + float yf = y - std::floor(y); - float u = fade(xf); - float v = fade(yf); + float u = Fade(xf); + float v = Fade(yf); - int aa = perms[perms[xi] + yi]; - int ab = perms[perms[xi] + yi + 1]; - int ba = perms[perms[xi + 1] + yi]; - int bb = perms[perms[xi + 1] + yi + 1]; + int aa = m_perms[m_perms[xi] + yi]; + int ab = m_perms[m_perms[xi] + yi + 1]; + int ba = m_perms[m_perms[xi + 1] + yi]; + int bb = m_perms[m_perms[xi + 1] + yi + 1]; - float x1 = std::lerp(grad2D(aa, xf, yf), - grad2D(ba, xf - 1, yf), u); - float x2 = std::lerp(grad2D(ab, xf, yf - 1), - grad2D(bb, xf - 1, yf - 1), u); + float x1 = std::lerp(Grad2D(aa, xf, yf), + Grad2D(ba, xf - 1, yf), u); + float x2 = std::lerp(Grad2D(ab, xf, yf - 1), + Grad2D(bb, xf - 1, yf - 1), u); return ((std::lerp(x1, x2, v) + 1.0f) / 2.0f); } @@ -69,11 +69,11 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t for(int i = 0; i < this->c_octaves; ++i) { - total += Perlin2D(x * tmp_freq, y * tmp_freq) * tmp_amp; - maxValue += tmp_amp; + total += Perlin2D(x * tmp_freq, y * tmp_freq) * tmp_amp; + maxValue += tmp_amp; tmp_amp *= c_persistance; - tmp_freq *= c_lacunarity; - } + tmp_freq *= c_lacunarity; + } float normalized = total / maxValue; normalized = std::clamp(normalized, 0.0f, 1.0f); normalized = std::pow(normalized * c_compensatory_factor, c_redistribution); @@ -84,10 +84,10 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t { // Wrapper to unnormalise input and output float scaledX = static_cast(x) * c_frequency; float scaledY = static_cast(y) * c_frequency; - return floor(ApplyPerlin2DParameters(scaledX, scaledY)); + return std::floor(ApplyPerlin2DParameters(scaledX, scaledY)); } -[[nodiscard]] const float Noise::grad2D(int hash, float x, float y) noexcept +[[nodiscard]] const float Noise::Grad2D(int hash, float x, float y) noexcept { int h = hash & 7; // 8 directions float u = h < 4 ? x : y; @@ -96,7 +96,7 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t return ((h & 1) ? -u : u) + ((h & 2) ? -v : v); } -[[nodiscard]] const float Noise::grad(int hash, float x, float y, float z) noexcept +[[nodiscard]] const float Noise::Grad(int hash, float x, float y, float z) noexcept { int h = hash & 15; // 16 directions possibles float u = h < 8 ? x : y; @@ -115,41 +115,41 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t [[nodiscard]] const float Noise::Perlin3D(float x, float y, float z) noexcept { - int xi = static_cast(x) & 255; - int yi = static_cast(y) & 255; - int zi = static_cast(z) & 255; + int xi = static_cast(x) & 255; + int yi = static_cast(y) & 255; + int zi = static_cast(z) & 255; - float xf = x - floor(x); - float yf = y - floor(y); - float zf = z - floor(z); + float xf = x - std::floor(x); + float yf = y - std::floor(y); + float zf = z - std::floor(z); - float u = fade(xf); - float v = fade(yf); - float w = fade(zf); + float u = Fade(xf); + float v = Fade(yf); + float w = Fade(zf); - int aaa = perms[perms[perms[xi] + yi] + zi]; - int aba = perms[perms[perms[xi] + yi + 1] + zi]; - int aab = perms[perms[perms[xi] + yi] + zi + 1]; - int abb = perms[perms[perms[xi] + yi + 1] + zi + 1]; - int baa = perms[perms[perms[xi + 1] + yi] + zi]; - int bba = perms[perms[perms[xi + 1] + yi + 1] + zi]; - int bab = perms[perms[perms[xi + 1] + yi] + zi + 1]; - int bbb = perms[perms[perms[xi + 1] + yi + 1] + zi + 1]; + int aaa = m_perms[m_perms[m_perms[xi] + yi] + zi]; + int aba = m_perms[m_perms[m_perms[xi] + yi + 1] + zi]; + int aab = m_perms[m_perms[m_perms[xi] + yi] + zi + 1]; + int abb = m_perms[m_perms[m_perms[xi] + yi + 1] + zi + 1]; + int baa = m_perms[m_perms[m_perms[xi + 1] + yi] + zi]; + int bba = m_perms[m_perms[m_perms[xi + 1] + yi + 1] + zi]; + int bab = m_perms[m_perms[m_perms[xi + 1] + yi] + zi + 1]; + int bbb = m_perms[m_perms[m_perms[xi + 1] + yi + 1] + zi + 1]; - float x1, x2, y1, y2; - x1 = std::lerp(grad(aaa, xf, yf, zf), - grad(baa, xf - 1, yf, zf), u); - x2 = std::lerp(grad(aba, xf, yf - 1, zf), - grad(bba, xf - 1, yf - 1, zf), u); - y1 = std::lerp(x1, x2, v); + float x1, x2, y1, y2; + x1 = std::lerp(Grad(aaa, xf, yf, zf), + Grad(baa, xf - 1, yf, zf), u); + x2 = std::lerp(Grad(aba, xf, yf - 1, zf), + Grad(bba, xf - 1, yf - 1, zf), u); + y1 = std::lerp(x1, x2, v); - x1 = std::lerp(grad(aab, xf, yf, zf - 1), - grad(bab, xf - 1, yf, zf - 1), u); - x2 = std::lerp(grad(abb, xf, yf - 1, zf - 1), - grad(bbb, xf - 1, yf - 1, zf - 1), u); - y2 = std::lerp(x1, x2, v); + x1 = std::lerp(Grad(aab, xf, yf, zf - 1), + Grad(bab, xf - 1, yf, zf - 1), u); + x2 = std::lerp(Grad(abb, xf, yf - 1, zf - 1), + Grad(bbb, xf - 1, yf - 1, zf - 1), u); + y2 = std::lerp(x1, x2, v); - return ((std::lerp(y1, y2, w) + 1.0f) / 2.0f) * c_amplitude; + return ((std::lerp(y1, y2, w) + 1.0f) / 2.0f) * c_amplitude; } [[nodiscard]] const int Noise::ApplyPerlin3DParameters(float x, float y, float z) noexcept @@ -185,19 +185,19 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t // const std::uint32_t value = Perlin3D(pos.x, y, pos.y); if(y > std::min(height, CHUNK_SIZE.y) - 2) { - if (height <= 23) + if(height <= 23) data[y] = static_cast(BlockType::Sand); - else if (height < 140) + else if(height < 140) data[y] = static_cast(BlockType::Grass); else - data[y] = static_cast(BlockType::Stone); + data[y] = static_cast(BlockType::Snow); } else data[y] = static_cast(BlockType::Stone); } - for (std::uint32_t y = 0; y < WATER_LEVEL; y++) + for(std::uint32_t y = 0; y < WATER_LEVEL; y++) { - if (data[y] == static_cast(BlockType::Air)) + if(data[y] == static_cast(BlockType::Air)) data[y] = static_cast(BlockType::Sand); } return data; diff --git a/Application/Noise.h b/Application/Noise.h index 39d74c1..a84a30c 100644 --- a/Application/Noise.h +++ b/Application/Noise.h @@ -16,18 +16,10 @@ class Noise [[nodiscard]] std::array GetHeight(Scop::Vec2i pos); [[nodiscard]] const int Perlin2D(int x, int y) noexcept; [[nodiscard]] const int Perlin3D(int x, int y, int z) noexcept; - ~Noise() = default; + ~Noise() = default; + private: - std::mt19937 m_seed; - std::array perms; - const float c_frequency; - const float c_amplitude; - const int c_octaves; - const float c_lacunarity; - const float c_persistance; - const int c_redistribution; - const float c_compensatory_factor; void InitPermutation(void); [[nodiscard]] const float Perlin2D(float x, float y) noexcept; @@ -36,9 +28,20 @@ class Noise [[nodiscard]] const float Perlin3D(float x, float y, float z) noexcept; [[nodiscard]] const int ApplyPerlin3DParameters(float x, float y, float z) noexcept; - [[nodiscard]] const float fade(float t) noexcept; - [[nodiscard]] const float grad2D(int hash, float x, float y) noexcept; - [[nodiscard]] const float grad(int hash, float x, float y, float z) noexcept; + [[nodiscard]] const float Fade(float t) noexcept; + [[nodiscard]] const float Grad2D(int hash, float x, float y) noexcept; + [[nodiscard]] const float Grad(int hash, float x, float y, float z) noexcept; + + private: + std::mt19937 m_seed; + std::array m_perms; + const float c_frequency; + const float c_amplitude; + const int c_octaves; + const float c_lacunarity; + const float c_persistance; + const int c_redistribution; + const float c_compensatory_factor; }; #endif diff --git a/Application/NoiseCollection.cpp b/Application/NoiseCollection.cpp index f114557..aaff9a7 100644 --- a/Application/NoiseCollection.cpp +++ b/Application/NoiseCollection.cpp @@ -2,27 +2,11 @@ #include #include -NoiseCollection::NoiseCollection(const std::uint32_t seed) -{ - m_collection["terrain"] = std::make_unique(seed); - m_collection["caves"] = std::make_unique(seed); // TODO !!!!!! -} - -void NoiseCollection::AddNoise(const std::string& key, std::unique_ptr noise) -{ - m_collection[key] = std::move(noise); -} - -Noise* NoiseCollection::GetNoise(const std::string key) const +Noise* NoiseCollection::GetNoise(const std::string& key) const { auto it = m_collection.find(key); - if (it != m_collection.end()) + if(it != m_collection.end()) return it->second.get(); Scop::FatalError("A non existant Noise has been requested"); return nullptr; } - -[[nodiscard]] std::array NoiseCollection::GetBlocks(Scop::Vec2i pos) -{ - return m_collection["terrain"]->GetHeight(pos); -} diff --git a/Application/NoiseCollection.h b/Application/NoiseCollection.h index 6dcc45a..2c360f7 100644 --- a/Application/NoiseCollection.h +++ b/Application/NoiseCollection.h @@ -9,11 +9,18 @@ class NoiseCollection { public: - NoiseCollection(const std::uint32_t seed); - ~NoiseCollection(void) = default; - void AddNoise(const std::string& key, std::unique_ptr noise); - Noise* GetNoise(const std::string key) const; - [[nodiscard]] std::array GetBlocks(Scop::Vec2i pos); + inline NoiseCollection(const std::uint32_t seed) + { + m_collection.emplace("terrain", std::make_unique(seed)); + m_collection.emplace("caves", std::make_unique(seed)); // TODO !!!!!! + } + + Noise* GetNoise(const std::string& key) const; + + inline void AddNoise(std::string key, std::unique_ptr noise) { m_collection.emplace(std::move(key), std::move(noise)); } + [[nodiscard]] inline std::array GetBlocks(Scop::Vec2i pos) const { return m_collection.at("terrain")->GetHeight(pos); } + + ~NoiseCollection() = default; private: std::unordered_map> m_collection;