diff options
author | Julian T <julian@jtle.dk> | 2021-02-06 23:43:06 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-02-06 23:43:06 +0100 |
commit | b64c7e972c52b7d015d661866f0cf902370343e5 (patch) | |
tree | 8d3dc9a8ae6b491b9f8f639f2d0bad6387d59069 /src/sample | |
parent | 0d5e6bd9363d5ed5c4f28174819fc0f5fd9aa586 (diff) |
Implement pathtracing
Diffstat (limited to 'src/sample')
-rw-r--r-- | src/sample/mod.rs | 26 | ||||
-rw-r--r-- | src/sample/uniform.rs | 5 |
2 files changed, 24 insertions, 7 deletions
diff --git a/src/sample/mod.rs b/src/sample/mod.rs index 6f2c3eb..84ac755 100644 --- a/src/sample/mod.rs +++ b/src/sample/mod.rs @@ -1,11 +1,33 @@ use crate::Float; -use crate::core::Vector2f; +use crate::core::{Vector3f, Vector2f}; +use std::f32::consts::PI; mod uniform; pub use uniform::UniformSampler; +fn distribute_between(x: Float, a: Float, b: Float) -> Float { + x * (b - a) + a +} + pub trait Sampler { fn get_sample(&mut self) -> Float; - fn get_sample_2d(&mut self) -> Vector2f; + + fn get_sample_2d(&mut self) -> Vector2f { + Vector2f::new_xy(self.get_sample(), self.get_sample()) + } + + fn get_unit_vector(&mut self) -> Vector3f { + let s2d = self.get_sample_2d(); + + let lambda = distribute_between(s2d.x, -PI, PI); + let costheta = 2.0 * s2d.y - 1.0; + let sintheta = costheta.acos().sin(); + + Vector3f::new_xyz( + lambda.cos() * sintheta, + lambda.sin() * sintheta, + costheta, + ) + } } diff --git a/src/sample/uniform.rs b/src/sample/uniform.rs index 221fdf8..cc6825a 100644 --- a/src/sample/uniform.rs +++ b/src/sample/uniform.rs @@ -1,4 +1,3 @@ -use crate::core::Vector2f; use crate::Float; use super::Sampler; @@ -25,8 +24,4 @@ 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()) - } } |