7 |
|
/* |
8 |
|
* Convert a Wavefront .obj file to Radiance format. |
9 |
|
* |
10 |
< |
* Currently, we support only polygonal geometry, and faces |
11 |
< |
* must be either quads or triangles for smoothing to work. |
10 |
> |
* Currently, we support only polygonal geometry. Non-planar |
11 |
> |
* faces are broken rather haphazardly into triangles. |
12 |
|
* Also, texture map indices only work for triangles, though |
13 |
< |
* I'm not sure they work correctly. |
13 |
> |
* I'm not sure they work correctly. (Taken out -- see TEXMAPS defines.) |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "standard.h" |
69 |
|
|
70 |
|
char *defmat = DEFMAT; /* default (starting) material name */ |
71 |
|
char *defobj = DEFOBJ; /* default (starting) object name */ |
72 |
– |
int donames = 0; /* only get qualifier names */ |
72 |
|
|
73 |
|
char *getmtl(), *getonm(); |
74 |
|
|
76 |
|
char matname[64]; /* current material name */ |
77 |
|
char group[16][32]; /* current group names */ |
78 |
|
char objname[128]; /* current object name */ |
79 |
+ |
char *inpfile; /* input file name */ |
80 |
|
int lineno; /* current line number */ |
81 |
< |
int faceno; /* number of faces read */ |
81 |
> |
int faceno; /* current face number */ |
82 |
|
|
83 |
|
|
84 |
|
main(argc, argv) /* read in .obj file and convert */ |
85 |
|
int argc; |
86 |
|
char *argv[]; |
87 |
|
{ |
88 |
< |
char *fname; |
88 |
> |
int donames = 0; |
89 |
|
int i; |
90 |
|
|
91 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
105 |
|
if (i > argc | i < argc-1) |
106 |
|
goto userr; |
107 |
|
if (i == argc) |
108 |
< |
fname = "<stdin>"; |
109 |
< |
else if (freopen(fname=argv[i], "r", stdin) == NULL) { |
110 |
< |
fprintf(stderr, "%s: cannot open\n", fname); |
108 |
> |
inpfile = "<stdin>"; |
109 |
> |
else if (freopen(inpfile=argv[i], "r", stdin) == NULL) { |
110 |
> |
fprintf(stderr, "%s: cannot open\n", inpfile); |
111 |
|
exit(1); |
112 |
|
} |
113 |
|
if (donames) { /* scan for ids */ |
114 |
|
getnames(stdin); |
115 |
< |
printf("filename \"%s\"\n", fname); |
115 |
> |
printf("filename \"%s\"\n", inpfile); |
116 |
|
printf("filetype \"Wavefront\"\n"); |
117 |
|
write_quals(&qlist, qual, stdout); |
118 |
|
printf("qualifier %s begin\n", qlist.qual[Q_FAC]); |
121 |
|
} else { /* translate file */ |
122 |
|
printf("# "); |
123 |
|
printargs(argc, argv, stdout); |
124 |
< |
convert(fname, stdin); |
124 |
> |
convert(stdin); |
125 |
|
} |
126 |
|
exit(0); |
127 |
|
userr: |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
< |
convert(fname, fp) /* convert a T-mesh */ |
184 |
< |
char *fname; |
183 |
> |
convert(fp) /* convert a T-mesh */ |
184 |
|
FILE *fp; |
185 |
|
{ |
186 |
|
char *argv[MAXARG]; |
187 |
|
int argc; |
188 |
|
int nstats, nunknown; |
189 |
|
register int i; |
190 |
< |
/* start fresh */ |
192 |
< |
freeverts(); |
193 |
< |
mapname[0] = '\0'; |
194 |
< |
strcpy(matname, defmat); |
195 |
< |
strcpy(objname, defobj); |
196 |
< |
lineno = 0; |
190 |
> |
|
191 |
|
nstats = nunknown = 0; |
192 |
|
/* scan until EOF */ |
193 |
|
while (argc = getstmt(argv, fp)) { |
196 |
|
switch (argv[0][1]) { |
197 |
|
case '\0': /* point */ |
198 |
|
if (badarg(argc-1,argv+1,"fff")) |
199 |
< |
syntax(fname, lineno, "Bad vertex"); |
199 |
> |
syntax("Bad vertex"); |
200 |
|
newv(atof(argv[1]), atof(argv[2]), |
201 |
|
atof(argv[3])); |
202 |
|
break; |
204 |
|
if (argv[0][2]) |
205 |
|
goto unknown; |
206 |
|
if (badarg(argc-1,argv+1,"fff")) |
207 |
< |
syntax(fname, lineno, "Bad normal"); |
207 |
> |
syntax("Bad normal"); |
208 |
|
if (!newvn(atof(argv[1]), atof(argv[2]), |
209 |
|
atof(argv[3]))) |
210 |
< |
syntax(fname, lineno, "Zero normal"); |
210 |
> |
syntax("Zero normal"); |
211 |
|
break; |
212 |
|
case 't': /* texture map */ |
213 |
|
if (argv[0][2]) |
226 |
|
faceno++; |
227 |
|
switch (argc-1) { |
228 |
|
case 0: case 1: case 2: |
229 |
< |
syntax(fname, lineno, "Too few vertices"); |
229 |
> |
syntax("Too few vertices"); |
230 |
|
break; |
231 |
|
case 3: |
232 |
|
if (!puttri(argv[1], argv[2], argv[3])) |
233 |
< |
syntax(fname, lineno, "Bad triangle"); |
233 |
> |
syntax("Bad triangle"); |
234 |
|
break; |
235 |
|
case 4: |
236 |
|
if (!putquad(argv[1], argv[2], |
237 |
|
argv[3], argv[4])) |
238 |
< |
syntax(fname, lineno, "Bad quad"); |
238 |
> |
syntax("Bad quad"); |
239 |
|
break; |
240 |
|
default: |
241 |
|
if (!putface(argc-1, argv+1)) |
242 |
< |
syntax(fname, lineno, "Bad face"); |
242 |
> |
syntax("Bad face"); |
243 |
|
break; |
244 |
|
} |
245 |
|
break; |
254 |
|
if (!strcmp(argv[1], "off")) |
255 |
|
mapname[0] = '\0'; |
256 |
|
else |
257 |
< |
strcpy(mapname, argv[1]); |
257 |
> |
sprintf(mapname, "%s.pic", argv[1]); |
258 |
|
} else |
259 |
|
goto unknown; |
260 |
|
break; |
273 |
|
group[i-1][0] = '\0'; |
274 |
|
break; |
275 |
|
case '#': /* comment */ |
276 |
+ |
printargs(argc, argv, stdout); |
277 |
|
break; |
278 |
|
default:; /* something we don't deal with */ |
279 |
|
unknown: |
282 |
|
} |
283 |
|
nstats++; |
284 |
|
} |
285 |
< |
printf("\n# Done processing file: %s\n", fname); |
285 |
> |
printf("\n# Done processing file: %s\n", inpfile); |
286 |
|
printf("# %d lines, %d statements, %d unrecognized\n", |
287 |
|
lineno, nstats, nunknown); |
288 |
|
} |
327 |
|
{ |
328 |
|
register RULEHD *rp = ourmapping; |
329 |
|
|
330 |
< |
if (rp == NULL) /* no rule set */ |
331 |
< |
return(matname); |
330 |
> |
if (rp == NULL) { /* no rule set */ |
331 |
> |
if (matname[0]) |
332 |
> |
return(matname); |
333 |
> |
if (group[0][0]) |
334 |
> |
return(group[0]); |
335 |
> |
return(defmat); |
336 |
> |
} |
337 |
|
/* check for match */ |
338 |
|
do { |
339 |
|
if (matchrule(rp)) { |
354 |
|
static char name[64]; |
355 |
|
register char *cp1, *cp2; |
356 |
|
register int i; |
357 |
< |
|
358 |
< |
if (!group[0][0] || strcmp(objname, DEFOBJ)) |
359 |
< |
return(objname); /* good enough for us */ |
360 |
< |
|
357 |
> |
/* check for preset */ |
358 |
> |
if (objname[0]) |
359 |
> |
return(objname); |
360 |
> |
if (!group[0][0]) |
361 |
> |
return(defobj); |
362 |
|
cp1 = name; /* else make name out of groups */ |
363 |
|
for (i = 0; group[i][0]; i++) { |
364 |
|
cp2 = group[i]; |
382 |
|
register int i; |
383 |
|
|
384 |
|
if (rp->qflg & FL(Q_MTL)) { |
385 |
+ |
if (!matname[0]) |
386 |
+ |
return(0); |
387 |
|
tmpid.number = 0; |
388 |
|
tmpid.name = matname; |
389 |
|
if (!matchid(&tmpid, &idm(rp)[Q_MTL])) |
390 |
|
return(0); |
391 |
|
} |
392 |
|
if (rp->qflg & FL(Q_MAP)) { |
393 |
+ |
if (!mapname[0]) |
394 |
+ |
return(0); |
395 |
|
tmpid.number = 0; |
396 |
|
tmpid.name = mapname; |
397 |
|
if (!matchid(&tmpid, &idm(rp)[Q_MAP])) |
408 |
|
return(0); |
409 |
|
} |
410 |
|
if (rp->qflg & FL(Q_OBJ)) { |
411 |
+ |
if (!objname[0]) |
412 |
+ |
return(0); |
413 |
|
tmpid.number = 0; |
414 |
|
tmpid.name = objname; |
415 |
|
if (!matchid(&tmpid, &idm(rp)[Q_OBJ])) |
472 |
|
} |
473 |
|
|
474 |
|
|
475 |
< |
putface(ac, av) /* put out an N-sided polygon */ |
475 |
> |
nonplanar(ac, av) /* are vertices are non-planar? */ |
476 |
|
register int ac; |
477 |
|
register char **av; |
478 |
|
{ |
479 |
|
VNDX vi; |
480 |
< |
char *mod; |
480 |
> |
FLOAT *p0, *p1; |
481 |
> |
FVECT v1, v2, nsum, newn; |
482 |
> |
double d; |
483 |
> |
register int i; |
484 |
|
|
485 |
< |
if ((mod = getmtl()) == NULL) |
485 |
> |
if (!cvtndx(vi, av[0])) |
486 |
> |
return(0); |
487 |
> |
if (vi[2] >= 0) |
488 |
> |
return(1); /* has interpolated normals */ |
489 |
> |
if (ac < 4) |
490 |
> |
return(0); /* it's a triangle! */ |
491 |
> |
/* set up */ |
492 |
> |
p0 = vlist[vi[0]]; |
493 |
> |
if (!cvtndx(vi, av[1])) |
494 |
> |
return(0); /* error gets caught later */ |
495 |
> |
nsum[0] = nsum[1] = nsum[2] = 0.; |
496 |
> |
p1 = vlist[vi[0]]; |
497 |
> |
fvsum(v2, p1, p0, -1.0); |
498 |
> |
for (i = 2; i < ac; i++) { |
499 |
> |
VCOPY(v1, v2); |
500 |
> |
if (!cvtndx(vi, av[i])) |
501 |
> |
return(0); |
502 |
> |
p1 = vlist[vi[0]]; |
503 |
> |
fvsum(v2, p1, p0, -1.0); |
504 |
> |
fcross(newn, v1, v2); |
505 |
> |
if (normalize(newn) == 0.0) { |
506 |
> |
if (i < 3) |
507 |
> |
return(1); /* can't deal with this */ |
508 |
> |
fvsum(nsum, nsum, nsum, 1./(i-2)); |
509 |
> |
continue; |
510 |
> |
} |
511 |
> |
d = fdot(newn,nsum); |
512 |
> |
if (d >= 0) { |
513 |
> |
if (d < (1.0-FTINY)*(i-2)) |
514 |
> |
return(1); |
515 |
> |
fvsum(nsum, nsum, newn, 1.0); |
516 |
> |
} else { |
517 |
> |
if (d > -(1.0-FTINY)*(i-2)) |
518 |
> |
return(1); |
519 |
> |
fvsum(nsum, nsum, newn, -1.0); |
520 |
> |
} |
521 |
> |
} |
522 |
> |
return(0); |
523 |
> |
} |
524 |
> |
|
525 |
> |
|
526 |
> |
putface(ac, av) /* put out an N-sided polygon */ |
527 |
> |
int ac; |
528 |
> |
register char **av; |
529 |
> |
{ |
530 |
> |
VNDX vi; |
531 |
> |
char *cp; |
532 |
> |
register int i; |
533 |
> |
|
534 |
> |
if (nonplanar(ac, av)) { /* break into quads and triangles */ |
535 |
> |
while (ac > 3) { |
536 |
> |
if (!putquad(av[0], av[1], av[2], av[3])) |
537 |
> |
return(0); |
538 |
> |
ac -= 2; /* remove two vertices & rotate */ |
539 |
> |
cp = av[0]; |
540 |
> |
for (i = 0; i < ac-1; i++) |
541 |
> |
av[i] = av[i+3]; |
542 |
> |
av[i] = cp; |
543 |
> |
} |
544 |
> |
if (ac == 3 && !puttri(av[0], av[1], av[2])) |
545 |
> |
return(0); |
546 |
> |
return(1); |
547 |
> |
} |
548 |
> |
if ((cp = getmtl()) == NULL) |
549 |
|
return(-1); |
550 |
< |
printf("\n%s polygon %s.%d\n", mod, getonm(), faceno); |
550 |
> |
printf("\n%s polygon %s.%d\n", cp, getonm(), faceno); |
551 |
|
printf("0\n0\n%d\n", 3*ac); |
552 |
< |
while (ac--) { |
553 |
< |
if (!cvtndx(vi, *av++)) |
552 |
> |
for (i = 0; i < ac; i++) { |
553 |
> |
if (!cvtndx(vi, av[i])) |
554 |
|
return(0); |
555 |
|
pvect(vlist[vi[0]]); |
556 |
|
} |
573 |
|
return(0); |
574 |
|
/* compute barycentric coordinates */ |
575 |
|
texOK = (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0); |
576 |
+ |
#ifdef TEXMAPS |
577 |
|
patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0); |
578 |
+ |
#else |
579 |
+ |
patOK = 0; |
580 |
+ |
#endif |
581 |
|
if (texOK | patOK) |
582 |
|
if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]], |
583 |
|
vlist[v3i[0]]) < 0) |
599 |
|
vnlist[v1i[2]][2], vnlist[v2i[2]][2], |
600 |
|
vnlist[v3i[2]][2]); |
601 |
|
} |
602 |
+ |
#ifdef TEXMAPS |
603 |
|
/* put out pattern (if any) */ |
604 |
|
if (patOK) { |
605 |
|
printf("\n%s colorpict %s\n", mod, PATNAME); |
612 |
|
printf("\t%f %f %f\n", vtlist[v1i[1]][1], |
613 |
|
vtlist[v2i[1]][1], vtlist[v3i[1]][1]); |
614 |
|
} |
615 |
+ |
#endif |
616 |
|
/* put out triangle */ |
617 |
|
printf("\n%s polygon %s.%d\n", mod, getonm(), faceno); |
618 |
|
printf("0\n0\n9\n"); |
684 |
|
FVECT v1, v2, vc1, vc2; |
685 |
|
int ok1, ok2; |
686 |
|
|
687 |
+ |
#ifdef TEXMAPS |
688 |
+ |
/* also should output texture index coordinates, |
689 |
+ |
* which will require new .cal file |
690 |
+ |
*/ |
691 |
+ |
#endif |
692 |
|
if ((mod = getmtl()) == NULL) |
693 |
|
return(-1); |
694 |
|
name = getonm(); |
711 |
|
axis = norminterp(norm, p0i, p1i, p2i, p3i); |
712 |
|
|
713 |
|
/* put out quadrilateral? */ |
714 |
< |
if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) { |
714 |
> |
if (ok1 & ok2 && fabs(fdot(vc1,vc2)) >= 1.0-FTINY) { |
715 |
|
printf("\n%s ", mod); |
716 |
|
if (axis != -1) { |
717 |
|
printf("texfunc %s\n", TEXNAME); |
789 |
|
FVECT v1; |
790 |
|
register int i, j; |
791 |
|
|
792 |
+ |
#ifdef TEXMAPS |
793 |
+ |
/* also check for texture indices */ |
794 |
+ |
#endif |
795 |
|
if (!(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0)) |
796 |
|
return(-1); |
797 |
|
/* find dominant axis */ |
828 |
|
eqnmat[j][1]*vnlist[p1i[2]][i] + |
829 |
|
eqnmat[j][2]*vnlist[p2i[2]][i] + |
830 |
|
eqnmat[j][3]*vnlist[p3i[2]][i]; |
831 |
+ |
#ifdef TEXMAPS |
832 |
+ |
/* compute result matrix for texture indices */ |
833 |
+ |
#endif |
834 |
|
return(ax); |
835 |
|
|
836 |
|
#undef u |
929 |
|
} |
930 |
|
|
931 |
|
|
932 |
< |
syntax(fn, ln, er) /* report syntax error and exit */ |
843 |
< |
char *fn; |
844 |
< |
int ln; |
932 |
> |
syntax(er) /* report syntax error and exit */ |
933 |
|
char *er; |
934 |
|
{ |
935 |
|
fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n", |
936 |
< |
fn, ln, er); |
936 |
> |
inpfile, lineno, er); |
937 |
|
exit(1); |
938 |
|
} |