summaryrefslogtreecommitdiff
path: root/source/lessons/uniform buffer objects/main.cpp
blob: 889536f22f7016497342e9b90cd3e664983a21a3 (plain)
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
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
#include <stdio.h>
#include <SDL2/SDL.h>
#include <glad/glad.h>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <vector>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

/* @lookup:
*  - understand kernals, how they work and how they affect post processing
*  - Check to see why it is necessary to do glBindTexture() 
*    - understand the difference between binding textures, and activating a texture unit
*  - I do not understand how floating point numbers work, so I should probably look into that.
*	 - The normal matrix calculation in the fragment shader for the object affected by light has been mainly copied.
*		 I have tried to understand the formula, and whilst it made some sense, it is not fully clear to me, and I cannot picture it yet.
*		 Revisit the derivation for the normal matrix some time in the future.
*  - Lookup the derivation of the formula for reflecting a vector about a normal. I am doing that for specular lighting, but the learnopengl tutorial
*		 just uses a glsl reflect formula, and at the time of writing it is also very late so I am not in the mood or position to look into it at present.
*  - One of the things I have observed with specular lights is that the circle/specular highlight follows the camera (me) when I move. I would like to figure
*		 out a way by which this does not happen and it remains fixed on the object, at the angle at which it hits. All of this will be made complicated by the fact
*		 that ofcourse everything is actually happening from the cameras' perspective. I would still love to figure this out.
*/

typedef uint8_t   u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

typedef int8_t    s8;
typedef int16_t  s16;
typedef int32_t  s32;
typedef int64_t  s64;

typedef float    r32;
typedef double   r64;

typedef u8        b8;

#include "math.h"

// =========== Shader Loading =============

unsigned int gl_create_vertex_shader(char* vertex_shader_source)
{
	unsigned int vertex_shader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
	glCompileShader(vertex_shader);
    
	int success;
	char info_log[512];
	glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
	if (!success)
	{
		glGetShaderInfoLog(vertex_shader, 512, NULL, info_log);
		printf("================================\n");
		printf("vertex shader compilation failed:\n%s\n", info_log);
	}
    
	return vertex_shader;
}

unsigned int gl_create_fragment_shader(char* fragment_shader_source)
{
	unsigned int fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
	glCompileShader(fragment_shader);
    
	int success;
	char info_log[512];
	glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
	if (!success)
	{
		glGetShaderInfoLog(fragment_shader, 512, NULL, info_log);
		printf("================================\n");
		printf("fragment shader compilation failed:\n%s\n", info_log);
	}
    
	return fragment_shader;
}

unsigned int gl_create_shader_program(unsigned int vertex_shader, unsigned int fragment_shader)
{
	unsigned int shader_program = glCreateProgram();
    
	glAttachShader(shader_program, vertex_shader);
	glAttachShader(shader_program, fragment_shader);
	glLinkProgram(shader_program);
    
	int success;
	char info_log[512];
	glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
	if (!success)
	{
		glGetProgramInfoLog(shader_program, 512, NULL, info_log);
		printf("================================\n");
		printf("shader program linking failed:\n%s\n", info_log);
	}
    
	glDeleteShader(vertex_shader);
	glDeleteShader(fragment_shader);
    
	return shader_program;
}

unsigned int gl_shader_program(char* vertex_shader_source, char* fragment_shader_source)
{
  unsigned int vertex_shader = gl_create_vertex_shader(vertex_shader_source);
  unsigned int fragment_shader = gl_create_fragment_shader(fragment_shader_source);
  unsigned int shader_program = gl_create_shader_program(vertex_shader, fragment_shader);

  return shader_program;
}

Mat4 camera_create4m(Vec3 camera_pos, Vec3 camera_look, Vec3 camera_up)
{
	// @note: We do this because this allows the camera to have the axis it looks at
	// inwards be the +z axis.
	// If we did not do this, then the inward axis the camera looks at would be negative. 
	// I am still learning from learnopengl.com but I imagine that this was done for conveniences' sake.
	Vec3 camera_forward_dir = normalize3v(subtract3v(camera_pos, camera_look));
	Vec3 camera_right_dir   = normalize3v(cross_multiply3v(camera_up, camera_forward_dir));
	Vec3 camera_up_dir      = normalize3v(cross_multiply3v(camera_forward_dir, camera_right_dir));
    
	Mat4 res = lookat4m(camera_up_dir, camera_forward_dir, camera_right_dir, camera_pos);
    
	return res;
}

Vec3 camera_look_around(r32 angle_pitch, r32 angle_yaw)
{
    Vec3 camera_look = {0.0};
    camera_look.x = cosf(angle_yaw) * cosf(angle_pitch);
    camera_look.y = sinf(angle_pitch);
    camera_look.z = sinf(angle_yaw) * cosf(angle_pitch);
    camera_look = normalize3v(camera_look);
    
    return camera_look;
}

s32 gl_load_texture(u32 texture_id, const char* path)
{
  s32 width, height, nrChannels;
  unsigned char *data = stbi_load(path, &width, &height, &nrChannels, 0);
  if (data)
  {
    GLenum format;
    if (nrChannels == 1)
      format = GL_RED;
    else if (nrChannels == 3)
      format = GL_RGB;
    else if (nrChannels == 4)
      format = GL_RGBA;

    glBindTexture(GL_TEXTURE_2D, texture_id);
    glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    stbi_image_free(data);
  }
  else
  {
    printf("failed to load image texture at path: %s", path);
    stbi_image_free(data);
  }

  return texture_id;
}

// =================== Model Loading ========================
// This section contains a whole host of things:
// 1. classes
// 2. std::vectors
// 3. std::strings
// that I have only used as a glue for I did not know if I had the model loading setup properly.
// @todo: replace these things eventually. For now the goal is to complete learnopengl

s32 TextureFromFile(const char* filepath, std::string directory)
{
  // @note: this function is stupid as it already became outdated as I needed to tweak the parameters
  // for wrapping. Either those become function parameters (Which makes sense I guess) or I look at
  // exactly what steps I am reusing and just make that a function so the function is called fewer times.
  //
  // I am guessing this won't look good from a design point of view for all those jobs and postings, even if
  // this may be the simpler and faster thing to do, albeit at the cost of typing.
  std::string filename = std::string(filepath);
  filename = directory + '/' + filename;

  u32 texid;
  glGenTextures(1, &texid);

  s32 width, height, nrChannels;
  unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrChannels, 0);
  if (data)
  {
    GLenum format;
    if (nrChannels == 1)
      format = GL_RED;
    else if (nrChannels == 3)
      format = GL_RGB;
    else if (nrChannels == 4)
      format = GL_RGBA;

    glBindTexture(GL_TEXTURE_2D, texid);
    glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    stbi_image_free(data);
  }
  else
  {
    printf("failed to load image texture at path: %s", filepath);
    stbi_image_free(data);
  }

  return texid;
}

enum TextureType { TextureDiffuse=0, TextureSpecular };

