--- ray/src/common/readwfobj.c 2020/05/01 18:55:34 2.3 +++ ray/src/common/readwfobj.c 2022/01/15 02:00:21 2.9 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: readwfobj.c,v 2.3 2020/05/01 18:55:34 greg Exp $"; +static const char RCSid[] = "$Id: readwfobj.c,v 2.9 2022/01/15 02:00:21 greg Exp $"; #endif /* * readobj.c @@ -16,8 +16,6 @@ static const char RCSid[] = "$Id: readwfobj.c,v 2.3 20 #include #include "objutil.h" -typedef int VNDX[3]; /* vertex index (point,map,normal) */ - #define MAXARG 512 /* maximum # arguments in a statement */ static int lineno; /* current line number */ @@ -143,47 +141,27 @@ group_name(int ac, char **av) return(nambuf); } -/* Add a new face to scene */ +/* add a new face to scene */ static int add_face(Scene *sc, const VNDX ondx, int ac, char *av[]) { - Face *f; + VNDX vdef[4]; + VNDX *varr = vdef; + Face *f = NULL; int i; - if (ac < 3) + if (ac < 3) /* legal polygon? */ return(0); - f = (Face *)emalloc(sizeof(Face)+sizeof(VertEnt)*(ac-3)); - f->flags = 0; - f->nv = ac; - f->grp = sc->lastgrp; - f->mat = sc->lastmat; - for (i = 0; i < ac; i++) { /* add each vertex */ - VNDX vin; - int j; - if (!cvtndx(vin, sc, ondx, av[i])) { - efree((char *)f); - return(0); - } - f->v[i].vid = vin[0]; - f->v[i].tid = vin[1]; - f->v[i].nid = vin[2]; - f->v[i].fnext = NULL; - for (j = i; j-- > 0; ) - if (f->v[j].vid == vin[0]) - break; - if (j < 0) { /* first occurrence? */ - f->v[i].fnext = sc->vert[vin[0]].vflist; - sc->vert[vin[0]].vflist = f; - } else if (ac == 3) /* degenerate triangle? */ - f->flags |= FACE_DEGENERATE; - } - f->next = sc->flist; /* push onto face list */ - sc->flist = f; - sc->nfaces++; - /* check face area */ - if (!(f->flags & FACE_DEGENERATE) && faceArea(sc, f, NULL) <= FTINY) - f->flags |= FACE_DEGENERATE; - return(1); + if (ac > 4) /* need to allocate array? */ + varr = (VNDX *)emalloc(ac*sizeof(VNDX)); + for (i = ac; i--; ) /* index each vertex */ + if (!cvtndx(varr[i], sc, ondx, av[i])) + break; + if (i < 0) /* create face if indices OK */ + f = addFace(sc, varr, ac); + if (varr != vdef) + efree(varr); + return(f != NULL); } /* Load a .OBJ file */ @@ -193,7 +171,7 @@ loadOBJ(Scene *sc, const char *fspec) FILE *fp; char *argv[MAXARG]; int argc; - char buf[256]; + char buf[1024]; int nstats=0, nunknown=0; int onfaces; VNDX ondx; @@ -298,13 +276,23 @@ loadOBJ(Scene *sc, const char *fspec) fprintf(stderr, " %8d statements\r", nstats); } #if POPEN_SUPPORT - if (fspec[0] == '!') - pclose(fp); - else + if (fspec[0] == '!') { + if (pclose(fp) != 0) { + sprintf(errmsg, "Bad return status from: %s", fspec+1); + error(USER, errmsg); + freeScene(sc); + return(NULL); + } + } else #endif if (fp != stdin) fclose(fp); - sprintf(buf, "%d statements read from \"%s\"", nstats, fspec); + if (verbose) + fprintf(stderr, "Read %d statements\n", nstats); + if (strlen(fspec) < sizeof(buf)-32) + sprintf(buf, "%d statements read from \"%s\"", nstats, fspec); + else + sprintf(buf, "%d statements read from (TOO LONG TO SHOW)", nstats); addComment(sc, buf); if (nunknown) { sprintf(buf, "\t%d unrecognized", nunknown);