summaryrefslogtreecommitdiff
path: root/demo/Matrix.lua
blob: d07944e765bf4a1f2cbe234d1053610ef46122c2 (plain)
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
local Vector = require('Vector')

local Matrix = {}

Matrix.Mat3 = {}

Matrix.Mat3.prototype = {}

Matrix.Mat3.prototype.at = function(self, i, j)
   return honey.cglm.get_value(self.array, (i-1) + 3*(j-1))
end

Matrix.Mat3.prototype.setAt = function(self, i, j, value)
   honey.cglm.set_value(self.array, (i-1) + 3*(j-1), value)
end

Matrix.Mat3.prototype.clone = function(self)
   local clone = honey.cglm.new_array_zero(9)
   clone.array = honey.cglm.copy_array(self.array, 9)
   return clone
end

Matrix.Mat3.prototype.mul = function(self, M, dest)
   local result
   if dest == nil then
      result = Matrix.Mat3.new()
   else
      result = dest
   end

   honey.cglm.mat3.mul(self.array, M.array, result)
   return result
end

Matrix.Mat3.prototype.muls = function(self, s, dest)
   local result
   if dest == nil then
      result = Matrix.Mat3.new()
   else
      result = dest
   end

   honey.cglm.mat3.muls(s, self.array, result)
   return result
end

Matrix.Mat3.prototype.mulv = function(self, v, dest)
   local result
   if dest == nil then
      result = Matrix.Vec3.new()
   else
      result = dest
   end

   honey.cglm.mat3.mulv(self.array, v.array, result)
   return result
end
 
Matrix.Mat3.prototype.T = function(self, auto)
   auto = auto or false   
   if auto then
      honey.cglm.mat3.trans(self.array)
      return
   end

   local M = self.clone()
      
   honey.cglm.mat3.trans(M.array)
   return M
end

Matrix.Mat3.prototype.det = function(self)
   return honey.cglm.mat3.det(self.array)
end

Matrix.Mat3.prototype.trace = function(self)
   return honey.cglm.mat3.trace(self.array)
end

Matrix.Mat3.prototype.inv = function(self, dest)
   local result
   if dest == nil then
      result = Matrix.Mat3.new()
   else
      result = dest
   end

   honey.cglm.mat3.inv(self.array, result.array)
   return result
end

Matrix.Mat3.prototype.basis = function(self)
   local x0 = self:at(1,1)
   local x1 = self:at(2,1)
   local x2 = self:at(3,1)

   local y0 = self:at(1,2)
   local y1 = self:at(2,2)
   local y2 = self:at(3,2)

   local z0 = self:at(1,3)
   local z1 = self:at(2,3)
   local z2 = self:at(3,3)
   
   local b = {}
   b.x = Vector.Vec3.new{x0, x1, x2}
   b.y = Vector.Vec3.new{y0, y1, y2}
   b.z = Vector.Vec3.new{z0, z1, z2}

   return b
end
   
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Matrix.Mat3.mt = {}

Matrix.Mat3.mt.__index = Matrix.Mat3.prototype
Matrix.Mat3.mt.__tostring = function(mat3)
   local line1 = 'mat3('..
      tostring(mat3:at(1,1))..', '..
      tostring(mat3:at(1,2))..', '..
      tostring(mat3:at(1,3))..', \n'
   local line2 = '    '..
      tostring(mat3:at(2,1))..', '..
      tostring(mat3:at(2,2))..', '..
      tostring(mat3:at(2,3))..', \n'
   local line3 = '    '..
      tostring(mat3:at(3,1))..', '..
      tostring(mat3:at(3,2))..', '..
      tostring(mat3:at(3,3))..')'
   return line1..line2..line3
end

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Matrix.Mat3.new = function(tbl)
   local mat3 = {}
   mat3.array = honey.cglm.new_array_zero(9)
   setmetatable(mat3, Matrix.Mat3.mt)

   if tbl == nil then return mat3 end
   
   for i = 1,3 do
      for j = 1,3 do
	 mat3:setAt(i,j, tbl[3*(i-1) + j])
      end
   end

    return mat3
end

Matrix.Mat3.eye = function()
   local eye3 = Matrix.Mat3.new()
   honey.cglm.mat3.identity(eye3.array)
   return eye3
end

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
-- Mat4
--
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Matrix.Mat4 = {}

Matrix.Mat4.prototype = {}

Matrix.Mat4.prototype.at = function(self, i, j)
   return honey.cglm.get_value(self.array, (i-1) + 4*(j-1))
end

Matrix.Mat4.prototype.setAt = function(self, i, j, value)
   honey.cglm.set_value(self.array, (i-1) + 4*(j-1), value)
end

Matrix.Mat4.prototype.clone = function(self)
   local clone = honey.cglm.new_array_zero(16)
   clone.array = honey.cglm.copy_array(self.array, 16)
   return clone
end

