diff options
Diffstat (limited to 'obj.c')
-rw-r--r-- | obj.c | 41 |
1 files changed, 27 insertions, 14 deletions
@@ -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) |