diff options
author | sanine <sanine.not@pm.me> | 2022-09-27 20:36:05 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-09-27 20:36:05 -0500 |
commit | 9b42315ef051745b94eeb31d4680f821779b50a2 (patch) | |
tree | cea3187182a2120916c8fc7c3c938e7da1f9868d | |
parent | d822cf5b786776788cace9c3f9b2be9df41812e9 (diff) |
fix off-by-1 bug in face indexing
-rw-r--r-- | src/import/import.c | 11 | ||||
-rw-r--r-- | src/import/import_mesh.test.c | 17 | ||||
-rw-r--r-- | src/main.c | 25 |
3 files changed, 42 insertions, 11 deletions
diff --git a/src/import/import.c b/src/import/import.c index a093068..becb1fe 100644 --- a/src/import/import.c +++ b/src/import/import.c @@ -15,7 +15,10 @@ int import_file(lua_State *L) char *filename; hs_parse_args(L, hs_str(filename)); - const struct aiScene *scene = aiImportFile(filename, aiProcess_Triangulate | aiProcess_FlipUVs); + const struct aiScene *scene = aiImportFile( + filename, + aiProcess_Triangulate | aiProcess_FlipUVs + ); if (scene == NULL) hs_throw_error(L, "failed to load file '%s'", filename); @@ -53,7 +56,11 @@ static void push_face(lua_State *L, struct aiFace face) for (int i=0; i<face.mNumIndices; i++) { /* add one to the index bc lua is 1-indexed */ - lua_pushinteger(L, face.mIndices[i]+1); + /* the above comment is WRONG WRONG WRONG!! + * lua is indeed 1-indexed, but this data is being interpreted + * by *opengl* not lua. we should not add 1!! + */ + lua_pushinteger(L, face.mIndices[i]); lua_rawseti(L, tbl, i+1); } } diff --git a/src/import/import_mesh.test.c b/src/import/import_mesh.test.c index fdbfbf0..7dc8624 100644 --- a/src/import/import_mesh.test.c +++ b/src/import/import_mesh.test.c @@ -55,18 +55,19 @@ void test_push_face() lily_assert_int_equal(lua_objlen(L, facetbl), 6); /* the numbers should be one higher because of lua's 1-indexing */ + /* WRONG! the numbers should be 0-indexed because they will be interpreted by opengl */ lua_rawgeti(L, facetbl, 1); - lily_assert_int_equal(lua_tointeger(L, -1), 1); + lily_assert_int_equal(lua_tointeger(L, -1), 0); lua_rawgeti(L, facetbl, 2); - lily_assert_int_equal(lua_tointeger(L, -1), 2); + lily_assert_int_equal(lua_tointeger(L, -1), 1); lua_rawgeti(L, facetbl, 3); - lily_assert_int_equal(lua_tointeger(L, -1), 3); + lily_assert_int_equal(lua_tointeger(L, -1), 2); lua_rawgeti(L, facetbl, 4); - lily_assert_int_equal(lua_tointeger(L, -1), 4); + lily_assert_int_equal(lua_tointeger(L, -1), 3); lua_rawgeti(L, facetbl, 5); - lily_assert_int_equal(lua_tointeger(L, -1), 5); + lily_assert_int_equal(lua_tointeger(L, -1), 4); lua_rawgeti(L, facetbl, 6); - lily_assert_int_equal(lua_tointeger(L, -1), 6); + lily_assert_int_equal(lua_tointeger(L, -1), 5); lua_close(L); } @@ -414,8 +415,8 @@ static int check_face(lua_State *L, int meshtbl, const char *field, int index, void test_faces(lua_State *L, int meshtbl) { - check_face(L, meshtbl, "faces", 1, 1, 2, 4); - check_face(L, meshtbl, "faces", 2, 1, 4, 3); + check_face(L, meshtbl, "faces", 1, 0, 1, 3); + check_face(L, meshtbl, "faces", 2, 0, 3, 2); } @@ -10,6 +10,7 @@ #include "logging/logging.h" #include "options/options.h" +void print_load_error(lua_State *L, const char *script_file, int error_type); int main(int argc, char **argv) { @@ -38,7 +39,7 @@ int main(int argc, char **argv) /* load main script */ int err = luaL_loadfile(L, options.script_file); if (err != 0) { - printf("cannot open file '%s'\n", options.script_file); + print_load_error(L, options.script_file, err); lua_close(L); return 0; } @@ -54,3 +55,25 @@ int main(int argc, char **argv) lua_close(L); return 0; } + + +void print_load_error(lua_State *L, const char *script_file, int error_type) +{ + switch(error_type) { + case LUA_ERRFILE: + printf("error: cannot open file '%s'\n", script_file); + break; + + case LUA_ERRSYNTAX: + printf("error: failed to compile file: %s\n", lua_tostring(L, -1)); + break; + + case LUA_ERRMEM: + printf("error: memory error: %s\n", lua_tostring(L, -1)); + break; + + default: + printf("error: an unknown error occured when trying to load file '%s'.\n", script_file); + break; + }; +} |