From 1c6ec0b7b8e437a003137428636149132d998357 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Mon, 11 Mar 2024 15:35:40 +1100 Subject: 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! --- src/platform/path.c | 15 +++++++++++++++ src/platform/path.h | 16 ++++++++++++++++ src/resources/obj.c | 23 +++++++++++++++++------ src/std/str.c | 2 ++ src/std/str.h | 5 +++++ 5 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 src/platform/path.c create mode 100644 src/platform/path.h (limited to 'src') 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 +#include +#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 */ -- cgit v1.2.3-70-g09d2 From 25fc6a03a3c78c755e8b00c481c7d71da6e145c4 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Mon, 11 Mar 2024 15:40:25 +1100 Subject: minor cleanup --- src/resources/obj.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src') diff --git a/src/resources/obj.c b/src/resources/obj.c index 031377d..658967e 100644 --- a/src/resources/obj.c +++ b/src/resources/obj.c @@ -51,7 +51,7 @@ model_handle model_load_obj(core *core, const char *path, bool invert_textures_y // TODO: store the relative path without the name.obj at the end model model = { 0 }; - model.name = str8lit(path); + model.name = str8_cstr_view(path); model.meshes = mesh_darray_new(1); model.materials = material_darray_new(1); @@ -182,7 +182,6 @@ 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); - // sscanf(pch + offset, "%s", path + 7); if (!load_material_lib(mtllib_path, out_model->materials)) { ERROR("couldnt load material lib"); return false; -- cgit v1.2.3-70-g09d2