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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use std::mem;
use std::f32;
use enum_set::{EnumSet, CLike};
use linalg::{self, Vector};
use film::Colorf;
use mc;
pub use self::bsdf::BSDF;
pub use self::lambertian::Lambertian;
pub use self::oren_nayar::OrenNayar;
pub use self::specular_reflection::SpecularReflection;
pub use self::specular_transmission::SpecularTransmission;
pub use self::merl::Merl;
pub use self::torrance_sparrow::TorranceSparrow;
pub use self::microfacet_transmission::MicrofacetTransmission;
pub mod bsdf;
pub mod lambertian;
pub mod oren_nayar;
pub mod fresnel;
pub mod specular_reflection;
pub mod specular_transmission;
pub mod merl;
pub mod microfacet;
pub mod torrance_sparrow;
pub mod microfacet_transmission;
#[repr(u32)]
#[derive(Clone, Copy, Debug)]
pub enum BxDFType {
Reflection, Transmission, Diffuse, Glossy, Specular,
}
impl BxDFType {
pub fn all_types() -> EnumSet<BxDFType> {
let mut e = EnumSet::new();
e.insert(BxDFType::Diffuse);
e.insert(BxDFType::Glossy);
e.insert(BxDFType::Specular);
e
}
pub fn all_brdf() -> EnumSet<BxDFType> {
let mut e = BxDFType::all_types();
e.insert(BxDFType::Reflection);
e
}
pub fn all_btdf() -> EnumSet<BxDFType> {
let mut e = BxDFType::all_types();
e.insert(BxDFType::Transmission);
e
}
pub fn all() -> EnumSet<BxDFType> { BxDFType::all_brdf().union(BxDFType::all_btdf()) }
pub fn specular() -> EnumSet<BxDFType> {
let mut e = EnumSet::new();
e.insert(BxDFType::Specular);
e.insert(BxDFType::Reflection);
e.insert(BxDFType::Transmission);
e
}
pub fn non_specular() -> EnumSet<BxDFType> {
let mut e = EnumSet::new();
e.insert(BxDFType::Diffuse);
e.insert(BxDFType::Glossy);
e.insert(BxDFType::Reflection);
e.insert(BxDFType::Transmission);
e
}
}
impl CLike for BxDFType {
fn to_u32(&self) -> u32 { *self as u32 }
unsafe fn from_u32(v: u32) -> BxDFType { mem::transmute(v) }
}
pub trait BxDF {
fn bxdf_type(&self) -> EnumSet<BxDFType>;
fn eval(&self, w_o: &Vector, w_i: &Vector) -> Colorf;
fn sample(&self, w_o: &Vector, samples: &(f32, f32)) -> (Colorf, Vector, f32) {
let mut w_i = mc::cos_sample_hemisphere(samples);
if w_o.z < 0.0 {
w_i.z *= -1.0;
}
(self.eval(w_o, &w_i), w_i, self.pdf(w_o, &w_i))
}
fn matches(&self, flags: EnumSet<BxDFType>) -> bool {
self.bxdf_type().is_subset(&flags)
}
fn pdf(&self, w_o: &Vector, w_i: &Vector) -> f32 {
if same_hemisphere(w_o, w_i) {
f32::abs(cos_theta(w_i)) * f32::consts::FRAC_1_PI
} else {
0.0
}
}
}
pub fn cos_theta(v: &Vector) -> f32 { v.z }
pub fn cos_theta_sqr(v: &Vector) -> f32 { v.z * v.z }
pub fn sin_theta_sqr(v: &Vector) -> f32 { f32::max(0.0, 1.0 - v.z * v.z) }
pub fn sin_theta(v: &Vector) -> f32 { f32::sqrt(sin_theta_sqr(v)) }
pub fn tan_theta(v: &Vector) -> f32 {
let sin_theta_2 = sin_theta_sqr(v);
if sin_theta_2 <= 0.0 {
0.0
} else {
f32::sqrt(sin_theta_2) / cos_theta(v)
}
}
pub fn tan_theta_sqr(v: &Vector) -> f32 {
sin_theta_sqr(v) / cos_theta_sqr(v)
}
pub fn arctan_theta(v: &Vector) -> f32 { cos_theta(v) / sin_theta(v) }
pub fn cos_phi(v: &Vector) -> f32 {
let sin_theta = sin_theta(v);
if sin_theta == 0.0 {
1.0
} else {
linalg::clamp(v.x / sin_theta, -1.0, 1.0)
}
}
pub fn sin_phi(v: &Vector) -> f32 {
let sin_theta = sin_theta(v);
if sin_theta == 0.0 {
0.0
} else {
linalg::clamp(v.y / sin_theta, -1.0, 1.0)
}
}
pub fn same_hemisphere(a: &Vector, b: &Vector) -> bool { a.z * b.z > 0.0 }