-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphere.go
56 lines (46 loc) · 1.3 KB
/
sphere.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
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"math"
)
type Sphere struct {
center Point3
radius float64
material Material
}
func (s Sphere) Intersect(r Ray) (ret HitRecord) {
oc := s.center.Sub(r.origin)
projectedDistance := oc.Dot(r.direction)
projectingVector := r.direction.Scale(projectedDistance).Sub(oc)
dSquared := projectingVector.Dot(projectingVector)
radiusSquared := s.radius * s.radius
if dSquared > radiusSquared {
ret.hit = false
} else {
ret.hit = true
offset := math.Sqrt(radiusSquared - dSquared)
if projectedDistance-offset > 0 {
ret.hitDistance = projectedDistance - offset
} else if projectedDistance+offset > 0 {
ret.hitDistance = projectedDistance + offset
} else {
ret.hit = false
}
}
if s.material.reflective {
if r.depth > 8 {
ret.color = ColorRGB{0, 0, 0}
} else {
hitPoint := r.At(ret.hitDistance)
normal := hitPoint.Sub(s.center).Normalize()
normal = normal.Scale(-normal.Dot(r.direction))
reflectedDirection := r.direction.Sub(normal.Scale(-2))
reflectedRay := NewRay(hitPoint.Add(normal.Scale(0.0000001)), reflectedDirection, r.depth+1)
ret.color = reflectedRay.GetColor()
}
} else {
normal := r.At(ret.hitDistance).Sub(s.center).Normalize()
factor := (normal.Dot(vertical) + 1) / 2
ret.color = s.material.color.Scale(factor)
}
return
}