raytracer 0.2.2

Toy raytracer in Rust
Documentation
use crate::{
    HitData,
    random,
    Material,
    Ray, ray,
    Vec3, dot, normalize, reflect
};



/// Metal (solid reflective) [`Material`].
///
/// Used for solid mirror-like shapes that do not arbitrarily scatter rays or do so with set
/// strength.
///
/// [`Material`]: trait.Material.html
pub struct Metal {
    /// Defines how much red, green and blue light this material reflects.
    albedo: Vec3,
    /// Defines how fuzzy metal surface is.
    ///
    /// Clamped between 0.0 (very smooth) and 1.0 (very fuzzy, almost like diffuse).
    fuzziness: f32
}

impl Metal {
    pub fn new(albedo: Vec3, fuzziness: f32) -> Metal {
        let fuzziness = if fuzziness < 0f32 { 0f32 } else
                        if fuzziness > 1f32 { 1f32 } else
                         { fuzziness };
        Metal {albedo, fuzziness}
    }
}

impl Material for Metal {
    fn scatter(&self, ray_in: &Ray, hit_data: &HitData) -> (Vec3, Ray) {
        let reflected = reflect!(normalize!(ray_in.direction), hit_data.normal);

        let attenuation = self.albedo;
        let mut scattered = ray!(
            hit_data.point,
            reflected + self.fuzziness * random::point_in_sphere()
        );

        if dot!(scattered.direction, hit_data.normal) < 0f32 {
            scattered.direction = -scattered.direction
        }

        (attenuation, scattered)
    }
}