game.c (4780B)
1 #include "game.h" 2 3 #ifndef GAME_WEBGL 4 #include "glad.c" 5 #endif 6 7 #include "shader.c" 8 9 void game_init(struct GameState *game_state, u32 screen_width, u32 screen_height) 10 { 11 // load cube vertex data 12 { 13 f32 cube_vertices[] = { 14 // positions // colors 15 -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 16 -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 17 -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 18 -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 19 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 20 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 21 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 22 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 23 }; 24 25 u32 elements[] = { 26 0, 4, 6, 6, 2, 0, 27 1, 5, 7, 7, 3, 1, 28 3, 2, 0, 0, 1, 3, 29 7, 6, 4, 4, 5, 7, 30 0, 4, 5, 5, 1, 0, 31 2, 6, 7, 7, 3, 2, 32 }; 33 34 u32 cube_vao; 35 glGenVertexArrays(1, &cube_vao); 36 glBindVertexArray(cube_vao); 37 38 u32 cube_vbo; 39 glGenBuffers(1, &cube_vbo); 40 glBindBuffer(GL_ARRAY_BUFFER, cube_vbo); 41 glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW); 42 43 // positions 44 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(*cube_vertices), (GLvoid*)0); 45 glEnableVertexAttribArray(0); 46 47 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(*cube_vertices), (GLvoid*)(3 * sizeof(*cube_vertices))); 48 glEnableVertexAttribArray(1); 49 50 u32 cube_ebo; 51 glGenBuffers(1, &cube_ebo); 52 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube_ebo); 53 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); 54 55 56 game_state->cube_vao = cube_vao; 57 game_state->cube_vbo = cube_vbo; 58 game_state->cube_ebo = cube_ebo; 59 } 60 61 // game_shader_load 62 { 63 struct PlatformApi platform = game_state->platform; 64 char *v_source = platform.platform_read_entire_file("shader/cube_v.glsl"); 65 char *f_source = platform.platform_read_entire_file("shader/cube_f.glsl"); 66 print = platform.platform_print; 67 struct Shader main_shader = {0}; 68 // TODO: Check this result 69 shader_compile(v_source, f_source, &main_shader); 70 platform.platform_memory_free(v_source); 71 platform.platform_memory_free(f_source); 72 game_state->cube_shader = main_shader; 73 } 74 75 //glEnable(GL_DEPTH_TEST); 76 77 #ifndef GAME_WEBGL 78 //glEnable(GL_MULTISAMPLE); 79 #endif 80 } 81 82 void game_update_and_render(struct GameState *game_state, float dt, u32 screen_width, u32 screen_height) 83 { 84 glDepthMask(GL_TRUE); 85 glClearColor(0.2f, 0.2f, 0.3f, 1.0f); 86 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 87 88 //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 89 90 m4 view = glmth_m4_init_id(); 91 m4 projection = glmth_m4_init_id(); 92 view = glmth_translate(view, glmth_v3_init(0.0f, 0.0f, -3.0f)); 93 projection = glmth_projection_perspective_fov(glmth_rad(45.0f), (float)screen_width / (float)screen_height, 0.1f, 100.0f); 94 95 // render cube 96 { 97 // Depth testing causes the blending to be applied inconsistently 98 // depending on the draw order, so we must disable it. 99 glDisable(GL_DEPTH_TEST); 100 101 glEnable(GL_BLEND); 102 // TODO: get this to work regardless of background color 103 glBlendColor(0.0f, 0.0f, 0.0f, 1.0f); 104 glBlendFunc(GL_SRC_ALPHA, GL_CONSTANT_ALPHA); 105 106 shader_use(&game_state->cube_shader); 107 shader_setm4(&game_state->cube_shader, SHADER_UNIFORM_VIEW, &view); 108 shader_setm4(&game_state->cube_shader, SHADER_UNIFORM_PROJECTION, &projection); 109 110 m4 model = glmth_m4_init_id(); 111 f32 angle = 20.0f; 112 // TODO: fmodf this so we don't get huge numbers eventually 113 f32 rot_rad = dt * glmth_rad(angle); 114 model = glmth_rotate(model, rot_rad, glmth_v3_init(1.0f, 0.0f, 0.0f)); 115 model = glmth_rotate(model, rot_rad, glmth_v3_init(0.0f, 1.0f, 0.0f)); 116 model = glmth_rotate(model, rot_rad, glmth_v3_init(0.0f, 0.0f, 1.0f)); 117 118 f32 alpha = 0.2f * (1.5f + glmth_sinf(0.5f * dt)); 119 shader_setm4(&game_state->cube_shader, SHADER_UNIFORM_MODEL, &model); 120 shader_setf(&game_state->cube_shader, SHADER_UNIFORM_ALPHA, alpha); 121 122 glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); 123 124 glEnable(GL_DEPTH_TEST); 125 glDisable(GL_BLEND); 126 } 127 } 128 129 void game_cleanup(struct GameState *game_state) 130 { 131 glDeleteVertexArrays(1, &game_state->cube_vao); 132 glDeleteBuffers(1, &game_state->cube_vbo); 133 glDeleteBuffers(1, &game_state->cube_ebo); 134 } 135 136 #ifdef PLATFORM_HOTLOAD_GAME_CODE 137 void game_load_opengl_symbols(void) 138 { 139 gladLoadGL(); 140 } 141 #endif