1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use crate::{ Color, HitRecord, Material, Ray, Real, reflect, refract, schlick_approximation };

pub struct Dielectric {
    ir: Real
}

impl Dielectric {
    pub fn new(ir: Real) -> Self {
        Self { ir }
    }
}

impl Material for Dielectric {
    fn scatter(&self, ray: Ray, hr: &HitRecord) -> Option< (Ray, Color) > {
        let ratio = if hr.ff { 1.0 / self.ir } else { self.ir };
        let ud = ray.dir.normalize();
        let ct = 1f64.min(-ud.dot(&hr.normal));
        let st = (1.0 - ct * ct).sqrt();
        let lr = ratio * st > 1.0 || schlick_approximation(ct, ratio) > rand::random();
        let direction = if lr {
            reflect(ud, hr.normal)
        }
        else {
            refract(ud, hr.normal, ratio)
        };
        Some((Ray::new(hr.p, direction), Color::new(1.0, 1.0, 1.0)))
    }
}