| 9 |  | #include "copyright.h" | 
| 10 |  | #include "standard.h" | 
| 11 |  | #include "cvmesh.h" | 
| 12 | + | #include "triangulate.h" | 
| 13 |  | #include <ctype.h> | 
| 14 |  |  | 
| 15 |  | typedef int     VNDX[3];        /* vertex index (point,map,normal) */ | 
| 16 |  |  | 
| 17 |  | #define CHUNKSIZ        1024    /* vertex allocation chunk size */ | 
| 18 |  |  | 
| 19 | < | #define MAXARG          64      /* maximum # arguments in a statement */ | 
| 19 | > | #define MAXARG          512     /* maximum # arguments in a statement */ | 
| 20 |  |  | 
| 21 |  | static FVECT    *vlist;         /* our vertex list */ | 
| 22 |  | static int      nvs;            /* number of vertices in our list */ | 
| 27 |  |  | 
| 28 |  | static char     *inpfile;       /* input file name */ | 
| 29 |  | static int      havemats;       /* materials available? */ | 
| 30 | < | static char     material[64];   /* current material name */ | 
| 31 | < | static char     group[64];      /* current group name */ | 
| 30 | > | static char     material[256];  /* current material name */ | 
| 31 | > | static char     group[256];     /* current group name */ | 
| 32 |  | static int      lineno;         /* current line number */ | 
| 33 |  | static int      faceno;         /* current face number */ | 
| 34 |  |  | 
| 35 | < | static int      getstmt(); | 
| 36 | < | static int      cvtndx(); | 
| 37 | < | static OBJECT   getmod(); | 
| 38 | < | static int      putface(); | 
| 39 | < | static int      puttri(); | 
| 40 | < | static void     freeverts(); | 
| 41 | < | static int      newv(); | 
| 42 | < | static int      newvn(); | 
| 43 | < | static int      newvt(); | 
| 44 | < | static void     syntax(); | 
| 35 | > | static int getstmt(char *av[MAXARG], FILE       *fp); | 
| 36 | > | static int cvtndx(VNDX  vi, char        *vs); | 
| 37 | > | static int putface(int  ac, char        **av); | 
| 38 | > | static OBJECT getmod(void); | 
| 39 | > | static int puttri(char  *v1, char       *v2, char       *v3); | 
| 40 | > | static void freeverts(void); | 
| 41 | > | static int newv(double  x, double       y, double       z); | 
| 42 | > | static int newvn(double x, double       y, double       z); | 
| 43 | > | static int newvt(double x, double       y); | 
| 44 | > | static void syntax(char *er); | 
| 45 |  |  | 
| 46 |  |  | 
| 47 |  | void | 
| 48 | < | wfreadobj(objfn)                /* read in .OBJ file and convert */ | 
| 49 | < | char    *objfn; | 
| 48 | > | wfreadobj(              /* read in .OBJ file and convert */ | 
| 49 | > | char    *objfn | 
| 50 | > | ) | 
| 51 |  | { | 
| 52 |  | FILE    *fp; | 
| 53 |  | char    *argv[MAXARG]; | 
| 54 |  | int     argc; | 
| 55 |  | int     nstats, nunknown; | 
| 54 | – | int     i; | 
| 56 |  |  | 
| 57 |  | if (objfn == NULL) { | 
| 58 |  | inpfile = "<stdin>"; | 
| 158 |  |  | 
| 159 |  |  | 
| 160 |  | static int | 
| 161 | < | getstmt(av, fp)                         /* read the next statement from fp */ | 
| 162 | < | register char   *av[MAXARG]; | 
| 163 | < | FILE    *fp; | 
| 161 | > | getstmt(                                /* read the next statement from fp */ | 
| 162 | > | char    *av[MAXARG], | 
| 163 | > | FILE    *fp | 
| 164 | > | ) | 
| 165 |  | { | 
| 166 |  | static char     sbuf[MAXARG*16]; | 
| 167 | < | register char   *cp; | 
| 168 | < | register int    i; | 
| 167 | > | char    *cp; | 
| 168 | > | int     i; | 
| 169 |  |  | 
| 170 |  | do { | 
| 171 |  | if (fgetline(cp=sbuf, sizeof(sbuf), fp) == NULL) | 
| 177 |  | lineno++; | 
| 178 |  | *cp++ = '\0'; | 
| 179 |  | } | 
| 180 | < | if (!*cp || i >= MAXARG-1) | 
| 180 | > | if (!*cp) | 
| 181 |  | break; | 
| 182 | + | if (i >= MAXARG-1) { | 
| 183 | + | sprintf(errmsg, | 
| 184 | + | "%s: too many arguments near line %d (limit %d)\n", | 
| 185 | + | inpfile, lineno+1, MAXARG-1); | 
| 186 | + | break; | 
| 187 | + | } | 
| 188 |  | av[i++] = cp; | 
| 189 |  | while (*++cp && !isspace(*cp)) | 
| 190 |  | ; | 
| 198 |  |  | 
| 199 |  |  | 
| 200 |  | static int | 
| 201 | < | cvtndx(vi, vs)                          /* convert vertex string to index */ | 
| 202 | < | register VNDX   vi; | 
| 203 | < | register char   *vs; | 
| 201 | > | cvtndx(                         /* convert vertex string to index */ | 
| 202 | > | VNDX    vi, | 
| 203 | > | char    *vs | 
| 204 | > | ) | 
| 205 |  | { | 
| 206 |  | /* get point */ | 
| 207 |  | vi[0] = atoi(vs); | 
| 245 |  | return(1); | 
| 246 |  | } | 
| 247 |  |  | 
| 248 | + | /* determine dominant axis for triangle */ | 
| 249 | + | static int | 
| 250 | + | dominant_axis(char *v1, char *v2, char *v3) | 
| 251 | + | { | 
| 252 | + | VNDX    v1i, v2i, v3i; | 
| 253 | + | FVECT   e1, e2, vn; | 
| 254 | + | int     i, imax; | 
| 255 |  |  | 
| 256 | + | if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) | 
| 257 | + | return(-1); | 
| 258 | + | VSUB(e1, vlist[v2i[0]], vlist[v1i[0]]); | 
| 259 | + | VSUB(e2, vlist[v3i[0]], vlist[v2i[0]]); | 
| 260 | + | VCROSS(vn, e1, e2); | 
| 261 | + | for (i = imax = 2; i--; ) | 
| 262 | + | if (vn[i]*vn[i] > vn[imax]*vn[imax]) | 
| 263 | + | imax = i; | 
| 264 | + | return(vn[imax]*vn[imax] > FTINY ? imax : -1); | 
| 265 | + | } | 
| 266 | + |  | 
| 267 | + | /* callback for triangle output from polygon */ | 
| 268 |  | static int | 
| 269 | < | putface(ac, av)                         /* put out an N-sided polygon */ | 
| 242 | < | int     ac; | 
| 243 | < | register char   **av; | 
| 269 | > | tri_out(const Vert2_list *tp, int a, int b, int c) | 
| 270 |  | { | 
| 271 | < | char            *cp; | 
| 272 | < | register int    i; | 
| 271 | > | return( puttri( ((char **)tp->p)[a], | 
| 272 | > | ((char **)tp->p)[b], | 
| 273 | > | ((char **)tp->p)[c] ) ); | 
| 274 | > | } | 
| 275 |  |  | 
| 276 | < | while (ac > 3) {                /* break into triangles */ | 
| 277 | < | if (!puttri(av[0], av[1], av[2])) | 
| 276 | > | static int | 
| 277 | > | putface(                                /* put out an N-sided polygon */ | 
| 278 | > | int     ac, | 
| 279 | > | char    **av | 
| 280 | > | ) | 
| 281 | > | { | 
| 282 | > | Vert2_list      *poly = polyAlloc(ac); | 
| 283 | > | int             i, ax, ay; | 
| 284 | > |  | 
| 285 | > | if (poly == NULL) | 
| 286 | > | return(0); | 
| 287 | > | poly->p = (void *)av; | 
| 288 | > | for (i = ac-3; i >= 0; i--)     /* identify dominant axis */ | 
| 289 | > | if ((ax = dominant_axis(av[i], av[i+1], av[i+2])) >= 0) | 
| 290 | > | break; | 
| 291 | > | if (ax < 0) | 
| 292 | > | return(1);              /* ignore degenerate face */ | 
| 293 | > | if (++ax >= 3) ax = 0; | 
| 294 | > | ay = ax; | 
| 295 | > | if (++ay >= 3) ay = 0; | 
| 296 | > | for (i = 0; i < ac; i++) {      /* convert to 2-D polygon */ | 
| 297 | > | VNDX    vi; | 
| 298 | > | if (!cvtndx(vi, av[i])) { | 
| 299 | > | error(WARNING, "bad vertex reference"); | 
| 300 | > | polyFree(poly); | 
| 301 |  | return(0); | 
| 302 | < | ac--;                   /* remove vertex & rotate */ | 
| 303 | < | cp = av[0]; | 
| 304 | < | for (i = 0; i < ac-1; i++) | 
| 254 | < | av[i] = av[i+2]; | 
| 255 | < | av[i] = cp; | 
| 302 | > | } | 
| 303 | > | poly->v[i].mX = vlist[vi[0]][ax]; | 
| 304 | > | poly->v[i].mY = vlist[vi[0]][ay]; | 
| 305 |  | } | 
| 306 | < | return(puttri(av[0], av[1], av[2])); | 
| 306 | > | /* break into triangles & output */ | 
| 307 | > | if (!polyTriangulate(poly, &tri_out)) | 
| 308 | > | error(WARNING, "self-intersecting face"); | 
| 309 | > | polyFree(poly); | 
| 310 | > | return(1); | 
| 311 |  | } | 
| 312 |  |  | 
| 313 |  |  | 
| 314 |  | static OBJECT | 
| 315 | < | getmod()                                /* get current modifier ID */ | 
| 315 | > | getmod(void)                            /* get current modifier ID */ | 
| 316 |  | { | 
| 317 |  | char    *mnam; | 
| 318 |  | OBJECT  mod; | 
| 338 |  |  | 
| 339 |  |  | 
| 340 |  | static int | 
| 341 | < | puttri(v1, v2, v3)                      /* convert a triangle */ | 
| 342 | < | char    *v1, *v2, *v3; | 
| 341 | > | puttri(                 /* convert a triangle */ | 
| 342 | > | char    *v1, | 
| 343 | > | char    *v2, | 
| 344 | > | char    *v3 | 
| 345 | > | ) | 
| 346 |  | { | 
| 347 |  | VNDX    v1i, v2i, v3i; | 
| 348 |  | RREAL   *v1c, *v2c, *v3c; | 
| 349 |  | RREAL   *v1n, *v2n, *v3n; | 
| 350 |  |  | 
| 351 | < | if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) | 
| 351 | > | if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) { | 
| 352 | > | error(WARNING, "bad vertex reference"); | 
| 353 |  | return(0); | 
| 354 | < |  | 
| 354 | > | } | 
| 355 |  | if (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0) { | 
| 356 |  | v1c = vtlist[v1i[1]]; | 
| 357 |  | v2c = vtlist[v2i[1]]; | 
| 372 |  |  | 
| 373 |  |  | 
| 374 |  | static void | 
| 375 | < | freeverts()                     /* free all vertices */ | 
| 375 | > | freeverts(void)                 /* free all vertices */ | 
| 376 |  | { | 
| 377 |  | if (nvs) { | 
| 378 |  | free((void *)vlist); | 
| 390 |  |  | 
| 391 |  |  | 
| 392 |  | static int | 
| 393 | < | newv(x, y, z)                   /* create a new vertex */ | 
| 394 | < | double  x, y, z; | 
| 393 | > | newv(                   /* create a new vertex */ | 
| 394 | > | double  x, | 
| 395 | > | double  y, | 
| 396 | > | double  z | 
| 397 | > | ) | 
| 398 |  | { | 
| 399 |  | if (!(nvs%CHUNKSIZ)) {          /* allocate next block */ | 
| 400 |  | if (nvs == 0) | 
| 414 |  |  | 
| 415 |  |  | 
| 416 |  | static int | 
| 417 | < | newvn(x, y, z)                  /* create a new vertex normal */ | 
| 418 | < | double  x, y, z; | 
| 417 | > | newvn(                  /* create a new vertex normal */ | 
| 418 | > | double  x, | 
| 419 | > | double  y, | 
| 420 | > | double  z | 
| 421 | > | ) | 
| 422 |  | { | 
| 423 |  | if (!(nvns%CHUNKSIZ)) {         /* allocate next block */ | 
| 424 |  | if (nvns == 0) | 
| 440 |  |  | 
| 441 |  |  | 
| 442 |  | static int | 
| 443 | < | newvt(x, y)                     /* create a new texture map vertex */ | 
| 444 | < | double  x, y; | 
| 443 | > | newvt(                  /* create a new texture map vertex */ | 
| 444 | > | double  x, | 
| 445 | > | double  y | 
| 446 | > | ) | 
| 447 |  | { | 
| 448 |  | if (!(nvts%CHUNKSIZ)) {         /* allocate next block */ | 
| 449 |  | if (nvts == 0) | 
| 462 |  |  | 
| 463 |  |  | 
| 464 |  | static void | 
| 465 | < | syntax(er)                      /* report syntax error and exit */ | 
| 466 | < | char    *er; | 
| 465 | > | syntax(                 /* report syntax error and exit */ | 
| 466 | > | char    *er | 
| 467 | > | ) | 
| 468 |  | { | 
| 469 |  | sprintf(errmsg, "%s: Wavefront syntax error near line %d: %s\n", | 
| 470 |  | inpfile, lineno, er); |