-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDielectric.java
More file actions
35 lines (28 loc) · 1.21 KB
/
Dielectric.java
File metadata and controls
35 lines (28 loc) · 1.21 KB
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
public final class Dielectric implements Material {
private final double refractionIndex;
public Dielectric(double refractionIndex) {
this.refractionIndex = refractionIndex;
}
@Override
public boolean scatter(Ray rIn, HitRecord rec, ScatterRecord out) {
out.attenuation = new Color(1.0, 1.0, 1.0);
double ri = rec.frontFace ? (1.0 / refractionIndex) : refractionIndex;
Vec3 unitDirection = Vec3.unitVector(rIn.direction());
double cosTheta = Math.min(Vec3.dot(unitDirection.negate(), rec.normal), 1.0);
double sinTheta = Math.sqrt(1.0 - cosTheta * cosTheta);
boolean cannotRefract = ri * sinTheta > 1.0;
Vec3 direction;
if (cannotRefract || reflectance(cosTheta, ri) > Rtweekend.randomDouble()) {
direction = Vec3.reflect(unitDirection, rec.normal);
} else {
direction = Vec3.refract(unitDirection, rec.normal, ri);
}
out.scattered = new Ray(rec.p, direction);
return true;
}
private static double reflectance(double cosine, double refIdx) {
double r0 = (1 - refIdx) / (1 + refIdx);
r0 = r0 * r0;
return r0 + (1 - r0) * Math.pow(1 - cosine, 5);
}
}