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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger                                  |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/
#include "gate/graphics/gl_apis.h"

#include "gate/debugging.h"
#include "gate/mathematics.h"

#if (defined(GATE_SYS_WIN) && !defined(GATE_SYS_WINCE) && !defined(GATE_SYS_WINSTORE) && !defined(GATE_SYS_WIN16)) || (defined(GATE_SYS_POSIX) && !defined(GATE_SYS_OPENBSD) && !defined(GATE_SYS_BEOS)) || defined(GATE_SYS_WASM)
#   define GATE_GRAPHICS_GL_OPENGL
#else
#   define GATE_GARPHICS_GL_NOIMPL
#endif

#if defined(GATE_GRAPHICS_GL_OPENGL)

#include "gate/graphics/platform/opengl_apis.h"

#if defined(GATE_GRAPHICS_OPENGL_GLUT)
#   define GATE_GL_API_GLUT 1
#elif defined(GATE_SYS_WIN) && !defined(GATE_SYS_WINCE)
#   define GATE_GL_API_WINAPI 1
#elif defined(GATE_SYS_DARWIN)
#   define GATE_GL_API_GLUT 1
#else
#   define GATE_GL_API_EGL 1
#endif



gate_result_t gate_gl_api_init()
{
    return gate_gl_api_load_functions();
}

void gate_gl_api_color3(gate_real32_t r, gate_real32_t g, gate_real32_t b)
{
    gate_gl_api_color4(r, g, b, 1.0f);
}
void gate_gl_api_color4(gate_real32_t r, gate_real32_t g, gate_real32_t b, gate_real32_t a)
{
    gate_gl_api.glColor4f(r, g, b, a);
}
void gate_gl_api_color3b(gate_uint8_t r, gate_uint8_t g, gate_uint8_t b)
{
    gate_gl_api_color4b(r, g, b, 255);
}
void gate_gl_api_color4b(gate_uint8_t r, gate_uint8_t g, gate_uint8_t b, gate_uint8_t a)
{
    gate_gl_api.glColor4f((gate_real32_t)r / 255.0f,
        (gate_real32_t)g / 255.0f,
        (gate_real32_t)b / 255.0f,
        (gate_real32_t)a / 255.0f);
}

void gate_gl_api_vertexpointer(gate_size_t coord_count, gate_real32_t const* ptr_coords)
{
    gate_gl_api.glVertexPointer((GLint)coord_count, GL_FLOAT, 0, ptr_coords);
}

void gate_gl_api_texcoordpointer(gate_size_t coord_count, gate_real32_t const* ptr_coords)
{
    gate_gl_api.glTexCoordPointer((GLint)coord_count, GL_FLOAT, 0, ptr_coords);
}

void gate_gl_api_shademodel(gate_gl_api_shademodel_t model)
{
    GLenum value = 0;
    switch (model)
    {
    case gate_gl_api_shademodel_flat:   value = GL_FLAT; break;
    case gate_gl_api_shademodel_smooth: value = GL_SMOOTH; break;
    }
    gate_gl_api.glShadeModel(value);
}

static GLenum resolve_blend_function(gate_gl_api_blend_t blend_function)
{
    switch (blend_function)
    {
    case gate_gl_api_blend_zero:                return GL_ZERO;
    case gate_gl_api_blend_one:                 return GL_ONE;
    case gate_gl_api_blend_src_color:           return GL_SRC_COLOR;
    case gate_gl_api_blend_one_minus_src_color: return GL_ONE_MINUS_SRC_COLOR;
    case gate_gl_api_blend_src_alpha:           return GL_SRC_ALPHA;
    case gate_gl_api_blend_one_minus_src_alpha: return GL_ONE_MINUS_SRC_ALPHA;
    case gate_gl_api_blend_dst_alpha:           return GL_DST_ALPHA;
    case gate_gl_api_blend_one_minus_dst_alpha: return GL_ONE_MINUS_DST_ALPHA;
    case gate_gl_api_blend_dst_color:           return GL_DST_COLOR;
    case gate_gl_api_blend_one_minus_dst_color: return GL_ONE_MINUS_DST_COLOR;
    case gate_gl_api_blend_src_alpha_saturate:  return GL_SRC_ALPHA_SATURATE;
    }
    return 0;
}

void gate_gl_api_blendfunc(gate_gl_api_blend_t blend_func_src, gate_gl_api_blend_t blend_func_dst)
{
    gate_gl_api.glBlendFunc(resolve_blend_function(blend_func_src), resolve_blend_function(blend_func_dst));
}

void gate_gl_api_clearcolor(gate_real32_t r, gate_real32_t g, gate_real32_t b, gate_real32_t a)
{
    gate_gl_api.glClearColor(r, g, b, a);
}

void gate_gl_api_cleardepth(gate_real32_t depth)
{
    gate_gl_api.glClearDepth(depth);
}

