- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.lua
More file actions
Latest commit
256 lines (237 loc) · 9.13 KB
/
Copy pathMatrix.lua
File metadata and controls
256 lines (237 loc) · 9.13 KB
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
localVec=require'Vector'
localvec2, vec3, vec4=Vec.vec2, Vec.vec3, Vec.vec4
localmat2
mat2=setmetatable({}, {
__call=function (t, m11, m21, m12, m22) returnsetmetatable({_type='mat2', m11=m11, m12=m12, m21=m21, m22=m22}, {
-- __add = function(a, b) return mat2(a.x + b.x, a.y + b.y) end,
-- __sub = function(a, b) return mat2(a.x - b.x, a.y - b.y) end,
__mul=function(a, b)
iftype(b) =='table' andb._type=='mat2' then
returnmat2(a.m11*b.m11+a.m21*b.m12, a.m11*b.m21+a.m21*b.m22,
a.m12*b.m11+a.m22*b.m12, a.m12*b.m21+a.m22*b.m22)
elseiftype(b) =='table' andb._type=='vec2' then
returnvec2(a.m11*b.x+a.m21*b.y,
a.m12*b.x+a.m22*b.y)
elseiftype(b) =='number' then
returnmat2(a.m11*b, a.m21*b,
a.m12*b, a.m22*b)
else
error('Unsupported input in mat2 __mul')
end
end,
__tostring=function(m) returnstring.format("[%4.1f %4.1f ]\n[%4.1f %4.1f ]",
m.m11, m.m21, m.m12, m.m22) end
}) end,
})
localmat3
mat3=setmetatable({}, {
__call=function (t,
m11, m21, m31,
m12, m22, m32,
m13, m23, m33)
returnsetmetatable({
_type='mat3',
m11=m11, m21=m21, m31=m31,
m12=m12, m22=m22, m32=m32,
m13=m13, m23=m23, m33=m33 }, {
-- __add = function(a, b) return mat3(a.x + b.x, a.y + b.y) end,
-- __sub = function(a, b) return mat3(a.x - b.x, a.y - b.y) end,
__mul=function(a, b)
iftype(b) =='table' andb._type=='mat3' then
returnmat3(
a.m11*b.m11+a.m21*b.m12+a.m31*b.m13, a.m11*b.m21+a.m21*b.m22+a.m31*b.m23, a.m11*b.m31+a.m21*b.m32+a.m31*b.m33,
a.m12*b.m11+a.m22*b.m12+a.m32*b.m13, a.m12*b.m21+a.m22*b.m22+a.m32*b.m23, a.m12*b.m31+a.m22*b.m32+a.m32*b.m33,
a.m13*b.m11+a.m23*b.m12+a.m33*b.m13, a.m13*b.m21+a.m23*b.m22+a.m33*b.m23, a.m13*b.m31+a.m23*b.m32+a.m33*b.m33)
elseiftype(b) =='table' andb._type=='vec3' then
returnvec3(
a.m11*b.x+a.m21*b.y+a.m31*b.z,
a.m12*b.x+a.m22*b.y+a.m32*b.z,
a.m13*b.x+a.m23*b.y+a.m33*b.z)
elseiftype(b) =='number' then
returnmat3(
a.m11*b, a.m21*b, a.m31*b,
a.m12*b, a.m22*b, a.m32*b,
a.m13*b, a.m23*b, a.m33*b)
else
error('Unsupported input in mat3 __mul')
end
end,
__tostring=function(m) returnstring.format("[%4.1f %4.1f %4.1f ]\n[%4.1f %4.1f %4.1f ]\n[%4.1f %4.1f %4.1f ]",
m.m11, m.m21, m.m31, m.m12, m.m22, m.m32, m.m13, m.m23, m.m33) end
}) end,
})
localmat4
mat4=setmetatable({}, {
__call=function (t,
m11, m21, m31, m41,
m12, m22, m32, m42,
m13, m23, m33, m43,
m14, m24, m34, m44)
returnsetmetatable({
_type='mat4',
m11=m11, m21=m21, m31=m31, m41=m41,
m12=m12, m22=m22, m32=m32, m42=m42,
m13=m13, m23=m23, m33=m33, m43=m43,
m14=m14, m24=m24, m34=m34, m44=m44 }, {
-- __add = function(a, b) return mat4(a.x + b.x, a.y + b.y) end,
-- __sub = function(a, b) return mat4(a.x - b.x, a.y - b.y) end,
__mul=function(a, b)
iftype(b) =='table' andb._type=='mat4' then
returnmat4(
a.m11*b.m11+a.m21*b.m12+a.m31*b.m13+a.m41*b.m14,
a.m11*b.m21+a.m21*b.m22+a.m31*b.m23+a.m41*b.m24,
a.m11*b.m31+a.m21*b.m32+a.m31*b.m33+a.m41*b.m34,
a.m11*b.m41+a.m21*b.m42+a.m31*b.m43+a.m41*b.m44,
a.m12*b.m11+a.m22*b.m12+a.m32*b.m13+a.m42*b.m14,
a.m12*b.m21+a.m22*b.m22+a.m32*b.m23+a.m42*b.m24,
a.m12*b.m31+a.m22*b.m32+a.m32*b.m33+a.m42*b.m34,
a.m12*b.m41+a.m22*b.m42+a.m32*b.m43+a.m42*b.m44,
a.m13*b.m11+a.m23*b.m12+a.m33*b.m13+a.m43*b.m14,
a.m13*b.m21+a.m23*b.m22+a.m33*b.m23+a.m43*b.m24,
a.m13*b.m31+a.m23*b.m32+a.m33*b.m33+a.m43*b.m34,
a.m13*b.m41+a.m23*b.m42+a.m33*b.m43+a.m43*b.m44,
a.m14*b.m11+a.m24*b.m12+a.m34*b.m13+a.m44*b.m14,
a.m14*b.m21+a.m24*b.m22+a.m34*b.m23+a.m44*b.m24,
a.m14*b.m31+a.m24*b.m32+a.m34*b.m33+a.m44*b.m34,
a.m14*b.m41+a.m24*b.m42+a.m34*b.m43+a.m44*b.m44)
elseiftype(b) =='table' andb._type=='vec4' then
returnvec4(
a.m11*b.x+a.m21*b.y+a.m31*b.z+a.m41*b.w,
a.m12*b.x+a.m22*b.y+a.m32*b.z+a.m42*b.w,
a.m13*b.x+a.m23*b.y+a.m33*b.z+a.m43*b.w,
a.m14*b.x+a.m24*b.y+a.m34*b.z+a.m44*b.w)
elseiftype(b) =='number' then
returnmat4(
a.m11*b, a.m21*b, a.m31*b, a.m41*b,
a.m12*b, a.m22*b, a.m32*b, a.m42*b,
a.m13*b, a.m23*b, a.m33*b, a.m43*b,
a.m14*b, a.m24*b, a.m34*b, a.m44*b)
else
error('Unsupported input in mat4 __mul')
end
end,
__tostring=function(m) returnstring.format("[%4.1f %4.1f %4.1f %4.1f ]\n[%4.1f %4.1f %4.1f %4.1f ]\n[%4.1f %4.1f %4.1f %4.1f ]\n[%4.1f %4.1f %4.1f %4.1f ]",
m.m11, m.m21, m.m31, m.m41, m.m12, m.m22, m.m32, m.m42, m.m13, m.m23, m.m33, m.m43, m.m14, m.m24, m.m34, m.m44) end
}) end,
})
localmath=require'math'
localsin, cos=math.sin, math.cos
localfunctionrotate2(r)
returnmat2(cos(r), -sin(r),
sin(r), cos(r))
end
localfunctionrotate3x(r)
returnmat3(1, 0, 0,
0, cos(r), -sin(r),
0, sin(r), cos(r))
end
localfunctionrotate3y(r)
returnmat3(cos(r), 0, sin(r),
0, 1, 0,
-sin(r), 0, cos(r))
end
localfunctionrotate3z(r)
returnmat3(cos(r), -sin(r), 0,
sin(r), cos(r), 0,
0, 0, 1)
end
localfunctionrotate3(rx, ry, rz)
returnrotate3x(rx) *rotate3y(ry) *rotate3z(rz)
end
localfunctionrotate4x(r)
returnmat4(1, 0, 0, 0,
0, cos(r), -sin(r), 0,
0, sin(r), cos(r), 0,
0, 0, 0, 1)
end
localfunctionrotate4y(r)
returnmat4(cos(r), 0, sin(r), 0,
0, 1, 0, 0,
-sin(r), 0, cos(r), 0,
0, 0, 0, 1)
end
localfunctionrotate4z(r)
returnmat4(cos(r), -sin(r), 0, 0,
sin(r), cos(r), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1)
end
localfunctionrotate4(rx, ry, rz)
returnrotate4x(rx) *rotate4y(ry) *rotate4z(rz)
end
localM= {}
M.mat2=mat2
M.mat3=mat3
M.mat4=mat4
M.mat=mat4
M.rotate2=rotate2
M.rotate3x=rotate3x
M.rotate3y=rotate3y
M.rotate3z=rotate3z
M.rotate3=rotate3
M.rotate4x=rotate4x
M.rotate4y=rotate4y
M.rotate4z=rotate4z
M.rotate4=rotate4
M.rotatex=rotate4x
M.rotatey=rotate4y
M.rotatez=rotate4z
M.rotate=rotate4
M.identity2=mat2(1, 0,
0, 1)
M.identity3=mat3(1, 0, 0,
0, 1, 0,
0, 0, 1)
M.identity4=mat4(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1)
M.identity=M.identity4
M.scale=function ( sx, sy, sz )
sy=syor1
sz=szor1
returnmat4(sx, 0, 0, 0,
0, sy, 0, 0,
0, 0, sz, 0,
0, 0, 0, 1)
end
M.translate=function (x, y, z)
y=yor0
z=zor0
returnmat4(1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, z,
0, 0, 0, 1)
end
functionM.frustum(l, r, b, t, n, f)
returnmat4(2*n/(r-l), 0, (r+l)/(r-l), 0,
0, 2*n/(t-b), (t+b)/(t-b), 0,
0, 0, -(f+n)/(f-n), -2*n*f/(f-n),
0, 0, -1, 0)
end
functionM.perspective(fovy, aspect, n, f)
localt=n*math.tan(fovy*math.pi/360.0)
localr=t*aspect
returnM.frustum(-r, r, -t, t, n, f)
end
functionM.ortho(l, r, b, t, n, f)
returnmat4(2/(r-l), 0, 0, -(r+l)/(r-l),
0, 2/(t-b), 0, -(t+b)/(t-b),
0, 0, -2/(f-n), -(f+n)/(f-n),
0, 0, 0, 1)
end
functionM.lookat( cameraPosition, cameraTarget, upVector)
iftype(cameraPosition) =='table' andcameraPosition._type=='vec3' and
type(cameraTarget) =='table' andcameraTarget._type=='vec3' and
type(upVector) =='table' andupVector._type=='vec3' then
localf=Vec.normalize3(cameraTarget-cameraPosition)
locals=Vec.normalize3(Vec.cross3(f, upVector))
localu=Vec.cross3(s, f)
returnmat4(
s.x, s.y, s.z, -cameraPosition.x,
u.x, u.y, u.z, -cameraPosition.y,
-f.x, -f.y, -f.z, -cameraPosition.z,
0, 0, 0, 1)
end
end
returnM