aboutsummaryrefslogtreecommitdiff
path: root/src/material/dielectric.rs
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-08-05 15:44:40 +0200
committerJulian T <julian@jtle.dk>2021-08-05 15:44:40 +0200
commit3ef8f4d918406eec6bdc29e0ebd883fabfac9b2e (patch)
treeaa4b1aac1e165821c16f222ebfb9212a9740e98b /src/material/dielectric.rs
parent45119506c0293fdde6cef35f6e6f82d4055b46b6 (diff)
Add picture for c5505ab84820248c6dba35fc06aef9e0ced183derendered
Diffstat (limited to 'src/material/dielectric.rs')
-rw-r--r--src/material/dielectric.rs56
1 files changed, 0 insertions, 56 deletions
diff --git a/src/material/dielectric.rs b/src/material/dielectric.rs
deleted file mode 100644
index c8dc279..0000000
--- a/src/material/dielectric.rs
+++ /dev/null
@@ -1,56 +0,0 @@
-use super::Material;
-use crate::core::{min, Vector3f, Spectrum, Ray};
-use crate::world::Intersection;
-use crate::sample::Sampler;
-use crate::Float;
-use crate::material::reflectant::reflect;
-
-pub struct Dielectric {
- ratio: Float,
-}
-
-// Implementation from RTIOW
-fn refract(v: Vector3f, n: Vector3f, r_ratio: Float, cos_theta: Float) -> Vector3f {
- let r_perp = (v + n * cos_theta) * r_ratio;
- let r_parallel = n * (-(1.0 - r_perp.len_squared()).abs().sqrt());
-
- r_perp + r_parallel
-}
-
-// Schlick Approximation, explained in RTIOW
-fn fresnel(cos: Float, ratio: Float) -> Float {
- let mut r0 = (1.0-ratio) / (1.0+ratio);
- r0 = r0 * r0;
-
- r0 + (1.0-r0)*(1.0-cos).powi(5)
-}
-
-impl Dielectric {
- pub fn new(ratio: Float) -> Self {
- Self { ratio }
- }
-}
-
-impl Material for Dielectric {
- // Implementation from RTIOW
- fn scatter(&self, ray: &Ray, i: &Intersection, sampler: &mut dyn Sampler) -> Option<(Spectrum, Ray)> {
- let ratio = if i.front {1.0/self.ratio} else {self.ratio};
-
- let ray_dir = ray.direction.norm();
- let cos_theta = min((-ray_dir).dot(&i.n), 1.0);
- let sin_theta = (1.0 - cos_theta*cos_theta).sqrt();
-
- // Test if it is possible for the ray the retract or if it must reflect.
- let cannot_refract = (ratio * sin_theta) > 1.0;
- let direction = if cannot_refract || (fresnel(cos_theta, ratio) > sampler.get_sample()) {
- reflect(ray_dir, i.n)
- } else {
- refract(ray_dir, i.n, ratio, cos_theta)
- };
-
- Some((
- Spectrum::WHITE,
- Ray::new(i.p, direction),
- ))
- }
-}