static GLenum translate_capability(gate_gl_api_capability_t capability)
{
    switch (capability)
    {
    case gate_gl_api_capability_alpha_test:             return GL_ALPHA_TEST;
#if defined(GL_AUTO_NORMAL)
    case gate_gl_api_capability_auto_normal:            return GL_AUTO_NORMAL;
#endif
    case gate_gl_api_capability_blend:                  return GL_BLEND;
    //case gate_gl_api_capability_clip_plane:             return GL_CLIP_PLANEi;
    case gate_gl_api_capability_color_logic_op:         return GL_COLOR_LOGIC_OP;
    case gate_gl_api_capability_color_material:         return GL_COLOR_MATERIAL;
    case gate_gl_api_capability_cull_face:              return GL_CULL_FACE;
    case gate_gl_api_capability_depth_test:             return GL_DEPTH_TEST;
    case gate_gl_api_capability_dither:                 return GL_DITHER;
    case gate_gl_api_capability_fog:                    return GL_FOG;
#if defined(GL_INDEX_LOGIC_OP)
    case gate_gl_api_capability_index_logic_op:         return GL_INDEX_LOGIC_OP;
#endif
    //case gate_gl_api_capability_light:                  return GL_LIGHTi;
    case gate_gl_api_capability_lighting:               return GL_LIGHTING;
    case gate_gl_api_capability_line_smooth:            return GL_LINE_SMOOTH;
#if defined(GL_LINE_STIPPLE)
    case gate_gl_api_capability_line_stipple:           return GL_LINE_STIPPLE;
#endif
#if defined(GL_LOGIC_OP)
    case gate_gl_api_capability_logic_op:               return GL_LOGIC_OP;
#endif
#if defined(GL_MAP1_COLOR_4)
    case gate_gl_api_capability_map1_color_4:           return GL_MAP1_COLOR_4;
#endif
#if defined(GL_MAP1_INDEX)
    case gate_gl_api_capability_map1_index:             return GL_MAP1_INDEX;
#endif
#if defined(GL_MAP1_NORMAL)
    case gate_gl_api_capability_map1_normal:            return GL_MAP1_NORMAL;
#endif
#if defined(GL_MAP1_TEXTURE_COORD_1)
    case gate_gl_api_capability_map1_texture_coord_1:   return GL_MAP1_TEXTURE_COORD_1;
#endif
#if defined(GL_MAP1_TEXTURE_COORD_2)
    case gate_gl_api_capability_map1_texture_coord_2:   return GL_MAP1_TEXTURE_COORD_2;
#endif
#if defined(GL_MAP1_TEXTURE_COORD_3)
    case gate_gl_api_capability_map1_texture_coord_3:   return GL_MAP1_TEXTURE_COORD_3;
#endif
#if defined(GL_MAP1_TEXTURE_COORD_4)
    case gate_gl_api_capability_map1_texture_coord_4:   return GL_MAP1_TEXTURE_COORD_4;
#endif
#if defined(GL_MAP1_VERTEX_3)
    case gate_gl_api_capability_map1_vertex_3:          return GL_MAP1_VERTEX_3;
#endif
#if defined(GL_MAP1_VERTEX_4)
    case gate_gl_api_capability_map1_vertex_4:          return GL_MAP1_VERTEX_4;
#endif
#if defined(GL_MAP2_COLOR_4)
    case gate_gl_api_capability_map2_color_4:           return GL_MAP2_COLOR_4;
#endif
#if defined(GL_MAP2_INDEX)
    case gate_gl_api_capability_map2_index:             return GL_MAP2_INDEX;
#endif
#if defined(GL_MAP2_NORMAL)
    case gate_gl_api_capability_map2_normal:            return GL_MAP2_NORMAL;
#endif
#if defined(GL_MAP2_TEXTURE_COORD_1)
    case gate_gl_api_capability_map2_texture_coord_1:   return GL_MAP2_TEXTURE_COORD_1;
#endif
#if defined(GL_MAP2_TEXTURE_COORD_2)
    case gate_gl_api_capability_map2_texture_coord_2:   return GL_MAP2_TEXTURE_COORD_2;
#endif
#if defined(GL_MAP2_TEXTURE_COORD_3)
    case gate_gl_api_capability_map2_texture_coord_3:   return GL_MAP2_TEXTURE_COORD_3;
#endif
#if defined(GL_MAP2_TEXTURE_COORD_4)
    case gate_gl_api_capability_map2_texture_coord_4:   return GL_MAP2_TEXTURE_COORD_4;
#endif
#if defined(GL_MAP2_VERTEX_3)
    case gate_gl_api_capability_map2_vertex_3:          return GL_MAP2_VERTEX_3;
#endif
#if defined(GL_MAP2_VERTEX_4)
    case gate_gl_api_capability_map2_vertex_4:          return GL_MAP2_VERTEX_4;
#endif
    case gate_gl_api_capability_normalize:              return GL_NORMALIZE;
    case gate_gl_api_capability_point_smooth:           return GL_POINT_SMOOTH;
    case gate_gl_api_capability_polygon_offset_fill:    return GL_POLYGON_OFFSET_FILL;
#if defined(GL_POLYGON_OFFSET_LINE)
    case gate_gl_api_capability_polygon_offset_line:    return GL_POLYGON_OFFSET_LINE;
#endif
#if defined(GL_POLYGON_OFFSET_POINT)
    case gate_gl_api_capability_polygon_offset_point:   return GL_POLYGON_OFFSET_POINT;
#endif
#if defined(GL_POLYGON_SMOOTH)
    case gate_gl_api_capability_polygon_smooth:         return GL_POLYGON_SMOOTH;
#endif
#if defined(GL_POLYGON_STIPPLE)
    case gate_gl_api_capability_polygon_stipple:        return GL_POLYGON_STIPPLE;
#endif
    case gate_gl_api_capability_scissor_test:           return GL_SCISSOR_TEST;
    case gate_gl_api_capability_stencil_test:           return GL_STENCIL_TEST;
#if defined(GL_TEXTURE_1D)
    case gate_gl_api_capability_texture_1d:             return GL_TEXTURE_1D;
#endif
    case gate_gl_api_capability_texture_2d:             return GL_TEXTURE_2D;
#if defined(GL_TEXTURE_GEN_Q)
    case gate_gl_api_capability_texture_gen_q:          return GL_TEXTURE_GEN_Q;
#endif
#if defined(GL_TEXTURE_GEN_R)
    case gate_gl_api_capability_texture_gen_r:          return GL_TEXTURE_GEN_R;
#endif
#if defined(GL_TEXTURE_GEN_S)
    case gate_gl_api_capability_texture_gen_s:          return GL_TEXTURE_GEN_S;
#endif
#if defined(GL_TEXTURE_GEN_T)
    case gate_gl_api_capability_texture_gen_t:          return GL_TEXTURE_GEN_T;
#endif
    default:                                            return 0;
    }
}


void gate_gl_api_enable(gate_gl_api_capability_t capability)
{
    gate_gl_api.glEnable(translate_capability(capability));
}

void gate_gl_api_disable(gate_gl_api_capability_t capability)
{
    gate_gl_api.glDisable(translate_capability(capability));
}

void gate_gl_api_depthfunc(gate_gl_api_depth_t depth_function)
{
    GLenum value = 0;
    switch (depth_function)
    {
    case gate_gl_api_depth_never:   value = GL_NEVER;   break;
    case gate_gl_api_depth_less:    value = GL_LESS;    break;
    case gate_gl_api_depth_lequal:  value = GL_LEQUAL;  break;
    case gate_gl_api_depth_equal:   value = GL_EQUAL;   break;
    case gate_gl_api_depth_greater: value = GL_GREATER; break;
    case gate_gl_api_depth_notequal:value = GL_NOTEQUAL;break;
    case gate_gl_api_depth_gequal:  value = GL_GEQUAL;  break;
    case gate_gl_api_depth_always:  value = GL_ALWAYS;  break;
    }
    gate_gl_api.glDepthFunc(value);
}

