From 4f008bf699888b51f7888cbbc24cab711483ac4a Mon Sep 17 00:00:00 2001 From: Kyle K Date: Sat, 16 Oct 2010 14:16:51 -0500 Subject: Make a helper func for extracting path --- obj.c | 41 +++++++++++++++++++++++++++-------------- obj.h | 1 + objloader.c | 8 ++------ 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/obj.c b/obj.c index 9d4f62c..62ff7b1 100644 --- a/obj.c +++ b/obj.c @@ -18,6 +18,7 @@ ObjModel* ObjLoadModel(char *memory, size_t size) { char *p = NULL, *e = NULL; ObjModel *ret = (ObjModel *) calloc(1, sizeof(ObjModel)); + /* initialize to zero */ memset(ret, 0, sizeof(ObjModel)); p = memory; @@ -133,12 +134,13 @@ ObjModel* ObjLoadModel(char *memory, size_t size) if (ret->mtllib != NULL && ret->usemtl) { /* append directory to filename */ - char *fname = (char *) malloc(sizeof(char) * 31); + //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 *delimeter = strrchr(obj_last_fname, '/'); + //unsigned int offset = delimeter - obj_last_fname; + //strncpy(fname, obj_last_fname, offset + 1); + //strcat(fname, ret->mtllib); + char *fname = ObjGetPath(ret->mtllib); char *mtl_mem = NULL; size_t mtl_bytes = ObjLoadFile(fname, &mtl_mem); @@ -146,17 +148,14 @@ ObjModel* ObjLoadModel(char *memory, size_t size) if (mtl_bytes) { ObjMtl *mtl = (ObjMtl *) calloc(1, sizeof(ObjMtl)); + /* initialize to zero */ memset(mtl, 0, sizeof(ObjMtl)); - /* allocate space for members */ + /* allocate space for dynamic members */ mtl->map_Ka = (char *) malloc(sizeof(char) * 21); mtl->map_Kd = (char *) malloc(sizeof(char) * 21); - /* reset, what about the rest? */ - mtl->Ns = 0.0f; - mtl->Ni = 0.0f; - mtl->d = 0.0f; - mtl->Tr = 0.0f; - mtl->illum = 0; + *mtl->map_Ka = '\0'; + *mtl->map_Kd = '\0'; char *st = mtl_mem; char *ed = st + mtl_bytes; @@ -255,7 +254,7 @@ void ObjList(ObjModel *model) for (i = 0; i < model->nTexCoord; i++) printf("v %f %f\n", model->TexCoordArray[i].u, model->TexCoordArray[i].v); - printf("%d faces\n", model->nFace); + printf("%d faces\n", model->nFace); for (i = 0; i < model->nFace; i++) { printf("f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d\n", @@ -271,7 +270,21 @@ void ObjList(ObjModel *model) model->FaceArray[i].Vertex[3], model->FaceArray[i].TexCoord[3], model->FaceArray[i].Normal[3]); - } + } +} + +char *ObjGetPath(char *fname) +{ + if (obj_last_fname == NULL) + return NULL; + + char *path = (char *) malloc(sizeof(char) * 31); + char *delimeter = strrchr(obj_last_fname, '/'); + unsigned int offset = delimeter - obj_last_fname; + strncpy(path, obj_last_fname, offset + 1); + strcat(path, fname); + + return path; } void ObjFree(ObjModel *model) diff --git a/obj.h b/obj.h index 8182129..fd2c13a 100644 --- a/obj.h +++ b/obj.h @@ -58,6 +58,7 @@ ObjModel *ObjLoadModel(char *, size_t); /* read a file into argument 2, and return amount of bytes read */ size_t ObjLoadFile(char *, char **); void ObjList(ObjModel *); +char *ObjGetPath(char *); void ObjFree(ObjModel *); #endif diff --git a/objloader.c b/objloader.c index 1374530..1f5eddd 100644 --- a/objloader.c +++ b/objloader.c @@ -78,12 +78,8 @@ static void setup_opengl(ObjModel *model) GLint iWidth, iHeight, iComponents; GLenum eFormat; - /* get the filename from obj file */ - char *fname = (char *) malloc(sizeof(char) * 31); - char *delimeter = strrchr(obj_last_fname, '/'); - unsigned int offset = delimeter - obj_last_fname; - strncpy(fname, obj_last_fname, offset + 1); - strcat(fname, model->mtl->map_Ka); /* ambient texture map */ + /* get the texture filename from obj */ + char *fname = ObjGetPath(model->mtl->map_Ka); /* ambient texture map */ if (model->mtl != NULL) printf("ambient texture map: %s\n", model->mtl->map_Ka); -- cgit v1.2.3