summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-09-18 16:09:24 -0500
committersanine <sanine.not@pm.me>2022-09-18 16:09:24 -0500
commit64b1fcb3e7e55e55bb8da8cd9f5c3d9d4d92fdb8 (patch)
treec58aab3c8621392914520f2cff65e1acdd3068df /src
parent33a88ffe0cdab355547573b0fc566adbf91b8d23 (diff)
add normal pushing
Diffstat (limited to 'src')
-rw-r--r--src/import/import.c18
-rw-r--r--src/import/import.test.c84
2 files changed, 93 insertions, 9 deletions
diff --git a/src/import/import.c b/src/import/import.c
index 2debc3a..a68ac05 100644
--- a/src/import/import.c
+++ b/src/import/import.c
@@ -47,11 +47,24 @@ void push_mesh(lua_State *L, struct aiMesh mesh)
int vertices = lua_gettop(L);
pop_count += 1;
+ int normals = 0;
+ if (mesh.mNormals != NULL) {
+ lua_createtable(L, count, 0);
+ normals = lua_gettop(L);
+ pop_count += 1;
+ }
+
/* populate vertices */
for (int i=0; i<count; i++) {
/* positions */
push_vector(L, mesh.mVertices[i]);
lua_rawseti(L, vertices, i+1);
+
+ /* normals */
+ if (normals) {
+ push_vector(L, mesh.mNormals[i]);
+ lua_rawseti(L, normals, i+1);
+ }
}
/* populate faces */
@@ -70,6 +83,11 @@ void push_mesh(lua_State *L, struct aiMesh mesh)
lua_pushvalue(L, vertices);
lua_setfield(L, meshtbl, "vertices");
+ if (normals) {
+ lua_pushvalue(L, normals);
+ lua_setfield(L, meshtbl, "normals");
+ }
+
if (faces) {
lua_pushvalue(L, faces);
lua_setfield(L, meshtbl, "faces");
diff --git a/src/import/import.test.c b/src/import/import.test.c
index f850eef..4daaaf5 100644
--- a/src/import/import.test.c
+++ b/src/import/import.test.c
@@ -94,6 +94,9 @@ void test_vertices(lua_State *L, int meshtbl);
void create_faces(struct aiMesh *mesh);
void test_faces(lua_State *L, int meshtbl);
+void create_normals(struct aiMesh *mesh);
+void test_normals(lua_State *L, int meshtbl);
+
void test_nil(lua_State *L, int meshtbl, const char *field);
@@ -101,6 +104,19 @@ void test_nil(lua_State *L, int meshtbl, const char *field);
#define NUM_MESH_FACES 2
+#define ALLOCATE_MEMORY() \
+ struct aiVector3D vertices[NUM_MESH_VERTICES]; \
+ mesh.mVertices = vertices; \
+ struct aiVector3D normals[NUM_MESH_VERTICES]; \
+ mesh.mNormals = normals; \
+ struct aiFace faces[NUM_MESH_FACES]; \
+ unsigned int index_array[3*NUM_MESH_FACES]; \
+ for (int i=0; i<NUM_MESH_FACES; i++) \
+ faces[i].mIndices = index_array + (3*i); \
+ mesh.mFaces = faces;
+
+
+
void test_push_mesh()
{
lua_State *L = luaL_newstate();
@@ -109,8 +125,8 @@ void test_push_mesh()
mesh.mNumFaces = 0;
/* allocate memory */
- struct aiVector3D vertices[NUM_MESH_VERTICES];
- mesh.mVertices = vertices;
+ ALLOCATE_MEMORY();
+ mesh.mNormals = NULL;
/* setup mesh */
create_vertices(&mesh);
@@ -138,13 +154,8 @@ void test_push_mesh_faces()
mesh.mNumFaces = NUM_MESH_FACES;
/* allocate memory */
- struct aiVector3D vertices[NUM_MESH_VERTICES];
- mesh.mVertices = vertices;
- struct aiFace faces[NUM_MESH_FACES];
- unsigned int index_array[3*NUM_MESH_FACES];
- for (int i=0; i<NUM_MESH_FACES; i++)
- faces[i].mIndices = index_array + (3*i);
- mesh.mFaces = faces;
+ ALLOCATE_MEMORY();
+ mesh.mNormals = NULL;
/* setup mesh */
create_vertices(&mesh);
@@ -165,6 +176,37 @@ void test_push_mesh_faces()
}
+void test_push_mesh_normals()
+{
+ lua_State *L = luaL_newstate();
+ struct aiMesh mesh;
+ mesh.mNumVertices = NUM_MESH_VERTICES;
+ mesh.mNumFaces = NUM_MESH_FACES;
+
+ /* allocate memory */
+ ALLOCATE_MEMORY();
+
+ /* setup mesh */
+ create_vertices(&mesh);
+ create_faces(&mesh);
+ create_normals(&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);
+ test_faces(L, meshtbl);
+ test_normals(L, meshtbl);
+
+ lua_close(L);
+}
+
+
void create_vertices(struct aiMesh *mesh)
{
mesh->mVertices[0] = (struct aiVector3D) { 0, 0, 0 };
@@ -278,6 +320,29 @@ void test_faces(lua_State *L, int meshtbl)
}
+void create_normals(struct aiMesh *mesh)
+{
+ /* these normals are deliberately not, uh, normalized
+ * in order to distinguish them for the purposes of testing.
+ * (this could also happen in real life -- assimp won't normalize
+ * normals taken straight from the object file)
+ */
+ mesh->mNormals[0] = (struct aiVector3D) { 0, 0.1, 0 };
+ mesh->mNormals[1] = (struct aiVector3D) { 0, 0.2, 0 };
+ mesh->mNormals[2] = (struct aiVector3D) { 0, 0.4, 0 };
+ mesh->mNormals[3] = (struct aiVector3D) { 0, 1.0, 0 };
+}
+
+
+void test_normals(lua_State *L, int meshtbl)
+{
+ check_vector(L, meshtbl, "normals", 1, 0, 0.1, 0);
+ check_vector(L, meshtbl, "normals", 2, 0, 0.2, 0);
+ check_vector(L, meshtbl, "normals", 3, 0, 0.4, 0);
+ check_vector(L, meshtbl, "normals", 4, 0, 1.0, 0);
+}
+
+
void test_nil(lua_State *L, int meshtbl, const char *field)
{
lua_getfield(L, meshtbl, field);
@@ -297,4 +362,5 @@ void suite_import()
lily_run_test(test_push_mesh);
lily_run_test(test_push_mesh_faces);
+ lily_run_test(test_push_mesh_normals);
}