small refactor

This commit is contained in:
2025-05-31 12:29:51 +02:00
parent aecb071472
commit 48decb4204
4 changed files with 88 additions and 94 deletions

View File

@@ -26,36 +26,36 @@ void Noise::InitPermutation()
std::shuffle(permutations.begin(), permutations.end(), m_seed); std::shuffle(permutations.begin(), permutations.end(), m_seed);
for(int i = 0; i < 256; ++i) for(int i = 0; i < 256; ++i)
{ {
this->perms[i] = permutations[i]; m_perms[i] = permutations[i];
this->perms[i + 256] = 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); return t * t * t * (t * (t * 6 - 15) + 10);
} }
const float Noise::Perlin2D(float x, float y) noexcept const float Noise::Perlin2D(float x, float y) noexcept
{ {
int xi = static_cast<std::int32_t>(floor(x)) & 255; int xi = static_cast<std::int32_t>(std::floor(x)) & 255;
int yi = static_cast<std::int32_t>(floor(y)) & 255; int yi = static_cast<std::int32_t>(std::floor(y)) & 255;
float xf = x - floor(x); float xf = x - std::floor(x);
float yf = y - floor(y); float yf = y - std::floor(y);
float u = fade(xf); float u = Fade(xf);
float v = fade(yf); float v = Fade(yf);
int aa = perms[perms[xi] + yi]; int aa = m_perms[m_perms[xi] + yi];
int ab = perms[perms[xi] + yi + 1]; int ab = m_perms[m_perms[xi] + yi + 1];
int ba = perms[perms[xi + 1] + yi]; int ba = m_perms[m_perms[xi + 1] + yi];
int bb = perms[perms[xi + 1] + yi + 1]; int bb = m_perms[m_perms[xi + 1] + yi + 1];
float x1 = std::lerp(grad2D(aa, xf, yf), float x1 = std::lerp(Grad2D(aa, xf, yf),
grad2D(ba, xf - 1, yf), u); Grad2D(ba, xf - 1, yf), u);
float x2 = std::lerp(grad2D(ab, xf, yf - 1), float x2 = std::lerp(Grad2D(ab, xf, yf - 1),
grad2D(bb, xf - 1, yf - 1), u); Grad2D(bb, xf - 1, yf - 1), u);
return ((std::lerp(x1, x2, v) + 1.0f) / 2.0f); return ((std::lerp(x1, x2, v) + 1.0f) / 2.0f);
} }
@@ -84,10 +84,10 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t
{ // Wrapper to unnormalise input and output { // Wrapper to unnormalise input and output
float scaledX = static_cast<float>(x) * c_frequency; float scaledX = static_cast<float>(x) * c_frequency;
float scaledY = static_cast<float>(y) * c_frequency; float scaledY = static_cast<float>(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 int h = hash & 7; // 8 directions
float u = h < 4 ? x : y; 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); 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 int h = hash & 15; // 16 directions possibles
float u = h < 8 ? x : y; float u = h < 8 ? x : y;
@@ -119,34 +119,34 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t
int yi = static_cast<int>(y) & 255; int yi = static_cast<int>(y) & 255;
int zi = static_cast<int>(z) & 255; int zi = static_cast<int>(z) & 255;
float xf = x - floor(x); float xf = x - std::floor(x);
float yf = y - floor(y); float yf = y - std::floor(y);
float zf = z - floor(z); float zf = z - std::floor(z);
float u = fade(xf); float u = Fade(xf);
float v = fade(yf); float v = Fade(yf);
float w = fade(zf); float w = Fade(zf);
int aaa = perms[perms[perms[xi] + yi] + zi]; int aaa = m_perms[m_perms[m_perms[xi] + yi] + zi];
int aba = perms[perms[perms[xi] + yi + 1] + zi]; int aba = m_perms[m_perms[m_perms[xi] + yi + 1] + zi];
int aab = perms[perms[perms[xi] + yi] + zi + 1]; int aab = m_perms[m_perms[m_perms[xi] + yi] + zi + 1];
int abb = perms[perms[perms[xi] + yi + 1] + zi + 1]; int abb = m_perms[m_perms[m_perms[xi] + yi + 1] + zi + 1];
int baa = perms[perms[perms[xi + 1] + yi] + zi]; int baa = m_perms[m_perms[m_perms[xi + 1] + yi] + zi];
int bba = perms[perms[perms[xi + 1] + yi + 1] + zi]; int bba = m_perms[m_perms[m_perms[xi + 1] + yi + 1] + zi];
int bab = perms[perms[perms[xi + 1] + yi] + zi + 1]; int bab = m_perms[m_perms[m_perms[xi + 1] + yi] + zi + 1];
int bbb = perms[perms[perms[xi + 1] + yi + 1] + zi + 1]; int bbb = m_perms[m_perms[m_perms[xi + 1] + yi + 1] + zi + 1];
float x1, x2, y1, y2; float x1, x2, y1, y2;
x1 = std::lerp(grad(aaa, xf, yf, zf), x1 = std::lerp(Grad(aaa, xf, yf, zf),
grad(baa, xf - 1, yf, zf), u); Grad(baa, xf - 1, yf, zf), u);
x2 = std::lerp(grad(aba, xf, yf - 1, zf), x2 = std::lerp(Grad(aba, xf, yf - 1, zf),
grad(bba, xf - 1, yf - 1, zf), u); Grad(bba, xf - 1, yf - 1, zf), u);
y1 = std::lerp(x1, x2, v); y1 = std::lerp(x1, x2, v);
x1 = std::lerp(grad(aab, xf, yf, zf - 1), x1 = std::lerp(Grad(aab, xf, yf, zf - 1),
grad(bab, xf - 1, yf, zf - 1), u); Grad(bab, xf - 1, yf, zf - 1), u);
x2 = std::lerp(grad(abb, xf, yf - 1, zf - 1), x2 = std::lerp(Grad(abb, xf, yf - 1, zf - 1),
grad(bbb, xf - 1, yf - 1, zf - 1), u); Grad(bbb, xf - 1, yf - 1, zf - 1), u);
y2 = std::lerp(x1, x2, v); 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;
@@ -190,7 +190,7 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t
else if(height < 140) else if(height < 140)
data[y] = static_cast<std::uint32_t>(BlockType::Grass); data[y] = static_cast<std::uint32_t>(BlockType::Grass);
else else
data[y] = static_cast<std::uint32_t>(BlockType::Stone); data[y] = static_cast<std::uint32_t>(BlockType::Snow);
} }
else else
data[y] = static_cast<std::uint32_t>(BlockType::Stone); data[y] = static_cast<std::uint32_t>(BlockType::Stone);