struct Vertex {
  Vec3 position;
  Vec3 normal;
  Vec2 texture;
};

struct Texture {
  u32 id;
  enum TextureType type;
  std::string fname;
};

class Mesh {
  public:
  std::vector<Vertex>  vertices;
  std::vector<u32> indices;
  std::vector<Texture> textures;

  u32 vao;
  u32 vbo;
  u32 ebo;

  Mesh(std::vector<Vertex> vertices, std::vector<u32> indices, std::vector<Texture> textures)
  {
    this->vertices = vertices;
    this->indices = indices;
    this->textures = textures;

    // setup mesh shader stuff
    glGenVertexArrays(1, &vao);
    glGenBuffers(1, &vbo);
    glGenBuffers(1, &ebo);

    glBindVertexArray(vao);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(struct Vertex), &(this->vertices[0]), GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(u32), &(this->indices[0]), GL_STATIC_DRAW);

    // position
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
    // normal
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
    // texture
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texture));

    glBindVertexArray(0);
  }

  void draw(u32 shader_program)
  {
    glUseProgram(shader_program);

    u32 diffuse_num = 1;
    u32 specular_num = 1;
    char tex_unit_name[64];
    // set shininess
    s32 mat_shine_loc = glGetUniformLocation(shader_program, "material.shininess");
    glUniform1f(mat_shine_loc, 32.0f);

    for (u32 i=0; i<textures.size(); i++)
    {
      struct Texture curr_tex = textures[i];
      if (curr_tex.type == TextureDiffuse)
      {
        sprintf(tex_unit_name, "material.diffuse[%i]", diffuse_num);
      }
      else if (curr_tex.type == TextureSpecular)
      {
        sprintf(tex_unit_name, "material.diffuse[%i]", specular_num);
      }

      glActiveTexture(GL_TEXTURE0 + i);
      s32 tex_unit_loc = glGetUniformLocation(shader_program, tex_unit_name);
      glUniform1i(tex_unit_loc, i);
      glBindTexture(GL_TEXTURE_2D, curr_tex.id);
    }
    glActiveTexture(GL_TEXTURE0);

    glBindVertexArray(vao);
    glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
  }
};

class Model
{
  public:
    Model(std::string path)
    {
      load_model(path);
    }
    void draw(u32 shader_program);
  private:
    std::vector<Texture> loaded_textures;
    std::vector<Mesh> meshes;
    std::string directory;

    void load_model(std::string path);
    void process_node(aiNode *node, const aiScene *scene);
    Mesh process_mesh(aiMesh *mesh, const aiScene *scene);
    std::vector<Texture> load_material_textures(aiMaterial *mat, aiTextureType type, TextureType type_name);
};

void Model::draw(u32 shader_program)
{
  for (int i=0; i < meshes.size(); i++)
  {
    meshes[i].draw(shader_program);
  }
}

void Model::load_model(std::string path)
{
  Assimp::Importer import;
  const aiScene *scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);

  if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
  {
    printf("error loading model :%s\n", import.GetErrorString());
    return;
  }

  directory = path.substr(0, path.find_last_of('/'));
  process_node(scene->mRootNode, scene);
}

void Model::process_node(aiNode *node, const aiScene *scene)
{
  for (int i=0; i < node->mNumMeshes; i++)
  {
    aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
    meshes.push_back(process_mesh(mesh, scene));
  }

  for (int i=0; i<node->mNumChildren; i++)
  {
    process_node(node->mChildren[i], scene);
  }
}

Mesh Model::process_mesh(aiMesh *mesh, const aiScene *scene)
{
  std::vector<Vertex> vertices;
  std::vector<u32> indices;
  std::vector<Texture> textures;

  for (u32 i=0; i < mesh->mNumVertices; i++)
  {
    Vec3 position;
    position.x = mesh->mVertices[i].x;
    position.y = mesh->mVertices[i].y;
    position.z = mesh->mVertices[i].z;

    Vec3 normal;
    normal.x = mesh->mNormals[i].x;
    normal.y = mesh->mNormals[i].y;
    normal.z = mesh->mNormals[i].z;

    Vec2 texture = {0, 0};
    if (mesh->mTextureCoords[0])
    {
      texture.x = mesh->mTextureCoords[0][i].x;
      texture.y = mesh->mTextureCoords[0][i].y;
    }

    struct Vertex vertex;
    vertex.position = position;
    vertex.normal = normal;
    vertex.texture = texture;

    vertices.push_back(vertex);
  }
  // process indices
  for (u32 i = 0; i < mesh->mNumFaces; i++)
  {
    aiFace face = mesh->mFaces[i];
    for(u32 j = 0; j < face.mNumIndices; j++)
    {
      indices.push_back(face.mIndices[j]);
    }
  }
  // process material
  if (mesh->mMaterialIndex >= 0)
  {
    aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];
    std::vector<Texture> diffuse_maps = load_material_textures(material, aiTextureType_DIFFUSE, TextureDiffuse);
    textures.insert(textures.end(), diffuse_maps.begin(), diffuse_maps.end());
    std::vector<Texture> specular_maps = load_material_textures(material, aiTextureType_SPECULAR, TextureSpecular);
    textures.insert(textures.end(), specular_maps.begin(), specular_maps.end());
  }

  return Mesh(vertices, indices, textures);
}

std::vector<Texture> Model::load_material_textures(aiMaterial *mat, aiTextureType type, TextureType tex_type)
{
  std::vector<Texture> textures;
  for(u32 i=0; i<mat->GetTextureCount(type); i++)
  {
    bool load_texture = true;
    aiString str;
    mat->GetTexture(type, i, &str);
    const char* fname = str.C_Str();

    for (s32 j=0; j<loaded_textures.size();  j++)
    {
      if (std::strcmp(loaded_textures[j].fname.data(), fname) == 0)
      {
        load_texture = false;
        textures.push_back(loaded_textures[j]);
        break;
      }
    }
    if (load_texture)
    {
      Texture texture;
      texture.id = TextureFromFile(fname, directory);
      texture.type = tex_type;
      texture.fname = std::string(fname);
      textures.push_back(texture);
      loaded_textures.push_back(texture);
    }
  }

  return textures;
}

class Shader {
  // @note: this is a draft, I think frankly, it's a stupid idea to be making this at this point
  // but my goal is to look at my code at this stage (which is in the second half of (or so I think)
  // learnopengl and identify repeated code that I think I can yank out and can make convenient to write.
  // The precondition for all of this is that I do not remodel the program based off of some vague idea of
  // cleanliness in my head. This is all still very procedural, I just want to minimize the amount I type
  // and at the same time see how well I can identify good abstractions
  //
  //
  // I much prefer to have things be not a class, especially if I look at how I did my lovely
  // math functions, which are so simple and straightforward
  public:
    u32 id;
    
    // @note: all well and good until you get compute shaders
    // then the entire thing shits the bed
    Shader(char* vertex_shader_source, char* fragment_shader_source) {
      id = gl_shader_program(vertex_shader_source, fragment_shader_source);
    }

