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()
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")
}

View file

@ -70,7 +70,11 @@ impl Object {
pub fn getcolor(&self, point: Point3<f32>) -> Color { self.surface.getcolor(point) }
}
pub type Scene = Vec<Object>;
pub struct Scene {
pub objects: Vec<Object>,
pub background: Color
}
#[cfg(test)]
mod tests {

View file

@ -205,7 +205,6 @@ impl Surface for TriangleMesh {
}
fn smallest_sphere(points: Vec<&Point3<f32>>, boundary: Vec<&Point3<f32>>) -> (Point3<f32>, f32) {
println!("{:?}\n{:?}\n", points, boundary);
if points.len() == 0 || boundary.len() == 4 {
match boundary.len() {
0 => (Point3::new(0.0, 0.0, 0.0), 0.0),