-
Notifications
You must be signed in to change notification settings - Fork 0
/
libmatrix.c
795 lines (708 loc) · 21.6 KB
/
libmatrix.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
#include <math.h>
#include <stdio.h>
#include <assert.h>
float cot(float rad)
{
return 1.0/tan(rad);
}
void assert_vector_equiv(vec4 a, vec4 b)
{
assert(a.x == b.x);
assert(a.y == b.y);
assert(a.z == b.z);
assert(a.w == b.w);
}
void assert_matrix_equiv(mat4 a, mat4 b)
{
assert_vector_equiv(a.a, b.a);
assert_vector_equiv(a.b, b.b);
assert_vector_equiv(a.c, b.c);
assert_vector_equiv(a.d, b.d);
return;
}
void vector_print(vec4 a)
{
printf("[ %.6f, %.6f, %.6f, %.6f ]\n", a.x, a.y, a.z, a.w);
}
vec4 vector_scalar_multiply(float s, vec4 a)
{
vec4 res = {a.x * s, a.y * s, a.z * s, a.w * s};
return res;
}
vec4 vector_add(vec4 a, vec4 b)
{
vec4 res = {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
return res;
}
vec4 vector_subtract(vec4 a, vec4 b)
{
vec4 res = {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
return res;
}
float vector_dot_product(vec4 a, vec4 b)
{
return (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + (a.w * b.w);
}
vec4 vector_cross_product(vec4 a, vec4 b)
{
vec4 res = {(a.y * b.z) - (a.z * b.y), (a.z * b.x) - (a.x * b.z),
(a.x * b.y) - (a.y * b.x), 0.0};
return res;
}
float vector_magnitude(vec4 v)
{
return sqrt(pow(v.x, 2) + pow(v.y, 2) + pow(v.z, 2) + pow(v.w, 2));
}
vec4 vector_normalize(vec4 v)
{
return vector_scalar_multiply(1 / vector_magnitude(v), v);
}
void matrix_print(mat4 m)
{
printf("┌ %.6f\t%.6f\t%.6f\t%.6f\t┐\n"
"│ %.6f\t%.6f\t%.6f\t%.6f\t│\n"
"│ %.6f\t%.6f\t%.6f\t%.6f\t│\n"
"└ %.6f\t%.6f\t%.6f\t%.6f\t┘\n",
m.a.x, m.b.x, m.c.x, m.d.x,
m.a.y, m.b.y, m.c.y, m.d.y,
m.a.z, m.b.z, m.c.z, m.d.z,
m.a.w, m.b.w, m.c.w, m.d.w);
}
mat4 matrix_scalar_multiply(float a, mat4 m)
{
vec4 x = vector_scalar_multiply(a, m.a);
vec4 y = vector_scalar_multiply(a, m.b);
vec4 z = vector_scalar_multiply(a, m.c);
vec4 w = vector_scalar_multiply(a, m.d);
mat4 res = {x, y, z, w};
return res;
}
mat4 matrix_add(mat4 a, mat4 b)
{
vec4 x = vector_add(a.a, b.a);
vec4 y = vector_add(a.b, b.b);
vec4 z = vector_add(a.c, b.c);
vec4 w = vector_add(a.d, b.d);
mat4 res = {x, y, z, w};
return res;
}
mat4 matrix_subtract(mat4 a, mat4 b)
{
vec4 x = vector_subtract(a.a, b.a);
vec4 y = vector_subtract(a.b, b.b);
vec4 z = vector_subtract(a.c, b.c);
vec4 w = vector_subtract(a.d, b.d);
mat4 res = {x, y, z, w};
return res;
}
mat4 matrix_multiply(mat4 a, mat4 b)
{
float c11 = (a.a.x * b.a.x) + (a.b.x * b.a.y) + (a.c.x * b.a.z) + (a.d.x * b.a.w);
float c21 = (a.a.y * b.a.x) + (a.b.y * b.a.y) + (a.c.y * b.a.z) + (a.d.y * b.a.w);
float c31 = (a.a.z * b.a.x) + (a.b.z * b.a.y) + (a.c.z * b.a.z) + (a.d.z * b.a.w);
float c41 = (a.a.w * b.a.x) + (a.b.w * b.a.y) + (a.c.w * b.a.z) + (a.d.w * b.a.w);
float c12 = (a.a.x * b.b.x) + (a.b.x * b.b.y) + (a.c.x * b.b.z) + (a.d.x * b.b.w);
float c22 = (a.a.y * b.b.x) + (a.b.y * b.b.y) + (a.c.y * b.b.z) + (a.d.y * b.b.w);
float c32 = (a.a.z * b.b.x) + (a.b.z * b.b.y) + (a.c.z * b.b.z) + (a.d.z * b.b.w);
float c42 = (a.a.w * b.b.x) + (a.b.w * b.b.y) + (a.c.w * b.b.z) + (a.d.w * b.b.w);
float c13 = (a.a.x * b.c.x) + (a.b.x * b.c.y) + (a.c.x * b.c.z) + (a.d.x * b.c.w);
float c23 = (a.a.y * b.c.x) + (a.b.y * b.c.y) + (a.c.y * b.c.z) + (a.d.y * b.c.w);
float c33 = (a.a.z * b.c.x) + (a.b.z * b.c.y) + (a.c.z * b.c.z) + (a.d.z * b.c.w);
float c43 = (a.a.w * b.c.x) + (a.b.w * b.c.y) + (a.c.w * b.c.z) + (a.d.w * b.c.w);
float c14 = (a.a.x * b.d.x) + (a.b.x * b.d.y) + (a.c.x * b.d.z) + (a.d.x * b.d.w);
float c24 = (a.a.y * b.d.x) + (a.b.y * b.d.y) + (a.c.y * b.d.z) + (a.d.y * b.d.w);
float c34 = (a.a.z * b.d.x) + (a.b.z * b.d.y) + (a.c.z * b.d.z) + (a.d.z * b.d.w);
float c44 = (a.a.w * b.d.x) + (a.b.w * b.d.y) + (a.c.w * b.d.z) + (a.d.w * b.d.w);
mat4 res =
{{c11, c21, c31, c41},
{c12, c22, c32, c42},
{c13, c23, c33, c43},
{c14, c24, c34, c44}};
return res;
}
mat4 matrix_transpose(mat4 m)
{
mat4 res = {{m.a.x, m.b.x, m.c.x, m.d.x}, {m.a.y, m.b.y, m.c.y, m.d.y}, {m.a.z, m.b.z, m.c.z, m.d.z}, {m.a.w, m.b.w, m.c.w, m.d.w}};
return res;
}
vec4 matrix_multiply_vector(mat4 m, vec4 v)
{
vec4 col1 = vector_scalar_multiply(v.x, m.a);
vec4 col2 = vector_scalar_multiply(v.y, m.b);
vec4 col3 = vector_scalar_multiply(v.z, m.c);
vec4 col4 = vector_scalar_multiply(v.w, m.d);
vec4 res = vector_add(vector_add(vector_add(col1, col2), col3), col4);
return res;
}
mat4 matrix_cofactor(mat4 m)
{
mat4 cofactor = {{m.a.x, -m.a.y, m.a.z, -m.a.w},
{-m.b.x, m.b.y, -m.b.z, m.b.w},
{m.c.x, -m.c.y, m.c.z, -m.c.w},
{-m.d.x, m.d.y, -m.d.z, m.d.w}};
return cofactor;
}
void print_sub_3(mat3 m)
{
printf("%.6f %.6f %.6f, %.6f %.6f %.6f, %.6f %.6f %.6f \n",
m.a, m.b, m.c, m.d, m.e, m.f, m.g, m.h, m.i);
}
float matrix3_determinant(mat3 m)
{
return m.a * m.e * m.i + m.b * m.f * m.g + m.c * m.d * m.h - m.g * m.e * m.c -
m.h * m.f * m.a - m.i * m.d * m.b;
}
float m3d(mat3 m)
{
return matrix3_determinant(m);
}
mat4 matrix_minor(mat4 m)
{
mat3 m11 = {m.b.y, m.c.y, m.d.y, m.b.z, m.c.z, m.d.z, m.b.w, m.c.w, m.d.w};
mat3 m12 = {m.a.y, m.c.y, m.d.y, m.a.z, m.c.z, m.d.z, m.a.w, m.c.w, m.d.w};
mat3 m13 = {m.a.y, m.b.y, m.d.y, m.a.z, m.b.z, m.d.z, m.a.w, m.b.w, m.d.w};
mat3 m14 = {m.a.y, m.b.y, m.c.y, m.a.z, m.b.z, m.c.z, m.a.w, m.b.w, m.c.w};
mat3 m21 = {m.b.x, m.c.x, m.d.x, m.b.z, m.c.z, m.d.z, m.b.w, m.c.w, m.d.w};
mat3 m22 = {m.a.x, m.c.x, m.d.x, m.a.z, m.c.z, m.d.z, m.a.w, m.c.w, m.d.w};
mat3 m23 = {m.a.x, m.b.x, m.d.x, m.a.z, m.b.z, m.d.z, m.a.w, m.b.w, m.d.w};
mat3 m24 = {m.a.x, m.b.x, m.c.x, m.a.z, m.b.z, m.c.z, m.a.w, m.b.w, m.c.w};
mat3 m31 = {m.b.x, m.c.x, m.d.x, m.b.y, m.c.y, m.d.y, m.b.w, m.c.w, m.d.w};
mat3 m32 = {m.a.x, m.c.x, m.d.x, m.a.y, m.c.y, m.d.y, m.a.w, m.c.w, m.d.w};
mat3 m33 = {m.a.x, m.b.x, m.d.x, m.a.y, m.b.y, m.d.y, m.a.w, m.b.w, m.d.w};
mat3 m34 = {m.a.x, m.b.x, m.c.x, m.a.y, m.b.y, m.c.y, m.a.w, m.b.w, m.c.w};
mat3 m41 = {m.b.x, m.c.x, m.d.x, m.b.y, m.c.y, m.d.y, m.b.z, m.c.z, m.d.z};
mat3 m42 = {m.a.x, m.c.x, m.d.x, m.a.y, m.c.y, m.d.y, m.a.z, m.c.z, m.d.z};
mat3 m43 = {m.a.x, m.b.x, m.d.x, m.a.y, m.b.y, m.d.y, m.a.z, m.b.z, m.d.z};
mat3 m44 = {m.a.x, m.b.x, m.c.x, m.a.y, m.b.y, m.c.y, m.a.z, m.b.z, m.c.z};
mat4 minor =
{{m3d(m11), m3d(m21), m3d(m31), m3d(m41)},
{m3d(m12), m3d(m22), m3d(m32), m3d(m42)},
{m3d(m13), m3d(m23), m3d(m33), m3d(m43)},
{m3d(m14), m3d(m24), m3d(m34), m3d(m44)}};
return minor;
}
mat4 matrix_invert(mat4 m)
{
mat4 minor = matrix_minor(m);
mat4 cofactor = matrix_cofactor(minor);
mat4 transpose = matrix_transpose(cofactor);
float determinant =
m.a.x * minor.a.x - m.b.x * minor.b.x + m.c.x * minor.c.x - m.d.x * minor.d.x;
mat4 res = matrix_scalar_multiply(1 / determinant, transpose);
return res;
}
mat4 rotation_z_matrix(float radians)
{
mat4 res = {{cos(radians), sin(radians), 0, 0}, {-sin(radians), cos(radians), 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
return res;
}
mat4 rotation_x_matrix(float radians)
{
mat4 res = {{1, 0, 0, 0}, {0, cos(radians), sin(radians), 0}, {0, -sin(radians), cos(radians), 0}, {0, 0, 0, 1}};
return res;
}
mat4 rotation_y_matrix(float radians)
{
mat4 res = {{cos(radians), 0, -sin(radians), 0}, {0, 1, 0, 0}, {sin(radians), 0, cos(radians), 0}, {0, 0, 0, 1}};
return res;
}
mat4 scaling_matrix(float x, float y, float z)
{
mat4 res = {{x, 0, 0, 0}, {0, y, 0, 0}, {0, 0, z, 0}, {0, 0, 0, 1}};
return res;
}
mat4 scaling_matrix_vector(vec4 a)
{
return scaling_matrix(a.x, a.y, a.z);
}
void gen_cone(vec4 *buf, int deg, int offset, int len)
{
// printf("Starting gen_cone at %d until %d", offset, len);
float r = 0.5;
float theta_rad = deg * (M_PI / 180);
mat4 rmat = rotation_y_matrix(theta_rad);
vec4 origin = {0.0, 0.0, 0.0, 1.0};
vec4 br = {r, 0.0, 0.0, 1.0};
vec4 tr = {r * cos(theta_rad), 0.0, r * sin(theta_rad), 1.0};
vec4 top = {0.0, r, 0.0, 1.0};
// top right is the one that gets the rotation
// br = tr;
for (int i = offset; i < len;)
{
buf[i] = origin;
i++;
buf[i] = tr;
i++;
buf[i] = br;
i++;
buf[i] = top;
i++;
buf[i] = tr;
i++;
buf[i] = br;
i++;
br = tr;
tr = matrix_multiply_vector(rmat, tr);
}
}
void generate_torus(vec4 *buf, int len, int deg)
{
//first start with a circle offset by the origin
float r0 = 0.25; // the torus will be of radius 0.5
float r1 = 0.125; //0.1275; // the radius of the disk we will rotate about y
float theta_rad = deg * (M_PI / 180);
mat4 trl = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {r0, 0, 0, 1}};
mat4 rmatz = rotation_z_matrix(theta_rad);
mat4 rmaty = rotation_y_matrix(theta_rad);
mat4 rmaty90 = rotation_y_matrix(M_PI / 2);
mat4 rmatx90 = rotation_x_matrix(M_PI / 2);
int sections = 360 / deg;
// start with the parameters for the first mesh of the segment
vec4 r = {r1, 0.0, 0.0, 1.0};
vec4 d = {r1 * cos(theta_rad), r1 * sin(theta_rad), 0.0, 1.0};
vec4 dz = matrix_multiply_vector(rmaty, d);
vec4 rz = matrix_multiply_vector(rmaty, r);
vector_print(r);
vector_print(d);
puts("");
// 0 thru 36 of buf is the first section
int i;
for (i = 0; i < 216;)
{
// trl matrix will move the section into position along the radius
buf[i] = matrix_multiply_vector(trl, dz);
i++;
buf[i] = matrix_multiply_vector(trl, rz);
i++;
buf[i] = matrix_multiply_vector(trl, r);
i++;
buf[i] = matrix_multiply_vector(trl, dz);
i++;
buf[i] = matrix_multiply_vector(trl, r);
i++;
buf[i] = matrix_multiply_vector(trl, d);
i++;
r = d;
rz = dz;
d = matrix_multiply_vector(rmatz, d);
dz = matrix_multiply_vector(rmatz, dz);
}
rmaty = rotation_y_matrix(theta_rad / 2);
for (int section = 1; section < 36; section++)
{
for (int j = 0; j < 216, i < len; j++)
{
buf[i] = matrix_multiply_vector(rmaty, buf[i - 216]);
i++;
buf[i] = matrix_multiply_vector(rmaty, buf[i - 216]);
i++;
buf[i] = matrix_multiply_vector(rmaty, buf[i - 216]);
i++;
buf[i] = matrix_multiply_vector(rmaty, buf[i - 216]);
i++;
buf[i] = matrix_multiply_vector(rmaty, buf[i - 216]);
i++;
buf[i] = matrix_multiply_vector(rmaty, buf[i - 216]);
i++;
}
}
for (int x = 0; x < len; x++)
{
buf[x] = matrix_multiply_vector(rmatx90, buf[x]);
}
}
void generate_rectangle(vec4 *buf, int len, float l, float w, float h)
{
vec4 rectangle_vertices[36] = {
// front face of cube
{0.0, 0.0, 0.0, 1.0},
{0.0, h, 0.0, 1.0},
{w, 0.0, 0.0, 1.0},
{w, 0.0, 0.0, 1.0},
{0.0, h, 0.0, 1.0},
{w, h, 0.0, 1.0},
//right side of cube
{w, 0.0, 0.0, 1.0},
{w, h, 0.0, 1.0},
{w, 0.0, l, 1.0},
{w, 0.0, l, 1.0},
{w, h, 0.0, 1.0},
{w, h, l, 1.0},
//back side of cube
{w, 0.0, l, 1.0},
{w, h, l, 1.0},
{0.0, 0.0, l, 1.0},
{0.0, 0.0, l, 1.0},
{w, h, l, 1.0},
{0.0, h, l, 1.0},
//left side of cube
{0.0, 0.0, l, 1.0},
{0.0, h, l, 1.0},
{0.0, 0.0, 0.0, 1.0},
{0.0, 0.0, 0.0, 1.0},
{0.0, h, l, 1.0},
{0.0, h, 0.0, 1.0},
//top of cube
{0.0, h, 0.0, 1.0},
{0.0, h, l, 1.0},
{w, h, 0.0, 1.0},
{w, h, 0.0, 1.0},
{0.0, h, l, 1.0},
{w, h, l, 1.0},
//bottom of cube
{0.0, 0.0, 0.0, 1.0},
{w, 0.0, 0.0, 1.0},
{0.0, 0.0, l, 1.0},
{0.0, 0.0, l, 1.0},
{w, 0.0, 0.0, 1.0},
{w, 0.0, l, 1.0},
};
for (int i = 0; i < len && i < 36; i++)
{
buf[i] = rectangle_vertices[i];
}
}
void generate_cube(vec4 *buf, int len, float delta, int offset)
{
vec4 cube_vertices[72] = {
//front
{0.0, 0.0, 0.0, 1.0},
{0.0, delta, 0.0, 1.0},
{delta, 0.0, 0.0, 1.0},
{delta, 0.0, 0.0, 1.0},
{0.0, delta, 0.0, 1.0},
{delta, delta, 0.0, 1.0},
//right side of cube
{delta, 0.0, 0.0, 1.0},
{delta, delta, 0.0, 1.0},
{delta, 0.0, delta, 1.0},
{delta, 0.0, delta, 1.0},
{delta, delta, 0.0, 1.0},
{delta, delta, delta, 1.0},
//back side of cube
{delta, 0.0, delta, 1.0},
{delta, delta, delta, 1.0},
{0.0, 0.0, delta, 1.0},
{0.0, 0.0, delta, 1.0},
{delta, delta, delta, 1.0},
{0.0, delta, delta, 1.0},
//left side of cube
{0.0, 0.0, delta, 1.0},
{0.0, delta, delta, 1.0},
{0.0, 0.0, 0.0, 1.0},
{0.0, 0.0, 0.0, 1.0},
{0.0, delta, delta, 1.0},
{0.0, delta, 0.0, 1.0},
//top of cube
{0.0, delta, 0.0, 1.0},
{0.0, delta, delta, 1.0},
{delta, delta, 0.0, 1.0},
{delta, delta, 0.0, 1.0},
{0.0, delta, delta, 1.0},
{delta, delta, delta, 1.0},
//bottom of cube
{0.0, 0.0, 0.0, 1.0},
{delta, 0.0, 0.0, 1.0},
{0.0, 0.0, delta, 1.0},
{0.0, 0.0, delta, 1.0},
{delta, 0.0, 0.0, 1.0},
{delta, 0.0, delta, 1.0},
// ********* BLACK VERTICES ****************
// front face of cube
{0.0, 0.0, 0.0, 1.0},
{0.0, delta, 0.0, 1.0},
{delta, 0.0, 0.0, 1.0},
{delta, 0.0, 0.0, 1.0},
{0.0, delta, 0.0, 1.0},
{delta, delta, 0.0, 1.0},
//right side of cube
{delta, 0.0, 0.0, 1.0},
{delta, delta, 0.0, 1.0},
{delta, 0.0, delta, 1.0},
{delta, 0.0, delta, 1.0},
{delta, delta, 0.0, 1.0},
{delta, delta, delta, 1.0},
//back side of cube
{delta, 0.0, delta, 1.0},
{delta, delta, delta, 1.0},
{0.0, 0.0, delta, 1.0},
{0.0, 0.0, delta, 1.0},
{delta, delta, delta, 1.0},
{0.0, delta, delta, 1.0},
//left side of cube
{0.0, 0.0, delta, 1.0},
{0.0, delta, delta, 1.0},
{0.0, 0.0, 0.0, 1.0},
{0.0, 0.0, 0.0, 1.0},
{0.0, delta, delta, 1.0},
{0.0, delta, 0.0, 1.0},
//top of cube
{0.0, delta, 0.0, 1.0},
{0.0, delta, delta, 1.0},
{delta, delta, 0.0, 1.0},
{delta, delta, 0.0, 1.0},
{0.0, delta, delta, 1.0},
{delta, delta, delta, 1.0},
//bottom of cube
{0.0, 0.0, 0.0, 1.0},
{delta, 0.0, 0.0, 1.0},
{0.0, 0.0, delta, 1.0},
{0.0, 0.0, delta, 1.0},
{delta, 0.0, 0.0, 1.0},
{delta, 0.0, delta, 1.0},
};
for (int i = offset, j = 0; i < len && j < 72; i++, j++)
{
buf[i] = cube_vertices[j];
}
}
vec4 generate_panel(vec4* buf, int len, int offset, vec4 start)
{
mat4 rotation_y = rotation_y_matrix( -(SPHERE_THETA * (M_PI/180)) );
mat4 rotation_z = rotation_z_matrix( (SPHERE_THETA * (M_PI/180)) );
// with the start vector, it will rotate it once around the x
// axis and once around the z axis, and then it will rotate that point
// it created via x by the z axis as well.
vec4 origin = start;
vec4 tl = matrix_multiply_vector(rotation_y, start);
vec4 br = matrix_multiply_vector(rotation_z, start);
vec4 tr = matrix_multiply_vector(rotation_z, tl);
// now rip them into the buffer
buf[offset] = origin;
buf[offset + 1] = tr;
buf[offset + 2] = br;
buf[offset + 3] = origin;
buf[offset + 4] = tl;
buf[offset + 5] = tr;
vector_print(origin);
vector_print(tr);
vector_print(br);
vector_print(origin);
vector_print(tl);
vector_print(tr);
}
// we will use degree ten
void generate_sphere_section(vec4* buf, int len, int offset)
{
float r = 1.0;
int max_points = 36;
float theta = SPHERE_THETA * (M_PI/180);
}
// we should probably just keep it standardized to 10
void generate_sphere(vec4 *buf, int len, float phi)
{
// use z-axis rotation for these points
float r = 1.0;
int max_points = 360 / phi;
// the amount to shrink each ring by is determined by the amount of points
// divided by two and inverted, so that everything remains uniformly
// proportional and eventually r will equal 0 for the last ring
float delta = 1/(max_points / 2);
vec4 ring_a[max_points];
vec4 ring_b[max_points];
float theta = phi * (M_PI/180);
mat4 rmat = rotation_z_matrix(theta);
// rightmost point on the unit circle
vec4 head = {r, 0.0, 0.0, 1.0};
ring_a[0] = head;
// we will, for every ring of points, only generate 360/phi points
for(int i = 1; i < max_points; i++)
{
// you fill the ring by rotation the preceeding point around the z axis
ring_a[i] = matrix_multiply_vector(rmat, ring_a[i-1]);
}
r = r - delta;
for(int i = 0; i < max_points; i++)
{
// the second ring just appears as a smaller ring later on down the line
ring_b[i] = vector_scalar_multiply(r, ring_a[i]);
}
// TODO finish this later
}
void generate_pool_table(vec4* buf, int len, int offset)
{
vec4 bl, br, tr, tl;
bl = (vec4){-POOL_TABLE_UNIT, -POOL_TABLE_UNIT, 0.0, 1.0};
br = (vec4){POOL_TABLE_UNIT, -POOL_TABLE_UNIT, 0.0, 1.0};
tr = (vec4){POOL_TABLE_UNIT, POOL_TABLE_UNIT, 0.0, 1.0};
tl = (vec4){-POOL_TABLE_UNIT, POOL_TABLE_UNIT, 0.0, 1.0};
buf[offset] = bl;
buf[offset + 1] = br;
buf[offset + 2] = tr;
buf[offset + 3] = bl;
buf[offset + 4] = tr;
buf[offset + 5] = tl;
}
mat4 scale(float factor)
{
mat4 result = {{factor, 0, 0, 0}, {0, factor, 0, 0}, {0, 0, factor, 0}, {0, 0, 0, 1}};
return result;
}
mat4 translate(float x, float y, float z)
{
mat4 result = {{1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0}};
result.d.x = x;
result.d.y = y;
result.d.z = z;
return result;
}
mat4 translate_vector(vec4 a)
{
return translate(a.x, a.y, a.z);
}
mat4 identity()
{
mat4 result = {{1.0, 0.0, 0.0, 0.0}, {0.0, 1.0, 0.0, 0.0}, {0.0, 0.0, 1.0, 0.0}, {0.0, 0.0, 0.0, 1.0}};
return result;
}
mat4 look_at(float eyex, float eyey, float eyez, float atx, float aty, float atz, float upx, float upy, float upz)
{
vec4 e = {eyex, eyey, eyez, 1.0}; // looking at this object (vrp)
vec4 a = {atx, aty, atz, 1.0}; // from this point
vec4 vup = {upx, upy, upz, 0.0}; // this is the vector from the top of our heads, up
vec4 vpn = vector_subtract(a, e);
vec4 n = vector_normalize(vpn);
vec4 u = vector_cross_product(vup, n);
u = vector_normalize(u);
vec4 v = vector_cross_product(n, u);
v = vector_normalize(v);
mat4 T = translate(-eyex, -eyey, -eyez);
mat4 A = {u, v, n, {0, 0, 0, 1}};
mat4 R = matrix_invert(A);
mat4 V = matrix_multiply(R, T);
return V;
}
mat4 look_at_vector(vec4 eye, vec4 at, vec4 up)
{
return look_at(eye.x, eye.y, eye.z, at.x, at.y, at.z, up.x, up.y, up.z);
}
/* Frustum using Tan's method */
mat4 frustum(float left, float right,
float bottom, float top,
float near, float far)
{
float a = (-2.0 * near) / (right - left);
float b = (-2.0 * near) / (top - bottom);
float A = (left + right) / (right - left);
float B = (bottom + top) / (top - bottom);
float C = (near + far) / (far - near);
float D = -(2 * near * far) / (far - near);
vec4 cola = {a, 0, 0, 0};
vec4 colb = {0, b, 0, 0};
vec4 colc = {A, B, C, -1};
vec4 cold = {0, 0, D, 0};
mat4 result = {cola, colb, colc, cold};
// matrix_print(result);
return result;
}
/* Frustum using OpenGL specifications */
mat4 frustum_gl(float left, float right,
float bottom, float top,
float near, float far)
{
float a = (2*near)/(right - left);
float b = (2*near)/(top - bottom);
float A = (right + left)/(right - left);
float B = (top + bottom)/(top - bottom);
float C = -(far + near)/(far - near);
float D = -(2*far*near)/(far - near);
vec4 cola = {a, 0, 0, 0};
vec4 colb = {0, b, 0, 0};
vec4 colc = {A, B, C, -1};
vec4 cold = {0, 0, D, 0};
return (mat4){cola, colb, colc, cold};
}
mat4 fov_perspective(float fovy, float aspect, float near, float far)
{
vec4 a = {-cot(fovy/2.0)/aspect, 0, 0, 0};
vec4 b = {0, -cot(fovy/2.0), 0, 0};
vec4 c = {0, 0, (near+far)/(far-near), -1};
vec4 d = {0, 0, -(2*near*far)/(far-near), 0};
return (mat4){a, b, c, d};
}
mat4 perspective(float left, float right,
float bottom, float top,
float near, float far)
{
mat4 result = {{-(near / right),0,0,0},
{0, -(near / top), 0, 0},
{0, 0, (near + far) / (far - near), -1},
{0,0,-(2 * near * far) / (far - near), 0}};
return result;
}
void fill_colors_until(vec4* buf, int len, int offset)
{
for(int i = offset; i < len; i++)
{
buf[i] = COLORS[i % 72];
}
}
void fill_colors(vec4 *buf, int len)
{
for (int i = 0; i < len; i++)
{
buf[i] = COLORS[i % 72];
}
}
void fill_colors_with(vec4* buf, vec4 color, int offset, int len)
{
// printf("Starting fill_colors_with at %d until %d", offset, len);
for(int i = offset; i < len; i++)
{
buf[i] = color;
}
}
void uniform_transform(vec4* buf, mat4 ctm, int offset, int len)
{
// printf("Starting uniform transform at %d until %d", offset, len);
for(int i = offset; i < len; i++)
{
buf[i] = matrix_multiply_vector(ctm, buf[i]);
}
}
vec4 vector_abs(vec4 a)
{
return (vec4){abs(a.x), abs(a.y), abs(a.z), abs(a.w)};
}
vec4 vector_abs_normalize(vec4 v)
{
return vector_scalar_multiply(1 / vector_magnitude(vector_abs(v)), v);
}
void point_subtractions(vec4* buf, vec4 p1, vec4 p2, vec4 p3)
{
buf[0] = vector_subtract(p2, p1);
buf[1] = vector_subtract(p3, p2);
}
vec4 normal(vec4 a, vec4 b)
{
vec4 n = vector_cross_product(a, b);
return vector_abs_normalize(n);
}
vec4 triangle_normal(vec4 p1, vec4 p2, vec4 p3)
{
vec4 vectors[2];
point_subtractions(vectors, p1, p2, p3);
puts("*");
vector_print(vectors[0]);
vector_print(vectors[1]);
return normal(vectors[0], vectors[1]);
}
vec4 return_normal_at(int index, vec4* buf, int len)
{
if(!(index >= len || index+1 >= len || index+2 >= len)){
return triangle_normal(buf[index], buf[index+1], buf[index+2]);
}
}
void fill_with_normals(vec4* vertices, vec4* normals, int offset, int len)
{
for(int i = offset; i < len; i+=3)
{
vec4 cur_normal = return_normal_at(i, vertices, len);
normals[i] = cur_normal;
normals[i+1] = cur_normal;
normals[i+1] = cur_normal;
}
}
void generate_rubiks_cube(vec4* vertices, int offset, int len)
{
}