Formatting

This commit is contained in:
Namonay
2025-05-29 19:27:37 +02:00
parent de2a1a003c
commit 4d245e56ac
2 changed files with 32 additions and 25 deletions

View File

@@ -27,18 +27,19 @@ void Noise::InitPermutation(void)
} }
} }
[[nodiscard]] const float Noise::fade(float t) [[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);
} }
[[nodiscard]] const float Noise::lerp(float a, float b, float t) [[nodiscard]] const float Noise::lerp(float a, float b, float t) noexcept
{ {
return (a + t * (b - a)); return (a + t * (b - a));
} }
const float Noise::perlin2D(float x, float y) { const float Noise::Perlin2D(float x, float y) noexcept
{
int xi = (int)floor(x) & 255; int xi = (int)floor(x) & 255;
int yi = (int)floor(y) & 255; int yi = (int)floor(y) & 255;
@@ -61,7 +62,7 @@ const float Noise::perlin2D(float x, float y) {
return ((lerp(x1, x2, v) + 1.0f) / 2.0f); return ((lerp(x1, x2, v) + 1.0f) / 2.0f);
} }
const int Noise::perlin2D2(float x, float y) // Wrapper to apply various mumbo jumbo to get a very worldlike generation const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper to apply various mumbo jumbo to get a very worldlike generation
{ {
float total = 0.0f; float total = 0.0f;
float tmp_freq = frequency; float tmp_freq = frequency;
@@ -69,7 +70,7 @@ const int Noise::perlin2D2(float x, float y) // Wrapper to apply various mumbo j
float maxValue = 0.0f; float maxValue = 0.0f;
for (int i = 0; i < this->octaves; ++i) { for (int i = 0; i < this->octaves; ++i) {
total += perlin2D(x * tmp_freq, y * tmp_freq) * tmp_amp; total += Perlin2D(x * tmp_freq, y * tmp_freq) * tmp_amp;
maxValue += tmp_amp; maxValue += tmp_amp;
tmp_amp *= persistance; tmp_amp *= persistance;
tmp_freq *= lacunarity; tmp_freq *= lacunarity;
@@ -79,15 +80,16 @@ const int Noise::perlin2D2(float x, float y) // Wrapper to apply various mumbo j
return static_cast<int>(normalized * 255.0f); return static_cast<int>(normalized * 255.0f);
} }
const int Noise::perlin2D(int x, int y) { // Wrapper to unnormalise input and output [[nodiscard]] const int Noise::Perlin2D(int x, int y) noexcept
{ // Wrapper to unnormalise input and output
float scaledX = static_cast<float>(x) * frequency; float scaledX = static_cast<float>(x) * frequency;
float scaledY = static_cast<float>(y) * frequency; float scaledY = static_cast<float>(y) * frequency;
return floor(perlin2D2(scaledX, scaledY)); return floor(ApplyPerlin2DParameters(scaledX, scaledY));
} }
[[nodiscard]] const float Noise::grad2D(int hash, float x, float y) [[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 +98,8 @@ const int Noise::perlin2D(int x, int y) { // Wrapper to unnormalise input and ou
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) { [[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;
float v = h < 4 ? y : (h == 12 || h == 14 ? x : z); float v = h < 4 ? y : (h == 12 || h == 14 ? x : z);
@@ -104,15 +107,16 @@ const int Noise::perlin2D(int x, int y) { // Wrapper to unnormalise input and ou
} }
const int Noise::perlin3D(int x, int y, int z) [[nodiscard]] const int Noise::Perlin3D(int x, int y, int z) noexcept
{ {
float scaledX = static_cast<float>(x) * frequency; float scaledX = static_cast<float>(x) * frequency;
float scaledY = static_cast<float>(y) * frequency; float scaledY = static_cast<float>(y) * frequency;
float scaledZ = static_cast<float>(z) * frequency; float scaledZ = static_cast<float>(z) * frequency;
return floor(perlin3D2(scaledX, scaledY, scaledZ)); return floor(ApplyPerlin3DParameters(scaledX, scaledY, scaledZ));
} }
const float Noise::perlin3D(float x, float y, float z) { [[nodiscard]] const float Noise::Perlin3D(float x, float y, float z) noexcept
{
int xi = (int)floor(x) & 255; int xi = (int)floor(x) & 255;
int yi = (int)floor(y) & 255; int yi = (int)floor(y) & 255;
int zi = (int)floor(z) & 255; int zi = (int)floor(z) & 255;
@@ -150,7 +154,7 @@ const float Noise::perlin3D(float x, float y, float z) {
return ((lerp(y1, y2, w) + 1.0f) / 2.0f) * amplitude; return ((lerp(y1, y2, w) + 1.0f) / 2.0f) * amplitude;
} }
const int Noise::perlin3D2(float x, float y, float z) [[nodiscard]] const int Noise::ApplyPerlin3DParameters(float x, float y, float z) noexcept
{ {
float total = 0.0f; float total = 0.0f;
float tmp_freq = frequency; float tmp_freq = frequency;
@@ -158,7 +162,7 @@ const int Noise::perlin3D2(float x, float y, float z)
float maxValue = 0.0f; float maxValue = 0.0f;
for (int i = 0; i < this->octaves; ++i) { for (int i = 0; i < this->octaves; ++i) {
total += perlin3D(x * tmp_freq, y * tmp_freq, z * tmp_freq) * tmp_amp; total += Perlin3D(x * tmp_freq, y * tmp_freq, z * tmp_freq) * tmp_amp;
maxValue += tmp_amp; maxValue += tmp_amp;
tmp_amp *= persistance; tmp_amp *= persistance;
tmp_freq *= lacunarity; tmp_freq *= lacunarity;
@@ -177,7 +181,7 @@ const int Noise::perlin3D2(float x, float y, float z)
//std::uint32_t height = std::abs(std::sin((float)pos.x / 20.0f) * std::cos((float)pos.y / 20.0f) * 60.0f) + 1; //std::uint32_t height = std::abs(std::sin((float)pos.x / 20.0f) * std::cos((float)pos.y / 20.0f) * 60.0f) + 1;
std::uint32_t height = perlin2D(pos.x, pos.y); std::uint32_t height = Perlin2D(pos.x, pos.y);
// Must not exceed CHUNK_SIZE.y // Must not exceed CHUNK_SIZE.y
for(std::uint32_t y = 0; y < std::min(height, CHUNK_SIZE.y); y++) for(std::uint32_t y = 0; y < std::min(height, CHUNK_SIZE.y); y++)
{ {

View File

@@ -14,12 +14,8 @@ class Noise
Noise(const std::uint32_t seed = 42, float frequency = 0.05f, float amplitude = 0.9f, int octaves = 4, float lacunarity = 2.0f, float persistance = 0.5f); Noise(const std::uint32_t seed = 42, float frequency = 0.05f, float amplitude = 0.9f, int octaves = 4, float lacunarity = 2.0f, float persistance = 0.5f);
[[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);
const float perlin2D(float x, float y); [[nodiscard]] const int Perlin2D(int x, int y) noexcept;
const int perlin2D(int x, int y); [[nodiscard]] const int Perlin3D(int x, int y, int z) noexcept;
const int perlin2D2(float x, float y);
const int perlin3D(int x, int y, int z);
const float perlin3D(float x, float y, float z);
const int perlin3D2(float x, float y, float z);
~Noise() = default; ~Noise() = default;
private: private:
@@ -31,10 +27,17 @@ class Noise
const float lacunarity; const float lacunarity;
const float persistance; const float persistance;
void InitPermutation(void); void InitPermutation(void);
[[nodiscard]] const float fade(float t);
[[nodiscard]] const float lerp(float a, float b, float t); [[nodiscard]] const float Perlin2D(float x, float y) noexcept;
[[nodiscard]] const float grad2D(int hash, float x, float y); [[nodiscard]] const int ApplyPerlin2DParameters(float x, float y) noexcept;
[[nodiscard]] const float grad(int hash, float x, float y, float z);
[[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 lerp(float a, float b, 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;
}; };
#endif #endif