void gate_gl_api_depthmask(gate_bool_t enabled)
{
    gate_gl_api.glDepthMask(enabled ? GL_TRUE : GL_FALSE);
}

void gate_gl_api_hint(gate_gl_api_hint_t hint_type, gate_gl_api_hintmode_t hint_mode)
{
    GLenum ht = 0, hm = 0;
    switch (hint_type)
    {
    case gate_gl_api_hint_fog_hint:                     ht = GL_FOG_HINT;                   break;
    case gate_gl_api_hint_line_smooth_hint:             ht = GL_LINE_SMOOTH_HINT;           break;
    case gate_gl_api_hint_perspective_correction_hint:  ht = GL_PERSPECTIVE_CORRECTION_HINT;break;
    case gate_gl_api_hint_point_smooth_hint:            ht = GL_POINT_SMOOTH_HINT;          break;
#if defined(GL_POLYGON_SMOOTH_HINT)
    case gate_gl_api_hint_polygon_smooth_hint:          ht = GL_POLYGON_SMOOTH_HINT;        break;
#endif
    default:                                            ht = 0; break;
    }

    switch (hint_mode)
    {
    case gate_gl_api_hintmode_fastest:      hm = GL_FASTEST;    break;
    case gate_gl_api_hintmode_nicest:       hm = GL_NICEST;     break;
    case gate_gl_api_hintmode_dont_care:    hm = GL_DONT_CARE;  break;
    }

    gate_gl_api.glHint(ht, hm);
}

void gate_gl_api_viewport(gate_int32_t x, gate_int32_t y, gate_int32_t width, gate_int32_t height)
{
    gate_gl_api.glViewport(x, y, width, height);
}

void gate_gl_api_matrixmode(gate_gl_api_matrixmode_t matrix_mode)
{
    GLenum mm = 0;
    switch (matrix_mode)
    {
    case gate_gl_api_matrixmode_modelview:  mm = GL_MODELVIEW;  break;
    case gate_gl_api_matrixmode_projection: mm = GL_PROJECTION; break;
    case gate_gl_api_matrixmode_texture:    mm = GL_TEXTURE;    break;
    }
    gate_gl_api.glMatrixMode(mm);
}

void gate_gl_api_loadidentity()
{
    gate_gl_api.glLoadIdentity();
}

void gate_gl_api_multmatrix(gate_real32_t const matrix[16])
{
    gate_gl_api.glMultMatrixf(matrix);
}

void gate_gl_api_translate(gate_real32_t x, gate_real32_t y, gate_real32_t z)
{
    gate_gl_api.glTranslatef(x, y, z);
}

void gate_gl_api_rotate(gate_real32_t angle, gate_real32_t x, gate_real32_t y, gate_real32_t z)
{
    gate_gl_api.glRotatef(angle, x, y, z);
}


void gate_gl_api_flush()
{
    gate_gl_api.glFlush();
}

void gate_gl_api_finish()
{
    gate_gl_api.glFinish();
}

void gate_gl_api_clear(unsigned clear_bits)
{
    GLbitfield bf = 0;

    if (clear_bits & gate_gl_api_clearbit_color_buffer_bit)
    {
        bf |= GL_COLOR_BUFFER_BIT;
    }
    if (clear_bits & gate_gl_api_clearbit_depth_buffer_bit)
    {
        bf |= GL_DEPTH_BUFFER_BIT;
    }
    if (clear_bits & gate_gl_api_clearbit_accum_buffer_bit)
    {
#if defined(GL_ACCUM_BUFFER_BIT)
        bf |= GL_ACCUM_BUFFER_BIT;
#endif
    }
    if (clear_bits & gate_gl_api_clearbit_stencil_buffer_bit)
    {
        bf |= GL_STENCIL_BUFFER_BIT;
    }
    gate_gl_api.glClear(bf);
}

void gate_gl_api_perspective(gate_real32_t fovy, gate_real32_t aspect, gate_real32_t znear, gate_real32_t zfar)
{
    gate_real32_t f = (gate_real32_t)(1.0 / gate_math_tan(gate_math_deg2rad(fovy) / 2.0));
    GLfloat a = f / aspect;
    GLfloat m[16];
    m[0] = a; m[1] = 0; m[2] = 0; m[3] = 0;
    m[4] = 0; m[5] = f; m[6] = 0; m[7] = 0;
    m[8] = 0; m[9] = 0; m[10] = (zfar + znear) / (znear - zfar); m[11] = -1.0f;
    m[12] = 0; m[13] = 0; m[14] = (2.0f * zfar * znear) / (znear - zfar); m[15] = 0;
    gate_gl_api.glMultMatrixf(m);
}


static void math_cross_prod(float x1, float y1, float z1, float x2, float y2, float z2, float res[3])
{
    res[0] = y1 * z2 - y2 * z1;
    res[1] = x2 * z1 - x1 * z2;
    res[2] = x1 * y2 - x2 * y1;
}

/* https://www.khronos.org/message_boards/showthread.php/4991-The-Solution-for-gluLookAt%28%29-Function!!!! */
/* https://community.khronos.org/t/the-solution-for-glulookat-function/521 */

static double square(double const arg)
{
    return arg * arg;
}


void gate_gl_api_lookat(gate_real32_t eyeX, gate_real32_t eyeY, gate_real32_t eyeZ,
    gate_real32_t lookX, gate_real32_t lookY, gate_real32_t lookZ,
    gate_real32_t upX, gate_real32_t upY, gate_real32_t upZ)
{
    float f[3];
    float fMag;
    float upMag;
    float s[3], u[3];
    float M[16];


    f[0] = lookX - eyeX;
    f[1] = lookY - eyeY;
    f[2] = lookZ - eyeZ;

    fMag = (float)gate_math_sqrt(square(f[0]) + square(f[1]) + square(f[2]));
    upMag = (float)gate_math_sqrt(square(upX) + square(upY) + square(upZ));

    if (fMag != 0)
    {
        f[0] = f[0] / fMag;
        f[1] = f[1] / fMag;
        f[2] = f[2] / fMag;
    }

    if (upMag != 0)
    {
        upX = upX / upMag;
        upY = upY / upMag;
        upZ = upZ / upMag;
    }

    math_cross_prod(f[0], f[1], f[2], upX, upY, upZ, s);
    math_cross_prod(s[0], s[1], s[2], f[0], f[1], f[2], u);

    M[0] = s[0]; M[1] = u[0]; M[2] = -f[0]; M[3] = 0;
    M[4] = s[1]; M[5] = u[1]; M[6] = -f[1]; M[7] = 0;
    M[8] = s[2]; M[9] = u[2]; M[10] = -f[2]; M[11] = 0;
    M[12] = 0; M[13] = 0; M[14] = 0; M[15] = 1;

    gate_gl_api.glMultMatrixf(M);
    gate_gl_api.glTranslatef(-eyeX, -eyeY, -eyeZ);
}

