diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/image/image.c | 161 | ||||
-rw-r--r-- | src/import/CMakeLists.txt | 8 | ||||
-rw-r--r-- | src/import/import.c | 286 | ||||
-rw-r--r-- | src/main.c | 2 |
4 files changed, 11 insertions, 446 deletions
diff --git a/src/image/image.c b/src/image/image.c index 13152d0..64b7330 100644 --- a/src/image/image.c +++ b/src/image/image.c @@ -2,7 +2,7 @@ #include <honeysuckle.h> #include <cairo/cairo.h> /* assimp provides its own stb_image implementation */ -/*#define STB_IMAGE_IMPLEMENTATION*/ +#define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" #include "image.h" @@ -11,17 +11,6 @@ int empty(lua_State *L); int load_image(lua_State *L); int free_image(lua_State *L); -int surface_create(lua_State *L); -int surface_destroy(lua_State *L); -int surface_get_data(lua_State *L); -int context_create(lua_State *L); -int context_destroy(lua_State *L); -int context_select_font_face(lua_State *L); -int context_set_font_size(lua_State *L); -int context_show_text(lua_State *L); -int context_set_source_rgb(lua_State *L); -int context_move_to(lua_State *L); - void setup_image(lua_State *L, int honey_index) { @@ -30,26 +19,6 @@ void setup_image(lua_State *L, int honey_index) hs_str_cfunc("null", empty), hs_str_cfunc("load", load_image), hs_str_cfunc("destroy", free_image), - - /* cairo bindings */ - hs_str_cfunc("surface_create", surface_create), - hs_str_cfunc("surface_destroy", surface_destroy), - hs_str_cfunc("surface_get_data", surface_get_data), - hs_str_cfunc("context_create", context_create), - hs_str_cfunc("context_destroy", context_destroy), - hs_str_cfunc("context_select_font_face", context_select_font_face), - hs_str_cfunc("context_set_font_size", context_set_font_size), - hs_str_cfunc("context_show_text", context_show_text), - hs_str_cfunc("context_set_source_rgb", context_set_source_rgb), - hs_str_cfunc("context_move_to", context_move_to), - - /* format enum */ - hs_str_int("FORMAT_ARGB32", CAIRO_FORMAT_ARGB32), - - - /* font enums */ - hs_str_int("FONT_SLANT_NORMAL", CAIRO_FONT_SLANT_NORMAL), - hs_str_int("FONT_WEIGHT_NORMAL", CAIRO_FONT_WEIGHT_NORMAL), ); lua_setfield(L, honey_index, "image"); @@ -90,131 +59,3 @@ int free_image(lua_State *L) stbi_image_free(data); return 0; } - - -/* --===== cairo bindings =====-- */ - -int surface_create(lua_State *L) -{ - lua_Integer format, width, height; - hs_parse_args(L, hs_int(format), hs_int(width), hs_int(height)); - - cairo_surface_t *surface = cairo_image_surface_create(format, width, height); - cairo_status_t status = cairo_surface_status(surface); - if (status != CAIRO_STATUS_SUCCESS) - hs_throw_error(L, "error creating cairo surface: %s", cairo_status_to_string(status)); - - lua_pushlightuserdata(L, surface); - return 1; -} - - -int surface_destroy(lua_State *L) -{ - void *surface_ptr; - hs_parse_args(L, hs_light(surface_ptr)); - cairo_surface_t *surface = surface_ptr; - - cairo_surface_destroy(surface); - return 0; -} - - -int surface_get_data(lua_State *L) -{ - void *surface_ptr; - hs_parse_args(L, hs_light(surface_ptr)); - cairo_surface_t *surface = surface_ptr; - - unsigned char *data = cairo_image_surface_get_data(surface); - lua_pushlightuserdata(L, data); - return 1; -} - - -int context_create(lua_State *L) -{ - void *surface_ptr; - hs_parse_args(L, hs_light(surface_ptr)); - cairo_surface_t *surface = surface_ptr; - - cairo_t *cr = cairo_create(surface); - cairo_status_t status = cairo_status(cr); - if (status != CAIRO_STATUS_SUCCESS) - hs_throw_error(L, "error creating cairo context: %s", cairo_status_to_string(status)); - - lua_pushlightuserdata(L, cr); - return 1; -} - - -int context_destroy(lua_State *L) -{ - void *cr_ptr; - hs_parse_args(L, hs_light(cr_ptr)); - cairo_t *cr = cr_ptr; - - cairo_destroy(cr); - return 0; -} - - -int context_select_font_face(lua_State *L) -{ - void *cr_ptr; - char *family; - lua_Integer slant, weight; - hs_parse_args(L, hs_light(cr_ptr), hs_str(family), hs_int(slant), hs_int(weight)); - cairo_t *cr = cr_ptr; - - cairo_select_font_face(cr, family, slant, weight); - return 0; -} - - -int context_set_font_size(lua_State *L) -{ - void *cr_ptr; - lua_Number size; - hs_parse_args(L, hs_light(cr_ptr), hs_num(size)); - cairo_t *cr = cr_ptr; - - cairo_set_font_size(cr, size); - return 0; -} - - -int context_show_text(lua_State *L) -{ - void *cr_ptr; - char *str; - hs_parse_args(L, hs_light(cr_ptr), hs_str(str)); - cairo_t *cr = cr_ptr; - - cairo_show_text(cr, str); - return 0; -} - - -int context_set_source_rgb(lua_State *L) -{ - void *cr_ptr; - lua_Number r, g, b; - hs_parse_args(L, hs_light(cr_ptr), hs_num(r), hs_num(g), hs_num(b)); - cairo_t *cr = cr_ptr; - - cairo_set_source_rgb(cr, r, g, b); - return 0; -} - - -int context_move_to(lua_State *L) -{ - void *cr_ptr; - lua_Number x, y; - hs_parse_args(L, hs_light(cr_ptr), hs_num(x), hs_num(y)); - cairo_t *cr = cr_ptr; - - cairo_move_to(cr, x, y); - return 0; -} diff --git a/src/import/CMakeLists.txt b/src/import/CMakeLists.txt index 358e96c..31390f6 100644 --- a/src/import/CMakeLists.txt +++ b/src/import/CMakeLists.txt @@ -6,8 +6,8 @@ target_sources(honey PUBLIC target_sources(test PUBLIC - ${CMAKE_CURRENT_LIST_DIR}/import.test.c - ${CMAKE_CURRENT_LIST_DIR}/import_mesh.test.c - ${CMAKE_CURRENT_LIST_DIR}/import_node.test.c - ${CMAKE_CURRENT_LIST_DIR}/import_scene.test.c + # ${CMAKE_CURRENT_LIST_DIR}/import.test.c + # ${CMAKE_CURRENT_LIST_DIR}/import_mesh.test.c + # ${CMAKE_CURRENT_LIST_DIR}/import_node.test.c + # ${CMAKE_CURRENT_LIST_DIR}/import_scene.test.c ) diff --git a/src/import/import.c b/src/import/import.c index becb1fe..f860396 100644 --- a/src/import/import.c +++ b/src/import/import.c @@ -1,297 +1,21 @@ #include <lua.h> #include <honeysuckle.h> -#include <assimp/scene.h> -#include <assimp/cimport.h> -#include <assimp/postprocess.h> #include "import.h" +#define TINYOBJ_LOADER_C_IMPLEMENTATION +#include <tinyobj/tinyobj_loader_c.h> -#ifndef LILY_TEST_H - -static void push_scene(lua_State *L, struct aiScene *scene); - -int import_file(lua_State *L) +int dummy(lua_State *L) { - char *filename; - hs_parse_args(L, hs_str(filename)); - - const struct aiScene *scene = aiImportFile( - filename, - aiProcess_Triangulate | aiProcess_FlipUVs - ); - if (scene == NULL) - hs_throw_error(L, "failed to load file '%s'", filename); - - push_scene(L, (struct aiScene*) scene); - return 1; + return 0; } void setup_import(lua_State *L, int honey_tbl) { hs_create_table(L, - hs_str_cfunc("importFile", import_file), + hs_str_cfunc("dummy", dummy), ); lua_setfield(L, honey_tbl, "import"); } - -#endif - - -static void push_vector(lua_State *L, struct aiVector3D vec) -{ - hs_create_table(L, - hs_str_num("x", vec.x), - hs_str_num("y", vec.y), - hs_str_num("z", vec.z), - ); -} - - -static void push_face(lua_State *L, struct aiFace face) -{ - lua_createtable(L, face.mNumIndices, 0); - int tbl = lua_gettop(L); - - for (int i=0; i<face.mNumIndices; i++) { - /* add one to the index bc lua is 1-indexed */ - /* 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); - } -} - - -static void push_aistring(lua_State *L, struct aiString str) -{ - lua_pushstring(L, str.data); -} - - -#ifndef TEST_IMPORT_SCENE - -/* mesh components: - * DONE: - * mBitangents - * mFaces - * mNormals - * mNumFaces - * mNumVertices - * mTangents - * mVertices - * mNumUVComponents - * mTextureCoords - * - * TODO: - * mAnimMeshes - * mBones - * mColors - * mMaterialIndex - * mMethod - * mName - * mNumAnimMeshes - * mNumBones - * mPrimitiveTypes - * mTextureCoordsNames - */ -static void push_mesh(lua_State *L, struct aiMesh *mesh) -{ - /* --=== create mesh table ===-- */ - lua_createtable(L, 0, 1); - int meshtbl = lua_gettop(L); - - int count = mesh->mNumVertices; - int pop_count = 0; - - /* --=== 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); - normals = lua_gettop(L); - pop_count += 1; - } - - /* 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; - } - - /* uvs */ - int uvs = 0; - int uv_components = 0; - int uv_channels[AI_MAX_NUMBER_OF_TEXTURECOORDS]; - for (int i=0; i<AI_MAX_NUMBER_OF_TEXTURECOORDS; i++) { - if (mesh->mTextureCoords[i] != NULL) { - if (!uvs) { - /* ensure uv table is created */ - lua_createtable(L, 1, 0); - uvs = lua_gettop(L); - lua_createtable(L, 1, 0); - uv_components = lua_gettop(L); - pop_count += 2; - } - lua_createtable(L, count, 0); - uv_channels[i] = lua_gettop(L); - pop_count += 1; - - /* store table in uvs */ - lua_pushvalue(L, uv_channels[i]); - lua_rawseti(L, uvs, i+1); - /* store dimensionality */ - lua_pushinteger(L, mesh->mNumUVComponents[i]); - lua_rawseti(L, uv_components, i+1); - } - else - uv_channels[i] = 0; - } - - /* --=== 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); - } - - /* 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); - } - - /* uvs */ - if (uvs) { - for (int j=0; j<AI_MAX_NUMBER_OF_TEXTURECOORDS; j++) { - if (uv_channels[j]) { - push_vector(L, mesh->mTextureCoords[j][i]); - lua_rawseti(L, uv_channels[j], i+1); - } - } - } - } - - /* --=== populate faces ===-- */ - - int faces = 0; - if (mesh->mNumFaces != 0) { - lua_createtable(L, mesh->mNumFaces, 0); - faces = lua_gettop(L); - pop_count += 1; - for (int i=0; i<mesh->mNumFaces; i++) { - push_face(L, mesh->mFaces[i]); - lua_rawseti(L, faces, i+1); - } - } - - /* --=== assign values ===-- */ - - lua_pushvalue(L, vertices); - lua_setfield(L, meshtbl, "vertices"); - - if (normals) { - lua_pushvalue(L, normals); - 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"); - } - - if (uvs) { - lua_pushvalue(L, uvs); - lua_setfield(L, meshtbl, "uvs"); - lua_pushvalue(L, uv_components); - lua_setfield(L, meshtbl, "numUvComponents"); - } - - /* --=== clean up ===-- */ - lua_pop(L, pop_count); -} - - -static void push_node(lua_State *L, struct aiNode *node) -{ - lua_createtable(L, 0, 0); - int nodetbl = lua_gettop(L); - - int pop_count = 0; - - if (node->mMeshes != NULL) { - lua_createtable(L, node->mNumMeshes, 0); - int meshes = lua_gettop(L); - for (int i=0; i<node->mNumMeshes; i++) { - lua_pushinteger(L, node->mMeshes[i]+1); - lua_rawseti(L, meshes, i+1); - } - lua_setfield(L, nodetbl, "meshes"); - } - - if (node->mChildren != NULL) { - lua_createtable(L, node->mNumChildren, 0); - int children = lua_gettop(L); - for (int i=0; i<node->mNumChildren; i++) { - push_node(L, node->mChildren[i]); - lua_rawseti(L, children, i+1); - } - lua_setfield(L, nodetbl, "children"); - } -} - -#endif - - -static void push_scene(lua_State *L, struct aiScene *scene) -{ - /* meshes */ - lua_createtable(L, scene->mNumMeshes, 0); - int meshtbl = lua_gettop(L); - for (int i=0; i<scene->mNumMeshes; i++) { - push_mesh(L, scene->mMeshes[i]); - lua_rawseti(L, meshtbl, i+1); - } - - /* nodes */ - push_node(L, scene->mRootNode); - int nodetbl = lua_gettop(L); - - /* scene */ - hs_create_table(L, - hs_str_tbl("rootNode", nodetbl), - hs_str_tbl("meshes", meshtbl), - ); -} - - - @@ -32,7 +32,7 @@ int main(int argc, char **argv) setup_gl(L, honey_index); setup_glm(L, honey_index); setup_image(L, honey_index); - setup_import(L, honey_index); + // setup_import(L, honey_index); setup_logging(L, honey_index); setup_ode(L, honey_index); setup_util(L, honey_index); |