From 306e6cca8e49638779b111d66852877416971a59 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Sun, 29 Nov 2020 22:38:45 -0600 Subject: add camera functions and fix bug in argument parsing --- src/glm_bindings.c | 6 +- src/glm_bindings.h | 8 +++ src/glm_mat4_bindings.c | 98 +++++++++++++++++++++++++++++++++ src/honey_lua.c | 142 ++++++++++++++++++++++-------------------------- 4 files changed, 177 insertions(+), 77 deletions(-) (limited to 'src') diff --git a/src/glm_bindings.c b/src/glm_bindings.c index 41e1d4d..a3ce419 100644 --- a/src/glm_bindings.c +++ b/src/glm_bindings.c @@ -87,7 +87,7 @@ void honey_setup_glm(lua_State* L) honey_lua_create_table (L, 2, - HONEY_TABLE, "__index", 16, + HONEY_TABLE, "__index", 21, HONEY_FUNCTION, "copy", honey_glm_mat4_copy, HONEY_FUNCTION, "eye", honey_glm_mat4_eye, HONEY_FUNCTION, "zero", honey_glm_mat4_zero, @@ -105,6 +105,10 @@ void honey_setup_glm(lua_State* L) HONEY_FUNCTION, "rotateY", honey_glm_rotate_y, HONEY_FUNCTION, "rotateZ", honey_glm_rotate_z, HONEY_FUNCTION, "rotate", honey_glm_rotate, + HONEY_FUNCTION, "perspective", honey_glm_perspective, + HONEY_FUNCTION, "perspectiveResize", honey_glm_perspective_resize, + HONEY_FUNCTION, "lookAt", honey_glm_lookat, + HONEY_FUNCTION, "look", honey_glm_look, HONEY_FUNCTION, "__gc", honey_glm_array_destroy); honey_glm_mat4_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX); diff --git a/src/glm_bindings.h b/src/glm_bindings.h index c9eaa91..6c1898f 100644 --- a/src/glm_bindings.h +++ b/src/glm_bindings.h @@ -188,4 +188,12 @@ int honey_glm_rotate_z(lua_State* L); int honey_glm_rotate(lua_State* L); +int honey_glm_perspective(lua_State* L); + +int honey_glm_perspective_resize(lua_State* L); + +int honey_glm_lookat(lua_State* L); + +int honey_glm_look(lua_State* L); + #endif diff --git a/src/glm_mat4_bindings.c b/src/glm_mat4_bindings.c index f10f954..460cde2 100644 --- a/src/glm_mat4_bindings.c +++ b/src/glm_mat4_bindings.c @@ -397,3 +397,101 @@ int honey_glm_rotate(lua_State* L) return 0; } +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * Camera functions + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +int honey_glm_perspective(lua_State* L) +{ + honey_glm_array *self; + float fov, aspect, near, far; + honey_lua_parse_arguments + (L, 1, 5, + HONEY_USERDATA, &self, + HONEY_NUMBER, &fov, HONEY_NUMBER, &aspect, + HONEY_NUMBER, &near, HONEY_NUMBER, &far); + + glm_perspective(fov, aspect, near, far, self->data); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_glm_perspective_resize(lua_State* L) +{ + honey_glm_array *self; + float new_aspect; + honey_lua_parse_arguments + (L, 1, 2, HONEY_USERDATA, &self, HONEY_NUMBER, new_aspect); + + glm_perspective_resize(new_aspect, self->data); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_glm_lookat(lua_State* L) +{ + honey_glm_array *self, *eye, *center, *up; + honey_lua_parse_arguments + (L, 1, 4, + HONEY_USERDATA, &self, + HONEY_USERDATA, &eye, + HONEY_USERDATA, ¢er, + HONEY_USERDATA, &up); + + if (eye->type != VEC3) + honey_lua_throw_error + (L, "eye vector must be of type VEC3 (%d); got %d instead", + VEC3, eye->type); + + if (center->type != VEC3) + honey_lua_throw_error + (L, "center vector must be of type VEC3 (%d); got %d instead", + VEC3, center->type); + + if (up->type != VEC3) + honey_lua_throw_error + (L, "up vector must be of type VEC3 (%d); got %d instead", + VEC3, up->type); + + glm_lookat(eye->data, center->data, up->data, self->data); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_glm_look(lua_State* L) +{ + honey_glm_array *self, *eye, *dir, *up; + honey_lua_parse_arguments + (L, 1, 4, + HONEY_USERDATA, &self, + HONEY_USERDATA, &eye, + HONEY_USERDATA, &dir, + HONEY_USERDATA, &up); + + if (eye->type != VEC3) + honey_lua_throw_error + (L, "eye vector must be of type VEC3 (%d); got %d instead", + VEC3, eye->type); + + if (dir->type != VEC3) + honey_lua_throw_error + (L, "direction vector must be of type VEC3 (%d); got %d instead", + VEC3, dir->type); + + if (up->type != VEC3) + honey_lua_throw_error + (L, "up vector must be of type VEC3 (%d); got %d instead", + VEC3, up->type); + + glm_look(eye->data, dir->data, up->data, self->data); + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + diff --git a/src/honey_lua.c b/src/honey_lua.c index 2469d37..72624be 100644 --- a/src/honey_lua.c +++ b/src/honey_lua.c @@ -25,8 +25,7 @@ struct argument_list { */ /* string must be able to hold at least 16 characters. */ -static int type_to_string(char* string, - honey_lua_type type); +static const char* type_to_string(honey_lua_type type); static bool check_argument(lua_State* L, honey_lua_type type, @@ -100,34 +99,6 @@ void honey_lua_throw_error(lua_State* L, * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -static void honey_lua_arg_error(lua_State* L, - honey_lua_type type, - int position) -{ - char expected_type[16]; - type_to_string(expected_type, type); - - char* error_message; - honey_result result; - char* got_type = (char*) lua_typename(L, lua_type(L, position)); - result = honey_format_string(&error_message, - "bad argument in position %d: " - "expected %s, but got %s instead.", - position, - expected_type, - got_type); - if (result != HONEY_OK) { - lua_pushstring(L, "error allocating memory for error message :("); - } - else { - lua_pushstring(L, error_message); - free(error_message); - } - lua_error(L); -} - -/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ - static bool check_arg_list(lua_State* L, struct argument_list arg_list) { @@ -141,28 +112,22 @@ static bool check_arg_list(lua_State* L, /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -static int arg_list_to_string(char* string, - int start_index, +static void arg_list_to_string(char** string, struct argument_list arg_list) { struct argument_pair* args = arg_list.args; - string[start_index] = '('; + size_t size = sizeof(char) * (18*arg_list.length + 5); + *string = malloc(size); + + memcpy(*string, "(", 2); - int index = start_index + 1; - for (int i=0; i