Something went wrong. Try again.
A CPU ray tracer in rust
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182pub mod prism;pub mod quad;pub mod sphere;
use crate::{ data::{Bounded, Hit, Intersect}, math::*,};pub use prism::Prism;pub use quad::Quad;pub use sphere::Sphere;
#[derive(Debug, Clone, Copy)]pub enum Shape { Sphere(Sphere), Quad(Quad), Prism(Prism),}
impl Shape { pub fn sphere(center: Vec3, radius: f64) -> Shape { Shape::Sphere(Sphere::new(center, radius)) }
pub fn quad(corner: Vec3, side_a: Vec3, side_b: Vec3) -> Shape { Shape::Quad(Quad::new(corner, side_a, side_b)) }
pub fn prism(pivot: Vec3, side_a: Vec3, side_b: Vec3, side_c: Vec3) -> Shape { Shape::Prism(Prism::new(pivot, side_a, side_b, side_c)) }}
impl From<Sphere> for Shape { fn from(value: Sphere) -> Self { Shape::Sphere(value) }}
impl From<Quad> for Shape { fn from(value: Quad) -> Self { Shape::Quad(value) }}
impl Bounded for Shape { fn get_box(&self) -> AABB { match self { Shape::Sphere(sphere) => sphere.get_box(), Shape::Quad(quad) => quad.get_box(), Shape::Prism(prism) => prism.get_box(), } }}
impl Intersect for Shape { type Object = Self; fn intersect(&self, ray: &Ray, range: &Interval) -> Option<Hit<'_, Self::Object>> { match self { Shape::Sphere(sphere) => sphere.intersect(ray, range).map(|hit| hit.set_object(self)), Shape::Quad(quad) => quad.intersect(ray, range).map(|hit| hit.set_object(self)), Shape::Prism(prism) => prism.intersect(ray, range).map(|hit| hit.set_object(self)), } }
fn does_intersect(&self, ray: &Ray, range: &Interval) -> bool { match self { Shape::Sphere(sphere) => sphere.does_intersect(ray, range), Shape::Quad(quad) => quad.does_intersect(ray, range), Shape::Prism(prism) => prism.does_intersect(ray, range), } }
fn intersect_range(&self, ray: &Ray, range: &Interval) -> Option<Interval> { match self { Shape::Sphere(sphere) => sphere.intersect_range(ray, range), Shape::Quad(quad) => quad.intersect_range(ray, range), Shape::Prism(prism) => prism.intersect_range(ray, range), } }}