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 |
|
* |
14 |
|
|
15 |
|
#include "trans.h" |
16 |
|
|
17 |
+ |
#include "tmesh.h" |
18 |
+ |
|
19 |
|
#include <ctype.h> |
20 |
|
|
22 |
– |
#define TCALNAME "tmesh.cal" /* triangle interp. file */ |
21 |
|
#define PATNAME "M-pat" /* mesh pattern name (reused) */ |
22 |
|
#define TEXNAME "M-nor" /* mesh texture name (reused) */ |
23 |
|
#define DEFOBJ "unnamed" /* default object name */ |
24 |
|
#define DEFMAT "white" /* default material name */ |
25 |
|
|
28 |
– |
#define ABS(x) ((x)>=0 ? (x) : -(x)) |
29 |
– |
|
26 |
|
#define pvect(v) printf("%18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2]) |
27 |
|
|
28 |
|
FVECT *vlist; /* our vertex list */ |
29 |
|
int nvs; /* number of vertices in our list */ |
30 |
|
FVECT *vnlist; /* vertex normal list */ |
31 |
|
int nvns; |
32 |
< |
FLOAT (*vtlist)[2]; /* map vertex list */ |
32 |
> |
RREAL (*vtlist)[2]; /* map vertex list */ |
33 |
|
int nvts; |
34 |
|
|
39 |
– |
typedef struct { |
40 |
– |
int ax; /* major axis */ |
41 |
– |
FLOAT tm[2][3]; /* transformation */ |
42 |
– |
} BARYCCM; |
43 |
– |
|
35 |
|
typedef int VNDX[3]; /* vertex index (point,map,normal) */ |
36 |
|
|
37 |
|
#define CHUNKSIZ 256 /* vertex allocation chunk size */ |
100 |
|
default: |
101 |
|
goto userr; |
102 |
|
} |
103 |
< |
if (i > argc | i < argc-1) |
103 |
> |
if ((i > argc) | (i < argc-1)) |
104 |
|
goto userr; |
105 |
|
if (i == argc) |
106 |
|
inpfile = "<stdin>"; |
137 |
|
ID tmpid; |
138 |
|
register int i; |
139 |
|
|
140 |
< |
while (argc = getstmt(argv, fp)) |
140 |
> |
while ( (argc = getstmt(argv, fp)) ) |
141 |
|
switch (argv[0][0]) { |
142 |
|
case 'f': /* face */ |
143 |
|
if (!argv[0][1]) |
188 |
|
|
189 |
|
nstats = nunknown = 0; |
190 |
|
/* scan until EOF */ |
191 |
< |
while (argc = getstmt(argv, fp)) { |
191 |
> |
while ( (argc = getstmt(argv, fp)) ) { |
192 |
|
switch (argv[0][0]) { |
193 |
|
case 'v': /* vertex */ |
194 |
|
switch (argv[0][1]) { |
357 |
|
cp2 = group[i]; |
358 |
|
if (cp1 > name) |
359 |
|
*cp1++ = '.'; |
360 |
< |
while (*cp1 = *cp2++) |
360 |
> |
while ( (*cp1 = *cp2++) ) |
361 |
|
if (++cp1 >= name+sizeof(name)-2) { |
362 |
|
*cp1 = '\0'; |
363 |
|
return(name); |
470 |
|
register char **av; |
471 |
|
{ |
472 |
|
VNDX vi; |
473 |
< |
FLOAT *p0, *p1; |
473 |
> |
RREAL *p0, *p1; |
474 |
|
FVECT v1, v2, nsum, newn; |
475 |
|
double d; |
476 |
|
register int i; |
555 |
|
char *mod; |
556 |
|
VNDX v1i, v2i, v3i; |
557 |
|
BARYCCM bvecs; |
558 |
< |
FLOAT bcoor[3][3]; |
558 |
> |
RREAL bcoor[3][3]; |
559 |
|
int texOK, patOK; |
560 |
+ |
int flatness; |
561 |
|
register int i; |
562 |
|
|
563 |
|
if ((mod = getmtl()) == NULL) |
566 |
|
if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) |
567 |
|
return(0); |
568 |
|
/* compute barycentric coordinates */ |
569 |
< |
texOK = !flatten && (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0); |
569 |
> |
if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0) |
570 |
> |
flatness = flat_tri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]], |
571 |
> |
vnlist[v1i[2]], vnlist[v2i[2]], vnlist[v3i[2]]); |
572 |
> |
else |
573 |
> |
flatness = ISFLAT; |
574 |
> |
|
575 |
> |
switch (flatness) { |
576 |
> |
case DEGEN: /* zero area */ |
577 |
> |
return(-1); |
578 |
> |
case RVFLAT: /* reversed normals, but flat */ |
579 |
> |
case ISFLAT: /* smoothing unnecessary */ |
580 |
> |
texOK = 0; |
581 |
> |
break; |
582 |
> |
case RVBENT: /* reversed normals with smoothing */ |
583 |
> |
case ISBENT: /* proper smoothing */ |
584 |
> |
texOK = 1; |
585 |
> |
break; |
586 |
> |
} |
587 |
> |
if (flatten) |
588 |
> |
texOK = 0; |
589 |
|
#ifdef TEXMAPS |
590 |
|
patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0); |
591 |
|
#else |
623 |
|
put_baryc(&bvecs, bcoor, 2); |
624 |
|
} |
625 |
|
#endif |
626 |
< |
/* put out triangle */ |
626 |
> |
/* put out (reversed) triangle */ |
627 |
|
printf("\n%s polygon %s.%d\n", mod, getonm(), faceno); |
628 |
|
printf("0\n0\n9\n"); |
629 |
< |
pvect(vlist[v1i[0]]); |
630 |
< |
pvect(vlist[v2i[0]]); |
631 |
< |
pvect(vlist[v3i[0]]); |
632 |
< |
|
629 |
> |
if (flatness == RVFLAT || flatness == RVBENT) { |
630 |
> |
pvect(vlist[v3i[0]]); |
631 |
> |
pvect(vlist[v2i[0]]); |
632 |
> |
pvect(vlist[v1i[0]]); |
633 |
> |
} else { |
634 |
> |
pvect(vlist[v1i[0]]); |
635 |
> |
pvect(vlist[v2i[0]]); |
636 |
> |
pvect(vlist[v3i[0]]); |
637 |
> |
} |
638 |
|
return(1); |
639 |
|
} |
640 |
|
|
641 |
|
|
626 |
– |
int |
627 |
– |
comp_baryc(bcm, v1, v2, v3) /* compute barycentric vectors */ |
628 |
– |
register BARYCCM *bcm; |
629 |
– |
FLOAT *v1, *v2, *v3; |
630 |
– |
{ |
631 |
– |
FLOAT *vt; |
632 |
– |
FVECT va, vab, vcb; |
633 |
– |
double d; |
634 |
– |
int ax0, ax1; |
635 |
– |
register int i, j; |
636 |
– |
/* compute major axis */ |
637 |
– |
for (i = 0; i < 3; i++) { |
638 |
– |
vab[i] = v1[i] - v2[i]; |
639 |
– |
vcb[i] = v3[i] - v2[i]; |
640 |
– |
} |
641 |
– |
fcross(va, vab, vcb); |
642 |
– |
bcm->ax = ABS(va[0]) > ABS(va[1]) ? 0 : 1; |
643 |
– |
bcm->ax = ABS(va[bcm->ax]) > ABS(va[2]) ? bcm->ax : 2; |
644 |
– |
ax0 = (bcm->ax + 1) % 3; |
645 |
– |
ax1 = (bcm->ax + 2) % 3; |
646 |
– |
for (j = 0; j < 2; j++) { |
647 |
– |
vab[0] = v1[ax0] - v2[ax0]; |
648 |
– |
vcb[0] = v3[ax0] - v2[ax0]; |
649 |
– |
vab[1] = v1[ax1] - v2[ax1]; |
650 |
– |
vcb[1] = v3[ax1] - v2[ax1]; |
651 |
– |
d = vcb[0]*vcb[0] + vcb[1]*vcb[1]; |
652 |
– |
if (d <= FTINY) |
653 |
– |
return(-1); |
654 |
– |
d = (vcb[0]*vab[0]+vcb[1]*vab[1])/d; |
655 |
– |
va[0] = vab[0] - vcb[0]*d; |
656 |
– |
va[1] = vab[1] - vcb[1]*d; |
657 |
– |
d = va[0]*va[0] + va[1]*va[1]; |
658 |
– |
if (d <= FTINY) |
659 |
– |
return(-1); |
660 |
– |
bcm->tm[j][0] = va[0] /= d; |
661 |
– |
bcm->tm[j][1] = va[1] /= d; |
662 |
– |
bcm->tm[j][2] = -(v2[ax0]*va[0]+v2[ax1]*va[1]); |
663 |
– |
/* rotate vertices */ |
664 |
– |
vt = v1; |
665 |
– |
v1 = v2; |
666 |
– |
v2 = v3; |
667 |
– |
v3 = vt; |
668 |
– |
} |
669 |
– |
return(0); |
670 |
– |
} |
671 |
– |
|
672 |
– |
|
673 |
– |
put_baryc(bcm, com, n) /* put barycentric coord. vectors */ |
674 |
– |
register BARYCCM *bcm; |
675 |
– |
register FLOAT com[][3]; |
676 |
– |
int n; |
677 |
– |
{ |
678 |
– |
double a, b; |
679 |
– |
register int i, j; |
680 |
– |
|
681 |
– |
printf("%d\t%d\n", 1+3*n, bcm->ax); |
682 |
– |
for (i = 0; i < n; i++) { |
683 |
– |
a = com[i][0] - com[i][2]; |
684 |
– |
b = com[i][1] - com[i][2]; |
685 |
– |
printf("%14.8f %14.8f %14.8f\n", |
686 |
– |
bcm->tm[0][0]*a + bcm->tm[1][0]*b, |
687 |
– |
bcm->tm[0][1]*a + bcm->tm[1][1]*b, |
688 |
– |
bcm->tm[0][2]*a + bcm->tm[1][2]*b + com[i][2]); |
689 |
– |
} |
690 |
– |
} |
691 |
– |
|
692 |
– |
|
642 |
|
freeverts() /* free all vertices */ |
643 |
|
{ |
644 |
|
if (nvs) { |
645 |
< |
free((char *)vlist); |
645 |
> |
free((void *)vlist); |
646 |
|
nvs = 0; |
647 |
|
} |
648 |
|
if (nvts) { |
649 |
< |
free((char *)vtlist); |
649 |
> |
free((void *)vtlist); |
650 |
|
nvts = 0; |
651 |
|
} |
652 |
|
if (nvns) { |
653 |
< |
free((char *)vnlist); |
653 |
> |
free((void *)vnlist); |
654 |
|
nvns = 0; |
655 |
|
} |
656 |
|
} |
664 |
|
if (nvs == 0) |
665 |
|
vlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT)); |
666 |
|
else |
667 |
< |
vlist = (FVECT *)realloc((char *)vlist, |
667 |
> |
vlist = (FVECT *)realloc((void *)vlist, |
668 |
|
(nvs+CHUNKSIZ)*sizeof(FVECT)); |
669 |
|
if (vlist == NULL) { |
670 |
|
fprintf(stderr, |
688 |
|
if (nvns == 0) |
689 |
|
vnlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT)); |
690 |
|
else |
691 |
< |
vnlist = (FVECT *)realloc((char *)vnlist, |
691 |
> |
vnlist = (FVECT *)realloc((void *)vnlist, |
692 |
|
(nvns+CHUNKSIZ)*sizeof(FVECT)); |
693 |
|
if (vnlist == NULL) { |
694 |
|
fprintf(stderr, |
712 |
|
{ |
713 |
|
if (!(nvts%CHUNKSIZ)) { /* allocate next block */ |
714 |
|
if (nvts == 0) |
715 |
< |
vtlist = (FLOAT (*)[2])malloc(CHUNKSIZ*2*sizeof(FLOAT)); |
715 |
> |
vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL)); |
716 |
|
else |
717 |
< |
vtlist = (FLOAT (*)[2])realloc((char *)vtlist, |
718 |
< |
(nvts+CHUNKSIZ)*2*sizeof(FLOAT)); |
717 |
> |
vtlist = (RREAL (*)[2])realloc((void *)vtlist, |
718 |
> |
(nvts+CHUNKSIZ)*2*sizeof(RREAL)); |
719 |
|
if (vtlist == NULL) { |
720 |
|
fprintf(stderr, |
721 |
|
"Out of memory while allocating texture vertex %d\n", |