From a6302805d19273c95278c8d792ffbd9b2633fe20 Mon Sep 17 00:00:00 2001 From: Sakarias Johansson Date: Thu, 12 Jan 2023 21:09:43 +0100 Subject: =?UTF-8?q?=F0=9F=96=8C=EF=B8=8F=20Add=20materials?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- racer-tracer/src/material/metal.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 racer-tracer/src/material/metal.rs (limited to 'racer-tracer/src/material/metal.rs') 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)) + } + } +} -- cgit v1.2.3