This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mtx.c
633 lines (525 loc) · 15.7 KB
/
mtx.c
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*
* mtx.c
* artoolkitX
*
* This file is part of artoolkitX.
*
* artoolkitX is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* artoolkitX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with artoolkitX. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders of this library give you
* permission to link this library with independent modules to produce an
* executable, regardless of the license terms of these independent modules, and to
* copy and distribute the resulting executable under terms of your choice,
* provided that you also meet, for each linked independent module, the terms and
* conditions of the license of that module. An independent module is a module
* which is neither derived from nor based on this library. If you modify this
* library, you may extend this exception to your version of the library, but you
* are not obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* Copyright 2018 Realmax, Inc.
* Copyright 2015 Daqri, LLC.
* Copyright 2013-2015 ARToolworks, Inc.
*
* Author(s): Philip Lamb
*/
#include <math.h>
#include "mtx.h"
#define DTORf 0.01745329251994f
#define DTORd 0.01745329251994
#define CROSS(dest,v1,v2) {dest[0] = v1[1]*v2[2] - v1[2]*v2[1]; dest[1] = v1[2]*v2[0] - v1[0]*v2[2]; dest[2] = v1[0]*v2[1] - v1[1]*v2[0];}
static float normalisef(float v[3])
{
float l;
l = sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
if (l) {
v[0] /= l;
v[1] /= l;
v[2] /= l;
}
return (l);
}
static double normalised(double v[3])
{
double l;
l = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
if (l) {
v[0] /= l;
v[1] /= l;
v[2] /= l;
}
return (l);
}
void mtxLoadIdentityf(float M[16])
{
M[ 0] = 1.0f; M[ 1] = M[ 2] = M[ 3] = 0.0f; // Column 0;
M[ 4] = 0.0f; M[ 5] = 1.0f; M[ 6] = M[ 7] = 0.0f; // Column 1;
M[ 8] = M[ 9] = 0.0f; M[10] = 1.0f; M[11] = 0.0f; // Column 2;
M[12] = M[13] = M[14] = 0.0f; M[15] = 1.0f; // Column 3;
}
void mtxLoadMatrixf(float M[16], const float T[16])
{
M[ 0] = T[ 0]; M[ 1] = T[ 1]; M[ 2] = T[ 2]; M[ 3] = T[ 3];
M[ 4] = T[ 4]; M[ 5] = T[ 5]; M[ 6] = T[ 6]; M[ 7] = T[ 7];
M[ 8] = T[ 8]; M[ 9] = T[ 9]; M[10] = T[10]; M[11] = T[11];
M[12] = T[12]; M[13] = T[13]; M[14] = T[14]; M[15] = T[15];
}
void mtxMultMatrixf(float M[16], const float T[16])
{
float M0[16];
// Copy M to M0 so that result can be returned in M.
M0[ 0] = M[ 0]; M0[ 1] = M[ 1]; M0[ 2] = M[ 2]; M0[ 3] = M[ 3];
M0[ 4] = M[ 4]; M0[ 5] = M[ 5]; M0[ 6] = M[ 6]; M0[ 7] = M[ 7];
M0[ 8] = M[ 8]; M0[ 9] = M[ 9]; M0[10] = M[10]; M0[11] = M[11];
M0[12] = M[12]; M0[13] = M[13]; M0[14] = M[14]; M0[15] = M[15];
M[ 0] = M0[ 0] * T[ 0] + M0[ 4] * T[ 1] + M0[ 8] * T[ 2] + M0[12] * T[ 3];
M[ 1] = M0[ 1] * T[ 0] + M0[ 5] * T[ 1] + M0[ 9] * T[ 2] + M0[13] * T[ 3];
M[ 2] = M0[ 2] * T[ 0] + M0[ 6] * T[ 1] + M0[10] * T[ 2] + M0[14] * T[ 3];
M[ 3] = M0[ 3] * T[ 0] + M0[ 7] * T[ 1] + M0[11] * T[ 2] + M0[15] * T[ 3];
M[ 4] = M0[ 0] * T[ 4] + M0[ 4] * T[ 5] + M0[ 8] * T[ 6] + M0[12] * T[ 7];
M[ 5] = M0[ 1] * T[ 4] + M0[ 5] * T[ 5] + M0[ 9] * T[ 6] + M0[13] * T[ 7];
M[ 6] = M0[ 2] * T[ 4] + M0[ 6] * T[ 5] + M0[10] * T[ 6] + M0[14] * T[ 7];
M[ 7] = M0[ 3] * T[ 4] + M0[ 7] * T[ 5] + M0[11] * T[ 6] + M0[15] * T[ 7];
M[ 8] = M0[ 0] * T[ 8] + M0[ 4] * T[ 9] + M0[ 8] * T[10] + M0[12] * T[11];
M[ 9] = M0[ 1] * T[ 8] + M0[ 5] * T[ 9] + M0[ 9] * T[10] + M0[13] * T[11];
M[10] = M0[ 2] * T[ 8] + M0[ 6] * T[ 9] + M0[10] * T[10] + M0[14] * T[11];
M[11] = M0[ 3] * T[ 8] + M0[ 7] * T[ 9] + M0[11] * T[10] + M0[15] * T[11];
M[12] = M0[ 0] * T[12] + M0[ 4] * T[13] + M0[ 8] * T[14] + M0[12] * T[15];
M[13] = M0[ 1] * T[12] + M0[ 5] * T[13] + M0[ 9] * T[14] + M0[13] * T[15];
M[14] = M0[ 2] * T[12] + M0[ 6] * T[13] + M0[10] * T[14] + M0[14] * T[15];
M[15] = M0[ 3] * T[12] + M0[ 7] * T[13] + M0[11] * T[14] + M0[15] * T[15];
}
void mtxTranslatef(float M[16], const float x, const float y, const float z)
{
float T[16];
T[ 0] = 1.0f; T[ 1] = T[ 2] = T[ 3] = 0.0f; // Column 0;
T[ 4] = 0.0f; T[ 5] = 1.0f; T[ 6] = T[ 7] = 0.0f; // Column 1;
T[ 8] = T[ 9] = 0.0f; T[10] = 1.0f; T[11] = 0.0f; // Column 2;
T[12] = x; T[13] = y; T[14] = z; T[15] = 1.0f; // Column 3;
mtxMultMatrixf(M, T);
}
void mtxRotatef(float M[16], const float q, const float x, const float y, const float z)
{
float ll, l, x0, y0, z0;
float T[16];
float C, S, V;
float xy, yz, xz;
float Sx, Sy, Sz;
float Vxy, Vyz, Vxz;
if (q == 0.0f) return;
ll = x*x + y*y + z*z;
if (ll != 1.0f) {
l = sqrtf(ll);
if (!l) return;
x0 = x / l;
y0 = y / l;
z0 = z / l;
} else {
x0 = x;
y0 = y;
z0 = z;
}
C = cosf(DTORf*q);
S = sinf(DTORf*q);
V = 1.0f - C;
xy = x0*y0;
yz = y0*z0;
xz = x0*z0;
Sx = S*x0;
Sy = S*y0;
Sz = S*z0;
Vxy = V*xy;
Vyz = V*yz;
Vxz = V*xz;
// Column 0;
T[ 0] = V*x0*x0 + C;
T[ 1] = Vxy + Sz;
T[ 2] = Vxz - Sy;
T[ 3] = 0.0f;
// Column 1;
T[ 4] = Vxy - Sz;
T[ 5] = V*y0*y0 + C;
T[ 6] = Vyz + Sx;
T[ 7] = 0.0f;
// Column 2;
T[ 8] = Vxz + Sy;
T[ 9] = Vyz - Sx;
T[10] = V*z0*z0 + C;
T[11] = 0.0f;
// Column 3;
T[12] = 0.0f;
T[13] = 0.0f;
T[14] = 0.0f;
T[15] = 1.0f;
mtxMultMatrixf(M, T);
}
void mtxScalef(float M[16], const float x, const float y, const float z)
{
float T[16];
T[ 0] = x; T[ 1] = T[ 2] = T[ 3] = 0.0f; // Column 0;
T[ 4] = 0.0f; T[ 5] = y; T[ 6] = T[ 7] = 0.0f; // Column 1;
T[ 8] = T[ 9] = 0.0f; T[10] = z; T[11] = 0.0f; // Column 2;
T[12] = T[13] = T[14] = 0.0f; T[15] = 1.0f; // Column 3;
mtxMultMatrixf(M, T);
}
void mtxOrthof(float M[16], float left, float right, float bottom, float top, float zNear, float zFar)
{
float T[16];
float r_l = right - left;
float t_b = top - bottom;
float f_n = zFar - zNear;
// Column 0;
T[ 0] = 2.0f / r_l;
T[ 1] = 0.0f;
T[ 2] = 0.0f;
T[ 3] = 0.0f;
// Column 1;
T[ 4] = 0.0f;
T[ 5] = 2.0f / t_b;
T[ 6] = 0.0f;
T[ 7] = 0.0f;
// Column 2;
T[ 8] = 0.0f;
T[ 9] = 0.0f;
T[10] = -2.0f / f_n;
T[11] = 0.0f;
// Column 3;
T[12] = - (right + left) / r_l;
T[13] = - (top + bottom) / t_b;
T[14] = - (zFar + zNear) / f_n;
T[15] = 1.0f;
mtxMultMatrixf(M, T);
}
void mtxFrustumf(float M[16], float left, float right, float bottom, float top, float zNear, float zFar)
{
float T[16];
float r_l = right - left;
float t_b = top - bottom;
float f_n = zFar - zNear;
// Column 0;
T[ 0] = 2.0f * zNear / r_l;
T[ 1] = 0.0f;
T[ 2] = 0.0f;
T[ 3] = 0.0f;
// Column 1;
T[ 4] = 0.0f;
T[ 5] = 2.0f * zNear / t_b;
T[ 6] = 0.0f;
T[ 7] = 0.0f;
// Column 2;
T[ 8] = (right + left) / r_l;
T[ 9] = (top + bottom) / t_b;
T[10] = - (zFar + zNear) / f_n;
T[11] = -1.0f;
// Column 3;
T[12] = 0.0f;
T[13] = 0.0f;
T[14] = - (2.0f * zFar * zNear) / f_n;
T[15] = 0.0f;
mtxMultMatrixf(M, T);
}
void mtxPerspectivef(float M[16], float fovy, float aspect, float zNear, float zFar)
{
float T[16];
float f = 1.0f / tanf(DTORf*fovy/2.0f);
float n_f = zNear - zFar;
// Column 0;
T[ 0] = f / aspect;
T[ 1] = 0.0f;
T[ 2] = 0.0f;
T[ 3] = 0.0f;
// Column 1;
T[ 4] = 0.0f;
T[ 5] = f;
T[ 6] = 0.0f;
T[ 7] = 0.0f;
// Column 2;
T[ 8] = 0.0f;
T[ 9] = 0.0f;
T[10] = (zFar + zNear) / n_f;
T[11] = -1.0f;
// Column 3;
T[12] = 0.0f;
T[13] = 0.0f;
T[14] = 2.0f * zFar * zNear / n_f;
T[15] = 0.0f;
mtxMultMatrixf(M, T);
}
void mtxLookAtf(float M[16], float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ)
{
float T[16];
float F[3], UP[3], S[3];
F[0] = centerX - eyeX;
F[1] = centerY - eyeY;
F[2] = centerZ - eyeZ;
normalisef(F);
UP[0] = upX;
UP[1] = upY;
UP[2] = upZ;
normalisef(UP);
CROSS(S, F, UP);
CROSS(UP, S, F);
// Column 0;
T[ 0] = S[0];
T[ 1] = UP[0];
T[ 2] = -F[0];
T[ 3] = 0.0f;
// Column 1;
T[ 4] = S[1];
T[ 5] = UP[1];
T[ 6] = -F[1];
T[ 7] = 0.0f;
// Column 2;
T[ 8] = S[2];
T[ 9] = UP[2];
T[10] = -F[2];
T[11] = 0.0f;
// Column 3;
T[12] = -eyeX * S[0] - eyeY * S[1] - eyeZ * S[2];
T[13] = -eyeX * UP[0] - eyeY * UP[1] - eyeZ * UP[2];
T[14] = eyeX * F[0] + eyeY * F[1] + eyeZ * F[2];
T[15] = 1.0f;
mtxMultMatrixf(M, T);
}
void mtxLoadIdentityd(double M[16])
{
M[ 0] = 1.0; M[ 1] = M[ 2] = M[ 3] = 0.0; // Column 0;
M[ 4] = 0.0; M[ 5] = 1.0; M[ 6] = M[ 7] = 0.0; // Column 1;
M[ 8] = M[ 9] = 0.0; M[10] = 1.0; M[11] = 0.0; // Column 2;
M[12] = M[13] = M[14] = 0.0; M[15] = 1.0; // Column 3;
}
void mtxLoadMatrixd(double M[16], const double T[16])
{
M[ 0] = T[ 0]; M[ 1] = T[ 1]; M[ 2] = T[ 2]; M[ 3] = T[ 3];
M[ 4] = T[ 4]; M[ 5] = T[ 5]; M[ 6] = T[ 6]; M[ 7] = T[ 7];
M[ 8] = T[ 8]; M[ 9] = T[ 9]; M[10] = T[10]; M[11] = T[11];
M[12] = T[12]; M[13] = T[13]; M[14] = T[14]; M[15] = T[15];
}
void mtxMultMatrixd(double M[16], const double T[16])
{
double M0[16];
// Copy M to M0 so that result can be returned in M.
M0[ 0] = M[ 0]; M0[ 1] = M[ 1]; M0[ 2] = M[ 2]; M0[ 3] = M[ 3];
M0[ 4] = M[ 4]; M0[ 5] = M[ 5]; M0[ 6] = M[ 6]; M0[ 7] = M[ 7];
M0[ 8] = M[ 8]; M0[ 9] = M[ 9]; M0[10] = M[10]; M0[11] = M[11];
M0[12] = M[12]; M0[13] = M[13]; M0[14] = M[14]; M0[15] = M[15];
M[ 0] = M0[ 0] * T[ 0] + M0[ 4] * T[ 1] + M0[ 8] * T[ 2] + M0[12] * T[ 3];
M[ 1] = M0[ 1] * T[ 0] + M0[ 5] * T[ 1] + M0[ 9] * T[ 2] + M0[13] * T[ 3];
M[ 2] = M0[ 2] * T[ 0] + M0[ 6] * T[ 1] + M0[10] * T[ 2] + M0[14] * T[ 3];
M[ 3] = M0[ 3] * T[ 0] + M0[ 7] * T[ 1] + M0[11] * T[ 2] + M0[15] * T[ 3];
M[ 4] = M0[ 0] * T[ 4] + M0[ 4] * T[ 5] + M0[ 8] * T[ 6] + M0[12] * T[ 7];
M[ 5] = M0[ 1] * T[ 4] + M0[ 5] * T[ 5] + M0[ 9] * T[ 6] + M0[13] * T[ 7];
M[ 6] = M0[ 2] * T[ 4] + M0[ 6] * T[ 5] + M0[10] * T[ 6] + M0[14] * T[ 7];
M[ 7] = M0[ 3] * T[ 4] + M0[ 7] * T[ 5] + M0[11] * T[ 6] + M0[15] * T[ 7];
M[ 8] = M0[ 0] * T[ 8] + M0[ 4] * T[ 9] + M0[ 8] * T[10] + M0[12] * T[11];
M[ 9] = M0[ 1] * T[ 8] + M0[ 5] * T[ 9] + M0[ 9] * T[10] + M0[13] * T[11];
M[10] = M0[ 2] * T[ 8] + M0[ 6] * T[ 9] + M0[10] * T[10] + M0[14] * T[11];
M[11] = M0[ 3] * T[ 8] + M0[ 7] * T[ 9] + M0[11] * T[10] + M0[15] * T[11];
M[12] = M0[ 0] * T[12] + M0[ 4] * T[13] + M0[ 8] * T[14] + M0[12] * T[15];
M[13] = M0[ 1] * T[12] + M0[ 5] * T[13] + M0[ 9] * T[14] + M0[13] * T[15];
M[14] = M0[ 2] * T[12] + M0[ 6] * T[13] + M0[10] * T[14] + M0[14] * T[15];
M[15] = M0[ 3] * T[12] + M0[ 7] * T[13] + M0[11] * T[14] + M0[15] * T[15];
}
void mtxTranslated(double M[16], const double x, const double y, const double z)
{
double T[16];
T[ 0] = 1.0; T[ 1] = T[ 2] = T[ 3] = 0.0; // Column 0;
T[ 4] = 0.0; T[ 5] = 1.0; T[ 6] = T[ 7] = 0.0; // Column 1;
T[ 8] = T[ 9] = 0.0; T[10] = 1.0; T[11] = 0.0; // Column 2;
T[12] = x; T[13] = y; T[14] = z; T[15] = 1.0; // Column 3;
mtxMultMatrixd(M, T);
}
void mtxRotated(double M[16], const double q, const double x, const double y, const double z)
{
double ll, l, x0, y0, z0;
double T[16];
double C, S, V;
double xy, yz, xz;
double Sx, Sy, Sz;
double Vxy, Vyz, Vxz;
if (q == 0.0) return;
ll = x*x + y*y + z*z;
if (ll != 1.0) {
l = sqrt(ll);
if (!l) return;
x0 = x / l;
y0 = y / l;
z0 = z / l;
} else {
x0 = x;
y0 = y;
z0 = z;
}
C = cos(DTORd*q);
S = sin(DTORd*q);
V = 1.0 - C;
xy = x0*y0;
yz = y0*z0;
xz = x0*z0;
Sx = S*x0;
Sy = S*y0;
Sz = S*z0;
Vxy = V*xy;
Vyz = V*yz;
Vxz = V*xz;
// Column 0;
T[ 0] = V*x0*x0 + C;
T[ 1] = Vxy + Sz;
T[ 2] = Vxz - Sy;
T[ 3] = 0.0;
// Column 1;
T[ 4] = Vxy - Sz;
T[ 5] = V*y0*y0 + C;
T[ 6] = Vyz + Sx;
T[ 7] = 0.0;
// Column 2;
T[ 8] = Vxz + Sy;
T[ 9] = Vyz - Sx;
T[10] = V*z0*z0 + C;
T[11] = 0.0;
// Column 3;
T[12] = 0.0;
T[13] = 0.0;
T[14] = 0.0;
T[15] = 1.0;
mtxMultMatrixd(M, T);
}
void mtxScaled(double M[16], const double x, const double y, const double z)
{
double T[16];
T[ 0] = x; T[ 1] = T[ 2] = T[ 3] = 0.0; // Column 0;
T[ 4] = 0.0; T[ 5] = y; T[ 6] = T[ 7] = 0.0; // Column 1;
T[ 8] = T[ 9] = 0.0; T[10] = z; T[11] = 0.0; // Column 2;
T[12] = T[13] = T[14] = 0.0; T[15] = 1.0; // Column 3;
mtxMultMatrixd(M, T);
}
void mtxOrthod(double M[16], double left, double right, double bottom, double top, double zNear, double zFar)
{
double T[16];
double r_l = right - left;
double t_b = top - bottom;
double f_n = zFar - zNear;
// Column 0;
T[ 0] = 2.0 / r_l;
T[ 1] = 0.0;
T[ 2] = 0.0;
T[ 3] = 0.0;
// Column 1;
T[ 4] = 0.0;
T[ 5] = 2.0 / t_b;
T[ 6] = 0.0;
T[ 7] = 0.0;
// Column 2;
T[ 8] = 0.0;
T[ 9] = 0.0;
T[10] = -2.0 / f_n;
T[11] = 0.0;
// Column 3;
T[12] = - (right + left) / r_l;
T[13] = - (top + bottom) / t_b;
T[14] = - (zFar + zNear) / f_n;
T[15] = 1.0;
mtxMultMatrixd(M, T);
}
void mtxFrustumd(double M[16], double left, double right, double bottom, double top, double zNear, double zFar)
{
double T[16];
double r_l = right - left;
double t_b = top - bottom;
double f_n = zFar - zNear;
// Column 0;
T[ 0] = 2.0 * zNear / r_l;
T[ 1] = 0.0;
T[ 2] = 0.0;
T[ 3] = 0.0;
// Column 1;
T[ 4] = 0.0;
T[ 5] = 2.0 * zNear / t_b;
T[ 6] = 0.0;
T[ 7] = 0.0;
// Column 2;
T[ 8] = (right + left) / r_l;
T[ 9] = (top + bottom) / t_b;
T[10] = - (zFar + zNear) / f_n;
T[11] = -1.0;
// Column 3;
T[12] = 0.0;
T[13] = 0.0;
T[14] = - (2.0 * zFar * zNear) / f_n;
T[15] = 0.0;
mtxMultMatrixd(M, T);
}
void mtxPerspectived(double M[16], double fovy, double aspect, double zNear, double zFar)
{
double T[16];
double f = 1.0 / tan(DTORd*fovy/2.0);
double n_f = zNear - zFar;
// Column 0;
T[ 0] = f / aspect;
T[ 1] = 0.0;
T[ 2] = 0.0;
T[ 3] = 0.0;
// Column 1;
T[ 4] = 0.0;
T[ 5] = f;
T[ 6] = 0.0;
T[ 7] = 0.0;
// Column 2;
T[ 8] = 0.0;
T[ 9] = 0.0;
T[10] = (zFar + zNear) / n_f;
T[11] = -1.0;
// Column 3;
T[12] = 0.0;
T[13] = 0.0;
T[14] = 2.0 * zFar * zNear / n_f;
T[15] = 0.0;
mtxMultMatrixd(M, T);
}
void mtxLookAtd(double M[16], double eyeX, double eyeY, double eyeZ, double centerX, double centerY, double centerZ, double upX, double upY, double upZ)
{
double T[16];
double F[3], UP[3], S[3];
F[0] = centerX - eyeX;
F[1] = centerY - eyeY;
F[2] = centerZ - eyeZ;
normalised(F);
UP[0] = upX;
UP[1] = upY;
UP[2] = upZ;
normalised(UP);
CROSS(S, F, UP);
CROSS(UP, S, F);
// Column 0;
T[ 0] = S[0];
T[ 1] = UP[0];
T[ 2] = -F[0];
T[ 3] = 0.0;
// Column 1;
T[ 4] = S[1];
T[ 5] = UP[1];
T[ 6] = -F[1];
T[ 7] = 0.0;
// Column 2;
T[ 8] = S[2];
T[ 9] = UP[2];
T[10] = -F[2];
T[11] = 0.0;
// Column 3;
T[12] = -eyeX * S[0] - eyeY * S[1] - eyeZ * S[2];
T[13] = -eyeX * UP[0] - eyeY * UP[1] - eyeZ * UP[2];
T[14] = eyeX * F[0] + eyeY * F[1] + eyeZ * F[2];
T[15] = 1.0;
mtxMultMatrixd(M, T);
}