-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix4.lua
436 lines (300 loc) · 12.6 KB
/
Matrix4.lua
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
local ffi
Matrix4 = {}
setmetatable(Matrix4, Matrix4)
Matrix4.keys = {
r1c1 = 1, r1c2 = 2, r1c3 = 3, r1c4 = 4,
r2c1 = 5, r2c2 = 6, r2c3 = 7, r2c4 = 8,
r3c1 = 9, r3c2 = 10, r3c3 = 11, r3c4 = 12,
r4c1 = 13, r4c2 = 14, r4c3 = 15, r4c4 = 16,
}
if jit and jit.status() then
ffi = require"ffi"
ffi.cdef([[
typedef union Matrix4
{
float components[16];
} Matrix4;
]])
ffi.metatype("Matrix4", Matrix4)
end
function isMatrix4(matrix)
if ffi then
return ffi.istype("Matrix4", matrix)
else
return type(matrix) == "table" and matrix._isclass and matrix._isclass(Matrix4)
end
end
function Matrix4._isclass(c)
return c == Matrix4
end
local function getComponents(args, components)
for i=1,#args do
if type(args[i]) == "number" then
table.insert(components, args[i])
elseif type(args[i]) == "table" or isVector2(args[i]) or isVector3(args[i]) or isVector4(args[i]) then
getComponents(args[i], components)
else
error("Invalid component #"..(#components+1).." \""..tostring(args[i]).."\"")
end
end
end
------------------------------------------------------------------------
-- CONSTRUCTORS
------------------------------------------------------------------------
function Matrix4.new(...)
local args = {...}
local components = {}
getComponents(args, components)
if #components == 0 then
components = {
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1,
}
end
assert(#components == 16, "16 components expected, got "..#components)
local matrix
if ffi then
matrix = ffi.new("Matrix4")
else
matrix = setmetatable({components={}}, Matrix4)
end
for i=1,#components do
matrix.components[i-1] = components[i]
end
return matrix
end
function Matrix4.fromPosition(pos)
return Matrix4(
1,0,0,pos.x,
0,1,0,pos.y,
0,0,1,pos.z,
0,0,0,1
)
end
function Matrix4.fromScale(scale)
return Matrix4(
scale.x,0,0,0,
0,scale.y,0,0,
0,0,scale.z,0,
0,0,0,1
)
end
function Matrix4.fromRotationX(angle)
local c = math.cos(angle)
local s = math.sin(angle)
return Matrix4(
1, 0, 0, 0,
0, c,-s, 0,
0, s, c, 0,
0, 0, 0, 1
)
end
function Matrix4.fromRotationY(angle)
local c = math.cos(angle)
local s = math.sin(angle)
return Matrix4(
c, 0, s, 0,
0, 1, 0, 0,
-s, 0, c, 0,
0, 0, 0, 1
)
end
function Matrix4.fromRotationZ(angle)
local c = math.cos(angle)
local s = math.sin(angle)
return Matrix4(
c,-s, 0, 0,
s, c, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
)
end
function Matrix4.fromRotation(rot)
return Matrix4.fromRotationX(rot.x) * Matrix4.fromRotationY(rot.y) * Matrix4.fromRotationZ(rot.z)
end
function Matrix4.fromTransform(pos, rot, scale)
local out = Matrix4()
if pos then out = out * Matrix4.fromPosition(pos) end
if scale then out = out * Matrix4.fromScale(scale) end
if rot then out = out * Matrix4.fromRotation(rot) end
return out
end
function Matrix4.fromView(pos, rot)
local out = Matrix4()
if rot then out = out * Matrix4.fromRotation(rot) end
if pos then out = out * Matrix4.fromPosition(-pos) end
return out
end
function Matrix4.fromPerspective(fov, ratio, near, far)
local out = Matrix4()
local t = math.tan(math.rad(fov)/2)
out:set(1,1, 1 / (ratio * t) )
out:set(2,2, 1 / t )
out:set(3,3, -(far + near) / (far - near) )
out:set(3,4, -(2 * far * near) / (far - near) )
out:set(4,3, -1 )
out:set(4,4, 0 )
return out
end
function Matrix4.fromOrthographic(left, right, top, bottom, near, far)
local out = Matrix4()
out:set(1,1, 2 / (right - left) )
out:set(2,2, 2 / (top - bottom) )
out:set(3,3, -2 / (far - near) )
out:set(4,1, -((right + left) / (right - left)) )
out:set(4,2, -((top + bottom) / (top - bottom)) )
out:set(4,3, -((far + near) / (far - near)) )
out:set(4,4, 0 )
return out
end
------------------------------------------------------------------------
-- METHODS
------------------------------------------------------------------------
function Matrix4.get(matrix, row, column)
return matrix[(row-1)*4+column]
end
function Matrix4.set(matrix, row, column, value)
matrix[(row-1)*4+column] = value
end
function Matrix4.unpack(m)
return m[1], m[2], m[3], m[4],
m[5], m[6], m[7], m[8],
m[9], m[10], m[11], m[12],
m[13], m[14], m[15], m[16]
end
local tmp = {}
function Matrix4.send(mat)
for i=1,16 do
tmp[i] = mat[i]
end
return tmp
end
function Matrix4.invert(matrix)
local out = Matrix4()
out[1] = matrix[6] * matrix[11] * matrix[16] - matrix[6] * matrix[15] * matrix[12] - matrix[7] * matrix[10] * matrix[16] + matrix[7] * matrix[14] * matrix[12] + matrix[8] * matrix[10] * matrix[15] - matrix[8] * matrix[14] * matrix[11]
out[5] = -matrix[5] * matrix[11] * matrix[16] + matrix[5] * matrix[15] * matrix[12] + matrix[7] * matrix[9] * matrix[16] - matrix[7] * matrix[13] * matrix[12] - matrix[8] * matrix[9] * matrix[15] + matrix[8] * matrix[13] * matrix[11]
out[9] = matrix[5] * matrix[10] * matrix[16] - matrix[5] * matrix[14] * matrix[12] - matrix[6] * matrix[9] * matrix[16] + matrix[6] * matrix[13] * matrix[12] + matrix[8] * matrix[9] * matrix[14] - matrix[8] * matrix[13] * matrix[10]
out[13] = -matrix[5] * matrix[10] * matrix[15] + matrix[5] * matrix[14] * matrix[11] + matrix[6] * matrix[9] * matrix[15] - matrix[6] * matrix[13] * matrix[11] - matrix[7] * matrix[9] * matrix[14] + matrix[7] * matrix[13] * matrix[10]
out[2] = -matrix[2] * matrix[11] * matrix[16] + matrix[2] * matrix[15] * matrix[12] + matrix[3] * matrix[10] * matrix[16] - matrix[3] * matrix[14] * matrix[12] - matrix[4] * matrix[10] * matrix[15] + matrix[4] * matrix[14] * matrix[11]
out[6] = matrix[1] * matrix[11] * matrix[16] - matrix[1] * matrix[15] * matrix[12] - matrix[3] * matrix[9] * matrix[16] + matrix[3] * matrix[13] * matrix[12] + matrix[4] * matrix[9] * matrix[15] - matrix[4] * matrix[13] * matrix[11]
out[10] = -matrix[1] * matrix[10] * matrix[16] + matrix[1] * matrix[14] * matrix[12] + matrix[2] * matrix[9] * matrix[16] - matrix[2] * matrix[13] * matrix[12] - matrix[4] * matrix[9] * matrix[14] + matrix[4] * matrix[13] * matrix[10]
out[14] = matrix[1] * matrix[10] * matrix[15] - matrix[1] * matrix[14] * matrix[11] - matrix[2] * matrix[9] * matrix[15] + matrix[2] * matrix[13] * matrix[11] + matrix[3] * matrix[9] * matrix[14] - matrix[3] * matrix[13] * matrix[10]
out[3] = matrix[2] * matrix[7] * matrix[16] - matrix[2] * matrix[15] * matrix[8] - matrix[3] * matrix[6] * matrix[16] + matrix[3] * matrix[14] * matrix[8] + matrix[4] * matrix[6] * matrix[15] - matrix[4] * matrix[14] * matrix[7]
out[7] = -matrix[1] * matrix[7] * matrix[16] + matrix[1] * matrix[15] * matrix[8] + matrix[3] * matrix[5] * matrix[16] - matrix[3] * matrix[13] * matrix[8] - matrix[4] * matrix[5] * matrix[15] + matrix[4] * matrix[13] * matrix[7]
out[11] = matrix[1] * matrix[6] * matrix[16] - matrix[1] * matrix[14] * matrix[8] - matrix[2] * matrix[5] * matrix[16] + matrix[2] * matrix[13] * matrix[8] + matrix[4] * matrix[5] * matrix[14] - matrix[4] * matrix[13] * matrix[6]
out[15] = -matrix[1] * matrix[6] * matrix[15] + matrix[1] * matrix[14] * matrix[7] + matrix[2] * matrix[5] * matrix[15] - matrix[2] * matrix[13] * matrix[7] - matrix[3] * matrix[5] * matrix[14] + matrix[3] * matrix[13] * matrix[6]
out[4] = -matrix[2] * matrix[7] * matrix[12] + matrix[2] * matrix[11] * matrix[8] + matrix[3] * matrix[6] * matrix[12] - matrix[3] * matrix[10] * matrix[8] - matrix[4] * matrix[6] * matrix[11] + matrix[4] * matrix[10] * matrix[7]
out[8] = matrix[1] * matrix[7] * matrix[12] - matrix[1] * matrix[11] * matrix[8] - matrix[3] * matrix[5] * matrix[12] + matrix[3] * matrix[9] * matrix[8] + matrix[4] * matrix[5] * matrix[11] - matrix[4] * matrix[9] * matrix[7]
out[12] = -matrix[1] * matrix[6] * matrix[12] + matrix[1] * matrix[10] * matrix[8] + matrix[2] * matrix[5] * matrix[12] - matrix[2] * matrix[9] * matrix[8] - matrix[4] * matrix[5] * matrix[10] + matrix[4] * matrix[9] * matrix[6]
out[16] = matrix[1] * matrix[6] * matrix[11] - matrix[1] * matrix[10] * matrix[7] - matrix[2] * matrix[5] * matrix[11] + matrix[2] * matrix[9] * matrix[7] + matrix[3] * matrix[5] * matrix[10] - matrix[3] * matrix[9] * matrix[6]
local det = matrix[1] * out[1] + matrix[5] * out[2] + matrix[9] * out[3] + matrix[13] * out[4]
if det == 0 then return matrix end
det = 1/det
for i=1,16 do
out[i] = out[i] * det
end
return out
end
function Matrix4.transpose(matrix)
local out = Matrix4()
for r=1,4 do
for c=1,4 do
out:set(r,c, matrix:get(c,r))
end
end
return out
end
function Matrix4.normal(matrix)
return matrix:invert():transpose()
end
------------------------------------------------------------------------
-- META
------------------------------------------------------------------------
function Matrix4:__len()
return 16
end
function Matrix4:__index(key)
if rawget(Matrix4, key) then
return rawget(Matrix4, key)
elseif type(key) == "number" or Matrix4.keys[key] then
if Matrix4.keys[key] then key = Matrix4.keys[key] end
return self.components[key-1]
else
return rawget(self, key)
end
end
function Matrix4:__newindex(key, value)
if type(key) == "number" or Matrix4.keys[key] then
if Matrix4.keys[key] then key = Matrix4.keys[key] end
self.components[key-1] = value
elseif self == Matrix4 then
rawset(self, key, value)
else
error("Matrix4 has no attribute \""..tostring(key).."\"")
end
end
function Matrix4:__tostring()
local out = ""
for r=1,4 do
if r > 1 then out = out .. "\n" end
out = out .. "|"
for c=1,4 do
if c > 1 then out = out .. "," end
out = out .. self:get(r,c)
end
out = out .. "|"
end
return out
end
function Matrix4.__concat(a,b)
return tostring(a) .. tostring(b)
end
function Matrix4:__call(...)
return Matrix4.new(...)
end
------------------------------------------------------------------------
-- OPERATORS
------------------------------------------------------------------------
function Matrix4.__mul(a,b)
if isMatrix4(b) and not isMatrix4(a) then a,b = b,a end
if isMatrix4(b) then
return a:mulMatrix4(b)
elseif isVector4(b) then
return Vector4.mulMatrix4(b,a)
else
error("Attempt to perform arithmetic between Matrix4 and "..type(b))
end
end
function Matrix4.mulMatrix4(a,b)
return Matrix4(
a.r1c1*b.r1c1 + a.r1c2*b.r2c1 + a.r1c3*b.r3c1 + a.r1c4*b.r4c1,
a.r1c1*b.r1c2 + a.r1c2*b.r2c2 + a.r1c3*b.r3c2 + a.r1c4*b.r4c2,
a.r1c1*b.r1c3 + a.r1c2*b.r2c3 + a.r1c3*b.r3c3 + a.r1c4*b.r4c3,
a.r1c1*b.r1c4 + a.r1c2*b.r2c4 + a.r1c3*b.r3c4 + a.r1c4*b.r4c4,
a.r2c1*b.r1c1 + a.r2c2*b.r2c1 + a.r2c3*b.r3c1 + a.r2c4*b.r4c1,
a.r2c1*b.r1c2 + a.r2c2*b.r2c2 + a.r2c3*b.r3c2 + a.r2c4*b.r4c2,
a.r2c1*b.r1c3 + a.r2c2*b.r2c3 + a.r2c3*b.r3c3 + a.r2c4*b.r4c3,
a.r2c1*b.r1c4 + a.r2c2*b.r2c4 + a.r2c3*b.r3c4 + a.r2c4*b.r4c4,
a.r3c1*b.r1c1 + a.r3c2*b.r2c1 + a.r3c3*b.r3c1 + a.r3c4*b.r4c1,
a.r3c1*b.r1c2 + a.r3c2*b.r2c2 + a.r3c3*b.r3c2 + a.r3c4*b.r4c2,
a.r3c1*b.r1c3 + a.r3c2*b.r2c3 + a.r3c3*b.r3c3 + a.r3c4*b.r4c3,
a.r3c1*b.r1c4 + a.r3c2*b.r2c4 + a.r3c3*b.r3c4 + a.r3c4*b.r4c4,
a.r4c1*b.r1c1 + a.r4c2*b.r2c1 + a.r4c3*b.r3c1 + a.r4c4*b.r4c1,
a.r4c1*b.r1c2 + a.r4c2*b.r2c2 + a.r4c3*b.r3c2 + a.r4c4*b.r4c2,
a.r4c1*b.r1c3 + a.r4c2*b.r2c3 + a.r4c3*b.r3c3 + a.r4c4*b.r4c3,
a.r4c1*b.r1c4 + a.r4c2*b.r2c4 + a.r4c3*b.r3c4 + a.r4c4*b.r4c4
)
end
function Matrix4.__eq(a,b)
if not (isMatrix4(a) and isMatrix4(b)) then
return false
end
for i=1,16 do
if not (a[i] == b[i]) then
return false
end
end
return true
end