This repository has been archived by the owner on Sep 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.js
114 lines (99 loc) · 2.62 KB
/
matrix.js
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
var Matrix = {
temp: [[],[],[],[]],
multiply: function(a, b, output) {
for(var row = 0; row < 4; row++) {
for(var column = 0; column < 4; column++) {
var temp = 0
for(var component = 0; component < 4; component++) {
temp += a[component * 4 + column] * b[row * 4 + component]
}
output[row * 4 + column] = temp
}
}
},
identity: function(output) {
for(var row = 0; row < 4; row++) {
for(var column = 0; column < 4; column++) {
output[row * 4 + column] = row == column ? 1 : 0
}
}
},
translation: function(x, y, z, output) {
output[0] = 1
output[1] = 0
output[2] = 0
output[3] = x
output[4] = 0
output[5] = 1
output[6] = 0
output[7] = y
output[8] = 0
output[9] = 0
output[10] = 1
output[11] = z
output[12] = 0
output[13] = 0
output[14] = 0
output[15] = 1
},
rotateX: function(radians, output) {
var sin = Math.sin(radians)
var cos = Math.cos(radians)
output[0] = 1
output[1] = 0
output[2] = 0
output[3] = 0
output[4] = 0
output[5] = cos
output[6] = -sin
output[7] = 0
output[8] = 0
output[9] = sin
output[10] = cos
output[11] = 0
output[12] = 0
output[13] = 0
output[14] = 0
output[15] = 1
},
rotateY: function(radians, output) {
var sin = Math.sin(radians)
var cos = Math.cos(radians)
output[0] = cos
output[1] = 0
output[2] = -sin
output[3] = 0
output[4] = 0
output[5] = 1
output[6] = 0
output[7] = 0
output[8] = sin
output[9] = 0
output[10] = cos
output[11] = 0
output[12] = 0
output[13] = 0
output[14] = 0
output[15] = 1
},
rotateZ: function(radians, output) {
var sin = Math.sin(radians)
var cos = Math.cos(radians)
output[0] = cos
output[1] = -sin
output[2] = 0
output[3] = 0
output[4] = sin
output[5] = cos
output[6] = 0
output[7] = 0
output[8] = 0
output[9] = 0
output[10] = 1
output[11] = 0
output[12] = 0
output[13] = 0
output[14] = 0
output[15] = 1
}
}