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);
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<std::int32_t>(floor(x)) & 255;
int yi = static_cast<std::int32_t>(floor(y)) & 255;
int xi = static_cast<std::int32_t>(std::floor(x)) & 255;
int yi = static_cast<std::int32_t>(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<float>(x) * 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
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<int>(x) & 255;
int yi = static_cast<int>(y) & 255;
int zi = static_cast<int>(z) & 255;
int xi = static_cast<int>(x) & 255;
int yi = static_cast<int>(y) & 255;
int zi = static_cast<int>(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<std::uint32_t>(BlockType::Sand);
else if (height < 140)
else if(height < 140)
data[y] = static_cast<std::uint32_t>(BlockType::Grass);
else
data[y] = static_cast<std::uint32_t>(BlockType::Stone);
data[y] = static_cast<std::uint32_t>(BlockType::Snow);
}
else
data[y] = static_cast<std::uint32_t>(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<std::uint32_t>(BlockType::Air))
if(data[y] == static_cast<std::uint32_t>(BlockType::Air))
data[y] = static_cast<std::uint32_t>(BlockType::Sand);
}
return data;

View File

@@ -16,18 +16,10 @@ class Noise
[[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 Perlin3D(int x, int y, int z) noexcept;
~Noise() = default;
~Noise() = default;
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);
[[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<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

View File

@@ -2,27 +2,11 @@
#include <Noise.h>
#include <memory>
NoiseCollection::NoiseCollection(const std::uint32_t seed)
{
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
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<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
{
public:
NoiseCollection(const std::uint32_t seed);
~NoiseCollection(void) = default;
void AddNoise(const std::string& key, std::unique_ptr<Noise> noise);
Noise* GetNoise(const std::string key) const;
[[nodiscard]] std::array<std::uint32_t, CHUNK_SIZE.y> GetBlocks(Scop::Vec2i pos);
inline NoiseCollection(const std::uint32_t seed)
{
m_collection.emplace("terrain", std::make_unique<Noise>(seed));
m_collection.emplace("caves", std::make_unique<Noise>(seed)); // TODO !!!!!!
}
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:
std::unordered_map<std::string, std::unique_ptr<Noise>> m_collection;