mirror of
https://github.com/Kbz-8/42_vox.git
synced 2026-01-11 14:43:34 +00:00
adding sand
This commit is contained in:
@@ -11,6 +11,7 @@ enum class BlockType : std::uint32_t
|
|||||||
Dirt,
|
Dirt,
|
||||||
Stone,
|
Stone,
|
||||||
Grass,
|
Grass,
|
||||||
|
Sand,
|
||||||
|
|
||||||
EndEnum
|
EndEnum
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,10 +9,11 @@ constexpr Scop::Vec2ui ATLAS_SIZE = { 64, 64 };
|
|||||||
constexpr Scop::Vec2f SPRITE_UNIT = Scop::Vec2f(SPRITE_SIZE) / Scop::Vec2f(ATLAS_SIZE);
|
constexpr Scop::Vec2f SPRITE_UNIT = Scop::Vec2f(SPRITE_SIZE) / Scop::Vec2f(ATLAS_SIZE);
|
||||||
|
|
||||||
constexpr std::array<std::array<Scop::Vec2ui, 3>, BlocksCount> BLOCKS_TO_ATLAS = {
|
constexpr std::array<std::array<Scop::Vec2ui, 3>, BlocksCount> BLOCKS_TO_ATLAS = {
|
||||||
std::array<Scop::Vec2ui, 3>{ Scop::Vec2ui{ 0, 0 } }, // Air
|
std::array<Scop::Vec2ui, 3>{ Scop::Vec2ui{ 0, 0 }, Scop::Vec2ui{ 0, 0 }, Scop::Vec2ui{ 0, 0 } }, // Air
|
||||||
std::array<Scop::Vec2ui, 3>{ Scop::Vec2ui{ 0, 0 } }, // Dirt
|
std::array<Scop::Vec2ui, 3>{ Scop::Vec2ui{ 0, 0 }, Scop::Vec2ui{ 0, 0 }, Scop::Vec2ui{ 0, 0 } }, // Dirt
|
||||||
std::array<Scop::Vec2ui, 3>{ Scop::Vec2ui{ 1, 0 } }, // Stone
|
std::array<Scop::Vec2ui, 3>{ Scop::Vec2ui{ 1, 0 }, Scop::Vec2ui{ 1, 0 }, Scop::Vec2ui{ 1, 0 } }, // Stone
|
||||||
std::array<Scop::Vec2ui, 3>{ Scop::Vec2ui{ 2, 0 }, Scop::Vec2ui{ 0, 0 }, Scop::Vec2ui{ 3, 0 } }, // Grass
|
std::array<Scop::Vec2ui, 3>{ Scop::Vec2ui{ 2, 0 }, Scop::Vec2ui{ 0, 0 }, Scop::Vec2ui{ 3, 0 } }, // Grass
|
||||||
|
std::array<Scop::Vec2ui, 3>{ Scop::Vec2ui{ 0, 1 }, Scop::Vec2ui{ 0, 1 }, Scop::Vec2ui{ 0, 1 } }, // Sand
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Side : std::uint8_t
|
enum class Side : std::uint8_t
|
||||||
|
|||||||
@@ -2,18 +2,18 @@
|
|||||||
#include <Block.h>
|
#include <Block.h>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <stdexcept>
|
|
||||||
|
constexpr float HEIGHT_COEFF = 255.0f;
|
||||||
|
|
||||||
Noise::Noise(const std::uint32_t seed, float frequency, float amplitude, int octaves, float lacunarity, float persistance): seed(std::mt19937(seed)), frequency(frequency), amplitude(amplitude), octaves(octaves), lacunarity(lacunarity), persistance(persistance)
|
Noise::Noise(const std::uint32_t seed, float frequency, float amplitude, int octaves, float lacunarity, float persistance): seed(std::mt19937(seed)), frequency(frequency), amplitude(amplitude), octaves(octaves), lacunarity(lacunarity), persistance(persistance)
|
||||||
{
|
{
|
||||||
if(amplitude > 1.0f || amplitude < -1.0f)
|
if(amplitude > 1.0f || amplitude < -1.0f)
|
||||||
{
|
Scop::FatalError("Amplitude value must be in [-1;1]");
|
||||||
throw std::invalid_argument("Amplitude value must be in [-1;1]");
|
|
||||||
}
|
|
||||||
InitPermutation();
|
InitPermutation();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Noise::InitPermutation(void)
|
void Noise::InitPermutation()
|
||||||
{
|
{
|
||||||
std::array<int, 256> permutations;
|
std::array<int, 256> permutations;
|
||||||
|
|
||||||
@@ -29,19 +29,13 @@ void Noise::InitPermutation(void)
|
|||||||
|
|
||||||
[[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);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] const float Noise::lerp(float a, float b, float t) noexcept
|
|
||||||
{
|
|
||||||
return (a + t * (b - a));
|
|
||||||
}
|
|
||||||
|
|
||||||
const float Noise::Perlin2D(float x, float y) noexcept
|
const float Noise::Perlin2D(float x, float y) noexcept
|
||||||
{
|
{
|
||||||
int xi = (int)floor(x) & 255;
|
int xi = static_cast<std::int32_t>(floor(x)) & 255;
|
||||||
int yi = (int)floor(y) & 255;
|
int yi = static_cast<std::int32_t>(floor(y)) & 255;
|
||||||
|
|
||||||
float xf = x - floor(x);
|
float xf = x - floor(x);
|
||||||
float yf = y - floor(y);
|
float yf = y - floor(y);
|
||||||
@@ -54,12 +48,12 @@ const float Noise::Perlin2D(float x, float y) noexcept
|
|||||||
int ba = perms[perms[xi + 1] + yi];
|
int ba = perms[perms[xi + 1] + yi];
|
||||||
int bb = perms[perms[xi + 1] + yi + 1];
|
int bb = perms[perms[xi + 1] + yi + 1];
|
||||||
|
|
||||||
float x1 = 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 = 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 ((lerp(x1, x2, v) + 1.0f) / 2.0f);
|
return ((std::lerp(x1, x2, v) + 1.0f) / 2.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // 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
|
||||||
@@ -69,7 +63,8 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t
|
|||||||
float tmp_amp = amplitude;
|
float tmp_amp = amplitude;
|
||||||
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;
|
||||||
@@ -77,7 +72,7 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t
|
|||||||
}
|
}
|
||||||
float normalized = total / maxValue;
|
float normalized = total / maxValue;
|
||||||
normalized = std::clamp(normalized, 0.0f, 1.0f);
|
normalized = std::clamp(normalized, 0.0f, 1.0f);
|
||||||
return static_cast<int>(normalized * 255.0f);
|
return static_cast<int>(normalized * HEIGHT_COEFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] const int Noise::Perlin2D(int x, int y) noexcept
|
[[nodiscard]] const int Noise::Perlin2D(int x, int y) noexcept
|
||||||
@@ -87,8 +82,6 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t
|
|||||||
return floor(ApplyPerlin2DParameters(scaledX, scaledY));
|
return 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
|
||||||
@@ -139,19 +132,19 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t
|
|||||||
int bbb = perms[perms[perms[xi + 1] + yi + 1] + zi + 1];
|
int bbb = perms[perms[perms[xi + 1] + yi + 1] + zi + 1];
|
||||||
|
|
||||||
float x1, x2, y1, y2;
|
float x1, x2, y1, y2;
|
||||||
x1 = 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 = 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 = lerp(x1, x2, v);
|
y1 = std::lerp(x1, x2, v);
|
||||||
|
|
||||||
x1 = 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 = 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 = lerp(x1, x2, v);
|
y2 = std::lerp(x1, x2, v);
|
||||||
|
|
||||||
return ((lerp(y1, y2, w) + 1.0f) / 2.0f) * amplitude;
|
return ((std::lerp(y1, y2, w) + 1.0f) / 2.0f) * amplitude;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] const int Noise::ApplyPerlin3DParameters(float x, float y, float z) noexcept
|
[[nodiscard]] const int Noise::ApplyPerlin3DParameters(float x, float y, float z) noexcept
|
||||||
@@ -161,7 +154,8 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t
|
|||||||
float tmp_amp = amplitude;
|
float tmp_amp = amplitude;
|
||||||
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;
|
||||||
@@ -169,20 +163,17 @@ const int Noise::ApplyPerlin2DParameters(float x, float y) noexcept // Wrapper t
|
|||||||
}
|
}
|
||||||
float normalized = total / maxValue;
|
float normalized = total / maxValue;
|
||||||
normalized = std::clamp(normalized, 0.0f, 1.0f);
|
normalized = std::clamp(normalized, 0.0f, 1.0f);
|
||||||
return static_cast<int>(normalized * 255.0f);
|
return static_cast<int>(normalized * HEIGHT_COEFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[[nodiscard]] std::array<std::uint32_t, CHUNK_SIZE.y> Noise::GetHeight(Scop::Vec2i pos)
|
[[nodiscard]] std::array<std::uint32_t, CHUNK_SIZE.y> Noise::GetHeight(Scop::Vec2i pos)
|
||||||
{
|
{
|
||||||
std::array<std::uint32_t, CHUNK_SIZE.y> data;
|
std::array<std::uint32_t, CHUNK_SIZE.y> data;
|
||||||
std::memset(data.data(), static_cast<std::uint32_t>(BlockType::Air), data.size() * sizeof(std::uint32_t));
|
std::memset(data.data(), static_cast<std::uint32_t>(BlockType::Air), data.size() * sizeof(std::uint32_t));
|
||||||
|
|
||||||
|
|
||||||
//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
|
|
||||||
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++)
|
||||||
{
|
{
|
||||||
if(y > std::min(height, CHUNK_SIZE.y) - 2)
|
if(y > std::min(height, CHUNK_SIZE.y) - 2)
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ class Noise
|
|||||||
[[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 lerp(float a, float b, 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;
|
||||||
};
|
};
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
Reference in New Issue
Block a user