transparent-cube

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

commit 539713663796d1162978cef7631ca1fe4c7f783c
parent 7b9b2067b116b6eab9fcb1ba97eabefd05c242fb
Author: amin <dev@aminmesbah.com>
Date:   Sat, 22 Jun 2019 01:50:00 +0000

Discover we don't need sqrtf

Yay

FossilOrigin-Name: a0e94925635a44cca05d8b1ddf84dc55b4aa892ec111718fbc92dceb48f2744d
Diffstat:
Msrc/game.c | 50--------------------------------------------------
Msrc/glmth.c | 41++---------------------------------------
Msrc/glmth.h | 5-----
3 files changed, 2 insertions(+), 94 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -47,56 +47,6 @@ float randf(float min, float max) void game_init(struct GameState *game_state, uint32_t screen_width, uint32_t screen_height) { - // load pyramid vertex data - { - float edge_length = 1.0f; - float height = 1.0f / glmth_sqrtf(2.0f) * edge_length; - float half_length = edge_length / 2.0f; - float half_height = height / 2.0f; - - GLfloat pyramid_vertices[] = { - // positions - -half_length, -half_height, -half_length, - -half_length, -half_height, half_length, - half_length, -half_height, half_length, - half_length, -half_height, -half_length, - 0.0f, half_height, 0.0f, - }; - - GLuint elements[] = { - 0, 3, 2, - 2, 1, 0, - 0, 4, 3, - 3, 4, 2, - 2, 4, 1, - 1, 4, 0, - }; - - GLuint pyramid_vao_id; - glGenVertexArrays(1, &pyramid_vao_id); - glBindVertexArray(pyramid_vao_id); - - GLuint pyramid_vbo_id; - glGenBuffers(1, &pyramid_vbo_id); - glBindBuffer(GL_ARRAY_BUFFER, pyramid_vbo_id); - glBufferData(GL_ARRAY_BUFFER, sizeof(pyramid_vertices), pyramid_vertices, GL_STATIC_DRAW); - - // positions - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(*pyramid_vertices), (GLvoid*)0); - glEnableVertexAttribArray(0); - - GLuint pyramid_ebo_id; - glGenBuffers(1, &pyramid_ebo_id); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pyramid_ebo_id); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); - - glBindVertexArray(0); - - game_state->pyramid_vao_id = pyramid_vao_id; - game_state->pyramid_vbo_id = pyramid_vbo_id; - game_state->pyramid_ebo_id = pyramid_ebo_id; - } - // load cube vertex data { GLfloat cube_vertices[] = { diff --git a/src/glmth.c b/src/glmth.c @@ -115,11 +115,6 @@ v3 glmth_v3_init_f(f32 f) return glmth_v3_init(f, f, f); } -f32 glmth_v3_length(v3 v) -{ - return glmth_sqrtf((v.x * v.x) + (v.y * v.y) + (v.z * v.z)); -} - v3 glmth_v3f_m(v3 v, f32 s) { v3 r = { .x = v.x * s, .y = v.y * s, .z = v.z * s }; @@ -132,15 +127,6 @@ v3 glmth_v3_negate(v3 v) return r; } - -v3 glmth_v3_normalize(v3 v) -{ - f32 l = glmth_v3_length(v); - v3 r = glmth_v3_init(v.x / l, v.y / l, v.z / l); - return r; -} - - void glmth_v3_print(v3 v) { printf("( %f, %f, %f )\n", v.x, v.y, v.z); @@ -266,9 +252,9 @@ m4 glmth_rotate_z(m4 m, f32 rad) } -m4 glmth_rotate(m4 m, f32 rad, v3 axis) +m4 glmth_rotate(m4 m, f32 rad, v3 normalized_axis) { - axis = glmth_v3_normalize(axis); + v3 axis = normalized_axis; f32 c = glmth_cosf(rad); f32 s = glmth_sinf(rad); @@ -386,26 +372,3 @@ m4 glmth_projection_perspective_fov(f32 fovy, f32 aspect, f32 near, f32 far) return glmth_projection_perspective(left, right, bottom, top, near, far); } - - -m4 glmth_camera_look_at(v3 camera_pos, v3 camera_target, v3 up) -{ - v3 camera_direction = glmth_v3_normalize(glmth_v3_s(camera_pos, camera_target)); - v3 camera_right = glmth_v3_normalize(glmth_v3_cross(up, camera_direction)); - v3 camera_up = glmth_v3_cross(camera_direction, camera_right); - - m4 look = glmth_m4_init_id(); - look.E[0][0] = camera_right.x; - look.E[0][1] = camera_right.y; - look.E[0][2] = camera_right.z; - - look.E[1][0] = camera_up.x; - look.E[1][1] = camera_up.y; - look.E[1][2] = camera_up.z; - - look.E[2][0] = camera_direction.x; - look.E[2][1] = camera_direction.y; - look.E[2][2] = camera_direction.z; - - return glmth_m4m4_m(look, glmth_translate(glmth_m4_init_id(), glmth_v3_negate(camera_pos))); -} diff --git a/src/glmth.h b/src/glmth.h @@ -22,11 +22,6 @@ 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);