This commit is contained in:
bijan2005 2021-03-20 18:26:23 -04:00
parent 72acde6ea8
commit 68f158f3c9
3 changed files with 16 additions and 19 deletions

View file

@ -1,27 +1,26 @@
extern crate nalgebra as na;
use std::f32::consts::PI;
use std::cmp::Ordering;
use std::f32::consts::PI;
use na::*;
use na::geometry::Point3;
use crate::types::*;
use crate::object::*;
use crate::types::*;
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))
.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 light_point(objects: &Vec<Object>, obj: &Object, point: Point3f, light: &dyn Light) -> Color {
if light.check_shadow(point, objects) {
let texture = obj.get_texture(point);
let texture = obj.gettexture(point);
light.getcolor(point) * (texture.albedo / PI) * light.intensity(point) * obj.normal(point).dot(&*light.direction(point))
light.get_color(point) * (texture.albedo / PI) * light.intensity(point) * obj.normal(point).dot(&*light.direction(point))
} else {
// Point is in shadow
Color::black()
@ -31,11 +30,10 @@ fn light_point(objects: &Vec<Object>, obj: &Object, point: Point3f, light: &dyn
pub fn cast_ray(ray: Ray, scene: &Scene) -> Color {
if let Some((obj, dist)) = trace(ray, &scene.objects) {
let point = ray.project(dist);
let surface_color = obj.gettexture(point).color;
let surface_color = obj.get_texture(point).color;
scene.lights.iter()
.map(|&light| light_point(&scene.objects, obj, point, &*light))
.fold(Color::black(), |acc, c| acc + c) * surface_color
}
else { scene.background }
.map(|light| light_point(&scene.objects, obj, point, &**light))
.fold(Color::black(), |acc, c| acc + c) * surface_color
} else { scene.background }
}