void gate_gl_api_ortho(gate_real32_t left, gate_real32_t right, gate_real32_t bottom, gate_real32_t top, gate_real32_t near_val, gate_real32_t far_val)
{
    gate_gl_api.glOrtho(left, right, bottom, top, near_val, far_val);
}


static GLenum translate_clientstate(gate_gl_api_clientstate_t client_state)
{
    switch (client_state)
    {
    case gate_gl_api_clientstate_color_array:           return GL_COLOR_ARRAY;
#if defined(GL_EDGE_FLAG_ARRAY)
    case gate_gl_api_clientstate_edge_flag_array:       return GL_EDGE_FLAG_ARRAY;
#endif
#if defined(GL_INDEX_ARRAY)
    case gate_gl_api_clientstate_index_array:           return GL_INDEX_ARRAY;
#endif
    case gate_gl_api_clientstate_normal_array:          return GL_NORMAL_ARRAY;
    case gate_gl_api_clientstate_texture_coord_array:   return GL_TEXTURE_COORD_ARRAY;
    case gate_gl_api_clientstate_vertex_array:          return GL_VERTEX_ARRAY;
    default:                                            return 0;
    }
}

void gate_gl_api_enableclientstate(gate_gl_api_clientstate_t state)
{
    gate_gl_api.glEnableClientState(translate_clientstate(state));
}

void gate_gl_api_disableclientstate(gate_gl_api_clientstate_t state)
{
    gate_gl_api.glDisableClientState(translate_clientstate(state));
}

static GLenum translate_draw_mode(gate_gl_api_drawmode_t draw_mode)
{
    GLenum ret = 0;
    switch (draw_mode)
    {
    case gate_gl_api_drawmode_points:           ret = GL_POINTS;            break;
    case gate_gl_api_drawmode_lines:            ret = GL_LINES;             break;
    case gate_gl_api_drawmode_line_loop:        ret = GL_LINE_LOOP;         break;
    case gate_gl_api_drawmode_line_strip:       ret = GL_LINE_STRIP;        break;
    case gate_gl_api_drawmode_triangles:        ret = GL_TRIANGLES;         break;
    case gate_gl_api_drawmode_triangle_strip:   ret = GL_TRIANGLE_STRIP;    break;
    case gate_gl_api_drawmode_triangle_fan:     ret = GL_TRIANGLE_FAN;      break;
#if defined(GL_QUADS)
    case gate_gl_api_drawmode_quads:            ret = GL_QUADS;             break;
#endif
#if defined(GL_QUAD_STRIP)
    case gate_gl_api_drawmode_quad_strip:       ret = GL_QUAD_STRIP;        break;
#endif
#if defined(GL_POLYGON)
    case gate_gl_api_drawmode_polygon:          ret = GL_POLYGON;           break;
#endif
    default:                                    ret = 0;                    break;
    }
    return ret;
}

void gate_gl_api_drawarrays(gate_gl_api_drawmode_t draw_mode, int index, size_t count)
{
    gate_gl_api.glDrawArrays(translate_draw_mode(draw_mode), (GLint)index, (GLsizei)count);
}

void* gate_gl_api_createtexture(gate_gl_api_pixelformat_t pixel_format,
    gate_uint32_t width, gate_uint32_t height, void const* data)
{
    GLenum format;
    GLenum datatype;
    GLuint tex;
    GLenum paramFilter = GL_LINEAR;
    GLint internalFormat =
#if defined(GL_RGBA8)
        GL_RGBA8;
#elif defined(GL_RGB8)
        GL_RGB8;
#else
        GL_RGBA;
#endif

    switch (pixel_format)
    {
    case gate_gl_api_pixelformat_rgb:
        {
            format = GL_RGB;
            datatype = GL_UNSIGNED_BYTE;
            break;
        }
    case gate_gl_api_pixelformat_rgba:
        {
            format = GL_RGBA;
            datatype = GL_UNSIGNED_BYTE;
            break;
        }
    default:
        {
            /* not supported */
            return 0;
        }
    }

    gate_gl_api.glEnable(GL_TEXTURE_2D);
    gate_gl_api.glGenTextures(1, &tex);
    gate_gl_api.glBindTexture(GL_TEXTURE_2D, tex);

    gate_gl_api.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, paramFilter);
    gate_gl_api.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, paramFilter);

    gate_gl_api.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    gate_gl_api.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    gate_gl_api.glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, datatype, data);
    gate_gl_api.glDisable(GL_TEXTURE_2D);

    return (void*)(gate_uintptr_t)(tex);
}

void gate_gl_api_deletetexture(void* texture_id)
{
    GLuint tex = (GLuint)(gate_uintptr_t)(texture_id);
    gate_gl_api.glDeleteTextures(1, &tex);
}

void gate_gl_api_bindtexture(void* texture_id)
{
    GLuint tex = (GLuint)(gate_uintptr_t)texture_id;
    gate_gl_api.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
    gate_gl_api.glBindTexture(GL_TEXTURE_2D, tex);
}

void gate_gl_api_cullface(gate_gl_api_cullface_t mode)
{
    GLenum value = GL_FRONT_AND_BACK;
    switch (mode)
    {
    case gate_gl_api_cullface_front:            value = GL_FRONT; break;
    case gate_gl_api_cullface_back:             value = GL_BACK; break;
    case gate_gl_api_cullface_front_and_back:   value = GL_FRONT_AND_BACK; break;
    }
    gate_gl_api.glCullFace(value);
}



#if defined(GATE_GRAPHICS_OPENGL2_SUPPORT)

