-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtransform.go
66 lines (50 loc) · 1.29 KB
/
transform.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
package stl
// This file contains generic 3D transformation stuff
import (
"math"
)
// RotationMatrix calculates a 4x4 rotation matrix for a rotation of Angle in radians
// around a rotation axis defined by a point on it (pos) and its direction (dir).
// The result is written into *rotationMatrix.
func RotationMatrix(pos Vec3, dir Vec3, angle float64, rotationMatrix *Mat4) {
dirN := dir.UnitVec3()
s := math.Sin(angle)
c := math.Cos(angle)
u := float64(dirN[0])
v := float64(dirN[1])
w := float64(dirN[2])
su := s * u
sv := s * v
sw := s * w
ic := 1 - c
icu := ic * u
icuu := icu * u
icuv := icu * v
icuw := icu * w
icv := ic * v
icvv := icv * v
icvw := icv * w
icw := ic * w
icww := icw * w
mRotate := Mat4{
Vec4{icuu + c, icuv - sw, icuw + sv, 0},
Vec4{icuv + sw, icvv + c, icvw - su, 0},
Vec4{icuw - sv, icvw + su, icww + c, 0},
Vec4{0, 0, 0, 1},
}
mTranslateForward := Mat4{
Vec4{1, 0, 0, float64(pos[0])},
Vec4{0, 1, 0, float64(pos[1])},
Vec4{0, 0, 1, float64(pos[2])},
Vec4{0, 0, 0, 1},
}
mTranslateBackward := Mat4{
Vec4{1, 0, 0, float64(-pos[0])},
Vec4{0, 1, 0, float64(-pos[1])},
Vec4{0, 0, 1, float64(-pos[2])},
Vec4{0, 0, 0, 1},
}
var mTmp Mat4
mRotate.MultMat4(&mTranslateForward, &mTmp)
mTranslateBackward.MultMat4(&mTmp, rotationMatrix)
}