From 09f87160bb80d1ad331c697000ecd7a122f9ef60 Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Fri, 19 Aug 2022 18:06:04 +0200 Subject: [PATCH] Rewritten OBJ export to include outpath --- obj.c | 43 +++++++++++++++++++++++++++++++++---------- obj.h | 23 +++++++++++++---------- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/obj.c b/obj.c index 0597bb8..85dbc9c 100644 --- a/obj.c +++ b/obj.c @@ -1,3 +1,6 @@ +/* MIT License - 2022 JackCarterSmith */ +/* Modified and cleaned version - 19/08/2022 */ +/* */ /* Copyright (c) 2005,2013,2014 Robert Kooima */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining a */ @@ -26,11 +29,7 @@ #include #ifndef CONF_NO_GL -#ifdef __APPLE__ -# include -#else -# include -#endif +#include #endif #define MAXSTR 1024 @@ -399,12 +398,12 @@ static void read_image(obj *O, int mi, int ki, const char *line, char pathname[MAXSTR]; - char map[MAXSTR]; + char map[MAXSTR - 1]; char val[MAXSTR]; const char *end; - memset(map, 0, MAXSTR); + memset(map, 0, MAXSTR - 1); memset(val, 0, MAXSTR); while (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') @@ -2437,12 +2436,36 @@ static void obj_write_obj(const obj *O, const char *obj, } } -void obj_write(const obj *O, const char *obj, const char *mtl, int prec) +void obj_write(const obj *O, const char *obj, const char *mtl, const char *outPath, int prec) { + char objPath[MAXSTR]; + char mtlPath[MAXSTR]; + +#ifdef _WIN32 +#define EXPORT_FORMAT "%s\\%s" +#else +#define EXPORT_FORMAT "%s/%s" +#endif + assert(O); - if (obj) obj_write_obj(O, obj, mtl, prec); - if (mtl) obj_write_mtl(O, mtl); + if (obj) { + if (outPath != NULL) + sprintf_s(objPath, MAXSTR, EXPORT_FORMAT, outPath, obj); + else + sprintf_s(objPath, MAXSTR, "%s", obj); + + obj_write_obj(O, objPath, mtl, prec); + } + + if (mtl) { + if (outPath != NULL) + sprintf_s(mtlPath, MAXSTR, EXPORT_FORMAT, outPath, mtl); + else + sprintf_s(mtlPath, MAXSTR, "%s", mtl); + + obj_write_mtl(O, mtlPath); + } } /*============================================================================*/ diff --git a/obj.h b/obj.h index d2da08c..c5f6c62 100644 --- a/obj.h +++ b/obj.h @@ -1,3 +1,6 @@ +/* MIT License - 2022 JackCarterSmith */ +/* Modified and cleaned version - 19/08/2022 */ +/* */ /* Copyright (c) 2005 Robert Kooima */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining a */ @@ -72,17 +75,17 @@ void obj_del_surf(obj *, int); void obj_set_mtrl_name(obj *, int, const char *); void obj_set_mtrl_map (obj *, int, int, const char *); void obj_set_mtrl_opt (obj *, int, int, unsigned int); -void obj_set_mtrl_c (obj *, int, int, const float *); -void obj_set_mtrl_o (obj *, int, int, const float *); -void obj_set_mtrl_s (obj *, int, int, const float *); +void obj_set_mtrl_c (obj *, int, int, const float[4]); +void obj_set_mtrl_o (obj *, int, int, const float[3]); +void obj_set_mtrl_s (obj *, int, int, const float[3]); -void obj_set_vert_v(obj *, int, const float *); -void obj_set_vert_t(obj *, int, const float *); -void obj_set_vert_n(obj *, int, const float *); -void obj_set_vert_u(obj *, int, const float *); +void obj_set_vert_v(obj *, int, const float[3]); +void obj_set_vert_t(obj *, int, const float[2]); +void obj_set_vert_n(obj *, int, const float[3]); +void obj_set_vert_u(obj *, int, const float[3]); -void obj_set_poly(obj *, int, int, const int *); -void obj_set_line(obj *, int, int, const int *); +void obj_set_poly(obj *, int, int, const int[3]); +void obj_set_line(obj *, int, int, const int[2]); void obj_set_surf(obj *, int, int); void obj_set_vert_loc(obj *, int, int, int, int); @@ -123,7 +126,7 @@ void obj_sort(obj *, int); float obj_acmr(obj *, int); void obj_bound(const obj *, float *); -void obj_write(const obj *, const char *, const char *, int); +void obj_write(const obj *, const char *, const char *, const char *, int); /*======================================================================+=====*/