diff options
author | Julian T <julian@jtle.dk> | 2021-01-17 00:09:15 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-01-17 00:09:15 +0100 |
commit | 94217187eb2785939458f08d96c7b1b9e55439ab (patch) | |
tree | b10e4ab95f213b13b90ac5e0053ed9e92f973693 /src/sampling | |
parent | ed14718ff073e285374fad2d3471552b9b497825 (diff) |
Minor changed and draft for random samplingold_rework
Diffstat (limited to 'src/sampling')
-rw-r--r-- | src/sampling/random.cpp | 13 | ||||
-rw-r--r-- | src/sampling/random.hpp | 18 | ||||
-rw-r--r-- | src/sampling/sampler.cpp | 10 | ||||
-rw-r--r-- | src/sampling/sampler.hpp | 52 |
4 files changed, 93 insertions, 0 deletions
diff --git a/src/sampling/random.cpp b/src/sampling/random.cpp new file mode 100644 index 0000000..d77e78a --- /dev/null +++ b/src/sampling/random.cpp @@ -0,0 +1,13 @@ +#include "random.hpp" + +RandomSampler::RandomSampler(long samples, int seed) + : Sampler(samples), m_rng(seed) {} + +double RandomSampler::getSample() { + return m_rng.getDouble(); +} + +Vec2d RandomSampler::get2dSample() { + return Vec2d(m_rng.getDouble(), m_rng.getDouble()); +} + diff --git a/src/sampling/random.hpp b/src/sampling/random.hpp new file mode 100644 index 0000000..d1acaa8 --- /dev/null +++ b/src/sampling/random.hpp @@ -0,0 +1,18 @@ +#ifndef SAMPLER_RANDOM_H +#define SAMPLER_RANDOM_H + +#include "sampler.hpp" +#include <core/random.hpp> + +class RandomSampler : public Sampler { + public: + RandomSampler(long samples, int seed = 0); + + double getSample(); + Vec2d get2dSample(); + + private: + Random m_rng; +}; + +#endif diff --git a/src/sampling/sampler.cpp b/src/sampling/sampler.cpp new file mode 100644 index 0000000..d960816 --- /dev/null +++ b/src/sampling/sampler.cpp @@ -0,0 +1,10 @@ +#include "sampler.hpp" + +void Sampler::startPixel(const Vec2d &p) { + m_curPixel = p; + m_curPixelSampleIndex = 0; +} + +bool Sampler::startNextSample() { + return ++m_curPixelSampleIndex < m_sampleCount; +} diff --git a/src/sampling/sampler.hpp b/src/sampling/sampler.hpp new file mode 100644 index 0000000..cd52421 --- /dev/null +++ b/src/sampling/sampler.hpp @@ -0,0 +1,52 @@ +#ifndef SAMPLER_H +#define SAMPLER_H + +#include <core/vector.hpp> + +#include <memory> + +static const double LargestBelowOne = 0x1.fffffffffffffp-1; + +class Sampler { + public: + Sampler(long sampleCount) : m_sampleCount(sampleCount) {} + + /** @brief Start a new pixel sample + * + * Some samplers might find it usefull to get the position + */ + void startPixel(const Vec2d &p); + + /** @brief Called by renderers when a new sample begins + * + * @return Whether this is the last sample + */ + bool startNextSample(); + + /** @brief Get a single sample */ + virtual double getSample() = 0; + + /** @brief Get a 2d sample + * + * Algorithms can optimize for getting 2d samples + */ + virtual Vec2d get2dSample() = 0; + + /** @brief Clones the sampler + * + * Usefull when multiple threads need a sampler. + * + * @param seed Seed to set in cloned sampler + */ + virtual std::unique_ptr<Sampler> clone(int seed) = 0; + + const long m_sampleCount; + + protected: + Vec2d m_curPixel; + long m_curPixelSampleIndex; + +}; + + +#endif |