-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.rs
72 lines (63 loc) · 1.58 KB
/
point.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use glam::Vec3A;
use rand::Rng;
use crate::{
colour::Colour,
light::Light,
photonmap::{Photon, Type},
ray::Ray,
Vertex,
};
#[derive(Clone, Debug, PartialEq)]
pub struct Point {
pub position: Vertex,
pub intensity: Colour,
}
impl Default for Point {
fn default() -> Self {
Self {
position: Vertex::default(),
intensity: Colour::default(),
}
}
}
impl Point {
pub fn new(position: Vertex, intensity: Colour) -> Self {
Self {
position,
intensity,
}
}
}
impl Light for Point {
fn get_direction(&self, surface: Vertex) -> (Vec3A, bool) {
// point lights emit in all directions, so this boolean is always true
((self.position - surface).normalize(), true)
}
fn get_position(&self) -> Option<Vertex> {
Some(self.position)
}
fn get_intensity(&self, _surface: Vertex) -> Colour {
self.intensity
}
fn generate_photon(&self) -> Photon {
let direction = random_in_unit_sphere();
Photon {
ray: Ray::new(self.position, direction),
colour: self.intensity,
type_: Type::Direct,
}
}
}
pub fn random_in_unit_sphere() -> Vec3A {
let mut rng = rand::thread_rng();
loop {
let p = Vec3A::new(
rng.gen_range(-1.0..1.0),
rng.gen_range(-1.0..1.0),
rng.gen_range(-1.0..1.0),
);
if p.length_squared() < 1. {
return p;
}
}
}