Redefine Scene struct

This commit is contained in:
bijan2005 2020-12-10 09:23:45 -05:00
parent b53ea71eb0
commit 53f5c8bac5
3 changed files with 18 additions and 12 deletions

View file

@ -11,20 +11,20 @@ mod camera; use camera::*;
mod types; use types::*;
mod object; use object::*;
fn trace(ray: Ray, scene: &Scene) -> Option<(&Object, f32)> {
scene.iter()
.filter_map(|obj| obj.intersect(ray)
.map(|x| (obj, x)))
.min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal))
fn trace(ray: Ray, objects: &Vec<Object>) -> Option<(&Object, f32)> {
objects.iter()
.filter_map(|obj| obj.intersect(ray)
.map(|x| (obj, x)))
.min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal))
}
fn cast_ray(ray: Ray, scene: &Scene) -> Color {
if let Some((obj, dist)) = trace(ray, scene) {
if let Some((obj, dist)) = trace(ray, &scene.objects) {
let point = ray.project(dist);
obj.getcolor(point)
}
else { Color::black() }
else { scene.background }
}
fn render(camera: &Camera, scene: &Scene, filename: &str) -> std::io::Result<()> {
@ -54,9 +54,12 @@ fn main() -> std::io::Result<()> {
let camera = Camera::new(Point3::new(0.0,0.0,0.0), Vector3::new(0.0,0.0,1.0), 1.0, 16.0 / 9.0, 2.0, 480);
let scene = vec![
Object::new_boundless(TriangleMesh::singleton(Point3::new(-1.0, -1.0, 2.0), Point3::new(0.0, 1.0, 2.0), Point3::new(1.0, -1.0, 2.0), |t, u, v| Color::new(t, u, v)))
];
let scene = Scene {
objects: vec![
Object::new(TriangleMesh::singleton(Point3::new(-1.0, -1.0, 2.0), Point3::new(0.0, 1.0, 2.0), Point3::new(1.0, -1.0, 2.0), |t, u, v| Color::new(t, u, v)))
],
background: Color::black()
};
render(&camera, &scene, "out.ppm")
}