summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-09-18 14:08:20 -0500
committersanine <sanine.not@pm.me>2022-09-18 14:08:20 -0500
commit123b9e22cd4272ef7dcf6e519b03c3ca5ba73953 (patch)
tree03d8258facad79547596616b6b717406b0bb4bc4 /src
parent99672f4c2bdd1cddb4d749f8e82640db69f6ed08 (diff)
add basic mesh vertex loading
Diffstat (limited to 'src')
-rw-r--r--src/import/import.c39
-rw-r--r--src/import/import.test.c136
-rw-r--r--src/test/lily-test.h1
3 files changed, 176 insertions, 0 deletions
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; i<face.mNumIndices; i++) {
+ lua_pushinteger(L, face.mIndices[i]);
+ lua_rawseti(L, tbl, i+1);
+ }
+}
+
+
void push_aistring(lua_State *L, struct aiString str)
{
lua_pushstring(L, str.data);
}
+
+
+void push_mesh(lua_State *L, struct aiMesh mesh)
+{
+ int count = mesh.mNumVertices;
+
+ /* create tables */
+ lua_createtable(L, count, 0);
+ int vertices = lua_gettop(L);
+
+ /* populate */
+ for (int i=0; i<count; i++) {
+ /* positions */
+ push_vector(L, mesh.mVertices[i]);
+ lua_rawseti(L, vertices, i+1);
+ }
+
+ /* create mesh table */
+ lua_createtable(L, 0, 1);
+ int meshtbl = lua_gettop(L);
+
+ lua_pushvalue(L, vertices);
+ lua_setfield(L, meshtbl, "vertices");
+
+ /* clean up */
+ lua_remove(L, vertices);
+}
diff --git a/src/import/import.test.c b/src/import/import.test.c
index a234bda..69b781b 100644
--- a/src/import/import.test.c
+++ b/src/import/import.test.c
@@ -39,6 +39,37 @@ void test_push_vector()
}
+void test_push_face()
+{
+ lua_State *L = luaL_newstate();
+ struct aiFace face;
+ int indices[] = { 0, 1, 2, 3, 4, 5 };
+ face.mIndices = indices;
+ face.mNumIndices = 6;
+
+ push_face(L, face);
+
+ lily_assert_int_equal(lua_type(L, -1), LUA_TTABLE);
+ int facetbl = lua_gettop(L);
+ lily_assert_int_equal(lua_objlen(L, facetbl), 6);
+
+ lua_rawgeti(L, facetbl, 1);
+ lily_assert_int_equal(lua_tointeger(L, -1), 0);
+ lua_rawgeti(L, facetbl, 2);
+ lily_assert_int_equal(lua_tointeger(L, -1), 1);
+ lua_rawgeti(L, facetbl, 3);
+ lily_assert_int_equal(lua_tointeger(L, -1), 2);
+ lua_rawgeti(L, facetbl, 4);
+ lily_assert_int_equal(lua_tointeger(L, -1), 3);
+ lua_rawgeti(L, facetbl, 5);
+ lily_assert_int_equal(lua_tointeger(L, -1), 4);
+ lua_rawgeti(L, facetbl, 6);
+ lily_assert_int_equal(lua_tointeger(L, -1), 5);
+
+ lua_close(L);
+}
+
+
void test_push_aistring()
{
lua_State *L = luaL_newstate();
@@ -54,8 +85,113 @@ void test_push_aistring()
}
+/* --===== mesh tests =====-- */
+
+
+void create_vertices(struct aiMesh *mesh);
+void test_vertices(lua_State *L, int meshtbl);
+
+
+#define NUM_MESH_VERTICES 8
+
+
+void test_push_mesh()
+{
+ lua_State *L = luaL_newstate();
+ struct aiMesh mesh;
+ mesh.mNumVertices = NUM_MESH_VERTICES;
+
+ /* allocate memory */
+ struct aiVector3D vertices[NUM_MESH_VERTICES];
+ mesh.mVertices = vertices;
+
+ /* setup mesh */
+ create_vertices(&mesh);
+
+ /* push */
+ int top_before = lua_gettop(L);
+ push_mesh(L, mesh);
+ int meshtbl = lua_gettop(L);
+
+ /* check output */
+ lily_assert_int_equal(lua_type(L, meshtbl), LUA_TTABLE);
+ lily_assert_int_equal(meshtbl - top_before, 1); /* make sure we cleaned up correctly */
+ test_vertices(L, meshtbl);
+
+ lua_close(L);
+}
+
+
+void create_vertices(struct aiMesh *mesh)
+{
+ mesh->mVertices[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);
}
diff --git a/src/test/lily-test.h b/src/test/lily-test.h
index b5f380c..b001198 100644
--- a/src/test/lily-test.h
+++ b/src/test/lily-test.h
@@ -92,6 +92,7 @@ void _lily_run_suite(const char *name, lily_test_t suite);
*/
/** basic assertion function, mostly used by the other assertions */
+#define lily_assert(statement, ...) lily_assert_msg(statement, LILY_LOCATION, __VA_ARGS__)
void lily_assert_msg(bool statement, const char *location,
const char *format_string, ...);