Browse Source

debug tinygltf structure

render_group_fix
cinnaboot 5 years ago
parent
commit
5aaf90655c
  1. 9
      examples/render_groups/main.cpp
  2. 24
      include/asset.h
  3. 2
      include/renderer.h
  4. 56
      src/asset.cpp

9
examples/render_groups/main.cpp

@ -178,12 +178,9 @@ main()
renDoRenderLoop(rs, 60, doFrameCallbackPre); renDoRenderLoop(rs, 60, doFrameCallbackPre);
#endif #endif
rs->arena = arenaInit(); rs->arena = arenaInit();
rs->asset_list = rs->models = assetLoadFromFile(rs->arena, "../data/blender/icosphere.gltf");
(render_asset*) arenaAllocateBlock(rs->arena, sizeof(render_asset)); assert(rs->models != nullptr);
// TODO: need to update render_asset->next somewhere // TODO: need to update model->next somewhere
render_asset* rass =
assetLoadFromFile(rs->arena, "../data/blender/icosphere.gltf");
renShutdown(rs); renShutdown(rs);
return 0; return 0;

24
include/asset.h

@ -8,27 +8,31 @@
#include "util_image.h" #include "util_image.h"
struct mesh_asset // NOTE: wrapper tinygltf https://github.com/syoyo/tinygltf
// https://github.com/KhronosGroup/glTF
struct mesh
{ {
uint asset_size;
uint num_vertices; uint num_vertices;
uint num_indices; uint num_indices;
mesh_asset* next;
glm::mat4 model_transform;
glm::vec3* vertices; glm::vec3* vertices;
glm::vec3* normals;
uint* indices; uint* indices;
// FIXME: will have to use vec2 here to be able to use memcpy
// this will break render_object::initGLFLoatBuffer logic which expects
// vec3
glm::vec3* texture_coords; glm::vec3* texture_coords;
glm::mat4 xform;
}; };
#define MAX_PATH_SIZE 256 #define MAX_PATH_SIZE 256
struct render_asset struct model
{ {
uint total_size;
uint num_meshes; uint num_meshes;
uint name_len; model* next;
render_asset* next;
char* filepath; char* filepath;
mesh_asset* meshes; // NOTE: fixed sized array uint64_t filepath_hash;
mesh* meshes; // NOTE: fixed sized array
// NOTE: pointer to a texture in render_state->textures // NOTE: pointer to a texture in render_state->textures
util_image* diffuse_texture; util_image* diffuse_texture;
// FIXME: node_animation has a pointer to a render_object. need to decouple // FIXME: node_animation has a pointer to a render_object. need to decouple
@ -36,6 +40,6 @@ struct render_asset
//node_animation* node_anim; //node_animation* node_anim;
}; };
render_asset* model*
assetLoadFromFile(memory_arena* arena, const char* filename); assetLoadFromFile(memory_arena* arena, const char* filename);

2
include/renderer.h

@ -56,7 +56,7 @@ struct render_state
uint render_group_count; uint render_group_count;
// WIP // WIP
render_asset* asset_list; // NOTE: head pointer of linked list model* models; // NOTE: head pointer of linked list
memory_arena* arena; memory_arena* arena;
// FIXME: should be a hashmap for textures loaded from assets // FIXME: should be a hashmap for textures loaded from assets
// ie) pallete textures // ie) pallete textures

56
src/asset.cpp

@ -1,5 +1,6 @@
#include <iostream> #include <cassert>
#include <cstring>
#define TINYGLTF_IMPLEMENTATION #define TINYGLTF_IMPLEMENTATION
//#define STB_IMAGE_IMPLEMENTATION //#define STB_IMAGE_IMPLEMENTATION
@ -42,41 +43,48 @@ getComponentType(int componentType)
case 5130: return "TINYGLTF_COMPONENT_TYPE_DOUBLE"; case 5130: return "TINYGLTF_COMPONENT_TYPE_DOUBLE";
default: return "UNKOWN"; } default: return "UNKOWN"; }
} }
render_asset*
model*
assetLoadFromFile(memory_arena* arena, const char* filename) assetLoadFromFile(memory_arena* arena, const char* filename)
{ {
tinygltf::Model model; tinygltf::Model mdl;
// TODO: see if there's any impact in creating a new context for each file
tinygltf::TinyGLTF gltf_ctx; tinygltf::TinyGLTF gltf_ctx;
std::string err; std::string err;
std::string warn; std::string warn;
if (!gltf_ctx.LoadASCIIFromFile(&model, &err, &warn, filename)) { if (!gltf_ctx.LoadASCIIFromFile(&mdl, &err, &warn, filename)) {
LOG(Error) << "Error loading file: " << filename << "\n"; LOG(Error) << "Error loading file: " << filename << "\n";
return nullptr; return nullptr;
} }
uint buf_count = model.bufferViews.size(); uint buf_count = mdl.bufferViews.size();
render_asset* ra = model* ra = (model*) arenaAllocateBlock(arena, sizeof(model));
(render_asset*) arenaAllocateBlock(arena, sizeof(render_asset)); ra->meshes = (mesh*) arenaAllocateBlock(arena, buf_count * sizeof(mesh));
ra->meshes = ra->num_meshes = mdl.meshes.size();
(mesh_asset*) arenaAllocateBlock(arena, buf_count * sizeof(mesh_asset)); 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);
for (uint i = 0; i < model.accessors.size(); i++) { // FIXME: need to recreate node hierarchy
tinygltf::Accessor acc = model.accessors[i]; for (tinygltf::Node node : mdl.nodes) {
size_t bv_idx = acc.bufferView; for (tinygltf::Accessor acc : mdl.accessors) {
assert(bv_idx >= 0 && bv_idx < model.bufferViews.size()); size_t bv_idx = acc.bufferView;
tinygltf::BufferView bv = model.bufferViews[bv_idx]; assert(bv_idx >= 0 && bv_idx < mdl.bufferViews.size());
tinygltf::BufferView bv = mdl.bufferViews[bv_idx];
LOG(Debug) << "-----------------------\n"; LOG(Debug) << "-----------------------\n";
LOG(Debug) << "buf idx: " << i << "\n"; LOG(Debug) << "buf idx: " << bv_idx << "\n";
LOG(Debug) << "buf target: " << getTargetStr(bv.target) << "\n"; LOG(Debug) << "buf target: " << getTargetStr(bv.target) << "\n";
LOG(Debug) << "buf len: " << bv.byteLength << "\n"; LOG(Debug) << "buf len: " << bv.byteLength << "\n";
LOG(Debug) << "buf offset: " << bv.byteOffset << "\n"; LOG(Debug) << "buf offset: " << bv.byteOffset << "\n";
LOG(Debug) << "buf stride: " << bv.byteStride << "\n"; LOG(Debug) << "buf stride: " << bv.byteStride << "\n";
LOG(Debug) << "buf component type: " LOG(Debug) << "acc component type: "
<< getComponentType(acc.componentType) << "\n"; << getComponentType(acc.componentType) << "\n";
LOG(Debug) << "buf element type: " << getElementType(acc.type) << "\n"; LOG(Debug) << "acc element type: "
<< getElementType(acc.type) << "\n";
}
} }
return ra; return ra;

Loading…
Cancel
Save