diff options
Diffstat (limited to 'obj.c')
-rw-r--r-- | obj.c | 15 |
1 files changed, 10 insertions, 5 deletions
@@ -1,7 +1,7 @@ /* obj.c * * OBJ Loader - * inital code: http://www.gamedev.net/community/forums/topic.asp?topic_id=312335 + * initial code: http://www.gamedev.net/community/forums/topic.asp?topic_id=312335 * * notes: the mtl file should be implemented using arrays * more that one mtl needs to be supported some day @@ -133,8 +133,12 @@ ObjModel* ObjLoadModel(char *memory, size_t size) if (ret->mtllib != NULL && ret->usemtl) { /* append directory to filename */ - char *fname = (char *) malloc(sizeof(char) * 21); - sprintf(fname, "./cube/%s", ret->mtllib); + char *fname = (char *) malloc(sizeof(char) * 31); + /* sprintf(fname, "./cube/%s", ret->mtllib); */ + char *delimeter = strrchr(obj_last_fname, '/'); + unsigned int offset = delimeter - obj_last_fname; + strncpy(fname, obj_last_fname, offset + 1); + strcat(fname, ret->mtllib); char *mtl_mem = NULL; size_t mtl_bytes = ObjLoadFile(fname, &mtl_mem); @@ -147,7 +151,7 @@ ObjModel* ObjLoadModel(char *memory, size_t size) /* allocate space for members */ mtl->map_Ka = (char *) malloc(sizeof(char) * 21); mtl->map_Kd = (char *) malloc(sizeof(char) * 21); - /* set, what about rest? */ + /* reset, what about the rest? */ mtl->Ns = 0.0f; mtl->Ni = 0.0f; mtl->d = 0.0f; @@ -198,7 +202,7 @@ ObjModel* ObjLoadModel(char *memory, size_t size) if (memcmp(st, "map_Kd", 6) == 0) sscanf(st, "map_Kd %s", mtl->map_Kd); - /* got to next line */ + /* go to next line */ while (*st++ != (char) 0x0a); } /* set the mtl */ @@ -215,6 +219,7 @@ ObjModel* ObjLoadModel(char *memory, size_t size) size_t ObjLoadFile(char *szFileName, char **memory) { + strcpy(obj_last_fname, szFileName); /* seems useful for tracking */ size_t bytes = 0; FILE *file = fopen(szFileName, "rt"); |