-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat44.go
95 lines (82 loc) · 2.22 KB
/
mat44.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 main
import (
"math"
)
type mat44 struct {
m [4][4]float64
}
func (m mat44) multiplyVector(i Vector4) Vector4 {
return Vector4{
i.x*m.m[0][0] + i.y*m.m[1][0] + i.z*m.m[2][0] + i.w*m.m[3][0],
i.x*m.m[0][1] + i.y*m.m[1][1] + i.z*m.m[2][1] + i.w*m.m[3][1],
i.x*m.m[0][2] + i.y*m.m[1][2] + i.z*m.m[2][2] + i.w*m.m[3][2],
i.x*m.m[0][3] + i.y*m.m[1][3] + i.z*m.m[2][3] + i.w*m.m[3][3], -1,
NewTexVector(
i.texVec.u, i.texVec.v,
i.texVec.w,
),
}
}
func (m mat44) multiplyTriangle(t Triangle) Triangle {
return Triangle{
vecs: [3]Vector4{
m.multiplyVector(t.vecs[0]),
m.multiplyVector(t.vecs[1]),
m.multiplyVector(t.vecs[2]),
},
}
}
func identityMatrix() mat44 {
return mat44{
m: [4][4]float64{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},
},
}
}
func rotationMatrix(rotationRads Vector4) mat44 {
gamma := rotationRads.x
beta := rotationRads.y
alpha := rotationRads.z
mat := mat44{
m: [4][4]float64{
{math.Cos(alpha) * math.Cos(beta), math.Cos(alpha)*math.Sin(beta)*math.Sin(gamma) - math.Sin(alpha)*math.Cos(gamma), math.Cos(alpha)*math.Sin(beta)*math.Cos(gamma) + math.Sin(alpha)*math.Sin(gamma), 0},
{math.Sin(alpha) * math.Cos(beta), math.Sin(alpha)*math.Sin(beta)*math.Sin(gamma) + math.Cos(alpha)*math.Cos(gamma), math.Sin(alpha)*math.Sin(beta)*math.Cos(gamma) - math.Cos(alpha)*math.Sin(gamma), 0},
{-math.Sin(beta), math.Cos(beta) * math.Sin(gamma), math.Cos(beta) * math.Cos(gamma), 0},
{0, 0, 0, 1},
},
}
return mat
}
func MakeTranslation(x, y, z float64) mat44 {
return mat44{
m: [4][4]float64{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{x, y, z, 1},
},
}
}
func projectionMatrix(aspectRatio, fovDeg, nearDist, farDist float64) mat44 {
fovRad := 1 / math.Tan(degToRad(fovDeg/2))
return mat44{
m: [4][4]float64{
{aspectRatio * fovRad, 0, 0, 0},
{0, fovRad, 0, 0},
{0, 0, farDist / (farDist - nearDist), -1},
{0, 0, (-farDist * nearDist) / (farDist - nearDist), 0},
},
}
}
func (m1 mat44) multiplyMatrix(m2 mat44) mat44 {
mat := mat44{}
for c := 0; c < 4; c++ {
for r := 0; r < 4; r++ {
mat.m[r][c] = m1.m[r][0]*m2.m[0][c] + m1.m[r][1]*m2.m[1][c] + m1.m[r][2]*m2.m[2][c] + m1.m[r][3]*m2.m[3][c]
}
}
return mat
}