1 |
– |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Convert a Wavefront .obj file to Radiance format. |
6 |
|
* |
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" |
19 |
– |
|
21 |
|
#include "tmesh.h" |
22 |
|
|
22 |
– |
#include <ctype.h> |
23 |
|
|
24 |
|
#define PATNAME "M-pat" /* mesh pattern name (reused) */ |
25 |
|
#define TEXNAME "M-nor" /* mesh texture name (reused) */ |
32 |
|
int nvs; /* number of vertices in our list */ |
33 |
|
FVECT *vnlist; /* vertex normal list */ |
34 |
|
int nvns; |
35 |
< |
FLOAT (*vtlist)[2]; /* map vertex list */ |
35 |
> |
RREAL (*vtlist)[2]; /* map vertex list */ |
36 |
|
int nvts; |
37 |
|
|
38 |
+ |
int ndegen = 0; /* count of degenerate faces */ |
39 |
+ |
|
40 |
|
typedef int VNDX[3]; /* vertex index (point,map,normal) */ |
41 |
|
|
42 |
< |
#define CHUNKSIZ 256 /* vertex allocation chunk size */ |
42 |
> |
#define CHUNKSIZ 1024 /* vertex allocation chunk size */ |
43 |
|
|
44 |
< |
#define MAXARG 64 /* maximum # arguments in a statement */ |
44 |
> |
#define MAXARG 512 /* maximum # arguments in a statement */ |
45 |
|
|
46 |
|
/* qualifiers */ |
47 |
|
#define Q_MTL 0 |
70 |
|
|
71 |
|
int flatten = 0; /* discard surface normal information */ |
72 |
|
|
71 |
– |
char *getmtl(), *getonm(); |
72 |
– |
|
73 |
|
char mapname[128]; /* current picture file */ |
74 |
|
char matname[64]; /* current material name */ |
75 |
|
char group[16][32]; /* current group names */ |
78 |
|
int lineno; /* current line number */ |
79 |
|
int faceno; /* current face number */ |
80 |
|
|
81 |
+ |
static void getnames(FILE *fp); |
82 |
+ |
static void convert(FILE *fp); |
83 |
+ |
static int getstmt(char *av[MAXARG], FILE *fp); |
84 |
+ |
static char * getmtl(void); |
85 |
+ |
static char * getonm(void); |
86 |
+ |
static int matchrule(RULEHD *rp); |
87 |
+ |
static int cvtndx(VNDX vi, char *vs); |
88 |
+ |
static int nonplanar(int ac, char **av); |
89 |
+ |
static int putface(int ac, char **av); |
90 |
+ |
static int puttri(char *v1, char *v2, char *v3); |
91 |
+ |
static void freeverts(void); |
92 |
+ |
static int newv(double x, double y, double z); |
93 |
+ |
static int newvn(double x, double y, double z); |
94 |
+ |
static int newvt(double x, double y); |
95 |
+ |
static void syntax(char *er); |
96 |
|
|
97 |
< |
main(argc, argv) /* read in .obj file and convert */ |
98 |
< |
int argc; |
99 |
< |
char *argv[]; |
97 |
> |
|
98 |
> |
int |
99 |
> |
main( /* read in .obj file and convert */ |
100 |
> |
int argc, |
101 |
> |
char *argv[] |
102 |
> |
) |
103 |
|
{ |
104 |
|
int donames = 0; |
105 |
|
int i; |
121 |
|
default: |
122 |
|
goto userr; |
123 |
|
} |
124 |
< |
if (i > argc | i < argc-1) |
124 |
> |
if ((i > argc) | (i < argc-1)) |
125 |
|
goto userr; |
126 |
|
if (i == argc) |
127 |
|
inpfile = "<stdin>"; |
142 |
|
printargs(argc, argv, stdout); |
143 |
|
convert(stdin); |
144 |
|
} |
145 |
+ |
if (ndegen) |
146 |
+ |
printf("# %d degenerate faces\n", ndegen); |
147 |
|
exit(0); |
148 |
|
userr: |
149 |
|
fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n", |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
< |
getnames(fp) /* get valid qualifier names */ |
156 |
< |
FILE *fp; |
155 |
> |
void |
156 |
> |
getnames( /* get valid qualifier names */ |
157 |
> |
FILE *fp |
158 |
> |
) |
159 |
|
{ |
160 |
|
char *argv[MAXARG]; |
161 |
|
int argc; |
162 |
|
ID tmpid; |
163 |
|
register int i; |
164 |
|
|
165 |
< |
while (argc = getstmt(argv, fp)) |
165 |
> |
while ( (argc = getstmt(argv, fp)) ) |
166 |
|
switch (argv[0][0]) { |
167 |
|
case 'f': /* face */ |
168 |
|
if (!argv[0][1]) |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
< |
convert(fp) /* convert a T-mesh */ |
207 |
< |
FILE *fp; |
206 |
> |
void |
207 |
> |
convert( /* convert a T-mesh */ |
208 |
> |
FILE *fp |
209 |
> |
) |
210 |
|
{ |
211 |
|
char *argv[MAXARG]; |
212 |
|
int argc; |
215 |
|
|
216 |
|
nstats = nunknown = 0; |
217 |
|
/* scan until EOF */ |
218 |
< |
while (argc = getstmt(argv, fp)) { |
218 |
> |
while ( (argc = getstmt(argv, fp)) ) { |
219 |
|
switch (argv[0][0]) { |
220 |
|
case 'v': /* vertex */ |
221 |
|
switch (argv[0][1]) { |
274 |
|
if (!strcmp(argv[1], "off")) |
275 |
|
mapname[0] = '\0'; |
276 |
|
else |
277 |
< |
sprintf(mapname, "%s.pic", argv[1]); |
277 |
> |
sprintf(mapname, "%s.hdr", argv[1]); |
278 |
|
} else |
279 |
|
goto unknown; |
280 |
|
break; |
309 |
|
|
310 |
|
|
311 |
|
int |
312 |
< |
getstmt(av, fp) /* read the next statement from fp */ |
313 |
< |
register char *av[MAXARG]; |
314 |
< |
FILE *fp; |
312 |
> |
getstmt( /* read the next statement from fp */ |
313 |
> |
register char *av[MAXARG], |
314 |
> |
FILE *fp |
315 |
> |
) |
316 |
|
{ |
317 |
< |
extern char *fgetline(); |
293 |
< |
static char sbuf[MAXARG*10]; |
317 |
> |
static char sbuf[MAXARG*16]; |
318 |
|
register char *cp; |
319 |
|
register int i; |
320 |
|
|
328 |
|
lineno++; |
329 |
|
*cp++ = '\0'; |
330 |
|
} |
331 |
< |
if (!*cp || i >= MAXARG-1) |
331 |
> |
if (!*cp) |
332 |
|
break; |
333 |
+ |
if (i >= MAXARG-1) { |
334 |
+ |
fprintf(stderr, |
335 |
+ |
"warning: line %d: too many arguments (limit %d)\n", |
336 |
+ |
lineno+1, MAXARG-1); |
337 |
+ |
break; |
338 |
+ |
} |
339 |
|
av[i++] = cp; |
340 |
|
while (*++cp && !isspace(*cp)) |
341 |
|
; |
349 |
|
|
350 |
|
|
351 |
|
char * |
352 |
< |
getmtl() /* figure material for this face */ |
352 |
> |
getmtl(void) /* figure material for this face */ |
353 |
|
{ |
354 |
|
register RULEHD *rp = ourmapping; |
355 |
|
|
375 |
|
|
376 |
|
|
377 |
|
char * |
378 |
< |
getonm() /* invent a good name for object */ |
378 |
> |
getonm(void) /* invent a good name for object */ |
379 |
|
{ |
380 |
|
static char name[64]; |
381 |
|
register char *cp1, *cp2; |
390 |
|
cp2 = group[i]; |
391 |
|
if (cp1 > name) |
392 |
|
*cp1++ = '.'; |
393 |
< |
while (*cp1 = *cp2++) |
393 |
> |
while ( (*cp1 = *cp2++) ) |
394 |
|
if (++cp1 >= name+sizeof(name)-2) { |
395 |
|
*cp1 = '\0'; |
396 |
|
return(name); |
400 |
|
} |
401 |
|
|
402 |
|
|
403 |
< |
matchrule(rp) /* check for a match on this rule */ |
404 |
< |
register RULEHD *rp; |
403 |
> |
int |
404 |
> |
matchrule( /* check for a match on this rule */ |
405 |
> |
register RULEHD *rp |
406 |
> |
) |
407 |
|
{ |
408 |
|
ID tmpid; |
409 |
|
int gotmatch; |
453 |
|
} |
454 |
|
|
455 |
|
|
456 |
< |
cvtndx(vi, vs) /* convert vertex string to index */ |
457 |
< |
register VNDX vi; |
458 |
< |
register char *vs; |
456 |
> |
int |
457 |
> |
cvtndx( /* convert vertex string to index */ |
458 |
> |
register VNDX vi, |
459 |
> |
register char *vs |
460 |
> |
) |
461 |
|
{ |
462 |
|
/* get point */ |
463 |
|
vi[0] = atoi(vs); |
502 |
|
} |
503 |
|
|
504 |
|
|
505 |
< |
nonplanar(ac, av) /* are vertices non-planar? */ |
506 |
< |
register int ac; |
507 |
< |
register char **av; |
505 |
> |
int |
506 |
> |
nonplanar( /* are vertices non-planar? */ |
507 |
> |
register int ac, |
508 |
> |
register char **av |
509 |
> |
) |
510 |
|
{ |
511 |
|
VNDX vi; |
512 |
< |
FLOAT *p0, *p1; |
512 |
> |
RREAL *p0, *p1; |
513 |
|
FVECT v1, v2, nsum, newn; |
514 |
|
double d; |
515 |
|
register int i; |
555 |
|
} |
556 |
|
|
557 |
|
|
558 |
< |
putface(ac, av) /* put out an N-sided polygon */ |
559 |
< |
int ac; |
560 |
< |
register char **av; |
558 |
> |
int |
559 |
> |
putface( /* put out an N-sided polygon */ |
560 |
> |
int ac, |
561 |
> |
register char **av |
562 |
> |
) |
563 |
|
{ |
564 |
|
VNDX vi; |
565 |
|
char *cp; |
578 |
|
return(1); |
579 |
|
} |
580 |
|
if ((cp = getmtl()) == NULL) |
581 |
< |
return(-1); |
581 |
> |
return(0); |
582 |
|
printf("\n%s polygon %s.%d\n", cp, getonm(), faceno); |
583 |
|
printf("0\n0\n%d\n", 3*ac); |
584 |
|
for (i = 0; i < ac; i++) { |
590 |
|
} |
591 |
|
|
592 |
|
|
593 |
< |
puttri(v1, v2, v3) /* put out a triangle */ |
594 |
< |
char *v1, *v2, *v3; |
593 |
> |
int |
594 |
> |
puttri( /* put out a triangle */ |
595 |
> |
char *v1, |
596 |
> |
char *v2, |
597 |
> |
char *v3 |
598 |
> |
) |
599 |
|
{ |
600 |
|
char *mod; |
601 |
|
VNDX v1i, v2i, v3i; |
602 |
|
BARYCCM bvecs; |
603 |
< |
FLOAT bcoor[3][3]; |
604 |
< |
int texOK, patOK; |
603 |
> |
RREAL bcoor[3][3]; |
604 |
> |
int texOK = 0, patOK; |
605 |
> |
int flatness; |
606 |
|
register int i; |
607 |
|
|
608 |
|
if ((mod = getmtl()) == NULL) |
609 |
< |
return(-1); |
609 |
> |
return(0); |
610 |
|
|
611 |
|
if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) |
612 |
|
return(0); |
613 |
|
/* compute barycentric coordinates */ |
614 |
< |
if (!flatten && v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0) |
615 |
< |
switch (flat_tri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]], |
616 |
< |
vnlist[v1i[2]], vnlist[v2i[2]], vnlist[v3i[2]])) { |
574 |
< |
case DEGEN: /* zero area */ |
575 |
< |
return(-1); |
576 |
< |
case RVFLAT: /* reversed normals, but flat */ |
577 |
< |
case ISFLAT: /* smoothing unnecessary */ |
578 |
< |
texOK = 0; |
579 |
< |
break; |
580 |
< |
case RVBENT: /* reversed normals with smoothing */ |
581 |
< |
case ISBENT: /* proper smoothing */ |
582 |
< |
texOK = 1; |
583 |
< |
break; |
584 |
< |
} |
614 |
> |
if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0) |
615 |
> |
flatness = flat_tri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]], |
616 |
> |
vnlist[v1i[2]], vnlist[v2i[2]], vnlist[v3i[2]]); |
617 |
|
else |
618 |
+ |
flatness = ISFLAT; |
619 |
+ |
|
620 |
+ |
switch (flatness) { |
621 |
+ |
case DEGEN: /* zero area */ |
622 |
+ |
ndegen++; |
623 |
+ |
return(-1); |
624 |
+ |
case RVFLAT: /* reversed normals, but flat */ |
625 |
+ |
case ISFLAT: /* smoothing unnecessary */ |
626 |
|
texOK = 0; |
627 |
+ |
break; |
628 |
+ |
case RVBENT: /* reversed normals with smoothing */ |
629 |
+ |
case ISBENT: /* proper smoothing */ |
630 |
+ |
texOK = 1; |
631 |
+ |
break; |
632 |
+ |
} |
633 |
+ |
if (flatten) |
634 |
+ |
texOK = 0; |
635 |
|
#ifdef TEXMAPS |
636 |
|
patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0); |
637 |
|
#else |
640 |
|
if (texOK | patOK) |
641 |
|
if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]], |
642 |
|
vlist[v3i[0]]) < 0) |
643 |
< |
return(-1); |
643 |
> |
texOK = patOK = 0; |
644 |
|
/* put out texture (if any) */ |
645 |
|
if (texOK) { |
646 |
|
printf("\n%s texfunc %s\n", mod, TEXNAME); |
669 |
|
put_baryc(&bvecs, bcoor, 2); |
670 |
|
} |
671 |
|
#endif |
672 |
< |
/* put out triangle */ |
672 |
> |
/* put out (reversed) triangle */ |
673 |
|
printf("\n%s polygon %s.%d\n", mod, getonm(), faceno); |
674 |
|
printf("0\n0\n9\n"); |
675 |
< |
pvect(vlist[v1i[0]]); |
676 |
< |
pvect(vlist[v2i[0]]); |
677 |
< |
pvect(vlist[v3i[0]]); |
678 |
< |
|
675 |
> |
if (flatness == RVFLAT || flatness == RVBENT) { |
676 |
> |
pvect(vlist[v3i[0]]); |
677 |
> |
pvect(vlist[v2i[0]]); |
678 |
> |
pvect(vlist[v1i[0]]); |
679 |
> |
} else { |
680 |
> |
pvect(vlist[v1i[0]]); |
681 |
> |
pvect(vlist[v2i[0]]); |
682 |
> |
pvect(vlist[v3i[0]]); |
683 |
> |
} |
684 |
|
return(1); |
685 |
|
} |
686 |
|
|
687 |
|
|
688 |
< |
freeverts() /* free all vertices */ |
688 |
> |
void |
689 |
> |
freeverts(void) /* free all vertices */ |
690 |
|
{ |
691 |
|
if (nvs) { |
692 |
< |
free((char *)vlist); |
692 |
> |
free((void *)vlist); |
693 |
|
nvs = 0; |
694 |
|
} |
695 |
|
if (nvts) { |
696 |
< |
free((char *)vtlist); |
696 |
> |
free((void *)vtlist); |
697 |
|
nvts = 0; |
698 |
|
} |
699 |
|
if (nvns) { |
700 |
< |
free((char *)vnlist); |
700 |
> |
free((void *)vnlist); |
701 |
|
nvns = 0; |
702 |
|
} |
703 |
|
} |
704 |
|
|
705 |
|
|
706 |
|
int |
707 |
< |
newv(x, y, z) /* create a new vertex */ |
708 |
< |
double x, y, z; |
707 |
> |
newv( /* create a new vertex */ |
708 |
> |
double x, |
709 |
> |
double y, |
710 |
> |
double z |
711 |
> |
) |
712 |
|
{ |
713 |
|
if (!(nvs%CHUNKSIZ)) { /* allocate next block */ |
714 |
|
if (nvs == 0) |
715 |
|
vlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT)); |
716 |
|
else |
717 |
< |
vlist = (FVECT *)realloc((char *)vlist, |
717 |
> |
vlist = (FVECT *)realloc((void *)vlist, |
718 |
|
(nvs+CHUNKSIZ)*sizeof(FVECT)); |
719 |
|
if (vlist == NULL) { |
720 |
|
fprintf(stderr, |
731 |
|
|
732 |
|
|
733 |
|
int |
734 |
< |
newvn(x, y, z) /* create a new vertex normal */ |
735 |
< |
double x, y, z; |
734 |
> |
newvn( /* create a new vertex normal */ |
735 |
> |
double x, |
736 |
> |
double y, |
737 |
> |
double z |
738 |
> |
) |
739 |
|
{ |
740 |
|
if (!(nvns%CHUNKSIZ)) { /* allocate next block */ |
741 |
|
if (nvns == 0) |
742 |
|
vnlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT)); |
743 |
|
else |
744 |
< |
vnlist = (FVECT *)realloc((char *)vnlist, |
744 |
> |
vnlist = (FVECT *)realloc((void *)vnlist, |
745 |
|
(nvns+CHUNKSIZ)*sizeof(FVECT)); |
746 |
|
if (vnlist == NULL) { |
747 |
|
fprintf(stderr, |
760 |
|
|
761 |
|
|
762 |
|
int |
763 |
< |
newvt(x, y) /* create a new texture map vertex */ |
764 |
< |
double x, y; |
763 |
> |
newvt( /* create a new texture map vertex */ |
764 |
> |
double x, |
765 |
> |
double y |
766 |
> |
) |
767 |
|
{ |
768 |
|
if (!(nvts%CHUNKSIZ)) { /* allocate next block */ |
769 |
|
if (nvts == 0) |
770 |
< |
vtlist = (FLOAT (*)[2])malloc(CHUNKSIZ*2*sizeof(FLOAT)); |
770 |
> |
vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL)); |
771 |
|
else |
772 |
< |
vtlist = (FLOAT (*)[2])realloc((char *)vtlist, |
773 |
< |
(nvts+CHUNKSIZ)*2*sizeof(FLOAT)); |
772 |
> |
vtlist = (RREAL (*)[2])realloc((void *)vtlist, |
773 |
> |
(nvts+CHUNKSIZ)*2*sizeof(RREAL)); |
774 |
|
if (vtlist == NULL) { |
775 |
|
fprintf(stderr, |
776 |
|
"Out of memory while allocating texture vertex %d\n", |
785 |
|
} |
786 |
|
|
787 |
|
|
788 |
< |
syntax(er) /* report syntax error and exit */ |
789 |
< |
char *er; |
788 |
> |
void |
789 |
> |
syntax( /* report syntax error and exit */ |
790 |
> |
char *er |
791 |
> |
) |
792 |
|
{ |
793 |
|
fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n", |
794 |
|
inpfile, lineno, er); |