diff options
author | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-17 15:01:54 +1100 |
---|---|---|
committer | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-17 15:01:54 +1100 |
commit | c97327fcdcbe8b85d7718cfb722e1525986a8514 (patch) | |
tree | 9c0df938345bff356f8fcc97740afedb1f500b3d /src/resources | |
parent | 1afe4876cb8133c5b47fdcfeb07decc5565c4844 (diff) | |
parent | 51b4a3fc75351d6ecd2142c228d31a1f7ed52152 (diff) |
Merge branch 'master' into transform-hierarchy
Diffstat (limited to 'src/resources')
-rw-r--r-- | src/resources/obj.c | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/src/resources/obj.c b/src/resources/obj.c index 658967e..c6e9fa6 100644 --- a/src/resources/obj.c +++ b/src/resources/obj.c @@ -15,6 +15,7 @@ #include "file.h" #include "log.h" #include "maths.h" +#include "mem.h" #include "path.h" #include "render.h" #include "render_types.h" @@ -35,21 +36,21 @@ KITC_DECL_TYPED_ARRAY(face) void create_submesh(mesh_darray *meshes, vec3_darray *tmp_positions, vec3_darray *tmp_normals, vec2_darray *tmp_uvs, face_darray *tmp_faces, material_darray *materials, bool material_loaded, char current_material_name[256]); -bool load_material_lib(const char *path, material_darray *materials); +bool load_material_lib(const char *path, str8 relative_path, material_darray *materials); bool model_load_obj_str(const char *file_string, str8 relative_path, model *out_model, bool invert_textures_y); model_handle model_load_obj(core *core, const char *path, bool invert_textures_y) { + size_t arena_size = 1024; + arena scratch = arena_create(malloc(arena_size), arena_size); + TRACE("Loading model at Path %s\n", path); - path_opt relative_path = path_parent(path); + path_opt relative_path = path_parent(&scratch, path); if (!relative_path.has_value) { WARN("Couldnt get a relative path for the path to use for loading materials & textures later"); } - printf("Relative path: %s\n", relative_path.path.buf); const char *file_string = string_from_file(path); - // TODO: store the relative path without the name.obj at the end - model model = { 0 }; model.name = str8_cstr_view(path); model.meshes = mesh_darray_new(1); @@ -64,6 +65,9 @@ model_handle model_load_obj(core *core, const char *path, bool invert_textures_y u32 index = model_darray_len(core->models); model_darray_push(core->models, model); + + arena_free_all(&scratch); + arena_free_storage(&scratch); return (model_handle){ .raw = index }; } @@ -182,7 +186,7 @@ bool model_load_obj_str(const char *file_string, str8 relative_path, model *out_ sscanf(pch + offset, "%s", filename); char mtllib_path[1024]; snprintf(mtllib_path, sizeof(mtllib_path), "%s/%s", relative_path.buf, filename); - if (!load_material_lib(mtllib_path, out_model->materials)) { + if (!load_material_lib(mtllib_path, relative_path, out_model->materials)) { ERROR("couldnt load material lib"); return false; } @@ -262,7 +266,6 @@ void create_submesh(mesh_darray *meshes, vec3_darray *tmp_positions, vec3_darray material_darray_iter mat_iter = material_darray_iter_new(materials); blinn_phong_material *cur_material; while ((cur_material = material_darray_iter_next(&mat_iter))) { - printf("Material name %s vs %s \n", cur_material->name, current_material_name); if (strcmp(cur_material->name, current_material_name) == 0) { DEBUG("Found match"); m.material_index = mat_iter.current_idx - 1; @@ -280,7 +283,7 @@ void create_submesh(mesh_darray *meshes, vec3_darray *tmp_positions, vec3_darray mesh_darray_push(meshes, m); } -bool load_material_lib(const char *path, material_darray *materials) { +bool load_material_lib(const char *path, str8 relative_path, material_darray *materials) { TRACE("BEGIN load material lib at %s", path); const char *file_string = string_from_file(path); @@ -338,9 +341,13 @@ bool load_material_lib(const char *path, material_darray *materials) { // specular exponent sscanf(pch + offset, "%f", ¤t_material.spec_exponent); } else if (strcmp(line_header, "map_Kd") == 0) { - char diffuse_map_path[1024] = "assets/"; - sscanf(pch + offset, "%s", diffuse_map_path + 7); + char diffuse_map_filename[1024]; + sscanf(pch + offset, "%s", diffuse_map_filename); + char diffuse_map_path[1024]; + snprintf(diffuse_map_path, sizeof(diffuse_map_path), "%s/%s", relative_path.buf, + diffuse_map_filename); printf("load from %s\n", diffuse_map_path); + // -------------- texture diffuse_texture = texture_data_load(diffuse_map_path, true); current_material.diffuse_texture = diffuse_texture; @@ -348,8 +355,14 @@ bool load_material_lib(const char *path, material_darray *materials) { texture_data_upload(¤t_material.diffuse_texture); // -------------- } else if (strcmp(line_header, "map_Ks") == 0) { - char specular_map_path[1024] = "assets/"; - sscanf(pch + offset, "%s", specular_map_path + 7); + // char specular_map_path[1024] = "assets/"; + // sscanf(pch + offset, "%s", specular_map_path + 7); + char specular_map_filename[1024]; + sscanf(pch + offset, "%s", specular_map_filename); + char specular_map_path[1024]; + snprintf(specular_map_path, sizeof(specular_map_path), "%s/%s", relative_path.buf, + specular_map_filename); + printf("load from %s\n", specular_map_path); // -------------- texture specular_texture = texture_data_load(specular_map_path, true); current_material.specular_texture = specular_texture; |