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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use crate::prelude::*;
#[derive(Clone, Debug)]
pub struct State {
pub depth : u16,
pub eta : F,
pub hit_dist : F,
pub fhp : F3,
pub normal : F3,
pub ffnormal : F3,
pub is_emitter : bool,
pub material : Material,
pub medium : Medium,
}
impl State {
pub fn new() -> Self {
Self {
depth : 4,
eta : 0.0,
hit_dist : -1.0,
fhp : F3::zeros(),
normal : F3::zeros(),
ffnormal : F3::zeros(),
is_emitter : false,
material : Material::new(),
medium : Medium::new(),
}
}
pub fn onb(&mut self, n: F3, t: &mut F3, b: &mut F3) {
let up = if n.z.abs() < 0.999 { F3::new(0.0, 0.0, 1.0) } else { F3::new(1.0, 0.0, 0.0) };
*t = up.cross(&n).normalize();
*b = n.cross(&t);
}
pub fn finalize(&mut self, ray: &Ray) {
self.fhp = ray.at(&self.hit_dist);
if dot(&self.normal, &ray.direction) <= 0.0 {
self.ffnormal = self.normal;
} else {
self.ffnormal = -self.normal;
}
self.material.finalize();
self.eta = if dot(&ray.direction, &self.normal) < 0.0 { 1.0 / self.material.ior } else { self.material.ior };
}
}
#[derive(PartialEq, Clone, Debug)]
pub enum LightType {
Rectangular,
Spherical,
Distant,
}
#[derive(PartialEq, Clone, Debug)]
pub struct Light {
pub light_type : LightType,
pub position : F3,
pub emission : F3,
pub u : F3,
pub v : F3,
pub radius : F,
pub area : F,
}
#[derive(PartialEq, Clone, Debug)]
pub struct ScatterSampleRec {
pub l : F3,
pub f : F3,
pub pdf : F,
}
impl ScatterSampleRec {
pub fn new() -> Self {
Self {
l : F3::zeros(),
f : F3::zeros(),
pdf : 0.0,
}
}
}
#[derive(PartialEq, Clone, Debug)]
pub struct LightSampleRec {
pub normal : F3,
pub emission : F3,
pub direction : F3,
pub dist : F,
pub pdf : F,
}
impl LightSampleRec {
pub fn new() -> Self {
Self {
normal : F3::zeros(),
emission : F3::zeros(),
direction : F3::zeros(),
dist : 0.0,
pdf : 0.0,
}
}
}