-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tree.go
45 lines (38 loc) · 1.04 KB
/
tree.go
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
package rendering
import (
"github.com/EliCDavis/polyform/math/geometry"
"github.com/EliCDavis/polyform/trees"
)
type Tree struct {
tree trees.Tree
items []Hittable
}
func NewBVH(items []Hittable, startTime, endTime float64) Tree {
boxElements := make([]trees.Element, len(items))
for i, h := range items {
boxElements[i] = trees.BoundingBoxElement(*h.BoundingBox(startTime, endTime))
}
return Tree{
tree: trees.NewOctree(boxElements),
items: items,
}
}
func (bvh Tree) Hit(r *TemporalRay, min, max float64, hitRecord *HitRecord) bool {
intersections := bvh.tree.ElementsIntersectingRay(r.Ray(), min, max)
tempRecord := NewHitRecord()
hitAnything := false
closestSoFar := max
for _, itemIndex := range intersections {
item := bvh.items[itemIndex]
if item.Hit(r, min, closestSoFar, tempRecord) {
hitAnything = true
closestSoFar = tempRecord.Distance
*hitRecord = *tempRecord
}
}
return hitAnything
}
func (bvh Tree) BoundingBox(startTime, endTime float64) *geometry.AABB {
box := bvh.tree.BoundingBox()
return &box
}