From: John Tsiombikas Date: Fri, 1 Mar 2019 13:44:40 +0000 (+0200) Subject: foo X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=commitdiff_plain;h=e50e34cb3791268a6665e3970ed76d2ad0d7aed2;p=vrtris foo --- diff --git a/src/cmesh.c b/src/cmesh.c index 993fa2c..e40e4f4 100644 --- a/src/cmesh.c +++ b/src/cmesh.c @@ -1296,9 +1296,14 @@ int cmesh_dump_obj(struct cmesh *cm, const char *fname) return -1; } +#define HAS_VN 1 +#define HAS_VT 2 + int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs) { + static const char *fmtstr[] = {" %u", " %u//%u", " %u/%u", " %u/%u/%u"}; int i, j, num, nelem; + unsigned int aflags = 0; if(!cmesh_has_attrib(cm, CMESH_ATTR_VERTEX)) { return -1; @@ -1315,6 +1320,7 @@ int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs) } if(cmesh_has_attrib(cm, CMESH_ATTR_NORMAL)) { + aflags |= HAS_VN; nelem = cm->vattr[CMESH_ATTR_NORMAL].nelem; if((num = dynarr_size(cm->vattr[CMESH_ATTR_NORMAL].data)) != cm->nverts * nelem) { warning_log("normal array size (%d) != nverts (%d)\n", num, cm->nverts); @@ -1326,6 +1332,7 @@ int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs) } if(cmesh_has_attrib(cm, CMESH_ATTR_TEXCOORD)) { + aflags |= HAS_VT; nelem = cm->vattr[CMESH_ATTR_TEXCOORD].nelem; if((num = dynarr_size(cm->vattr[CMESH_ATTR_TEXCOORD].data)) != cm->nverts * nelem) { warning_log("texcoord array size (%d) != nverts (%d)\n", num, cm->nverts); @@ -1346,7 +1353,7 @@ int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs) fputc('f', fp); for(j=0; j<3; j++) { unsigned int idx = *idxptr++ + 1 + voffs; - fprintf(fp, " %u/%u/%u", idx, idx, idx); + fprintf(fp, fmtstr[aflags], idx, idx, idx); } fputc('\n', fp); } @@ -1356,7 +1363,7 @@ int cmesh_dump_obj_file(struct cmesh *cm, FILE *fp, int voffs) for(i=0; i 3) found_color = 1; switch(num) { case 3: v.r = 1.0f; @@ -141,6 +143,7 @@ int cmesh_load(struct cmesh *mesh, const char *fname) if((node = rb_find(rbtree, &fv))) { unsigned int idx = (unsigned int)node->data; + assert(idx < cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX)); if(cmesh_push_index(mesh, idx) == -1) { fprintf(stderr, "load_mesh: failed to resize index array\n"); goto err; @@ -149,26 +152,33 @@ int cmesh_load(struct cmesh *mesh, const char *fname) unsigned int newidx = cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX); struct facevertex *newfv; struct vertex_pos_color *vptr = varr + fv.vidx; - float tu, tv; if(cmesh_push_attrib3f(mesh, CMESH_ATTR_VERTEX, vptr->x, vptr->y, vptr->z) == -1) { fprintf(stderr, "load_mesh: failed to resize vertex array\n"); goto err; } - if(cmesh_push_attrib(mesh, CMESH_ATTR_COLOR, &vptr->r) == -1) { - fprintf(stderr, "load_mesh: failed to resize color array\n"); - goto err; + if(found_color) { + if(cmesh_push_attrib(mesh, CMESH_ATTR_COLOR, &vptr->r) == -1) { + fprintf(stderr, "load_mesh: failed to resize color array\n"); + goto err; + } } - if(fv.tidx >= 0) { - tu = tarr[fv.tidx].x; - tv = tarr[fv.tidx].y; - } else { - tu = vptr->x; - tv = vptr->y; + if(fv.nidx >= 0) { + float nx = narr[fv.nidx].x; + float ny = narr[fv.nidx].y; + float nz = narr[fv.nidx].z; + if(cmesh_push_attrib3f(mesh, CMESH_ATTR_NORMAL, nx, ny, nz) == -1) { + fprintf(stderr, "load_mesh: failed to resize normal array\n"); + goto err; + } } - if(cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, tu, tv) == -1) { - fprintf(stderr, "load_mesh: failed to resize texcoord array\n"); - goto err; + if(fv.tidx >= 0) { + float tu = tarr[fv.tidx].x; + float tv = tarr[fv.tidx].y; + if(cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, tu, tv) == -1) { + fprintf(stderr, "load_mesh: failed to resize texcoord array\n"); + goto err; + } } if((newfv = malloc(sizeof *newfv))) {