-
Notifications
You must be signed in to change notification settings - Fork 0
/
vlibc.h
1556 lines (1346 loc) · 48 KB
/
vlibc.h
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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __VLIBC__
#define __VLIBC__
typedef unsigned char vlibc_uint8_t;
typedef unsigned short vlibc_uint16_t;
typedef unsigned int vlibc_uint32_t;
typedef unsigned long int vlibc_uint64_t;
typedef long unsigned int vlibc_size_t;
#ifndef bool
typedef int bool;
enum { false, true };
#endif
#define VLIBCDEF static const
#define VLIBC_MIN(a,b) (((a)<(b))?(a):(b))
#define VLIBC_MAX(a,b) (((a)>(b))?(a):(b))
#define VLIBC_SIGN(T, a) ((T)(a > 0) - (T)(a < 0))
#define VLIBC_ABS(T, a) (VLIBC_SIGN(T, a)*a)
#ifndef VLIBC_NULL
#define VLIBC_NULL '\0'
#endif
#define vlibc_nullptr ((void*)VLIBC_NULL)
#define VLIBC_PI 3.141592653589793
#define VLIBC_HALF_PI 1.570796326794897
#define VLIBC_DOUBLE_PI 6.283185307179586
#define VLIBC_SIN_CURVE_A 0.0415896
#define VLIBC_SIN_CURVE_B 0.00129810625032
#define VLIBC_RAD2DEG (180.f / VLIBC_PI)
#define VLIBC_DEG2RAD (VLIBC_PI / 180.f)
typedef struct {
float x, y;
} vlibc_vec2d;
typedef struct {
float x, y, z;
} vlibc_vec3d;
typedef struct {
float x, y, z, w;
} vlibc_vec4d;
typedef struct {
vlibc_uint32_t *pixels;
vlibc_vec2d size;
} vlibc_canvas;
typedef struct {
vlibc_uint8_t r, g, b, a;
} vlibc_rgba;
typedef struct {
float r, g, b, a;
} vlibc_f_rgba;
typedef struct {
vlibc_vec2d pos;
vlibc_f_rgba* col;
int col_count;
} vlibc_vertex;
typedef union {
float m[4][4];
struct {
float m00, m01, m02, m03;
float m10, m11, m12, m13;
float m20, m21, m22, m23;
float m30, m31, m32, m33;
};
} vlibc_mat4_t;
typedef struct vlibc_shader_data {
float *data;
vlibc_f_rgba* passthrough_data;
vlibc_size_t size;
} vlibc_shader_data_t;
typedef struct vlibc_fragment_shader{
vlibc_uint32_t (*func)(vlibc_vec2d frag_pos, vlibc_rgba frag_color, vlibc_shader_data_t* data);
} vlibc_fragment_shader_t;
#define __vlibc_fast_put_pixel(vlibcc, color, pos) if (vlibc_in_bounds(vlibcc, pos) == true) { vlibcc->pixels[(int)(pos.y*vlibcc->size.x + pos.x)] = vlibc_rgba_to_hex(color); }
#define VLIBC_VEC2D_NULL ((vlibc_vec2d){0, 0})
#define VLIBC_VEC3D_NULL ((vlibc_vec3d){0, 0})
#define VLIBC_VEC4D_NULL ((vlibc_vec4d){0, 0})
#define VLIBC_RGBA_NULL ((vlibc_rgba){0, 0})
#define VLIBC_VERTEX_NULL ((vlibc_vertex){VLIBC_VEC2D_NULL, VLIBC_RGBA_NULL})
#define VLIBC_CANVAS_NULL ((vlibc_canvas){vlibc_nullptr, VLIBC_VEC2D_NULL})
#define VLIBC_SHADER_DATA_NULL ((vlibc_shader_data_t){vlibc_nullptr, 0})
/*math functions*/
VLIBCDEF int vlibc_swap(int *f, int *s);
VLIBCDEF float vlibc_swap_float(float *f, float *s);
VLIBCDEF float vlibc_cross_product(vlibc_vec2d p1, vlibc_vec2d p2);
VLIBCDEF float vlibc_edge(vlibc_vec2d p1, vlibc_vec2d p2, vlibc_vec2d p);
VLIBCDEF float vlibc_lerp(float a, float b, float t);
VLIBCDEF float vlibc_linearize_depth(float d, float near, float far);
VLIBCDEF float vlibc_fract(float f);
VLIBCDEF float vlibc_clamp(float x, float a, float b);
VLIBCDEF float vlibc_saturate(float x);
VLIBCDEF float vlibc_smoothstep(float a, float b, float x);
VLIBCDEF int vlibc_step(float x, float num);
VLIBCDEF double vlibc_pow(double a, double b);
VLIBCDEF double vlibc_fact(double x);
VLIBCDEF double vlibc_sin(double x);
VLIBCDEF double vlibc_cos(double x);
VLIBCDEF double vlibc_tan(double x);
VLIBCDEF float vlibc_rsqrt(float x);
VLIBCDEF float vlibc_sqrt(float x);
VLIBCDEF bool vlibc_in_bounds(vlibc_canvas *vlibcc, vlibc_vec2d p);
/*vector functions*/
VLIBCDEF vlibc_vec3d vlibc_vec3d_cross(vlibc_vec3d a, vlibc_vec3d b);
VLIBCDEF vlibc_vec3d vlibc_vec3d_proj(vlibc_vec3d v, vlibc_vec3d onto);
VLIBCDEF float vlibc_vec2d_dot(vlibc_vec2d a, vlibc_vec2d b);
VLIBCDEF float vlibc_vec3d_dot(vlibc_vec3d a, vlibc_vec3d b);
VLIBCDEF float vlibc_vec4d_dot(vlibc_vec4d a, vlibc_vec4d b);
VLIBCDEF vlibc_vec2d vlibc_vec2d_normalize(vlibc_vec2d v);
VLIBCDEF vlibc_vec3d vlibc_vec3d_normalize(vlibc_vec3d v);
VLIBCDEF vlibc_vec4d vlibc_vec4d_normalize(vlibc_vec4d v);
VLIBCDEF float vlibc_vec2d_length(vlibc_vec2d p);
VLIBCDEF float vlibc_vec3d_length(vlibc_vec3d p);
VLIBCDEF float vlibc_vec4d_length(vlibc_vec4d p);
VLIBCDEF vlibc_vec2d vlibc_vec2d_reflect(vlibc_vec2d i, vlibc_vec2d n);
VLIBCDEF vlibc_vec3d vlibc_vec3d_reflect(vlibc_vec3d i, vlibc_vec3d n);
VLIBCDEF vlibc_vec4d vlibc_vec4d_reflect(vlibc_vec4d i, vlibc_vec4d n);
VLIBCDEF vlibc_vec2d vlibc_vec2d_negate(vlibc_vec2d x);
VLIBCDEF vlibc_vec3d vlibc_vec3d_negate(vlibc_vec3d x);
VLIBCDEF vlibc_vec4d vlibc_vec4d_negate(vlibc_vec4d x);
VLIBCDEF vlibc_vec2d vlibc_mul_vec2d(vlibc_vec2d a, vlibc_vec2d b);
VLIBCDEF vlibc_vec2d vlibc_mul_vec2df(vlibc_vec2d a, float b);
VLIBCDEF vlibc_vec3d vlibc_mul_vec3d(vlibc_vec3d a, vlibc_vec3d b);
VLIBCDEF vlibc_vec3d vlibc_mul_vec3df(vlibc_vec3d a, float b);
VLIBCDEF vlibc_vec4d vlibc_mul_vec4d(vlibc_vec4d a, vlibc_vec4d b);
VLIBCDEF vlibc_vec4d vlibc_mul_vec4df(vlibc_vec4d a, float b);
VLIBCDEF vlibc_vec2d vlibc_add_vec2d(vlibc_vec2d a, vlibc_vec2d b);
VLIBCDEF vlibc_vec2d vlibc_add_vec2df(vlibc_vec2d a, float b);
VLIBCDEF vlibc_vec3d vlibc_add_vec3d(vlibc_vec3d a, vlibc_vec3d b);
VLIBCDEF vlibc_vec3d vlibc_add_vec3df(vlibc_vec3d a, float b);
VLIBCDEF vlibc_vec4d vlibc_add_vec4d(vlibc_vec4d a, vlibc_vec4d b);
VLIBCDEF vlibc_vec4d vlibc_add_vec4df(vlibc_vec4d a, float b);
VLIBCDEF vlibc_vec2d vlibc_div_vec2d(vlibc_vec2d a, vlibc_vec2d b);
VLIBCDEF vlibc_vec2d vlibc_div_vec2df(vlibc_vec2d a, float b);
VLIBCDEF vlibc_vec3d vlibc_div_vec3d(vlibc_vec3d a, vlibc_vec3d b);
VLIBCDEF vlibc_vec3d vlibc_div_vec3df(vlibc_vec3d a, float b);
VLIBCDEF vlibc_vec4d vlibc_div_vec4d(vlibc_vec4d a, vlibc_vec4d b);
VLIBCDEF vlibc_vec4d vlibc_div_vec4df(vlibc_vec4d a, float b);
VLIBCDEF vlibc_vec2d vlibc_sub_vec2d(vlibc_vec2d a, vlibc_vec2d b);
VLIBCDEF vlibc_vec2d vlibc_sub_vec2df(vlibc_vec2d a, float b);
VLIBCDEF vlibc_vec3d vlibc_sub_vec3d(vlibc_vec3d a, vlibc_vec3d b);
VLIBCDEF vlibc_vec3d vlibc_sub_vec3df(vlibc_vec3d a, float b);
VLIBCDEF vlibc_vec4d vlibc_sub_vec4d(vlibc_vec4d a, vlibc_vec4d b);
VLIBCDEF vlibc_vec4d vlibc_sub_vec4df(vlibc_vec4d a, float b);
VLIBCDEF vlibc_vec2d vlibc_div_fvec2d(float b, vlibc_vec2d a);
VLIBCDEF vlibc_vec3d vlibc_div_fvec3d(float b, vlibc_vec3d a);
VLIBCDEF vlibc_vec4d vlibc_div_fvec4d(float b, vlibc_vec4d a);
VLIBCDEF vlibc_vec2d vlibc_sub_fvec2d(float b, vlibc_vec2d a);
VLIBCDEF vlibc_vec3d vlibc_sub_fvec3d(float b, vlibc_vec3d a);
VLIBCDEF vlibc_vec4d vlibc_sub_fvec4d(float b, vlibc_vec4d a);
VLIBCDEF bool vlibc_vec2d_equal(vlibc_vec2d a, vlibc_vec2d b);
VLIBCDEF bool vlibc_vec3d_equal(vlibc_vec3d a, vlibc_vec3d b);
VLIBCDEF bool vlibc_vec4d_equal(vlibc_vec4d a, vlibc_vec4d b);
VLIBCDEF bool vlibc_rgba_equal(vlibc_rgba a, vlibc_rgba b);
VLIBCDEF bool vlibc_vertex_equal(vlibc_vertex a, vlibc_vertex b);
/*matrix functions*/
VLIBCDEF vlibc_mat4_t vlibc_mat4(float m00, float m10, float m20, float m30, float m01, float m11, float m21, float m31, float m02, float m12, float m22, float m32, float m03, float m13, float m23, float m33);
VLIBCDEF vlibc_mat4_t vlibc_mat4_identity();
VLIBCDEF vlibc_mat4_t vlibc_mat4_translation(vlibc_vec3d offset);
VLIBCDEF vlibc_mat4_t vlibc_mat4_scaling(vlibc_vec3d scale);
VLIBCDEF vlibc_mat4_t vlibc_mat4_rotation_x(float angle_in_rad);
VLIBCDEF vlibc_mat4_t vlibc_mat4_rotation_y(float angle_in_rad);
VLIBCDEF vlibc_mat4_t vlibc_mat4_rotation_z(float angle_in_rad);
VLIBCDEF vlibc_mat4_t vlibc_mat4_rotation(float angle_in_rad, vlibc_vec3d axis);
VLIBCDEF vlibc_mat4_t vlibc_mat4_ortho(float left, float right, float bottom, float top, float back, float front);
VLIBCDEF vlibc_mat4_t vlibc_mat4_perspective(float vertical_field_of_view_in_deg, float aspect_ratio, float near_view_distance, float far_view_distance);
VLIBCDEF vlibc_mat4_t vlibc_mat4_look_at(vlibc_vec3d from, vlibc_vec3d to, vlibc_vec3d up);
VLIBCDEF vlibc_mat4_t vlibc_mat4_transpose(vlibc_mat4_t matrix);
VLIBCDEF vlibc_mat4_t vlibc_mat4_mul(vlibc_mat4_t a, vlibc_mat4_t b);
VLIBCDEF vlibc_mat4_t vlibc_mat4_invert_affine(vlibc_mat4_t matrix);
VLIBCDEF vlibc_vec3d vlibc_mat4_mul_pos(vlibc_mat4_t matrix, vlibc_vec3d position);
VLIBCDEF vlibc_vec3d vlibc_mat4_mul_dir(vlibc_mat4_t matrix, vlibc_vec3d direction);
/*color convertion*/
VLIBCDEF vlibc_rgba vlibc_f_rgba_to_rgba(vlibc_f_rgba rgba);
VLIBCDEF vlibc_uint32_t vlibc_rgba_to_hex(vlibc_rgba c);
VLIBCDEF vlibc_rgba vlibc_hex_to_rgba(vlibc_uint32_t c);
/*shader functions*/
VLIBCDEF vlibc_shader_data_t vlibc_create_shader_data(float *shader_data, vlibc_size_t shader_data_size, vlibc_f_rgba *passthrough_data);
VLIBCDEF vlibc_fragment_shader_t vlibc_create_fragment_shader(vlibc_uint32_t (*func)(vlibc_vec2d, vlibc_rgba, vlibc_shader_data_t*));
VLIBCDEF vlibc_vec2d vlibc_calc_uv(vlibc_vec2d frag_pos, vlibc_vec2d resolution);
VLIBCDEF int vlibc_shader_data_parse_int(float *shader_data, int index);
VLIBCDEF vlibc_vec2d vlibc_shader_data_parse_vec2d(float *shader_data, int index);
VLIBCDEF vlibc_vec3d vlibc_shader_data_parse_vec3d(float *shader_data, int index);
VLIBCDEF vlibc_vec4d vlibc_shader_data_parse_vec4d(float *shader_data, int index);
VLIBCDEF vlibc_rgba vlibc_shader_data_parse_rgba(float *shader_data, int index);
VLIBCDEF vlibc_vertex vlibc_shader_data_parse_vertex(float *shader_data, int index);
/*gets average of red, green, blue and alpha and multiplies it by phase*/
VLIBCDEF vlibc_rgba vlibc_mix_colors(vlibc_rgba c1, vlibc_rgba c2, float phase);
/*puts a pixel on position*/
VLIBCDEF void vlibc_put_pixel(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_vec2d pos);
/*gets a pixel on position*/
VLIBCDEF vlibc_uint32_t vlibc_get_pixel(vlibc_canvas *vlibcc, vlibc_vec2d pos, int inversion);
/*fills entire canvas with one color*/
VLIBCDEF void vlibc_fill(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_fragment_shader_t *shader, vlibc_shader_data_t *shader_data);
/*creates rect without filling it*/
VLIBCDEF void vlibc_rect(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_vec2d pos1, vlibc_vec2d pos2, vlibc_fragment_shader_t *shader, vlibc_shader_data_t *shader_data);
/*creates filled rect*/
VLIBCDEF void vlibc_filled_rect(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_vec2d position, vlibc_vertex *vertices, vlibc_fragment_shader_t *shader, vlibc_shader_data_t *shader_data);
/*creates row*/
VLIBCDEF void vlibc_row(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_vec2d pos, int width, vlibc_fragment_shader_t *shader, vlibc_shader_data_t *shader_data);
/*creates column*/
VLIBCDEF void vlibc_column(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_vec2d pos, int height, vlibc_fragment_shader_t *shader, vlibc_shader_data_t *shader_data);
/*creates line*/
VLIBCDEF void vlibc_line(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_vec2d start, vlibc_vec2d end, vlibc_fragment_shader_t *shader, vlibc_shader_data_t *shader_data);
/*creates circle*/
VLIBCDEF void vlibc_circle(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_vec2d pos, int r, vlibc_fragment_shader_t *shader, vlibc_shader_data_t *shader_data);
/*creates filled circle*/
VLIBCDEF void vlibc_filled_circle(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_vec2d pos, int r, vlibc_fragment_shader_t *shader, vlibc_shader_data_t *shader_data);
/*creates ellipse*/
VLIBCDEF void vlibc_ellipse(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_vec2d pos, int rx, int ry, vlibc_fragment_shader_t *shader, vlibc_shader_data_t *shader_data);
/*creates filled ellipse*/
VLIBCDEF void vlibc_filled_ellipse(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_vec2d pos, int rx, int ry, vlibc_fragment_shader_t *shader, vlibc_shader_data_t *shader_data);
/*creates triangle*/
VLIBCDEF void vlibc_triangle(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_vec2d pos, vlibc_vec2d p1, vlibc_vec2d p2, vlibc_vec2d p3, vlibc_fragment_shader_t *shader, vlibc_shader_data_t *shader_data);
/*creates filled triangle*/
VLIBCDEF void vlibc_filled_triangle(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_vec2d pos, vlibc_vertex *vertices, vlibc_fragment_shader_t *shader, vlibc_shader_data_t *shader_data);
/*creates figure with filled triangles*/
VLIBCDEF void vlibc_filled_figure(vlibc_canvas* vlibcc, vlibc_rgba color, vlibc_vec2d pos, vlibc_vertex *vertices, int num_of_vertices, vlibc_fragment_shader_t *shader, vlibc_shader_data_t *shader_data);
#endif
#ifdef __VLIBC_IMPL__
int vlibc_swap(int *f, int *s) {
if (f == vlibc_nullptr && s == vlibc_nullptr) return -1;
int t = 0;
t = *f;
*f = *s;
*s = t;
return t;
}
float vlibc_swap_float(float *f, float *s) {
if (f == vlibc_nullptr && s == vlibc_nullptr) return -1;
float t = 0;
t = *f;
*f = *s;
*s = t;
return t;
}
float vlibc_cross_product(vlibc_vec2d p1, vlibc_vec2d p2) {
return p1.x * p2.y - p1.y * p2.x;
}
vlibc_rgba vlibc_f_rgba_to_rgba(vlibc_f_rgba rgba) {
return (vlibc_rgba) {
(vlibc_uint8_t) vlibc_clamp(rgba.r, 0, 255), (vlibc_uint8_t) vlibc_clamp(rgba.g, 0, 255), (vlibc_uint8_t) vlibc_clamp(rgba.b, 0, 255), (vlibc_uint8_t) vlibc_clamp(rgba.a, 0, 255)
};
}
vlibc_uint32_t vlibc_rgba_to_hex(vlibc_rgba c) {
return (vlibc_uint32_t)( (vlibc_uint8_t) c.r & 0xFF | ( (vlibc_uint8_t) c.g &0xFF) << 8 | ( (vlibc_uint8_t) c.b &0xFF) << 16 | ( (vlibc_uint8_t) c.a &0xFF) << 24 );
}
vlibc_rgba vlibc_hex_to_rgba(vlibc_uint32_t c) {
return *(vlibc_rgba*)&c;
}
float vlibc_lerp(float a, float b, float t) {
return a * (1.0 - t) + (b * t);
}
float vlibc_linearize_depth(float d, float near, float far) {
return near * far / (far + d * (near - far));
}
float vlibc_fract(float f) {
return f-(long)f;
}
float vlibc_clamp(float x, float a, float b) {
return VLIBC_MAX(a, VLIBC_MIN(b, x));
}
float vlibc_saturate(float x) {
return VLIBC_MAX(0, VLIBC_MIN(1, x));
}
float vlibc_smoothstep(float a, float b, float x) {
float t = vlibc_saturate((x - a)/(b - a));
return t*t*(3.0 - (2.0*t));
}
int vlibc_step(float x, float num) {
return x > num ? 0 : 1;
}
double vlibc_pow(double a, double b) {
double c = 1;
for (int i=0; i<b; i++)
c *= a;
return c;
}
double vlibc_fact(double x) {
double ret = 1;
for (int i=1; i<=x; i++)
ret *= i;
return ret;
}
/*i know this is super weird but it very fast*/
double vlibc_sin(double x) {
x *= 0.63661977236758134308;
int sign = x < 0.0;
x = sign ? -x : x;
int xf = (int)x;
x -= xf;
if ((xf & 1) == 1)
x = 1 - x;
int per = (xf >> 1) & 1;
double y;
if (x <= 0.5)
{
double xx = x * x;
y = x * (1.5707963267948965822 + xx * (-0.6459640975062407217 +
xx * (0.07969262624592800593 + xx * (-0.0046817541307639977752 +
xx * (0.00016044114022967599853 + xx * (-3.5986097146969802712e-6 +
5.629793865626169033e-8 * xx))))));
}
else
{
x = 1.0 - x;
double xx = x * x;
y = 1.0 - xx * (1.2337005501361513498 + xx * (-0.25366950789986513871 +
xx * (0.020863480734953519901 + xx * (-0.0009192599500952791151 +
xx * (0.000025200135454917479526 - 4.6552987291490935821e-7 * xx)))));
}
return sign ^ per ? -y : y;
}
double vlibc_cos(double x) {
if (x < 0) {
int q = -x / VLIBC_DOUBLE_PI;
q += 1;
double y = q * VLIBC_DOUBLE_PI;
x = -(x - y);
}
if (x >= VLIBC_DOUBLE_PI) {
int q = x / VLIBC_DOUBLE_PI;
double y = q * VLIBC_DOUBLE_PI;
x = x - y;
}
int s = 1;
if (x >= VLIBC_PI) {
s = -1;
x -= VLIBC_PI;
}
if (x > VLIBC_HALF_PI) {
x = VLIBC_PI - x;
s = -s;
}
double z = x * x;
double r = z * (z * (VLIBC_SIN_CURVE_A - VLIBC_SIN_CURVE_B * z) - 0.5) + 1.0;
if (r > 1.0) r = r - 2.0;
if (s > 0) return r;
else return -r;
}
double vlibc_tan(double x) {
return vlibc_sin(x)/vlibc_cos(x);
}
float vlibc_rsqrt(float x) {
unsigned int i = *(unsigned int*) &x;
i += 127 << 23;
i >>= 1;
return 1/(*(float*) &i);
}
float vlibc_sqrt(float x) {
return 1/vlibc_rsqrt(x);
}
bool vlibc_in_bounds(vlibc_canvas *vlibcc, vlibc_vec2d p) {
if (p.x > vlibcc->size.x || p.y > vlibcc->size.y || p.x < 0 || p.y < 0) return false;
return true;
}
vlibc_vec3d vlibc_vec3d_cross(vlibc_vec3d a, vlibc_vec3d b) {
return (vlibc_vec3d) {
.x = a.y * b.z - a.z * b.y,
.y = a.z * b.x - a.x * b.z,
.z = a.x * b.y - a.y * b.x
};
}
vlibc_vec3d vlibc_vec3d_proj(vlibc_vec3d v, vlibc_vec3d onto) {
return vlibc_mul_vec3df(onto, vlibc_vec3d_dot(v, onto) / vlibc_vec3d_dot(onto, onto));
}
float vlibc_vec2d_dot(vlibc_vec2d a, vlibc_vec2d b) {
return a.x*b.x + a.y*b.y;
}
float vlibc_vec3d_dot(vlibc_vec3d a, vlibc_vec3d b) {
return a.x*b.x + a.y*b.y + a.z*b.z;
}
float vlibc_vec4d_dot(vlibc_vec4d a, vlibc_vec4d b) {
return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;
}
vlibc_vec2d vlibc_vec2d_normalize(vlibc_vec2d v) {
return (vlibc_vec2d) {
.x = vlibc_rsqrt(vlibc_vec2d_dot(v,v)) * v.x,
.y = vlibc_rsqrt(vlibc_vec2d_dot(v,v)) * v.y
};
}
vlibc_vec3d vlibc_vec3d_normalize(vlibc_vec3d v) {
return (vlibc_vec3d) {
.x = vlibc_rsqrt(vlibc_vec3d_dot(v,v)) * v.x,
.y = vlibc_rsqrt(vlibc_vec3d_dot(v,v)) * v.y,
.z = vlibc_rsqrt(vlibc_vec3d_dot(v,v)) * v.z
};
}
vlibc_vec4d vlibc_vec4d_normalize(vlibc_vec4d v) {
return (vlibc_vec4d) {
.x = vlibc_rsqrt(vlibc_vec4d_dot(v,v)) * v.x,
.y = vlibc_rsqrt(vlibc_vec4d_dot(v,v)) * v.y,
.z = vlibc_rsqrt(vlibc_vec4d_dot(v,v)) * v.z,
.w = vlibc_rsqrt(vlibc_vec4d_dot(v,v)) * v.w
};
}
float vlibc_vec2d_length(vlibc_vec2d p) {
return vlibc_sqrt(p.x*p.x + p.y*p.y);
}
float vlibc_vec3d_length(vlibc_vec3d p) {
return vlibc_sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
}
float vlibc_vec4d_length(vlibc_vec4d p) {
return vlibc_sqrt(p.x*p.x + p.y*p.y + p.z*p.z + p.w*p.w);
}
vlibc_vec2d vlibc_vec2d_reflect(vlibc_vec2d i, vlibc_vec2d n) {
float d = vlibc_vec2d_dot(n, i);
return vlibc_sub_vec2d(i, vlibc_mul_vec2d((vlibc_vec2d){2.0, 2.0}, (vlibc_vec2d){d, d}));
}
vlibc_vec3d vlibc_vec3d_reflect(vlibc_vec3d i, vlibc_vec3d n) {
float d = vlibc_vec3d_dot(n, i);
return vlibc_sub_vec3d(i, vlibc_mul_vec3d((vlibc_vec3d){2.0, 2.0, 2.0}, (vlibc_vec3d){d, d, d}));
}
vlibc_vec4d vlibc_vec4d_reflect(vlibc_vec4d i, vlibc_vec4d n) {
float d = vlibc_vec4d_dot(n, i);
return vlibc_sub_vec4d(i, vlibc_mul_vec4d((vlibc_vec4d){2.0, 2.0, 2.0, 2.0}, (vlibc_vec4d){d, d, d, d}));
}
vlibc_vec2d vlibc_vec2d_negate(vlibc_vec2d x) {
return (vlibc_vec2d){-x.x, -x.y};
}
vlibc_vec3d vlibc_vec3d_negate(vlibc_vec3d x) {
return (vlibc_vec3d){-x.x, -x.y, -x.z};
}
vlibc_vec4d vlibc_vec4d_negate(vlibc_vec4d x) {
return (vlibc_vec4d){-x.x, -x.y, -x.z, -x.w};
}
vlibc_vec2d vlibc_mul_vec2d(vlibc_vec2d a, vlibc_vec2d b) {
return (vlibc_vec2d) {
.x = a.x*b.x,
.y = a.y*b.y
};
}
vlibc_vec2d vlibc_mul_vec2df(vlibc_vec2d a, float b) {
return (vlibc_vec2d) {
.x = a.x*b,
.y = a.y*b
};
}
vlibc_vec3d vlibc_mul_vec3d(vlibc_vec3d a, vlibc_vec3d b) {
return (vlibc_vec3d) {
.x = a.x*b.x,
.y = a.y*b.y,
.z = a.z*b.z
};
}
vlibc_vec3d vlibc_mul_vec3df(vlibc_vec3d a, float b) {
return (vlibc_vec3d) {
.x = a.x*b,
.y = a.y*b,
.z = a.z*b
};
}
vlibc_vec4d vlibc_mul_vec4d(vlibc_vec4d a, vlibc_vec4d b) {
return (vlibc_vec4d) {
.x = a.x*b.x,
.y = a.y*b.y,
.z = a.z*b.z,
.w = a.w*b.w
};
}
vlibc_vec4d vlibc_mul_vec4df(vlibc_vec4d a, float b) {
return (vlibc_vec4d) {
.x = a.x*b,
.y = a.y*b,
.z = a.z*b,
.w = a.w*b
};
}
vlibc_vec2d vlibc_add_vec2d(vlibc_vec2d a, vlibc_vec2d b) {
return (vlibc_vec2d) {
.x = a.x+b.x,
.y = a.y+b.y
};
}
vlibc_vec2d vlibc_add_vec2df(vlibc_vec2d a, float b) {
return (vlibc_vec2d) {
.x = a.x+b,
.y = a.y+b
};
}
vlibc_vec3d vlibc_add_vec3d(vlibc_vec3d a, vlibc_vec3d b) {
return (vlibc_vec3d) {
.x = a.x+b.x,
.y = a.y+b.y,
.z = a.z+b.z
};
}
vlibc_vec3d vlibc_add_vec3df(vlibc_vec3d a, float b) {
return (vlibc_vec3d) {
.x = a.x+b,
.y = a.y+b,
.z = a.z+b
};
}
vlibc_vec4d vlibc_add_vec4d(vlibc_vec4d a, vlibc_vec4d b) {
return (vlibc_vec4d) {
.x = a.x+b.x,
.y = a.y+b.y,
.z = a.z+b.z,
.w = a.w+b.w
};
}
vlibc_vec4d vlibc_add_vec4df(vlibc_vec4d a, float b) {
return (vlibc_vec4d) {
.x = a.x+b,
.y = a.y+b,
.z = a.z+b,
.w = a.w+b
};
}
vlibc_vec2d vlibc_div_vec2d(vlibc_vec2d a, vlibc_vec2d b) {
return (vlibc_vec2d) {
.x = a.x/b.x,
.y = a.y/b.y
};
}
vlibc_vec2d vlibc_div_vec2df(vlibc_vec2d a, float b) {
return (vlibc_vec2d) {
.x = a.x/b,
.y = a.y/b
};
}
vlibc_vec3d vlibc_div_vec3d(vlibc_vec3d a, vlibc_vec3d b) {
return (vlibc_vec3d) {
.x = a.x/b.x,
.y = a.y/b.y,
.z = a.z/b.z
};
}
vlibc_vec3d vlibc_div_vec3df(vlibc_vec3d a, float b) {
return (vlibc_vec3d) {
.x = a.x/b,
.y = a.y/b,
.z = a.z/b
};
}
vlibc_vec4d vlibc_div_vec4d(vlibc_vec4d a, vlibc_vec4d b) {
return (vlibc_vec4d) {
.x = a.x/b.x,
.y = a.y/b.y,
.z = a.z/b.z,
.w = a.w/b.w
};
}
vlibc_vec4d vlibc_div_vec4df(vlibc_vec4d a, float b) {
return (vlibc_vec4d) {
.x = a.x/b,
.y = a.y/b,
.z = a.z/b,
.w = a.w/b
};
}
vlibc_vec2d vlibc_sub_vec2d(vlibc_vec2d a, vlibc_vec2d b) {
return (vlibc_vec2d) {
.x = a.x-b.x,
.y = a.y-b.y
};
}
vlibc_vec2d vlibc_sub_vec2df(vlibc_vec2d a, float b) {
return (vlibc_vec2d) {
.x = a.x-b,
.y = a.y-b
};
}
vlibc_vec3d vlibc_sub_vec3d(vlibc_vec3d a, vlibc_vec3d b) {
return (vlibc_vec3d) {
.x = a.x-b.x,
.y = a.y-b.y,
.z = a.z-b.z
};
}
vlibc_vec3d vlibc_sub_vec3df(vlibc_vec3d a, float b) {
return (vlibc_vec3d) {
.x = a.x-b,
.y = a.y-b,
.z = a.z-b
};
}
vlibc_vec4d vlibc_sub_vec4d(vlibc_vec4d a, vlibc_vec4d b) {
return (vlibc_vec4d) {
.x = a.x-b.x,
.y = a.y-b.y,
.z = a.z-b.z,
.w = a.w-b.w
};
}
vlibc_vec4d vlibc_sub_vec4df(vlibc_vec4d a, float b) {
return (vlibc_vec4d) {
.x = a.x-b,
.y = a.y-b,
.z = a.z-b,
.w = a.w-b
};
}
vlibc_vec2d vlibc_div_fvec2d(float b, vlibc_vec2d a) {
return (vlibc_vec2d) {
.x = b/a.x,
.y = b/a.y
};
}
vlibc_vec3d vlibc_div_fvec3d(float b, vlibc_vec3d a) {
return (vlibc_vec3d) {
.x = b/a.x,
.y = b/a.y,
.z = b/a.z
};
}
vlibc_vec4d vlibc_div_fvec4d(float b, vlibc_vec4d a) {
return (vlibc_vec4d) {
.x = b/a.x,
.y = b/a.y,
.z = b/a.z,
.w = b/a.w
};
}
vlibc_vec2d vlibc_sub_fvec2d(float b, vlibc_vec2d a) {
return (vlibc_vec2d) {
.x = b-a.x,
.y = b-a.y
};
}
vlibc_vec3d vlibc_sub_fvec3d(float b, vlibc_vec3d a) {
return (vlibc_vec3d) {
.x = b-a.x,
.y = b-a.y,
.z = b-a.z
};
}
vlibc_vec4d vlibc_sub_fvec4d(float b, vlibc_vec4d a) {
return (vlibc_vec4d) {
.x = b-a.x,
.y = b-a.y,
.z = b-a.z,
.w = b-a.w
};
}
bool vlibc_vec2d_equal(vlibc_vec2d a, vlibc_vec2d b) {
return a.x == b.x && a.y == b.y;
}
bool vlibc_vec3d_equal(vlibc_vec3d a, vlibc_vec3d b) {
return a.x == b.x && a.y == b.y && a.z == b.z;
}
bool vlibc_vec4d_equal(vlibc_vec4d a, vlibc_vec4d b) {
return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
}
bool vlibc_rgba_equal(vlibc_rgba a, vlibc_rgba b) {
return a.r == b.r && a.g == b.g && a.b == b.b && a.a == b.a;
}
bool vlibc_vertex_equal(vlibc_vertex a, vlibc_vertex b) {
if (a.col_count != b.col_count) return false;
if (a.pos.x != b.pos.x || a.pos.y != b.pos.y) return false;
return true;
}
vlibc_mat4_t vlibc_mat4(float m00, float m10, float m20, float m30, float m01, float m11, float m21, float m31, float m02, float m12, float m22, float m32, float m03, float m13, float m23, float m33) {
return (vlibc_mat4_t) {
.m[0][0] = m00, .m[1][0] = m10, .m[2][0] = m20, .m[3][0] = m30,
.m[0][1] = m01, .m[1][1] = m11, .m[2][1] = m21, .m[3][1] = m31,
.m[0][2] = m02, .m[1][2] = m12, .m[2][2] = m22, .m[3][2] = m32,
.m[0][3] = m03, .m[1][3] = m13, .m[2][3] = m23, .m[3][3] = m33
};
}
vlibc_mat4_t vlibc_mat4_identity() {
return vlibc_mat4(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
}
vlibc_mat4_t vlibc_mat4_translation(vlibc_vec3d offset) {
return vlibc_mat4(
1, 0, 0, offset.x,
0, 1, 0, offset.y,
0, 0, 1, offset.z,
0, 0, 0, 1
);
}
vlibc_mat4_t vlibc_mat4_scaling(vlibc_vec3d scale) {
float x = scale.x;
float y = scale.y;
float z = scale.z;
return vlibc_mat4(
x, 0, 0, 0,
0, y, 0, 0,
0, 0, z, 0,
0, 0, 0, 1
);
}
vlibc_mat4_t vlibc_mat4_rotation_x(float angle_in_rad) {
float s = vlibc_sin(angle_in_rad);
float c = vlibc_cos(angle_in_rad);
return vlibc_mat4(
1, 0, 0, 0,
0, c, -s, 0,
0, s, c, 0,
0, 0, 0, 1
);
}
vlibc_mat4_t vlibc_mat4_rotation_y(float angle_in_rad) {
float s = vlibc_sin(angle_in_rad);
float c = vlibc_cos(angle_in_rad);
return vlibc_mat4(
c, 0, s, 0,
0, 1, 0, 0,
-s, 0, c, 0,
0, 0, 0, 1
);
}
vlibc_mat4_t vlibc_mat4_rotation_z(float angle_in_rad) {
float s = vlibc_sin(angle_in_rad);
float c = vlibc_cos(angle_in_rad);
return vlibc_mat4(
c, -s, 0, 0,
s, c, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
);
}
vlibc_mat4_t vlibc_mat4_rotation(float angle_in_rad, vlibc_vec3d axis) {
vlibc_vec3d normalized_axis = vlibc_vec3d_normalize(axis);
float x = normalized_axis.x, y = normalized_axis.y, z = normalized_axis.z;
float c = vlibc_cos(angle_in_rad), s = vlibc_sin(angle_in_rad);
return vlibc_mat4(
c + x*x*(1-c), x*y*(1-c) - z*s, x*z*(1-c) + y*s, 0,
y*x*(1-c) + z*s, c + y*y*(1-c), y*z*(1-c) - x*s, 0,
z*x*(1-c) - y*s, z*y*(1-c) + x*s, c + z*z*(1-c), 0,
0, 0, 0, 1
);
}
vlibc_mat4_t vlibc_mat4_ortho(float left, float right, float bottom, float top, float back, float front) {
float l = left, r = right, b = bottom, t = top, n = front, f = back;
float tx = -(r + l) / (r - l);
float ty = -(t + b) / (t - b);
float tz = -(f + n) / (f - n);
return vlibc_mat4(
2 / (r - l), 0, 0, tx,
0, 2 / (t - b), 0, ty,
0, 0, 2 / (f - n), tz,
0, 0, 0, 1
);
}
vlibc_mat4_t vlibc_mat4_perspective(float vertical_field_of_view_in_deg, float aspect_ratio, float near_view_distance, float far_view_distance) {
float fovy_in_rad = vertical_field_of_view_in_deg / 180 * VLIBC_PI;
float f = 1.0f / vlibc_tan(fovy_in_rad / 2.0f);
float ar = aspect_ratio;
float nd = near_view_distance, fd = far_view_distance;
return vlibc_mat4(
f / ar, 0, 0, 0,
0, f, 0, 0,
0, 0, (fd+nd)/(nd-fd), (2*fd*nd)/(nd-fd),
0, 0, -1, 0
);
}
vlibc_mat4_t vlibc_mat4_look_at(vlibc_vec3d from, vlibc_vec3d to, vlibc_vec3d up) {
vlibc_vec3d z = vlibc_mul_vec3df(vlibc_vec3d_normalize(vlibc_sub_vec3d(to, from)), -1);
vlibc_vec3d x = vlibc_vec3d_normalize(vlibc_vec3d_cross(up, z));
vlibc_vec3d y = vlibc_vec3d_cross(z, x);
return vlibc_mat4(
x.x, x.y, x.z, -vlibc_vec3d_dot(from, x),
y.x, y.y, y.z, -vlibc_vec3d_dot(from, y),
z.x, z.y, z.z, -vlibc_vec3d_dot(from, z),
0, 0, 0, 1
);
}
vlibc_mat4_t vlibc_mat4_transpose(vlibc_mat4_t matrix) {
return vlibc_mat4(
matrix.m00, matrix.m01, matrix.m02, matrix.m03,
matrix.m10, matrix.m11, matrix.m12, matrix.m13,
matrix.m20, matrix.m21, matrix.m22, matrix.m23,
matrix.m30, matrix.m31, matrix.m32, matrix.m33
);
}
vlibc_mat4_t vlibc_mat4_mul(vlibc_mat4_t a, vlibc_mat4_t b) {
vlibc_mat4_t result;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
float sum = 0;
for(int k = 0; k < 4; k++) {
sum += a.m[k][j] * b.m[i][k];
}
result.m[i][j] = sum;
}
}
return result;
}
vlibc_mat4_t vlibc_mat4_invert_affine(vlibc_mat4_t matrix) {
float m00 = matrix.m00, m10 = matrix.m10, m20 = matrix.m20, m30 = matrix.m30;
float m01 = matrix.m01, m11 = matrix.m11, m21 = matrix.m21, m31 = matrix.m31;
float m02 = matrix.m02, m12 = matrix.m12, m22 = matrix.m22, m32 = matrix.m32;
float c00 = m11*m22 - m12*m21, c10 = -(m01*m22 - m02*m21), c20 = m01*m12 - m02*m11;
float c01 = -(m10*m22 - m12*m20), c11 = m00*m22 - m02*m20, c21 = -(m00*m12 - m02*m10);
float c02 = m10*m21 - m11*m20, c12 = -(m00*m21 - m01*m20), c22 = m00*m11 - m01*m10;
float det = m00*c00 + m10*c10 + m20 * c20;
if (VLIBC_ABS(float, det) < 0.00001)
return vlibc_mat4_identity();
float i00 = c00 / det, i10 = c01 / det, i20 = c02 / det;
float i01 = c10 / det, i11 = c11 / det, i21 = c12 / det;
float i02 = c20 / det, i12 = c21 / det, i22 = c22 / det;
return vlibc_mat4(
i00, i10, i20, -(i00*m30 + i10*m31 + i20*m32),
i01, i11, i21, -(i01*m30 + i11*m31 + i21*m32),
i02, i12, i22, -(i02*m30 + i12*m31 + i22*m32),
0, 0, 0, 1
);
}
vlibc_vec3d vlibc_mat4_mul_pos(vlibc_mat4_t matrix, vlibc_vec3d position) {
vlibc_vec3d result = (vlibc_vec3d) {
matrix.m00 * position.x + matrix.m10 * position.y + matrix.m20 * position.z + matrix.m30,
matrix.m01 * position.x + matrix.m11 * position.y + matrix.m21 * position.z + matrix.m31,
matrix.m02 * position.x + matrix.m12 * position.y + matrix.m22 * position.z + matrix.m32
};
float w = matrix.m03 * position.x + matrix.m13 * position.y + matrix.m23 * position.z + matrix.m33;
if (w != 0 && w != 1)
return (vlibc_vec3d){result.x / w, result.y / w, result.z / w};
return result;
}
vlibc_vec3d vlibc_mat4_mul_dir(vlibc_mat4_t matrix, vlibc_vec3d direction) {
vlibc_vec3d result = (vlibc_vec3d) {
matrix.m00 * direction.x + matrix.m10 * direction.y + matrix.m20 * direction.z,
matrix.m01 * direction.x + matrix.m11 * direction.y + matrix.m21 * direction.z,
matrix.m02 * direction.x + matrix.m12 * direction.y + matrix.m22 * direction.z
};
float w = matrix.m03 * direction.x + matrix.m13 * direction.y + matrix.m23 * direction.z;
if (w != 0 && w != 1)
return (vlibc_vec3d){result.x / w, result.y / w, result.z / w};
return result;
}
float vlibc_edge(vlibc_vec2d p1, vlibc_vec2d p2, vlibc_vec2d p) {
vlibc_vec2d a = p;
vlibc_vec2d b = p2;
a.x -= p1.x;
a.y -= p1.y;
b.x -= p1.x;
b.y -= p1.y;
return a.x * b.y - a.y * b.x;
}
vlibc_fragment_shader_t vlibc_create_fragment_shader(vlibc_uint32_t (*func)(vlibc_vec2d, vlibc_rgba, vlibc_shader_data_t*)) {
return (vlibc_fragment_shader_t) {
.func = func
};
}
vlibc_shader_data_t vlibc_create_shader_data(float *shader_data, vlibc_size_t shader_data_size, vlibc_f_rgba *passthrough_data) {
return (vlibc_shader_data_t) {
shader_data,
passthrough_data,
shader_data_size
};
}
vlibc_vec2d vlibc_calc_uv(vlibc_vec2d frag_pos, vlibc_vec2d resolution) {
vlibc_vec2d uv = (vlibc_vec2d) {
.x = frag_pos.x/resolution.x * 2,
.y = frag_pos.y/resolution.y * 2
};
uv.x -= 1;
uv.y -= 1;
uv.x *= resolution.x/resolution.y;
return uv;
}