summaryrefslogtreecommitdiff
path: root/racer-tracer/src/geometry.rs
diff options
context:
space:
mode:
authorSakarias Johansson <sakarias.johansson@goodbyekansas.com>2023-01-08 17:51:44 +0100
committerSakarias Johansson <sakarias.johansson@goodbyekansas.com>2023-01-08 17:51:44 +0100
commit899f81eed6c221dce22333ad03704b12d7634a54 (patch)
treee9ea6b377bada412629341e666ae5d2eb929420a /racer-tracer/src/geometry.rs
parent928b4191bf5a0d27da6d680ccaade7f94860359e (diff)
downloadracer-tracer-899f81eed6c221dce22333ad03704b12d7634a54.tar.gz
racer-tracer-899f81eed6c221dce22333ad03704b12d7634a54.tar.xz
racer-tracer-899f81eed6c221dce22333ad03704b12d7634a54.zip
🌍 Add Geometry
- 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).
Diffstat (limited to 'racer-tracer/src/geometry.rs')
-rw-r--r--racer-tracer/src/geometry.rs54
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>;
+}