diff --git a/src/asset.cpp b/src/asset.cpp index b52a581..74dc3c7 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -15,6 +15,9 @@ model* initModel(Assets* assets, memory_arena* arena, tinygltf::Model t_mdl, const char* filename); +util_image* copyDiffuseTexture(Assets* assets, + memory_arena* arena, + const tinygltf::Model& t_mdl); bool parseMeshNode(mesh* m, memory_arena* arena, const tinygltf::Node& node, @@ -23,40 +26,6 @@ bool parseMeshNode(mesh* m, // interface -// FIXME: move to internal when finished -# if 0 -util_image* -copyDiffuseTexture(texture_assets* textures, - memory_arena* arena, - const tinygltf::Model& t_mdl) -{ - // NOTE: assuming material[0] since we're using pallete texture - assert(t_mdl.materials.size() == 1 - && t_mdl.textures.size() == 1 - && t_mdl.images.size() == 1 - && t_mdl.images[0].image.size() > 0); - tinygltf::Image t_img = t_mdl.images[0]; - - // TODO: re-alloc array when out of space - assert(textures->count < textures->max && arena != nullptr); - util_image* dtex = &textures->images[textures->count]; - textures->count++; - - dtex->w = t_img.width; - dtex->h = t_img.height; - dtex->bits_per_channel = t_img.bits; - dtex->num_channels = t_img.component; - dtex->data_len = t_img.image.size(); - dtex->pixels = ARENA_ALLOC(arena, u8, dtex->data_len); - // FIXME: overwriting memory here... also fix in libTangerine - std::strncpy(dtex->file_path, t_img.uri.c_str(), t_img.uri.size()); - dtex->filepath_hash = utilFNV64a_str(t_img.uri.c_str()); - std::memcpy(dtex->pixels, t_img.image.data(), dtex->data_len); - - return dtex; -} -#endif - model* assetLoadFromFile(Assets* assets, memory_arena* arena, @@ -80,15 +49,13 @@ assetLoadFromFile(Assets* assets, // NOTE: assume we're working with a single buffer assert(t_mdl.buffers.size() == 1); model* mdl = initModel(assets, arena, t_mdl, filename); -#if 0 - // FIXME: probably a bug here with overwriting arena memory - mdl->diffuse_texture = copyDiffuseTexture(textures, arena, t_mdl); + mdl->diffuse_texture = copyDiffuseTexture(assets, arena, t_mdl); + if (mdl->diffuse_texture == nullptr) { LOG(Error) << "Error Loading diffuse texture\n"; // TODO: reclaim arena memory return nullptr; } -#endif uint mesh_idx = 0; @@ -248,6 +215,44 @@ parseMeshNode(mesh* m, return false; } +util_image* +getFreeTexture(Assets* assets) +{ + if (assets->num_textures < assets->max_textures) + return &assets->textures[assets->num_textures++]; + + printf("%s(), no free textures\n", __FUNCTION__); + return nullptr; +} + +util_image* +copyDiffuseTexture(Assets* assets, + memory_arena* arena, + const tinygltf::Model& t_mdl) +{ + // NOTE: assuming material[0] since we're using pallete texture + assert(t_mdl.materials.size() == 1 + && t_mdl.textures.size() == 1 + && t_mdl.images.size() == 1 + && t_mdl.images[0].image.size() > 0); + tinygltf::Image t_img = t_mdl.images[0]; + + util_image* dtex = getFreeTexture(assets); + if (!dtex) return nullptr; + + dtex->w = t_img.width; + dtex->h = t_img.height; + dtex->bits_per_channel = t_img.bits; + dtex->num_channels = t_img.component; + dtex->data_len = t_img.image.size(); + dtex->pixels = ARENA_ALLOC(arena, u8, dtex->data_len); + std::strncpy(dtex->file_path, t_img.uri.c_str(), sizeof(dtex->file_path)); + dtex->filepath_hash = utilFNV64a_str(t_img.uri.c_str()); + std::memcpy(dtex->pixels, t_img.image.data(), dtex->data_len); + + return dtex; +} + const char* getTargetStr(int target) { diff --git a/src/asset.h b/src/asset.h index 6155996..66539fc 100644 --- a/src/asset.h +++ b/src/asset.h @@ -18,8 +18,6 @@ struct util_image uint data_len; u8* pixels; u64 filepath_hash; - // FIXME: should use a pointer here, and just add the length of file_path - // onto the allocation for util_image char file_path[256]; };