aboutsummaryrefslogtreecommitdiff
path: root/src/sample
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-02-05 20:17:20 +0100
committerJulian T <julian@jtle.dk>2021-02-05 20:17:20 +0100
commit1e83ea211055eb234b89c69b5d03602e3fcb98fb (patch)
tree7b58c7e6d7d174906541e9f2acf546294f8e9ca5 /src/sample
parent9235e74dfbc41895a5f8807e1ab93508268a39ea (diff)
Achieve anti aliasing with multi pixel sampling
Diffstat (limited to 'src/sample')
-rw-r--r--src/sample/mod.rs11
-rw-r--r--src/sample/uniform.rs32
2 files changed, 43 insertions, 0 deletions
diff --git a/src/sample/mod.rs b/src/sample/mod.rs
new file mode 100644
index 0000000..6f2c3eb
--- /dev/null
+++ b/src/sample/mod.rs
@@ -0,0 +1,11 @@
+use crate::Float;
+use crate::core::Vector2f;
+
+mod uniform;
+
+pub use uniform::UniformSampler;
+
+pub trait Sampler {
+ fn get_sample(&mut self) -> Float;
+ fn get_sample_2d(&mut self) -> Vector2f;
+}
diff --git a/src/sample/uniform.rs b/src/sample/uniform.rs
new file mode 100644
index 0000000..221fdf8
--- /dev/null
+++ b/src/sample/uniform.rs
@@ -0,0 +1,32 @@
+use crate::core::Vector2f;
+use crate::Float;
+use super::Sampler;
+
+use rand::prelude::*;
+use rand::distributions::Uniform;
+use rand_pcg::Pcg32;
+
+#[derive(Clone)]
+pub struct UniformSampler {
+ r: Pcg32,
+ d: Uniform<Float>,
+}
+
+impl UniformSampler {
+ pub fn new() -> Self {
+ Self {
+ r: Pcg32::seed_from_u64(1),
+ d: Uniform::from(0.0..1.0),
+ }
+ }
+}
+
+impl Sampler for UniformSampler {
+ fn get_sample(&mut self) -> Float {
+ self.d.sample(&mut self.r)
+ }
+
+ fn get_sample_2d(&mut self) -> Vector2f {
+ Vector2f::new_xy(self.get_sample(), self.get_sample())
+ }
+}