summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-09-19 10:37:38 -0500
committersanine <sanine.not@pm.me>2022-09-19 10:37:38 -0500
commit7f5d38aaf8cf7a067d1dce677b33f8b597f6a974 (patch)
tree323e918b6427a4d5b7201f3f069094c84e01741f /src
parent64b1fcb3e7e55e55bb8da8cd9f5c3d9d4d92fdb8 (diff)
add tangents & bitangents
Diffstat (limited to 'src')
-rw-r--r--src/import/import.c68
-rw-r--r--src/import/import.test.c79
2 files changed, 141 insertions, 6 deletions
diff --git a/src/import/import.c b/src/import/import.c
index a68ac05..73075d4 100644
--- a/src/import/import.c
+++ b/src/import/import.c
@@ -33,20 +33,47 @@ void push_aistring(lua_State *L, struct aiString str)
}
+/* mesh components:
+ * DONE:
+ * mBitangents
+ * mFaces
+ * mNormals
+ * mNumFaces
+ * mNumVertices
+ * mTangents
+ * mVertices
+ *
+ * TODO:
+ * mAnimMeshes
+ * mBones
+ * mColors
+ * mMaterialIndex
+ * mMethod
+ * mName
+ * mNumAnimMeshes
+ * mNumBones
+ * mNumUVComponents
+ * mPrimitiveTypes
+ * mTextureCoords
+ * mTextureCoordsNames
+ */
void push_mesh(lua_State *L, struct aiMesh mesh)
{
- /* create mesh table */
+ /* --=== create mesh table ===-- */
lua_createtable(L, 0, 1);
int meshtbl = lua_gettop(L);
int count = mesh.mNumVertices;
int pop_count = 0;
- /* create tables */
+ /* --=== create tables ===-- */
+
+ /* positions */
lua_createtable(L, count, 0);
int vertices = lua_gettop(L);
pop_count += 1;
+ /* normals */
int normals = 0;
if (mesh.mNormals != NULL) {
lua_createtable(L, count, 0);
@@ -54,7 +81,19 @@ void push_mesh(lua_State *L, struct aiMesh mesh)
pop_count += 1;
}
- /* populate vertices */
+ /* tangents & bitangents */
+ int tangents = 0;
+ int bitangents = 0;
+ if (mesh.mTangents != NULL) {
+ lua_createtable(L, count, 0);
+ tangents = lua_gettop(L);
+ lua_createtable(L, count, 0);
+ bitangents = lua_gettop(L);
+ pop_count += 2;
+ }
+
+ /* --=== populate vertices ===-- */
+
for (int i=0; i<count; i++) {
/* positions */
push_vector(L, mesh.mVertices[i]);
@@ -65,9 +104,18 @@ void push_mesh(lua_State *L, struct aiMesh mesh)
push_vector(L, mesh.mNormals[i]);
lua_rawseti(L, normals, i+1);
}
+
+ /* tangents */
+ if (tangents) {
+ push_vector(L, mesh.mTangents[i]);
+ lua_rawseti(L, tangents, i+1);
+ push_vector(L, mesh.mBitangents[i]);
+ lua_rawseti(L, bitangents, i+1);
+ }
}
- /* populate faces */
+ /* --=== populate faces ===-- */
+
int faces = 0;
if (mesh.mNumFaces != 0) {
lua_createtable(L, mesh.mNumFaces, 0);
@@ -79,7 +127,8 @@ void push_mesh(lua_State *L, struct aiMesh mesh)
}
}
- /* assign values */
+ /* --=== assign values ===-- */
+
lua_pushvalue(L, vertices);
lua_setfield(L, meshtbl, "vertices");
@@ -88,11 +137,18 @@ void push_mesh(lua_State *L, struct aiMesh mesh)
lua_setfield(L, meshtbl, "normals");
}
+ if (tangents) {
+ lua_pushvalue(L, tangents);
+ lua_setfield(L, meshtbl, "tangents");
+ lua_pushvalue(L, bitangents);
+ lua_setfield(L, meshtbl, "bitangents");
+ }
+
if (faces) {
lua_pushvalue(L, faces);
lua_setfield(L, meshtbl, "faces");
}
- /* clean up */
+ /* --=== clean up ===-- */
lua_pop(L, pop_count);
}
diff --git a/src/import/import.test.c b/src/import/import.test.c
index 4daaaf5..e17a685 100644
--- a/src/import/import.test.c
+++ b/src/import/import.test.c
@@ -53,6 +53,7 @@ void test_push_face()
int facetbl = lua_gettop(L);
lily_assert_int_equal(lua_objlen(L, facetbl), 6);
+ /* the numbers should be one higher because of lua's 1-indexing */
lua_rawgeti(L, facetbl, 1);
lily_assert_int_equal(lua_tointeger(L, -1), 1);
lua_rawgeti(L, facetbl, 2);
@@ -97,6 +98,9 @@ void test_faces(lua_State *L, int meshtbl);
void create_normals(struct aiMesh *mesh);
void test_normals(lua_State *L, int meshtbl);
+void create_tangents(struct aiMesh *mesh);
+void test_tangents(lua_State *L, int meshtbl);
+
void test_nil(lua_State *L, int meshtbl, const char *field);
@@ -109,6 +113,10 @@ void test_nil(lua_State *L, int meshtbl, const char *field);
mesh.mVertices = vertices; \
struct aiVector3D normals[NUM_MESH_VERTICES]; \
mesh.mNormals = normals; \
+ struct aiVector3D tangents[NUM_MESH_VERTICES]; \
+ struct aiVector3D bitangents[NUM_MESH_VERTICES]; \
+ mesh.mTangents = tangents; \
+ mesh.mBitangents = bitangents; \
struct aiFace faces[NUM_MESH_FACES]; \
unsigned int index_array[3*NUM_MESH_FACES]; \
for (int i=0; i<NUM_MESH_FACES; i++) \
@@ -127,6 +135,8 @@ void test_push_mesh()
/* allocate memory */
ALLOCATE_MEMORY();
mesh.mNormals = NULL;
+ mesh.mTangents = NULL;
+ mesh.mBitangents = NULL;
/* setup mesh */
create_vertices(&mesh);
@@ -156,6 +166,8 @@ void test_push_mesh_faces()
/* allocate memory */
ALLOCATE_MEMORY();
mesh.mNormals = NULL;
+ mesh.mTangents = NULL;
+ mesh.mBitangents = NULL;
/* setup mesh */
create_vertices(&mesh);
@@ -185,6 +197,8 @@ void test_push_mesh_normals()
/* allocate memory */
ALLOCATE_MEMORY();
+ mesh.mTangents = NULL;
+ mesh.mBitangents = NULL;
/* setup mesh */
create_vertices(&mesh);
@@ -207,6 +221,40 @@ void test_push_mesh_normals()
}
+void test_push_mesh_tangents()
+{
+ 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);
+ create_tangents(&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);
+ test_tangents(L, meshtbl);
+
+ lua_close(L);
+
+}
+
+
void create_vertices(struct aiMesh *mesh)
{
mesh->mVertices[0] = (struct aiVector3D) { 0, 0, 0 };
@@ -343,6 +391,36 @@ void test_normals(lua_State *L, int meshtbl)
}
+void create_tangents(struct aiMesh *mesh)
+{
+ /* these tangents are not normalized -- see note in create_normals */
+ mesh->mTangents[0] = (struct aiVector3D) { 0.1, 0, 0 };
+ mesh->mTangents[1] = (struct aiVector3D) { 0.2, 0, 0 };
+ mesh->mTangents[2] = (struct aiVector3D) { 0.4, 0, 0 };
+ mesh->mTangents[3] = (struct aiVector3D) { 1.0, 0, 0 };
+
+ mesh->mBitangents[0] = (struct aiVector3D) { 0, 0, 0.1 };
+ mesh->mBitangents[1] = (struct aiVector3D) { 0, 0, 0.2 };
+ mesh->mBitangents[2] = (struct aiVector3D) { 0, 0, 0.4 };
+ mesh->mBitangents[3] = (struct aiVector3D) { 0, 0, 1.0 };
+}
+
+
+void test_tangents(lua_State *L, int meshtbl)
+{
+ check_vector(L, meshtbl, "tangents", 1, 0.1, 0, 0);
+ check_vector(L, meshtbl, "tangents", 2, 0.2, 0, 0);
+ check_vector(L, meshtbl, "tangents", 3, 0.4, 0, 0);
+ check_vector(L, meshtbl, "tangents", 4, 1.0, 0, 0);
+
+ check_vector(L, meshtbl, "bitangents", 1, 0, 0, 0.1);
+ check_vector(L, meshtbl, "bitangents", 2, 0, 0, 0.2);
+ check_vector(L, meshtbl, "bitangents", 3, 0, 0, 0.4);
+ check_vector(L, meshtbl, "bitangents", 4, 0, 0, 1.0);
+}
+
+
+
void test_nil(lua_State *L, int meshtbl, const char *field)
{
lua_getfield(L, meshtbl, field);
@@ -363,4 +441,5 @@ void suite_import()
lily_run_test(test_push_mesh);
lily_run_test(test_push_mesh_faces);
lily_run_test(test_push_mesh_normals);
+ lily_run_test(test_push_mesh_tangents);
}