From ba9a7162b72e6b20466de7dd115719777315b5c2 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Sat, 5 Dec 2020 18:37:44 -0600 Subject: switch meshes to use metamathods --- demo/SpatialShader.lua | 6 +++--- demo/main.lua | 4 ++-- src/mesh.c | 17 +++++++++++++---- src/mesh.h | 2 ++ src/primitives.c | 4 ++++ 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/demo/SpatialShader.lua b/demo/SpatialShader.lua index 699cff2..0c65176 100644 --- a/demo/SpatialShader.lua +++ b/demo/SpatialShader.lua @@ -74,10 +74,10 @@ SpatialShader.prototype.setCamera = function(self, camera) self.shader:setMat4('projection', camera.projection) end -SpatialShader.prototype.drawMesh = function(self, mesh) - self.shader:setMat4('model', mesh.transform) +SpatialShader.prototype.drawMesh = function(self, meshInstance) + self.shader:setMat4('model', meshInstance.transform) self.albedo:use(0) - honey.mesh.draw(mesh.mesh, self.shader) + meshInstance.mesh:draw(self.shader) end -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/demo/main.lua b/demo/main.lua index 5b19166..8e8d3d3 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -20,7 +20,7 @@ local lightDirection = honey.glm.vec3{1,1,1} lightDirection:normalize() shader:setVec3('directional_lights[0].direction', lightDirection) shader:setVec3('directional_lights[0].color', honey.glm.vec3{0,1,0}) -local meshes = honey.mesh.load('Suzanne.obj') +local meshes = honey.mesh('Suzanne.obj') local suzanne = MeshInstance.new(sceneRoot, honey.glm.vec3{0,0,3}, honey.glm.vec3{0,math.pi,0}, @@ -41,7 +41,7 @@ local plane2 = MeshInstance.new(suzanne, shader) suzanne.update = function(self, dt) - self:rotate('y', 10*dt) + self:rotate('y', dt) end local total_frames = 0 diff --git a/src/mesh.c b/src/mesh.c index e3b3a43..85364a8 100644 --- a/src/mesh.c +++ b/src/mesh.c @@ -1,5 +1,7 @@ #include "mesh.h" +int honey_mesh_mt_ref = LUA_NOREF; + static int honey_mesh_lua_draw(lua_State* L) { honey_mesh* mesh; @@ -22,10 +24,14 @@ static int honey_mesh_lua_delete(lua_State* L) void honey_setup_mesh(lua_State* L) { honey_lua_create_table - (L, 3, - HONEY_FUNCTION, "load", honey_mesh_load, - HONEY_FUNCTION, "draw", honey_mesh_lua_draw, - HONEY_FUNCTION, "delete", honey_mesh_lua_delete); + (L, 2, + HONEY_TABLE, "__index", 1, + HONEY_FUNCTION, "draw", honey_mesh_lua_draw, + + HONEY_FUNCTION, "__gc", honey_mesh_lua_delete); + honey_mesh_mt_ref = luaL_ref(L, LUA_REGISTRYINDEX); + + lua_pushcfunction(L, honey_mesh_load); lua_setfield(L, -2, "mesh"); } @@ -107,6 +113,9 @@ static void process_nodes_recursively(lua_State* L, { for (int i=0; imNumMeshes; i++) { honey_mesh* mesh = lua_newuserdata(L, sizeof(honey_mesh)); + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_mesh_mt_ref); + lua_setmetatable(L, -2); + struct aiMesh* assimp_mesh = scene->mMeshes[node->mMeshes[i]]; *mesh = assimp_to_honey_mesh(assimp_mesh, scene); lua_rawseti(L, -2, *n_meshes); diff --git a/src/mesh.h b/src/mesh.h index 70b7f4d..c97e3cb 100644 --- a/src/mesh.h +++ b/src/mesh.h @@ -8,6 +8,8 @@ #include "common.h" +extern int honey_mesh_mt_ref; + typedef struct { unsigned int n_vertices, n_indices; unsigned int vertex_array, vertex_buffer, element_buffer; diff --git a/src/primitives.c b/src/primitives.c index ee68f6a..d858cc6 100644 --- a/src/primitives.c +++ b/src/primitives.c @@ -8,6 +8,8 @@ static int honey_mesh_lua_plane(lua_State* L) HONEY_NUMBER, &height); honey_mesh* mesh = lua_newuserdata(L, sizeof(honey_mesh)); + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_mesh_mt_ref); + lua_setmetatable(L, -2); if (honey_mesh_new_textured_plane(mesh, width, height) != HONEY_OK) { lua_pushstring(L, "error encountered while building plane"); lua_error(L); @@ -24,6 +26,8 @@ static int honey_mesh_lua_cube(lua_State* L) HONEY_NUMBER, &depth); honey_mesh* mesh = lua_newuserdata(L, sizeof(honey_mesh)); + lua_rawgeti(L, LUA_REGISTRYINDEX, honey_mesh_mt_ref); + lua_setmetatable(L, -2); if (honey_mesh_new_textured_cube(mesh, width, height, depth) != HONEY_OK) { lua_pushstring(L, "error encountered while building plane"); lua_error(L); -- cgit v1.2.1