mtllib path patch

This commit is contained in:
JackCarterSmith 2022-07-28 17:19:31 +02:00
parent ea59163941
commit 6f1c5f46ac
Signed by: JackCarterSmith
GPG Key ID: 832E52F4E23F8F24
3 changed files with 64 additions and 7 deletions

View File

@ -104,7 +104,7 @@ static unsigned int mainProcess(int args_cnt, char *args_value[]) {
} }
static int checkInputArgs(int arg_nbr, char *args[]) { 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]; char test[256];
int i; int i;
@ -120,7 +120,10 @@ static int checkInputArgs(int arg_nbr, char *args[]) {
printf("[OPTN] Verbose enabled.\n"); printf("[OPTN] Verbose enabled.\n");
} else if (strcmp(args[i], "-no-subdir") == 0) { } else if (strcmp(args[i], "-no-subdir") == 0) {
_o &= ~OUTPUT_DIR; _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 { } else {
printf("[ERR] Unknown option: %s\n", args[i]); printf("[ERR] Unknown option: %s\n", args[i]);
} }
@ -163,7 +166,10 @@ static void cleanUpMemory(T_HOB* hobStruct) {
static inline void dispHelp() { static inline void dispHelp() {
printf("\n"); 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("\n");
printf("Usage: RSE-Model_%s [options] <hob files...>\n", VERSION); printf("Usage: RSE-Model_%s [options] <hob files...>\n", VERSION);
printf("\n"); printf("\n");

View File

@ -15,6 +15,8 @@
#include "obj_exporter.h" #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) { unsigned char exportOBJModel(T_HOB_OBJECT* hob_objects, const char *out_path) {
char objExport_path[128]; char objExport_path[128];
char mtlExport_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(objExport_path, hob_objects->name);
} }
strcpy(mtlExport_path, objExport_path); strcpy(mtlExport_path, objExport_path);
strcat(objExport_path, ".obj"); strcat(objExport_path, ".obj\0");
strcat(mtlExport_path, ".mtl"); strcat(mtlExport_path, ".mtl\0");
objConstruct = obj_create(NULL); 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); 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); obj_delete(objConstruct);
return NO_ERROR; 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);
}
}

View File

@ -3,6 +3,7 @@
#define VERBOSE_ENABLED 0x0001 #define VERBOSE_ENABLED 0x0001
#define OUTPUT_DIR 0x0002 #define OUTPUT_DIR 0x0002
#define EXPORT_MTL 0x0004
extern int _options; extern int _options;