Browse Source

move copyDiffuseTexture function from interface section

main
cinnaboot 5 years ago
parent
commit
c3c3a5be61
  1. 81
      src/asset.cpp
  2. 2
      src/asset.h

81
src/asset.cpp

@ -15,6 +15,9 @@ model* initModel(Assets* assets,
memory_arena* arena, memory_arena* arena,
tinygltf::Model t_mdl, tinygltf::Model t_mdl,
const char* filename); const char* filename);
util_image* copyDiffuseTexture(Assets* assets,
memory_arena* arena,
const tinygltf::Model& t_mdl);
bool parseMeshNode(mesh* m, bool parseMeshNode(mesh* m,
memory_arena* arena, memory_arena* arena,
const tinygltf::Node& node, const tinygltf::Node& node,
@ -23,40 +26,6 @@ bool parseMeshNode(mesh* m,
// interface // 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* model*
assetLoadFromFile(Assets* assets, assetLoadFromFile(Assets* assets,
memory_arena* arena, memory_arena* arena,
@ -80,15 +49,13 @@ assetLoadFromFile(Assets* assets,
// NOTE: assume we're working with a single buffer // NOTE: assume we're working with a single buffer
assert(t_mdl.buffers.size() == 1); assert(t_mdl.buffers.size() == 1);
model* mdl = initModel(assets, arena, t_mdl, filename); model* mdl = initModel(assets, arena, t_mdl, filename);
#if 0 mdl->diffuse_texture = copyDiffuseTexture(assets, arena, t_mdl);
// FIXME: probably a bug here with overwriting arena memory
mdl->diffuse_texture = copyDiffuseTexture(textures, arena, t_mdl);
if (mdl->diffuse_texture == nullptr) { if (mdl->diffuse_texture == nullptr) {
LOG(Error) << "Error Loading diffuse texture\n"; LOG(Error) << "Error Loading diffuse texture\n";
// TODO: reclaim arena memory // TODO: reclaim arena memory
return nullptr; return nullptr;
} }
#endif
uint mesh_idx = 0; uint mesh_idx = 0;
@ -248,6 +215,44 @@ parseMeshNode(mesh* m,
return false; 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* const char*
getTargetStr(int target) getTargetStr(int target)
{ {

2
src/asset.h

@ -18,8 +18,6 @@ struct util_image
uint data_len; uint data_len;
u8* pixels; u8* pixels;
u64 filepath_hash; 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]; char file_path[256];
}; };

Loading…
Cancel
Save