gate_uintptr_t gate_gl2_api_create_shader(gate_gl2_api_shader_type_t type)
{
    GLenum itype = GL_VERTEX_SHADER;
    switch(type)
    {
    case gate_gl2_api_shader_type_compute:          itype = GL_COMPUTE_SHADER; break;
    case gate_gl2_api_shader_type_vertex:           itype = GL_VERTEX_SHADER; break;
    case gate_gl2_api_shader_type_tess_control:     itype = GL_TESS_CONTROL_SHADER; break;
    case gate_gl2_api_shader_type_tess_evaluation:  itype = GL_TESS_EVALUATION_SHADER; break;
    case gate_gl2_api_shader_type_geometry:         itype = GL_GEOMETRY_SHADER; break;
    case gate_gl2_api_shader_type_fragment:         itype = GL_FRAGMENT_SHADER; break;
    }
    return gate_gl2_api.glCreateShader(itype);
}
void gate_gl2_api_shader_source(gate_uintptr_t shader, gate_size_t count, char const * const * code, gate_size_t const* length)
{
    GLint const iLength = length ? (GLint)*length : 0;
    gate_gl2_api.glShaderSource((GLuint)shader, (GLsizei)count, code, length ? &iLength : NULL);
}
void gate_gl2_api_compile_shader(gate_uintptr_t shader)
{
    gate_gl2_api.glCompileShader((GLuint)shader);
}
void gate_gl2_api_get_shader_iv(gate_uintptr_t shader, gate_gl2_api_shader_param_t param_name, gate_intptr_t* params)
{
    GLenum pname = GL_SHADER_TYPE;
    GLint iparam = (GLint)*params;
    switch(param_name)
    {
    case gate_gl2_api_shader_param_type:            pname = GL_SHADER_TYPE; break;
    case gate_gl2_api_shader_param_delete_status:   pname = GL_DELETE_STATUS; break;
    case gate_gl2_api_shader_param_compile_status:  pname = GL_COMPILE_STATUS; break;
    case gate_gl2_api_shader_param_info_log_length: pname = GL_INFO_LOG_LENGTH; break;
    case gate_gl2_api_shader_param_source_length:   pname = GL_SHADER_SOURCE_LENGTH; break;
    }
    gate_gl2_api.glGetShaderiv(shader, pname, &iparam);
    *params = (gate_intptr_t)iparam;
}
void gate_gl2_api_get_shader_info_log(gate_uintptr_t shader, gate_size_t maxLength, gate_size_t* length, char* infoLog)
{
    GLsizei max_length = (GLsizei)maxLength;
    GLsizei len = max_length;
    GLchar* ptr_info = (GLchar*)infoLog;
    gate_gl2_api.glGetShaderInfoLog((GLuint)shader, max_length, &len, ptr_info);
}
void gate_gl2_api_delete_shader(gate_uintptr_t shader)
{
    gate_gl2_api.glDeleteShader((GLuint)shader);
}


gate_uintptr_t gate_gl2_api_create_program(void)
{
    return gate_gl2_api.glCreateProgram();
}
void gate_gl2_api_link_program(gate_uintptr_t program)
{
    gate_gl2_api.glLinkProgram((GLuint)program);
}
void gate_gl2_api_get_program_iv(gate_uintptr_t program, gate_gl2_api_program_param_t param_name, gate_intptr_t* params)
{
    GLenum pname = GL_LINK_STATUS;
    GLint iparams = (GLint)*params;
    switch(param_name)
    {
    case gate_gl2_api_program_param_active_attributes:                      pname = GL_ACTIVE_ATTRIBUTES; break;
    case gate_gl2_api_program_param_active_attribute_max_length:            pname = GL_ACTIVE_ATTRIBUTE_MAX_LENGTH; break;
    case gate_gl2_api_program_param_active_uniforms:                        pname = GL_ACTIVE_UNIFORMS; break;
    case gate_gl2_api_program_param_active_uniform_blocks:                  pname = GL_ACTIVE_UNIFORM_BLOCKS; break;
    case gate_gl2_api_program_param_active_uniform_block_max_name_length:   pname = GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH; break;
    case gate_gl2_api_program_param_active_uniform_max_length:              pname = GL_ACTIVE_UNIFORM_MAX_LENGTH; break;
    case gate_gl2_api_program_param_attached_shaders:                       pname = GL_ATTACHED_SHADERS; break;
    case gate_gl2_api_program_param_delete_status:                          pname = GL_DELETE_STATUS; break;
    case gate_gl2_api_program_param_info_log_length:                        pname = GL_INFO_LOG_LENGTH; break;
    case gate_gl2_api_program_param_link_status:                            pname = GL_LINK_STATUS; break;
    case gate_gl2_api_program_param_program_binary_retrievable_hint:        pname = GL_PROGRAM_BINARY_RETRIEVABLE_HINT; break;
    case gate_gl2_api_program_param_transform_feedback_buffer_mode:         pname = GL_TRANSFORM_FEEDBACK_BUFFER_MODE; break;
    case gate_gl2_api_program_param_transform_feedback_varyings:            pname = GL_TRANSFORM_FEEDBACK_VARYINGS; break;
    case gate_gl2_api_program_param_transform_feedback_varying_max_length:  pname = GL_TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH; break;
    case gate_gl2_api_program_param_validate_status:                        pname = GL_VALIDATE_STATUS; break;
    }
    gate_gl2_api.glGetProgramiv(program, pname, &iparams);
    *params = (gate_intptr_t)iparams;
}
void gate_gl2_api_get_program_info_log(gate_uintptr_t program, gate_size_t bufSize, gate_size_t* length, char* infoLog)
{
    GLsizei len = *length;
    gate_gl2_api.glGetProgramInfoLog((GLuint)program, (GLsizei)bufSize, &len, infoLog);
    *length = len;
}

void gate_gl2_api_use_program(gate_uintptr_t program)
{
    gate_gl2_api.glUseProgram((GLuint)program);
}
void gate_gl2_api_attach_shader(gate_uintptr_t program, gate_uintptr_t shader)
{
    gate_gl2_api.glAttachShader((GLuint)program, (GLuint)shader);
}
void gate_gl2_api_delete_program(gate_uintptr_t program)
{
    gate_gl2_api.glDeleteProgram((GLuint)program);
}
void gate_gl2_api_bind_attrib_location(gate_uintptr_t program, gate_size_t index, char const* name)
{
    gate_gl2_api.glBindAttribLocation((GLuint)program, (GLuint)index, name);
}
gate_intptr_t gate_gl2_api_get_attrib_location(gate_uintptr_t program, char const* name)
{
    return gate_gl2_api.glGetAttribLocation((GLuint)program, name);
}
gate_intptr_t gate_gl2_api_get_uniform_location(gate_uintptr_t program, char const* name)
{
    return gate_gl2_api.glGetUniformLocation((GLuint)program, name);
}