    void use() {
      glUseProgram(id);
    }

    void draw_triangles(u32 vao, u32 count) {
      glBindVertexArray(vao);
      glDrawArrays(GL_TRIANGLES, 0, count);
    }

    void set_1i(char* variable, s32 value) {
      s32 loc = glGetUniformLocation(id, variable);
      glUniform1i(loc, value);
    }

    void set_matrix4fv(char* variable, Mat4 value, int count) {
      s32 loc = glGetUniformLocation(id, variable);
      glUniformMatrix4fv(loc, count, GL_TRUE, value.buffer);
    };

    ~Shader() {
      // @note: this can literally be replaced by another function that goes over the entire list
      // of shader_programs near the program exit and deletes them, if I even need to do that.
      glDeleteProgram(id);
    }
};

int main(int argc, char* argv[])
{

  // ============ END ============
	int width = 1024;
	int height = 768;
    
	if (SDL_Init(SDL_INIT_VIDEO) != 0)
	{
		printf("Error initialising SDL2: %s\n", SDL_GetError());
		return 0;
	};
    
	// set opengl version and profile
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 8 );
    
	// initialise window with opengl flag
	SDL_Window* window = SDL_CreateWindow("SDL Test",
                                          50,
                                          50,
                                          width,
                                          height,
                                          SDL_WINDOW_OPENGL);
    
    SDL_SetRelativeMouseMode(SDL_TRUE);
   
	// create an opengl context
	SDL_GLContext context = SDL_GL_CreateContext(window);
	if (!context)
	{
		printf("OpenGL context creation failed: %s\n", SDL_GetError());
		return -1;
	}
    
    
	// load glad
	if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) {
		printf("Failed to initialize Glad\n");
		return 1;
	}
    
  // filesystem playground stuff
  size_t read_count;
  char* vertex_source = (char*)SDL_LoadFile("./source/shaders/depth_test.vs.glsl", &read_count);
  char* fragment_source = (char*)SDL_LoadFile("./source/shaders/depth_test.fs.glsl", &read_count);
  char* blend_shader_source = (char*)SDL_LoadFile("./source/shaders/blend_test.fs.glsl", &read_count);
  char* fbo_vs = (char*)SDL_LoadFile("./source/shaders/fbo.vs.glsl", &read_count);
  char* fbo_fs = (char*)SDL_LoadFile("./source/shaders/fbo.fs.glsl", &read_count);
  char* cubemap_vs = (char*)SDL_LoadFile("./source/shaders/cubemap.vs.glsl", &read_count);
  char* cubemap_fs = (char*)SDL_LoadFile("./source/shaders/cubemap.fs.glsl", &read_count);
  char* refl_vs = (char*)SDL_LoadFile("./source/shaders/refl.vs.glsl", &read_count);
  char* refl_fs = (char*)SDL_LoadFile("./source/shaders/refl.fs.glsl", &read_count);
  char* refr_vs = (char*)SDL_LoadFile("./source/shaders/refr.vs.glsl", &read_count);
  char* refr_fs = (char*)SDL_LoadFile("./source/shaders/refr.fs.glsl", &read_count);
    
  u32 shader_program = gl_shader_program(vertex_source, fragment_source);
	u32 blend_shader_program = gl_shader_program(vertex_source, blend_shader_source);
	u32 fbo_shader_program = gl_shader_program(fbo_vs, fbo_fs);
  u32 cubemap_shader_program = gl_shader_program(cubemap_vs, cubemap_fs);
  u32 refl_shader_program = gl_shader_program(refl_vs, refl_fs);
  u32 refr_shader_program = gl_shader_program(refr_vs, refr_fs);

	printf("Successfully compiled shaders.\n");
    
  float cubeVertices[] = {
    // Back face
    -0.5f, -0.5f, -0.5f,  0.0f, 0.0f, // Bottom-left
     0.5f,  0.5f, -0.5f,  1.0f, 1.0f, // top-right
     0.5f, -0.5f, -0.5f,  1.0f, 0.0f, // bottom-right         
     0.5f,  0.5f, -0.5f,  1.0f, 1.0f, // top-right
    -0.5f, -0.5f, -0.5f,  0.0f, 0.0f, // bottom-left
    -0.5f,  0.5f, -0.5f,  0.0f, 1.0f, // top-left
    // Front face
    -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, // bottom-left
     0.5f, -0.5f,  0.5f,  1.0f, 0.0f, // bottom-right
     0.5f,  0.5f,  0.5f,  1.0f, 1.0f, // top-right
     0.5f,  0.5f,  0.5f,  1.0f, 1.0f, // top-right
    -0.5f,  0.5f,  0.5f,  0.0f, 1.0f, // top-left
    -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, // bottom-left
    // Left face
    -0.5f,  0.5f,  0.5f,  1.0f, 0.0f, // top-right
    -0.5f,  0.5f, -0.5f,  1.0f, 1.0f, // top-left
    -0.5f, -0.5f, -0.5f,  0.0f, 1.0f, // bottom-left
    -0.5f, -0.5f, -0.5f,  0.0f, 1.0f, // bottom-left
    -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, // bottom-right
    -0.5f,  0.5f,  0.5f,  1.0f, 0.0f, // top-right
    // Right face
     0.5f,  0.5f,  0.5f,  1.0f, 0.0f, // top-left
     0.5f, -0.5f, -0.5f,  0.0f, 1.0f, // bottom-right
     0.5f,  0.5f, -0.5f,  1.0f, 1.0f, // top-right         
     0.5f, -0.5f, -0.5f,  0.0f, 1.0f, // bottom-right
     0.5f,  0.5f,  0.5f,  1.0f, 0.0f, // top-left
     0.5f, -0.5f,  0.5f,  0.0f, 0.0f, // bottom-left     
    // Bottom face
    -0.5f, -0.5f, -0.5f,  0.0f, 1.0f, // top-right
     0.5f, -0.5f, -0.5f,  1.0f, 1.0f, // top-left
     0.5f, -0.5f,  0.5f,  1.0f, 0.0f, // bottom-left
     0.5f, -0.5f,  0.5f,  1.0f, 0.0f, // bottom-left
    -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, // bottom-right
    -0.5f, -0.5f, -0.5f,  0.0f, 1.0f, // top-right
    // Top face
    -0.5f,  0.5f, -0.5f,  0.0f, 1.0f, // top-left
     0.5f,  0.5f,  0.5f,  1.0f, 0.0f, // bottom-right
     0.5f,  0.5f, -0.5f,  1.0f, 1.0f, // top-right     
     0.5f,  0.5f,  0.5f,  1.0f, 0.0f, // bottom-right
    -0.5f,  0.5f, -0.5f,  0.0f, 1.0f, // top-left
    -0.5f,  0.5f,  0.5f,  0.0f, 0.0f  // bottom-left     
  };

   float planeVertices[] = {
      // positions          // texture Coords (note we set these higher than 1 (together with GL_REPEAT as texture wrapping mode). this will cause the floor texture to repeat)
      // BottomFace
      -5.0f, -1.0f, -5.0f,  0.0f, 2.0f, // top-right
       5.0f, -1.0f, -5.0f,  2.0f, 2.0f, // top-left
       5.0f, -1.0f,  5.0f,  2.0f, 0.0f, // bottom-left
       5.0f, -1.0f,  5.0f,  2.0f, 0.0f, // bottom-left
      -5.0f, -1.0f,  5.0f,  0.0f, 0.0f, // bottom-right
      -5.0f, -1.0f, -5.0f,  0.0f, 2.0f, // top-right
      // Top face
      -5.0f, -0.5f, -5.0f,  0.0f, 2.0f, // top-left
       5.0f, -0.5f,  5.0f,  2.0f, 0.0f, // bottom-right
       5.0f, -0.5f, -5.0f,  2.0f, 2.0f, // top-right     
       5.0f, -0.5f,  5.0f,  2.0f, 0.0f, // bottom-right
      -5.0f, -0.5f, -5.0f,  0.0f, 2.0f, // top-left
      -5.0f, -0.5f,  5.0f,  0.0f, 0.0f  // bottom-left     
    };

  float quadVertices[] = {
      // Front face
      -0.5f, -0.5f,  0.0f, 0.0f, // bottom-left
       0.5f, -0.5f,  1.0f, 0.0f, // bottom-right
       0.5f,  0.5f,  1.0f, 1.0f, // top-right
       0.5f,  0.5f,  1.0f, 1.0f, // top-right
      -0.5f,  0.5f,  0.0f, 1.0f, // top-left
      -0.5f, -0.5f,  0.0f, 0.0f, // bottom-left
      // Back face
      -0.5f, -0.5f,  0.0f, 0.0f, // Bottom-left
       0.5f,  0.5f,  1.0f, 1.0f, // top-right
       0.5f, -0.5f,  1.0f, 0.0f, // bottom-right         
       0.5f,  0.5f,  1.0f, 1.0f, // top-right
      -0.5f, -0.5f,  0.0f, 0.0f, // bottom-left
      -0.5f,  0.5f,  0.0f, 1.0f, // top-left
    };

  float fboVertices[] = {
      -1.0f, -1.0f,  0.0f, 0.0f, // bottom-left
       1.0f, -1.0f,  1.0f, 0.0f, // bottom-right
       1.0f,  1.0f,  1.0f, 1.0f, // top-right
       1.0f,  1.0f,  1.0f, 1.0f, // top-right
      -1.0f,  1.0f,  0.0f, 1.0f, // top-left
      -1.0f, -1.0f,  0.0f, 0.0f, // bottom-left
  };

  float skyboxVertices[] = {
    // positions          
    -1.0f,  1.0f, -1.0f,
    -1.0f, -1.0f, -1.0f,
     1.0f, -1.0f, -1.0f,
     1.0f, -1.0f, -1.0f,
     1.0f,  1.0f, -1.0f,
    -1.0f,  1.0f, -1.0f,

    -1.0f, -1.0f,  1.0f,
    -1.0f, -1.0f, -1.0f,
    -1.0f,  1.0f, -1.0f,
    -1.0f,  1.0f, -1.0f,
    -1.0f,  1.0f,  1.0f,
    -1.0f, -1.0f,  1.0f,

     1.0f, -1.0f, -1.0f,
     1.0f, -1.0f,  1.0f,
     1.0f,  1.0f,  1.0f,
     1.0f,  1.0f,  1.0f,
     1.0f,  1.0f, -1.0f,
     1.0f, -1.0f, -1.0f,

    -1.0f, -1.0f,  1.0f,
    -1.0f,  1.0f,  1.0f,
     1.0f,  1.0f,  1.0f,
     1.0f,  1.0f,  1.0f,
     1.0f, -1.0f,  1.0f,
    -1.0f, -1.0f,  1.0f,

    -1.0f,  1.0f, -1.0f,
     1.0f,  1.0f, -1.0f,
     1.0f,  1.0f,  1.0f,
     1.0f,  1.0f,  1.0f,
    -1.0f,  1.0f,  1.0f,
    -1.0f,  1.0f, -1.0f,

    -1.0f, -1.0f, -1.0f,
    -1.0f, -1.0f,  1.0f,
     1.0f, -1.0f, -1.0f,
     1.0f, -1.0f, -1.0f,
    -1.0f, -1.0f,  1.0f,
     1.0f, -1.0f,  1.0f
  };
  float reflVertices[] = {
    // positions          // texture Coords (note we set these higher than 1 (together with GL_REPEAT as texture wrapping mode). this will cause the floor texture to repeat)
    // Top face
    //-5.0f,  0.5f, -5.0f,  0.0f, 1.0f, 0.0f,// top-left
    // 5.0f,  0.5f,  5.0f,  0.0f, 1.0f, 0.0f,// bottom-right
    // 5.0f,  0.5f, -5.0f,  0.0f, 1.0f, 0.0f,// top-right     
    // 5.0f,  0.5f,  5.0f,  0.0f, 1.0f, 0.0f,// bottom-right
    //-5.0f,  0.5f, -5.0f,  0.0f, 1.0f, 0.0f,// top-left
    //-5.0f,  0.5f,  5.0f,  0.0f, 1.0f, 0.0f,// bottom-left     
#if REFLECTIVE_PLANE
    // Back face
    -0.5f, -0.5f, -0.5f,  0.0f, 0.0f, -1.0f, // Bottom-left
     0.5f,  0.5f, -0.5f,  0.0f, 0.0f, -1.0f, // top-right
     0.5f, -0.5f, -0.5f,  0.0f, 0.0f, -1.0f, // bottom-right         
     0.5f,  0.5f, -0.5f,  0.0f, 0.0f, -1.0f, // top-right
    -0.5f, -0.5f, -0.5f,  0.0f, 0.0f, -1.0f, // bottom-left
    -0.5f,  0.5f, -0.5f,  0.0f, 0.0f, -1.0f, // top-left
    // Front face
    -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, 1.0f, // bottom-left
     0.5f, -0.5f,  0.5f,  0.0f, 0.0f, 1.0f, // bottom-right
     0.5f,  0.5f,  0.5f,  0.0f, 0.0f, 1.0f, // top-right
     0.5f,  0.5f,  0.5f,  0.0f, 0.0f, 1.0f, // top-right
    -0.5f,  0.5f,  0.5f,  0.0f, 0.0f, 1.0f, // top-left
    -0.5f, -0.5f,  0.5f,  0.0f, 0.0f, 1.0f, // bottom-left
    // Left face
    -0.5f,  0.5f,  0.5f,  -1.0f, 0.0f, 0.0f, // top-right
    -0.5f,  0.5f, -0.5f,  -1.0f, 0.0f, 0.0f, // top-left
    -0.5f, -0.5f, -0.5f,  -1.0f, 0.0f, 0.0f, // bottom-left
    -0.5f, -0.5f, -0.5f,  -1.0f, 0.0f, 0.0f, // bottom-left
    -0.5f, -0.5f,  0.5f,  -1.0f, 0.0f, 0.0f, // bottom-right
    -0.5f,  0.5f,  0.5f,  -1.0f, 0.0f, 0.0f, // top-right
    // Right face
     0.5f,  0.5f,  0.5f,   1.0f, 0.0f, 0.0f, // top-left
     0.5f, -0.5f, -0.5f,   1.0f, 0.0f, 0.0f, // bottom-right
     0.5f,  0.5f, -0.5f,   1.0f, 0.0f, 0.0f, // top-right         
     0.5f, -0.5f, -0.5f,   1.0f, 0.0f, 0.0f, // bottom-right
     0.5f,  0.5f,  0.5f,   1.0f, 0.0f, 0.0f, // top-left
     0.5f, -0.5f,  0.5f,   1.0f, 0.0f, 0.0f, // bottom-left     
    // Bottom face
    -0.5f, -0.5f, -0.5f,  0.0f, -1.0f, 0.0f, // top-right
     0.5f, -0.5f, -0.5f,  0.0f, -1.0f, 0.0f, // top-left
     0.5f, -0.5f,  0.5f,  0.0f, -1.0f, 0.0f, // bottom-left
     0.5f, -0.5f,  0.5f,  0.0f, -1.0f, 0.0f, // bottom-left
    -0.5f, -0.5f,  0.5f,  0.0f, -1.0f, 0.0f, // bottom-right
    -0.5f, -0.5f, -0.5f,  0.0f, -1.0f, 0.0f, // top-right
#endif
    // Top face
    -4.0f,  0.0f, -4.0f,  0.0f,  1.0f, 0.0f, // top-left
     4.0f,  0.0f,  4.0f,  0.0f,  1.0f, 0.0f, // bottom-right
     4.0f,  0.0f, -4.0f,  0.0f,  1.0f, 0.0f, // top-right     
     4.0f,  0.0f,  4.0f,  0.0f,  1.0f, 0.0f, // bottom-right
    -4.0f,  0.0f, -4.0f,  0.0f,  1.0f, 0.0f, // top-left
    -4.0f,  0.0f,  4.0f,  0.0f,  1.0f, 0.0f  // bottom-left      
  };

  float refrVertices[] = {
    // Left face
    -0.5f,  0.5f,  0.5f,  -1.0f, 0.0f, 0.0f, // top-right
    -0.5f,  0.5f, -0.5f,  -1.0f, 0.0f, 0.0f, // top-left
    -0.5f, -0.5f, -0.5f,  -1.0f, 0.0f, 0.0f, // bottom-left
    -0.5f, -0.5f, -0.5f,  -1.0f, 0.0f, 0.0f, // bottom-left
    -0.5f, -0.5f,  0.5f,  -1.0f, 0.0f, 0.0f, // bottom-right
    -0.5f,  0.5f,  0.5f,  -1.0f, 0.0f, 0.0f, // top-right
    // Right face
    -0.5f,  0.5f,  0.5f,   1.0f, 0.0f, 0.0f, // top-left
    -0.5f, -0.5f, -0.5f,   1.0f, 0.0f, 0.0f, // bottom-right
    -0.5f,  0.5f, -0.5f,   1.0f, 0.0f, 0.0f, // top-right         
    -0.5f, -0.5f, -0.5f,   1.0f, 0.0f, 0.0f, // bottom-right
    -0.5f,  0.5f,  0.5f,   1.0f, 0.0f, 0.0f, // top-left
    -0.5f, -0.5f,  0.5f,   1.0f, 0.0f, 0.0f, // bottom-left     
  };

  stbi_set_flip_vertically_on_load(1);
  
	u32 cube_vao, cube_vbo, plane_vao, plane_vbo, quad_vao, quad_vbo, fbo_vao, fbo_vbo, 
      skybox_vao, skybox_vbo, refl_vao, refl_vbo, refr_vao, refr_vbo;
	
	glGenVertexArrays(1, &cube_vao);
	glGenBuffers(1, &cube_vbo);

	glBindVertexArray(cube_vao);
  glBindBuffer(GL_ARRAY_BUFFER, cube_vbo);
  glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), &cubeVertices, GL_STATIC_DRAW);
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
  glEnableVertexAttribArray(1);
  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
  glBindVertexArray(0);

	glGenVertexArrays(1, &plane_vao);
	glGenBuffers(1, &plane_vbo);

	glBindVertexArray(plane_vao);
  glBindBuffer(GL_ARRAY_BUFFER, plane_vbo);
  glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), &planeVertices, GL_STATIC_DRAW);
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
  glEnableVertexAttribArray(1);
  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
  glBindVertexArray(0);

	glGenVertexArrays(1, &quad_vao);
	glGenBuffers(1, &quad_vbo);

	glBindVertexArray(quad_vao);
  glBindBuffer(GL_ARRAY_BUFFER, quad_vbo);
  glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
  glEnableVertexAttribArray(1);
  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2*sizeof(float)));
  glBindVertexArray(0);

  // framebuffer objects
	glGenVertexArrays(1, &fbo_vao);
	glGenBuffers(1, &fbo_vbo);

	glBindVertexArray(fbo_vao);
  glBindBuffer(GL_ARRAY_BUFFER, fbo_vbo);
  glBufferData(GL_ARRAY_BUFFER, sizeof(fboVertices), &fboVertices, GL_STATIC_DRAW);
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
  glEnableVertexAttribArray(1);
  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2*sizeof(float)));
  glBindVertexArray(0);

  // skybox objects
	glGenVertexArrays(1, &skybox_vao);
	glGenBuffers(1, &skybox_vbo);

	glBindVertexArray(skybox_vao);
  glBindBuffer(GL_ARRAY_BUFFER, skybox_vbo);
  glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
  glBindVertexArray(0);

  // reflective surface objects
	glGenVertexArrays(1, &refl_vao);
	glGenBuffers(1, &refl_vbo);

	glBindVertexArray(refl_vao);
  glBindBuffer(GL_ARRAY_BUFFER, refl_vbo);
  glBufferData(GL_ARRAY_BUFFER, sizeof(reflVertices), &reflVertices, GL_STATIC_DRAW);
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
  glEnableVertexAttribArray(1);
  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float)));
  glBindVertexArray(0);

  // refractive surface objects
	glGenVertexArrays(1, &refr_vao);
	glGenBuffers(1, &refr_vbo);

	glBindVertexArray(refr_vao);
  glBindBuffer(GL_ARRAY_BUFFER, refr_vbo);
  glBufferData(GL_ARRAY_BUFFER, sizeof(refrVertices), &refrVertices, GL_STATIC_DRAW);
  glEnableVertexAttribArray(0);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
  glEnableVertexAttribArray(1);
  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float)));
  glBindVertexArray(0);

  // uniform buffer objects
  u32 ubo_camera_block;
  glGenBuffers(1, &ubo_camera_block);
  glBindBuffer(GL_UNIFORM_BUFFER, ubo_camera_block);
  glBufferData(GL_UNIFORM_BUFFER, 128, NULL, GL_STATIC_DRAW);
  glBindBuffer(GL_UNIFORM_BUFFER, 0);

  u32 window_tex_id;
  glGenTextures(1, &window_tex_id);
  glActiveTexture(GL_TEXTURE2);
  {
    char *path = "assets/window.png";
    s32 _width, _height, nrChannels;
    unsigned char *data = stbi_load(path, &_width, &_height, &nrChannels, 0);
    if (data)
    {
      GLenum format;
      if (nrChannels == 1)
        format = GL_RED;
      else if (nrChannels == 3)
        format = GL_RGB;
      else if (nrChannels == 4)
        format = GL_RGBA;

      glBindTexture(GL_TEXTURE_2D, window_tex_id);
      glTexImage2D(GL_TEXTURE_2D, 0, format, _width, _height, 0, format, GL_UNSIGNED_BYTE, data);
      glGenerateMipmap(GL_TEXTURE_2D);

      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

      stbi_image_free(data);
    }
    else
    {
      printf("failed to load image texture at path: %s", path);
      stbi_image_free(data);
    }
  }

  u32 cube_tex_id;
  glGenTextures(1, &cube_tex_id);
  glActiveTexture(GL_TEXTURE0);
  gl_load_texture(cube_tex_id, "assets/container.jpg");

  u32 plane_tex_id;
  glGenTextures(1, &plane_tex_id);
  glActiveTexture(GL_TEXTURE1);
  gl_load_texture(plane_tex_id, "assets/smiling.png");

	// objects
	Vec3 model_translations[] = {
		Vec3{  0.5, 0.0,  0.0}, // 0: origin square
		Vec3{ -1.0, 0.0, -1.0}, // 1: plane
		Vec3{ -1.0, 0.0, -0.5}, // 2: window between squares
		Vec3{  0.0, 0.0,  3.0}, // 3: window infront of origin square
		Vec3{ -2.5, 0.0, -0.5}, // 4: square to the left
		Vec3{ -1.0, 0.0, -1.5}, // 5: random square behind window between squares
    Vec3{ -1.0, 0.0, -8.0}, // 6: reflective plane
    Vec3{ -1.0, 2.0, -8.0}, // 6: refractive "window"
	};
    
	r32 FOV = 90.0;
	r32 time_curr;
	r32 time_prev = SDL_GetTicks64() / 100.0;
	// camera stuff
	Vec3 camera_pos  = Vec3{ 0.0, 0.0, 10.0f};
	Vec3 preset_up_dir = Vec3{ 0.0, 1.0, 0.0 };
    
  r32 angle_yaw, angle_pitch, angle_roll;
  angle_pitch = (r32)To_Radian(0.0f);
  angle_yaw = (r32)-To_Radian(90.0f);
  
  Vec3 camera_look = camera_look_around(angle_pitch, angle_yaw);
  
  // @todo: remove this, I dont like this and think that this is unnecessary
  Vec3 camera_look_increment;
	r32 camera_speed = 0.5f;
    
	Mat4 view = camera_create4m(camera_pos, camera_look, preset_up_dir);
	
	Mat4 proj = perspective4m((r32)To_Radian(90.0), (r32)width / (r32)height, 0.1f, 100.0f);
  // needs this
  u32 proj_loc;
  glUseProgram(cubemap_shader_program);
	proj_loc = glGetUniformLocation(cubemap_shader_program, "Projection");
	glUniformMatrix4fv(proj_loc, 1, GL_TRUE, proj.buffer);

  glUseProgram(refl_shader_program);
	proj_loc = glGetUniformLocation(refl_shader_program, "Projection");
	glUniformMatrix4fv(proj_loc, 1, GL_TRUE, proj.buffer);

  glUseProgram(refr_shader_program);
	proj_loc = glGetUniformLocation(refr_shader_program, "Projection");
	glUniformMatrix4fv(proj_loc, 1, GL_TRUE, proj.buffer);

  u32 block_binding = 0;
  u32 matrices_ind;
  matrices_ind = glGetUniformBlockIndex(shader_program, "Matrices");
  glUniformBlockBinding(shader_program, matrices_ind, block_binding);
  matrices_ind = glGetUniformBlockIndex(blend_shader_program, "Matrices");
  glUniformBlockBinding(blend_shader_program, matrices_ind, block_binding);
  // or glBindBufferBase();
  glBindBufferRange(GL_UNIFORM_BUFFER, block_binding, ubo_camera_block, 0, 128); 

  Mat4 col_major_proj = to_col_major4m(proj);
  glBindBuffer(GL_UNIFORM_BUFFER, ubo_camera_block);
  glBufferSubData(GL_UNIFORM_BUFFER, 64, 64, col_major_proj.buffer);
  glBindBuffer(GL_UNIFORM_BUFFER, 0);
	
	glEnable(GL_DEPTH_TEST);
  glEnable(GL_BLEND);
  glEnable(GL_CULL_FACE);
  glDepthFunc(GL_LESS);
  glCullFace(GL_BACK);
  //glFrontFace(GL_CW); // front-faces are clock-wise, this will make the back-faces visible (since the arrays used were
                      // setup as counter-clockwise)
  u32 fbo;
  glGenFramebuffers(1, &fbo);
  glBindFramebuffer(GL_FRAMEBUFFER, fbo);

  // create a texture for the framebuffer to render data to
  u32 fbo_tex;
  glGenTextures(1, &fbo_tex);
  glActiveTexture(GL_TEXTURE3);
  glBindTexture(GL_TEXTURE_2D, fbo_tex);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glBindTexture(GL_TEXTURE_2D, 0);

  u32 rbo;
  glGenRenderbuffers(1, &rbo);
  glBindRenderbuffer(GL_RENDERBUFFER, rbo);
  glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
  glBindRenderbuffer(GL_RENDERBUFFER, 0);

  // attach to current bound framebuffer object
  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo_tex, 0);
  glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);

  if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
    printf("Error! Framebuffer is not completely setup\n");
    return -1;
  }
  glBindFramebuffer(GL_FRAMEBUFFER, 0);

  // generate_cubemap
  const char *cm_face_right = "assets/skybox/right.jpg";
  const char *cm_face_left = "assets/skybox/left.jpg";
  const char *cm_face_top = "assets/skybox/top.jpg";
  const char *cm_face_bottom = "assets/skybox/bottom.jpg";
  const char *cm_face_back = "assets/skybox/back.jpg";
  const char *cm_face_front = "assets/skybox/front.jpg";

  u32 cm_tex_id;
  {
    stbi_set_flip_vertically_on_load(0);
    glGenTextures(1, &cm_tex_id);
    glBindTexture(GL_TEXTURE_CUBE_MAP, cm_tex_id);
    int cm_width, cm_height, cm_nrChannels;
    unsigned char *data;
    // right face
    data = stbi_load(cm_face_right, &cm_width, &cm_height, &cm_nrChannels, 0);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 0, 0, GL_RGB, cm_width, cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    // left face
    data = stbi_load(cm_face_left, &cm_width, &cm_height, &cm_nrChannels, 0);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 1, 0, GL_RGB, cm_width, cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    // top face
    data = stbi_load(cm_face_top, &cm_width, &cm_height, &cm_nrChannels, 0);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 2, 0, GL_RGB, cm_width, cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    // bottom face
    data = stbi_load(cm_face_bottom, &cm_width, &cm_height, &cm_nrChannels, 0);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 3, 0, GL_RGB, cm_width, cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    // back face
    data = stbi_load(cm_face_back, &cm_width, &cm_height, &cm_nrChannels, 0);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 4, 0, GL_RGB, cm_width, cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    // front face
    data = stbi_load(cm_face_front, &cm_width, &cm_height, &cm_nrChannels, 0);
    glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + 5, 0, GL_RGB, cm_width, cm_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

    // texture filtering methods
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

    glUseProgram(cubemap_shader_program);
    glActiveTexture(GL_TEXTURE0);
    s32 skybox_loc = glGetUniformLocation(cubemap_shader_program, "skybox");
    glUniform1i(skybox_loc, 0);
    stbi_set_flip_vertically_on_load(1);
  }

  {
    glUseProgram(refl_shader_program);
    s32 skybox_loc = glGetUniformLocation(refl_shader_program, "skybox");
    glUniform1i(skybox_loc, 0);
  }

  {
    glUseProgram(refr_shader_program);
    s32 skybox_loc = glGetUniformLocation(refr_shader_program, "skybox");
    glUniform1i(skybox_loc, 0);
  }

	u8 game_running = true;
    
  u8 hold_lshift = false;
	u8 move_w = false;
	u8 move_a = false;
	u8 move_s = false;
	u8 move_d = false;
    
	while(game_running)
	{
		
		// frame delta
		time_curr = SDL_GetTicks64() / 100.0;
		r32 time_delta = time_curr - time_prev;
		
		r32 camera_speed_adjusted = time_delta * camera_speed;
		camera_look_increment = scaler_multiply3v(camera_look, camera_speed_adjusted);
        
		SDL_Event ev;
		while(SDL_PollEvent(&ev))
		{
            
			// INPUT
			switch (ev.type)
			{
				case (SDL_QUIT):
				{
					game_running = false;
				} break;
				case (SDL_KEYDOWN):
				{
          if (ev.key.keysym.sym == SDLK_LSHIFT)
          {
              hold_lshift = true;
          }
					if (ev.key.keysym.sym == SDLK_w)
					{
						move_w = true;
					}
					if (ev.key.keysym.sym == SDLK_s)
					{
						move_s = true;
					}
					if (ev.key.keysym.sym == SDLK_a)
					{
						move_a = true;
					}
					if (ev.key.keysym.sym == SDLK_d)
					{
						move_d = true;
					}
				} break;
				case (SDL_KEYUP):
				{
          if (ev.key.keysym.sym == SDLK_LSHIFT)
          {
              hold_lshift = false;
          }
					if (ev.key.keysym.sym == SDLK_w)
					{
						move_w = false;
					}
					if (ev.key.keysym.sym == SDLK_s)
					{
						move_s = false;
					}
					if (ev.key.keysym.sym == SDLK_a)
					{
						move_a = false;
					}
					if (ev.key.keysym.sym == SDLK_d)
					{
						move_d = false;
					}
				} break;
        case (SDL_MOUSEMOTION):
        {
          SDL_MouseMotionEvent mouse_event = ev.motion;
          r32 x_motion = (r32)mouse_event.xrel;
          r32 y_motion = (r32)mouse_event.yrel;
					if (x_motion != 0.0 || y_motion != 0.0)
					{
						angle_yaw = angle_yaw + To_Radian(x_motion * 0.1f);
						angle_pitch = clampf(angle_pitch + To_Radian(-y_motion * 0.1f), To_Radian(-89.0f), To_Radian(89.0f));
						
						camera_look = camera_look_around(angle_pitch, angle_yaw);
					}
        } break;
				default:
				{
					break;
				}
			}
		}
        
		// PROCESS
		if (move_w)
		{
			camera_pos = add3v(camera_pos, camera_look_increment);
		}
		if (move_s)
		{
			camera_pos = subtract3v(camera_pos, camera_look_increment);
		}
		if (move_a)
		{
			Vec3 camera_right = normalize3v(cross_multiply3v(preset_up_dir, camera_look));
			Vec3 camera_right_scaled = scaler_multiply3v(camera_right, camera_speed_adjusted);
			camera_pos = add3v(camera_pos, camera_right_scaled);
		}
		if (move_d)
		{
			Vec3 camera_right = normalize3v(cross_multiply3v(preset_up_dir, camera_look));
			Vec3 camera_right_scaled = scaler_multiply3v(camera_right, camera_speed_adjusted);
			camera_pos = subtract3v(camera_pos, camera_right_scaled);
		}
    view = camera_create4m(camera_pos, add3v(camera_pos, camera_look), preset_up_dir);
        
		// object shader program stuff
    Mat4 col_major_view = to_col_major4m(view);
    glBindBuffer(GL_UNIFORM_BUFFER, ubo_camera_block);
    glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, col_major_view.buffer);
    glBindBuffer(GL_UNIFORM_BUFFER, 0);
    
    u32 view_loc;
    glUseProgram(refl_shader_program);
    view_loc = glGetUniformLocation(refl_shader_program, "View");
		glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer);

    glUseProgram(refr_shader_program);
    view_loc = glGetUniformLocation(refr_shader_program, "View");
		glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer);

    glUseProgram(cubemap_shader_program);
    view_loc = glGetUniformLocation(cubemap_shader_program, "View");
    view.xyzw[0].w = 0.0f;
    view.xyzw[1].w = 0.0f;
    view.xyzw[2].w = 0.0f;
		glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer);

		time_prev = time_curr;
		
		// OUTPUT
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
		glClearColor(1.0f, 0.6f, .6f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_DEPTH_TEST);
    glDisable(GL_BLEND);
    { //plane
      glUseProgram(shader_program);
      // @note: this is just a test, I dont need to do this here;
      s32 tex_id_loc = glGetUniformLocation(shader_program, "TexId");
      glUniform1i(tex_id_loc, 1);
      Vec3 translation_iter = model_translations[1];
      Mat4 model = init_value4m(1.0);
      Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z);
      model = multiply4m(model_translation, model);
      uint32_t model_loc = glGetUniformLocation(shader_program, "Model");
      glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer);
      glBindVertexArray(plane_vao);
      glBindTexture(GL_TEXTURE_2D, plane_tex_id);
      glDrawArrays(GL_TRIANGLES, 0, 12);
    }

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    {//origin square
      glUseProgram(shader_program);
      glBindTexture(GL_TEXTURE_2D, cube_tex_id);
      s32 tex_id_loc = glGetUniformLocation(shader_program, "TexId");
      glUniform1i(tex_id_loc, 0);
      Vec3 translation_iter = model_translations[0];
      Mat4 model = init_value4m(1.0);
      Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z);
      model = multiply4m(model_translation, model);
      uint32_t model_loc = glGetUniformLocation(shader_program, "Model");
      glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer);
      glBindVertexArray(cube_vao);
      glDrawArrays(GL_TRIANGLES, 0, 36);
    }
    {//square to the left
      glUseProgram(shader_program);
      glBindTexture(GL_TEXTURE_2D, cube_tex_id);
      s32 tex_id_loc = glGetUniformLocation(shader_program, "TexId");
      glUniform1i(tex_id_loc, 0);
      Vec3 translation_iter = model_translations[4];
      Mat4 model = init_value4m(1.0);
      Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z);
      model = multiply4m(model_translation, model);
      uint32_t model_loc = glGetUniformLocation(shader_program, "Model");
      glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer);
      glBindVertexArray(cube_vao);
      glDrawArrays(GL_TRIANGLES, 0, 36);
    }
    {//random square behind window between squares
      glUseProgram(shader_program);
      s32 tex_id_loc = glGetUniformLocation(shader_program, "TexId");
      glUniform1i(tex_id_loc, 0);
      Vec3 translation_iter = model_translations[5];
      Mat4 model = init_value4m(1.0);
      Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z);
      model = multiply4m(model_translation, model);
      uint32_t model_loc = glGetUniformLocation(shader_program, "Model");
      glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer);
      glBindVertexArray(cube_vao);
      glBindTexture(GL_TEXTURE_2D, cube_tex_id);
      glDrawArrays(GL_TRIANGLES, 0, 36);
    }
    { // reflective plane
      glUseProgram(refl_shader_program);
      Vec3 translation_iter = model_translations[6];
      Mat4 model = init_value4m(1.0);
      Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z);

      model = multiply4m(model_translation, model);
      uint32_t model_loc = glGetUniformLocation(refl_shader_program, "Model");
      glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer);

      uint32_t camera_pos_loc = glGetUniformLocation(refl_shader_program, "cameraPos");
      glUniform3fv(camera_pos_loc, 1, camera_pos.data);
      glBindVertexArray(refl_vao);
      glDrawArrays(GL_TRIANGLES, 0, 6);
    }
    { // reflective plane
      glUseProgram(refr_shader_program);
      Vec3 translation_iter = model_translations[7];
      Mat4 model = init_value4m(1.0);
      Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z);

      model = multiply4m(model_translation, model);
      uint32_t model_loc = glGetUniformLocation(refr_shader_program, "Model");
      glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer);

      uint32_t camera_pos_loc = glGetUniformLocation(refr_shader_program, "cameraPos");
      glUniform3fv(camera_pos_loc, 1, camera_pos.data);
      glBindVertexArray(refr_vao);
      glDrawArrays(GL_TRIANGLES, 0, 12);
    }
    {
      glDepthFunc(GL_LEQUAL);
      glUseProgram(cubemap_shader_program);
      glActiveTexture(GL_TEXTURE0);
      glBindVertexArray(skybox_vao);
      glBindTexture(GL_TEXTURE_CUBE_MAP, cm_tex_id);
      glDrawArrays(GL_TRIANGLES, 0, 36);
      glDepthFunc(GL_LESS);
    }

    // @note: this is to demonstrate the limitations of blending with the depth buffer.
    // Currently, this will show a weird behavior where since we render the transparent object
    // closest to the camera first, the second window, further from the player will be discarded by the 
    // depth buffer and will be invisible.
    // This is a limiation of transparent objects with the depth buffer. This does not happen with opaque objects.
    //
    // The "fix" if we can even call it that, is to render objects in order, furthest first and closest second, in layers.
    
    {//window infront of origin square
      glUseProgram(blend_shader_program);
      s32 tex_id_loc = glGetUniformLocation(blend_shader_program, "TexId");
      glUniform1i(tex_id_loc, 2);
      Vec3 translation_iter = model_translations[3];
      Mat4 model = init_value4m(1.0);
      Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z);
      model = multiply4m(model_translation, model);
      uint32_t model_loc = glGetUniformLocation(blend_shader_program, "Model");
      glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer);
      glBindVertexArray(quad_vao);
      glBindTexture(GL_TEXTURE_2D, window_tex_id);
      glDrawArrays(GL_TRIANGLES, 0, 12);
    }
    {//window between squares
      glUseProgram(blend_shader_program);
      s32 tex_id_loc = glGetUniformLocation(blend_shader_program, "TexId");
      glUniform1i(tex_id_loc, 2);
      Vec3 translation_iter = model_translations[2];
      Mat4 model = init_value4m(1.0);
      Mat4 model_translation = translation_matrix4m(translation_iter.x, translation_iter.y, translation_iter.z);
      model = multiply4m(model_translation, model);
      uint32_t model_loc = glGetUniformLocation(blend_shader_program, "Model");
      glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer);
      glBindVertexArray(quad_vao);
      glBindTexture(GL_TEXTURE_2D, window_tex_id);
      glDrawArrays(GL_TRIANGLES, 0, 12);
    }
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    // draw everything to a framebuffer object
    glDisable(GL_DEPTH_TEST);
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 
    glClear(GL_COLOR_BUFFER_BIT);
    {
      glUseProgram(fbo_shader_program);
      glBindVertexArray(fbo_vao);
      // @note: this glActiveTexture call was the bane of my existence for the day, this is done to check this particular
      // thing regarding which texture unit the framebuffer object gets called to and it turns out, it needs a glActiveTexture
      // call (atleast in my case) since I am rendering multiple textures which are bound to different texture units,
      // primarily because of the different shaders I am using. 
      //
      // So this is why I need to call this, but what I could also do is call glActiveTexture when creating the texture 
      // and it would have the same effect as this
      glActiveTexture(GL_TEXTURE0);
      glBindTexture(GL_TEXTURE_2D, fbo_tex);
      s32 tex_id_loc = glGetUniformLocation(fbo_shader_program, "TexId");
      glUniform1i(tex_id_loc, 0);
      glDrawArrays(GL_TRIANGLES, 0, 6);
    }
    glEnable(GL_DEPTH_TEST);
		SDL_GL_SwapWindow(window);
	}
	
	// opengl free calls
	//glDeleteVertexArrays(1, &VAO);
	//glDeleteBuffers(1, &VBO);
	glDeleteProgram(shader_program);
	glDeleteProgram(blend_shader_program);
  glDeleteProgram(fbo_shader_program);
	
	// sdl free calls
	SDL_GL_DeleteContext(context);
	SDL_DestroyWindow(window);
	SDL_Quit();
	return 0;
}