From 6f1c5f46ac61abf785e25746e2e7e02d6c4b535e Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Thu, 28 Jul 2022 17:19:31 +0200 Subject: [PATCH] mtllib path patch --- src/Model-Extractor.c | 12 +++++++--- src/obj_exporter.c | 56 ++++++++++++++++++++++++++++++++++++++++--- src/options.h | 3 ++- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/Model-Extractor.c b/src/Model-Extractor.c index 078e2ab..2c70ea8 100644 --- a/src/Model-Extractor.c +++ b/src/Model-Extractor.c @@ -104,7 +104,7 @@ static unsigned int mainProcess(int args_cnt, char *args_value[]) { } static int checkInputArgs(int arg_nbr, char *args[]) { - int _o = 0x0002; // Default options parameters + int _o = (OUTPUT_DIR | EXPORT_MTL); // Default options parameters char test[256]; int i; @@ -120,7 +120,10 @@ static int checkInputArgs(int arg_nbr, char *args[]) { printf("[OPTN] Verbose enabled.\n"); } else if (strcmp(args[i], "-no-subdir") == 0) { _o &= ~OUTPUT_DIR; - printf("[OPTN] Extract to current directory.\n"); + printf("[OPTN] Export to current directory.\n"); + } else if (strcmp(args[i], "-mtl") == 0) { + _o &= ~EXPORT_MTL; + printf("[OPTN] No materials datas.\n"); } else { printf("[ERR] Unknown option: %s\n", args[i]); } @@ -163,7 +166,10 @@ static void cleanUpMemory(T_HOB* hobStruct) { static inline void dispHelp() { printf("\n"); - printf("Options:\n -h Print this message\n -v Activate verbose console output\n -no-subdir Extract textures inside current folder\n"); + printf("Options:\n -h Print this message\n"); + printf(" -v Activate verbose console output\n"); + printf(" -no-subdir Export models inside current folder\n"); + printf(" -no-mtl Disable materials datas export with model\n"); printf("\n"); printf("Usage: RSE-Model_%s [options] \n", VERSION); printf("\n"); diff --git a/src/obj_exporter.c b/src/obj_exporter.c index 6a88f6b..4430bcd 100644 --- a/src/obj_exporter.c +++ b/src/obj_exporter.c @@ -15,6 +15,8 @@ #include "obj_exporter.h" +static void mtlPathPatch(const char* out_file, const char* obj_name) ; + unsigned char exportOBJModel(T_HOB_OBJECT* hob_objects, const char *out_path) { char objExport_path[128]; char mtlExport_path[128]; @@ -40,8 +42,8 @@ unsigned char exportOBJModel(T_HOB_OBJECT* hob_objects, const char *out_path) { strcpy(objExport_path, hob_objects->name); } strcpy(mtlExport_path, objExport_path); - strcat(objExport_path, ".obj"); - strcat(mtlExport_path, ".mtl"); + strcat(objExport_path, ".obj\0"); + strcat(mtlExport_path, ".mtl\0"); objConstruct = obj_create(NULL); @@ -90,8 +92,56 @@ unsigned char exportOBJModel(T_HOB_OBJECT* hob_objects, const char *out_path) { indexOffset = obj_num_vert(objConstruct); } - obj_write(objConstruct, objExport_path, NULL, 8); + if (_options & EXPORT_MTL) { + obj_write(objConstruct, objExport_path, mtlExport_path, 8); + if (_options & OUTPUT_DIR) mtlPathPatch(objExport_path, hob_objects->name); + } else obj_write(objConstruct, objExport_path, NULL, 8); + obj_delete(objConstruct); return NO_ERROR; } + +static void mtlPathPatch(const char* out_file, const char* obj_name) { + FILE* obj = NULL; + char* memFile = NULL; + long fileSize,i,pos = 0,lines; + char _path[128],b; + + obj = fopen(out_file, "r"); + if ( obj != NULL ) { + fseek(obj, 0, SEEK_END); + fileSize = ftell(obj); + fseek(obj, 0, SEEK_SET); + + // Find the end of first line + for ( i = 0; i < fileSize + 1; i++) { + b = (char)fgetc(obj); + if (b == '\n') { + if (pos == 0) pos = i; + lines++; + } + } + + // Prepare mtl path for output + strcpy(_path, obj_name); + strcat(_path, ".mtl"); + + memFile = malloc(fileSize - (pos + lines)); + if ( memFile != NULL ) { + // Read the rest of file in memory + fseek(obj, pos, SEEK_SET); + fread(memFile, fileSize - (pos + lines), 1, obj); + fclose(obj); + + // Begin rewrite file + obj = fopen(out_file, "w"); + fprintf(obj, "mtllib %s", _path); + fwrite(memFile, fileSize - (pos + lines), 1, obj); + + free(memFile); + } + + fclose(obj); + } +} diff --git a/src/options.h b/src/options.h index 904c2d3..392e9a2 100644 --- a/src/options.h +++ b/src/options.h @@ -2,7 +2,8 @@ #define OPTIONS_H_ #define VERBOSE_ENABLED 0x0001 -#define OUTPUT_DIR 0x0002 +#define OUTPUT_DIR 0x0002 +#define EXPORT_MTL 0x0004 extern int _options;