-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvec3.go
More file actions
160 lines (132 loc) · 3.19 KB
/
vec3.go
File metadata and controls
160 lines (132 loc) · 3.19 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package mathgl
import "fmt"
// 2 dimensional vector.
type Vec3 struct {
X, Y, Z float32
}
// Fills the vector with the given float32
func (v *Vec3) Fill(x, y, z float32) {
v.X = x
v.Y = y
v.Z = z
}
// Returns the length as float32
func (v *Vec3) Length() float32 {
return Fsqrt32(Fsqr32(v.X) + Fsqr32(v.Y) + Fsqr32(v.Z))
}
// Returns the length as square as float32
func (v *Vec3) LengthSq() float32 {
return Fsqr32(v.X) + Fsqr32(v.Y) + Fsqr32(v.Z)
}
// Normalize the vector
func (v *Vec3) Normalize() {
var l float32 = 1.0 / v.Length()
v.X *= l
v.Y *= l
v.Z *= l
}
// Adds the given Vec3 with the vector
func (v *Vec3) Add(x *Vec3) {
v.X += x.X
v.Y += x.Y
v.Z += x.Z
}
// Returns the cosine of the angle between the vectors as float32
func (v *Vec3) Dot(x *Vec3) float32 {
return v.X*x.X + v.Y*x.Y + v.Z*x.Z
}
// Saves the Vec3 perpendicular to the given Vec3
func (v *Vec3) Cross(x *Vec3) {
var t Vec3
t.X = v.X
t.Y = v.Y
t.Z = v.Z
v.X = (t.Y * x.Z) - (t.Z * x.Y)
v.Y = (t.Z * x.X) - (t.X * x.Z)
v.Z = (t.X * x.Y) - (t.Y * x.X)
}
// Subtracts the given Vec3 from the vector
func (v *Vec3) Subtract(x *Vec3) {
v.X -= x.X
v.Y -= x.Y
v.Z -= x.Z
}
// Transforms the Vec3 by a given Mat4
func (v *Vec3) Transform(m *Mat4) {
var t Vec3
t.X = v.X
t.Y = v.Y
t.Z = v.Z
v.X = t.X*m[0] + t.Y*m[4] + t.Z*m[8] + m[12]
v.Y = t.X*m[1] + t.Y*m[5] + t.Z*m[9] + m[13]
v.Z = t.X*m[2] + t.Y*m[6] + t.Z*m[10] + m[14]
}
// Transforms the Vec3 by a given Mat4 inversely
func (v *Vec3) InverseTransform(m *Mat4) {
var t Vec3
t.X = v.X - m[12]
t.Y = v.Y - m[13]
t.Z = v.Z - m[14]
v.X = t.X*m[0] + t.Y*m[1] + t.Z*m[2]
v.Y = t.X*m[4] + t.Y*m[5] + t.Z*m[6]
v.Z = t.X*m[8] + t.Y*m[9] + t.Z*m[10]
}
// Transform a texture Vec3 with the given Mat4 matrix
func (v *Vec3) TransformCoord(m *Mat4) {
var t Vec4
t.Fill(v.X, v.Y, v.Z, 1.0)
t.Transform(m)
v.X = t.X / t.W
v.Y = t.Y / t.W
v.Z = t.Z / t.W
}
// Transform a normal Vec3 with the given Mat4 matrix. Omits the translation, only scaling + rotating
func (v *Vec3) TransformNormal(m *Mat4) {
var t Vec3
t.X = v.X
t.Y = v.Y
t.Z = v.Z
v.X = t.X*m[0] + t.Y*m[4] + t.Z*m[8]
v.Y = t.X*m[1] + t.Y*m[5] + t.Z*m[9]
v.Z = t.X*m[2] + t.Y*m[6] + t.Z*m[10]
}
// Transforms a normal Vec3 with the given Mat4 matrix inversely. Omits the translation, only scaling + rotating
func (v *Vec3) InverseTransformNormal(m *Mat4) {
var t Vec3
t.X = v.X
t.Y = v.Y
t.Z = v.Z
v.X = t.X*m[0] + t.Y*m[1] + t.Z*m[2]
v.Y = t.X*m[4] + t.Y*m[5] + t.Z*m[6]
v.Z = t.X*m[8] + t.Y*m[9] + t.Z*m[10]
}
// Scales a vector to the given length s in float32.
func (v *Vec3) Scale(s float32) {
v.X *= s
v.Y *= s
v.Z *= s
}
// Returns true if the vectors are approximately equal in value
func (v *Vec3) AreEqual(x *Vec3) bool {
return ((v.X < x.X+epsilon && v.X > x.X-epsilon) &&
(v.Y < x.Y+epsilon && v.Y > x.Y-epsilon) &&
(v.Z < x.Z+epsilon && v.Z > x.Z-epsilon))
}
// Assigns the given Vec3 to the Vec3
func (v *Vec3) Assign(x *Vec3) {
if v == x {
return
}
v.X = x.X
v.Y = x.Y
v.Z = x.Z
}
// Sets all the elements of Vec3 to zero
func (v *Vec3) Zero() {
v.X = 0.0
v.Y = 0.0
v.Z = 0.0
}
func (v *Vec3) String() string {
return fmt.Sprintf("Vec3(%f, %f, %f)", v.X, v.Y, v.Z)
}