-
Notifications
You must be signed in to change notification settings - Fork 2
/
zerogl.h
1606 lines (1365 loc) · 69.3 KB
/
zerogl.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 ZEROGL_H
#define ZEROGL_H
#include <assert.h>
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
/* Utils */
// Define ZGL_DEBUG to enable debug print
#ifdef ZGL_DEBUG
#define ZGL_DEBUG_PRINT(...) printf(__VA_ARGS__)
#else
#define ZGL_DEBUG_PRINT(...) do {} while (0)
#endif
/* Vectors and Matrices */
typedef struct {
float x, y, z;
} zgl_vec3_t;
typedef struct {
float x, y, z, w;
} zgl_vec4_t;
typedef struct {
float data[4][4];
} zgl_mat4x4_t;
static const zgl_mat4x4_t IDENTITY_M4x4 = {{
{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}
}};
static inline zgl_vec3_t zgl_cross(zgl_vec3_t a, zgl_vec3_t b);
static inline float zgl_dot(zgl_vec3_t a, zgl_vec3_t b);
static inline float zgl_dot_v4(zgl_vec4_t a, zgl_vec4_t b);
static inline float zgl_magnitude(zgl_vec3_t v);
static inline zgl_vec3_t zgl_sub(zgl_vec3_t a, zgl_vec3_t b);
static inline zgl_vec4_t zgl_sub_v4(zgl_vec4_t a, zgl_vec4_t b);
static inline zgl_vec3_t zgl_add(zgl_vec3_t a, zgl_vec3_t b);
static inline zgl_vec3_t zgl_add_three_vec3(zgl_vec3_t a, zgl_vec3_t b, zgl_vec3_t c);
static inline zgl_vec4_t zgl_add_v4(zgl_vec4_t a, zgl_vec4_t b);
static inline zgl_vec3_t zgl_normalize(zgl_vec3_t v);
static inline zgl_vec3_t zgl_mul_scalar(float k, zgl_vec3_t v);
static inline zgl_vec4_t zgl_mul_mat_v4(zgl_mat4x4_t mat4x4, zgl_vec4_t vec4);
static inline zgl_vec3_t zgl_mul_mat_v3(zgl_mat4x4_t mat4x4, zgl_vec3_t v);
static inline zgl_mat4x4_t zgl_mul_mat(zgl_mat4x4_t m1, zgl_mat4x4_t m2);
static inline zgl_mat4x4_t zgl_transpose(zgl_mat4x4_t m);
static inline zgl_mat4x4_t zgl_inverse(zgl_mat4x4_t matrix);
static inline zgl_mat4x4_t zgl_translation_mat(zgl_vec3_t vector);
static inline zgl_mat4x4_t zgl_scale_mat(float scale);
static inline zgl_mat4x4_t zgl_rotx_mat(float degrees);
static inline zgl_mat4x4_t zgl_roty_mat(float degrees);
static inline zgl_mat4x4_t zgl_rotz_mat(float degrees);
/* Quaternions */
typedef struct {
float w, x, y, z;
} zgl_quaternion_t;
static inline zgl_quaternion_t zgl_quaternion(float degrees, zgl_vec3_t axis);
static inline zgl_quaternion_t zgl_mul_quat(zgl_quaternion_t q1, zgl_quaternion_t q2);
static inline zgl_vec3_t zgl_rotate(zgl_vec3_t v, zgl_quaternion_t q);
/* Colors */
// Pixel formats
#define ZEROGL_PIXELFORMAT_RGBA8888 0
#define ZEROGL_PIXELFORMAT_ARGB8888 1
#define ZEROGL_PIXELFORMAT_BGRA8888 2
#define ZEROGL_PIXELFORMAT_ABGR8888 3
// Configure pixel format
#ifndef ZEROGL_PIXELFORMAT
#define ZEROGL_PIXELFORMAT ZEROGL_PIXELFORMAT_RGBA8888
#endif // ZEROGL_PIXELFORMAT
// Default colors
#if ZEROGL_PIXELFORMAT == ZEROGL_PIXELFORMAT_RGBA8888
static const uint32_t ZGL_COLOR_WHITE = 0xFFFFFF00;
static const uint32_t ZGL_COLOR_BLACK = 0x00000000;
static const uint32_t ZGL_COLOR_RED = 0xFF000000;
static const uint32_t ZGL_COLOR_GREEN = 0x00FF0000;
static const uint32_t ZGL_COLOR_BLUE = 0x0000FF00;
#elif ZEROGL_PIXELFORMAT == ZEROGL_PIXELFORMAT_ARGB8888
static const uint32_t ZGL_COLOR_WHITE = 0x00FFFFFF;
static const uint32_t ZGL_COLOR_BLACK = 0x00000000;
static const uint32_t ZGL_COLOR_RED = 0x00FF0000;
static const uint32_t ZGL_COLOR_GREEN = 0x0000FF00;
static const uint32_t ZGL_COLOR_BLUE = 0x000000FF;
#elif ZEROGL_PIXELFORMAT == ZEROGL_PIXELFORMAT_BGRA8888
static const uint32_t ZGL_COLOR_WHITE = 0xFFFFFF00;
static const uint32_t ZGL_COLOR_BLACK = 0x00000000;
static const uint32_t ZGL_COLOR_RED = 0x0000FF00;
static const uint32_t ZGL_COLOR_GREEN = 0x00FF0000;
static const uint32_t ZGL_COLOR_BLUE = 0xFF000000;
#elif ZEROGL_PIXELFORMAT == ZEROGL_PIXELFORMAT_ABGR8888
static const uint32_t ZGL_COLOR_WHITE = 0x00FFFFFF;
static const uint32_t ZGL_COLOR_BLACK = 0x00000000;
static const uint32_t ZGL_COLOR_RED = 0x000000FF;
static const uint32_t ZGL_COLOR_GREEN = 0x0000FF00;
static const uint32_t ZGL_COLOR_BLUE = 0x00FF0000;
#endif
static inline uint32_t zgl_color(uint8_t r, uint8_t g, uint8_t b);
static inline void zgl_color_components(uint32_t c, uint8_t* r, uint8_t* g, uint8_t* b);
static inline uint32_t zgl_mul_scalar_color(double x, uint32_t color);
static inline uint32_t zgl_mul_vec3_color(zgl_vec3_t v, uint32_t color);
static inline uint32_t zgl_add_colors(uint32_t c0, uint32_t c1);
static inline uint32_t zgl_color_from_floats(float r, float g, float b);
static inline void zgl_color_to_floats(uint32_t color, float* r, float* g, float* b);
/* 3D Objects */
typedef struct {
uint32_t* frameBuffer;
int width;
int height;
int hasDepthBuffer;
float* depthBuffer;
} zgl_canvas_t;
typedef struct {
int v0, v1, v2;
int t0, t1, t2;
int n0, n1, n2;
int materialIndex;
} zgl_triangle_t;
typedef struct {
char* name;
uint32_t ambientColor;
uint32_t diffuseColor;
uint32_t specularColor;
float specularExponent;
zgl_canvas_t ambientTexture;
zgl_canvas_t diffuseTexture;
zgl_canvas_t specularTexture;
} zgl_material_t;
typedef struct {
char* name;
int numVertices;
zgl_vec3_t* vertices;
int numTextureCoords;
zgl_vec3_t* textureCoords;
int numNormals;
zgl_vec3_t* normals;
float* invMagnitudeNormals;
int numTriangles;
zgl_triangle_t* triangles;
int numMaterials;
zgl_material_t* materials;
zgl_vec3_t center;
float boundsRadius;
} zgl_mesh_t;
typedef struct {
zgl_mesh_t* mesh;
zgl_vec3_t translation;
float scale;
zgl_mat4x4_t rotation;
zgl_mat4x4_t transform;
} zgl_object3D_t;
static inline zgl_object3D_t zgl_object(zgl_mesh_t *mesh, zgl_vec3_t translation, float scale, zgl_mat4x4_t rotation);
static inline zgl_vec3_t zgl_mesh_center(zgl_vec3_t* vertices, int numVertices);
static inline float zgl_mesh_bound_radius(zgl_vec3_t* vertices, int numVertices, zgl_vec3_t center);
uint32_t zgl_sample_texture(float u, float v, zgl_canvas_t texture, int bilinearFiltering);
/* Lighting */
typedef struct {
zgl_vec3_t intensity;
} zgl_ambient_light_t;
typedef struct {
zgl_vec3_t intensity;
zgl_vec3_t direction;
} zgl_dir_light_t;
typedef struct {
zgl_vec3_t intensity;
zgl_vec3_t position;
} zgl_point_light_t;
typedef struct {
int numAmbientLights;
zgl_ambient_light_t* ambientLights;
int numDirectionalLights;
zgl_dir_light_t* directionalLights;
int numPointLights;
zgl_point_light_t* pointLights;
} zgl_light_sources_t;
typedef struct {
zgl_vec3_t diffuse;
zgl_vec3_t specular;
zgl_vec3_t ambient;
} zgl_lighting_result_t;
static inline zgl_lighting_result_t zgl_lighting(zgl_vec3_t position, zgl_vec3_t normal, float invMagnitudeNormal, float specularExponent,
zgl_light_sources_t lightSources, uint8_t renderOptions);
/* Camera */
// Camera for left handed coordinate system
typedef struct {
zgl_vec3_t position; // x, y, z coordinates of the camera
zgl_vec3_t direction; // Direction in which the camera is looking
zgl_vec3_t up; // Up direction for the camera (usually [0, 1, 0])
float fov; // Field of view (in degrees)
float aspectRatio; // Aspect ratio of the viewport
float nearPlane; // Distance to the near clipping plane
float farPlane; // Distance to the far clipping plane
zgl_mat4x4_t viewMatrix; // View matrix
zgl_mat4x4_t projectionMatrix; // Projection matrix
zgl_mat4x4_t viewProjMatrix; // View * Projection matrix
zgl_vec4_t fustrumPlanes[6]; // View frustum planes
float movementSpeed;
float turningSpeed;
} zgl_camera_t;
static inline zgl_camera_t zgl_camera(zgl_vec3_t position, zgl_vec3_t direction, zgl_vec3_t up,
float fov, float aspectRatio, float near, float far,
float movementSpeed, float turningSpeed);
/* Shaders */
// Configuration
#ifndef ZGL_MAX_VERTEX_SHADER_ATTRIBUTES
#define ZGL_MAX_VERTEX_SHADER_ATTRIBUTES 50
#endif // ZGL_MAX_VERTEX_SHADER_ATTRIBUTES
typedef struct {
zgl_vec3_t position;
zgl_vec3_t normal;
zgl_vec3_t textureCoord;
int materialIndex;
} zgl_vertex_input_t;
typedef struct {
zgl_vec4_t position;
int numAttributes;
float attributes[ZGL_MAX_VERTEX_SHADER_ATTRIBUTES];
int numFlatAttributes;
float flatAttributes[ZGL_MAX_VERTEX_SHADER_ATTRIBUTES];
} zgl_shader_context_t;
typedef zgl_shader_context_t zgl_vertex_shader_t(void* inputVertex, void* uniformData);
typedef uint32_t zgl_fragment_shader_t(const zgl_shader_context_t* input, void* uniformData);
typedef struct {
zgl_mat4x4_t modelviewprojection;
} zgl_basic_uniform_t;
static inline zgl_shader_context_t zgl_basic_vertex_shader(void* inputVertex, void* uniformData);
static inline uint32_t zgl_basic_fragment_shader(const zgl_shader_context_t* input, void* uniformData);
typedef struct {
zgl_mat4x4_t modelviewprojection;
zgl_material_t* materials;
} zgl_colored_uniform_t;
static inline zgl_shader_context_t zgl_colored_vertex_shader(void* inputVertex, void* uniformData);
static inline uint32_t zgl_colored_fragment_shader(const zgl_shader_context_t* input, void* uniformData);
typedef struct {
zgl_mat4x4_t modelMatrix;
zgl_mat4x4_t modelInvRotationMatrixTransposed;
zgl_mat4x4_t viewProjectionMatrix;
int bilinearFiltering;
zgl_material_t* materials;
} zgl_unlit_uniform_t;
static inline zgl_shader_context_t zgl_unlit_vertex_shader(void* inputVertex, void* uniformData);
static inline uint32_t zgl_unlit_fragment_shader(const zgl_shader_context_t* input, void* uniformData);
typedef struct {
zgl_mat4x4_t modelMatrix;
zgl_mat4x4_t modelInvRotationMatrixTransposed;
zgl_mat4x4_t viewProjectionMatrix;
zgl_light_sources_t lightSources;
int bilinearFiltering;
zgl_material_t* materials;
} zgl_flat_uniform_t;
static inline zgl_shader_context_t zgl_flat_vertex_shader(void* inputVertex, void* uniformData);
static inline uint32_t zgl_flat_fragment_shader(const zgl_shader_context_t* input, void* uniformData);
typedef struct {
zgl_mat4x4_t modelMatrix;
zgl_mat4x4_t modelInvRotationMatrixTransposed;
zgl_mat4x4_t viewProjectionMatrix;
zgl_light_sources_t lightSources;
int bilinearFiltering;
zgl_material_t* materials;
} zgl_gourard_uniform_t;
static inline zgl_shader_context_t zgl_gourard_vertex_shader(void* inputVertex, void* uniformData);
static inline uint32_t zgl_gourard_fragment_shader(const zgl_shader_context_t* input, void* uniformData);
typedef struct {
zgl_mat4x4_t modelMatrix;
zgl_mat4x4_t modelInvRotationMatrixTransposed;
zgl_mat4x4_t viewProjectionMatrix;
zgl_light_sources_t lightSources;
int bilinearFiltering;
zgl_material_t* materials;
} zgl_phong_uniform_t;
static inline zgl_shader_context_t zgl_phong_vertex_shader(void* inputVertex, void* uniformData);
static inline uint32_t zgl_phong_fragment_shader(const zgl_shader_context_t* input, void* uniformData);
/* Rendering */
// Rendering options
#define ZGL_DIFFUSE_LIGHTING (1 << 0)
#define ZGL_SPECULAR_LIGHTING (1 << 1)
#define ZGL_BACKFACE_CULLING (1 << 2)
#define ZGL_FUSTRUM_CULLING (1 << 3)
#define ZGL_BILINEAR_FILTERING (1 << 4) // TODO: Implement bilinear filtering
static inline void zgl_clear_depth_buffer(zgl_canvas_t canvas);
static inline void zgl_render_pixel(int i, int j, float z, uint32_t color, zgl_canvas_t canvas);
static inline void zgl_render_fill(uint32_t color, zgl_canvas_t canvas);
static inline void zgl_render_line(int x0, int x1, int y0, int y1, uint32_t color, zgl_canvas_t canvas);
static inline void zgl_render_circle(int x, int y, int r, uint32_t color, zgl_canvas_t canvas);
static inline void zgl_render_triangle(int x0, int y0, uint32_t color0,
int x1, int y1, uint32_t color1,
int x2, int y2, uint32_t color2,
zgl_canvas_t canvas);
static inline void zgl_render_object3D(zgl_object3D_t* object, void *uniformData, zgl_camera_t camera, zgl_canvas_t canvas,
zgl_vertex_shader_t vertexShader, zgl_fragment_shader_t fragmentShader, uint16_t renderOptions);
#endif // ZEROGL_H
// Include implementation if the client sets ZEROGL_IMPLEMENTATION, but not twice
#ifdef ZEROGL_IMPLEMENTATION
#ifndef ZEROGL_IMPLEMENTATION_INCLUDED
#define ZEROGL_IMPLEMENTATION_INCLUDED
/* Utils */
#define ZGL__PI 3.14159265358979323846264338327950288
#define ZGL__MIN(a,b) (((a)<(b))?(a):(b))
#define ZGL__MAX(a,b) (((a)>(b))?(a):(b))
/* Vectors and Matrices */
static inline zgl_vec3_t zgl_cross(zgl_vec3_t a, zgl_vec3_t b) {
zgl_vec3_t result;
result.x = a.y * b.z - a.z * b.y;
result.y = a.z * b.x - a.x * b.z;
result.z = a.x * b.y - a.y * b.x;
return result;
}
static inline float zgl_dot(zgl_vec3_t a, zgl_vec3_t b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
static inline float zgl_dot_v4(zgl_vec4_t a, zgl_vec4_t b) {
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}
static inline float zgl_magnitude(zgl_vec3_t v) {
float result = sqrt(zgl_dot(v, v));
assert(result >= 0);
return result;
}
static inline zgl_vec3_t zgl_sub(zgl_vec3_t a, zgl_vec3_t b) {
return (zgl_vec3_t) {a.x - b.x, a.y - b.y, a.z - b.z};
}
static inline zgl_vec4_t zgl_sub_v4(zgl_vec4_t a, zgl_vec4_t b) {
return (zgl_vec4_t) {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
}
static inline zgl_vec3_t zgl_add(zgl_vec3_t a, zgl_vec3_t b) {
return (zgl_vec3_t) {a.x + b.x, a.y + b.y, a.z + b.z};
}
static inline zgl_vec3_t zgl_add_three_vec3(zgl_vec3_t a, zgl_vec3_t b, zgl_vec3_t c) {
return (zgl_vec3_t) {a.x + b.x + c.x, a.y + b.y + c.y, a.z + b.z + c.z};
}
static inline zgl_vec4_t zgl_add_v4(zgl_vec4_t a, zgl_vec4_t b) {
return (zgl_vec4_t) {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
}
static inline zgl_vec3_t zgl_normalize(zgl_vec3_t v) {
float mag = zgl_magnitude(v);
return (zgl_vec3_t) {v.x / mag, v.y / mag, v.z / mag};
}
static inline zgl_vec3_t zgl_mul_scalar(float k, zgl_vec3_t v) {
return (zgl_vec3_t) {k*v.x, k*v.y, k*v.z};
}
static inline zgl_vec4_t zgl_mul_mat_v4(zgl_mat4x4_t mat4x4, zgl_vec4_t vec4) {
float result[4] = {0};
for (int i = 0; i < 4; i++) {
result[i] += mat4x4.data[i][0]*vec4.x;
result[i] += mat4x4.data[i][1]*vec4.y;
result[i] += mat4x4.data[i][2]*vec4.z;
result[i] += mat4x4.data[i][3]*vec4.w;
}
return (zgl_vec4_t) {result[0], result[1], result[2], result[3]};
}
static inline zgl_vec3_t zgl_mul_mat_v3(zgl_mat4x4_t mat4x4, zgl_vec3_t v) {
zgl_vec4_t vec4 = {v.x, v.y, v.z, 1};
zgl_vec4_t result = zgl_mul_mat_v4(mat4x4, vec4);
return (zgl_vec3_t) {result.x, result.y, result.z};
}
static inline zgl_mat4x4_t zgl_mul_mat(zgl_mat4x4_t m1, zgl_mat4x4_t m2) {
zgl_mat4x4_t result = {0};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
result.data[i][j] += m1.data[i][k]*m2.data[k][j];
}
}
}
return result;
}
static inline zgl_mat4x4_t zgl_transpose(zgl_mat4x4_t m) {
zgl_mat4x4_t result = {0};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
result.data[i][j] = m.data[j][i];
}
}
return result;
}
static inline zgl_mat4x4_t zgl_inverse(zgl_mat4x4_t matrix) {
float m[16];
for (int i = 0; i < 16; i++) {
m[i] = matrix.data[i/4][i%4];
}
float invOut[16];
float inv[16], det;
inv[0] = m[5] * m[10] * m[15] -
m[5] * m[11] * m[14] -
m[9] * m[6] * m[15] +
m[9] * m[7] * m[14] +
m[13] * m[6] * m[11] -
m[13] * m[7] * m[10];
inv[4] = -m[4] * m[10] * m[15] +
m[4] * m[11] * m[14] +
m[8] * m[6] * m[15] -
m[8] * m[7] * m[14] -
m[12] * m[6] * m[11] +
m[12] * m[7] * m[10];
inv[8] = m[4] * m[9] * m[15] -
m[4] * m[11] * m[13] -
m[8] * m[5] * m[15] +
m[8] * m[7] * m[13] +
m[12] * m[5] * m[11] -
m[12] * m[7] * m[9];
inv[12] = -m[4] * m[9] * m[14] +
m[4] * m[10] * m[13] +
m[8] * m[5] * m[14] -
m[8] * m[6] * m[13] -
m[12] * m[5] * m[10] +
m[12] * m[6] * m[9];
inv[1] = -m[1] * m[10] * m[15] +
m[1] * m[11] * m[14] +
m[9] * m[2] * m[15] -
m[9] * m[3] * m[14] -
m[13] * m[2] * m[11] +
m[13] * m[3] * m[10];
inv[5] = m[0] * m[10] * m[15] -
m[0] * m[11] * m[14] -
m[8] * m[2] * m[15] +
m[8] * m[3] * m[14] +
m[12] * m[2] * m[11] -
m[12] * m[3] * m[10];
inv[9] = -m[0] * m[9] * m[15] +
m[0] * m[11] * m[13] +
m[8] * m[1] * m[15] -
m[8] * m[3] * m[13] -
m[12] * m[1] * m[11] +
m[12] * m[3] * m[9];
inv[13] = m[0] * m[9] * m[14] -
m[0] * m[10] * m[13] -
m[8] * m[1] * m[14] +
m[8] * m[2] * m[13] +
m[12] * m[1] * m[10] -
m[12] * m[2] * m[9];
inv[2] = m[1] * m[6] * m[15] -
m[1] * m[7] * m[14] -
m[5] * m[2] * m[15] +
m[5] * m[3] * m[14] +
m[13] * m[2] * m[7] -
m[13] * m[3] * m[6];
inv[6] = -m[0] * m[6] * m[15] +
m[0] * m[7] * m[14] +
m[4] * m[2] * m[15] -
m[4] * m[3] * m[14] -
m[12] * m[2] * m[7] +
m[12] * m[3] * m[6];
inv[10] = m[0] * m[5] * m[15] -
m[0] * m[7] * m[13] -
m[4] * m[1] * m[15] +
m[4] * m[3] * m[13] +
m[12] * m[1] * m[7] -
m[12] * m[3] * m[5];
inv[14] = -m[0] * m[5] * m[14] +
m[0] * m[6] * m[13] +
m[4] * m[1] * m[14] -
m[4] * m[2] * m[13] -
m[12] * m[1] * m[6] +
m[12] * m[2] * m[5];
inv[3] = -m[1] * m[6] * m[11] +
m[1] * m[7] * m[10] +
m[5] * m[2] * m[11] -
m[5] * m[3] * m[10] -
m[9] * m[2] * m[7] +
m[9] * m[3] * m[6];
inv[7] = m[0] * m[6] * m[11] -
m[0] * m[7] * m[10] -
m[4] * m[2] * m[11] +
m[4] * m[3] * m[10] +
m[8] * m[2] * m[7] -
m[8] * m[3] * m[6];
inv[11] = -m[0] * m[5] * m[11] +
m[0] * m[7] * m[9] +
m[4] * m[1] * m[11] -
m[4] * m[3] * m[9] -
m[8] * m[1] * m[7] +
m[8] * m[3] * m[5];
inv[15] = m[0] * m[5] * m[10] -
m[0] * m[6] * m[9] -
m[4] * m[1] * m[10] +
m[4] * m[2] * m[9] +
m[8] * m[1] * m[6] -
m[8] * m[2] * m[5];
det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
if (det == 0)
return IDENTITY_M4x4;
det = 1.0 / det;
for (int i = 0; i < 16; i++)
invOut[i] = inv[i] * det;
zgl_mat4x4_t invMatrix;
for (int i = 0; i < 4; i++) {
invMatrix.data[i][0] = invOut[i*4];
invMatrix.data[i][1] = invOut[i*4+1];
invMatrix.data[i][2] = invOut[i*4+2];
invMatrix.data[i][3] = invOut[i*4+3];
}
return invMatrix;
}
static inline zgl_mat4x4_t zgl_translation_mat(zgl_vec3_t vector) {
return (zgl_mat4x4_t) {{
{1, 0, 0, vector.x},
{0, 1, 0, vector.y},
{0, 0, 1, vector.z},
{0, 0, 0, 1}
}};
}
static inline zgl_mat4x4_t zgl_scale_mat(float scale) {
return (zgl_mat4x4_t) {{
{scale, 0, 0, 0},
{0, scale, 0, 0},
{0, 0, scale, 0},
{0, 0, 0, 1}
}};
}
// TODO: Only use quaternion for rotation
static inline zgl_mat4x4_t zgl_rotx_mat(float degrees) {
float radians = degrees * ZGL__PI / 180.0f;
float cos = cosf(radians);
float sin = sinf(radians);
return (zgl_mat4x4_t) {{
{ 1, 0, 0, 0 },
{ 0, cos, sin, 0 },
{ 0, -sin, cos, 0 },
{ 0, 0, 0, 1 }
}};
}
static inline zgl_mat4x4_t zgl_roty_mat(float degrees) {
float radians = degrees * ZGL__PI / 180.0f;
float cos = cosf(radians);
float sin = sinf(radians);
return (zgl_mat4x4_t) {{
{ cos, 0, -sin, 0 },
{ 0, 1, 0, 0 },
{ sin, 0, cos, 0 },
{ 0, 0, 0, 1 }
}};
}
static inline zgl_mat4x4_t zgl_rotz_mat(float degrees) {
float radians = degrees * ZGL__PI / 180.0f;
float cos = cosf(radians);
float sin = sinf(radians);
return (zgl_mat4x4_t) {{
{ cos, sin, 0, 0 },
{ -sin, cos, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
}};
}
/* Quaternions */
static inline zgl_quaternion_t zgl_quaternion(float degrees, zgl_vec3_t axis) {
zgl_vec3_t normalizedAxis = zgl_normalize(axis);
float angle = degrees * ZGL__PI / 180.0f;
float halfAngle = angle * 0.5f;
float sinHalfAngle = sinf(halfAngle);
return (zgl_quaternion_t) {cosf(halfAngle), normalizedAxis.x * sinHalfAngle, normalizedAxis.y * sinHalfAngle, normalizedAxis.z * sinHalfAngle};
}
static inline zgl_quaternion_t zgl_mul_quat(zgl_quaternion_t q1, zgl_quaternion_t q2) {
zgl_quaternion_t result;
result.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z;
result.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y;
result.y = q1.w * q2.y - q1.x * q2.z + q1.y * q2.w + q1.z * q2.x;
result.z = q1.w * q2.z + q1.x * q2.y - q1.y * q2.x + q1.z * q2.w;
return result;
}
static inline zgl_vec3_t zgl_rotate(zgl_vec3_t v, zgl_quaternion_t q) {
zgl_quaternion_t q_v = {0, v.x, v.y, v.z};
zgl_quaternion_t q_conjugate = {q.w, -q.x, -q.y, -q.z};
zgl_quaternion_t rotated = zgl_mul_quat(zgl_mul_quat(q, q_v), q_conjugate);
return (zgl_vec3_t){rotated.x, rotated.y, rotated.z};
}
/* Colors */
static inline uint32_t zgl_color(uint8_t r, uint8_t g, uint8_t b) {
#if ZEROGL_PIXELFORMAT == ZEROGL_PIXELFORMAT_RGBA8888
return 0x00000000 | (r << 24) | (g << 16) | (b << 8);
#elif ZEROGL_PIXELFORMAT == ZEROGL_PIXELFORMAT_ARGB8888
return 0x00000000 | (r << 16) | (g << 8) | b;
#elif ZEROGL_PIXELFORMAT == ZEROGL_PIXELFORMAT_BGRA8888
return 0x00000000 | (b << 24) | (g << 16) | (r << 8);
#elif ZEROGL_PIXELFORMAT == ZEROGL_PIXELFORMAT_ABGR8888
return 0x00000000 | (b << 16) | (g << 8) | r;
#endif
}
static inline void zgl_color_components(uint32_t c, uint8_t* r, uint8_t* g, uint8_t* b) {
#if ZEROGL_PIXELFORMAT == ZEROGL_PIXELFORMAT_RGBA8888
*r = (c & 0xFF000000) >> 24;
*g = (c & 0x00FF0000) >> 16;
*b = (c & 0x0000FF00) >> 8;
#elif ZEROGL_PIXELFORMAT == ZEROGL_PIXELFORMAT_ARGB8888
*r = (c & 0x00FF0000) >> 16;
*g = (c & 0x0000FF00) >> 8;
*b = c & 0x000000FF;
#elif ZEROGL_PIXELFORMAT == ZEROGL_PIXELFORMAT_BGRA8888
*r = (c & 0x0000FF00) >> 8;
*g = (c & 0x00FF0000) >> 16;
*b = (c & 0xFF000000) >> 24;
#elif ZEROGL_PIXELFORMAT == ZEROGL_PIXELFORMAT_ABGR8888
*r = c & 0x000000FF;
*g = (c & 0x0000FF00) >> 8;
*b = (c & 0x00FF0000) >> 16;
#endif
}
static inline float zgl__clamp(float v, float max) {
return v > max ? max : v;
}
static inline uint32_t zgl_mul_scalar_color(double x, uint32_t color) {
uint8_t r, g, b;
zgl_color_components(color, &r, &g, &b);
return zgl_color(zgl__clamp(x * r, 255.0), zgl__clamp(x * g, 255.0), zgl__clamp(x * b, 255.0));
}
static inline uint32_t zgl_mul_vec3_color(zgl_vec3_t v, uint32_t color) {
uint8_t r, g, b;
zgl_color_components(color, &r, &g, &b);
return zgl_color(zgl__clamp(v.x * r, 255.0), zgl__clamp(v.y * g, 255.0), zgl__clamp(v.z * b, 255.0));
}
static inline uint32_t zgl_add_colors(uint32_t c0, uint32_t c1) {
uint8_t r0, g0, b0;
uint8_t r1, g1, b1;
zgl_color_components(c0, &r0, &g0, &b0);
zgl_color_components(c1, &r1, &g1, &b1);
return zgl_color(zgl__clamp(r0 + r1, 255.0), zgl__clamp(g0 + g1, 255.0), zgl__clamp(b0 + b1, 255.0));
}
static inline uint32_t zgl_mul_colors(uint32_t c0, uint32_t c1) {
uint8_t r0, g0, b0;
uint8_t r1, g1, b1;
zgl_color_components(c0, &r0, &g0, &b0);
zgl_color_components(c1, &r1, &g1, &b1);
return zgl_color(zgl__clamp(r0 * r1 / 255.0, 255.0), zgl__clamp(g0 * g1 / 255.0, 255.0), zgl__clamp(b0 * b1 / 255.0, 255.0));
}
static inline uint32_t zgl_color_from_floats(float r, float g, float b) {
return zgl_color(zgl__clamp(r * 255.0, 255.0), zgl__clamp(g * 255.0, 255.0), zgl__clamp(b * 255.0, 255.0));
}
static inline void zgl_color_to_floats(uint32_t color, float* r, float* g, float* b) {
uint8_t r8, g8, b8;
zgl_color_components(color, &r8, &g8, &b8);
*r = r8 / 255.0f;
*g = g8 / 255.0f;
*b = b8 / 255.0f;
}
/* 3D Objects */
static inline zgl_object3D_t zgl_object(zgl_mesh_t *mesh, zgl_vec3_t translation, float scale, zgl_mat4x4_t rotation) {
zgl_mat4x4_t translationMatrix = zgl_translation_mat(translation);
zgl_mat4x4_t scaleMatrix = zgl_scale_mat(scale);
zgl_mat4x4_t transform = zgl_mul_mat(translationMatrix, zgl_mul_mat(rotation, scaleMatrix));
return (zgl_object3D_t) {mesh, translation, scale, rotation, transform};
}
static inline zgl_vec3_t zgl_mesh_center(zgl_vec3_t* vertices, int numVertices) {
zgl_vec3_t result = {0, 0, 0};
for (int i = 0; i < numVertices; i++) {
result = zgl_add(result, vertices[i]);
}
return zgl_mul_scalar(1.0f / numVertices, result);
}
static inline float zgl_mesh_bound_radius(zgl_vec3_t* vertices, int numVertices, zgl_vec3_t center) {
float result = 0.0f;
for (int i = 0; i < numVertices; i++) {
float distance = zgl_magnitude(zgl_sub(vertices[i], center));
if (distance > result) {
result = distance;
}
}
return result;
}
uint32_t zgl_sample_texture(float u, float v, zgl_canvas_t texture, int bilinearFiltering) {
int textureWidth = texture.width;
int textureHeight = texture.height;
float tex_u = ZGL__MIN(fabs(u * textureWidth), textureWidth - 1);
float tex_v = ZGL__MIN(fabs(v * textureHeight), textureHeight - 1);
int floor_u = floor(tex_u);
int floor_v = floor(tex_v);
if (bilinearFiltering) {
float ratio_u = tex_u - floor_u;
float ratio_v = tex_v - floor_v;
int next_u = ZGL__MIN(floor_u + 1, textureWidth - 1);
int next_v = ZGL__MIN(floor_v + 1, textureHeight - 1);
uint32_t color00 = texture.frameBuffer[floor_v * textureWidth + floor_u];
uint32_t color10 = texture.frameBuffer[floor_v * textureWidth + next_u];
uint32_t color01 = texture.frameBuffer[next_v * textureWidth + floor_u];
uint32_t color11 = texture.frameBuffer[next_v * textureWidth + next_u];
uint32_t color0 = zgl_add_colors(zgl_mul_scalar_color(1.0f - ratio_u, color00), zgl_mul_scalar_color(ratio_u, color10));
uint32_t color1 = zgl_add_colors(zgl_mul_scalar_color(1.0f - ratio_u, color01), zgl_mul_scalar_color(ratio_u, color11));
return zgl_add_colors(zgl_mul_scalar_color(1.0f - ratio_v, color0), zgl_mul_scalar_color(ratio_v, color1));
} else {
return texture.frameBuffer[floor_v * textureWidth + floor_u];
}
}
/* Lighting */
// TODO: Code here is a bit repeated between directional and point lights. Maybe refactor?
static inline zgl_lighting_result_t zgl_lighting(zgl_vec3_t position, zgl_vec3_t normal, float invMagnitudeNormal, float specularExponent,
zgl_light_sources_t lightSources, uint8_t renderOptions) {
int numDirectionalLights = lightSources.numDirectionalLights;
zgl_dir_light_t* directionalLights = lightSources.directionalLights;
int numPointLights = lightSources.numPointLights;
zgl_point_light_t* pointLights = lightSources.pointLights;
int numAmbientLights = lightSources.numAmbientLights;
zgl_ambient_light_t* ambientLights = lightSources.ambientLights;
zgl_vec3_t diffuseIntensity = {0.0, 0.0, 0.0};
zgl_vec3_t specularIntensity = {0.0, 0.0, 0.0};
zgl_vec3_t ambientIntensity = {0.0, 0.0, 0.0};
// Directional lights
for (int i = 0; i < numDirectionalLights; i++) {
zgl_vec3_t lightDirection = directionalLights[i].direction;
float magnitudeLightDirection = zgl_magnitude(lightDirection);
float invMagnitudeLightDirection = 1.0f / magnitudeLightDirection;
if (renderOptions & ZGL_DIFFUSE_LIGHTING) {
float cos_alpha = -zgl_dot(lightDirection, normal) * invMagnitudeLightDirection * invMagnitudeNormal;
diffuseIntensity.x += ZGL__MAX(cos_alpha, 0.0f) * directionalLights[i].intensity.x;
diffuseIntensity.y += ZGL__MAX(cos_alpha, 0.0f) * directionalLights[i].intensity.y;
diffuseIntensity.z += ZGL__MAX(cos_alpha, 0.0f) * directionalLights[i].intensity.z;
}
if (renderOptions & ZGL_SPECULAR_LIGHTING) {
zgl_vec3_t reflection = zgl_sub(zgl_mul_scalar(2 * -zgl_dot(lightDirection, normal), normal), lightDirection);
float cos_beta = -zgl_dot(reflection, normal) * invMagnitudeLightDirection * invMagnitudeNormal;
specularIntensity.x += pow(ZGL__MAX(cos_beta, 0.0f), specularExponent) * directionalLights[i].intensity.x;
specularIntensity.y += pow(ZGL__MAX(cos_beta, 0.0f), specularExponent) * directionalLights[i].intensity.y;
specularIntensity.z += pow(ZGL__MAX(cos_beta, 0.0f), specularExponent) * directionalLights[i].intensity.z;
}
}
// Point lights
for (int i = 0; i < numPointLights; i++) {
zgl_vec3_t lightDirection = zgl_sub(pointLights[i].position, position);
float invMagnitudeLightDirection = 1.0f / zgl_magnitude(lightDirection);
if (renderOptions & ZGL_DIFFUSE_LIGHTING) {
float cos_alpha = zgl_dot(lightDirection, normal) * invMagnitudeLightDirection * invMagnitudeNormal;
diffuseIntensity.x += ZGL__MAX(cos_alpha, 0) * pointLights[i].intensity.x;
diffuseIntensity.y += ZGL__MAX(cos_alpha, 0) * pointLights[i].intensity.y;
diffuseIntensity.z += ZGL__MAX(cos_alpha, 0) * pointLights[i].intensity.z;
}
if (renderOptions & ZGL_SPECULAR_LIGHTING) {
zgl_vec3_t reflection = zgl_sub(zgl_mul_scalar(2 * zgl_dot(lightDirection, normal), normal), lightDirection);
float cos_beta = zgl_dot(reflection, normal) * invMagnitudeLightDirection * invMagnitudeNormal;
specularIntensity.x += pow(ZGL__MAX(cos_beta, 0), specularExponent) * pointLights[i].intensity.x;
specularIntensity.y += pow(ZGL__MAX(cos_beta, 0), specularExponent) * pointLights[i].intensity.y;
specularIntensity.z += pow(ZGL__MAX(cos_beta, 0), specularExponent) * pointLights[i].intensity.z;
}
}
// Ambient light
for (int i = 0; i < numAmbientLights; i++) {
ambientIntensity.x += ambientLights[i].intensity.x;
ambientIntensity.y += ambientLights[i].intensity.y;
ambientIntensity.z += ambientLights[i].intensity.z;
}
zgl_lighting_result_t result = {diffuseIntensity, specularIntensity, ambientIntensity};
return result;
}
/* Camera */
static inline zgl_vec4_t zgl__normalize_plane(zgl_vec4_t plane) {
float mag = sqrt(plane.x*plane.x + plane.y*plane.y + plane.z*plane.z);
return (zgl_vec4_t) {plane.x / mag, plane.y / mag, plane.z / mag, plane.w / mag};
}
static inline zgl_camera_t zgl_camera(zgl_vec3_t position, zgl_vec3_t direction, zgl_vec3_t up,
float fov, float aspectRatio, float near, float far,
float movementSpeed, float turningSpeed) {
direction = zgl_normalize(direction);
up = zgl_normalize(up);
zgl_vec3_t right = zgl_normalize(zgl_cross(up, direction));
zgl_vec3_t correctedUp = zgl_cross(direction, right);
zgl_mat4x4_t rotationMatrix = (zgl_mat4x4_t) {{
{right.x, right.y, right.z, 0},
{correctedUp.x, correctedUp.y, correctedUp.z, 0},
{direction.x, direction.y, direction.z, 0},
{0, 0, 0, 1}
}};
// Create the translation matrix
zgl_mat4x4_t translationMatrix = (zgl_mat4x4_t) {{
{1, 0, 0, -position.x},
{0, 1, 0, -position.y},
{0, 0, 1, -position.z},
{0, 0, 0, 1 }
}};
// Create the view matrix
zgl_mat4x4_t viewMatrix = zgl_mul_mat(rotationMatrix, translationMatrix);
float fovRadians = fov * ZGL__PI / 180.0;
float yScale = 1.0 / tan(fovRadians / 2.0);
float xScale = yScale / aspectRatio;
float zScale = far / (far - near);
float zTranslate = -near * zScale;
zgl_mat4x4_t projectionMatrix = (zgl_mat4x4_t) {{
{xScale, 0, 0, 0 },
{0, yScale, 0, 0 },
{0, 0, zScale, zTranslate},
{0, 0, 1, 0 }
}};
zgl_mat4x4_t viewProjMatrix = zgl_mul_mat(projectionMatrix, viewMatrix);
// Compute the view frustum planes
zgl_vec4_t viewProjMatrixCol0 = {viewProjMatrix.data[0][0], viewProjMatrix.data[0][1], viewProjMatrix.data[0][2], viewProjMatrix.data[0][3]};
zgl_vec4_t viewProjMatrixCol1 = {viewProjMatrix.data[1][0], viewProjMatrix.data[1][1], viewProjMatrix.data[1][2], viewProjMatrix.data[1][3]};
zgl_vec4_t viewProjMatrixCol2 = {viewProjMatrix.data[2][0], viewProjMatrix.data[2][1], viewProjMatrix.data[2][2], viewProjMatrix.data[2][3]};
zgl_vec4_t viewProjMatrixCol3 = {viewProjMatrix.data[3][0], viewProjMatrix.data[3][1], viewProjMatrix.data[3][2], viewProjMatrix.data[3][3]};
zgl_vec4_t leftPlane = zgl__normalize_plane(zgl_add_v4(viewProjMatrixCol3, viewProjMatrixCol0));
zgl_vec4_t rightPlane = zgl__normalize_plane(zgl_sub_v4(viewProjMatrixCol3, viewProjMatrixCol0));
zgl_vec4_t bottomPlane = zgl__normalize_plane(zgl_add_v4(viewProjMatrixCol3, viewProjMatrixCol1));
zgl_vec4_t topPlane = zgl__normalize_plane(zgl_sub_v4(viewProjMatrixCol3, viewProjMatrixCol1));
zgl_vec4_t nearPlane = zgl__normalize_plane(viewProjMatrixCol2);
zgl_vec4_t farPlane = zgl__normalize_plane(zgl_add_v4(viewProjMatrixCol3, viewProjMatrixCol2));
return (zgl_camera_t) {
.position = position,
.direction = direction,
.up = up,
.fov = fov,
.aspectRatio = aspectRatio,
.nearPlane = near,
.farPlane = far,
.viewMatrix = viewMatrix,
.projectionMatrix = projectionMatrix,
.viewProjMatrix = viewProjMatrix,
.fustrumPlanes = {leftPlane, rightPlane, bottomPlane, topPlane, nearPlane, farPlane},
.movementSpeed = movementSpeed,
.turningSpeed = turningSpeed
};
}
static inline int zgl__tri_in_fustrum(zgl_vec4_t v1, zgl_vec4_t v2, zgl_vec4_t v3) {
// Using NDC coordinates
if (v1.x < -1 && v2.x < -1 && v3.x < -1) return 0;
if (v1.x > 1 && v2.x > 1 && v3.x > 1) return 0;
if (v1.y < -1 && v2.y < -1 && v3.y < -1) return 0;
if (v1.y > 1 && v2.y > 1 && v3.y > 1) return 0;
if (v1.z < 0 && v2.z < 0 && v3.z < 0) return 0;
if (v1.z > 1 && v2.z > 1 && v3.z > 1) return 0;
return 1;
}
/* Rendering */
static inline int zgl__edge_cross(int ax, int ay, int bx, int by, int px, int py) {
int abx = bx - ax;
int aby = by - ay;
int apx = px - ax;
int apy = py - ay;
return abx * apy - aby * apx;
}
static inline void zgl_clear_depth_buffer(zgl_canvas_t canvas) {
for (int i = 0; i < canvas.width * canvas.height; i++) {
canvas.depthBuffer[i] = FLT_MAX;
}
}
static inline void zgl_render_pixel(int i, int j, float z, uint32_t color, zgl_canvas_t canvas) {
if ((i >= 0) && (i < canvas.width) && (j >= 0) && (j < canvas.height)) {
int position = j * canvas.width + i;
canvas.frameBuffer[position] = color;
canvas.depthBuffer[position] = z;
}
}
static inline void zgl_render_fill(uint32_t color, zgl_canvas_t canvas) {
for (int i = 0; i < canvas.width * canvas.height; i++) {
canvas.frameBuffer[i] = color;
}
}
static inline void zgl_render_line(int x0, int x1, int y0, int y1, uint32_t color, zgl_canvas_t canvas) {
int delta_x = (x1 - x0);
int delta_y = (y1 - y0);
int longest_side_length = (abs(delta_x) >= abs(delta_y)) ? abs(delta_x) : abs(delta_y);
float x_inc = delta_x / (float)longest_side_length;
float y_inc = delta_y / (float)longest_side_length;
float current_x = x0;
float current_y = y0;
for (int i = 0; i <= longest_side_length; i++) {
zgl_render_pixel(round(current_x), round(current_y), 0.0, color, canvas);
current_x += x_inc;
current_y += y_inc;
}
}
static inline void zgl_render_circle(int x, int y, int r, uint32_t color, zgl_canvas_t canvas) {
int x1 = x - r;