-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlights.rs
40 lines (36 loc) · 1.09 KB
/
lights.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use std::f32::consts::PI;
use nalgebra_glm::Vec3;
use palette::LinSrgb;
use crate::{
hittable::{HitList, HitRecord},
ray::Ray,
};
pub enum Light {
Point {
position: Vec3,
color: LinSrgb,
luminosity: f32,
},
}
impl Light {
pub fn intensity(&self, hitrecord: &HitRecord, world: &HitList, tmin: f32) -> LinSrgb {
match *self {
Light::Point {
position,
color,
luminosity,
} => {
let shadow_ray = Ray {
origin: hitrecord.pos,
direction: position - hitrecord.pos,
};
let shadow_ray_dist = shadow_ray.norm();
let shadow_ray_normal = shadow_ray.direction / shadow_ray_dist;
let visible =
world.trace(&shadow_ray, tmin, shadow_ray_dist).is_none() as u32 as f32;
color * visible * shadow_ray_normal.dot(&hitrecord.normal).max(0.0) * luminosity
/ (4.0 * PI * shadow_ray_dist * shadow_ray_dist)
}
}
}
}