void gate_gl2_api_gen_buffers(gate_size_t n, unsigned int* buffers)
{
    gate_gl2_api.glGenBuffers((GLsizei)n, buffers);
}
void gate_gl2_api_delete_buffers(gate_size_t n, unsigned int const * buffers)
{
    gate_gl2_api.glDeleteBuffers((GLsizei)n, buffers);
}

static GLenum convert_target(gate_gl2_api_buffer_type_t target_type)
{
    switch(target_type)
    {
    default:
    case gate_gl2_api_buffer_type_array:                return GL_ARRAY_BUFFER;
    case gate_gl2_api_buffer_type_atomic_counter:       return GL_ATOMIC_COUNTER_BUFFER;
    case gate_gl2_api_buffer_type_copy_read:            return GL_COPY_READ_BUFFER;
    case gate_gl2_api_buffer_type_copy_write:           return GL_COPY_WRITE_BUFFER;
    case gate_gl2_api_buffer_type_dispatch_indirect:    return GL_DISPATCH_INDIRECT_BUFFER;
    case gate_gl2_api_buffer_type_draw_indirect:        return GL_DRAW_INDIRECT_BUFFER;
    case gate_gl2_api_buffer_type_element_array:        return GL_ELEMENT_ARRAY_BUFFER;
    case gate_gl2_api_buffer_type_pixel_pack:           return GL_PIXEL_PACK_BUFFER;
    case gate_gl2_api_buffer_type_pixel_unpack:         return GL_PIXEL_UNPACK_BUFFER;
    case gate_gl2_api_buffer_type_query:                return GL_QUERY_BUFFER;
    case gate_gl2_api_buffer_type_shader_storage:       return GL_SHADER_STORAGE_BUFFER;
    case gate_gl2_api_buffer_type_texture:              return GL_TEXTURE_BUFFER;
    case gate_gl2_api_buffer_type_transform_feedback:   return GL_TRANSFORM_FEEDBACK_BUFFER;
    case gate_gl2_api_buffer_type_uniform:              return GL_UNIFORM_BUFFER;
    }
}
void gate_gl2_api_bind_buffers(gate_gl2_api_buffer_type_t target_type, unsigned int buffer)
{
    GLenum target = convert_target(target_type);
    return gate_gl2_api.glBindBuffer(target, buffer);
}
static GLenum convert_usage(gate_gl2_api_usage_type_t usage_value)
{
    switch(usage_value)
    {
    default:
    case gate_gl2_api_usage_type_stream_draw:   return GL_STREAM_DRAW;
    case gate_gl2_api_usage_type_stream_read:   return GL_STREAM_READ;
    case gate_gl2_api_usage_type_stream_copy:   return GL_STREAM_COPY;
    case gate_gl2_api_usage_type_static_draw:   return GL_STATIC_DRAW;
    case gate_gl2_api_usage_type_static_read:   return GL_STATIC_READ;
    case gate_gl2_api_usage_type_static_copy:   return GL_STATIC_COPY;
    case gate_gl2_api_usage_type_dynamic_draw:  return GL_DYNAMIC_DRAW;
    case gate_gl2_api_usage_type_dynamic_read:  return GL_DYNAMIC_READ;
    case gate_gl2_api_usage_type_dynamic_copy:  return GL_DYNAMIC_COPY;
    }
}
void gate_gl2_api_buffer_data(gate_gl2_api_buffer_type_t target_type, gate_size_t size, void const* data, gate_gl2_api_usage_type_t usage_value)
{
    GLenum target = convert_target(target_type);
    GLenum usage = convert_usage(usage_value);
    return gate_gl2_api.glBufferData(target, size, data, usage);
}
/*
void gate_gl2_api_named_buffer_data(unsigned int buffer, gate_size_t size, void const* data, gate_gl2_api_usage_type_t usage_value)
{
    GLenum usage = convert_usage(usage_value);
    gate_gl2_api.glNamedBufferData(buffer, size, data, usage);
}
*/
void gate_gl2_api_vertex_attrib_pointer(gate_uintptr_t index, gate_size_t size, gate_gl2_api_vertex_attrib_type_t type, gate_bool_t normalized, gate_size_t stride, void const* pointer)
{
    GLenum vtype = GL_INT;
    GLboolean bnorm = normalized ? GL_TRUE : GL_FALSE;
    switch(type)
    {
    case gate_gl2_api_vertex_attrib_byte:                           vtype = GL_BYTE; break;
    case gate_gl2_api_vertex_attrib_unsigned_byte:                  vtype = GL_UNSIGNED_BYTE; break;
    case gate_gl2_api_vertex_attrib_short:                          vtype = GL_SHORT; break;
    case gate_gl2_api_vertex_attrib_unsigned_short:                 vtype = GL_UNSIGNED_SHORT; break;
    case gate_gl2_api_vertex_attrib_int:                            vtype = GL_INT; break;
    case gate_gl2_api_vertex_attrib_half_float:                     vtype = GL_HALF_FLOAT; break;
    case gate_gl2_api_vertex_attrib_float:                          vtype = GL_FLOAT; break;
    case gate_gl2_api_vertex_attrib_double:                         vtype = GL_DOUBLE; break;
    case gate_gl2_api_vertex_attrib_fixed:                          vtype = GL_FIXED; break;
    case gate_gl2_api_vertex_attrib_int_2_10_10_10_rev:             vtype = GL_INT_2_10_10_10_REV; break;
    case gate_gl2_api_vertex_attrib_unsigned_int_2_10_10_10_rev:    vtype = GL_UNSIGNED_INT_2_10_10_10_REV; break;
    case gate_gl2_api_vertex_attrib_unsigned_int_10F_11F_11F_rev:   vtype = GL_UNSIGNED_INT_10F_11F_11F_REV; break;
    }

    gate_gl2_api.glVertexAttribPointer((GLuint)index, (GLint)size, vtype, bnorm, (GLsizei)stride, pointer);
}

