Finished diffuse shading calculations
This commit is contained in:
parent
20c32fc467
commit
6ef2c65009
7 changed files with 65 additions and 171 deletions
|
|
@ -1,5 +1,6 @@
|
|||
extern crate nalgebra as na;
|
||||
|
||||
use std::f32::consts::PI;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
use na::*;
|
||||
|
|
@ -15,11 +16,26 @@ fn trace(ray: Ray, objects: &Vec<Object>) -> Option<(&Object, f32)> {
|
|||
.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.gettexture(point);
|
||||
|
||||
light.getcolor(point) * (texture.albedo / PI) * light.intensity(point) * obj.normal(point).dot(&*light.direction(point))
|
||||
} else {
|
||||
// Point is in shadow
|
||||
Color::black()
|
||||
}
|
||||
}
|
||||
|
||||
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_texture = obj.gettexture(point);
|
||||
surface_texture.color
|
||||
let surface_color = obj.gettexture(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 }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue