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" |
21 |
+ |
#include "tmesh.h" |
22 |
|
|
20 |
– |
#include <ctype.h> |
23 |
|
|
22 |
– |
#define TCALNAME "tmesh.cal" /* triangle interp. file */ |
23 |
– |
#define QCALNAME "surf.cal" /* quad interp. file */ |
24 |
|
#define PATNAME "M-pat" /* mesh pattern name (reused) */ |
25 |
|
#define TEXNAME "M-nor" /* mesh texture name (reused) */ |
26 |
|
#define DEFOBJ "unnamed" /* default object name */ |
27 |
|
#define DEFMAT "white" /* default material name */ |
28 |
|
|
29 |
– |
#define ABS(x) ((x)>=0 ? (x) : -(x)) |
30 |
– |
|
29 |
|
#define pvect(v) printf("%18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2]) |
30 |
|
|
31 |
|
FVECT *vlist; /* our vertex list */ |
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 |
< |
typedef FLOAT BARYCCM[3][4]; /* barycentric coordinate system */ |
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 |
68 |
|
char *defmat = DEFMAT; /* default (starting) material name */ |
69 |
|
char *defobj = DEFOBJ; /* default (starting) object name */ |
70 |
|
|
71 |
< |
char *getmtl(), *getonm(); |
71 |
> |
int flatten = 0; /* discard surface normal information */ |
72 |
|
|
73 |
|
char mapname[128]; /* current picture file */ |
74 |
|
char matname[64]; /* current material name */ |
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; |
115 |
|
case 'm': /* use custom mapfile */ |
116 |
|
ourmapping = getmapping(argv[++i], &qlist); |
117 |
|
break; |
118 |
+ |
case 'f': /* flatten surfaces */ |
119 |
+ |
flatten++; |
120 |
+ |
break; |
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] [file.obj]\n", |
149 |
> |
fprintf(stderr, "Usage: %s [-o obj][-m mapping][-n][-f] [file.obj]\n", |
150 |
|
argv[0]); |
151 |
|
exit(1); |
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]) { |
257 |
|
if (!puttri(argv[1], argv[2], argv[3])) |
258 |
|
syntax("Bad triangle"); |
259 |
|
break; |
235 |
– |
case 4: |
236 |
– |
if (!putquad(argv[1], argv[2], |
237 |
– |
argv[3], argv[4])) |
238 |
– |
syntax("Bad quad"); |
239 |
– |
break; |
260 |
|
default: |
261 |
|
if (!putface(argc-1, argv+1)) |
262 |
|
syntax("Bad face"); |
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(); |
297 |
< |
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); |
465 |
|
if (vi[0]-- > nvs) |
466 |
|
return(0); |
467 |
|
} else if (vi[0] < 0) { |
468 |
< |
vi[0] = nvs + vi[0]; |
468 |
> |
vi[0] += nvs; |
469 |
|
if (vi[0] < 0) |
470 |
|
return(0); |
471 |
|
} else |
479 |
|
if (vi[1]-- > nvts) |
480 |
|
return(0); |
481 |
|
} else if (vi[1] < 0) { |
482 |
< |
vi[1] = nvts + vi[1]; |
482 |
> |
vi[1] += nvts; |
483 |
|
if (vi[1] < 0) |
484 |
|
return(0); |
485 |
|
} else |
493 |
|
if (vi[2]-- > nvns) |
494 |
|
return(0); |
495 |
|
} else if (vi[2] < 0) { |
496 |
< |
vi[2] = nvns + vi[2]; |
496 |
> |
vi[2] += nvns; |
497 |
|
if (vi[2] < 0) |
498 |
|
return(0); |
499 |
|
} else |
502 |
|
} |
503 |
|
|
504 |
|
|
505 |
< |
nonplanar(ac, av) /* are vertices are 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; |
516 |
|
|
517 |
|
if (!cvtndx(vi, av[0])) |
518 |
|
return(0); |
519 |
< |
if (vi[2] >= 0) |
519 |
> |
if (!flatten && vi[2] >= 0) |
520 |
|
return(1); /* has interpolated normals */ |
521 |
|
if (ac < 4) |
522 |
|
return(0); /* it's a triangle! */ |
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; |
566 |
|
register int i; |
567 |
|
|
568 |
< |
if (nonplanar(ac, av)) { /* break into quads and triangles */ |
569 |
< |
while (ac > 3) { |
570 |
< |
if (!putquad(av[0], av[1], av[2], av[3])) |
568 |
> |
if (nonplanar(ac, av)) { /* break into triangles */ |
569 |
> |
while (ac > 2) { |
570 |
> |
if (!puttri(av[0], av[1], av[2])) |
571 |
|
return(0); |
572 |
< |
ac -= 2; /* remove two vertices & rotate */ |
572 |
> |
ac--; /* remove vertex & rotate */ |
573 |
|
cp = av[0]; |
574 |
|
for (i = 0; i < ac-1; i++) |
575 |
< |
av[i] = av[i+3]; |
575 |
> |
av[i] = av[i+2]; |
576 |
|
av[i] = cp; |
577 |
|
} |
544 |
– |
if (ac == 3 && !puttri(av[0], av[1], av[2])) |
545 |
– |
return(0); |
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 |
< |
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 |
< |
texOK = (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0); |
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 |
638 |
|
patOK = 0; |
639 |
|
#endif |
640 |
|
if (texOK | patOK) |
641 |
< |
if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]], |
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); |
647 |
|
mod = TEXNAME; |
648 |
|
printf("4 dx dy dz %s\n", TCALNAME); |
649 |
< |
printf("0\n21\n"); |
650 |
< |
put_baryc(bvecs); |
651 |
< |
printf("\t%14.12g %14.12g %14.12g\n", |
652 |
< |
vnlist[v1i[2]][0], vnlist[v2i[2]][0], |
653 |
< |
vnlist[v3i[2]][0]); |
654 |
< |
printf("\t%14.12g %14.12g %14.12g\n", |
655 |
< |
vnlist[v1i[2]][1], vnlist[v2i[2]][1], |
597 |
< |
vnlist[v3i[2]][1]); |
598 |
< |
printf("\t%14.12g %14.12g %14.12g\n", |
599 |
< |
vnlist[v1i[2]][2], vnlist[v2i[2]][2], |
600 |
< |
vnlist[v3i[2]][2]); |
649 |
> |
printf("0\n"); |
650 |
> |
for (i = 0; i < 3; i++) { |
651 |
> |
bcoor[i][0] = vnlist[v1i[2]][i]; |
652 |
> |
bcoor[i][1] = vnlist[v2i[2]][i]; |
653 |
> |
bcoor[i][2] = vnlist[v3i[2]][i]; |
654 |
> |
} |
655 |
> |
put_baryc(&bvecs, bcoor, 3); |
656 |
|
} |
657 |
|
#ifdef TEXMAPS |
658 |
|
/* put out pattern (if any) */ |
660 |
|
printf("\n%s colorpict %s\n", mod, PATNAME); |
661 |
|
mod = PATNAME; |
662 |
|
printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME); |
663 |
< |
printf("0\n18\n"); |
664 |
< |
put_baryc(bvecs); |
665 |
< |
printf("\t%f %f %f\n", vtlist[v1i[1]][0], |
666 |
< |
vtlist[v2i[1]][0], vtlist[v3i[1]][0]); |
667 |
< |
printf("\t%f %f %f\n", vtlist[v1i[1]][1], |
668 |
< |
vtlist[v2i[1]][1], vtlist[v3i[1]][1]); |
663 |
> |
printf("0\n"); |
664 |
> |
for (i = 0; i < 2; i++) { |
665 |
> |
bcoor[i][0] = vtlist[v1i[1]][i]; |
666 |
> |
bcoor[i][1] = vtlist[v2i[1]][i]; |
667 |
> |
bcoor[i][2] = vtlist[v3i[1]][i]; |
668 |
> |
} |
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 |
< |
|
679 |
< |
return(1); |
680 |
< |
} |
681 |
< |
|
682 |
< |
|
627 |
< |
int |
628 |
< |
comp_baryc(bcm, v1, v2, v3) /* compute barycentric vectors */ |
629 |
< |
register BARYCCM bcm; |
630 |
< |
FLOAT *v1, *v2, *v3; |
631 |
< |
{ |
632 |
< |
FLOAT *vt; |
633 |
< |
FVECT va, vab, vcb; |
634 |
< |
double d; |
635 |
< |
register int i, j; |
636 |
< |
|
637 |
< |
for (j = 0; j < 3; j++) { |
638 |
< |
for (i = 0; i < 3; i++) { |
639 |
< |
vab[i] = v1[i] - v2[i]; |
640 |
< |
vcb[i] = v3[i] - v2[i]; |
641 |
< |
} |
642 |
< |
d = DOT(vcb,vcb); |
643 |
< |
if (d <= FTINY) |
644 |
< |
return(-1); |
645 |
< |
d = DOT(vcb,vab)/d; |
646 |
< |
for (i = 0; i < 3; i++) |
647 |
< |
va[i] = vab[i] - vcb[i]*d; |
648 |
< |
d = DOT(va,va); |
649 |
< |
if (d <= FTINY) |
650 |
< |
return(-1); |
651 |
< |
for (i = 0; i < 3; i++) { |
652 |
< |
va[i] /= d; |
653 |
< |
bcm[j][i] = va[i]; |
654 |
< |
} |
655 |
< |
bcm[j][3] = -DOT(v2,va); |
656 |
< |
/* rotate vertices */ |
657 |
< |
vt = v1; |
658 |
< |
v1 = v2; |
659 |
< |
v2 = v3; |
660 |
< |
v3 = vt; |
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 |
|
} |
662 |
– |
return(0); |
663 |
– |
} |
664 |
– |
|
665 |
– |
|
666 |
– |
put_baryc(bcm) /* put barycentric coord. vectors */ |
667 |
– |
register BARYCCM bcm; |
668 |
– |
{ |
669 |
– |
register int i; |
670 |
– |
|
671 |
– |
for (i = 0; i < 3; i++) |
672 |
– |
printf("%14.8f %14.8f %14.8f %14.8f\n", |
673 |
– |
bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]); |
674 |
– |
} |
675 |
– |
|
676 |
– |
|
677 |
– |
putquad(p0, p1, p3, p2) /* put out a quadrilateral */ |
678 |
– |
char *p0, *p1, *p3, *p2; /* names correspond to binary pos. */ |
679 |
– |
{ |
680 |
– |
VNDX p0i, p1i, p2i, p3i; |
681 |
– |
FVECT norm[4]; |
682 |
– |
char *mod, *name; |
683 |
– |
int axis; |
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(); |
695 |
– |
/* get actual indices */ |
696 |
– |
if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) || |
697 |
– |
!cvtndx(p2i,p2) || !cvtndx(p3i,p3)) |
698 |
– |
return(0); |
699 |
– |
/* compute exact normals */ |
700 |
– |
fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0); |
701 |
– |
fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0); |
702 |
– |
fcross(vc1, v1, v2); |
703 |
– |
ok1 = normalize(vc1) != 0.0; |
704 |
– |
fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0); |
705 |
– |
fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0); |
706 |
– |
fcross(vc2, v1, v2); |
707 |
– |
ok2 = normalize(vc2) != 0.0; |
708 |
– |
if (!(ok1 | ok2)) |
709 |
– |
return(-1); |
710 |
– |
/* compute normal interpolation */ |
711 |
– |
axis = norminterp(norm, p0i, p1i, p2i, p3i); |
712 |
– |
|
713 |
– |
/* put out quadrilateral? */ |
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); |
718 |
– |
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
719 |
– |
printf("0\n13\t%d\n", axis); |
720 |
– |
pvect(norm[0]); |
721 |
– |
pvect(norm[1]); |
722 |
– |
pvect(norm[2]); |
723 |
– |
fvsum(v1, norm[3], vc1, -0.5); |
724 |
– |
fvsum(v1, v1, vc2, -0.5); |
725 |
– |
pvect(v1); |
726 |
– |
printf("\n%s ", TEXNAME); |
727 |
– |
} |
728 |
– |
printf("polygon %s.%d\n", name, faceno); |
729 |
– |
printf("0\n0\n12\n"); |
730 |
– |
pvect(vlist[p0i[0]]); |
731 |
– |
pvect(vlist[p1i[0]]); |
732 |
– |
pvect(vlist[p3i[0]]); |
733 |
– |
pvect(vlist[p2i[0]]); |
734 |
– |
return(1); |
735 |
– |
} |
736 |
– |
/* put out triangles? */ |
737 |
– |
if (ok1) { |
738 |
– |
printf("\n%s ", mod); |
739 |
– |
if (axis != -1) { |
740 |
– |
printf("texfunc %s\n", TEXNAME); |
741 |
– |
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
742 |
– |
printf("0\n13\t%d\n", axis); |
743 |
– |
pvect(norm[0]); |
744 |
– |
pvect(norm[1]); |
745 |
– |
pvect(norm[2]); |
746 |
– |
fvsum(v1, norm[3], vc1, -1.0); |
747 |
– |
pvect(v1); |
748 |
– |
printf("\n%s ", TEXNAME); |
749 |
– |
} |
750 |
– |
printf("polygon %s.%da\n", name, faceno); |
751 |
– |
printf("0\n0\n9\n"); |
752 |
– |
pvect(vlist[p0i[0]]); |
753 |
– |
pvect(vlist[p1i[0]]); |
754 |
– |
pvect(vlist[p2i[0]]); |
755 |
– |
} |
756 |
– |
if (ok2) { |
757 |
– |
printf("\n%s ", mod); |
758 |
– |
if (axis != -1) { |
759 |
– |
printf("texfunc %s\n", TEXNAME); |
760 |
– |
printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); |
761 |
– |
printf("0\n13\t%d\n", axis); |
762 |
– |
pvect(norm[0]); |
763 |
– |
pvect(norm[1]); |
764 |
– |
pvect(norm[2]); |
765 |
– |
fvsum(v2, norm[3], vc2, -1.0); |
766 |
– |
pvect(v2); |
767 |
– |
printf("\n%s ", TEXNAME); |
768 |
– |
} |
769 |
– |
printf("polygon %s.%db\n", name, faceno); |
770 |
– |
printf("0\n0\n9\n"); |
771 |
– |
pvect(vlist[p2i[0]]); |
772 |
– |
pvect(vlist[p1i[0]]); |
773 |
– |
pvect(vlist[p3i[0]]); |
774 |
– |
} |
684 |
|
return(1); |
685 |
|
} |
686 |
|
|
687 |
|
|
688 |
< |
int |
689 |
< |
norminterp(resmat, p0i, p1i, p2i, p3i) /* compute normal interpolation */ |
781 |
< |
register FVECT resmat[4]; |
782 |
< |
register VNDX p0i, p1i, p2i, p3i; |
688 |
> |
void |
689 |
> |
freeverts(void) /* free all vertices */ |
690 |
|
{ |
784 |
– |
#define u ((ax+1)%3) |
785 |
– |
#define v ((ax+2)%3) |
786 |
– |
|
787 |
– |
register int ax; |
788 |
– |
MAT4 eqnmat; |
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 */ |
798 |
– |
VCOPY(v1, vnlist[p0i[2]]); |
799 |
– |
fvsum(v1, v1, vnlist[p1i[2]], 1.0); |
800 |
– |
fvsum(v1, v1, vnlist[p2i[2]], 1.0); |
801 |
– |
fvsum(v1, v1, vnlist[p3i[2]], 1.0); |
802 |
– |
ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1; |
803 |
– |
ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2; |
804 |
– |
/* assign equation matrix */ |
805 |
– |
eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v]; |
806 |
– |
eqnmat[0][1] = vlist[p0i[0]][u]; |
807 |
– |
eqnmat[0][2] = vlist[p0i[0]][v]; |
808 |
– |
eqnmat[0][3] = 1.0; |
809 |
– |
eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v]; |
810 |
– |
eqnmat[1][1] = vlist[p1i[0]][u]; |
811 |
– |
eqnmat[1][2] = vlist[p1i[0]][v]; |
812 |
– |
eqnmat[1][3] = 1.0; |
813 |
– |
eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v]; |
814 |
– |
eqnmat[2][1] = vlist[p2i[0]][u]; |
815 |
– |
eqnmat[2][2] = vlist[p2i[0]][v]; |
816 |
– |
eqnmat[2][3] = 1.0; |
817 |
– |
eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v]; |
818 |
– |
eqnmat[3][1] = vlist[p3i[0]][u]; |
819 |
– |
eqnmat[3][2] = vlist[p3i[0]][v]; |
820 |
– |
eqnmat[3][3] = 1.0; |
821 |
– |
/* invert matrix (solve system) */ |
822 |
– |
if (!invmat4(eqnmat, eqnmat)) |
823 |
– |
return(-1); /* no solution */ |
824 |
– |
/* compute result matrix */ |
825 |
– |
for (j = 0; j < 4; j++) |
826 |
– |
for (i = 0; i < 3; i++) |
827 |
– |
resmat[j][i] = eqnmat[j][0]*vnlist[p0i[2]][i] + |
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 |
837 |
– |
#undef v |
838 |
– |
} |
839 |
– |
|
840 |
– |
|
841 |
– |
freeverts() /* free all vertices */ |
842 |
– |
{ |
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); |