-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHittableList.java
53 lines (44 loc) · 1.45 KB
/
HittableList.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
import java.util.ArrayList;
import java.util.List;
public class HittableList implements Hittable {
public List<Hittable> objects = new ArrayList<Hittable>();
private HitRecord latestHitRecord = new HitRecord();
public HittableList() {};
public HittableList(Hittable object) {
add(object);
}
public void clear() {
objects.clear();
}
public void add(Hittable object) {
objects.add(object);
}
public HitRecord getLatestHitRecord() {
return latestHitRecord;
}
public HittableList createCopy() {
HittableList copy = new HittableList();
for (Hittable object : objects) {
copy.add(object.createCopy());
}
return copy;
}
@Override
public boolean hit(Ray r, Interval ray_t, HitRecord rec) {
HitRecord tempRec = new HitRecord();
boolean hitAnything = false;
double closestSoFar = ray_t.max;
// Each object implements Hittable. Thus, each object can have its own hit method.
for (Hittable object : objects) {
// object.hit updates tempRec within the hit method.
if(object.hit(r, new Interval(ray_t.min, closestSoFar), tempRec)) {
hitAnything = true;
closestSoFar = tempRec.t;
latestHitRecord = tempRec;
//rec = tempRec;
//latestHitRecord = rec;
}
}
return hitAnything;
}
}