View File

@@ -16,18 +16,10 @@ class Noise
[[nodiscard]] std::array<std::uint32_t, CHUNK_SIZE.y> GetHeight(Scop::Vec2i pos); [[nodiscard]] std::array<std::uint32_t, CHUNK_SIZE.y> GetHeight(Scop::Vec2i pos);
[[nodiscard]] const int Perlin2D(int x, int y) noexcept; [[nodiscard]] const int Perlin2D(int x, int y) noexcept;
[[nodiscard]] const int Perlin3D(int x, int y, int z) noexcept; [[nodiscard]] const int Perlin3D(int x, int y, int z) noexcept;
~Noise() = default; ~Noise() = default;
private: private:
std::mt19937 m_seed;
std::array<int, NOISE_SIZE> 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); void InitPermutation(void);
[[nodiscard]] const float Perlin2D(float x, float y) noexcept; [[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 float Perlin3D(float x, float y, float z) noexcept;
[[nodiscard]] const int ApplyPerlin3DParameters(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 Fade(float t) noexcept;
[[nodiscard]] const float grad2D(int hash, float x, float y) 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 Grad(int hash, float x, float y, float z) noexcept;
private:
std::mt19937 m_seed;
std::array<int, NOISE_SIZE> 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 #endif

View File

@@ -2,18 +2,7 @@
#include <Noise.h> #include <Noise.h>
#include <memory> #include <memory>
NoiseCollection::NoiseCollection(const std::uint32_t seed) Noise* NoiseCollection::GetNoise(const std::string& key) const
{
m_collection["terrain"] = std::make_unique<Noise>(seed);
m_collection["caves"] = std::make_unique<Noise>(seed); // TODO !!!!!!
}
void NoiseCollection::AddNoise(const std::string& key, std::unique_ptr<Noise> noise)
{
m_collection[key] = std::move(noise);
}
Noise* NoiseCollection::GetNoise(const std::string key) const
{ {
auto it = m_collection.find(key); auto it = m_collection.find(key);
if(it != m_collection.end()) if(it != m_collection.end())
@@ -21,8 +10,3 @@ Noise* NoiseCollection::GetNoise(const std::string key) const
Scop::FatalError("A non existant Noise has been requested"); Scop::FatalError("A non existant Noise has been requested");
return nullptr; return nullptr;
} }
[[nodiscard]] std::array<std::uint32_t, CHUNK_SIZE.y> NoiseCollection::GetBlocks(Scop::Vec2i pos)
{
return m_collection["terrain"]->GetHeight(pos);
}

View File

@@ -9,11 +9,18 @@
class NoiseCollection class NoiseCollection
{ {
public: public:
NoiseCollection(const std::uint32_t seed); inline NoiseCollection(const std::uint32_t seed)
~NoiseCollection(void) = default; {
void AddNoise(const std::string& key, std::unique_ptr<Noise> noise); m_collection.emplace("terrain", std::make_unique<Noise>(seed));
Noise* GetNoise(const std::string key) const; m_collection.emplace("caves", std::make_unique<Noise>(seed)); // TODO !!!!!!
[[nodiscard]] std::array<std::uint32_t, CHUNK_SIZE.y> GetBlocks(Scop::Vec2i pos); }
Noise* GetNoise(const std::string& key) const;
inline void AddNoise(std::string key, std::unique_ptr<Noise> noise) { m_collection.emplace(std::move(key), std::move(noise)); }
[[nodiscard]] inline std::array<std::uint32_t, CHUNK_SIZE.y> GetBlocks(Scop::Vec2i pos) const { return m_collection.at("terrain")->GetHeight(pos); }
~NoiseCollection() = default;
private: private:
std::unordered_map<std::string, std::unique_ptr<Noise>> m_collection; std::unordered_map<std::string, std::unique_ptr<Noise>> m_collection;