diff options
Diffstat (limited to 'racer-tracer/src/material')
| -rw-r--r-- | racer-tracer/src/material/lambertian.rs | 33 | ||||
| -rw-r--r-- | racer-tracer/src/material/metal.rs | 33 |
2 files changed, 66 insertions, 0 deletions
diff --git a/racer-tracer/src/material/lambertian.rs b/racer-tracer/src/material/lambertian.rs new file mode 100644 index 0000000..8356685 --- /dev/null +++ b/racer-tracer/src/material/lambertian.rs @@ -0,0 +1,33 @@ +use crate::{ + material::Material, + ray::Ray, + vec3::{random_unit_vector, Color, Vec3}, +}; + +pub struct Lambertian { + color: Color, +} + +impl Lambertian { + pub fn new(color: Color) -> Self { + Self { color } + } +} + +impl Material for Lambertian { + fn scatter( + &self, + ray: &crate::ray::Ray, + rec: &crate::geometry::HitRecord, + ) -> Option<(Ray, Color)> { + let mut scatter_direction = rec.normal + random_unit_vector(); + + // Catch bad scatter direction + if scatter_direction.near_zero() { + scatter_direction = rec.normal; + } + + let scattered = Ray::new(rec.point, scatter_direction); + Some((scattered, self.color)) + } +} diff --git a/racer-tracer/src/material/metal.rs b/racer-tracer/src/material/metal.rs new file mode 100644 index 0000000..162342d --- /dev/null +++ b/racer-tracer/src/material/metal.rs @@ -0,0 +1,33 @@ +use crate::{ + material::Material, + ray::Ray, + vec3::{random_in_unit_sphere, reflect, Color}, +}; + +pub struct Metal { + color: Color, + fuzz: f64, +} + +impl Metal { + pub fn new(color: Color, fuzz: f64) -> Self { + Self { color, fuzz } + } +} + +impl Material for Metal { + fn scatter( + &self, + ray: &crate::ray::Ray, + rec: &crate::geometry::HitRecord, + ) -> Option<(Ray, Color)> { + let reflected = reflect(&ray.direction().unit_vector(), &rec.normal); + let scattered = Ray::new(rec.point, reflected + self.fuzz * random_in_unit_sphere()); + + if scattered.direction().dot(&rec.normal) < 0.0 { + None + } else { + Some((scattered, self.color)) + } + } +} |
