diff options
author | sanine-a <sanine.not@pm.me> | 2020-12-03 02:10:32 -0600 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-12-03 02:10:32 -0600 |
commit | e767e1ff9d81f8a76196caab91d9b6b4bbe0b30e (patch) | |
tree | 25a058779bcd4de00ce04ccf6cb0157cfba32611 /src | |
parent | f417d2b047369cac234c471ef37ab8281181d69f (diff) |
rewrite demo
Diffstat (limited to 'src')
-rw-r--r-- | src/glm_bindings.c | 13 | ||||
-rw-r--r-- | src/glm_bindings.h | 4 | ||||
-rw-r--r-- | src/glm_mat4_bindings.c | 41 | ||||
-rw-r--r-- | src/glm_vec3_bindings.c | 27 | ||||
-rw-r--r-- | src/honey.c | 18 | ||||
-rw-r--r-- | src/input.c | 17 | ||||
-rw-r--r-- | src/mesh.c | 12 | ||||
-rw-r--r-- | src/primitives.c | 4 | ||||
-rw-r--r-- | src/scene_tree_node.c | 103 | ||||
-rw-r--r-- | src/shader.c | 15 | ||||
-rw-r--r-- | src/texture.c | 18 |
11 files changed, 183 insertions, 89 deletions
diff --git a/src/glm_bindings.c b/src/glm_bindings.c index 6893534..aa40fff 100644 --- a/src/glm_bindings.c +++ b/src/glm_bindings.c @@ -10,8 +10,8 @@ int honey_glm_UNIT_Y_ref = LUA_NOREF; int honey_glm_UNIT_Z_ref = LUA_NOREF; static void create_vec3(lua_State* L, - int x, int y, int z, - int* ref) + int x, int y, int z, + int* ref) { lua_createtable(L, 3, 0); @@ -32,12 +32,14 @@ static void create_vec3(lua_State* L, lua_pop(L, 1); } +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + void honey_setup_glm(lua_State* L) { /* vec3 metatable */ honey_lua_create_table (L, 3, - HONEY_TABLE, "__index", 23, + HONEY_TABLE, "__index", 24, HONEY_FUNCTION, "get", honey_glm_array_vec_get, HONEY_FUNCTION, "set", honey_glm_array_vec_set, HONEY_FUNCTION, "copyTo", honey_glm_vec3_copy, @@ -53,6 +55,7 @@ void honey_setup_glm(lua_State* L) HONEY_FUNCTION, "sub", honey_glm_vec3_sub, HONEY_FUNCTION, "subs", honey_glm_vec3_subs, HONEY_FUNCTION, "mul", honey_glm_vec3_mul, + HONEY_FUNCTION, "muls", honey_glm_vec3_muls, HONEY_FUNCTION, "scale", honey_glm_vec3_scale, HONEY_FUNCTION, "scaleAs", honey_glm_vec3_scale_as, HONEY_FUNCTION, "div", honey_glm_vec3_div, @@ -124,7 +127,7 @@ void honey_setup_glm(lua_State* L) honey_lua_create_table (L, 3, - HONEY_TABLE, "__index", 24, + HONEY_TABLE, "__index", 25, HONEY_FUNCTION, "get", honey_glm_array_mat_get, HONEY_FUNCTION, "set", honey_glm_array_mat_set, HONEY_FUNCTION, "copyTo", honey_glm_mat4_copy, @@ -136,6 +139,7 @@ void honey_setup_glm(lua_State* L) HONEY_FUNCTION, "scale", honey_glm_mat4_scale, HONEY_FUNCTION, "det", honey_glm_mat4_det, HONEY_FUNCTION, "inv", honey_glm_mat4_inv, + HONEY_FUNCTION, "basis", honey_glm_mat4_basis, HONEY_FUNCTION, "translate", honey_glm_translate, HONEY_FUNCTION, "translateX", honey_glm_translate_x, HONEY_FUNCTION, "translateY", honey_glm_translate_y, @@ -442,7 +446,6 @@ int honey_glm_array_destroy(lua_State* L) { honey_glm_array* array; honey_lua_parse_arguments(L, 1, 1, HONEY_USERDATA, &array); - printf("DESTROY GLM ARRAY %d", array->type); free(array->data); return 0; } diff --git a/src/glm_bindings.h b/src/glm_bindings.h index b04016e..21d92b9 100644 --- a/src/glm_bindings.h +++ b/src/glm_bindings.h @@ -76,6 +76,8 @@ int honey_glm_vec3_sub(lua_State* L); int honey_glm_vec3_subs(lua_State* L); int honey_glm_vec3_mul(lua_State* L); + +int honey_glm_vec3_muls(lua_State* L); int honey_glm_vec3_scale(lua_State* L); @@ -186,6 +188,8 @@ int honey_glm_mat4_det(lua_State* L); int honey_glm_mat4_inv(lua_State* L); +int honey_glm_mat4_basis(lua_State* L); + int honey_glm_translate(lua_State* L); int honey_glm_translate_x(lua_State* L); diff --git a/src/glm_mat4_bindings.c b/src/glm_mat4_bindings.c index b082a4b..8320dcb 100644 --- a/src/glm_mat4_bindings.c +++ b/src/glm_mat4_bindings.c @@ -197,6 +197,47 @@ int honey_glm_mat4_inv(lua_State* L) return 0; } +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_glm_mat4_basis(lua_State* L) +{ + honey_glm_array* self; + honey_lua_parse_arguments(L, 1, 1, HONEY_USERDATA, &self); + + + lua_createtable(L, 0, 3); + honey_glm_array *x, *y, *z; + + lua_pushcfunction(L, honey_glm_new_vec3); + honey_lua_pcall(L, 0, 1); + x = lua_touserdata(L, -1); + lua_setfield(L, -2, "x"); + + lua_pushcfunction(L, honey_glm_new_vec3); + honey_lua_pcall(L, 0, 1); + y = lua_touserdata(L, -1); + lua_setfield(L, -2, "y"); + + lua_pushcfunction(L, honey_glm_new_vec3); + honey_lua_pcall(L, 0, 1); + z = lua_touserdata(L, -1); + lua_setfield(L, -2, "z"); + + x->data[0] = self->data[0]; + x->data[1] = self->data[1]; + x->data[2] = self->data[2]; + + y->data[0] = self->data[4]; + y->data[1] = self->data[5]; + y->data[2] = self->data[6]; + + z->data[0] = self->data[8]; + z->data[1] = self->data[9]; + z->data[2] = self->data[10]; + + return 1; +} + /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Affine Transforms diff --git a/src/glm_vec3_bindings.c b/src/glm_vec3_bindings.c index a8157f2..e6ea407 100644 --- a/src/glm_vec3_bindings.c +++ b/src/glm_vec3_bindings.c @@ -247,6 +247,33 @@ int honey_glm_vec3_mul(lua_State* L) /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +int honey_glm_vec3_muls(lua_State* L) +{ + honey_glm_array *self, *dest; + float s; + int choice = honey_lua_parse_arguments + (L, 2, + 2, HONEY_USERDATA, &self, HONEY_NUMBER, &s, + 3, HONEY_USERDATA, &self, HONEY_NUMBER, &s, HONEY_USERDATA, &dest); + + if (choice == 0) + dest = self; + else { + if (dest->type != VEC3) + honey_lua_throw_error + (L, "destination vector must be of type VEC3 (%d); got %d instead", + VEC3, dest->type); + } + + dest->data[0] = s * self->data[0]; + dest->data[1] = s * self->data[1]; + dest->data[2] = s * self->data[2]; + + return 0; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + int honey_glm_vec3_scale(lua_State* L) { honey_glm_array *self, *dest; diff --git a/src/honey.c b/src/honey.c index f58be3e..5f83544 100644 --- a/src/honey.c +++ b/src/honey.c @@ -57,17 +57,17 @@ bool honey_parse_options(honey_options* options, int argc, char** argv) static int honey_lua_clear_color(lua_State* L) { - float* color_array; + honey_glm_array* color_array; bool color, depth, stencil; - honey_lua_parse_arguments(L, 4, + honey_lua_parse_arguments(L, 1, 4, HONEY_USERDATA, &color_array, HONEY_BOOLEAN, &color, HONEY_BOOLEAN, &depth, HONEY_BOOLEAN, &stencil); - float r = color_array[0]; - float g = color_array[1]; - float b = color_array[2]; - float a = color_array[3]; + float r = color_array->data[0]; + float g = color_array->data[1]; + float b = color_array->data[2]; + float a = color_array->data[3]; int clear_flags = 0; if (color) @@ -87,7 +87,7 @@ static int honey_lua_clear_color(lua_State* L) int honey_lua_enable_depth_test(lua_State* L) { bool enable; - honey_lua_parse_arguments(L, 1, HONEY_BOOLEAN, &enable); + honey_lua_parse_arguments(L, 1, 1, HONEY_BOOLEAN, &enable); if (enable) glEnable(GL_DEPTH_TEST); else @@ -98,7 +98,7 @@ int honey_lua_enable_depth_test(lua_State* L) int honey_lua_set_viewport_size(lua_State* L) { int width, height; - honey_lua_parse_arguments(L, 2, + honey_lua_parse_arguments(L, 1, 2, HONEY_INTEGER, &width, HONEY_INTEGER, &height); glViewport(0,0,width,height); @@ -249,7 +249,7 @@ int honey_get_callback(lua_State* L, char* callback) int honey_set_framebuffer(lua_State* L) { int framebuffer; - honey_lua_parse_arguments(L, 1, HONEY_INTEGER, &framebuffer); + honey_lua_parse_arguments(L, 1, 1, HONEY_INTEGER, &framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); return 0; } diff --git a/src/input.c b/src/input.c index 3b0b286..ca5fc05 100644 --- a/src/input.c +++ b/src/input.c @@ -194,8 +194,8 @@ int honey_key_bind(lua_State* L) int choice = honey_lua_parse_arguments (L, 2, - 2, HONEY_INTEGER, &key, HONEY_FUNCTION, - 3, HONEY_INTEGER, &key, HONEY_FUNCTION, HONEY_ANY); + 2, HONEY_INTEGER, &key, HONEY_FUNCTION, NULL, + 3, HONEY_INTEGER, &key, HONEY_FUNCTION, NULL, HONEY_ANY, NULL); lua_pushvalue(L, 2); int callback = luaL_ref(L, LUA_REGISTRYINDEX); @@ -282,16 +282,19 @@ int honey_mouse_set_mode(lua_State* L) int honey_mouse_movement_bind(lua_State* L) { - honey_lua_parse_arguments - (L, 1, - 2, HONEY_FUNCTION, HONEY_ANY); + int choice = honey_lua_parse_arguments + (L, 2, + 1, HONEY_FUNCTION, NULL, + 2, HONEY_FUNCTION, NULL, HONEY_ANY, NULL); honey_mouse_movement_unbind(L); /* avoid memory leaks! */ lua_pushvalue(L, 1); honey_mouse_movement_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); - lua_pushvalue(L, 2); - honey_mouse_movement_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX); + if (choice == 1) { + lua_pushvalue(L, 2); + honey_mouse_movement_callback_data_ref = luaL_ref(L, LUA_REGISTRYINDEX); + } return 0; } @@ -3,18 +3,18 @@ static int honey_mesh_lua_draw(lua_State* L) { honey_mesh* mesh; - int shader; - honey_lua_parse_arguments(L, 2, + int* shader; + honey_lua_parse_arguments(L, 1, 2, HONEY_USERDATA, &mesh, - HONEY_INTEGER, &shader); - honey_mesh_draw(*mesh, shader); + HONEY_USERDATA, &shader); + honey_mesh_draw(*mesh, *shader); return 0; } static int honey_mesh_lua_delete(lua_State* L) { honey_mesh* mesh; - honey_lua_parse_arguments(L, 1, HONEY_USERDATA, &mesh); + honey_lua_parse_arguments(L, 1, 1, HONEY_USERDATA, &mesh); honey_mesh_delete(*mesh); return 0; } @@ -123,7 +123,7 @@ static void process_nodes_recursively(lua_State* L, int honey_mesh_load(lua_State* L) { char* filename; - honey_lua_parse_arguments(L, 1, HONEY_STRING, &filename); + honey_lua_parse_arguments(L, 1, 1, HONEY_STRING, &filename); int n_meshes = 1; diff --git a/src/primitives.c b/src/primitives.c index c2f6c9e..ee68f6a 100644 --- a/src/primitives.c +++ b/src/primitives.c @@ -3,7 +3,7 @@ static int honey_mesh_lua_plane(lua_State* L) { float width, height; - honey_lua_parse_arguments(L, 2, + honey_lua_parse_arguments(L, 1, 2, HONEY_NUMBER, &width, HONEY_NUMBER, &height); @@ -18,7 +18,7 @@ static int honey_mesh_lua_plane(lua_State* L) static int honey_mesh_lua_cube(lua_State* L) { float width, height, depth; - honey_lua_parse_arguments(L, 3, + honey_lua_parse_arguments(L, 1, 3, HONEY_NUMBER, &width, HONEY_NUMBER, &height, HONEY_NUMBER, &depth); diff --git a/src/scene_tree_node.c b/src/scene_tree_node.c index a3ca27e..87618e6 100644 --- a/src/scene_tree_node.c +++ b/src/scene_tree_node.c @@ -16,6 +16,9 @@ void honey_setup_scene_tree(lua_State* L) HONEY_FUNCTION, "scale", honey_node_scale); honey_node_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX); + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_node_mt_ref); + lua_setfield(L, -2, "nodeMetatable"); + lua_pushcfunction(L, honey_node_new); lua_setfield(L, -2, "node"); } @@ -42,18 +45,19 @@ int honey_node_new(lua_State* L) lua_rawgeti(L, LUA_REGISTRYINDEX, honey_node_mt_ref); lua_setmetatable(L, -2); - lua_pushvalue(L, 1); - if (!lua_isnil(L, -1)) { + if (lua_istable(L, 1)) { /* add self to parent.children */ - lua_getfield(L, -1, "children"); + lua_getfield(L, 1, "children"); int length = lua_objlen(L, -1); lua_pushinteger(L, length+1); - lua_pushvalue(L, 5); + lua_pushvalue(L, -3); lua_settable(L, -3); lua_pop(L, 1); + + lua_pushvalue(L, 1); + lua_setfield(L, -2, "parent"); } - lua_setfield(L, -2, "parent"); if (position->type != VEC3) honey_lua_throw_error @@ -105,46 +109,35 @@ int honey_node_update_transform(lua_State* L) { honey_lua_parse_arguments(L, 1, 1, HONEY_TABLE, NULL); - /* self.transform:eye() */ - lua_pushcfunction(L, honey_glm_mat4_eye); - lua_getfield(L, 1, "transform"); - lua_pushvalue(L, -1); - honey_lua_pcall(L, 1, 0); + honey_glm_array *transform, *position, *rotation, *parent_transform; - /* self.transform:translate(self.position) */ - lua_pushcfunction(L, honey_glm_translate); - lua_pushvalue(L, 2); - lua_getfield(L, 1, "position"); - honey_lua_pcall(L, 2, 0); + lua_getfield(L, 1, "transform"); + transform = lua_touserdata(L, -1); + lua_pop(L, 1); - /* self.transform:rotateZ(self.rotation:at(2)) */ - lua_pushcfunction(L, honey_glm_rotate_z); - lua_pushvalue(L, 2); lua_getfield(L, 1, "position"); - honey_glm_array* position = lua_touserdata(L, -1); + position = lua_touserdata(L, -1); + lua_pop(L, 1); + + lua_getfield(L, 1, "rotation"); + rotation = lua_touserdata(L, -1); lua_pop(L, 1); - lua_pushnumber(L, position->data[2]); - honey_lua_pcall(L, 2, 0); - /* self.transform:rotateY(self.rotation:at(1)) */ - lua_pushcfunction(L, honey_glm_rotate_y); - lua_pushvalue(L, 2); - lua_pushnumber(L, position->data[1]); - honey_lua_pcall(L, 2, 0); + glm_mat4_identity(transform->data); - /* self.transform:rotateX(self.rotation:at(0)) */ - lua_pushcfunction(L, honey_glm_rotate_x); - lua_pushvalue(L, 2); - lua_pushnumber(L, position->data[0]); - honey_lua_pcall(L, 2, 0); + glm_translate(transform->data, position->data); + + glm_rotate_z(transform->data, rotation->data[2], transform->data); + glm_rotate_y(transform->data, rotation->data[1], transform->data); + glm_rotate_x(transform->data, rotation->data[0], transform->data); lua_getfield(L, 1, "parent"); - if (!lua_isnil(L, -1)) { - lua_pushcfunction(L, honey_glm_mat4_mul); - lua_getfield(L, -2, "transform"); - lua_pushvalue(L, 2); - lua_pushvalue(L, -1); - honey_lua_pcall(L, 3, 0); + if (lua_istable(L, -1)) { + lua_getfield(L, -1, "transform"); + parent_transform = lua_touserdata(L, -1); + lua_pop(L, 1); + + glm_mat4_mul(parent_transform->data, transform->data, transform->data); } lua_pop(L, 1); @@ -191,13 +184,23 @@ int honey_node_update_cascade(lua_State* L) int honey_node_draw_cascade(lua_State* L) { - honey_lua_parse_arguments(L, 1, 1, HONEY_TABLE, NULL); + int* shader; + int choice = honey_lua_parse_arguments + (L, 2, + 2, HONEY_TABLE, NULL, HONEY_TABLE, NULL, + 3, HONEY_TABLE, NULL, HONEY_TABLE, NULL, HONEY_USERDATA, &shader); /* call self.draw if it exists */ lua_getfield(L, 1, "draw"); if (!lua_isnil(L, -1)) { lua_pushvalue(L, 1); - honey_lua_pcall(L, 1, 0); + lua_pushvalue(L, 2); + if (choice == 1) { + lua_pushvalue(L, 3); + honey_lua_pcall(L, 3, 0); + } + else + honey_lua_pcall(L, 2, 0); } else lua_pop(L, 1); @@ -209,7 +212,13 @@ int honey_node_draw_cascade(lua_State* L) lua_rawgeti(L, -1, i+1); lua_pushcfunction(L, honey_node_draw_cascade); lua_pushvalue(L, -2); - honey_lua_pcall(L, 1, 0); + lua_pushvalue(L, 2); + if (choice == 1) { + lua_pushvalue(L, 3); + honey_lua_pcall(L, 3, 0); + } + else + honey_lua_pcall(L, 2, 0); lua_pop(L, 1); } @@ -220,15 +229,17 @@ int honey_node_draw_cascade(lua_State* L) int honey_node_translate(lua_State* L) { - honey_glm_array* v; + honey_glm_array* position, *v; honey_lua_parse_arguments(L, 1, 2, HONEY_TABLE, NULL, HONEY_USERDATA, &v); - lua_pushcfunction(L, honey_glm_vec3_add); - lua_getfield(L, 1, "transform"); - lua_pushvalue(L, 2); - lua_pushvalue(L, -2); - honey_lua_pcall(L, 3, 0); + lua_getfield(L, 1, "position"); + position = lua_touserdata(L, -1); + lua_pop(L, 1); + position->data[0] += v->data[0]; + position->data[1] += v->data[1]; + position->data[2] += v->data[2]; + return 0; } diff --git a/src/shader.c b/src/shader.c index a05bd0c..048cf87 100644 --- a/src/shader.c +++ b/src/shader.c @@ -10,17 +10,20 @@ void honey_setup_shader(lua_State* L) /* honey.shader.prototype */ HONEY_FUNCTION, "use", honey_shader_use, - HONEY_FUNCTION, "set_int", honey_shader_set_int, - HONEY_FUNCTION, "set_float", honey_shader_set_float, - HONEY_FUNCTION, "set_vec3", honey_shader_set_vec3, - HONEY_FUNCTION, "set_vec4", honey_shader_set_vec4, - HONEY_FUNCTION, "set_mat3", honey_shader_set_mat3, - HONEY_FUNCTION, "set_mat4", honey_shader_set_mat4, + HONEY_FUNCTION, "setInteger", honey_shader_set_int, + HONEY_FUNCTION, "setFloat", honey_shader_set_float, + HONEY_FUNCTION, "setVec3", honey_shader_set_vec3, + HONEY_FUNCTION, "setVec4", honey_shader_set_vec4, + HONEY_FUNCTION, "setMat3", honey_shader_set_mat3, + HONEY_FUNCTION, "setMat4", honey_shader_set_mat4, HONEY_FUNCTION, "__gc", honey_shader_delete); honey_shader_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX); + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_shader_mt_ref); + lua_setfield(L, -2, "shaderMetatable"); + lua_pushcfunction(L, honey_shader_new); lua_setfield(L, -2, "shader"); } diff --git a/src/texture.c b/src/texture.c index 7388d50..cdd36c4 100644 --- a/src/texture.c +++ b/src/texture.c @@ -11,7 +11,7 @@ static int honey_lua_texture_create(lua_State* L) honey_texture* texture; int width, height; char* type; - honey_lua_parse_arguments(L, 4, + honey_lua_parse_arguments(L, 1, 4, HONEY_USERDATA, &texture, HONEY_STRING, &type, HONEY_INTEGER, &width, @@ -41,7 +41,7 @@ static int honey_lua_texture_load(lua_State* L) { honey_texture* texture; char* texture_path; - honey_lua_parse_arguments(L, 3, + honey_lua_parse_arguments(L, 1, 3, HONEY_USERDATA, &texture, HONEY_STRING, &texture_path); enum honey_texture_result result = honey_texture_load(texture, texture_path); @@ -62,7 +62,7 @@ static int honey_lua_texture_use(lua_State* L) { honey_texture* texture; int texture_unit; - honey_lua_parse_arguments(L, 2, + honey_lua_parse_arguments(L, 1, 2, HONEY_USERDATA, &texture, HONEY_INTEGER, &texture_unit); honey_texture_use(*texture, texture_unit); @@ -72,6 +72,13 @@ static int honey_lua_texture_use(lua_State* L) static int honey_lua_framebuffer_new(lua_State* L) { honey_texture* draw, *depth; + int width, height; + honey_lua_parse_arguments(L, 1, 4, + HONEY_ANY, NULL, + HONEY_ANY, NULL, + HONEY_INTEGER, &width, + HONEY_INTEGER, &height); + if (lua_isuserdata(L, 1)) draw = lua_touserdata(L, 1); else @@ -82,11 +89,6 @@ static int honey_lua_framebuffer_new(lua_State* L) else depth = NULL; - int width, height; - honey_lua_parse_arguments(L, 4, HONEY_ANY, HONEY_ANY, - HONEY_INTEGER, &width, - HONEY_INTEGER, &height); - unsigned int framebuffer; honey_texture_framebuffer_object_new(&framebuffer, draw, depth, |