diff --git a/examples/render_groups/main.cpp b/examples/render_groups/main.cpp index dbf2d7c..39365d8 100644 --- a/examples/render_groups/main.cpp +++ b/examples/render_groups/main.cpp @@ -177,9 +177,10 @@ main() renDoRenderLoop(rs, 60, doFrameCallbackPre); #endif - rs->models = assetLoadFromFile(rs->arena, "../data/blender/spaceship.gltf"); - assert(rs->models != nullptr); - // TODO: need to update model->next somewhere + model* ret = assetLoadFromFile(rs->models, + rs->arena, + "../data/blender/spaceship.gltf"); + assert(ret != nullptr); renShutdown(rs); return 0; diff --git a/include/asset.h b/include/asset.h index 245393c..b0b0288 100644 --- a/include/asset.h +++ b/include/asset.h @@ -29,7 +29,6 @@ struct mesh struct model { uint num_meshes; - model* next; char* filepath; uint64_t filepath_hash; mesh* meshes; // NOTE: fixed sized array @@ -40,6 +39,18 @@ struct model //node_animation* node_anim; }; +struct model_assets +{ + model* models; + uint count; + uint max; +}; + +model_assets* +assetInit(memory_arena* arena, uint asset_count); + model* -assetLoadFromFile(memory_arena* arena, const char* filename); +assetLoadFromFile(model_assets* assets, + memory_arena* arena, + const char* filename); diff --git a/include/renderer.h b/include/renderer.h index b9334d6..7200f93 100644 --- a/include/renderer.h +++ b/include/renderer.h @@ -56,7 +56,7 @@ struct render_state uint render_group_count; // WIP - model* models; // NOTE: head pointer of linked list + model_assets* models; memory_arena* arena; // FIXME: should be a hashmap for textures loaded from assets // ie) pallete textures @@ -69,11 +69,13 @@ struct render_state light_group* lights; }; +#define DEFAULT_ASSET_SIZE 256 render_state* renInit(const char* title = "Tangerine", glm::vec2 viewport_dims = glm::vec2(1280, 720), Uint32 SDL_init_flags = 0, - size_t arena_size = DEFAULT_ARENA_SIZE); + size_t arena_size = DEFAULT_ARENA_SIZE, + uint asset_size = DEFAULT_ASSET_SIZE); void renShutdown(render_state* rs); diff --git a/src/asset.cpp b/src/asset.cpp index 2aee3e9..d9abb55 100644 --- a/src/asset.cpp +++ b/src/asset.cpp @@ -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; } diff --git a/src/renderer.cpp b/src/renderer.cpp index fc24084..9d1a2b5 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -41,11 +41,13 @@ render_state* renInit(const char* title, glm::vec2 viewport_dims, Uint32 SDL_init_flags, - size_t arena_size) + size_t arena_size, + uint asset_size) { render_state* rs = UTIL_ALLOC(1, render_state); rs->handles = UTIL_ALLOC(1, SDL_Handles); rs->arena = arenaInit(arena_size); + rs->models = assetInit(rs->arena, asset_size); setDefaults(rs, viewport_dims); if (initSDL(rs->handles, SDL_init_flags) &&