diff options
| author | Sakarias Johansson <sakarias.johansson@goodbyekansas.com> | 2023-01-08 17:51:44 +0100 |
|---|---|---|
| committer | Sakarias Johansson <sakarias.johansson@goodbyekansas.com> | 2023-01-08 17:51:44 +0100 |
| commit | 899f81eed6c221dce22333ad03704b12d7634a54 (patch) | |
| tree | e9ea6b377bada412629341e666ae5d2eb929420a /racer-tracer/src/scene.rs | |
| parent | 928b4191bf5a0d27da6d680ccaade7f94860359e (diff) | |
| download | racer-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/scene.rs')
| -rw-r--r-- | racer-tracer/src/scene.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/racer-tracer/src/scene.rs b/racer-tracer/src/scene.rs new file mode 100644 index 0000000..2d530af --- /dev/null +++ b/racer-tracer/src/scene.rs @@ -0,0 +1,58 @@ +use crate::geometry::Hittable; + +pub struct Scene { + objects: Vec<Box<dyn Hittable>>, +} + +impl Scene { + pub fn new() -> Self { + Self { + objects: Vec::new(), + } + } + + pub fn add(&mut self, hittable: Box<dyn Hittable>) { + self.objects.push(hittable); + } +} + +// TODO: What to do? +// Cloning everything is nice since then every task can do whatever they like. +// Cloning everything is bad becuse you copy everything which takes time. +// Could also put locks on the Scene but then it becomes this global object that everyone +// wants to access at the same time. +// Will do some laborations later and decide on a solution. +impl Clone for Scene { + fn clone(&self) -> Self { + let mut objects = Vec::with_capacity(self.objects.capacity()); + for i in self.objects.iter() { + objects.push(i.clone_box()); + } + Self { objects } + } +} + +impl Hittable for Scene { + fn hit( + &self, + ray: &crate::ray::Ray, + t_min: f64, + t_max: f64, + ) -> Option<crate::geometry::HitRecord> { + let mut rec = None; + let mut closes_so_far = t_max; + + for obj in self.objects.iter() { + if let Some(hit_rec) = obj.hit(ray, t_min, closes_so_far) { + closes_so_far = hit_rec.t; + rec = Some(hit_rec); + } + } + + rec + } + + fn clone_box(&self) -> Box<dyn Hittable> { + Box::new(self.clone()) + } +} |
