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);
#endif
rs->arena = arenaInit();
rs->asset_list =
(render_asset*) arenaAllocateBlock(rs->arena, sizeof(render_asset));
// TODO: need to update render_asset->next somewhere
render_asset* rass =
assetLoadFromFile(rs->arena, "../data/blender/icosphere.gltf");
rs->models = assetLoadFromFile(rs->arena, "../data/blender/icosphere.gltf");
assert(rs->models != nullptr);
// TODO: need to update model->next somewhere
renShutdown(rs);
return 0;

24
include/asset.h

@ -8,27 +8,31 @@
#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_indices;
mesh_asset* next;
glm::mat4 model_transform;
glm::vec3* vertices;
glm::vec3* normals;
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::mat4 xform;
};
#define MAX_PATH_SIZE 256
struct render_asset
struct model
{
uint total_size;
uint num_meshes;
uint name_len;
render_asset* next;
model* next;
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
util_image* diffuse_texture;
// FIXME: node_animation has a pointer to a render_object. need to decouple
@ -36,6 +40,6 @@ struct render_asset
//node_animation* node_anim;
};
render_asset*
model*
assetLoadFromFile(memory_arena* arena, const char* filename);

2
include/renderer.h

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

56
src/asset.cpp

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

Loading…
Cancel
Save