-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRefractive.cpp
61 lines (47 loc) · 1.49 KB
/
Refractive.cpp
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
#include "Refractive.h"
const double PI = 3.14;
const double epsilon = 0.001;
const double inf = std::numeric_limits<double>::infinity();
Refractive::Refractive(double n): ior(n) {}
double Refractive::brdf(const Vector3d& wo, const Vector3d& wi, const Vector3d& n) {
return 1.0;
}
double Refractive::pdf(const Vector3d& wo, const Vector3d& wi, const Vector3d& n) {
return 1.0;
}
Color4dRGB Refractive::explicit_eval(const Vector3d& wo, const Vector3d& wi, const Vector3d& n) {
return 1.0;
}
Vector3d Refractive::importance_sampling(const Vector3d& wo, const Vector3d& n) {
return Vector3d(0.0, 0.0, 0.0);
}
// normal faces away from the volume
void Refractive::eval(Ray& r, const Vector3d& p, const Vector3d& n) {
double chi = -1.0;
if(n.dot(r.dir)>0){
chi = 1.0;
}
bool inside = chi>0;
double ni, no;
if(inside) {
ni = ior;
no = 1.0;
} else {
ni = 1.0;
no = ior;
}
double n_ratio = ni/no;
double cos_theta_i = r.dir.dot(n);
double sin_theta_i = sqrt(1-cos_theta_i*cos_theta_i);
bool total_internal_refl = (n_ratio > 1.0) ? n_ratio*sin_theta_i>1.0 : false;
Vector3d o;
if(!total_internal_refl) {
o = chi*sqrt(1-n_ratio*n_ratio*(1-cos_theta_i*cos_theta_i))*n+
n_ratio*(r.dir-cos_theta_i*n);
} else {
o = (-2*r.dir.dot(n)*n+r.dir);
}
r.origin = p+o*0.01;
r.dir = o;
}
Refractive::~Refractive() {}