transparent-cube

Minimal cross-platform native/wasm graphics example.
git clone git://git.amin.space/transparent-cube.git
Log | Files | Refs | README | LICENSE

commit 4268464c78aa655b36928a7e5cb03b426bd103e5
parent daa817605484e5a9abe9d242872b69ea3ebcac08
Author: amin <dev@aminmesbah.com>
Date:   Fri, 21 Jun 2019 02:23:55 +0000

Produce the first flicker of life

FossilOrigin-Name: 7a8f9a1155a334df96af6a286a1a15b4e01090095931b1882e924e6ecf4b38e1
Diffstat:
Msrc/game.c | 6++----
Msrc/game.h | 14+++++++-------
Msrc/glmth.h | 34+++++++++++++++++++++++++++++++++-
Msrc/platform_wasm.c | 5++++-
4 files changed, 46 insertions(+), 13 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -52,7 +52,6 @@ void game_init(struct GameState *game_state, u32 screen_width, u32 screen_height glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube_ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); - glBindVertexArray(0); game_state->cube_vao = cube_vao; game_state->cube_vbo = cube_vbo; @@ -91,7 +90,8 @@ void game_update_and_render(struct GameState *game_state, float dt, u32 screen_w m4 view = glmth_m4_init_id(); m4 projection = glmth_m4_init_id(); view = glmth_translate(view, glmth_v3_init(0.0f, 0.0f, -3.0f)); - projection = glmth_projection_perspective_fov(glmth_rad(45.0f), (float)screen_width / (float)screen_height, 0.1f, 100.0f); + //projection = glmth_projection_perspective_fov(glmth_rad(45.0f), (float)screen_width / (float)screen_height, 0.1f, 100.0f); + projection = glmth_projection_ortho(0.0f, screen_width, 0.0f, screen_height, -10.0f, 10.0f); // render cube { @@ -118,9 +118,7 @@ void game_update_and_render(struct GameState *game_state, float dt, u32 screen_w shader_setm4(&game_state->cube_shader, "model", &model); shader_setf(&game_state->cube_shader, "alpha", alpha); - glBindVertexArray(game_state->cube_vao); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); - glBindVertexArray(0); glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); diff --git a/src/game.h b/src/game.h @@ -9,29 +9,29 @@ // TODO: fix this #define assert(x) (void)0 -inline float sinf(float a) +inline float sinf(float x) { - return a; + return x; } -inline float cosf(float a) +inline float cosf(float x) { - return a; + return x; } inline float powf(float x, float p) { - return x; + return x * x; } inline float sqrtf(float a) { - return a; + return a * 0.5f; } inline float tanf(float a) { - return a; + return 0.4142135623730950488f; } typedef _Bool bool; diff --git a/src/glmth.h b/src/glmth.h @@ -156,7 +156,7 @@ static inline f32 glmth_rad(f32 deg) static inline m4 glmth_rotate(m4 m, f32 rad, v3 axis) { - axis = glmth_v3_normalize(axis); + //axis = glmth_v3_normalize(axis); f32 c = cosf(rad); f32 s = sinf(rad); @@ -187,6 +187,38 @@ static inline m4 glmth_translate(m4 m, v3 v) return glmth_m4m4_m(m, r); } +static inline m4 glmth_projection_ortho(f32 left, f32 right, f32 bottom, f32 top, f32 near, f32 far) +{ + // TODO: This assert fails when you minimise the window in Windows. + assert(left != right); + assert(bottom != top); + assert(near != far); + + m4 r = glmth_m4_init_id(); + + r.E[0][0] = 2.0f / (right - left); + r.E[0][1] = 0.0f; + r.E[0][2] = 0.0f; + r.E[0][3] = -(right + left) / (right - left); + + r.E[1][0] = 0.0f; + r.E[1][1] = 2.0f / (top - bottom); + r.E[1][2] = 0.0f; + r.E[1][3] = -(top + bottom) / (top - bottom); + + r.E[2][0] = 0.0f; + r.E[2][1] = 0.0f; + r.E[2][2] = -2.0f / (far - near); + r.E[2][3] = -(far + near) / (far - near); + + r.E[3][0] = 0.0f; + r.E[3][1] = 0.0f; + r.E[3][2] = 0.0f; + r.E[3][3] = 1.0f; + + return r; +} + static inline m4 glmth_projection_perspective(f32 left, f32 right, f32 bottom, f32 top, f32 near, f32 far) { assert(left != right); diff --git a/src/platform_wasm.c b/src/platform_wasm.c @@ -12,6 +12,8 @@ i32 g_width = PLATFORM_SCR_WIDTH; i32 g_height = PLATFORM_SCR_HEIGHT; char g_mem_buffer[1000] = {0}; i32 g_mem_buffer_i = 0; +u32 time = 0; + export bool init(void) { @@ -24,7 +26,8 @@ export bool init(void) export void render(void) { - game_update_and_render(&g_game_state, 16, g_width, g_height); + time += 16; + game_update_and_render(&g_game_state, time, g_width, g_height); } export void window_resize(int w, int h)