From 123b9e22cd4272ef7dcf6e519b03c3ca5ba73953 Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 18 Sep 2022 14:08:20 -0500 Subject: add basic mesh vertex loading --- src/import/import.c | 39 ++++++++++++++ src/import/import.test.c | 136 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) (limited to 'src/import') diff --git a/src/import/import.c b/src/import/import.c index cbd8d45..a4fc552 100644 --- a/src/import/import.c +++ b/src/import/import.c @@ -14,7 +14,46 @@ void push_vector(lua_State *L, struct aiVector3D vec) } +void push_face(lua_State *L, struct aiFace face) +{ + lua_createtable(L, face.mNumIndices, 0); + int tbl = lua_gettop(L); + + for (int i=0; imVertices[0] = (struct aiVector3D) { 0, 0, 0 }; + mesh->mVertices[1] = (struct aiVector3D) { 0, 0, 1 }; + mesh->mVertices[2] = (struct aiVector3D) { 0, 1, 0 }; + mesh->mVertices[3] = (struct aiVector3D) { 0, 1, 1 }; + mesh->mVertices[4] = (struct aiVector3D) { 1, 0, 0 }; + mesh->mVertices[5] = (struct aiVector3D) { 1, 0, 1 }; + mesh->mVertices[6] = (struct aiVector3D) { 1, 1, 0 }; + mesh->mVertices[7] = (struct aiVector3D) { 1, 1, 1 }; +} + + +static int check_vector(lua_State *L, int meshtbl, const char *field, int index, + float x, float y, float z) +{ + lua_getfield(L, meshtbl, field); + lily_assert(lua_type(L, -1) == LUA_TTABLE, "field '%s' is not a table!", field); + lua_rawgeti(L, -1, index); + lily_assert(lua_type(L, -1) == LUA_TTABLE, "%s[%d] is not a table!", field, index); + + lua_getfield(L, -1, "x"); + lily_assert(lua_type(L, -1) == LUA_TNUMBER, "%s[%d].x is not a number!", field, index); + float vx = lua_tonumber(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "y"); + lily_assert(lua_type(L, -1) == LUA_TNUMBER, "%s[%d].y is not a number!", field, index); + float vy = lua_tonumber(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "z"); + lily_assert(lua_type(L, -1) == LUA_TNUMBER, "%s[%d].z is not a number!", field, index); + float vz = lua_tonumber(L, -1); + lua_pop(L, 1); + + lua_pop(L, 2); + lily_assert( + (fabs(vx - x) < 0.1) && + (fabs(vy - y) < 0.1) && + (fabs(vz - z) < 0.1), + "%s[%d] is [%f, %f, %f], but expected [%f, %f, %f]!", + field, index, + vx, vy, vz, + x, y, z + ); +} + + +void test_vertices(lua_State *L, int meshtbl) +{ + check_vector(L, meshtbl, "vertices", 1, 0, 0, 0); + check_vector(L, meshtbl, "vertices", 2, 0, 0, 1); + check_vector(L, meshtbl, "vertices", 3, 0, 1, 0); + check_vector(L, meshtbl, "vertices", 4, 0, 1, 1); + check_vector(L, meshtbl, "vertices", 5, 1, 0, 0); + check_vector(L, meshtbl, "vertices", 6, 1, 0, 1); + check_vector(L, meshtbl, "vertices", 7, 1, 1, 0); + check_vector(L, meshtbl, "vertices", 8, 1, 1, 1); +} + + +/* --===== end mesh tests =====-- */ + + void suite_import() { lily_run_test(test_push_vector); + lily_run_test(test_push_face); lily_run_test(test_push_aistring); + + lily_run_test(test_push_mesh); } -- cgit v1.2.1