transparent-cube

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

commit 7b9b2067b116b6eab9fcb1ba97eabefd05c242fb
parent f9ef678d8a6d660b42c5b863c4c070b5b737ae97
Author: amin <dev@aminmesbah.com>
Date:   Sat, 22 Jun 2019 01:43:49 +0000

Wrap math functions

FossilOrigin-Name: fe2889779cf4b34c1ff722433e456388acee6cb7e6e69e8cfa5d0ea026b78667
Diffstat:
Msrc/game.c | 10+++++-----
Msrc/glmth.c | 20++++++++++----------
Msrc/glmth.h | 24++++++++++++++++++++++--
3 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -50,7 +50,7 @@ void game_init(struct GameState *game_state, uint32_t screen_width, uint32_t scr // load pyramid vertex data { float edge_length = 1.0f; - float height = 1.0f / sqrtf(2.0f) * edge_length; + float height = 1.0f / glmth_sqrtf(2.0f) * edge_length; float half_length = edge_length / 2.0f; float half_height = height / 2.0f; @@ -186,9 +186,9 @@ void game_update_and_render(struct GameState *game_state, float dt, uint32_t scr f32 color_freq = dt * 0.1f; v3 pyramid_color = glmth_v3_init( - sinf(color_freq), - sinf(color_freq + (2 * M_PI / 3)), - sinf(color_freq + (4 * M_PI / 3))); + glmth_sinf(color_freq), + glmth_sinf(color_freq + (2 * M_PI / 3)), + glmth_sinf(color_freq + (4 * M_PI / 3))); shader_setm4(&game_state->pyramid_shader, "model", &model); shader_setv3(&game_state->pyramid_shader, "pyramid_color", &pyramid_color); @@ -219,7 +219,7 @@ void game_update_and_render(struct GameState *game_state, float dt, uint32_t scr model = glmth_rotate(model, dt * glmth_rad(angle), glmth_v3_init(0.0f, 1.0f, 0.0f)); model = glmth_rotate(model, dt * glmth_rad(angle), glmth_v3_init(0.0f, 0.0f, 1.0f)); - f32 alpha = 0.2f * (1.5f + sinf(0.5f * dt)); + f32 alpha = 0.2f * (1.5f + glmth_sinf(0.5f * dt)); shader_setm4(&game_state->cube_shader, "model", &model); shader_setf(&game_state->cube_shader, "alpha", alpha); diff --git a/src/glmth.c b/src/glmth.c @@ -117,7 +117,7 @@ v3 glmth_v3_init_f(f32 f) f32 glmth_v3_length(v3 v) { - return sqrtf((v.x * v.x) + (v.y * v.y) + (v.z * v.z)); + return glmth_sqrtf((v.x * v.x) + (v.y * v.y) + (v.z * v.z)); } v3 glmth_v3f_m(v3 v, f32 s) @@ -217,8 +217,8 @@ f32 glmth_rad(f32 deg) m4 glmth_rotate_x(m4 m, f32 rad) { - f32 c = cosf(rad); - f32 s = sinf(rad); + f32 c = glmth_cosf(rad); + f32 s = glmth_sinf(rad); m4 r = glmth_m4_init_id(); @@ -234,8 +234,8 @@ m4 glmth_rotate_x(m4 m, f32 rad) m4 glmth_rotate_y(m4 m, f32 rad) { - f32 c = cosf(rad); - f32 s = sinf(rad); + f32 c = glmth_cosf(rad); + f32 s = glmth_sinf(rad); m4 r = glmth_m4_init_id(); @@ -251,8 +251,8 @@ m4 glmth_rotate_y(m4 m, f32 rad) m4 glmth_rotate_z(m4 m, f32 rad) { - f32 c = cosf(rad); - f32 s = sinf(rad); + f32 c = glmth_cosf(rad); + f32 s = glmth_sinf(rad); m4 r = glmth_m4_init_id(); @@ -270,8 +270,8 @@ m4 glmth_rotate(m4 m, f32 rad, v3 axis) { axis = glmth_v3_normalize(axis); - f32 c = cosf(rad); - f32 s = sinf(rad); + f32 c = glmth_cosf(rad); + f32 s = glmth_sinf(rad); m4 r = glmth_m4_init_id(); @@ -377,7 +377,7 @@ m4 glmth_projection_perspective(f32 left, f32 right, f32 bottom, f32 top, f32 ne m4 glmth_projection_perspective_fov(f32 fovy, f32 aspect, f32 near, f32 far) { - f32 half_height = tanf(fovy / 2.0f) * near; + f32 half_height = glmth_tanf(fovy / 2.0f) * near; f32 half_width = half_height * aspect; f32 left = -half_width; f32 right = half_width; diff --git a/src/glmth.h b/src/glmth.h @@ -5,8 +5,6 @@ #include <stdbool.h> #include <stdint.h> -#include <math.h> - #ifndef M_PI #define M_PI 3.14159265359f #endif @@ -22,6 +20,28 @@ typedef int64_t s64; typedef float f32; typedef double r64; +#include <math.h> + +static inline f32 glmth_sqrtf(f32 x) +{ + return sqrtf(x); +} + +static inline f32 glmth_sinf(f32 x) +{ + return sinf(x); +} + +static inline f32 glmth_cosf(f32 x) +{ + return cosf(x); +} + +static inline f32 glmth_tanf(f32 x) +{ + return tanf(x); +} + typedef union { struct