diff options
Diffstat (limited to 'src/resources/gltf.c')
-rw-r--r-- | src/resources/gltf.c | 232 |
1 files changed, 116 insertions, 116 deletions
diff --git a/src/resources/gltf.c b/src/resources/gltf.c index c51e30d..e381954 100644 --- a/src/resources/gltf.c +++ b/src/resources/gltf.c @@ -21,24 +21,24 @@ #define CGLTF_IMPLEMENTATION #include <cgltf.h> -extern core g_core; +extern Core g_core; struct face { cgltf_uint indices[3]; }; typedef struct face face; -KITC_DECL_TYPED_ARRAY(vec3) -KITC_DECL_TYPED_ARRAY(vec2) -KITC_DECL_TYPED_ARRAY(vec4u) -KITC_DECL_TYPED_ARRAY(vec4) +KITC_DECL_TYPED_ARRAY(Vec3) +KITC_DECL_TYPED_ARRAY(Vec2) +KITC_DECL_TYPED_ARRAY(Vec4u) +KITC_DECL_TYPED_ARRAY(Vec4) KITC_DECL_TYPED_ARRAY(face) // KITC_DECL_TYPED_ARRAY(joint) -bool model_load_gltf_str(const char *file_string, const char *filepath, str8 relative_path, - model *out_model, bool invert_textures_y); +bool model_load_gltf_str(const char *file_string, const char *filepath, Str8 relative_path, + Model *out_model, bool invert_textures_y); -model_handle model_load_gltf(const char *path, bool invert_texture_y) { +ModelHandle model_load_gltf(const char *path, bool invert_texture_y) { size_t arena_size = 1024; arena scratch = arena_create(malloc(arena_size), arena_size); @@ -49,22 +49,22 @@ model_handle model_load_gltf(const char *path, bool invert_texture_y) { } const char *file_string = string_from_file(path); - model_handle handle; - model *model = model_pool_alloc(&g_core.models, &handle); - model->name = str8_cstr_view(path); - model->meshes = mesh_darray_new(1); - model->materials = material_darray_new(1); + ModelHandle handle; + // modfl *model = model_pool_alloc(&g_core.models, &handle); + // model->name = str8_cstr_view(path); + // model->meshes = mesh_darray_new(1); + // model->materials = material_darray_new(1); - bool success = - model_load_gltf_str(file_string, path, relative_path.path, model, invert_texture_y); + // bool success = + // model_load_gltf_str(file_string, path, relative_path.path, model, invert_texture_y); - if (!success) { - FATAL("Couldnt load GLTF file at path %s", path); - ERROR_EXIT("Load fails are considered crash-worthy right now. This will change later.\n"); - } + // if (!success) { + // FATAL("Couldnt load GLTF file at path %s", path); + // ERROR_EXIT("Load fails are considered crash-worthy right now. This will change later.\n"); + // } - arena_free_all(&scratch); - arena_free_storage(&scratch); + // arena_free_all(&scratch); + // arena_free_storage(&scratch); return handle; } @@ -85,31 +85,31 @@ typedef struct model { u32 mesh_count; } model; */ -bool model_load_gltf_str(const char *file_string, const char *filepath, str8 relative_path, - model *out_model, bool invert_textures_y) { - TRACE("Load GLTF from string"); - - // Setup temps - vec3_darray *tmp_positions = vec3_darray_new(1000); - vec3_darray *tmp_normals = vec3_darray_new(1000); - vec2_darray *tmp_uvs = vec2_darray_new(1000); - vec4u_darray *tmp_joint_indices = vec4u_darray_new(1000); - vec4_darray *tmp_weights = vec4_darray_new(1000); - // FIXME - // joint_darray *tmp_joints = joint_darray_new(256); - // vertex_bone_data_darray *tmp_vertex_bone_data = vertex_bone_data_darray_new(1000); - - cgltf_options options = { 0 }; +bool model_load_gltf_str(const char *file_string, const char *filepath, Str8 relative_path, + Model *out_model, bool invert_textures_y) { + // TRACE("Load GLTF from string"); + + // // Setup temps + // vec3_darray *tmp_positions = vec3_darray_new(1000); + // vec3_darray *tmp_normals = vec3_darray_new(1000); + // vec2_darray *tmp_uvs = vec2_darray_new(1000); + // vec4u_darray *tmp_joint_indices = vec4u_darray_new(1000); + // vec4_darray *tmp_weights = vec4_darray_new(1000); + // // FIXME + // // joint_darray *tmp_joints = joint_darray_new(256); + // // vertex_bone_data_darray *tmp_vertex_bone_data = vertex_bone_data_darray_new(1000); + + // cgltf_options options = { 0 }; cgltf_data *data = NULL; - cgltf_result result = cgltf_parse_file(&options, filepath, &data); - if (result != cgltf_result_success) { - WARN("gltf load failed"); - // TODO: cleanup arrays(allocate all from arena ?) - return false; - } + // cgltf_result result = cgltf_parse_file(&options, filepath, &data); + // if (result != cgltf_result_success) { + // WARN("gltf load failed"); + // // TODO: cleanup arrays(allocate all from arena ?) + // return false; + // } - cgltf_load_buffers(&options, data, filepath); - DEBUG("loaded buffers"); + // cgltf_load_buffers(&options, data, filepath); + // DEBUG("loaded buffers"); // // --- Skin // size_t num_skins = data->skins_count; @@ -183,13 +183,13 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel snprintf(normal_map_path, sizeof(normal_map_path), "%s/%s", relative_path.buf, normal_tex_view.texture->image->uri); - material our_material = - pbr_material_load(albedo_map_path, normal_map_path, true, metal_rough_map_path, NULL, NULL); + // material our_material = + // pbr_material_load(albedo_map_path, normal_map_path, true, metal_rough_map_path, NULL, NULL); - our_material.name = malloc(strlen(gltf_material.name) + 1); - strcpy(our_material.name, gltf_material.name); + // our_material.name = malloc(strlen(gltf_material.name) + 1); + // strcpy(our_material.name, gltf_material.name); - material_darray_push(out_model->materials, our_material); + // material_darray_push(out_model->materials, our_material); } // TEMP @@ -214,11 +214,11 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel TRACE("Loading %d vec3 components", accessor->count); - for (cgltf_size v = 0; v < accessor->count; ++v) { - vec3 pos; - cgltf_accessor_read_float(accessor, v, &pos.x, 3); - vec3_darray_push(tmp_positions, pos); - } + // for (cgltf_size v = 0; v < accessor->count; ++v) { + // vec3 pos; + // cgltf_accessor_read_float(accessor, v, &pos.x, 3); + // vec3_darray_push(tmp_positions, pos); + // } } else if (attribute.type == cgltf_attribute_type_normal) { TRACE("Load normals from accessor"); @@ -227,11 +227,11 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel assert(accessor->component_type == cgltf_component_type_r_32f); // CASSERT_MSG(accessor->type == cgltf_type_vec3, "Normal vectors should be a vec3"); - for (cgltf_size v = 0; v < accessor->count; ++v) { - vec3 pos; - cgltf_accessor_read_float(accessor, v, &pos.x, 3); - vec3_darray_push(tmp_normals, pos); - } + // for (cgltf_size v = 0; v < accessor->count; ++v) { + // vec3 pos; + // cgltf_accessor_read_float(accessor, v, &pos.x, 3); + // vec3_darray_push(tmp_normals, pos); + // } } else if (attribute.type == cgltf_attribute_type_texcoord) { TRACE("Load texture coordinates from accessor"); @@ -241,12 +241,12 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel // vec2"); for (cgltf_size v = 0; v < accessor->count; ++v) { - vec2 tex; - bool success = cgltf_accessor_read_float(accessor, v, &tex.x, 2); - if (!success) { - ERROR("Error loading tex coord"); - } - vec2_darray_push(tmp_uvs, tex); + // vec2 tex; + // bool success = cgltf_accessor_read_float(accessor, v, &tex.x, 2); + // if (!success) { + // ERROR("Error loading tex coord"); + // } + // vec2_darray_push(tmp_uvs, tex); } } else if (attribute.type == cgltf_attribute_type_joints) { // FIXME: joints @@ -289,18 +289,18 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel } // mesh.vertex_bone_data = vertex_bone_data_darray_new(1); - if (primitive.material != NULL) { - ERROR("Primitive Material %s", primitive.material->name); - for (u32 i = 0; i < material_darray_len(out_model->materials); i++) { - printf("%s vs %s \n", primitive.material->name, out_model->materials->data[i].name); - if (strcmp(primitive.material->name, out_model->materials->data[i].name) == 0) { - INFO("Found material"); - mat_idx = i; - // mesh.material_index = i; - break; - } - } - } + // if (primitive.material != NULL) { + // ERROR("Primitive Material %s", primitive.material->name); + // for (u32 i = 0; i < material_darray_len(out_model->materials); i++) { + // printf("%s vs %s \n", primitive.material->name, out_model->materials->data[i].name); + // if (strcmp(primitive.material->name, out_model->materials->data[i].name) == 0) { + // INFO("Found material"); + // mat_idx = i; + // // mesh.material_index = i; + // break; + // } + // } + // } // // FIXME // // if (is_skinned) { @@ -322,34 +322,34 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel // // } bool has_indices = false; - vertex_darray *geo_vertices = vertex_darray_new(3); + Vertex_darray *geo_vertices = Vertex_darray_new(3); u32_darray *geo_indices = u32_darray_new(0); // Store vertices - printf("Positions %d Normals %d UVs %d\n", tmp_positions->len, tmp_normals->len, tmp_uvs->len); - assert(tmp_positions->len == tmp_normals->len); - assert(tmp_normals->len == tmp_uvs->len); - for (u32 v_i = 0; v_i < tmp_positions->len; v_i++) { - vertex v = { .static_3d = { - .position = tmp_positions->data[v_i], - .normal = tmp_normals->data[v_i], - .tex_coords = tmp_uvs->data[v_i], - } }; - vertex_darray_push(geo_vertices, v); + // printf("Positions %d Normals %d UVs %d\n", tmp_positions->len, tmp_normals->len, tmp_uvs->len); + // assert(tmp_positions->len == tmp_normals->len); + // assert(tmp_normals->len == tmp_uvs->len); + // for (u32 v_i = 0; v_i < tmp_positions->len; v_i++) { + // vertex v = { .static_3d = { + // .position = tmp_positions->data[v_i], + // .normal = tmp_normals->data[v_i], + // .tex_coords = tmp_uvs->data[v_i], + // } }; + // vertex_darray_push(geo_vertices, v); } // Store indices - cgltf_accessor *indices = primitive.indices; - if (primitive.indices > 0) { - WARN("indices! %d", indices->count); - has_indices = true; - - // store indices - for (cgltf_size i = 0; i < indices->count; ++i) { - cgltf_uint ei; - cgltf_accessor_read_uint(indices, i, &ei, 1); - u32_darray_push(geo_indices, ei); - } + // cgltf_accessor *indices = primitive.indices; + // if (primitive.indices > 0) { + // WARN("indices! %d", indices->count); + // has_indices = true; + + // // store indices + // for (cgltf_size i = 0; i < indices->count; ++i) { + // cgltf_uint ei; + // cgltf_accessor_read_uint(indices, i, &ei, 1); + // u32_darray_push(geo_indices, ei); + // } // fetch and store vertices for each index // for (cgltf_size i = 0; i < indices->count; ++i) { @@ -366,23 +366,23 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, str8 rel // } // // for each vertex do the bone data // } - } else { - has_indices = false; - return false; // TODO: handle this - } - - geometry_data *geometry = malloc(sizeof(geometry_data)); - geometry->format = VERTEX_STATIC_3D; - geometry->colour = (rgba){ 1, 1, 1, 1 }; - geometry->vertices = geo_vertices; - geometry->indices = geo_indices; - geometry->has_indices = has_indices; - - mesh m = mesh_create(geometry, true); - m.material_index = (u32_opt){ .has_value = mat_idx == 9999, .value = mat_idx }; - - mesh_darray_push(out_model->meshes, m); - } + // } else { + // has_indices = false; + // return false; // TODO: handle this + // } + + // geometry_data *geometry = malloc(sizeof(geometry_data)); + // geometry->format = VERTEX_STATIC_3D; + // geometry->colour = (rgba){ 1, 1, 1, 1 }; + // geometry->vertices = geo_vertices; + // geometry->indices = geo_indices; + // geometry->has_indices = has_indices; + + // mesh m = mesh_create(geometry, true); + // m.material_index = (u32_opt){ .has_value = mat_idx == 9999, .value = mat_idx }; + + // mesh_darray_push(out_model->meshes, m); + // } // // clear data for each mesh // vec3_darray_clear(tmp_positions); |