| 2 |
|
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 4 |
|
/* |
| 5 |
< |
* Convert a Wavefront .obj file to Radiance format. |
| 5 |
> |
* Convert a Wavefront .OBJ file to Radiance format. |
| 6 |
|
* |
| 7 |
|
* Currently, we support only polygonal geometry. Non-planar |
| 8 |
|
* faces are broken rather haphazardly into triangles. |
| 10 |
|
* I'm not sure they work correctly. (Taken out -- see TEXMAPS defines.) |
| 11 |
|
*/ |
| 12 |
|
|
| 13 |
< |
#include "standard.h" |
| 13 |
> |
#include <stdlib.h> |
| 14 |
> |
#include <stdio.h> |
| 15 |
> |
#include <ctype.h> |
| 16 |
|
|
| 17 |
+ |
#include "rtmath.h" |
| 18 |
+ |
#include "rtio.h" |
| 19 |
+ |
#include "resolu.h" |
| 20 |
|
#include "trans.h" |
| 16 |
– |
|
| 21 |
|
#include "tmesh.h" |
| 22 |
|
|
| 19 |
– |
#include <ctype.h> |
| 23 |
|
|
| 24 |
|
#define PATNAME "M-pat" /* mesh pattern name (reused) */ |
| 25 |
|
#define TEXNAME "M-nor" /* mesh texture name (reused) */ |
| 35 |
|
RREAL (*vtlist)[2]; /* map vertex list */ |
| 36 |
|
int nvts; |
| 37 |
|
|
| 38 |
+ |
int ndegen = 0; /* count of degenerate faces */ |
| 39 |
+ |
int n0norm = 0; /* count of zero normals */ |
| 40 |
+ |
|
| 41 |
|
typedef int VNDX[3]; /* vertex index (point,map,normal) */ |
| 42 |
|
|
| 43 |
< |
#define CHUNKSIZ 256 /* vertex allocation chunk size */ |
| 43 |
> |
#define CHUNKSIZ 1024 /* vertex allocation chunk size */ |
| 44 |
|
|
| 45 |
< |
#define MAXARG 64 /* maximum # arguments in a statement */ |
| 45 |
> |
#define MAXARG 512 /* maximum # arguments in a statement */ |
| 46 |
|
|
| 47 |
|
/* qualifiers */ |
| 48 |
|
#define Q_MTL 0 |
| 71 |
|
|
| 72 |
|
int flatten = 0; /* discard surface normal information */ |
| 73 |
|
|
| 74 |
< |
char *getmtl(), *getonm(); |
| 75 |
< |
|
| 76 |
< |
char mapname[128]; /* current picture file */ |
| 77 |
< |
char matname[64]; /* current material name */ |
| 72 |
< |
char group[16][32]; /* current group names */ |
| 73 |
< |
char objname[128]; /* current object name */ |
| 74 |
> |
char mapname[256]; /* current picture file */ |
| 75 |
> |
char matname[256]; /* current material name */ |
| 76 |
> |
char group[8][256]; /* current group name(s) */ |
| 77 |
> |
char objname[256]; /* current object name */ |
| 78 |
|
char *inpfile; /* input file name */ |
| 79 |
|
int lineno; /* current line number */ |
| 80 |
|
int faceno; /* current face number */ |
| 81 |
|
|
| 82 |
+ |
static void getnames(FILE *fp); |
| 83 |
+ |
static void convert(FILE *fp); |
| 84 |
+ |
static int getstmt(char *av[MAXARG], FILE *fp); |
| 85 |
+ |
static char * getmtl(void); |
| 86 |
+ |
static char * getonm(void); |
| 87 |
+ |
static int matchrule(RULEHD *rp); |
| 88 |
+ |
static int cvtndx(VNDX vi, char *vs); |
| 89 |
+ |
static int nonplanar(int ac, char **av); |
| 90 |
+ |
static int putface(int ac, char **av); |
| 91 |
+ |
static int puttri(char *v1, char *v2, char *v3); |
| 92 |
+ |
static void freeverts(void); |
| 93 |
+ |
static int newv(double x, double y, double z); |
| 94 |
+ |
static int newvn(double x, double y, double z); |
| 95 |
+ |
static int newvt(double x, double y); |
| 96 |
+ |
static void syntax(char *er); |
| 97 |
|
|
| 98 |
< |
main(argc, argv) /* read in .obj file and convert */ |
| 99 |
< |
int argc; |
| 100 |
< |
char *argv[]; |
| 98 |
> |
|
| 99 |
> |
int |
| 100 |
> |
main( /* read in .OBJ file and convert */ |
| 101 |
> |
int argc, |
| 102 |
> |
char *argv[] |
| 103 |
> |
) |
| 104 |
|
{ |
| 105 |
|
int donames = 0; |
| 106 |
|
int i; |
| 122 |
|
default: |
| 123 |
|
goto userr; |
| 124 |
|
} |
| 125 |
< |
if (i > argc | i < argc-1) |
| 125 |
> |
if ((i > argc) | (i < argc-1)) |
| 126 |
|
goto userr; |
| 127 |
|
if (i == argc) |
| 128 |
|
inpfile = "<stdin>"; |
| 143 |
|
printargs(argc, argv, stdout); |
| 144 |
|
convert(stdin); |
| 145 |
|
} |
| 146 |
+ |
if (ndegen) |
| 147 |
+ |
printf("# %d degenerate faces\n", ndegen); |
| 148 |
+ |
if (n0norm) |
| 149 |
+ |
printf("# %d invalid (zero) normals\n", n0norm); |
| 150 |
|
exit(0); |
| 151 |
|
userr: |
| 152 |
|
fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n", |
| 155 |
|
} |
| 156 |
|
|
| 157 |
|
|
| 158 |
< |
getnames(fp) /* get valid qualifier names */ |
| 159 |
< |
FILE *fp; |
| 158 |
> |
void |
| 159 |
> |
getnames( /* get valid qualifier names */ |
| 160 |
> |
FILE *fp |
| 161 |
> |
) |
| 162 |
|
{ |
| 163 |
|
char *argv[MAXARG]; |
| 164 |
|
int argc; |
| 165 |
|
ID tmpid; |
| 166 |
< |
register int i; |
| 166 |
> |
int i; |
| 167 |
|
|
| 168 |
< |
while (argc = getstmt(argv, fp)) |
| 168 |
> |
while ( (argc = getstmt(argv, fp)) ) |
| 169 |
|
switch (argv[0][0]) { |
| 170 |
|
case 'f': /* face */ |
| 171 |
|
if (!argv[0][1]) |
| 206 |
|
} |
| 207 |
|
|
| 208 |
|
|
| 209 |
< |
convert(fp) /* convert a T-mesh */ |
| 210 |
< |
FILE *fp; |
| 209 |
> |
void |
| 210 |
> |
convert( /* convert an OBJ stream */ |
| 211 |
> |
FILE *fp |
| 212 |
> |
) |
| 213 |
|
{ |
| 214 |
|
char *argv[MAXARG]; |
| 215 |
|
int argc; |
| 216 |
|
int nstats, nunknown; |
| 217 |
< |
register int i; |
| 217 |
> |
int i; |
| 218 |
|
|
| 219 |
|
nstats = nunknown = 0; |
| 220 |
|
/* scan until EOF */ |
| 221 |
< |
while (argc = getstmt(argv, fp)) { |
| 221 |
> |
while ( (argc = getstmt(argv, fp)) ) { |
| 222 |
|
switch (argv[0][0]) { |
| 223 |
|
case 'v': /* vertex */ |
| 224 |
|
switch (argv[0][1]) { |
| 277 |
|
if (!strcmp(argv[1], "off")) |
| 278 |
|
mapname[0] = '\0'; |
| 279 |
|
else |
| 280 |
< |
sprintf(mapname, "%s.pic", argv[1]); |
| 280 |
> |
sprintf(mapname, "%s.hdr", argv[1]); |
| 281 |
|
} else |
| 282 |
|
goto unknown; |
| 283 |
|
break; |
| 293 |
|
goto unknown; |
| 294 |
|
for (i = 1; i < argc; i++) |
| 295 |
|
strcpy(group[i-1], argv[i]); |
| 296 |
< |
group[i-1][0] = '\0'; |
| 296 |
> |
group[argc-1][0] = '\0'; |
| 297 |
|
break; |
| 298 |
|
case '#': /* comment */ |
| 299 |
|
printargs(argc, argv, stdout); |
| 312 |
|
|
| 313 |
|
|
| 314 |
|
int |
| 315 |
< |
getstmt(av, fp) /* read the next statement from fp */ |
| 316 |
< |
register char *av[MAXARG]; |
| 317 |
< |
FILE *fp; |
| 315 |
> |
getstmt( /* read the next statement from fp */ |
| 316 |
> |
char *av[MAXARG], |
| 317 |
> |
FILE *fp |
| 318 |
> |
) |
| 319 |
|
{ |
| 320 |
< |
extern char *fgetline(); |
| 321 |
< |
static char sbuf[MAXARG*10]; |
| 322 |
< |
register char *cp; |
| 292 |
< |
register int i; |
| 320 |
> |
static char sbuf[MAXARG*16]; |
| 321 |
> |
char *cp; |
| 322 |
> |
int i; |
| 323 |
|
|
| 324 |
|
do { |
| 325 |
|
if (fgetline(cp=sbuf, sizeof(sbuf), fp) == NULL) |
| 331 |
|
lineno++; |
| 332 |
|
*cp++ = '\0'; |
| 333 |
|
} |
| 334 |
< |
if (!*cp || i >= MAXARG-1) |
| 334 |
> |
if (!*cp) |
| 335 |
|
break; |
| 336 |
+ |
if (i >= MAXARG-1) { |
| 337 |
+ |
fprintf(stderr, |
| 338 |
+ |
"warning: line %d: too many arguments (limit %d)\n", |
| 339 |
+ |
lineno+1, MAXARG-1); |
| 340 |
+ |
break; |
| 341 |
+ |
} |
| 342 |
|
av[i++] = cp; |
| 343 |
|
while (*++cp && !isspace(*cp)) |
| 344 |
|
; |
| 352 |
|
|
| 353 |
|
|
| 354 |
|
char * |
| 355 |
< |
getmtl() /* figure material for this face */ |
| 355 |
> |
getmtl(void) /* figure material for this face */ |
| 356 |
|
{ |
| 357 |
< |
register RULEHD *rp = ourmapping; |
| 357 |
> |
RULEHD *rp = ourmapping; |
| 358 |
|
|
| 359 |
|
if (rp == NULL) { /* no rule set */ |
| 360 |
|
if (matname[0]) |
| 378 |
|
|
| 379 |
|
|
| 380 |
|
char * |
| 381 |
< |
getonm() /* invent a good name for object */ |
| 381 |
> |
getonm(void) /* invent a good name for object */ |
| 382 |
|
{ |
| 383 |
|
static char name[64]; |
| 384 |
< |
register char *cp1, *cp2; |
| 385 |
< |
register int i; |
| 384 |
> |
char *cp1, *cp2; |
| 385 |
> |
int i; |
| 386 |
|
/* check for preset */ |
| 387 |
|
if (objname[0]) |
| 388 |
|
return(objname); |
| 393 |
|
cp2 = group[i]; |
| 394 |
|
if (cp1 > name) |
| 395 |
|
*cp1++ = '.'; |
| 396 |
< |
while (*cp1 = *cp2++) |
| 396 |
> |
while ( (*cp1 = *cp2++) ) |
| 397 |
|
if (++cp1 >= name+sizeof(name)-2) { |
| 398 |
|
*cp1 = '\0'; |
| 399 |
|
return(name); |
| 403 |
|
} |
| 404 |
|
|
| 405 |
|
|
| 406 |
< |
matchrule(rp) /* check for a match on this rule */ |
| 407 |
< |
register RULEHD *rp; |
| 406 |
> |
int |
| 407 |
> |
matchrule( /* check for a match on this rule */ |
| 408 |
> |
RULEHD *rp |
| 409 |
> |
) |
| 410 |
|
{ |
| 411 |
|
ID tmpid; |
| 412 |
|
int gotmatch; |
| 413 |
< |
register int i; |
| 413 |
> |
int i; |
| 414 |
|
|
| 415 |
|
if (rp->qflg & FL(Q_MTL)) { |
| 416 |
|
if (!matname[0]) |
| 456 |
|
} |
| 457 |
|
|
| 458 |
|
|
| 459 |
< |
cvtndx(vi, vs) /* convert vertex string to index */ |
| 460 |
< |
register VNDX vi; |
| 461 |
< |
register char *vs; |
| 459 |
> |
int |
| 460 |
> |
cvtndx( /* convert vertex string to index */ |
| 461 |
> |
VNDX vi, |
| 462 |
> |
char *vs |
| 463 |
> |
) |
| 464 |
|
{ |
| 465 |
|
/* get point */ |
| 466 |
|
vi[0] = atoi(vs); |
| 501 |
|
return(0); |
| 502 |
|
} else |
| 503 |
|
vi[2] = -1; |
| 504 |
+ |
/* zero normal is not normal */ |
| 505 |
+ |
if (vi[2] >= 0 && DOT(vnlist[vi[2]],vnlist[vi[2]]) <= FTINY) |
| 506 |
+ |
vi[2] = -1; |
| 507 |
|
return(1); |
| 508 |
|
} |
| 509 |
|
|
| 510 |
|
|
| 511 |
< |
nonplanar(ac, av) /* are vertices non-planar? */ |
| 512 |
< |
register int ac; |
| 513 |
< |
register char **av; |
| 511 |
> |
int |
| 512 |
> |
nonplanar( /* are vertices non-planar? */ |
| 513 |
> |
int ac, |
| 514 |
> |
char **av |
| 515 |
> |
) |
| 516 |
|
{ |
| 517 |
|
VNDX vi; |
| 518 |
|
RREAL *p0, *p1; |
| 519 |
|
FVECT v1, v2, nsum, newn; |
| 520 |
|
double d; |
| 521 |
< |
register int i; |
| 521 |
> |
int i; |
| 522 |
|
|
| 523 |
|
if (!cvtndx(vi, av[0])) |
| 524 |
|
return(0); |
| 561 |
|
} |
| 562 |
|
|
| 563 |
|
|
| 564 |
< |
putface(ac, av) /* put out an N-sided polygon */ |
| 565 |
< |
int ac; |
| 566 |
< |
register char **av; |
| 564 |
> |
int |
| 565 |
> |
putface( /* put out an N-sided polygon */ |
| 566 |
> |
int ac, |
| 567 |
> |
char **av |
| 568 |
> |
) |
| 569 |
|
{ |
| 570 |
|
VNDX vi; |
| 571 |
|
char *cp; |
| 572 |
< |
register int i; |
| 572 |
> |
int i; |
| 573 |
|
|
| 574 |
|
if (nonplanar(ac, av)) { /* break into triangles */ |
| 575 |
|
while (ac > 2) { |
| 596 |
|
} |
| 597 |
|
|
| 598 |
|
|
| 599 |
< |
puttri(v1, v2, v3) /* put out a triangle */ |
| 600 |
< |
char *v1, *v2, *v3; |
| 599 |
> |
int |
| 600 |
> |
puttri( /* put out a triangle */ |
| 601 |
> |
char *v1, |
| 602 |
> |
char *v2, |
| 603 |
> |
char *v3 |
| 604 |
> |
) |
| 605 |
|
{ |
| 606 |
|
char *mod; |
| 607 |
|
VNDX v1i, v2i, v3i; |
| 608 |
|
BARYCCM bvecs; |
| 609 |
|
RREAL bcoor[3][3]; |
| 610 |
< |
int texOK, patOK; |
| 610 |
> |
int texOK = 0, patOK; |
| 611 |
|
int flatness; |
| 612 |
< |
register int i; |
| 612 |
> |
int i; |
| 613 |
|
|
| 614 |
|
if ((mod = getmtl()) == NULL) |
| 615 |
|
return(-1); |
| 625 |
|
|
| 626 |
|
switch (flatness) { |
| 627 |
|
case DEGEN: /* zero area */ |
| 628 |
+ |
ndegen++; |
| 629 |
|
return(-1); |
| 630 |
|
case RVFLAT: /* reversed normals, but flat */ |
| 631 |
|
case ISFLAT: /* smoothing unnecessary */ |
| 646 |
|
if (texOK | patOK) |
| 647 |
|
if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]], |
| 648 |
|
vlist[v3i[0]]) < 0) |
| 649 |
< |
return(-1); |
| 649 |
> |
texOK = patOK = 0; |
| 650 |
|
/* put out texture (if any) */ |
| 651 |
|
if (texOK) { |
| 652 |
|
printf("\n%s texfunc %s\n", mod, TEXNAME); |
| 691 |
|
} |
| 692 |
|
|
| 693 |
|
|
| 694 |
< |
freeverts() /* free all vertices */ |
| 694 |
> |
void |
| 695 |
> |
freeverts(void) /* free all vertices */ |
| 696 |
|
{ |
| 697 |
|
if (nvs) { |
| 698 |
|
free((void *)vlist); |
| 710 |
|
|
| 711 |
|
|
| 712 |
|
int |
| 713 |
< |
newv(x, y, z) /* create a new vertex */ |
| 714 |
< |
double x, y, z; |
| 713 |
> |
newv( /* create a new vertex */ |
| 714 |
> |
double x, |
| 715 |
> |
double y, |
| 716 |
> |
double z |
| 717 |
> |
) |
| 718 |
|
{ |
| 719 |
|
if (!(nvs%CHUNKSIZ)) { /* allocate next block */ |
| 720 |
|
if (nvs == 0) |
| 737 |
|
|
| 738 |
|
|
| 739 |
|
int |
| 740 |
< |
newvn(x, y, z) /* create a new vertex normal */ |
| 741 |
< |
double x, y, z; |
| 740 |
> |
newvn( /* create a new vertex normal */ |
| 741 |
> |
double x, |
| 742 |
> |
double y, |
| 743 |
> |
double z |
| 744 |
> |
) |
| 745 |
|
{ |
| 746 |
|
if (!(nvns%CHUNKSIZ)) { /* allocate next block */ |
| 747 |
|
if (nvns == 0) |
| 759 |
|
vnlist[nvns][0] = x; |
| 760 |
|
vnlist[nvns][1] = y; |
| 761 |
|
vnlist[nvns][2] = z; |
| 762 |
< |
if (normalize(vnlist[nvns]) == 0.0) |
| 704 |
< |
return(0); |
| 762 |
> |
n0norm += (normalize(vnlist[nvns]) == 0.0); |
| 763 |
|
return(++nvns); |
| 764 |
|
} |
| 765 |
|
|
| 766 |
|
|
| 767 |
|
int |
| 768 |
< |
newvt(x, y) /* create a new texture map vertex */ |
| 769 |
< |
double x, y; |
| 768 |
> |
newvt( /* create a new texture map vertex */ |
| 769 |
> |
double x, |
| 770 |
> |
double y |
| 771 |
> |
) |
| 772 |
|
{ |
| 773 |
|
if (!(nvts%CHUNKSIZ)) { /* allocate next block */ |
| 774 |
|
if (nvts == 0) |
| 790 |
|
} |
| 791 |
|
|
| 792 |
|
|
| 793 |
< |
syntax(er) /* report syntax error and exit */ |
| 794 |
< |
char *er; |
| 793 |
> |
void |
| 794 |
> |
syntax( /* report syntax error and exit */ |
| 795 |
> |
char *er |
| 796 |
> |
) |
| 797 |
|
{ |
| 798 |
|
fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n", |
| 799 |
|
inpfile, lineno, er); |