forked from fogleman/fauxgl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vertex.go
95 lines (87 loc) · 2.33 KB
/
vertex.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
package fauxgl
type Vertex struct {
Position Vector
Normal Vector
Texture Vector
Color Color
Output VectorW
// Vectors []Vector
// Colors []Color
// Floats []float64
}
func (a Vertex) Outside() bool {
return a.Output.Outside()
}
func InterpolateVertexes(v1, v2, v3 Vertex, b VectorW) Vertex {
v := Vertex{}
v.Position = InterpolateVectors(v1.Position, v2.Position, v3.Position, b)
v.Normal = InterpolateVectors(v1.Normal, v2.Normal, v3.Normal, b).Normalize()
v.Texture = InterpolateVectors(v1.Texture, v2.Texture, v3.Texture, b)
v.Color = InterpolateColors(v1.Color, v2.Color, v3.Color, b)
v.Output = InterpolateVectorWs(v1.Output, v2.Output, v3.Output, b)
// if v1.Vectors != nil {
// v.Vectors = make([]Vector, len(v1.Vectors))
// for i := range v.Vectors {
// v.Vectors[i] = InterpolateVectors(
// v1.Vectors[i], v2.Vectors[i], v3.Vectors[i], b)
// }
// }
// if v1.Colors != nil {
// v.Colors = make([]Color, len(v1.Colors))
// for i := range v.Colors {
// v.Colors[i] = InterpolateColors(
// v1.Colors[i], v2.Colors[i], v3.Colors[i], b)
// }
// }
// if v1.Floats != nil {
// v.Floats = make([]float64, len(v1.Floats))
// for i := range v.Floats {
// v.Floats[i] = InterpolateFloats(
// v1.Floats[i], v2.Floats[i], v3.Floats[i], b)
// }
// }
return v
}
func InterpolateFloats(v1, v2, v3 float64, b VectorW) float64 {
var n float64
n += v1 * b.X
n += v2 * b.Y
n += v3 * b.Z
return n * b.W
}
func InterpolateColors(v1, v2, v3 Color, b VectorW) Color {
n := Color{}
n = n.Add(v1.MulScalar(b.X))
n = n.Add(v2.MulScalar(b.Y))
n = n.Add(v3.MulScalar(b.Z))
return n.MulScalar(b.W)
}
func InterpolateVectors(v1, v2, v3 Vector, b VectorW) Vector {
n := Vector{}
n = n.Add(v1.MulScalar(b.X))
n = n.Add(v2.MulScalar(b.Y))
n = n.Add(v3.MulScalar(b.Z))
return n.MulScalar(b.W)
}
func InterpolateVectorWs(v1, v2, v3, b VectorW) VectorW {
n := VectorW{}
n = n.Add(v1.MulScalar(b.X))
n = n.Add(v2.MulScalar(b.Y))
n = n.Add(v3.MulScalar(b.Z))
return n.MulScalar(b.W)
}
func Barycentric(p1, p2, p3, p Vector) VectorW {
v0 := p2.Sub(p1)
v1 := p3.Sub(p1)
v2 := p.Sub(p1)
d00 := v0.Dot(v0)
d01 := v0.Dot(v1)
d11 := v1.Dot(v1)
d20 := v2.Dot(v0)
d21 := v2.Dot(v1)
d := d00*d11 - d01*d01
v := (d11*d20 - d01*d21) / d
w := (d00*d21 - d01*d20) / d
u := 1 - v - w
return VectorW{u, v, w, 1}
}