From 7f5d38aaf8cf7a067d1dce677b33f8b597f6a974 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 19 Sep 2022 10:37:38 -0500 Subject: add tangents & bitangents --- src/import/import.c | 68 +++++++++++++++++++++++++++++++++++++---- src/import/import.test.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 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; imVertices[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); } -- cgit v1.2.1