|
|
|
|
@ -44,48 +44,91 @@ getComponentType(int componentType)
|
|
|
|
|
default: return "UNKOWN"; } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
model_assets* |
|
|
|
|
assetInit(memory_arena* arena, uint asset_count) |
|
|
|
|
{ |
|
|
|
|
model_assets* assets = |
|
|
|
|
(model_assets*) arenaAllocateBlock(arena, sizeof(model_assets)); |
|
|
|
|
assert(assets != nullptr); |
|
|
|
|
assets->models = |
|
|
|
|
(model*) arenaAllocateBlock(arena, asset_count * sizeof(model)); |
|
|
|
|
assert(assets->models != nullptr); |
|
|
|
|
assets->max = asset_count; |
|
|
|
|
|
|
|
|
|
return assets; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
dumpBuffer(tinygltf::Model model, tinygltf::Accessor acc) |
|
|
|
|
{ |
|
|
|
|
size_t bv_idx = acc.bufferView; |
|
|
|
|
assert(bv_idx >= 0 && bv_idx < model.bufferViews.size()); |
|
|
|
|
tinygltf::BufferView bv = model.bufferViews[bv_idx]; |
|
|
|
|
|
|
|
|
|
LOG(Debug) << "-----------------------\n"; |
|
|
|
|
LOG(Debug) << "buf idx: " << bv_idx << "\n"; |
|
|
|
|
LOG(Debug) << "buf target: " << getTargetStr(bv.target) << "\n"; |
|
|
|
|
LOG(Debug) << "buf len: " << bv.byteLength << "\n"; |
|
|
|
|
LOG(Debug) << "buf offset: " << bv.byteOffset << "\n"; |
|
|
|
|
LOG(Debug) << "buf stride: " << bv.byteStride << "\n"; |
|
|
|
|
LOG(Debug) << "acc component type: " |
|
|
|
|
<< getComponentType(acc.componentType) << "\n"; |
|
|
|
|
LOG(Debug) << "acc element type: " |
|
|
|
|
<< getElementType(acc.type) << "\n"; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
model* |
|
|
|
|
assetLoadFromFile(memory_arena* arena, const char* filename) |
|
|
|
|
assetLoadFromFile(model_assets* assets, |
|
|
|
|
memory_arena* arena, |
|
|
|
|
const char* filename) |
|
|
|
|
{ |
|
|
|
|
tinygltf::Model mdl; |
|
|
|
|
// TODO: re-alloc array when out of space
|
|
|
|
|
assert(assets->count < assets->max && arena != nullptr); |
|
|
|
|
model* mdl = &assets->models[assets->count]; |
|
|
|
|
assets->count++; |
|
|
|
|
|
|
|
|
|
tinygltf::Model t_mdl; |
|
|
|
|
tinygltf::TinyGLTF gltf_ctx; |
|
|
|
|
std::string err; |
|
|
|
|
std::string warn; |
|
|
|
|
|
|
|
|
|
if (!gltf_ctx.LoadASCIIFromFile(&mdl, &err, &warn, filename)) { |
|
|
|
|
if (!gltf_ctx.LoadASCIIFromFile(&t_mdl, &err, &warn, filename)) { |
|
|
|
|
LOG(Error) << "Error loading file: " << filename << "\n"; |
|
|
|
|
return nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
uint buf_count = mdl.bufferViews.size(); |
|
|
|
|
model* ra = (model*) arenaAllocateBlock(arena, sizeof(model)); |
|
|
|
|
ra->meshes = (mesh*) arenaAllocateBlock(arena, buf_count * sizeof(mesh)); |
|
|
|
|
ra->num_meshes = mdl.meshes.size(); |
|
|
|
|
uint buf_count = t_mdl.bufferViews.size(); |
|
|
|
|
mdl->meshes = (mesh*) arenaAllocateBlock(arena, buf_count * sizeof(mesh)); |
|
|
|
|
mdl->num_meshes = t_mdl.meshes.size(); |
|
|
|
|
uint name_len = std::strlen(filename); |
|
|
|
|
assert(name_len < MAX_PATH_SIZE); |
|
|
|
|
ra->filepath = (char*) arenaAllocateBlock(arena, name_len + 1); |
|
|
|
|
std::strncpy(ra->filepath, filename, name_len); |
|
|
|
|
ra->filepath_hash = utilFNV64a_str(ra->filepath); |
|
|
|
|
|
|
|
|
|
// FIXME: need to recreate node hierarchy
|
|
|
|
|
for (tinygltf::Node node : mdl.nodes) { |
|
|
|
|
for (tinygltf::Accessor acc : mdl.accessors) { |
|
|
|
|
size_t bv_idx = acc.bufferView; |
|
|
|
|
assert(bv_idx >= 0 && bv_idx < mdl.bufferViews.size()); |
|
|
|
|
tinygltf::BufferView bv = mdl.bufferViews[bv_idx]; |
|
|
|
|
|
|
|
|
|
LOG(Debug) << "-----------------------\n"; |
|
|
|
|
LOG(Debug) << "buf idx: " << bv_idx << "\n"; |
|
|
|
|
LOG(Debug) << "buf target: " << getTargetStr(bv.target) << "\n"; |
|
|
|
|
LOG(Debug) << "buf len: " << bv.byteLength << "\n"; |
|
|
|
|
LOG(Debug) << "buf offset: " << bv.byteOffset << "\n"; |
|
|
|
|
LOG(Debug) << "buf stride: " << bv.byteStride << "\n"; |
|
|
|
|
LOG(Debug) << "acc component type: " |
|
|
|
|
<< getComponentType(acc.componentType) << "\n"; |
|
|
|
|
LOG(Debug) << "acc element type: " |
|
|
|
|
<< getElementType(acc.type) << "\n"; |
|
|
|
|
mdl->filepath = (char*) arenaAllocateBlock(arena, name_len + 1); |
|
|
|
|
std::strncpy(mdl->filepath, filename, name_len); |
|
|
|
|
mdl->filepath_hash = utilFNV64a_str(mdl->filepath); |
|
|
|
|
|
|
|
|
|
// TODO: also need to get each node xform
|
|
|
|
|
// TODO: also need to dump textures
|
|
|
|
|
for (tinygltf::Node node : t_mdl.nodes) { |
|
|
|
|
LOG(Debug) << "##################\n"; |
|
|
|
|
LOG(Debug) << "node mesh idx: " << node.mesh << "\n"; |
|
|
|
|
|
|
|
|
|
if (node.mesh >= 0) { |
|
|
|
|
LOG(Debug) << "node mesh name: " |
|
|
|
|
<< t_mdl.meshes[node.mesh].name << "\n"; |
|
|
|
|
|
|
|
|
|
tinygltf::Primitive prim = t_mdl.meshes[node.mesh].primitives[0]; |
|
|
|
|
for (auto& att : prim.attributes) { |
|
|
|
|
LOG(Debug) << "dumping buffer: " << att.first << "\n"; |
|
|
|
|
dumpBuffer(t_mdl, t_mdl.accessors[att.second]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
LOG(Debug) << "dumping index buffer\n"; |
|
|
|
|
dumpBuffer(t_mdl, t_mdl.accessors[prim.indices]); |
|
|
|
|
} else { |
|
|
|
|
LOG(Debug) << "Not a mesh node\n"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ra; |
|
|
|
|
return mdl; |
|
|
|
|
} |
|
|
|
|
|