-
Notifications
You must be signed in to change notification settings - Fork 4
/
matrix.go
173 lines (148 loc) · 3.6 KB
/
matrix.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
type Matrix [4][4]float32
func NewIdentityMatrix() Matrix {
return Matrix{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},
}
}
func NewScaleMatrix(x, y, z float32) Matrix {
return Matrix{
{x, 0, 0, 0},
{0, y, 0, 0},
{0, 0, z, 0},
{0, 0, 0, 1},
}
}
func NewTranslationMatrix(x, y, z float32) Matrix {
return Matrix{
{1, 0, 0, x},
{0, 1, 0, y},
{0, 0, 1, z},
{0, 0, 0, 1},
}
}
func NewRotationXMatrix(angle float32) Matrix {
if angle == 0 {
return NewIdentityMatrix()
}
sin, cos := sin32(angle), cos32(angle)
return Matrix{
{1, 0, 0, 0},
{0, cos, -sin, 0},
{0, sin, cos, 0},
{0, 0, 0, 1},
}
}
func NewRotationYMatrix(angle float32) Matrix {
if angle == 0 {
return NewIdentityMatrix()
}
sin, cos := sin32(angle), cos32(angle)
return Matrix{
{cos, 0, sin, 0},
{0, 1, 0, 0},
{-sin, 0, cos, 0},
{0, 0, 0, 1},
}
}
func NewRotationZMatrix(angle float32) Matrix {
if angle == 0 {
return NewIdentityMatrix()
}
sin, cos := sin32(angle), cos32(angle)
return Matrix{
{cos, -sin, 0, 0},
{sin, cos, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},
}
}
func NewRotationMatrix(x, y, z float32) Matrix {
m := NewIdentityMatrix()
m = m.Multiply(NewRotationXMatrix(x))
m = m.Multiply(NewRotationYMatrix(y))
m = m.Multiply(NewRotationZMatrix(z))
return m
}
func NewWorldMatrix(scale, rotation, translation Vec3) Matrix {
m := NewIdentityMatrix()
m = NewScaleMatrix(scale.X, scale.Y, scale.Z).Multiply(m)
m = NewRotationMatrix(rotation.X, rotation.Y, rotation.Z).Multiply(m)
m = NewTranslationMatrix(translation.X, translation.Y, translation.Z).Multiply(m)
return m
}
// NewPerspectiveMatrix returns a perspective projection matrix that transforms
// world coordinates to clip coordinates.
func NewPerspectiveMatrix(fov, aspect, zNear, zFar float32) Matrix {
tanHalfFov := tan32(fov / 2.0)
m00 := 1 / (aspect * tanHalfFov)
m11 := 1 / tanHalfFov
m22 := (zFar + zNear) / (zNear - zFar)
m23 := (2 * zFar * zNear) / (zNear - zFar)
return Matrix{
{m00, 0, 0, 0},
{0, m11, 0, 0},
{0, 0, -m22, -m23},
{0, 0, -1, 0},
}
}
func NewScreenMatrix(width, height int) Matrix {
hw := float32(width) / 2
hh := float32(height) / 2
return Matrix{
{hw, 0, 0, hw},
{0, hh, 0, hh},
{0, 0, 0.5, 0.5},
{0, 0, 0, 1},
}
}
func NewLookAtMatrix(eye, target, up Vec3) Matrix {
z := target.Sub(eye).Normalize()
x := up.CrossProduct(z).Normalize()
y := z.CrossProduct(x).Normalize()
return Matrix{
{x.X, x.Y, x.Z, -x.DotProduct(eye)},
{y.X, y.Y, y.Z, -y.DotProduct(eye)},
{z.X, z.Y, z.Z, -z.DotProduct(eye)},
{0, 0, 0, 1},
}
}
func NewViewMatrix(eye, direction, up Vec3) Matrix {
z := direction.Normalize()
x := up.CrossProduct(z).Normalize()
y := z.CrossProduct(x).Normalize()
return Matrix{
{x.X, x.Y, x.Z, -x.DotProduct(eye)},
{y.X, y.Y, y.Z, -y.DotProduct(eye)},
{z.X, z.Y, z.Z, -z.DotProduct(eye)},
{0, 0, 0, 1},
}
}
func (m Matrix) Transpose() Matrix {
return Matrix{
{m[0][0], m[1][0], m[2][0], m[3][0]},
{m[0][1], m[1][1], m[2][1], m[3][1]},
{m[0][2], m[1][2], m[2][2], m[3][2]},
{m[0][3], m[1][3], m[2][3], m[3][3]},
}
}
func (m Matrix) Multiply(other Matrix) (res Matrix) {
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
for k := 0; k < 4; k++ {
res[i][j] += m[i][k] * other[k][j]
}
}
}
return res
}
func matrixMultiplyVec4Inplace(m *Matrix, v *Vec4) {
x := m[0][0]*v.X + m[0][1]*v.Y + m[0][2]*v.Z + m[0][3]*v.W
y := m[1][0]*v.X + m[1][1]*v.Y + m[1][2]*v.Z + m[1][3]*v.W
z := m[2][0]*v.X + m[2][1]*v.Y + m[2][2]*v.Z + m[2][3]*v.W
w := m[3][0]*v.X + m[3][1]*v.Y + m[3][2]*v.Z + m[3][3]*v.W
v.X, v.Y, v.Z, v.W = x, y, z, w
}