forked from rswinkle/PortableGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portablegl.h
12039 lines (9832 loc) · 312 KB
/
portablegl.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
/*
PortableGL 0.93 MIT licensed software renderer that closely mirrors OpenGL 3.x
portablegl.com
robertwinkler.com
Do this:
#define PORTABLEGL_IMPLEMENTATION
before you include this file in *one* C or C++ file to create the implementation.
If you plan on using your own 3D vector/matrix library rather than crsw_math
that is built into PortableGL and your names are the standard glsl vec[2-4],
mat[3-4] etc., define MANGLE_TYPES too before including portablegl to
prefix all those builtin types with glinternal_ to avoid the clash.
You can check all the C++ examples and demos, I use my C++ rsw_math library.
// i.e. it should look like this:
#include ...
#include ...
#include ...
// if required
#define MANGLE_TYPES
#define PORTABLEGL_IMPLEMENTATION
#include "portablegl.h"
I use my CVector library for various types in PortableGL so you *can* #define
CVEC_ASSERT, CVEC_MEMMOVE, and (mutually inclusive) CVEC_MALLOC, CVEC_REALLOC,
and CVEC_FREE before the #include to avoid using the standard library
versions. However, currently, I use at least malloc, realloc, and memcpy in
PortableGL so doing so wouldn't actually avoid the standard library. Creating
equivalent PortableGL macros (that would automagically apply to any internally
used cvectors) is a TODO I suppose.
QUICK NOTES:
Primarily of interest to game/graphics developers and other people who
just want to play with the graphics pipeline and don't need peak
performance or the the entirety of OpenGL or Vulkan features.
RGBA32 is the only currently supported format for textures
Only GL_TEXTURE_MAG_FILTER is actually used internally but you can set the
MIN_FILTER for a texture.
8-bit per channel RGBA is the only supported format for the framebuffer
You can specify the order using the masks in init_glContext. Technically it'd be relatively
trivial to add support for other formats but for now we use a u32* to access the buffer.
Any PortableGL program has roughly this structure, with some things
possibly declared globally or passed around in function parameters
as needed:
#define WIDTH 640
#define HEIGHT 480
// shaders are functions matching thise prototypes
void smooth_vs(float* vs_output, void* vertex_attribs, Shader_Builtins* builtins, void* uniforms);
void smooth_fs(float* fs_input, Shader_Builtins* builtins, void* uniforms);
typedef struct My_Uniforms {
mat4 mvp_mat;
vec4 v_color;
} My_Uniforms;
u32* backbuf;
glContext the_context;
if (!init_glContext(&the_context, &backbuf, WIDTH, HEIGHT, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000)) {
puts("Failed to initialize glContext");
exit(0);
}
set_glContext(&the_context);
// interpolation is an array with an entry of SMOOTH, FLAT or
// NOPERSPECTIVE for each float being interpolated between the
// vertex and fragment shaders
// the last parameter is whether the fragment shader writes to
// gl_FragDepth or discard, but it's not currently used. In the future I may
// have a macro that enables early depth testing *if* that parameter is
// false for a minor performance boost but canonicaly depth test happens
// after frag shader (and scissoring)
GLenum interpolation[4] = { SMOOTH, SMOOTH, SMOOTH, SMOOTH };
GLuint myshader = pglCreateProgram(smooth_vs, smooth_fs, 4, interpolation, GL_FALSE);
glUseProgram(myshader);
My_Uniform the_uniforms;
pglSetUniform(&the_uniforms);
the_uniforms.v_color = Red; // not actually used, using per vert color
memcpy(the_uniforms.mvp_mat, identity, sizeof(mat4));
// Your standard OpenGL buffer setup etc. here
// Like the compatibility profile, we allow/enable a default
// VAO. We also have a default shader program for the same reason,
// something to fill index 0.
// see implementation of init_glContext for details
while (1) {
// standard glDraw calls, switching shaders etc.
// use backbuf however you want, whether that's blitting
// it to some framebuffer in your GUI system, or even writing
// it out to disk with something like stb_image_write.
}
free_glContext(&the_context);
// compare with equivalent glsl below
void smooth_vs(float* vs_output, void* vertex_attribs, Shader_Builtins* builtins, void* uniforms)
{
vec4* v_attribs = vertex_attribs;
((vec4*)vs_output)[0] = v_attribs[1]; //color
builtins->gl_Position = mult_mat4_vec4(*((mat4*)uniforms), v_attribs[0]);
}
void smooth_fs(float* fs_input, Shader_Builtins* builtins, void* uniforms)
{
builtins->gl_FragColor = ((vec4*)fs_input)[0];
}
// note smooth is the default so this is the same as smooth out vec4 vary_color
// https://www.khronos.org/opengl/wiki/Type_Qualifier_(GLSL)#Interpolation_qualifiers
uniform mvp_mat
layout (location = 0) in vec4 in_vertex;
layout (location = 1) in vec4 in_color;
out vec4 vary_color;
void main(void)
{
vary_color = in_color;
gl_Position = mvp_mat * in_vertex;
}
in vec4 vary_color;
out vec4 frag_color;
void main(void)
{
frag_color = vary_color;
}
That's basically it. There are some other non-standard features like
pglSetInterp that lets you change the interpolation of a shader
whenever you want. In real OpenGL you'd have to have 2 (or more) separate
but almost identical shaders to do that.
There are also these predefined maximums which, considering the performance
limitations of PortableGL, are probably more than enough. MAX_DRAW_BUFFERS
isn't used since they're not currently supported anyway.
#define MAX_VERTICES 500000
#define GL_MAX_VERTEX_ATTRIBS 16
#define GL_MAX_VERTEX_OUTPUT_COMPONENTS 64
#define GL_MAX_DRAW_BUFFERS 8
MIT License
Copyright (c) 2011-2021 Robert Winkler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
Clipping code copyright (c) Fabrice Bellard from TinyGL
https://bellard.org/TinyGL/
(C) 1997-1998 Fabrice Bellard
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product and its documentation
*is* required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
If you redistribute modified sources, I would appreciate that you
include in the files history information documenting your changes.
*/
#ifdef MANGLE_TYPES
#define vec2 glinternal_vec2
#define vec3 glinternal_vec3
#define vec4 glinternal_vec4
#define dvec2 glinternal_dvec2
#define dvec3 glinternal_dvec3
#define dvec4 glinternal_dvec4
#define ivec2 glinternal_ivec2
#define ivec3 glinternal_ivec3
#define ivec4 glinternal_ivec4
#define uvec2 glinternal_uvec2
#define uvec3 glinternal_uvec3
#define uvec4 glinternal_uvec4
#define mat2 glinternal_mat2
#define mat3 glinternal_mat3
#define mat4 glinternal_mat4
#define Color glinternal_Color
#define Line glinternal_Line
#define Plane glinternal_Plane
#endif
#ifndef GL_H
#define GL_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef CRSW_MATH_H
#define CRSW_MATH_H
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define RM_PI (3.14159265358979323846)
#define RM_2PI (2.0 * RM_PI)
#define PI_DIV_180 (0.017453292519943296)
#define INV_PI_DIV_180 (57.2957795130823229)
#define DEG_TO_RAD(x) ((x)*PI_DIV_180)
#define RAD_TO_DEG(x) ((x)*INV_PI_DIV_180)
/* Hour angles */
#define HR_TO_DEG(x) ((x) * (1.0 / 15.0))
#define HR_TO_RAD(x) DEG_TO_RAD(HR_TO_DEG(x))
#define DEG_TO_HR(x) ((x) * 15.0)
#define RAD_TO_HR(x) DEG_TO_HR(RAD_TO_DEG(x))
// TODO rename RM_MAX? make proper inline functions?
#ifndef MAX
#define MAX(a, b) ((a) > (b)) ? (a) : (b)
#endif
#ifndef MIN
#define MIN(a, b) ((a) < (b)) ? (a) : (b)
#endif
#define MAP(X, A, B, C, D) (X-A)/(B-A) * (D-C) + C
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
inline float rsw_rand_float(float min, float max)
{
return (rand()/((float)RAND_MAX-1))*(max-min) + min;
}
typedef struct vec2
{
float x;
float y;
} vec2;
typedef struct vec3
{
float x;
float y;
float z;
} vec3;
typedef struct vec4
{
float x;
float y;
float z;
float w;
} vec4;
#define SET_VEC2(v, _x, _y) \
do {\
(v).x = _x;\
(v).y = _y;\
} while (0)
#define SET_VEC3(v, _x, _y, _z) \
do {\
(v).x = _x;\
(v).y = _y;\
(v).z = _z;\
} while (0)
#define SET_VEC4(v, _x, _y, _z, _w) \
do {\
(v).x = _x;\
(v).y = _y;\
(v).z = _z;\
(v).w = _w;\
} while (0)
inline vec2 make_vec2(float x, float y)
{
vec2 v = { x, y };
return v;
}
inline vec3 make_vec3(float x, float y, float z)
{
vec3 v = { x, y, z };
return v;
}
inline vec4 make_vec4(float x, float y, float z, float w)
{
vec4 v = { x, y, z, w };
return v;
}
inline vec2 negate_vec2(vec2 v)
{
vec2 r = { -v.x, -v.y };
return r;
}
inline vec3 negate_vec3(vec3 v)
{
vec3 r = { -v.x, -v.y, -v.z };
return r;
}
inline vec4 negate_vec4(vec4 v)
{
vec4 r = { -v.x, -v.y, -v.z, -v.w };
return r;
}
inline void fprint_vec2(FILE* f, vec2 v, const char* append)
{
fprintf(f, "(%f, %f)%s", v.x, v.y, append);
}
inline void fprint_vec3(FILE* f, vec3 v, const char* append)
{
fprintf(f, "(%f, %f, %f)%s", v.x, v.y, v.z, append);
}
inline void fprint_vec4(FILE* f, vec4 v, const char* append)
{
fprintf(f, "(%f, %f, %f, %f)%s", v.x, v.y, v.z, v.w, append);
}
inline void print_vec2(vec2 v, const char* append)
{
printf("(%f, %f)%s", v.x, v.y, append);
}
inline void print_vec3(vec3 v, const char* append)
{
printf("(%f, %f, %f)%s", v.x, v.y, v.z, append);
}
inline void print_vec4(vec4 v, const char* append)
{
printf("(%f, %f, %f, %f)%s", v.x, v.y, v.z, v.w, append);
}
inline int fread_vec2(FILE* f, vec2* v)
{
int tmp = fscanf(f, " (%f, %f)", &v->x, &v->y);
return (tmp == 2);
}
inline int fread_vec3(FILE* f, vec3* v)
{
int tmp = fscanf(f, " (%f, %f, %f)", &v->x, &v->y, &v->z);
return (tmp == 3);
}
inline int fread_vec4(FILE* f, vec4* v)
{
int tmp = fscanf(f, " (%f, %f, %f, %f)", &v->x, &v->y, &v->z, &v->w);
return (tmp == 4);
}
typedef struct dvec2
{
double x;
double y;
} dvec2;
typedef struct dvec3
{
double x;
double y;
double z;
} dvec3;
typedef struct dvec4
{
double x;
double y;
double z;
double w;
} dvec4;
inline void fprint_dvec2(FILE* f, dvec2 v, const char* append)
{
fprintf(f, "(%f, %f)%s", v.x, v.y, append);
}
inline void fprint_dvec3(FILE* f, dvec3 v, const char* append)
{
fprintf(f, "(%f, %f, %f)%s", v.x, v.y, v.z, append);
}
inline void fprint_dvec4(FILE* f, dvec4 v, const char* append)
{
fprintf(f, "(%f, %f, %f, %f)%s", v.x, v.y, v.z, v.w, append);
}
inline int fread_dvec2(FILE* f, dvec2* v)
{
int tmp = fscanf(f, " (%lf, %lf)", &v->x, &v->y);
return (tmp == 2);
}
inline int fread_dvec3(FILE* f, dvec3* v)
{
int tmp = fscanf(f, " (%lf, %lf, %lf)", &v->x, &v->y, &v->z);
return (tmp == 3);
}
inline int fread_dvec4(FILE* f, dvec4* v)
{
int tmp = fscanf(f, " (%lf, %lf, %lf, %lf)", &v->x, &v->y, &v->z, &v->w);
return (tmp == 4);
}
typedef struct ivec2
{
int x;
int y;
} ivec2;
typedef struct ivec3
{
int x;
int y;
int z;
} ivec3;
typedef struct ivec4
{
int x;
int y;
int z;
int w;
} ivec4;
inline ivec2 make_ivec2(int x, int y)
{
ivec2 v = { x, y };
return v;
}
inline ivec3 make_ivec3(int x, int y, int z)
{
ivec3 v = { x, y, z };
return v;
}
inline ivec4 make_ivec4(int x, int y, int z, int w)
{
ivec4 v = { x, y, z, w };
return v;
}
inline void fprint_ivec2(FILE* f, ivec2 v, const char* append)
{
fprintf(f, "(%d, %d)%s", v.x, v.y, append);
}
inline void fprint_ivec3(FILE* f, ivec3 v, const char* append)
{
fprintf(f, "(%d, %d, %d)%s", v.x, v.y, v.z, append);
}
inline void fprint_ivec4(FILE* f, ivec4 v, const char* append)
{
fprintf(f, "(%d, %d, %d, %d)%s", v.x, v.y, v.z, v.w, append);
}
inline int fread_ivec2(FILE* f, ivec2* v)
{
int tmp = fscanf(f, " (%d, %d)", &v->x, &v->y);
return (tmp == 2);
}
inline int fread_ivec3(FILE* f, ivec3* v)
{
int tmp = fscanf(f, " (%d, %d, %d)", &v->x, &v->y, &v->z);
return (tmp == 3);
}
inline int fread_ivec4(FILE* f, ivec4* v)
{
int tmp = fscanf(f, " (%d, %d, %d, %d)", &v->x, &v->y, &v->z, &v->w);
return (tmp == 4);
}
typedef struct uvec2
{
unsigned int x;
unsigned int y;
} uvec2;
typedef struct uvec3
{
unsigned int x;
unsigned int y;
unsigned int z;
} uvec3;
typedef struct uvec4
{
unsigned int x;
unsigned int y;
unsigned int z;
unsigned int w;
} uvec4;
inline void fprint_uvec2(FILE* f, uvec2 v, const char* append)
{
fprintf(f, "(%u, %u)%s", v.x, v.y, append);
}
inline void fprint_uvec3(FILE* f, uvec3 v, const char* append)
{
fprintf(f, "(%u, %u, %u)%s", v.x, v.y, v.z, append);
}
inline void fprint_uvec4(FILE* f, uvec4 v, const char* append)
{
fprintf(f, "(%u, %u, %u, %u)%s", v.x, v.y, v.z, v.w, append);
}
inline int fread_uvec2(FILE* f, uvec2* v)
{
int tmp = fscanf(f, " (%u, %u)", &v->x, &v->y);
return (tmp == 2);
}
inline int fread_uvec3(FILE* f, uvec3* v)
{
int tmp = fscanf(f, " (%u, %u, %u)", &v->x, &v->y, &v->z);
return (tmp == 3);
}
inline int fread_uvec4(FILE* f, uvec4* v)
{
int tmp = fscanf(f, " (%u, %u, %u, %u)", &v->x, &v->y, &v->z, &v->w);
return (tmp == 4);
}
inline float length_vec2(vec2 a)
{
return sqrt(a.x * a.x + a.y * a.y);
}
inline float length_vec3(vec3 a)
{
return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}
inline vec2 norm_vec2(vec2 a)
{
float l = length_vec2(a);
vec2 c = { a.x/l, a.y/l };
return c;
}
inline vec3 norm_vec3(vec3 a)
{
float l = length_vec3(a);
vec3 c = { a.x/l, a.y/l, a.z/l };
return c;
}
inline void normalize_vec2(vec2* a)
{
float l = length_vec2(*a);
a->x /= l;
a->y /= l;
}
inline void normalize_vec3(vec3* a)
{
float l = length_vec3(*a);
a->x /= l;
a->y /= l;
a->z /= l;
}
inline vec2 add_vec2s(vec2 a, vec2 b)
{
vec2 c = { a.x + b.x, a.y + b.y };
return c;
}
inline vec3 add_vec3s(vec3 a, vec3 b)
{
vec3 c = { a.x + b.x, a.y + b.y, a.z + b.z };
return c;
}
inline vec4 add_vec4s(vec4 a, vec4 b)
{
vec4 c = { a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w };
return c;
}
inline vec2 sub_vec2s(vec2 a, vec2 b)
{
vec2 c = { a.x - b.x, a.y - b.y };
return c;
}
inline vec3 sub_vec3s(vec3 a, vec3 b)
{
vec3 c = { a.x - b.x, a.y - b.y, a.z - b.z };
return c;
}
inline vec4 sub_vec4s(vec4 a, vec4 b)
{
vec4 c = { a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w };
return c;
}
inline vec2 mult_vec2s(vec2 a, vec2 b)
{
vec2 c = { a.x * b.x, a.y * b.y };
return c;
}
inline vec3 mult_vec3s(vec3 a, vec3 b)
{
vec3 c = { a.x * b.x, a.y * b.y, a.z * b.z };
return c;
}
inline vec4 mult_vec4s(vec4 a, vec4 b)
{
vec4 c = { a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w };
return c;
}
inline vec2 div_vec2s(vec2 a, vec2 b)
{
vec2 c = { a.x / b.x, a.y / b.y };
return c;
}
inline vec3 div_vec3s(vec3 a, vec3 b)
{
vec3 c = { a.x / b.x, a.y / b.y, a.z / b.z };
return c;
}
inline vec4 div_vec4s(vec4 a, vec4 b)
{
vec4 c = { a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w };
return c;
}
inline float dot_vec2s(vec2 a, vec2 b)
{
return a.x*b.x + a.y*b.y;
}
inline float dot_vec3s(vec3 a, vec3 b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
inline float dot_vec4s(vec4 a, vec4 b)
{
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}
inline vec2 scale_vec2(vec2 a, float s)
{
vec2 b = { a.x * s, a.y * s };
return b;
}
inline vec3 scale_vec3(vec3 a, float s)
{
vec3 b = { a.x * s, a.y * s, a.z * s };
return b;
}
inline vec4 scale_vec4(vec4 a, float s)
{
vec4 b = { a.x * s, a.y * s, a.z * s, a.w * s };
return b;
}
inline int equal_vec2s(vec2 a, vec2 b)
{
return (a.x == b.x && a.y == b.y);
}
inline int equal_vec3s(vec3 a, vec3 b)
{
return (a.x == b.x && a.y == b.y && a.z == b.z);
}
inline int equal_vec4s(vec4 a, vec4 b)
{
return (a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w);
}
inline int equal_epsilon_vec2s(vec2 a, vec2 b, float epsilon)
{
return (fabs(a.x-b.x) < epsilon && fabs(a.y - b.y) < epsilon);
}
inline int equal_epsilon_vec3s(vec3 a, vec3 b, float epsilon)
{
return (fabs(a.x-b.x) < epsilon && fabs(a.y - b.y) < epsilon &&
fabs(a.z - b.z) < epsilon);
}
inline int equal_epsilon_vec4s(vec4 a, vec4 b, float epsilon)
{
return (fabs(a.x-b.x) < epsilon && fabs(a.y - b.y) < epsilon &&
fabs(a.z - b.z) < epsilon && fabs(a.w - b.w) < epsilon);
}
inline vec2 vec4_to_vec2(vec4 a)
{
vec2 v = { a.x, a.y };
return v;
}
inline vec3 vec4_to_vec3(vec4 a)
{
vec3 v = { a.x, a.y, a.z };
return v;
}
inline vec2 vec4_to_vec2h(vec4 a)
{
vec2 v = { a.x/a.w, a.y/a.w };
return v;
}
inline vec3 vec4_to_vec3h(vec4 a)
{
vec3 v = { a.x/a.w, a.y/a.w, a.z/a.w };
return v;
}
inline vec3 cross_product(const vec3 u, const vec3 v)
{
vec3 result;
result.x = u.y*v.z - v.y*u.z;
result.y = -u.x*v.z + v.x*u.z;
result.z = u.x*v.y - v.x*u.y;
return result;
}
inline float angle_between_vec3(const vec3 u, const vec3 v)
{
return acos(dot_vec3s(u, v));
}
/* matrices **************/
typedef float mat2[4];
typedef float mat3[9];
typedef float mat4[16];
#define IDENTITY_MAT2() { 1, 0, 0, 1 }
#define IDENTITY_MAT3() { 1, 0, 0, 0, 1, 0, 0, 0, 1 }
#define IDENTITY_MAT4() { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
#define SET_IDENTITY_MAT2(m) \
do { \
m[1] = m[2] = 0; \
m[0] = m[3] = 1; \
} while (0)
#define SET_IDENTITY_MAT3(m) \
do { \
memset(m, 0, sizeof(float)*9); \
m[0] = m[4] = m[8] = 1; \
} while (0)
#define SET_IDENTITY_MAT4(m) \
do { \
memset(m, 0, sizeof(float)*16); \
m[0] = m[5] = m[10] = m[15] = 1; \
} while (0)
#ifndef ROW_MAJOR
inline vec2 x_mat2(mat2 m) { return make_vec2(m[0], m[2]); }
inline vec2 y_mat2(mat2 m) { return make_vec2(m[1], m[3]); }
inline vec2 c1_mat2(mat2 m) { return make_vec2(m[0], m[1]); }
inline vec2 c2_mat2(mat2 m) { return make_vec2(m[2], m[3]); }
inline void setc1_mat2(mat2 m, vec2 v) { m[0]=v.x, m[1]=v.y; }
inline void setc2_mat2(mat2 m, vec2 v) { m[2]=v.x, m[3]=v.y; }
inline void setx_mat2(mat2 m, vec2 v) { m[0]=v.x, m[2]=v.y; }
inline void sety_mat2(mat2 m, vec2 v) { m[1]=v.x, m[3]=v.y; }
#else
inline vec2 x_mat2(mat2 m) { return make_vec2(m[0], m[1]); }
inline vec2 y_mat2(mat2 m) { return make_vec2(m[2], m[3]); }
inline vec2 c1_mat2(mat2 m) { return make_vec2(m[0], m[2]); }
inline vec2 c2_mat2(mat2 m) { return make_vec2(m[1], m[3]); }
inline void setc1_mat2(mat2 m, vec2 v) { m[0]=v.x, m[2]=v.y; }
inline void setc2_mat2(mat2 m, vec2 v) { m[1]=v.x, m[3]=v.y; }
inline void setx_mat2(mat2 m, vec2 v) { m[0]=v.x, m[1]=v.y; }
inline void sety_mat2(mat2 m, vec2 v) { m[2]=v.x, m[3]=v.y; }
#endif
#ifndef ROW_MAJOR
inline vec3 x_mat3(mat3 m) { return make_vec3(m[0], m[3], m[6]); }
inline vec3 y_mat3(mat3 m) { return make_vec3(m[1], m[4], m[7]); }
inline vec3 z_mat3(mat3 m) { return make_vec3(m[2], m[5], m[8]); }
inline vec3 c1_mat3(mat3 m) { return make_vec3(m[0], m[1], m[2]); }
inline vec3 c2_mat3(mat3 m) { return make_vec3(m[3], m[4], m[5]); }
inline vec3 c3_mat3(mat3 m) { return make_vec3(m[6], m[7], m[8]); }
inline void setc1_mat3(mat3 m, vec3 v) { m[0]=v.x, m[1]=v.y, m[2]=v.z; }
inline void setc2_mat3(mat3 m, vec3 v) { m[3]=v.x, m[4]=v.y, m[5]=v.z; }
inline void setc3_mat3(mat3 m, vec3 v) { m[6]=v.x, m[7]=v.y, m[8]=v.z; }
inline void setx_mat3(mat3 m, vec3 v) { m[0]=v.x, m[3]=v.y, m[6]=v.z; }
inline void sety_mat3(mat3 m, vec3 v) { m[1]=v.x, m[4]=v.y, m[7]=v.z; }
inline void setz_mat3(mat3 m, vec3 v) { m[2]=v.x, m[5]=v.y, m[8]=v.z; }
#else
inline vec3 x_mat3(mat3 m) { return make_vec3(m[0], m[1], m[2]); }
inline vec3 y_mat3(mat3 m) { return make_vec3(m[3], m[4], m[5]); }
inline vec3 z_mat3(mat3 m) { return make_vec3(m[6], m[7], m[8]); }
inline vec3 c1_mat3(mat3 m) { return make_vec3(m[0], m[3], m[6]); }
inline vec3 c2_mat3(mat3 m) { return make_vec3(m[1], m[4], m[7]); }
inline vec3 c3_mat3(mat3 m) { return make_vec3(m[2], m[5], m[8]); }
inline void setc1_mat3(mat3 m, vec3 v) { m[0]=v.x, m[3]=v.y, m[6]=v.z; }
inline void setc2_mat3(mat3 m, vec3 v) { m[1]=v.x, m[4]=v.y, m[7]=v.z; }
inline void setc3_mat3(mat3 m, vec3 v) { m[2]=v.x, m[5]=v.y, m[8]=v.z; }
inline void setx_mat3(mat3 m, vec3 v) { m[0]=v.x, m[1]=v.y, m[2]=v.z; }
inline void sety_mat3(mat3 m, vec3 v) { m[3]=v.x, m[4]=v.y, m[5]=v.z; }
inline void setz_mat3(mat3 m, vec3 v) { m[6]=v.x, m[7]=v.y, m[8]=v.z; }
#endif
#ifndef ROW_MAJOR
inline vec4 c1_mat4(mat4 m) { return make_vec4(m[ 0], m[ 1], m[ 2], m[ 3]); }
inline vec4 c2_mat4(mat4 m) { return make_vec4(m[ 4], m[ 5], m[ 6], m[ 7]); }
inline vec4 c3_mat4(mat4 m) { return make_vec4(m[ 8], m[ 9], m[10], m[11]); }
inline vec4 c4_mat4(mat4 m) { return make_vec4(m[12], m[13], m[14], m[15]); }
inline vec4 x_mat4(mat4 m) { return make_vec4(m[0], m[4], m[8], m[12]); }
inline vec4 y_mat4(mat4 m) { return make_vec4(m[1], m[5], m[9], m[13]); }
inline vec4 z_mat4(mat4 m) { return make_vec4(m[2], m[6], m[10], m[14]); }
inline vec4 w_mat4(mat4 m) { return make_vec4(m[3], m[7], m[11], m[15]); }
//sets 4th row to 0 0 0 1
inline void setc1_mat4v3(mat4 m, vec3 v) { m[ 0]=v.x, m[ 1]=v.y, m[ 2]=v.z, m[ 3]=0; }
inline void setc2_mat4v3(mat4 m, vec3 v) { m[ 4]=v.x, m[ 5]=v.y, m[ 6]=v.z, m[ 7]=0; }
inline void setc3_mat4v3(mat4 m, vec3 v) { m[ 8]=v.x, m[ 9]=v.y, m[10]=v.z, m[11]=0; }
inline void setc4_mat4v3(mat4 m, vec3 v) { m[12]=v.x, m[13]=v.y, m[14]=v.z, m[15]=1; }
inline void setc1_mat4v4(mat4 m, vec4 v) { m[ 0]=v.x, m[ 1]=v.y, m[ 2]=v.z, m[ 3]=v.w; }
inline void setc2_mat4v4(mat4 m, vec4 v) { m[ 4]=v.x, m[ 5]=v.y, m[ 6]=v.z, m[ 7]=v.w; }
inline void setc3_mat4v4(mat4 m, vec4 v) { m[ 8]=v.x, m[ 9]=v.y, m[10]=v.z, m[11]=v.w; }
inline void setc4_mat4v4(mat4 m, vec4 v) { m[12]=v.x, m[13]=v.y, m[14]=v.z, m[15]=v.w; }
//sets 4th column to 0 0 0 1
inline void setx_mat4v3(mat4 m, vec3 v) { m[0]=v.x, m[4]=v.y, m[ 8]=v.z, m[12]=0; }
inline void sety_mat4v3(mat4 m, vec3 v) { m[1]=v.x, m[5]=v.y, m[ 9]=v.z, m[13]=0; }
inline void setz_mat4v3(mat4 m, vec3 v) { m[2]=v.x, m[6]=v.y, m[10]=v.z, m[14]=0; }
inline void setw_mat4v3(mat4 m, vec3 v) { m[3]=v.x, m[7]=v.y, m[11]=v.z, m[15]=1; }
inline void setx_mat4v4(mat4 m, vec4 v) { m[0]=v.x, m[4]=v.y, m[ 8]=v.z, m[12]=v.w; }
inline void sety_mat4v4(mat4 m, vec4 v) { m[1]=v.x, m[5]=v.y, m[ 9]=v.z, m[13]=v.w; }
inline void setz_mat4v4(mat4 m, vec4 v) { m[2]=v.x, m[6]=v.y, m[10]=v.z, m[14]=v.w; }
inline void setw_mat4v4(mat4 m, vec4 v) { m[3]=v.x, m[7]=v.y, m[11]=v.z, m[15]=v.w; }
#else
inline vec4 c1_mat4(mat4 m) { return make_vec4(m[0], m[4], m[8], m[12]); }
inline vec4 c2_mat4(mat4 m) { return make_vec4(m[1], m[5], m[9], m[13]); }
inline vec4 c3_mat4(mat4 m) { return make_vec4(m[2], m[6], m[10], m[14]); }
inline vec4 c4_mat4(mat4 m) { return make_vec4(m[3], m[7], m[11], m[15]); }
inline vec4 x_mat4(mat4 m) { return make_vec4(m[0], m[1], m[2], m[3]); }
inline vec4 y_mat4(mat4 m) { return make_vec4(m[4], m[5], m[6], m[7]); }
inline vec4 z_mat4(mat4 m) { return make_vec4(m[8], m[9], m[10], m[11]); }
inline vec4 w_mat4(mat4 m) { return make_vec4(m[12], m[13], m[14], m[15]); }
//sets 4th row to 0 0 0 1
inline void setc1_mat4v3(mat4 m, vec3 v) { m[0]=v.x, m[4]=v.y, m[8]=v.z, m[12]=0; }
inline void setc2_mat4v3(mat4 m, vec3 v) { m[1]=v.x, m[5]=v.y, m[9]=v.z, m[13]=0; }
inline void setc3_mat4v3(mat4 m, vec3 v) { m[2]=v.x, m[6]=v.y, m[10]=v.z, m[14]=0; }
inline void setc4_mat4v3(mat4 m, vec3 v) { m[3]=v.x, m[7]=v.y, m[11]=v.z, m[15]=1; }
inline void setc1_mat4v4(mat4 m, vec4 v) { m[0]=v.x, m[4]=v.y, m[8]=v.z, m[12]=v.w; }
inline void setc2_mat4v4(mat4 m, vec4 v) { m[1]=v.x, m[5]=v.y, m[9]=v.z, m[13]=v.w; }
inline void setc3_mat4v4(mat4 m, vec4 v) { m[2]=v.x, m[6]=v.y, m[10]=v.z, m[14]=v.w; }
inline void setc4_mat4v4(mat4 m, vec4 v) { m[3]=v.x, m[7]=v.y, m[11]=v.z, m[15]=v.w; }
//sets 4th column to 0 0 0 1
inline void setx_mat4v3(mat4 m, vec3 v) { m[0]=v.x, m[1]=v.y, m[2]=v.z, m[3]=0; }
inline void sety_mat4v3(mat4 m, vec3 v) { m[4]=v.x, m[5]=v.y, m[6]=v.z, m[7]=0; }
inline void setz_mat4v3(mat4 m, vec3 v) { m[8]=v.x, m[9]=v.y, m[10]=v.z, m[11]=0; }
inline void setw_mat4v3(mat4 m, vec3 v) { m[12]=v.x, m[13]=v.y, m[14]=v.z, m[15]=1; }
inline void setx_mat4v4(mat4 m, vec4 v) { m[0]=v.x, m[1]=v.y, m[2]=v.z, m[3]=v.w; }
inline void sety_mat4v4(mat4 m, vec4 v) { m[4]=v.x, m[5]=v.y, m[6]=v.z, m[7]=v.w; }
inline void setz_mat4v4(mat4 m, vec4 v) { m[8]=v.x, m[9]=v.y, m[10]=v.z, m[11]=v.w; }
inline void setw_mat4v4(mat4 m, vec4 v) { m[12]=v.x, m[13]=v.y, m[14]=v.z, m[15]=v.w; }
#endif
inline void fprint_mat2(FILE* f, mat2 m, const char* append)
{
#ifndef ROW_MAJOR
fprintf(f, "[(%f, %f)\n (%f, %f)]%s",
m[0], m[2], m[1], m[3], append);
#else
fprintf(f, "[(%f, %f)\n (%f, %f)]%s",
m[0], m[1], m[2], m[3], append);
#endif
}
inline void fprint_mat3(FILE* f, mat3 m, const char* append)
{
#ifndef ROW_MAJOR
fprintf(f, "[(%f, %f, %f)\n (%f, %f, %f)\n (%f, %f, %f)]%s",
m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8], append);
#else
fprintf(f, "[(%f, %f, %f)\n (%f, %f, %f)\n (%f, %f, %f)]%s",
m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], append);
#endif
}
inline void fprint_mat4(FILE* f, mat4 m, const char* append)
{