void gate_gl2_api_enable_vertex_attrib_array(gate_uintptr_t index)
{
    gate_gl2_api.glEnableVertexAttribArray((GLuint)index);
}
void gate_gl2_api_disable_vertex_attrib_array(gate_uintptr_t index)
{
    gate_gl2_api.glDisableVertexAttribArray((GLuint)index);
}
void gate_gl2_api_active_texture(gate_uintptr_t texture_index)
{
    GLenum param = GL_TEXTURE0 + texture_index;
    gate_gl2_api.glActiveTexture(param);
}


void gate_gl2_api_uniform1f(gate_intptr_t location, gate_real32_t v0)
{
    gate_gl2_api.glUniform1f(location, v0);
}
void gate_gl2_api_uniform2f(gate_intptr_t location, gate_real32_t v0, gate_real32_t v1)
{
    gate_gl2_api.glUniform2f(location, v0, v1);
}
void gate_gl2_api_uniform3f(gate_intptr_t location, gate_real32_t v0, gate_real32_t v1, gate_real32_t v2)
{
    gate_gl2_api.glUniform3f(location, v0, v1, v2);
}
void gate_gl2_api_uniform4f(gate_intptr_t location, gate_real32_t v0, gate_real32_t v1, gate_real32_t v2, gate_real32_t v3)
{
    gate_gl2_api.glUniform4f(location, v0, v1, v2, v3);
}
void gate_gl2_api_uniform1fv(gate_intptr_t location, gate_size_t count,const gate_real32_t *value)
{
    gate_gl2_api.glUniform1fv(location, count, value);
}
void gate_gl2_api_uniform2fv(gate_intptr_t location, gate_size_t count, const gate_real32_t *value)
{
    gate_gl2_api.glUniform2fv(location, count, value);
}
void gate_gl2_api_uniform3fv(gate_intptr_t location, gate_size_t count, const gate_real32_t *value)
{
    gate_gl2_api.glUniform3fv(location, count, value);
}
void gate_gl2_api_uniform4fv(gate_intptr_t location, gate_size_t count, const gate_real32_t *value)
{
    gate_gl2_api.glUniform4fv(location, count, value);
}
void gate_gl2_api_uniform1i(gate_intptr_t location, int v0)
{
    gate_gl2_api.glUniform1i(location, v0);
}
void gate_gl2_api_uniform2i(gate_intptr_t location, int v0, int v1)
{
    gate_gl2_api.glUniform2i(location, v0, v1);
}
void gate_gl2_api_uniform3i(gate_intptr_t location, int v0, int v1, int v2)
{
    gate_gl2_api.glUniform3i(location, v0, v1, v2);
}
void gate_gl2_api_uniform4i(gate_intptr_t location, int v0, int v1, int v2, int v3)
{
    gate_gl2_api.glUniform4i(location, v0, v1, v2, v3);
}
void gate_gl2_api_uniform1iv(gate_intptr_t location, gate_size_t count, const int *value)
{
    gate_gl2_api.glUniform1iv(location, count, value);
}
void gate_gl2_api_uniform2iv(gate_intptr_t location, gate_size_t count, const int *value)
{
    gate_gl2_api.glUniform2iv(location, count, value);
}
void gate_gl2_api_uniform3iv(gate_intptr_t location, gate_size_t count, const int *value)
{
    gate_gl2_api.glUniform3iv(location, count, value);
}
void gate_gl2_api_uniform4iv(gate_intptr_t location, gate_size_t count, const int *value)
{
    gate_gl2_api.glUniform4iv(location, count, value);
}


void gate_gl2_api_uniform_matrix2fv(gate_intptr_t location, gate_size_t count, gate_bool_t transpose, const gate_real32_t *value)
{
    gate_gl2_api.glUniformMatrix2fv(location, count, transpose, value);
}
void gate_gl2_api_uniform_matrix3fv(gate_intptr_t location, gate_size_t count, gate_bool_t transpose, const gate_real32_t *value)
{
    gate_gl2_api.glUniformMatrix3fv(location, count, transpose, value);
}
void gate_gl2_api_uniform_matrix4fv(gate_intptr_t location, gate_size_t count, gate_bool_t transpose, const gate_real32_t *value)
{
    gate_gl2_api.glUniformMatrix4fv(location, count, transpose, value);
}



#endif /* GATE_GRAPHICS_OPENGL2_SUPPORT */



#if defined(GATE_GL_API_WINAPI)

#include "gate/graphics/platform/opengl_winapi_impl.h"

#endif /* GATE_GL_API_WINAPI */



#if defined(GATE_GL_API_GLUT)

#include "gate/graphics/platform/opengl_glut_impl.h"

#endif /* GATE_GL_API_GLUT */



#if defined(GATE_GL_API_EGL)

#   include "gate/graphics/platform/opengl_egl_apis.h" 

#   if defined(GATE_SYS_WINCE)
#   elif defined(GATE_SYS_WASM)
#       include "gate/graphics/platform/opengl_emscripten_impl.h"
#   elif defined(GATE_SYS_ANDROID)
#       include "gate/graphics/platform/opengl_android_impl.h"
#   else
#       include "gate/graphics/platform/opengl_xlib_impl.h"
#   endif

#endif /* GATE_GL_API_EGL */


#endif /* GATE_GRAPHICS_GL_OPENGL */



#if defined(GATE_GARPHICS_GL_NOIMPL)


