From ab0b3f165788c5d79b2d159aeefda8c8947b68ae Mon Sep 17 00:00:00 2001 From: Rosyid Haryadi Date: Mon, 3 Mar 2025 00:15:55 +0700 Subject: refactor material --- src/object.rs | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) (limited to 'src/object.rs') diff --git a/src/object.rs b/src/object.rs index 2192703..0161d38 100644 --- a/src/object.rs +++ b/src/object.rs @@ -1,7 +1,7 @@ use crate::calculus::calculus::{Point3, Ray, Vec3}; use crate::common::Color; use crate::interval::Interval; -use crate::material::MaterialType; +use crate::material::{Material, MaterialType}; #[derive(Clone)] pub struct HitRecord { @@ -9,9 +9,7 @@ pub struct HitRecord { pub normal: Vec3, pub t: f32, pub front_face: bool, - pub material: MaterialType, - pub color: Color, - pub fuzz: f32, + pub material: Material, } impl Default for HitRecord { @@ -21,9 +19,7 @@ impl Default for HitRecord { normal: Vec3 {x:0.0, y:0.0, z:0.0}, t: 0.0, front_face: false, - material: MaterialType::Diffuse, - color:Color::new(0.0, 0.0, 0.0), - fuzz: 1.0, + material: Material::new(Color::new(1.0, 1.0, 1.0), MaterialType::Diffuse), } } } @@ -42,15 +38,13 @@ pub trait Hittable { pub struct Sphere { center: Point3, radius: f32, - material: MaterialType, - color: Color, - fuzz: f32, + material: Material, } impl Sphere { - pub fn new(center: Vec3, radius: f32, material: MaterialType, color: Color, fuzz: f32) -> Self { + pub fn new(center: Vec3, radius: f32, material: Material) -> Self { let radius = f32::max(0.0, radius); - Self { center, radius, material, color, fuzz } + Self { center, radius, material } } } @@ -82,8 +76,6 @@ impl Hittable for Sphere { .scalar_mul(1.0 / self.radius); rec.set_face_normal(r, outward_normal); rec.material = self.material.clone(); - rec.color = self.color.clone(); - rec.fuzz = self.fuzz; true } @@ -105,7 +97,7 @@ impl HittableList { impl Hittable for HittableList { fn hit(&self, r: &Ray, ray_t: Interval, rec: &mut HitRecord) -> bool { - let mut temp_rec = HitRecord::default(); + let mut temp_rec = rec.clone(); let mut hit_anything = false; let mut closest_so_far = ray_t.max; -- cgit v1.2.3-70-g09d2