-
Notifications
You must be signed in to change notification settings - Fork 8
/
Mat4.pas
315 lines (280 loc) · 15.9 KB
/
Mat4.pas
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
{$mode objfpc}
unit Mat4;
interface
uses
BrowserConsole, JS, Math;
type
TScalar = single;
TScalarArray = array of TScalar;
type
TMat4 = class
public
constructor Identity;
constructor Translate(const tx,ty,tz: TScalar);
constructor RotateZ(const Angle:TScalar);
constructor RotateY(const Angle:TScalar);
constructor RotateX(const Angle:TScalar);
constructor Scale (const x, y, z: TScalar);
constructor Ortho(const Left,Right,Bottom,Top,zNear,zFar:TScalar);
constructor Perspective(const fovy,Aspect,zNear,zFar:TScalar);
function Multiply (m: TMat4): TMat4;
function Inverse: TMat4;
function CopyList: TScalarArray;
// property Values[const key:TDictionaryKey]:T read GetValue write SetValue; default;
procedure Show;
public
// TODO: multi dimensional arrays aren't going to work
// for webgl so replace this with something else
RawComponents:array[0..3, 0..3] of TScalar;
end;
procedure SinCos (angle: TScalar; var sinus: TScalar; var cosinus: TScalar);
implementation
const
PI = 3.14159265359;
DEG2RAD = PI/180.0;
RAD2DEG = 180.0/PI;
HalfPI = PI*0.5;
var
Matrix4x4Identity: TMat4;
// TODO: this should be in math
procedure SinCos (angle: TScalar; var sinus: TScalar; var cosinus: TScalar);
begin
sinus := sin(angle);
cosinus := cos(angle);
end;
// NOTE: [][] doesn't typecast to array of syntax so we need this
function TMat4.CopyList: TScalarArray;
var
x, y: integer;
list: TJSArray;
begin
list := TJSArray.new;
for x := 0 to 3 do
for y := 0 to 3 do
list.push(RawComponents[x,y]);
result := TScalarArray(list);
end;
function TMat4.Inverse:TMat4;
var
t0,t4,t8,t12,d:TScalar;
begin
t0:=(((RawComponents[1,1]*RawComponents[2,2]*RawComponents[3,3])-(RawComponents[1,1]*RawComponents[2,3]*RawComponents[3,2]))-(RawComponents[2,1]*RawComponents[1,2]*RawComponents[3,3])+(RawComponents[2,1]*RawComponents[1,3]*RawComponents[3,2])+(RawComponents[3,1]*RawComponents[1,2]*RawComponents[2,3]))-(RawComponents[3,1]*RawComponents[1,3]*RawComponents[2,2]);
t4:=((((-(RawComponents[1,0]*RawComponents[2,2]*RawComponents[3,3]))+(RawComponents[1,0]*RawComponents[2,3]*RawComponents[3,2])+(RawComponents[2,0]*RawComponents[1,2]*RawComponents[3,3]))-(RawComponents[2,0]*RawComponents[1,3]*RawComponents[3,2]))-(RawComponents[3,0]*RawComponents[1,2]*RawComponents[2,3]))+(RawComponents[3,0]*RawComponents[1,3]*RawComponents[2,2]);
t8:=((((RawComponents[1,0]*RawComponents[2,1]*RawComponents[3,3])-(RawComponents[1,0]*RawComponents[2,3]*RawComponents[3,1]))-(RawComponents[2,0]*RawComponents[1,1]*RawComponents[3,3]))+(RawComponents[2,0]*RawComponents[1,3]*RawComponents[3,1])+(RawComponents[3,0]*RawComponents[1,1]*RawComponents[2,3]))-(RawComponents[3,0]*RawComponents[1,3]*RawComponents[2,1]);
t12:=((((-(RawComponents[1,0]*RawComponents[2,1]*RawComponents[3,2]))+(RawComponents[1,0]*RawComponents[2,2]*RawComponents[3,1])+(RawComponents[2,0]*RawComponents[1,1]*RawComponents[3,2]))-(RawComponents[2,0]*RawComponents[1,2]*RawComponents[3,1]))-(RawComponents[3,0]*RawComponents[1,1]*RawComponents[2,2]))+(RawComponents[3,0]*RawComponents[1,2]*RawComponents[2,1]);
d:=(RawComponents[0,0]*t0)+(RawComponents[0,1]*t4)+(RawComponents[0,2]*t8)+(RawComponents[0,3]*t12);
result := TMat4.Identity;
if d<>0.0 then begin
d:=1.0/d;
result.RawComponents[0,0]:=t0*d;
result.RawComponents[0,1]:=(((((-(RawComponents[0,1]*RawComponents[2,2]*RawComponents[3,3]))+(RawComponents[0,1]*RawComponents[2,3]*RawComponents[3,2])+(RawComponents[2,1]*RawComponents[0,2]*RawComponents[3,3]))-(RawComponents[2,1]*RawComponents[0,3]*RawComponents[3,2]))-(RawComponents[3,1]*RawComponents[0,2]*RawComponents[2,3]))+(RawComponents[3,1]*RawComponents[0,3]*RawComponents[2,2]))*d;
result.RawComponents[0,2]:=(((((RawComponents[0,1]*RawComponents[1,2]*RawComponents[3,3])-(RawComponents[0,1]*RawComponents[1,3]*RawComponents[3,2]))-(RawComponents[1,1]*RawComponents[0,2]*RawComponents[3,3]))+(RawComponents[1,1]*RawComponents[0,3]*RawComponents[3,2])+(RawComponents[3,1]*RawComponents[0,2]*RawComponents[1,3]))-(RawComponents[3,1]*RawComponents[0,3]*RawComponents[1,2]))*d;
result.RawComponents[0,3]:=(((((-(RawComponents[0,1]*RawComponents[1,2]*RawComponents[2,3]))+(RawComponents[0,1]*RawComponents[1,3]*RawComponents[2,2])+(RawComponents[1,1]*RawComponents[0,2]*RawComponents[2,3]))-(RawComponents[1,1]*RawComponents[0,3]*RawComponents[2,2]))-(RawComponents[2,1]*RawComponents[0,2]*RawComponents[1,3]))+(RawComponents[2,1]*RawComponents[0,3]*RawComponents[1,2]))*d;
result.RawComponents[1,0]:=t4*d;
result.RawComponents[1,1]:=((((RawComponents[0,0]*RawComponents[2,2]*RawComponents[3,3])-(RawComponents[0,0]*RawComponents[2,3]*RawComponents[3,2]))-(RawComponents[2,0]*RawComponents[0,2]*RawComponents[3,3])+(RawComponents[2,0]*RawComponents[0,3]*RawComponents[3,2])+(RawComponents[3,0]*RawComponents[0,2]*RawComponents[2,3]))-(RawComponents[3,0]*RawComponents[0,3]*RawComponents[2,2]))*d;
result.RawComponents[1,2]:=(((((-(RawComponents[0,0]*RawComponents[1,2]*RawComponents[3,3]))+(RawComponents[0,0]*RawComponents[1,3]*RawComponents[3,2])+(RawComponents[1,0]*RawComponents[0,2]*RawComponents[3,3]))-(RawComponents[1,0]*RawComponents[0,3]*RawComponents[3,2]))-(RawComponents[3,0]*RawComponents[0,2]*RawComponents[1,3]))+(RawComponents[3,0]*RawComponents[0,3]*RawComponents[1,2]))*d;
result.RawComponents[1,3]:=(((((RawComponents[0,0]*RawComponents[1,2]*RawComponents[2,3])-(RawComponents[0,0]*RawComponents[1,3]*RawComponents[2,2]))-(RawComponents[1,0]*RawComponents[0,2]*RawComponents[2,3]))+(RawComponents[1,0]*RawComponents[0,3]*RawComponents[2,2])+(RawComponents[2,0]*RawComponents[0,2]*RawComponents[1,3]))-(RawComponents[2,0]*RawComponents[0,3]*RawComponents[1,2]))*d;
result.RawComponents[2,0]:=t8*d;
result.RawComponents[2,1]:=(((((-(RawComponents[0,0]*RawComponents[2,1]*RawComponents[3,3]))+(RawComponents[0,0]*RawComponents[2,3]*RawComponents[3,1])+(RawComponents[2,0]*RawComponents[0,1]*RawComponents[3,3]))-(RawComponents[2,0]*RawComponents[0,3]*RawComponents[3,1]))-(RawComponents[3,0]*RawComponents[0,1]*RawComponents[2,3]))+(RawComponents[3,0]*RawComponents[0,3]*RawComponents[2,1]))*d;
result.RawComponents[2,2]:=(((((RawComponents[0,0]*RawComponents[1,1]*RawComponents[3,3])-(RawComponents[0,0]*RawComponents[1,3]*RawComponents[3,1]))-(RawComponents[1,0]*RawComponents[0,1]*RawComponents[3,3]))+(RawComponents[1,0]*RawComponents[0,3]*RawComponents[3,1])+(RawComponents[3,0]*RawComponents[0,1]*RawComponents[1,3]))-(RawComponents[3,0]*RawComponents[0,3]*RawComponents[1,1]))*d;
result.RawComponents[2,3]:=(((((-(RawComponents[0,0]*RawComponents[1,1]*RawComponents[2,3]))+(RawComponents[0,0]*RawComponents[1,3]*RawComponents[2,1])+(RawComponents[1,0]*RawComponents[0,1]*RawComponents[2,3]))-(RawComponents[1,0]*RawComponents[0,3]*RawComponents[2,1]))-(RawComponents[2,0]*RawComponents[0,1]*RawComponents[1,3]))+(RawComponents[2,0]*RawComponents[0,3]*RawComponents[1,1]))*d;
result.RawComponents[3,0]:=t12*d;
result.RawComponents[3,1]:=(((((RawComponents[0,0]*RawComponents[2,1]*RawComponents[3,2])-(RawComponents[0,0]*RawComponents[2,2]*RawComponents[3,1]))-(RawComponents[2,0]*RawComponents[0,1]*RawComponents[3,2]))+(RawComponents[2,0]*RawComponents[0,2]*RawComponents[3,1])+(RawComponents[3,0]*RawComponents[0,1]*RawComponents[2,2]))-(RawComponents[3,0]*RawComponents[0,2]*RawComponents[2,1]))*d;
result.RawComponents[3,2]:=(((((-(RawComponents[0,0]*RawComponents[1,1]*RawComponents[3,2]))+(RawComponents[0,0]*RawComponents[1,2]*RawComponents[3,1])+(RawComponents[1,0]*RawComponents[0,1]*RawComponents[3,2]))-(RawComponents[1,0]*RawComponents[0,2]*RawComponents[3,1]))-(RawComponents[3,0]*RawComponents[0,1]*RawComponents[1,2]))+(RawComponents[3,0]*RawComponents[0,2]*RawComponents[1,1]))*d;
result.RawComponents[3,3]:=(((((RawComponents[0,0]*RawComponents[1,1]*RawComponents[2,2])-(RawComponents[0,0]*RawComponents[1,2]*RawComponents[2,1]))-(RawComponents[1,0]*RawComponents[0,1]*RawComponents[2,2]))+(RawComponents[1,0]*RawComponents[0,2]*RawComponents[2,1])+(RawComponents[2,0]*RawComponents[0,1]*RawComponents[1,2]))-(RawComponents[2,0]*RawComponents[0,2]*RawComponents[1,1]))*d;
end;
end;
function TMat4.Multiply (m: TMat4): TMat4;
begin
result := TMat4.Identity;
result.RawComponents[0,0]:=(m.RawComponents[0,0]*self.RawComponents[0,0])+(m.RawComponents[0,1]*self.RawComponents[1,0])+(m.RawComponents[0,2]*self.RawComponents[2,0])+(m.RawComponents[0,3]*self.RawComponents[3,0]);
result.RawComponents[0,1]:=(m.RawComponents[0,0]*self.RawComponents[0,1])+(m.RawComponents[0,1]*self.RawComponents[1,1])+(m.RawComponents[0,2]*self.RawComponents[2,1])+(m.RawComponents[0,3]*self.RawComponents[3,1]);
result.RawComponents[0,2]:=(m.RawComponents[0,0]*self.RawComponents[0,2])+(m.RawComponents[0,1]*self.RawComponents[1,2])+(m.RawComponents[0,2]*self.RawComponents[2,2])+(m.RawComponents[0,3]*self.RawComponents[3,2]);
result.RawComponents[0,3]:=(m.RawComponents[0,0]*self.RawComponents[0,3])+(m.RawComponents[0,1]*self.RawComponents[1,3])+(m.RawComponents[0,2]*self.RawComponents[2,3])+(m.RawComponents[0,3]*self.RawComponents[3,3]);
result.RawComponents[1,0]:=(m.RawComponents[1,0]*self.RawComponents[0,0])+(m.RawComponents[1,1]*self.RawComponents[1,0])+(m.RawComponents[1,2]*self.RawComponents[2,0])+(m.RawComponents[1,3]*self.RawComponents[3,0]);
result.RawComponents[1,1]:=(m.RawComponents[1,0]*self.RawComponents[0,1])+(m.RawComponents[1,1]*self.RawComponents[1,1])+(m.RawComponents[1,2]*self.RawComponents[2,1])+(m.RawComponents[1,3]*self.RawComponents[3,1]);
result.RawComponents[1,2]:=(m.RawComponents[1,0]*self.RawComponents[0,2])+(m.RawComponents[1,1]*self.RawComponents[1,2])+(m.RawComponents[1,2]*self.RawComponents[2,2])+(m.RawComponents[1,3]*self.RawComponents[3,2]);
result.RawComponents[1,3]:=(m.RawComponents[1,0]*self.RawComponents[0,3])+(m.RawComponents[1,1]*self.RawComponents[1,3])+(m.RawComponents[1,2]*self.RawComponents[2,3])+(m.RawComponents[1,3]*self.RawComponents[3,3]);
result.RawComponents[2,0]:=(m.RawComponents[2,0]*self.RawComponents[0,0])+(m.RawComponents[2,1]*self.RawComponents[1,0])+(m.RawComponents[2,2]*self.RawComponents[2,0])+(m.RawComponents[2,3]*self.RawComponents[3,0]);
result.RawComponents[2,1]:=(m.RawComponents[2,0]*self.RawComponents[0,1])+(m.RawComponents[2,1]*self.RawComponents[1,1])+(m.RawComponents[2,2]*self.RawComponents[2,1])+(m.RawComponents[2,3]*self.RawComponents[3,1]);
result.RawComponents[2,2]:=(m.RawComponents[2,0]*self.RawComponents[0,2])+(m.RawComponents[2,1]*self.RawComponents[1,2])+(m.RawComponents[2,2]*self.RawComponents[2,2])+(m.RawComponents[2,3]*self.RawComponents[3,2]);
result.RawComponents[2,3]:=(m.RawComponents[2,0]*self.RawComponents[0,3])+(m.RawComponents[2,1]*self.RawComponents[1,3])+(m.RawComponents[2,2]*self.RawComponents[2,3])+(m.RawComponents[2,3]*self.RawComponents[3,3]);
result.RawComponents[3,0]:=(m.RawComponents[3,0]*self.RawComponents[0,0])+(m.RawComponents[3,1]*self.RawComponents[1,0])+(m.RawComponents[3,2]*self.RawComponents[2,0])+(m.RawComponents[3,3]*self.RawComponents[3,0]);
result.RawComponents[3,1]:=(m.RawComponents[3,0]*self.RawComponents[0,1])+(m.RawComponents[3,1]*self.RawComponents[1,1])+(m.RawComponents[3,2]*self.RawComponents[2,1])+(m.RawComponents[3,3]*self.RawComponents[3,1]);
result.RawComponents[3,2]:=(m.RawComponents[3,0]*self.RawComponents[0,2])+(m.RawComponents[3,1]*self.RawComponents[1,2])+(m.RawComponents[3,2]*self.RawComponents[2,2])+(m.RawComponents[3,3]*self.RawComponents[3,2]);
result.RawComponents[3,3]:=(m.RawComponents[3,0]*self.RawComponents[0,3])+(m.RawComponents[3,1]*self.RawComponents[1,3])+(m.RawComponents[3,2]*self.RawComponents[2,3])+(m.RawComponents[3,3]*self.RawComponents[3,3]);
end;
constructor TMat4.Scale (const x, y, z: TScalar);
begin
RawComponents[0,0] := x;
RawComponents[0,1] := 0;
RawComponents[0,2] := 0;
RawComponents[0,3] := 0;
RawComponents[1,0] := 0;
RawComponents[1,1] := y;
RawComponents[1,2] := 0;
RawComponents[1,3] := 0;
RawComponents[2,0] := 0;
RawComponents[2,1] := 0;
RawComponents[2,2] := z;
RawComponents[2,3] := 0;
RawComponents[3,0] := 0;
RawComponents[3,1] := 0;
RawComponents[3,2] := 0;
RawComponents[3,3] := 1;
end;
constructor TMat4.RotateX(const Angle:TScalar);
begin
RawComponents[0,0]:=1.0;
RawComponents[0,1]:=0.0;
RawComponents[0,2]:=0.0;
RawComponents[0,3]:=0.0;
RawComponents[1,0]:=0.0;
SinCos(Angle,RawComponents[1,2],RawComponents[1,1]);
RawComponents[1,3]:=0.0;
RawComponents[2,0]:=0.0;
RawComponents[2,1]:=-RawComponents[1,2];
RawComponents[2,2]:=RawComponents[1,1];
RawComponents[2,3]:=0.0;
RawComponents[3,0]:=0.0;
RawComponents[3,1]:=0.0;
RawComponents[3,2]:=0.0;
RawComponents[3,3]:=1.0;
end;
constructor TMat4.RotateY(const Angle:TScalar);
begin
SinCos(Angle,RawComponents[2,0],RawComponents[0,0]);
RawComponents[0,1]:=0.0;
RawComponents[0,2]:=-RawComponents[2,0];
RawComponents[0,3]:=0.0;
RawComponents[1,0]:=0.0;
RawComponents[1,1]:=1.0;
RawComponents[1,2]:=0.0;
RawComponents[1,3]:=0.0;
RawComponents[2,1]:=0.0;
RawComponents[2,2]:=RawComponents[0,0];
RawComponents[2,3]:=0.0;
RawComponents[3,0]:=0.0;
RawComponents[3,1]:=0.0;
RawComponents[3,2]:=0.0;
RawComponents[3,3]:=1.0;
end;
constructor TMat4.RotateZ(const Angle:TScalar);
begin
SinCos(Angle,RawComponents[0,1],RawComponents[0,0]);
RawComponents[0,2]:=0.0;
RawComponents[0,3]:=0.0;
RawComponents[1,0]:=-RawComponents[0,1];
RawComponents[1,1]:=RawComponents[0,0];
RawComponents[1,2]:=0.0;
RawComponents[1,3]:=0.0;
RawComponents[2,0]:=0.0;
RawComponents[2,1]:=0.0;
RawComponents[2,2]:=1.0;
RawComponents[2,3]:=0.0;
RawComponents[3,0]:=0.0;
RawComponents[3,1]:=0.0;
RawComponents[3,2]:=0.0;
RawComponents[3,3]:=1.0;
end;
constructor TMat4.Translate(const tx,ty,tz: TScalar);
begin
RawComponents[0,0]:=1.0;
RawComponents[0,1]:=0.0;
RawComponents[0,2]:=0.0;
RawComponents[0,3]:=0.0;
RawComponents[1,0]:=0.0;
RawComponents[1,1]:=1.0;
RawComponents[1,2]:=0.0;
RawComponents[1,3]:=0.0;
RawComponents[2,0]:=0.0;
RawComponents[2,1]:=0.0;
RawComponents[2,2]:=1.0;
RawComponents[2,3]:=0.0;
RawComponents[3,0]:=tx;
RawComponents[3,1]:=ty;
RawComponents[3,2]:=tz;
RawComponents[3,3]:=1.0;
end;
constructor TMat4.Ortho(const Left,Right,Bottom,Top,zNear,zFar:TScalar);
var
rml,tmb,fmn:TScalar;
begin
rml:=Right-Left;
tmb:=Top-Bottom;
fmn:=zFar-zNear;
RawComponents[0,0]:=2.0/rml;
RawComponents[0,1]:=0.0;
RawComponents[0,2]:=0.0;
RawComponents[0,3]:=0.0;
RawComponents[1,0]:=0.0;
RawComponents[1,1]:=2.0/tmb;
RawComponents[1,2]:=0.0;
RawComponents[1,3]:=0.0;
RawComponents[2,0]:=0.0;
RawComponents[2,1]:=0.0;
RawComponents[2,2]:=(-2.0)/fmn;
RawComponents[2,3]:=0.0;
RawComponents[3,0]:=(-(Right+Left))/rml;
RawComponents[3,1]:=(-(Top+Bottom))/tmb;
RawComponents[3,2]:=(-(zFar+zNear))/fmn;
RawComponents[3,3]:=1.0;
end;
constructor TMat4.Perspective(const fovy,Aspect,zNear,zFar:TScalar);
var
Sine,Cotangent,ZDelta,Radians:TScalar;
begin
Radians:=(fovy*0.5)*DEG2RAD;
ZDelta:=zFar-zNear;
Sine:=sin(Radians);
if not ((ZDelta=0) or (Sine=0) or (aspect=0)) then begin
Cotangent:=cos(Radians)/Sine;
// NOTE: What happens on copy?
RawComponents:=Matrix4x4Identity.RawComponents;
RawComponents[0,0]:=Cotangent/aspect;
RawComponents[1,1]:=Cotangent;
RawComponents[2,2]:=(-(zFar+zNear))/ZDelta;
RawComponents[2,3]:=-1-0;
RawComponents[3,2]:=(-(2.0*zNear*zFar))/ZDelta;
RawComponents[3,3]:=0.0;
end;
end;
constructor TMat4.Identity;
begin
RawComponents[0,0]:=1.0;
RawComponents[0,1]:=0.0;
RawComponents[0,2]:=0.0;
RawComponents[0,3]:=0.0;
RawComponents[1,0]:=0.0;
RawComponents[1,1]:=1.0;
RawComponents[1,2]:=0.0;
RawComponents[1,3]:=0.0;
RawComponents[2,0]:=0.0;
RawComponents[2,1]:=0.0;
RawComponents[2,2]:=1.0;
RawComponents[2,3]:=0.0;
RawComponents[3,0]:=0.0;
RawComponents[3,1]:=0.0;
RawComponents[3,2]:=0.0;
RawComponents[3,3]:=1.0;
end;
procedure TMat4.Show;
var
x, y: integer;
begin
for y := 0 to 3 do
begin
write('[');
for x := 0 to 3 do
begin
if x < 3 then
write(RawComponents[x, y], ',')
else
write(RawComponents[x, y]);
end;
write(']');
end;
end;
begin
Matrix4x4Identity := TMat4.Identity;
end.