From 33a88ffe0cdab355547573b0fc566adbf91b8d23 Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 18 Sep 2022 15:44:23 -0500 Subject: add face pushing --- src/import/import.c | 33 +++++++++-- src/import/import.test.c | 141 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 149 insertions(+), 25 deletions(-) diff --git a/src/import/import.c b/src/import/import.c index a4fc552..2debc3a 100644 --- a/src/import/import.c +++ b/src/import/import.c @@ -20,7 +20,8 @@ void push_face(lua_State *L, struct aiFace face) 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 }; + mesh->mVertices[2] = (struct aiVector3D) { 1, 0, 0 }; + mesh->mVertices[3] = (struct aiVector3D) { 1, 0, 1 }; } @@ -175,12 +214,75 @@ 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); + check_vector(L, meshtbl, "vertices", 3, 1, 0, 0); + check_vector(L, meshtbl, "vertices", 4, 1, 0, 1); +} + + +static void setup_face(struct aiFace *face, int v0, int v1, int v2) +{ + face->mNumIndices = 3; + face->mIndices[0] = v0; + face->mIndices[1] = v1; + face->mIndices[2] = v2; +} + + +void create_faces(struct aiMesh *mesh) +{ + setup_face(mesh->mFaces + 0, 0, 1, 3); + setup_face(mesh->mFaces + 1, 0, 3, 2); +} + + +static int check_face(lua_State *L, int meshtbl, const char *field, int index, + int v0, int v1, int v2) +{ + 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_rawgeti(L, -1, 1); + lily_assert(lua_type(L, -1) == LUA_TNUMBER, "%s[%d][1] is not a number!", field, index); + int vv0 = lua_tointeger(L, -1); + lua_pop(L, 1); + + lua_rawgeti(L, -1, 2); + lily_assert(lua_type(L, -1) == LUA_TNUMBER, "%s[%d][2] is not a number!", field, index); + int vv1 = lua_tointeger(L, -1); + lua_pop(L, 1); + + lua_rawgeti(L, -1, 3); + lily_assert(lua_type(L, -1) == LUA_TNUMBER, "%s[%d][3] is not a number!", field, index); + int vv2 = lua_tointeger(L, -1); + lua_pop(L, 1); + + lua_pop(L, 2); + lily_assert( + (v0 == vv0) && + (v1 == vv1) && + (v2 == vv2), + "%s[%d] is [%d, %d, %d], but expected [%d, %d, %d]!", + field, index, + vv0, vv1, vv2, + v0, v1, v2 + ); +} + + +void test_faces(lua_State *L, int meshtbl) +{ + check_face(L, meshtbl, "faces", 1, 1, 2, 4); + check_face(L, meshtbl, "faces", 2, 1, 4, 3); +} + + +void test_nil(lua_State *L, int meshtbl, const char *field) +{ + lua_getfield(L, meshtbl, field); + lily_assert(lua_type(L, -1) == LUA_TNIL, "field '%s' is not nil!"); + lua_pop(L, 1); } @@ -194,4 +296,5 @@ void suite_import() lily_run_test(test_push_aistring); lily_run_test(test_push_mesh); + lily_run_test(test_push_mesh_faces); } -- cgit v1.2.1