-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmat.go
74 lines (64 loc) · 2.45 KB
/
mat.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
package g
import "fmt"
type Mat4 struct {
M00, M01, M02, M03 float32
M10, M11, M12, M13 float32
M20, M21, M22, M23 float32
M30, M31, M32, M33 float32
}
const Mat4Size = 4 * 4 * 4
func (a *Mat4) Ptr() *float32 { return &a.M00 }
func (a Mat4) String() string {
return fmt.Sprintf("[[%2.2f %2.2f %2.2f %2.2f][%2.2f %2.2f %2.2f %2.2f][%2.2f %2.2f %2.2f %2.2f][%2.2f %2.2f %2.2f %2.2f]]",
a.M00, a.M01, a.M02, a.M03,
a.M10, a.M11, a.M12, a.M13,
a.M20, a.M21, a.M22, a.M23,
a.M30, a.M31, a.M32, a.M33)
}
func (a Mat4) EqAlmost(b Mat4, eps float32) bool {
return (Abs(a.M00-b.M00) < eps) && (Abs(a.M01-b.M01) < eps) && (Abs(a.M02-b.M02) < eps) && (Abs(a.M03-b.M03) < eps) &&
(Abs(a.M10-b.M10) < eps) && (Abs(a.M11-b.M11) < eps) && (Abs(a.M12-b.M12) < eps) && (Abs(a.M13-b.M13) < eps) &&
(Abs(a.M20-b.M20) < eps) && (Abs(a.M21-b.M21) < eps) && (Abs(a.M22-b.M22) < eps) && (Abs(a.M23-b.M23) < eps) &&
(Abs(a.M30-b.M30) < eps) && (Abs(a.M31-b.M31) < eps) && (Abs(a.M32-b.M32) < eps) && (Abs(a.M33-b.M33) < eps)
}
func (a Mat4) Mul4(b Mat4) Mat4 {
return Mat4{
a.M00*b.M00 + a.M10*b.M01 + a.M20*b.M02 + a.M30*b.M03,
a.M01*b.M00 + a.M11*b.M01 + a.M21*b.M02 + a.M31*b.M03,
a.M02*b.M00 + a.M12*b.M01 + a.M22*b.M02 + a.M32*b.M03,
a.M03*b.M00 + a.M13*b.M01 + a.M23*b.M02 + a.M33*b.M03,
a.M00*b.M10 + a.M10*b.M11 + a.M20*b.M12 + a.M30*b.M13,
a.M01*b.M10 + a.M11*b.M11 + a.M21*b.M12 + a.M31*b.M13,
a.M02*b.M10 + a.M12*b.M11 + a.M22*b.M12 + a.M32*b.M13,
a.M03*b.M10 + a.M13*b.M11 + a.M23*b.M12 + a.M33*b.M13,
a.M00*b.M20 + a.M10*b.M21 + a.M20*b.M22 + a.M30*b.M23,
a.M01*b.M20 + a.M11*b.M21 + a.M21*b.M22 + a.M31*b.M23,
a.M02*b.M20 + a.M12*b.M21 + a.M22*b.M22 + a.M32*b.M23,
a.M03*b.M20 + a.M13*b.M21 + a.M23*b.M22 + a.M33*b.M23,
a.M00*b.M30 + a.M10*b.M31 + a.M20*b.M32 + a.M30*b.M33,
a.M01*b.M30 + a.M11*b.M31 + a.M21*b.M32 + a.M31*b.M33,
a.M02*b.M30 + a.M12*b.M31 + a.M22*b.M32 + a.M32*b.M33,
a.M03*b.M30 + a.M13*b.M31 + a.M23*b.M32 + a.M33*b.M33,
}
}
func Perspective(fovy, aspect, near, far float32) Mat4 {
f := 1.0 / Tan(fovy/2.0)
return Mat4{
f / aspect, 0, 0, 0,
0, f, 0, 0,
0, 0, -(near + far) / (far - near), -1,
0, 0, -2.0 * far * near / (far - near), 0,
}
}
func LookAtV(eye, center, up Vec3) Mat4 {
f := center.Sub(eye).Normalize()
s := f.Cross(up.Normalize()).Normalize()
u := s.Cross(f)
e0, e1, e2 := eye.Dot(s), eye.Dot(u), eye.Dot(f)
return Mat4{
s.X, u.X, -f.X, 0,
s.Y, u.Y, -f.Y, 0,
s.Z, u.Z, -f.Z, 0,
-e0, -e1, e2, 1,
}
}