ft: 6.5: hitlist

This commit is contained in:
2026-04-15 13:57:43 +02:00
parent 3a660f900a
commit 583b67f4f4
5 changed files with 23 additions and 26 deletions

View File

@@ -1,4 +1,4 @@
use crate::vec3::Vec3;
use crate::{objects::traits::Hittable, ray::Ray, vec3::Vec3};
pub struct Hit {
t: f32,
@@ -22,4 +22,17 @@ impl Hit {
pub fn n(&self) -> &Vec3 {
&self.n
}
pub fn hit_list<T: Hittable>(hittables: &Vec<T>, r: &Ray) -> Option<Hit> {
let mut closest: Option<Hit> = None;
for hittable in hittables {
if let Some(hit) = hittable.hit(r) {
// hit happens in front of camera and is closer than closest
if *hit.t() > 0. && (closest.is_none() || closest.as_ref().unwrap().t() > hit.t()) {
closest = Some(hit);
}
}
}
return closest;
}
}

View File

@@ -1,7 +1,7 @@
use core::f32::math::sqrt;
use crate::objects::hit::Hit;
use crate::objects::traits::{Hittable, Normal};
use crate::objects::traits::Hittable;
use crate::Vec3;
pub struct Sphere {
@@ -34,9 +34,7 @@ impl Hittable for Sphere {
Some(Hit::new(t, p, self.normal_at(&p)))
}
}
}
impl Normal for Sphere {
fn normal_at(&self, p: &Vec3) -> Vec3 {
(*p - self.center).get_unit()
}

View File

@@ -4,8 +4,5 @@ use crate::Vec3;
pub trait Hittable {
fn hit(&self, r: &Ray) -> Option<Hit>;
}
pub trait Normal {
fn normal_at(&self, p: &Vec3) -> Vec3;
}