-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaterial.java
53 lines (50 loc) · 1.64 KB
/
Material.java
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
/** Class to manage the material of a form
* @author Carol Chen
*/
public abstract class Material {
/**
* @param rayIn ray hitting the material
* @param hit information about the hit
* @param attenuation attenuation of ray
* @param scattered variable to put scattered ray in
* @return if scattering occurs
*/
public abstract boolean scatter(Ray rayIn, FormHit hit, Vector attenuation, Ray scattered);
/**
* @param v Incoming vector
* @param n normal of surface
* @param niOverNt value of ni over nt
* @param refracted refracted vector to update
* @return if refraction occurs
*/
public boolean refract(Vector v, Vector n, double niOverNt, Vector refracted) {
Vector unit = v.unitVector();
double dotProduct = unit.dot(n);
double discriminant = 1 - niOverNt * niOverNt * (1 - dotProduct * dotProduct);
if (discriminant > 0) {
Vector unitMinusNTimesDot = unit.subtract(n.multiply(dotProduct));
Vector nTimesRootDiscriminant = n.multiply(Math.sqrt(discriminant));
refracted.set(unitMinusNTimesDot.multiply(niOverNt).subtract(nTimesRootDiscriminant));
return true;
} else {
return false;
}
}
/**
* @param v vector
* @param n normal of surface
* @return Vector reflected
*/
public Vector reflect(Vector v, Vector n) {
return v.subtract(n.multiply(2 * v.dot(n)));
}
/**
* @param u coordinate
* @param v other coordinate
* @param p angle
* @return returns emitted light
*/
public Vector emitted(double u, double v, Vector p) {
return new Vector(0, 0, 0);
}
}