-
Notifications
You must be signed in to change notification settings - Fork 44
/
libmath.c
922 lines (844 loc) · 20.3 KB
/
libmath.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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
#include "math3d.h"
#include <lua.h>
#include <lauxlib.h>
static void *
check_userdata(lua_State *L, int idx) {
void * ret = lua_touserdata(L, idx);
luaL_argcheck(L, ret != NULL, idx, "Userdata should not be NULL");
return ret;
}
static int
lnewvec3(lua_State *L) {
struct vector3 tmp;
if (lua_isuserdata(L, 1)) {
struct vector3 *copy = check_userdata(L,1);
tmp = *copy;
} else {
tmp.x = luaL_optnumber(L, 1, 0);
tmp.y = luaL_optnumber(L, 2, 0);
tmp.z = luaL_optnumber(L, 3, 0);
}
struct vector3 *vec3 = lua_newuserdata(L, sizeof(*vec3));
*vec3 = tmp;
lua_pushvalue(L, lua_upvalueindex(1));
lua_setmetatable(L, -2);
return 1;
}
static int
lvec3_pack(lua_State *L) {
struct vector3 *vec3 = check_userdata(L, 1);
if (!lua_isnoneornil(L, 2)) {
vec3->x = luaL_checknumber(L, 2);
}
if (!lua_isnoneornil(L, 3)) {
vec3->y = luaL_checknumber(L, 3);
}
if (!lua_isnoneornil(L, 4)) {
vec3->z = luaL_checknumber(L, 4);
}
lua_settop(L,1);
return 1;
}
static int
lvec3_unpack(lua_State *L) {
struct vector3 *vec3 = check_userdata(L, 1);
lua_pushnumber(L, vec3->x);
lua_pushnumber(L, vec3->y);
lua_pushnumber(L, vec3->z);
return 3;
}
static int
lvec3_dot(lua_State *L) {
struct vector3 *a = check_userdata(L, 1);
struct vector3 *b = check_userdata(L, 2);
float v = vector3_dot(a,b);
lua_pushnumber(L, v);
return 1;
}
static int
lvec3_cross(lua_State *L) {
struct vector3 *v = check_userdata(L, 1);
struct vector3 *a = check_userdata(L, 2);
struct vector3 *b = check_userdata(L, 3);
vector3_cross(v,a,b);
lua_settop(L, 1);
return 1;
}
static int
lvec3_vector(lua_State *L) {
struct vector3 *v = check_userdata(L, 1);
struct vector3 *a = check_userdata(L, 2);
struct vector3 *b = check_userdata(L, 3);
vector3_vector(v,a,b);
lua_settop(L, 1);
return 1;
}
static int
lvec3_length(lua_State *L) {
struct vector3 *v = check_userdata(L, 1);
float len = vector3_length(v);
lua_pushnumber(L, len);
return 1;
}
static int
lvec3_normalize(lua_State *L) {
struct vector3 *v = check_userdata(L, 1);
vector3_normalize(v);
lua_settop(L,1);
return 1;
}
static int
lvec3_copy(lua_State *L) {
struct vector3 *v = check_userdata(L, 1);
struct vector3 *from = check_userdata(L, 2);
*v = *from;
lua_settop(L, 1);
return 1;
}
static int
lvec3_tostring(lua_State *L) {
struct vector3 *v = check_userdata(L, 1);
lua_pushfstring(L, "[%f, %f, %f]", v->x, v->y, v->z);
return 1;
}
static int
lvec3_rotation(lua_State *L) {
struct vector3 *v = check_userdata(L, 1);
struct vector3 *from = check_userdata(L, 2);
vector3_to_rotation(v, from);
lua_settop(L, 1);
return 1;
}
static int
lvec3_lerp(lua_State *L) {
struct vector3 *v = check_userdata(L, 1);
struct vector3 *a = check_userdata(L, 2);
struct vector3 *b = check_userdata(L, 3);
float f = luaL_checknumber(L,4);
vector3_lerp(v, a,b,f);
lua_settop(L,1);
return 1;
}
static void
create_meta(lua_State *L, luaL_Reg *l, const char *name, lua_CFunction tostring) {
int n = 0;
while(l[n].name)
++n;
lua_newtable(L);
lua_createtable(L, 0, n);
int i;
for (i=0;i<n;i++) {
lua_pushcfunction(L, l[i].func);
lua_setfield(L, -2, l[i].name);
}
lua_setfield(L, -2, "__index");
lua_pushstring(L, name);
lua_setfield(L, -2, "__metatable");
lua_pushcfunction(L, tostring);
lua_setfield(L, -2, "__tostring");
}
static int
lvec3_transmat(lua_State *L) {
struct vector3 *v = check_userdata(L,1);
union matrix44 *m = check_userdata(L,2);
matrix44_transmat(m,v->x,v->y,v->z);
lua_settop(L,2);
return 1;
}
static int
lvec3_scalemat(lua_State *L) {
struct vector3 *v = check_userdata(L,1);
union matrix44 *m = check_userdata(L,2);
matrix44_scalemat(m,v->x,v->y,v->z);
lua_settop(L,2);
return 1;
}
static int
lvec3_rotmat(lua_State *L) {
struct vector3 *v = check_userdata(L,1);
union matrix44 *m = check_userdata(L,2);
matrix44_rotmat(m,v->x,v->y,v->z);
lua_settop(L,2);
return 1;
}
static int
lvec3_rotaxis(lua_State *L) {
struct vector3 *v = check_userdata(L,1);
union matrix44 *m = check_userdata(L,2);
float angle = luaL_checknumber(L,3);
matrix44_rot_axis(m,v,angle);
lua_settop(L,2);
return 1;
}
static int
lvec3_mul(lua_State *L) {
if (lua_gettop(L) == 2) {
struct vector3 *v = check_userdata(L,1);
union matrix44 *m = check_userdata(L,2);
vector3_mul(v,m);
} else {
struct vector3 *r = check_userdata(L,1);
struct vector3 *v = check_userdata(L,2);
union matrix44 *m = check_userdata(L,3);
*r = *v;
vector3_mul(r,m);
}
lua_settop(L,1);
return 1;
}
static int
lvec3_mulH(lua_State *L) {
if (lua_gettop(L) == 2) {
struct vector3 *v = check_userdata(L,1);
union matrix44 *m = check_userdata(L,2);
vector3_mulH(v,m);
} else {
struct vector3 *r = check_userdata(L,1);
struct vector3 *v = check_userdata(L,2);
union matrix44 *m = check_userdata(L,3);
*r = *v;
vector3_mulH(r,m);
}
lua_settop(L,1);
return 1;
}
static int
lvec3_mul33(lua_State *L) {
struct vector3 *v = check_userdata(L,1);
union matrix44 *m = check_userdata(L,2);
vector3_mul33(v,m);
lua_settop(L,1);
return 1;
}
static int
lvec3_distAABB(lua_State *L) {
struct vector3 *v = check_userdata(L,1);
struct vector3 *mins = check_userdata(L,2);
struct vector3 *maxs = check_userdata(L,3);
float d = vector3_distAABB(v, mins, maxs);
lua_pushnumber(L,d);
return 1;
}
static int
lvec3_plane(lua_State *L) {
struct vector3 *v = check_userdata(L,1);
struct plane *p = check_userdata(L,2);
float d = luaL_optnumber(L,3,0);
plane_init(p, v, d);
lua_settop(L,2);
return 1;
}
static void
vector3(lua_State *L) {
luaL_Reg l[] = {
{ "pack", lvec3_pack },
{ "unpack", lvec3_unpack },
{ "dot", lvec3_dot },
{ "cross", lvec3_cross },
{ "vector", lvec3_vector },
{ "length", lvec3_length },
{ "normalize", lvec3_normalize },
{ "copy", lvec3_copy },
{ "rotation", lvec3_rotation },
{ "lerp", lvec3_lerp },
{ "transmat", lvec3_transmat },
{ "scalemat", lvec3_scalemat },
{ "rotmat", lvec3_rotmat },
{ "rotaxis", lvec3_rotaxis },
{ "mul", lvec3_mul },
{ "mulH", lvec3_mulH },
{ "mul33", lvec3_mul33 },
{ "plane", lvec3_plane },
{ "distAABB", lvec3_distAABB },
{ NULL, NULL },
};
create_meta(L, l, "vector3", lvec3_tostring);
lua_pushcclosure(L, lnewvec3, 1);
}
static int
lnewquat(lua_State *L) {
if (lua_isuserdata(L, 1)) {
struct quaternion *tmp = check_userdata(L,1);
struct quaternion *q = lua_newuserdata(L, sizeof(*q));
*q = *tmp;
} else if lua_isnoneornil(L, 1) {
struct quaternion *q = lua_newuserdata(L, sizeof(*q));
q->x = 0;
q->y = 0;
q->z = 0;
q->w = 1.0f;
} else {
float x = luaL_checknumber(L, 1);
float y = luaL_checknumber(L, 2);
float z = luaL_checknumber(L, 3);
struct quaternion *q = lua_newuserdata(L, sizeof(*q));
quaternion_init(q, x, y, z);
}
lua_pushvalue(L, lua_upvalueindex(1));
lua_setmetatable(L, -2);
return 1;
}
static int
lquat_tostring(lua_State *L) {
struct quaternion *q = check_userdata(L, 1);
lua_pushfstring(L, "[%f, %f, %f, %f]", q->x, q->y, q->z, q->w);
return 1;
}
static int
lquat_mul(lua_State *L) {
struct quaternion *q = check_userdata(L, 1);
struct quaternion *a = check_userdata(L, 2);
struct quaternion *b = check_userdata(L, 3);
quaternion_mul(q,a,b);
lua_settop(L,1);
return 1;
}
static int
lquat_copy(lua_State *L) {
struct quaternion *a = check_userdata(L, 1);
struct quaternion *b = check_userdata(L, 2);
*a = *b;
lua_settop(L,1);
return 1;
}
static int
lquat_slerp(lua_State *L) {
struct quaternion *q = check_userdata(L, 1);
struct quaternion *a = check_userdata(L, 2);
struct quaternion *b = check_userdata(L, 3);
float t = luaL_checknumber(L, 4);
quaternion_slerp(q,a,b,t);
lua_settop(L,1);
return 1;
}
static int
lquat_nslerp(lua_State *L) {
struct quaternion *q = check_userdata(L, 1);
struct quaternion *a = check_userdata(L, 2);
struct quaternion *b = check_userdata(L, 3);
float t = luaL_checknumber(L, 4);
quaternion_nslerp(q,a,b,t);
lua_settop(L,1);
return 1;
}
static int
lquat_inverted(lua_State *L) {
struct quaternion *q = check_userdata(L, 1);
quaternion_inverted(q);
lua_settop(L,1);
return 1;
}
static int
lquat_matrix(lua_State *L) {
struct quaternion *q = check_userdata(L,1);
union matrix44 *mat = check_userdata(L,2);
matrix44_from_quaternion(mat, q);
lua_settop(L,2);
return 1;
}
static int
lquat_pack(lua_State *L) {
struct quaternion *q = check_userdata(L,1);
if (lua_type(L, 2) == LUA_TSTRING) {
const char *fmt = lua_tostring(L, 2);
float *v = (float *)q;
int i;
int idx = 3;
for (i=0;i<4 && fmt[i];i++) {
switch(fmt[i]) {
case 'f':
v[i] = luaL_checknumber(L, idx+i);
break;
case 'd': {
union {
float f;
unsigned int d;
} u;
u.d = luaL_checkinteger(L, idx+i);
v[i] = u.f;
break;
}
case 'n':
break;
default:
return luaL_error(L, "Invalid pack %c", fmt[i]);
}
}
} else {
if (!lua_isnoneornil(L, 2)) {
q->x = luaL_checknumber(L, 2);
}
if (!lua_isnoneornil(L, 3)) {
q->y = luaL_checknumber(L, 3);
}
if (!lua_isnoneornil(L, 4)) {
q->z = luaL_checknumber(L, 4);
}
if (!lua_isnoneornil(L, 5)) {
q->w = luaL_checknumber(L, 5);
}
}
lua_settop(L,1);
return 1;
}
static int
lquat_unpack(lua_State *L) {
struct quaternion *q = check_userdata(L,1);
lua_pushnumber(L, q->x);
lua_pushnumber(L, q->y);
lua_pushnumber(L, q->z);
lua_pushnumber(L, q->w);
return 4;
}
static void
quaternion(lua_State *L) {
luaL_Reg l[] = {
{ "mul", lquat_mul },
{ "copy", lquat_copy },
{ "pack", lquat_pack },
{ "unpack", lquat_unpack },
{ "slerp", lquat_slerp },
{ "nslerp", lquat_nslerp },
{ "inverted", lquat_inverted },
{ "matrix", lquat_matrix },
{ NULL, NULL },
};
create_meta(L, l, "quateraion", lquat_tostring);
lua_pushcclosure(L, lnewquat, 1);
}
static int
lnewmat(lua_State *L) {
if (lua_isuserdata(L, 1)) {
union matrix44 *tmp = check_userdata(L,1);
union matrix44 *mat = lua_newuserdata(L, sizeof(*mat));
*mat = *tmp;
} else if lua_isnoneornil(L, 1) {
union matrix44 *mat = lua_newuserdata(L, sizeof(*mat));
matrix44_identity(mat);
} else {
float x = luaL_checknumber(L, 1);
float y = luaL_checknumber(L, 2);
float z = luaL_checknumber(L, 3);
union matrix44 *mat = lua_newuserdata(L, sizeof(*mat));
matrix44_rot(mat, x, y, z);
}
lua_pushvalue(L, lua_upvalueindex(1));
lua_setmetatable(L, -2);
return 1;
}
static int
lmat_tostring(lua_State *L) {
union matrix44 *m = check_userdata(L, 1);
lua_pushfstring(L, "[(%f, %f, %f, %f) (%f, %f, %f, %f) (%f, %f, %f, %f) (%f, %f, %f, %f)]",
m->c[0][0],m->c[0][1],m->c[0][2],m->c[0][3],
m->c[1][0],m->c[1][1],m->c[1][2],m->c[1][3],
m->c[2][0],m->c[2][1],m->c[2][2],m->c[2][3],
m->c[3][0],m->c[3][1],m->c[3][2],m->c[3][3]);
return 1;
}
static int
lmat_pack(lua_State *L) {
union matrix44 *m = check_userdata(L, 1);
int i;
for (i=0;i<16;i++) {
m->x[i] = luaL_checknumber(L, 2+i);
}
lua_settop(L,1);
return 1;
}
static int
lmat_packline(lua_State *L) {
union matrix44 *m = check_userdata(L, 1);
int l = luaL_checkinteger(L, 2);
if (l < 1 || l > 4) {
return luaL_error(L, "out of range (%d) of matrix line", l);
}
float * line = m->c[l-1];
int i;
for (i=0;i<4;i++) {
int idx = 3+i;
if (!lua_isnoneornil(L, idx)) {
line[i] = luaL_checknumber(L, idx);
}
}
lua_settop(L, 1);
return 1;
}
static int
lmat_unpack(lua_State *L) {
union matrix44 *m = check_userdata(L, 1);
int i;
for (i=0;i<16;i++) {
lua_pushnumber(L, m->x[i]);
}
return 16;
}
static int
lmat_copy(lua_State *L) {
union matrix44 *m = check_userdata(L, 1);
union matrix44 *from = check_userdata(L, 2);
*m=*from;
lua_settop(L,1);
return 1;
}
static int
lmat_identity(lua_State *L) {
union matrix44 *m = check_userdata(L, 1);
matrix44_identity(m);
lua_settop(L,1);
return 1;
}
static int
lmat_perspective(lua_State *L) {
union matrix44 *m = check_userdata(L, 1);
float l = luaL_checknumber(L, 2);
float r = luaL_checknumber(L, 3);
float b = luaL_checknumber(L, 4);
float t = luaL_checknumber(L, 5);
float n = luaL_checknumber(L, 6);
float f = luaL_checknumber(L, 7);
int homogeneousDepth = lua_toboolean(L, 8);
matrix44_perspective(m,l,r,b,t,n,f,homogeneousDepth);
lua_settop(L,1);
return 1;
}
static int
lmat_ortho(lua_State *L) {
union matrix44 *m = check_userdata(L, 1);
float l = luaL_checknumber(L, 2);
float r = luaL_checknumber(L, 3);
float b = luaL_checknumber(L, 4);
float t = luaL_checknumber(L, 5);
float n = luaL_checknumber(L, 6);
float f = luaL_checknumber(L, 7);
int homogeneousDepth = lua_toboolean(L, 8);
matrix44_ortho(m,l,r,b,t,n,f,homogeneousDepth);
lua_settop(L,1);
return 1;
}
static int
lmat_mul(lua_State *L) {
union matrix44 *m = check_userdata(L, 1);
union matrix44 *a = check_userdata(L, 2);
union matrix44 *b;
if (lua_gettop(L) ==2) {
b = a;
a = m;
} else {
b = check_userdata(L, 3);
}
matrix44_mul(m,a,b);
lua_settop(L,1);
return 1;
}
static int
lmat_fastmul43(lua_State *L) {
union matrix44 *m = check_userdata(L, 1);
union matrix44 *a = check_userdata(L, 2);
union matrix44 *b = check_userdata(L, 3);
matrix44_fastmul43(m,a,b);
lua_settop(L,1);
return 1;
}
static int
lmat_transposed(lua_State *L) {
union matrix44 *m = check_userdata(L,1);
matrix44_transposed(m);
lua_settop(L,1);
return 1;
}
static int
lmat_determinant(lua_State *L) {
union matrix44 *m = check_userdata(L,1);
float v = matrix44_determinant(m);
lua_pushnumber(L,v);
return 1;
}
static int
lmat_inverted(lua_State *L) {
union matrix44 *m = check_userdata(L,1);
if (lua_isnoneornil(L, 2)) {
union matrix44 tmp = *m;
matrix44_inverted(m, &tmp);
} else {
union matrix44 *from = check_userdata(L,2);
matrix44_inverted(m, from);
}
lua_settop(L,1);
return 1;
}
static int
lmat_gettrans(lua_State *L) {
union matrix44 *m = check_userdata(L,1);
struct vector3 *v = check_userdata(L,2);
matrix44_gettrans(m,v);
lua_settop(L,2);
return 1;
}
static int
lmat_getscale(lua_State *L) {
union matrix44 *m = check_userdata(L,1);
struct vector3 *v = check_userdata(L,2);
matrix44_getscale(m,v);
lua_settop(L,2);
return 1;
}
static int
lmat_decompose(lua_State *L) {
union matrix44 *m = check_userdata(L,1);
struct vector3 *trans = check_userdata(L,2);
struct vector3 *rot = check_userdata(L,3);
struct vector3 *scale = check_userdata(L,4);
matrix44_decompose(m, trans, rot, scale);
lua_settop(L, 4);
return 3;
}
static int
lmat_trans(lua_State *L) {
union matrix44 *m = check_userdata(L,1);
float x = luaL_checknumber(L, 2);
float y = luaL_checknumber(L, 3);
float z = luaL_checknumber(L, 4);
matrix44_trans(m,x,y,z);
lua_settop(L,1);
return 1;
}
static int
lmat_scale(lua_State *L) {
union matrix44 *m = check_userdata(L,1);
float x = luaL_checknumber(L, 2);
float y = luaL_checknumber(L, 3);
float z = luaL_checknumber(L, 4);
matrix44_scale(m,x,y,z);
lua_settop(L,1);
return 1;
}
static int
lmat_rot(lua_State *L) {
union matrix44 *m = check_userdata(L,1);
float x = luaL_checknumber(L, 2);
float y = luaL_checknumber(L, 3);
float z = luaL_checknumber(L, 4);
matrix44_rot(m,x,y,z);
lua_settop(L,1);
return 1;
}
static int
lmat_lookat(lua_State *L) {
union matrix44 *m = check_userdata(L,1);
struct vector3 *eye = check_userdata(L, 2);
struct vector3 *at = check_userdata(L, 3);
struct vector3 *up = lua_touserdata(L, 4);
matrix44_lookat(m, eye, at, up);
lua_settop(L, 1);
return 1;
}
static void
matrix(lua_State *L) {
luaL_Reg l[] = {
{ "pack", lmat_pack },
{ "packline", lmat_packline },
{ "unpack", lmat_unpack },
{ "copy", lmat_copy },
{ "identity", lmat_identity },
{ "perspective", lmat_perspective },
{ "ortho", lmat_ortho },
{ "mul", lmat_mul },
{ "fastmul43", lmat_fastmul43 },
{ "transposed", lmat_transposed },
{ "determinant", lmat_determinant },
{ "inverted", lmat_inverted },
{ "gettrans", lmat_gettrans },
{ "getscale", lmat_getscale },
{ "decompose", lmat_decompose },
{ "trans", lmat_trans },
{ "scale", lmat_scale },
{ "rot", lmat_rot },
{ "lookat", lmat_lookat },
{ NULL, NULL },
};
create_meta(L, l, "matrix", lmat_tostring);
lua_pushcclosure(L, lnewmat, 1);
}
static int
lnewvec4(lua_State *L) {
struct vector4 tmp;
if (lua_isuserdata(L, 1)) {
struct vector4 *copy = check_userdata(L,1);
tmp = *copy;
} else {
tmp.x = luaL_optnumber(L, 1, 0);
tmp.y = luaL_optnumber(L, 2, 0);
tmp.z = luaL_optnumber(L, 3, 0);
tmp.w = luaL_optnumber(L, 4, 1.0);
}
struct vector4 *vec4 = lua_newuserdata(L, sizeof(*vec4));
*vec4 = tmp;
lua_pushvalue(L, lua_upvalueindex(1));
lua_setmetatable(L, -2);
return 1;
}
#define lvec4_tostring lquat_tostring
#define lvec4_copy lquat_copy
#define lvec4_pack lquat_pack
#define lvec4_unpack lquat_unpack
static int
lvec4_mul(lua_State *L) {
if (lua_gettop(L) == 2) {
struct vector4 *v = check_userdata(L,1);
union matrix44 *m = check_userdata(L,2);
vector4_mul(v,m);
} else {
struct vector4 *r = check_userdata(L,1);
struct vector4 *v = check_userdata(L,2);
union matrix44 *m = check_userdata(L,3);
*r = *v;
vector4_mul(r,m);
}
lua_settop(L,1);
return 1;
}
static void
vector4(lua_State *L) {
luaL_Reg l[] = {
{ "copy", lvec4_copy },
{ "pack", lvec4_pack },
{ "unpack", lvec4_unpack },
{ "mul", lvec4_mul },
{ NULL, NULL },
};
create_meta(L, l, "vector4", lvec4_tostring);
lua_pushcclosure(L, lnewvec4, 1);
}
static int
lnewplane(lua_State *L) {
int top = lua_gettop(L);
if (top == 0) {
struct plane *p = lua_newuserdata(L, sizeof(*p));
p->normal.x = 0;
p->normal.y = 0;
p->normal.z = 1;
p->dist = 0; // XY plane
} else if (top == 1) {
struct plane *copy = check_userdata(L,1);
struct plane *p = lua_newuserdata(L, sizeof(*p));
*p = *copy;
} else if (top == 3) {
struct vector3 *a = check_userdata(L,1);
struct vector3 *b = check_userdata(L,2);
struct vector3 *c = check_userdata(L,3);
struct plane *p = lua_newuserdata(L, sizeof(*p));
plane_init_dot3(p, a,b,c);
} else {
return luaL_error(L, "Invalid new plane");
}
lua_pushvalue(L, lua_upvalueindex(1));
lua_setmetatable(L, -2);
return 1;
}
static int
lplane_tostring(lua_State *L) {
struct plane *p = check_userdata(L, 1);
lua_pushfstring(L, "[%f, %f, %f : %f]", p->normal.x, p->normal.x, p->normal.z, p->dist);
return 1;
}
static int
lplane_dist(lua_State *L) {
struct plane *p = check_userdata(L, 1);
struct vector3 *v = check_userdata(L, 2);
float d = plane_dist(p,v);
lua_pushnumber(L, d);
return 1;
}
static int
lplane_copy(lua_State *L) {
struct plane *p = check_userdata(L, 1);
struct plane *from = check_userdata(L, 2);
*p = *from;
lua_settop(L,1);
return 1;
}
static int
lplane_dot3(lua_State *L) {
struct plane *p = check_userdata(L, 1);
struct vector3 *a = check_userdata(L,2);
struct vector3 *b = check_userdata(L,3);
struct vector3 *c = check_userdata(L,4);
plane_init_dot3(p, a,b,c);
lua_settop(L,1);
return 1;
}
static int
lplane_normal(lua_State *L) {
struct plane *p = check_userdata(L, 1);
lua_pushnumber(L, p->normal.x);
lua_pushnumber(L, p->normal.y);
lua_pushnumber(L, p->normal.z);
return 3;
}
static void
plane(lua_State *L) {
luaL_Reg l[] = {
{ "copy", lplane_copy },
{ "dist", lplane_dist },
{ "dot3", lplane_dot3 },
{ "normal", lplane_normal },
{ NULL, NULL },
};
create_meta(L, l, "plane", lplane_tostring);
lua_pushcclosure(L, lnewplane, 1);
}
static int
lraytriangle(lua_State *L) {
int top = lua_gettop(L);
if (top != 6) {
return luaL_error(L, "intersection.raytriangle(rayOrig,rayDir,p0,p1,p2,ret)");
}
struct vector3 *ro = check_userdata(L,1);
struct vector3 *rd = check_userdata(L,2);
struct vector3 *p0 = check_userdata(L,3);
struct vector3 *p1 = check_userdata(L,4);
struct vector3 *p2 = check_userdata(L,5);
struct vector3 *inst = check_userdata(L,6);
if (intersection_raytriangle(ro,rd,p0,p1,p2,inst) == NULL) {
return 0;
}
return 1;
}
static int
lrayAABB(lua_State *L) {
int top = lua_gettop(L);
if (top != 4) {
return luaL_error(L, "intersection.rayAABB(rayOrig,rayDir,mins,maxs)");
}
struct vector3 *ro = check_userdata(L,1);
struct vector3 *rd = check_userdata(L,2);
struct vector3 *mins = check_userdata(L,3);
struct vector3 *maxs = check_userdata(L,4);
int r = intersection_rayAABB(ro,rd,mins,maxs);
lua_pushboolean(L,r);
return 1;
}
int
luaopen_math3d(lua_State *L) {
luaL_checkversion(L);
lua_newtable(L);
vector3(L);
lua_setfield(L, -2, "vector3");
quaternion(L);
lua_setfield(L, -2, "quaternion");
matrix(L);
lua_setfield(L, -2, "matrix");
vector4(L);
lua_setfield(L, -2, "vector4");
plane(L);
lua_setfield(L, -2, "plane");
luaL_Reg l[] = {
{ "raytriangle", lraytriangle },
{ "rayAABB", lrayAABB },
{ NULL, NULL },
};
luaL_newlib(L,l);
lua_setfield(L, -2, "intersection");
return 1;
}