-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdielectric.go
47 lines (35 loc) · 1017 Bytes
/
dielectric.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
package raytracer
import (
"math"
"math/rand"
)
type DielectricOptions struct {
IndexOfRefraction float64
}
type Dielectric struct {
DielectricOptions
}
func NewDielectric(options DielectricOptions) Dielectric {
return Dielectric{DielectricOptions: options}
}
func (d Dielectric) Scatter(random *rand.Rand, in Ray, hit Hit) (Ray, Color, bool) {
attenuation := ColorWhite
var refractionRatio float64
if hit.F {
refractionRatio = 1 / d.IndexOfRefraction
} else {
refractionRatio = d.IndexOfRefraction
}
unitDirection := in.Direction.Unit()
cosTheta := math.Min(unitDirection.Multiply(-1).Dot(hit.N), 1)
sinTheta := math.Sqrt(1 - cosTheta*cosTheta)
cannotRefract := refractionRatio*sinTheta > 1
var direction Vec3
if cannotRefract || Reflectance(cosTheta, refractionRatio) > random.Float64() {
direction = unitDirection.Reflect(hit.N)
} else {
direction = unitDirection.Refract(hit.N, refractionRatio)
}
scattered := NewRay(hit.P, direction)
return scattered, attenuation, true
}