From 899f81eed6c221dce22333ad03704b12d7634a54 Mon Sep 17 00:00:00 2001 From: Sakarias Johansson Date: Sun, 8 Jan 2023 17:51:44 +0100 Subject: =?UTF-8?q?=F0=9F=8C=8D=20Add=20Geometry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Created a trait for all geometry that has to implement a hit function. Depending on if the ray hits or not it returns an option with the color. - Add support for multiple samples per pixel Current issues: - Using cooperative multitasking which isn't that helpful in this situation since it's like running without async but without overhead. Should switch to rayon. - All data gets copied once per job. Will decide later what to do (copy or put locks and share data between jobs). --- racer-tracer/src/geometry.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 racer-tracer/src/geometry.rs (limited to 'racer-tracer/src/geometry.rs') 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; + + // 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; +} -- cgit v1.2.3