From db0bc36c52a223f96bf0b517ef1053ba6b6b16d9 Mon Sep 17 00:00:00 2001 From: sanine Date: Thu, 22 Sep 2022 01:02:12 -0500 Subject: add uv loading --- src/import/import.c | 48 ++++++++++++++++- src/import/import.test.c | 138 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+), 2 deletions(-) (limited to 'src/import') diff --git a/src/import/import.c b/src/import/import.c index 73075d4..d58de0f 100644 --- a/src/import/import.c +++ b/src/import/import.c @@ -42,6 +42,8 @@ void push_aistring(lua_State *L, struct aiString str) * mNumVertices * mTangents * mVertices + * mNumUVComponents + * mTextureCoords * * TODO: * mAnimMeshes @@ -52,9 +54,7 @@ void push_aistring(lua_State *L, struct aiString str) * mName * mNumAnimMeshes * mNumBones - * mNumUVComponents * mPrimitiveTypes - * mTextureCoords * mTextureCoordsNames */ void push_mesh(lua_State *L, struct aiMesh mesh) @@ -92,6 +92,33 @@ void push_mesh(lua_State *L, struct aiMesh mesh) pop_count += 2; } + /* uvs */ + int uvs = 0; + int uv_components = 0; + int uv_channels[AI_MAX_NUMBER_OF_TEXTURECOORDS]; + for (int i=0; imVertices[0] = (struct aiVector3D) { 0, 0, 0 }; @@ -420,6 +470,93 @@ void test_tangents(lua_State *L, int meshtbl) } +void create_uvs(struct aiMesh *mesh) +{ + mesh->mNumUVComponents[0] = 2; + mesh->mTextureCoords[0][0] = (struct aiVector3D) { 0, 0, 0 }; + mesh->mTextureCoords[0][1] = (struct aiVector3D) { 0, 1, 0 }; + mesh->mTextureCoords[0][2] = (struct aiVector3D) { 1, 0, 0 }; + mesh->mTextureCoords[0][3] = (struct aiVector3D) { 1, 1, 0 }; + + mesh->mNumUVComponents[1] = 1; + mesh->mTextureCoords[1][0] = (struct aiVector3D) { 0.0, 0, 0 }; + mesh->mTextureCoords[1][1] = (struct aiVector3D) { 0.2, 0, 0 }; + mesh->mTextureCoords[1][2] = (struct aiVector3D) { 0.4, 0, 0 }; + mesh->mTextureCoords[1][3] = (struct aiVector3D) { 0.8, 0, 0 }; + + for (int i=2; imTextureCoords[i] = NULL; +} + + +static int check_uv(lua_State *L, int meshtbl, int channel, int index, + float x, float y, float z) +{ + lua_getfield(L, meshtbl, "uvs"); + lily_assert(lua_type(L, -1) == LUA_TTABLE, "field 'uvs' is not a table!"); + lua_rawgeti(L, -1, channel); + lily_assert(lua_type(L, -1) == LUA_TTABLE, "uvs[%d] is not a table!", channel); + lua_rawgeti(L, -1, index); + lily_assert(lua_type(L, -1) == LUA_TTABLE, "uvs[%d][%d] is not a table!", channel, index); + + lua_getfield(L, -1, "x"); + lily_assert(lua_type(L, -1) == LUA_TNUMBER, "uvs[%d][%d].x is not a number!", channel, index); + float vx = lua_tonumber(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "y"); + lily_assert(lua_type(L, -1) == LUA_TNUMBER, "uvs[%d][%d].y is not a number!", channel, index); + float vy = lua_tonumber(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "z"); + lily_assert(lua_type(L, -1) == LUA_TNUMBER, "uvs[%d][%d].z is not a number!", channel, index); + float vz = lua_tonumber(L, -1); + lua_pop(L, 1); + + lua_pop(L, 3); + lily_assert( + (fabs(vx - x) < 0.1) && + (fabs(vy - y) < 0.1) && + (fabs(vz - z) < 0.1), + "uvs[%d][%d] is [%f, %f, %f], but expected [%f, %f, %f]!", + channel, index, + vx, vy, vz, + x, y, z + ); +} + + +void test_uvs(lua_State *L, int meshtbl) +{ + lua_getfield(L, meshtbl, "numUvComponents"); + lily_assert(lua_type(L, -1) == LUA_TTABLE, "field 'numUvComponents' is not a table!"); + lua_rawgeti(L, -1, 1); + lily_assert_int_equal(lua_tointeger(L, -1), 2); + lua_pop(L, 1); + lua_rawgeti(L, -1, 2); + lily_assert_int_equal(lua_tointeger(L, -1), 1); + lua_pop(L, 1); + + check_uv(L, meshtbl, 1, 1, 0, 0, 0); + check_uv(L, meshtbl, 1, 2, 0, 1, 0); + check_uv(L, meshtbl, 1, 3, 1, 0, 0); + check_uv(L, meshtbl, 1, 4, 1, 1, 0); + + check_uv(L, meshtbl, 2, 1, 0.0, 0, 0); + check_uv(L, meshtbl, 2, 2, 0.2, 0, 0); + check_uv(L, meshtbl, 2, 3, 0.4, 0, 0); + check_uv(L, meshtbl, 2, 4, 0.8, 0, 0); + + lua_getfield(L, meshtbl, "uvs"); + int uvtbl = lua_gettop(L); + for (int i=2; i