X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fscenefile.c;h=92cdb1fefb464df045ef9ca45679c75c2a0f302a;hb=67b9bf8d28722bbc8c03c897c0a46d202286bd16;hp=39d9b92b70a1636000bf5283b0c0fd0feb3ec8b7;hpb=f3d32774e0c196175d8143c21313097bcc8ff3a2;p=vrlugburz diff --git a/src/scenefile.c b/src/scenefile.c index 39d9b92..92cdb1f 100644 --- a/src/scenefile.c +++ b/src/scenefile.c @@ -61,6 +61,7 @@ int load_scenefile(struct scenefile *scn, const char *fname) char *sep; struct rbtree *rbtree = 0; + memset(scn, 0, sizeof *scn); varr_size = varr_max = narr_size = narr_max = tarr_size = tarr_max = 0; varr = narr = 0; @@ -73,8 +74,7 @@ int load_scenefile(struct scenefile *scn, const char *fname) if(!(rbtree = rb_create(cmp_facevert))) { fprintf(stderr, "load_scenefile: failed to create facevertex search tree\n"); - fclose(fp); - return -1; + goto fail; } rb_set_delete_func(rbtree, free_rbnode_key, 0); @@ -87,6 +87,14 @@ int load_scenefile(struct scenefile *scn, const char *fname) path_prefix = alloca(strlen(buf) + 1); strcpy(path_prefix, buf); + if(sep) { + sep = (char*)fname + (sep - buf); + } + if(!(scn->fname = strdup(sep ? sep + 1 : fname))) { + fprintf(stderr, "failed to allocate scenefile name buffer\n"); + goto fail; + } + if(!(mesh = malloc(sizeof *mesh))) { fprintf(stderr, "failed to allocate mesh\n"); fclose(fp); @@ -162,6 +170,7 @@ int load_scenefile(struct scenefile *scn, const char *fname) goto fail; } init_mesh(mesh); + mesh->name = strdup(cleanline(line + 2)); } break; @@ -199,12 +208,16 @@ int load_scenefile(struct scenefile *scn, const char *fname) } mesh = 0; - printf("load_scenefile: loaded %d meshes, %d vertices\n", scn->num_meshes, - varr_size); + printf("load_scenefile %s: loaded %d meshes, %d vertices\n", scn->fname, + scn->num_meshes, varr_size); res = 0; + if(0) { fail: + free(scn->fname); + } + fclose(fp); free(mesh); free(varr); @@ -320,7 +333,7 @@ static int load_mtllib(struct scenefile *scn, const char *path_prefix, const cha FILE *fp; char buf[256], *line; struct objmtl om; - struct material *mtl; + struct material *mtl = 0; if(path_prefix && *path_prefix) { sprintf(buf, "%s/%s", path_prefix, mtlfname); @@ -339,14 +352,18 @@ static int load_mtllib(struct scenefile *scn, const char *path_prefix, const cha if(memcmp(line, "newmtl", 6) == 0) { if(mtl) { + conv_mtl(mtl, &om, path_prefix); mtl->next = scn->mtllist; scn->mtllist = mtl; } - if((mtl = calloc(1, sizeof *mtl))) { - if((line = cleanline(line + 6))) { - mtl->name = strdup(line); - } + mtl = calloc(1, sizeof *mtl); + + memset(&om, 0, sizeof om); + + if((line = cleanline(line + 6))) { + om.name = strdup(line); } + } else if(memcmp(line, "Kd", 2) == 0) { sscanf(line + 3, "%f %f %f", &om.kd.x, &om.kd.y, &om.kd.z); } else if(memcmp(line, "Ks", 2) == 0) { @@ -370,10 +387,10 @@ static int load_mtllib(struct scenefile *scn, const char *path_prefix, const cha om.map_alpha = strdup(line); } } - conv_mtl(mtl, &om, path_prefix); } if(mtl) { + conv_mtl(mtl, &om, path_prefix); mtl->next = scn->mtllist; scn->mtllist = mtl; } @@ -397,7 +414,7 @@ static void conv_mtl(struct material *mm, struct objmtl *om, const char *path_pr int len, prefix_len, maxlen = 0; memset(mm, 0, sizeof *mm); - mm->name = strdup(om->name); + mm->name = om->name; mm->color = om->kd; mm->spec = om->ks; mm->shininess = om->shin;