From ec00c661a39073ba24a78ba7624009bc912ae572 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Tue, 27 Oct 2020 18:16:24 -0500 Subject: add cglm bindings for vec3 and mat3 functions --- demo/main.lua | 17 ++-- src/cglm_bindings.c | 259 +++++++++++++++++++++++++++++++++++++++++++++++----- src/cglm_bindings.h | 170 ++++++++++++++++++++++++++++++---- 3 files changed, 399 insertions(+), 47 deletions(-) diff --git a/demo/main.lua b/demo/main.lua index f3a3c4a..1be7eaf 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -4,6 +4,15 @@ end honey.input.key.bind(honey.input.key.escape, honey.exit) +local vec4 = function(v) + local vector = honey.cglm.new_array_zero(4) + honey.cglm.set_value(vector, 0, v[1]) + honey.cglm.set_value(vector, 1, v[2]) + honey.cglm.set_value(vector, 2, v[3]) + honey.cglm.set_value(vector, 3, v[4]) + return vector +end + local vertex_shader = [[ #version 330 core @@ -24,12 +33,8 @@ void main() { color = base_color; } ]] local shader = honey.shader.new(vertex_shader, fragment_shader) local plane = honey.primitives.plane(2,2) -local color1 = honey.cglm.new_array_zero(4) -honey.cglm.set_value(color1, 3, 1) -local color2 = honey.cglm.copy_array(color1, 4) -honey.cglm.set_value(color1, 0, 1) -honey.cglm.set_value(color2, 2, 1) - +local color1 = vec4{1,0,0,1} +local color2 = vec4{0,0,1,1} local color = honey.cglm.new_array_zero(4) local total_time = 0 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 @@ -326,6 +424,119 @@ int honey_cglm_vec4_lerp(lua_State* L) return 0; } +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * 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 diff --git a/src/cglm_bindings.h b/src/cglm_bindings.h index e8edf2f..e27eb68 100644 --- a/src/cglm_bindings.h +++ b/src/cglm_bindings.h @@ -60,42 +60,101 @@ int honey_cglm_array_copy(lua_State* L); * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -/** @brief Dot product of a and b. +/** @brief Compute the dot product of two vectors. * * @param[in] a Vector a. * @param[in] b Vector b. * - * @returns The result of dot(a, b) + * @returns dot(a,b). */ int honey_cglm_vec3_dot(lua_State* L); -/** @brief Cross product of a and b. +/** @brief Compute the square of the norm of a vector. * - * @param[in] a Vector a. - * @param[in] b Vector b. + * Use this if you are tempted to compute norm*norm; + * it avoids the two calls to sqrt. + * + * @param[in] v The vector to compute norm^2 for. * - * @returns vec3 of cross(a, b). + * @returns norm(v)^2. */ -int honey_cglm_vec3_cross(lua_State* L); +int honey_cglm_vec3_norm2(lua_State* L); -/** @brief Compute the square of the norm of a vector. +/** @brief Compute the norm of a vector. + * + * @param[in] v The vector. * - * This function is useful if you want norm*norm - * because it avoids the overhead of two sqrt calls. + * @returns norm(v). + */ +int honey_cglm_vec3_norm(lua_State* L); + +/** @brief Add two vectors together. * - * @param a Vector a. + * @param[in] a The first vector. + * @param[in] b The second vector. + * @param[out] dest vec3 to fill with a + b. * - * @returns The square of norm(a). + * @returns Nothing. */ -int honey_cglm_vec3_square_norm(lua_State* L); +int honey_cglm_vec3_add(lua_State* L); -/** @brief Compute the L2 norm of a vector. +/** @brief Add a scalar to a vector. * - * @param a Vector a. + * @param[in] a The scalar. + * @param[in] v The vector. + * @param[out] dest vec3 to fill with a + v * - * @returns The norm(a). + * @returns Nothing. */ -int honey_cglm_vec3_norm(lua_State* L); +int honey_cglm_vec3_adds(lua_State* L); + +/** @param Component-wise multiply two vectors together. + * + * @param[in] a The first vector. + * @param[in] b The second vector. + * @param[out] dest vec3 to fill with [ a.x*b.x, a.y*b.y, a.z*b.z, a.w*b.w ] + * + * @returns Nothing. + */ +int honey_cglm_vec3_mul(lua_State* L); + +/** @brief Multiply a vector by a scalar. + * + * @param[in] a The scalar. + * @param[in] v The vector. + * @param[out] dest vec3 to fill with a*v. + * + * @returns Nothing. + */ +int honey_cglm_vec3_muls(lua_State* L); + +/** @brief Normalize a vector. + * + * @param[in,out] v The vector. + * + * @returns Nothing. + */ +int honey_cglm_vec3_normalize(lua_State* L); + +/** @brief Compute the distance between two vectors. + * + * @param[in] a The first vector. + * @param[in] b The second vector. + * + * @returns norm(a-b). + */ +int honey_cglm_vec3_distance(lua_State* L); + +/** @brief Linearly interpolate between two values. + * + * @param[in] a The first vector. + * @param[in] b The second vector. + * @param[in] s A scalar. + * @param[out] dest vec3 to fill with a + s*(b-s). + * + * @returns Nothing. + */ +int honey_cglm_vec3_lerp(lua_State* L); /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * @@ -200,6 +259,83 @@ int honey_cglm_vec4_distance(lua_State* L); */ int honey_cglm_vec4_lerp(lua_State* L); +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * cglm mat3 functions + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +/** @brief Set a matrix to be the identity matrix. + * + * @param[out] matrix The matrix to set to the identity. + * + * @returns Nothing. + */ +int honey_cglm_mat3_identity(lua_State* L); + +/** @brief Multiply two mat3s together. + * + * @param[in] A The first matrix. + * @param[in] B The second matrix. + * @param[out] dest mat3 to fill with A*B. + * + * @returns Nothing. + */ +int honey_cglm_mat3_mul(lua_State* L); + +/** @brief Multiply a matrix by a scalar. + * + * @param[in] a The scalar. + * @param[in,out] M The matrix. + * + * @returns Nothing. + */ +int honey_cglm_mat3_muls(lua_State* L); + +/** @brief Multiply a matrix by a column vector. + * + * @param[in] M The matrix. + * @param[in] v The column vector. + * @param[out] dest Matrix to fill with M*v. + * + * @returns Nothing. + */ +int honey_cglm_mat3_mulv(lua_State* L); + +/** @brief Transpose a matrix. + * + * @param[in,out] M The matrix to transpose. + * + * @returns Nothing. + */ +int honey_cglm_mat3_trans(lua_State* L); + +/** @brief Get the determinant of a matrix. + * + * @param[in] M The matrix. + * + * @returns det(M). + */ +int honey_cglm_mat3_det(lua_State* L); + +/** @brief Get the trace of a matrix. + * + * @param[in] M The matrix. + * + * @returns trace(M). + */ +int honey_cglm_mat3_trace(lua_State* L); + +/** @brief Get the inverse of a matrix. + * + * @param[in] M The matrix to invert. + * @param[out] dest Matrix to fill with inv(M). + * + * @returns Nothing. + */ +int honey_cglm_mat3_inv(lua_State* L); + /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * cglm mat4 functions -- cgit v1.2.1