diff options
Diffstat (limited to 'src/cglm_bindings.c')
-rw-r--r-- | src/cglm_bindings.c | 259 |
1 files changed, 235 insertions, 24 deletions
diff --git a/src/cglm_bindings.c b/src/cglm_bindings.c index 8123d7a..95c9d81 100644 --- a/src/cglm_bindings.c +++ b/src/cglm_bindings.c @@ -3,10 +3,16 @@ void honey_setup_cglm(lua_State* L) { honey_lua_element vec3_elements[] = { - { "dot", HONEY_FUNCTION, { .function = honey_cglm_vec3_dot } }, - { "cross", HONEY_FUNCTION, { .function = honey_cglm_vec3_cross } }, - { "square_norm", HONEY_FUNCTION, { .function = honey_cglm_vec3_square_norm } }, - { "norm", HONEY_FUNCTION, { .function = honey_cglm_vec3_norm } }, + { "dot", HONEY_FUNCTION, { .function = honey_cglm_vec3_dot } }, + { "norm2", HONEY_FUNCTION, { .function = honey_cglm_vec3_norm2 } }, + { "norm", HONEY_FUNCTION, { .function = honey_cglm_vec3_norm } }, + { "add", HONEY_FUNCTION, { .function = honey_cglm_vec3_add } }, + { "adds", HONEY_FUNCTION, { .function = honey_cglm_vec3_adds } }, + { "mul", HONEY_FUNCTION, { .function = honey_cglm_vec3_mul } }, + { "muls", HONEY_FUNCTION, { .function = honey_cglm_vec3_muls } }, + { "normalize", HONEY_FUNCTION, { .function = honey_cglm_vec3_normalize } }, + { "distance", HONEY_FUNCTION, { .function = honey_cglm_vec3_distance } }, + { "lerp", HONEY_FUNCTION, { .function = honey_cglm_vec3_lerp } }, }; honey_lua_element vec4_elements[] = { @@ -22,6 +28,17 @@ void honey_setup_cglm(lua_State* L) { "lerp", HONEY_FUNCTION, { .function = honey_cglm_vec4_lerp } }, }; + honey_lua_element mat3_elements[] = { + { "identity", HONEY_FUNCTION, { .function = honey_cglm_mat3_identity } }, + { "mul", HONEY_FUNCTION, { .function = honey_cglm_mat3_mul } }, + { "muls", HONEY_FUNCTION, { .function = honey_cglm_mat3_muls } }, + { "mulv", HONEY_FUNCTION, { .function = honey_cglm_mat3_mulv } }, + { "trans", HONEY_FUNCTION, { .function = honey_cglm_mat3_trans } }, + { "det", HONEY_FUNCTION, { .function = honey_cglm_mat3_det } }, + { "trace", HONEY_FUNCTION, { .function = honey_cglm_mat3_trace } }, + { "inv", HONEY_FUNCTION, { .function = honey_cglm_mat3_inv } }, + }; + honey_lua_element mat4_elements[] = { { "identity", HONEY_FUNCTION, { .function = honey_cglm_mat4_identity } }, { "pick3", HONEY_FUNCTION, { .function = honey_cglm_mat4_pick3 } }, @@ -51,8 +68,9 @@ void honey_setup_cglm(lua_State* L) { "set_value", HONEY_FUNCTION, { .function = honey_cglm_array_set_value } }, { "get_value", HONEY_FUNCTION, { .function = honey_cglm_array_get_value } }, { "copy_array", HONEY_FUNCTION, { .function = honey_cglm_array_copy } }, - { "vec3", HONEY_TABLE, { .table = { 4, vec3_elements } } }, + { "vec3", HONEY_TABLE, { .table = { 10, vec3_elements } } }, { "vec4", HONEY_TABLE, { .table = { 10, vec4_elements } } }, + { "mat3", HONEY_TABLE, { .table = { 8, mat3_elements } } }, { "mat4", HONEY_TABLE, { .table = { 10, mat4_elements } } }, { "affine", HONEY_TABLE, { .table = { 3, affine_elements } } }, { "camera", HONEY_TABLE, { .table = { 2, camera_elements } } }, @@ -133,55 +151,135 @@ int honey_cglm_vec3_dot(lua_State* L) float* a = lua_touserdata(L, 1); float* b = lua_touserdata(L, 2); - float c = glm_vec3_dot(a, b); - lua_pushnumber(L, c); + float dot = glm_vec3_dot(a, b); + lua_pushnumber(L, dot); return 1; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -int honey_cglm_vec3_cross(lua_State* L) +int honey_cglm_vec3_norm2(lua_State* L) { - if (!honey_lua_validate_types(L, 2, HONEY_USERDATA, HONEY_USERDATA)) + if (!honey_lua_validate_types(L, 1, HONEY_USERDATA)) lua_error(L); - float* a = lua_touserdata(L, 1); - float* b = lua_touserdata(L, 2); + float* v = lua_touserdata(L, 1); - float* c = lua_newuserdata(L, 3*sizeof(float)); + float norm2 = glm_vec3_norm2(v); - glm_vec3_cross(a, b, c); + lua_pushnumber(L, norm2); return 1; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -int honey_cglm_vec3_square_norm(lua_State* L) +int honey_cglm_vec3_norm(lua_State* L) { if (!honey_lua_validate_types(L, 1, HONEY_USERDATA)) lua_error(L); - float* a = lua_touserdata(L, 1); + float* v = lua_touserdata(L, 1); - float n2 = glm_vec3_norm2(a); - lua_pushnumber(L, n2); + float norm = glm_vec3_norm(v); + + lua_pushnumber(L, norm); return 1; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_vec3_add(lua_State* L) +{ + float* a, *b, *dest; + honey_lua_parse_arguments(L, 3, + HONEY_USERDATA, &a, + HONEY_USERDATA, &b, + HONEY_USERDATA, &dest); + glm_vec3_add(a, b, dest); + return 0; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -int honey_cglm_vec3_norm(lua_State* L) +int honey_cglm_vec3_adds(lua_State* L) { - if (!honey_lua_validate_types(L, 1, HONEY_USERDATA)) - lua_error(L); + float a, *v, *dest; + honey_lua_parse_arguments(L, 3, + HONEY_NUMBER, &a, + HONEY_USERDATA, &v, + HONEY_USERDATA, &dest); + glm_vec3_adds(v, a, dest); + return 0; +} - float* a = lua_touserdata(L, 1); +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - float n = glm_vec3_norm(a); - lua_pushnumber(L, n); - return 1; +int honey_cglm_vec3_mul(lua_State* L) +{ + float *a, *b, *dest; + honey_lua_parse_arguments(L, 3, + HONEY_USERDATA, &a, + HONEY_USERDATA, &b, + HONEY_USERDATA, &dest); + glm_vec3_mul(a, b, dest); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_vec3_muls(lua_State* L) +{ + float a, *v, *dest; + honey_lua_parse_arguments(L, 3, + HONEY_NUMBER, &a, + HONEY_USERDATA, &v, + HONEY_USERDATA, &dest); + glm_vec3_scale(v, a, dest); + return 0; } +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_vec3_normalize(lua_State* L) +{ + float* a; + honey_lua_parse_arguments(L, 1, HONEY_USERDATA, &a); + + glm_vec3_normalize(a); + + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_vec3_distance(lua_State* L) +{ + float* a, *b; + honey_lua_parse_arguments(L, 2, + HONEY_USERDATA, &a, + HONEY_USERDATA, &b); + + float distance = glm_vec3_distance(a, b); + lua_pushnumber(L, distance); + + return 1; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_vec3_lerp(lua_State* L) +{ + float* a, *b, s, *dest; + honey_lua_parse_arguments(L, 4, + HONEY_USERDATA, &a, + HONEY_USERDATA, &b, + HONEY_NUMBER, &s, + HONEY_USERDATA, &dest); + glm_vec3_lerp(a, b, s, dest); + + return 0; +} + /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * cglm vec4 functions @@ -328,6 +426,119 @@ int honey_cglm_vec4_lerp(lua_State* L) /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * + * cglm mat3 functions + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +int honey_cglm_mat3_identity(lua_State* L) +{ + if (!honey_lua_validate_types(L, 1, HONEY_USERDATA)) + lua_error(L); + + float* matrix = lua_touserdata(L, 1); + glm_mat3_identity(matrix); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_mat3_mul(lua_State* L) +{ + float* A, *B, *dest; + honey_lua_parse_arguments(L, 3, + HONEY_USERDATA, &A, + HONEY_USERDATA, &B, + HONEY_USERDATA, &dest); + glm_mat3_mul(A, B, dest); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_mat3_muls(lua_State* L) +{ + float a; + float* M; + honey_lua_parse_arguments(L, 2, + HONEY_NUMBER, &a, + HONEY_USERDATA, &M); + glm_mat3_scale(M, a); + + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_mat3_mulv(lua_State* L) +{ + float* M, *v, *dest; + honey_lua_parse_arguments(L, 3, + HONEY_USERDATA, &M, + HONEY_USERDATA, &v, + HONEY_USERDATA, &dest); + glm_mat3_mulv(M, v, dest); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_mat3_trans(lua_State* L) +{ + if (!honey_lua_validate_types(L, 1, HONEY_USERDATA)) + lua_error(L); + + float* M = lua_touserdata(L, 1); + + glm_mat3_transpose(M); + + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_mat3_det(lua_State* L) +{ + if (!honey_lua_validate_types(L, 1, HONEY_USERDATA)) + lua_error(L); + + float* M = lua_touserdata(L, 1); + + float det = glm_mat3_det(M); + lua_pushnumber(L, det); + + return 1; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_mat3_trace(lua_State* L) +{ + if (!honey_lua_validate_types(L, 1, HONEY_USERDATA)) + lua_error(L); + + float* M = lua_touserdata(L, 1); + + float trace = glm_mat3_trace(M); + lua_pushnumber(L, trace); + + return 1; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_cglm_mat3_inv(lua_State* L) +{ + float* M, *dest; + honey_lua_parse_arguments(L, 2, + HONEY_USERDATA, &M, + HONEY_USERDATA, &dest); + glm_mat3_inv(M, dest); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * * cglm mat4 functions * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |