-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
46 lines (39 loc) · 1.18 KB
/
util.h
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
#ifndef UTIL_H_
#define UTIL_H_
#include <Eigen/Core>
using Vec3f = Eigen::Vector3f;
Vec3f GetRandomVecInUnitSphere() {
Vec3f p;
do {
p = 2.0 * Vec3f(drand48(), drand48(), drand48()) - Vec3f(1, 1, 1);
} while (p.squaredNorm() >= 1.0);
return p;
}
Vec3f GetRandomVecInUnitDisk() {
Vec3f p;
do {
p = 2.0 * Vec3f(drand48(), drand48(), 0) - Vec3f(1, 1, 0);
} while (p.squaredNorm() >= 1.0);
return p;
}
Vec3f Reflect(const Vec3f& direction, const Vec3f& normal) {
return direction - 2.0 * direction.dot(normal) * normal;
}
bool Refract(const Vec3f& direction, const Vec3f& normal, float ni_over_nt,
Vec3f* refracted) {
Vec3f unit_direction = direction.normalized();
float dot = unit_direction.dot(normal);
float discriminant = 1.0 - ni_over_nt * ni_over_nt * (1 - dot * dot);
if (discriminant > 0) {
*refracted = ni_over_nt * (unit_direction - normal * dot)
- normal * std::sqrt(discriminant);
return true;
}
return false;
}
float ComputeSchlick(float cosine, float refractive_index) {
float r0 = (1 - refractive_index) / (1 + refractive_index);
r0 = r0 * r0;
return r0 + (1 - r0) * std::pow((1 - cosine), 5);
}
#endif // UTIL_H_