gate_result_t gate_gl_surface_init(gate_gl_surface_t* surface)
{
    (void)surface;
    return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_gl_surface_uninit(gate_gl_surface_t* surface)
{
    (void)surface;
    return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_gl_surface_open(gate_gl_surface_t* surface, gate_gl_surface_type_t type,
    gate_uint32_t width, gate_uint32_t height, gate_enumint_t flags)
{
    (void)surface;
    (void)type;
    (void)width;
    (void)height;
    (void)flags;
    return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_gl_surface_close(gate_gl_surface_t* surface)
{
    (void)surface;
    return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_gl_surface_resize(gate_gl_surface_t* surface, gate_uint32_t width, gate_uint32_t height)
{
    (void)surface;
    (void)width;
    (void)height;
    return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_gl_surface_get_size(gate_gl_surface_t* surface, gate_uint32_t* width, gate_uint32_t* height)
{
    (void)surface;
    (void)width;
    (void)height;
    return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_gl_surface_print_image(gate_gl_surface_t* surface, gate_rasterimage_t* target_image)
{
    (void)surface;
    (void)target_image;
    return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_gl_surface_run_event_loop(gate_gl_surface_t* surface,
    gate_gl_surface_events_t* event_callbacks, void* user_param)
{
    (void)surface;
    (void)event_callbacks;
    (void)user_param;
    return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_gl_surface_exit_event_loop(gate_gl_surface_t* surface)
{
    (void)surface;
    return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_gl_surface_swap_buffers(gate_gl_surface_t* surface)
{
    (void)surface;
    return GATE_RESULT_NOTIMPLEMENTED;
}



gate_result_t gate_gl_api_init()
{
    return GATE_RESULT_NOTIMPLEMENTED;
}

void gate_gl_api_color3(gate_real32_t r, gate_real32_t g, gate_real32_t b)
{
    (void)r;
    (void)g;
    (void)b;
}
void gate_gl_api_color4(gate_real32_t r, gate_real32_t g, gate_real32_t b, gate_real32_t a)
{
    (void)r;
    (void)g;
    (void)b;
    (void)a;
}
void gate_gl_api_color3b(gate_uint8_t r, gate_uint8_t g, gate_uint8_t b)
{
    (void)r;
    (void)g;
    (void)b;
}
void gate_gl_api_color4b(gate_uint8_t r, gate_uint8_t g, gate_uint8_t b, gate_uint8_t a)
{
    (void)r;
    (void)g;
    (void)b;
    (void)a;
}
void gate_gl_api_vertexpointer(gate_size_t coord_count, gate_real32_t const* ptr_coords)
{
    (void)coord_count;
    (void)ptr_coords;
}
void gate_gl_api_texcoordpointer(gate_size_t coord_count, gate_real32_t const* ptr_coords)
{
    (void)coord_count;
    (void)ptr_coords;
}
void gate_gl_api_shademodel(gate_gl_api_shademodel_t model)
{
    (void)model;
}
void gate_gl_api_blendfunc(gate_gl_api_blend_t blend_func_src, gate_gl_api_blend_t blend_func_dst)
{
    (void)blend_func_src;
    (void)blend_func_dst;
}
void gate_gl_api_clearcolor(gate_real32_t r, gate_real32_t g, gate_real32_t b, gate_real32_t a)
{
    (void)r;
    (void)g;
    (void)b;
    (void)a;
}
void gate_gl_api_cleardepth(gate_real32_t depth)
{
    (void)depth;
}
void gate_gl_api_enable(gate_gl_api_capability_t capability)
{
    (void)capability;
}
void gate_gl_api_disable(gate_gl_api_capability_t capability)
{
    (void)capability;
}
void gate_gl_api_depthfunc(gate_gl_api_depth_t depth_function)
{
    (void)depth_function;
}
void gate_gl_api_depthmask(gate_bool_t enabled)
{
    (void)enabled;
}
void gate_gl_api_hint(gate_gl_api_hint_t hint_type, gate_gl_api_hintmode_t hint_mode)
{
    (void)hint_type;
    (void)hint_mode;
}
void gate_gl_api_viewport(gate_int32_t x, gate_int32_t y, gate_int32_t width, gate_int32_t height)
{
    (void)x;
    (void)y;
    (void)width;
    (void)height;
}
void gate_gl_api_matrixmode(gate_gl_api_matrixmode_t matrix_mode)
{
    (void)matrix_mode;
}
void gate_gl_api_loadidentity()
{
}
void gate_gl_api_multmatrix(gate_real32_t const matrix[16])
{
    (void)matrix;
}
void gate_gl_api_translate(gate_real32_t x, gate_real32_t y, gate_real32_t z)
{
    (void)x;
    (void)y;
    (void)z;
}
void gate_gl_api_rotate(gate_real32_t angle, gate_real32_t x, gate_real32_t y, gate_real32_t z)
{
    (void)angle;
    (void)x;
    (void)y;
    (void)z;
}
void gate_gl_api_flush()
{

}
void gate_gl_api_finish()
{

}
void gate_gl_api_clear(unsigned clear_bits)
{
    (void)clear_bits;
}
void gate_gl_api_perspective(gate_real32_t fovy, gate_real32_t aspect, gate_real32_t znear, gate_real32_t zfar)
{
    (void)fovy;
    (void)aspect;
    (void)znear;
    (void)zfar;
}
void gate_gl_api_lookat(gate_real32_t eyeX, gate_real32_t eyeY, gate_real32_t eyeZ,
    gate_real32_t lookX, gate_real32_t lookY, gate_real32_t lookZ,
    gate_real32_t upX, gate_real32_t upY, gate_real32_t upZ)
{
    (void)eyeX;
    (void)eyeY;
    (void)eyeZ;
    (void)lookX;
    (void)lookY;
    (void)lookZ;
    (void)upX;
    (void)upY;
    (void)upZ;
}
void gate_gl_api_ortho(gate_real32_t left, gate_real32_t right, gate_real32_t bottom, gate_real32_t top,
    gate_real32_t near_val, gate_real32_t far_val)
{
    (void)left;
    (void)right;
    (void)bottom;
    (void)top;
    (void)near_val;
    (void)far_val;
}
void gate_gl_api_enableclientstate(gate_gl_api_clientstate_t state)
{
    (void)state;
}
void gate_gl_api_disableclientstate(gate_gl_api_clientstate_t state)
{
    (void)state;
}
void gate_gl_api_drawarrays(gate_gl_api_drawmode_t draw_mode, int index, size_t count)
{
    (void)draw_mode;
    (void)index;
    (void)count;
}
void* gate_gl_api_createtexture(gate_gl_api_pixelformat_t pixel_format, gate_uint32_t width, gate_uint32_t height,
    void const* data)
{
    (void)pixel_format;
    (void)width;
    (void)height;
    (void)data;
    return NULL;
}
void gate_gl_api_deletetexture(void* texture_id)
{
    (void)texture_id;
}
void gate_gl_api_bindtexture(void* texture_id)
{
    (void)texture_id;
}
void gate_gl_api_cullface(gate_gl_api_cullface_t mode)
{
    (void)mode;
}

#endif /* GATE_GRAPHICS_GL_OPENGL */