This commit is contained in:
2023-02-02 00:58:59 +01:00
parent 560b2b1196
commit 72fb4ce3fd
3 changed files with 15 additions and 17 deletions

View File

@@ -16,16 +16,16 @@ impl HitRecord<'_> {
HitRecord { p: Point3::new(0.0, 0.0, 0.0), normal: Vec3::new(0.0, 0.0, 0.0), t: 0.0, front_face: false, mat_ptr: material } HitRecord { p: Point3::new(0.0, 0.0, 0.0), normal: Vec3::new(0.0, 0.0, 0.0), t: 0.0, front_face: false, mat_ptr: material }
} }
pub fn set_face_normal(&mut self, r: &Ray, outward_normal: &Vec3) { pub fn set_face_normal(&mut self, r: &Ray, outward_normal: Vec3) {
self.front_face = r.direction().dot(outward_normal) < 0.0; self.front_face = r.direction().dot(&outward_normal) < 0.0;
self.normal = if self.front_face { self.normal = if self.front_face {
*outward_normal outward_normal
} else { } else {
*outward_normal * -1.0 outward_normal * -1.0
} }
} }
} }
pub trait Hittable { pub trait Hittable {
fn hit(&mut self, r: &Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool; fn hit(&mut self, r: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord>;
} }

View File

@@ -19,19 +19,17 @@ impl HittableList {
} }
impl Hittable for HittableList { impl Hittable for HittableList {
fn hit(&mut self, r: &crate::ray::Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool { fn hit(&mut self, r: &crate::ray::Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
let mut temp_rec = HitRecord::new_empty(rec.mat_ptr.clone()); let mut temp_rec = None;
let mut hit_anything = false;
let mut closest_so_far = t_max; let mut closest_so_far = t_max;
for object in &mut self.objects { for object in &mut self.objects {
if object.hit(r, t_min, closest_so_far, &mut temp_rec) { if let Some(rec) = object.hit(r, t_min, closest_so_far) {
hit_anything = true; closest_so_far = rec.t;
closest_so_far = temp_rec.t; temp_rec = Some(rec);
*rec = temp_rec.clone();
} }
} }
hit_anything temp_rec
} }
} }

View File

@@ -23,7 +23,7 @@ use crate::{
camera::*, camera::*,
}; };
fn ray_color(r: &Ray, world: &mut dyn Hittable, depth: i32) -> Color { fn ray_color(r: &Ray, world: &dyn Hittable, depth: i32) -> Color {
let mut rec = HitRecord::new_empty(&Metal{ albedo: Color::new_empty() }); let mut rec = HitRecord::new_empty(&Metal{ albedo: Color::new_empty() });
// Limit the bounces // Limit the bounces
@@ -31,7 +31,7 @@ fn ray_color(r: &Ray, world: &mut dyn Hittable, depth: i32) -> Color {
return Color::new(0.0, 0.0, 0.0); return Color::new(0.0, 0.0, 0.0);
} }
if world.hit(r, 0.001, INFINITY, &mut rec) { if let Some(rec) = world.hit(r, 0.001, INFINITY) {
let mut scattered = Ray::new_empty(); let mut scattered = Ray::new_empty();
let mut attenuation = Color::new_empty(); let mut attenuation = Color::new_empty();
@@ -54,8 +54,8 @@ fn main() {
// World // World
let mut world = HittableList::new_empty(); let mut world = HittableList::new_empty();
//world.add(Box::new(Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5))); world.add(Box::new(Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5)));
//world.add(Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0))); world.add(Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0)));
// Camera // Camera
let cam = Camera::new(); let cam = Camera::new();