From d0de60dc33df75fbcacb53a09568b14d0fd48cb9 Mon Sep 17 00:00:00 2001 From: Mel Date: Mon, 12 Jun 2023 17:09:55 +0200 Subject: Multithreaded world generation with Perlin --- src/Util/Sampler.hpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/Util/Sampler.hpp (limited to 'src/Util/Sampler.hpp') diff --git a/src/Util/Sampler.hpp b/src/Util/Sampler.hpp new file mode 100644 index 0000000..3a4ff4f --- /dev/null +++ b/src/Util/Sampler.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include +#include "../Math/Vector.hpp" +#include "../Math/Grid.hpp" + +namespace MC::Util { + +template +class Sampler { + using Pos = Vector; + using Sample = std::function; + using Interpolate = std::function; +public: + Sampler( + Sample sample, + Pos offset = {}, + float scale = 1.0f, + Interpolate interpolate = nearest_interpolation + ) : m_sample([=](Sampler::Pos pos) -> T { + return interpolate(pos, [=](Sampler::Pos p) -> T { return sample(p * scale + offset); }); + }) {} + + T sample(Pos at) { + return m_sample(at); + } + + static T nearest_interpolation(Pos pos, Sample sample) { + return sample(pos); + } + +private: + Sample m_sample; +}; + +} \ No newline at end of file -- cgit 1.4.1