-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprimitives.go
98 lines (84 loc) · 1.79 KB
/
primitives.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// primitives.go
package main
import (
"fmt"
"math"
)
// CUERPOS
type Object interface {
Type() string
Material() int
Intersect(r *Ray, i int) bool
getNormal(point Vector) Vector
}
// ESFERA
type Sphere struct {
material int
position Vector
radius float64
}
func (e Sphere) Type() string {
return "sphere"
}
func (e Sphere) Material() int {
return e.material
}
func (e Sphere) Intersect(r *Ray, i int) bool {
sphereRay := e.position.Sub(r.origin)
v := sphereRay.Dot(r.direction)
if v-e.radius > r.interDist {
return false
}
collisionDist := e.radius*e.radius + v*v - sphereRay.x*sphereRay.x - sphereRay.y*sphereRay.y - sphereRay.z*sphereRay.z
if collisionDist < 0.0 {
return false
}
collisionDist = v - math.Sqrt(collisionDist)
if collisionDist > r.interDist || collisionDist < 0.0 {
return false
}
r.interDist = collisionDist
r.interObj = i
return true
}
func (e Sphere) getNormal(point Vector) Vector {
normal := point.Sub(e.position)
return normal.Normalize()
}
func (e Sphere) String() string {
return fmt.Sprintf("<Esf: %d %s %.2f>", e.material, e.position.String(), e.radius)
}
// PLANO
type Plane struct {
material int
normal Vector
distancia float64
}
func (p Plane) Type() string {
return "plane"
}
func (p Plane) Material() int {
return p.material
}
func (p Plane) Intersect(r *Ray, i int) bool {
v := p.normal.Dot(r.direction)
if v == 0 {
return false
}
collisionDist := -(p.normal.Dot(r.origin) + p.distancia) / v
if collisionDist < 0.0 {
return false
}
if collisionDist > r.interDist {
return false
}
r.interDist = collisionDist
r.interObj = i
return true
}
func (p Plane) getNormal(point Vector) Vector {
return p.normal
}
func (p Plane) String() string {
return fmt.Sprintf("<Pla: %d %s %.2f>", p.material, p.normal.String(), p.distancia)
}