summaryrefslogtreecommitdiff
path: root/source/lessons/models/main.cpp
blob: 18a816b12b770fcc89fb0a979327c3b770a076aa (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
#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:
*  - 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;
}

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;
}

// =================== 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)
{
  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;
}

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);
    
	// 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/model/model.vs.glsl", &read_count);
  char* fragment_source = (char*)SDL_LoadFile("./source/shaders/model/model.fs.glsl", &read_count);
    
	GLuint vertex_shader = gl_create_vertex_shader(vertex_source);
	GLuint fragment_shader = gl_create_fragment_shader(fragment_source);
	GLuint shader_program = gl_create_shader_program(vertex_shader, fragment_shader);
	printf("Successfully compiled shaders.\n");
    
  glUseProgram(shader_program);

  stbi_set_flip_vertically_on_load(1);
  // ============ Start Model handling using Assimp ============
  // loading a 3d model using assimp
  Model test_model = Model(std::string("assets/Survival_Backpack/backpack.obj"));

  // directional light things
  // - directional light params
  Vec3 DL_direction =   Vec3{ 0.0f, -0.5f, -1.0f };
  Vec3 DL_ambient   =   Vec3{ 0.2f, 0.2f, 0.2f };
  Vec3 DL_diffuse   =   Vec3{ 0.5f, 0.5f, 0.5f };
  Vec3 DL_specular  =   Vec3{ 1.0f, 1.0f, 1.0f };

  int DL_ambient_loc  = glGetUniformLocation(shader_program, "dirLight.ambient");
  int DL_diffuse_loc  = glGetUniformLocation(shader_program, "dirLight.diffuse");
  int DL_specular_loc = glGetUniformLocation(shader_program, "dirLight.specular");
  int DL_dir_loc      = glGetUniformLocation(shader_program, "dirLight.direction");

  glUniform3fv(DL_dir_loc, 1, DL_direction.data);
  glUniform3fv(DL_ambient_loc, 1, DL_ambient.data);
  glUniform3fv(DL_diffuse_loc, 1, DL_diffuse.data);
  glUniform3fv(DL_specular_loc, 1, DL_specular.data);
  
  // load point light
  Vec3 PL_position = Vec3{ 0.0f, 0.0f, 3.0f };
  Vec3 PL_ambient  = Vec3{ 0.2f, 0.2f, 0.2f };
  Vec3 PL_diffuse  = Vec3{ 0.5f, 0.5f, 0.5f };
  Vec3 PL_specular = Vec3{ 1.0f, 1.0f, 1.0f };

  s32 PL_pos_loc      = glGetUniformLocation(shader_program, "pointLight.position");
  s32 PL_ambient_loc  = glGetUniformLocation(shader_program, "pointLight.ambient");
  s32 PL_diffuse_loc  = glGetUniformLocation(shader_program, "pointLight.diffuse");
  s32 PL_specular_loc = glGetUniformLocation(shader_program, "pointLight.specular");
  s32 PL_kc_loc       = glGetUniformLocation(shader_program, "pointLight.kC");
  s32 PL_kl_loc       = glGetUniformLocation(shader_program, "pointLight.kL");
  s32 PL_kq_loc       = glGetUniformLocation(shader_program, "pointLight.kQ");

  glUniform3fv(PL_pos_loc,      1, PL_position.data);
  glUniform3fv(PL_ambient_loc,  1, PL_ambient.data);
  glUniform3fv(PL_diffuse_loc,  1, PL_diffuse.data);
  glUniform3fv(PL_specular_loc, 1, PL_specular.data);
  // attenuation factors
  glUniform1f(PL_kc_loc, 1.0f);
  glUniform1f(PL_kl_loc, 0.09f);
  glUniform1f(PL_kq_loc, 0.032f);

	int camera_pos_loc = glGetUniformLocation(shader_program, "cameraPosition");

	// objects
	Vec3 model_translations[] = {
		Vec3{  0.0, 0.0,  0.0},
		Vec3{ -1.0, 0.0,  -2.0},
		Vec3{  2.0,  0.0, -5.0},
		Vec3{ -3.0,  5.0, -6.0},
		Vec3{  3.0, -7.0, -6.0},
	};
    
	r32 FOV = 90.0;
	r32 time_curr;
	r32 time_prev = SDL_GetTicks64() / 100.0;
	uint32_t model_loc = glGetUniformLocation(shader_program, "Model");
    
	// camera stuff
	Vec3 camera_pos  = Vec3{ 0.0, 5.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);
	
	uint32_t view_loc = glGetUniformLocation(shader_program, "View");
	glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer);
    
	Mat4 proj = perspective4m((r32)To_Radian(90.0), (r32)width / (r32)height, 0.1f, 100.0f);
	uint32_t proj_loc = glGetUniformLocation(shader_program, "Projection");
	glUniformMatrix4fv(proj_loc, 1, GL_TRUE, proj.buffer);
	
	glEnable(GL_DEPTH_TEST);
    
	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
		glUseProgram(shader_program);
        
		glUniformMatrix4fv(view_loc, 1, GL_TRUE, view.buffer);
    glUniform3fv(camera_pos_loc, 1, camera_pos.data);
		
		time_prev = time_curr;
		
		// OUTPUT
		glClearColor(1.0f, 0.6f, .6f, 1.0f);
		//glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
		for (int i = 0; i < 1; i++)
		{
			Vec3 translation_iter = model_translations[i];
			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);
			glUniformMatrix4fv(model_loc, 1, GL_TRUE, model.buffer);
      test_model.draw(shader_program);
		}
        
		SDL_GL_SwapWindow(window);
	}
	
	// opengl free calls
	//glDeleteVertexArrays(1, &VAO);
	//glDeleteBuffers(1, &VBO);
	glDeleteProgram(shader_program);
	
	// sdl free calls
	SDL_GL_DeleteContext(context);
	SDL_DestroyWindow(window);
	SDL_Quit();
	return 0;
}