-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaterial.go
64 lines (52 loc) · 1.4 KB
/
material.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
57
58
59
60
61
62
63
64
package main
type Material interface {
scatter(ray *Ray, hit *Hit, attenuation *Color, scattered *Ray) bool
emmited() Color
}
type Lamberian struct {
color Color
refraction float64
}
func newLambertian(color Color) *Lamberian {
return &Lamberian{
color: color,
}
}
func (l *Lamberian) emmited() Color {
return Color{0, 0, 0}
}
func (l *Lamberian) scatter(ray *Ray, hit *Hit, attenuation *Color, scattered *Ray) bool {
attenuation.set(l.color)
scattered.origin = hit.point
scattered.direction = randomAlterate(hit.normal)
return true
}
type Metal struct {
color Color
}
func (metal *Metal) reflect(v *Vector3, n *Vector3) Vector3 {
s := n.times(2 * v.dot(n))
return fromToNormalizedVector2(&s, v)
}
func (metal *Metal) emmited() Color {
return Color{red: 0, green: 0, blue: 0}
}
func (metal *Metal) scatter(ray *Ray, hit *Hit, attenuation *Color, scattered *Ray) bool {
reflected := metal.reflect(&ray.direction, &hit.normal)
scattered.origin = hit.point
scattered.direction = reflected
attenuation.set(metal.color)
return scattered.direction.dot(&hit.normal) > 0
}
type Light struct {
color Color
}
func (light *Light) emmited() Color {
return light.color.clone()
}
func (light *Light) scatter(ray *Ray, hit *Hit, attenuation *Color, scattered *Ray) bool {
attenuation.set(light.color)
// scattered.origin = hit.point
// scattered.direction = randomAlterate(hit.normal)
return false
}