Matrix.Mat4.prototype.pick3 = function(self, dest)
   local result
   if dest == nil then
      result = Matrix.Mat3.new()
   else
      result = dest
   end

   honey.cglm.mat4.pick3(self.array, result.array)
   return result
end

Matrix.Mat4.prototype.mul = function(self, M, dest)
   local result
   if dest == nil then
      result = Matrix.Mat4.new()
   else
      result = dest
   end

   honey.cglm.mat4.mul(self.array, M.array, result.array)
   return result
end

Matrix.Mat4.prototype.muls = function(self, s, dest)
   local result
   if dest == nil then
      result = Matrix.Mat4.new()
   else
      result = dest
   end

   honey.cglm.mat4.muls(s, self.array, result)
   return result
end

Matrix.Mat4.prototype.mulv = function(self, v, dest)
   local result
   if dest == nil then
      result = Matrix.Vec4.new()
   else
      result = dest
   end

   honey.cglm.mat4.mulv(self.array, v.array, result)
   return result
end
 
Matrix.Mat4.prototype.T = function(self, auto)
   auto = auto or false   
   if auto then
      honey.cglm.mat4.trans(self.array)
      return
   end

   local M = self.clone()
      
   honey.cglm.mat4.trans(M.array)
   return M
end

Matrix.Mat4.prototype.det = function(self)
   return honey.cglm.mat4.det(self.array)
end

Matrix.Mat4.prototype.trace = function(self)
   return honey.cglm.mat4.trace(self.array)
end

Matrix.Mat4.prototype.inv = function(self, dest)
   local result
   if dest == nil then
      result = Matrix.Mat4.new()
   else
      result = dest
   end

   honey.cglm.mat4.inv(self.array, result.array)
   return result
end

Matrix.Mat4.prototype.fastInv = function(self, dest)
   local result
   if dest == nil then
      result = Matrix.Mat4.new()
   else
      result = dest
   end

   honey.cglm.mat4.inv_fast(self.array, result.array)
   return result
end

Matrix.Mat4.prototype.basis = function(self)
   local M = self:pick3()

   return M:basis()
end

Matrix.Mat4.prototype.rotate = function(self, center, axis, angle)
   honey.cglm.affine.rotate(self.array, center.array, axis.array, angle)
end

Matrix.Mat4.prototype.translate = function(self, v)
   honey.cglm.affine.translate(self.array, v.array)
end

Matrix.Mat4.prototype.scale = function(self, v)
   honey.cglm.affine.scale(self.array, v.array)
end

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Matrix.Mat4.mt = {}

Matrix.Mat4.mt.__index = Matrix.Mat4.prototype
Matrix.Mat4.mt.__tostring = function(mat4)
   local line1 = 'mat4('..
      tostring(mat4:at(1,1))..', '..
      tostring(mat4:at(1,2))..', '..
      tostring(mat4:at(1,3))..', '..
      tostring(mat4:at(1,4))..', \n'
   local line2 = '    '..
      tostring(mat4:at(2,1))..', '..
      tostring(mat4:at(2,2))..', '..
      tostring(mat4:at(2,3))..', '..
      tostring(mat4:at(2,4))..', \n'
   local line3 = '    '..
      tostring(mat4:at(3,1))..', '..
      tostring(mat4:at(3,2))..', '..
      tostring(mat4:at(3,3))..', '..
      tostring(mat4:at(3,4))..')'
   local line4 = '    '..
      tostring(mat4:at(4,1))..', '..
      tostring(mat4:at(4,2))..', '..
      tostring(mat4:at(4,3))..', '..
      tostring(mat4:at(4,4))..')'
   return line1..line2..line4
end

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Matrix.Mat4.new = function(tbl)
   local mat4 = {}
   mat4.array = honey.cglm.new_array_zero(16)
   setmetatable(mat4, Matrix.Mat4.mt)

   if tbl == nil then return mat4 end
   
   for i = 1,4 do
      for j = 1,4 do
	 mat4:setAt(i,j, tbl[4*(i-1) + j])
      end
   end

    return mat4
end

Matrix.Mat4.eye = function()
   local eye4 = Matrix.Mat4.new()
   honey.cglm.mat4.identity(eye4.array)
   return eye4
end

Matrix.Mat4.perspective = function(fov, aspectRatio, near, far)
   local M = Matrix.Mat4.new()
   honey.cglm.camera.perspective(M.array, fov, aspectRatio, near, far)
   return M
end

Matrix.Mat4.orthographic = function(a, b)
   local M = Matrix.Mat4.new()
   honey.cglm.camera.orthographic(M.array, a.array, b.array)
   return M
end

Matrix.Mat4.look = function(position, direction, up, dest)
   local result
   if dest == nil then
      result = Matrix.Mat4.new()
   else
      result = dest
   end

   honey.cglm.camera.look(position.array,
			  direction.array,
			  up.array,
			  dest.array)
   return dest
end

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

return Matrix