Bill Thiede 197c6df4a4 Add Plane rendering.
Add Element enum type that can hold Plane and Sphere.
2018-08-12 21:20:19 -07:00

49 lines
1019 B
Rust

use rendering::Element;
use rendering::Intersectable;
use rendering::Ray;
pub struct Intersection<'a> {
pub distance: f32,
pub object: &'a Element,
}
impl<'a> Intersection<'a> {
pub fn new(distance: f32, object: &Element) -> Intersection {
Intersection { distance, object }
}
}
pub struct Color {
pub red: f32,
pub green: f32,
pub blue: f32,
}
impl Color {
pub fn to_rgba(&self) -> [u8; 4] {
let v: [u8; 4] = [
(255. * self.red) as u8,
(255. * self.green) as u8,
(255. * self.blue) as u8,
255u8,
];
v
}
}
pub struct Scene {
pub width: u32,
pub height: u32,
pub fov: f32,
pub elements: Vec<Element>,
}
impl Scene {
pub fn trace(&self, ray: &Ray) -> Option<Intersection> {
self.elements
.iter()
.filter_map(|s| s.intersect(ray).map(|d| Intersection::new(d, s)))
.min_by(|i1, i2| i1.distance.partial_cmp(&i2.distance).unwrap())
}
}