diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-11 15:35:40 +1100 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-11 15:35:40 +1100 |
commit | 1c6ec0b7b8e437a003137428636149132d998357 (patch) | |
tree | ada9de786fa292cb622e66d9491834179307f392 /src | |
parent | 8019f49a4b0d657f882c148ae6582da78a7d2262 (diff) |
added a path_parent function to get path of a file
Now we can load .mtl files without hardcoding the full path from top-level like before. that was a hack!
Diffstat (limited to 'src')
-rw-r--r-- | src/platform/path.c | 15 | ||||
-rw-r--r-- | src/platform/path.h | 16 | ||||
-rw-r--r-- | src/resources/obj.c | 23 | ||||
-rw-r--r-- | src/std/str.c | 2 | ||||
-rw-r--r-- | src/std/str.h | 5 |
5 files changed, 55 insertions, 6 deletions
diff --git a/src/platform/path.c b/src/platform/path.c new file mode 100644 index 0000000..e67102b --- /dev/null +++ b/src/platform/path.c @@ -0,0 +1,15 @@ +#include "path.h" + +#include <libgen.h> +#include <string.h> +#include "str.h" + +#if defined(CEL_PLATFORM_LINUX) || defined(CEL_PLATFORM_MAC) +path_opt path_parent(const char* path) { + char* path_dirname = dirname(path); + return (path_opt){ .path = str8_cstr_view(path_dirname), .has_value = true }; +} +#endif +#ifdef CEL_PLATFORM_WINDOWS +// TODO: path_opt path_parent(const char* path) +#endif
\ No newline at end of file diff --git a/src/platform/path.h b/src/platform/path.h new file mode 100644 index 0000000..0ec6993 --- /dev/null +++ b/src/platform/path.h @@ -0,0 +1,16 @@ +/** + * @file path.h + * @brief + * @date 2024-03-11 + * @copyright Copyright (c) 2024 + */ +#pragma once + +#include "str.h" + +typedef struct path_opt { + str8 path; + bool has_value; +} path_opt; + +path_opt path_parent(const char* path); // TODO: convert to using str8
\ No newline at end of file diff --git a/src/resources/obj.c b/src/resources/obj.c index 24d5d14..031377d 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 "path.h" #include "render.h" #include "render_types.h" #include "str.h" @@ -35,10 +36,16 @@ void create_submesh(mesh_darray *meshes, vec3_darray *tmp_positions, vec3_darray 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 model_load_obj_str(const char *file_string, model *out_model, bool invert_textures_y); +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) { TRACE("Loading model at Path %s\n", path); + path_opt relative_path = path_parent(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 @@ -48,7 +55,7 @@ model_handle model_load_obj(core *core, const char *path, bool invert_textures_y model.meshes = mesh_darray_new(1); model.materials = material_darray_new(1); - bool success = model_load_obj_str(file_string, &model, invert_textures_y); + bool success = model_load_obj_str(file_string, relative_path.path, &model, invert_textures_y); if (!success) { FATAL("Couldnt load OBJ file at path %s", path); @@ -60,7 +67,8 @@ model_handle model_load_obj(core *core, const char *path, bool invert_textures_y return (model_handle){ .raw = index }; } -bool model_load_obj_str(const char *file_string, model *out_model, bool invert_textures_y) { +bool model_load_obj_str(const char *file_string, str8 relative_path, model *out_model, + bool invert_textures_y) { TRACE("Load OBJ from string"); // Setup temps @@ -170,9 +178,12 @@ bool model_load_obj_str(const char *file_string, model *out_model, bool invert_t // f.vertex_indices[2], f.uv_indices[2], f.normal_indices[2]); face_darray_push(tmp_faces, f); } else if (strcmp(line_header, "mtllib") == 0) { - char path[1024] = "assets/"; - sscanf(pch + offset, "%s", path + 7); - if (!load_material_lib(path, out_model->materials)) { + char filename[1024]; + sscanf(pch + offset, "%s", filename); + char mtllib_path[1024]; + snprintf(mtllib_path, sizeof(mtllib_path), "%s/%s", relative_path.buf, filename); + // sscanf(pch + offset, "%s", path + 7); + if (!load_material_lib(mtllib_path, out_model->materials)) { ERROR("couldnt load material lib"); return false; } diff --git a/src/std/str.c b/src/std/str.c index 1c687fa..07a8e73 100644 --- a/src/std/str.c +++ b/src/std/str.c @@ -5,6 +5,8 @@ str8 str8_create(u8* buf, size_t len) { return (str8){ .buf = buf, .len = len }; } +str8 str8_cstr_view(char* string) { return str8_create((u8*)string, strlen(string)); } + bool str8_equals(str8 a, str8 b) { if (a.len != b.len) { return false; diff --git a/src/std/str.h b/src/std/str.h index b87864e..1ebecac 100644 --- a/src/std/str.h +++ b/src/std/str.h @@ -38,6 +38,11 @@ char* str8_to_cstr(arena* a, str8 s); #define cstr(a, s) (str8_to_cstr(a, s)) // Shorthand +/** @brief Return a str8 that references a statically allocated string. + `string` therefore must already be null-terminated. + @note The backing `string` cannot be modified. */ +str8 str8_cstr_view(char* string); + // --- Comparisons /** @brief Compare two strings for exact equality */ |