diff options
Diffstat (limited to 'racer-tracer/src/geometry.rs')
| -rw-r--r-- | racer-tracer/src/geometry.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/racer-tracer/src/geometry.rs b/racer-tracer/src/geometry.rs new file mode 100644 index 0000000..aa1c18c --- /dev/null +++ b/racer-tracer/src/geometry.rs @@ -0,0 +1,54 @@ +pub mod sphere; + +use crate::ray::Ray; +use crate::vec3::Vec3; + +pub struct HitRecord { + pub point: Vec3, + pub normal: Vec3, + pub t: f64, + pub front_face: bool, + pub color: Vec3, +} + +impl HitRecord { + fn new(point: Vec3, t: f64, color: Vec3) -> Self { + Self { + point, + normal: Vec3::default(), + t, + front_face: true, + color, + } + } + + fn set_face_normal(&mut self, ray: &Ray, outward_normal: Vec3) { + self.front_face = ray.direction().dot(&outward_normal) < 0.0; + self.normal = if self.front_face { + outward_normal + } else { + -outward_normal + }; + } +} + +impl Default for HitRecord { + fn default() -> Self { + HitRecord { + point: Vec3::default(), + normal: Vec3::default(), + t: 0.0, + front_face: true, + color: Vec3::default(), + } + } +} + +pub trait Hittable { + //pub trait Hittable { + fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord>; + + // Manual ugly clone since forcing on Clone would make it a super trait. + // Clone requires Sized and super traits cannot be Sized. + fn clone_box(&self) -> Box<dyn Hittable>; +} |
