| 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 */ |
| 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 |
|
|
| 28 |
– |
#define ABS(x) ((x)>=0 ? (x) : -(x)) |
| 29 |
– |
|
| 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 |
|
|
| 39 |
– |
typedef struct { |
| 40 |
– |
int ax; /* major axis */ |
| 41 |
– |
FLOAT tm[2][3]; /* transformation */ |
| 42 |
– |
} BARYCCM; |
| 43 |
– |
|
| 38 |
|
typedef int VNDX[3]; /* vertex index (point,map,normal) */ |
| 39 |
|
|
| 40 |
|
#define CHUNKSIZ 256 /* vertex allocation chunk size */ |
| 68 |
|
|
| 69 |
|
int flatten = 0; /* discard surface normal information */ |
| 70 |
|
|
| 77 |
– |
char *getmtl(), *getonm(); |
| 78 |
– |
|
| 71 |
|
char mapname[128]; /* current picture file */ |
| 72 |
|
char matname[64]; /* current material name */ |
| 73 |
|
char group[16][32]; /* current group names */ |
| 76 |
|
int lineno; /* current line number */ |
| 77 |
|
int faceno; /* current face number */ |
| 78 |
|
|
| 79 |
+ |
static void getnames(FILE *fp); |
| 80 |
+ |
static void convert(FILE *fp); |
| 81 |
+ |
static int getstmt(char *av[MAXARG], FILE *fp); |
| 82 |
+ |
static char * getmtl(void); |
| 83 |
+ |
static char * getonm(void); |
| 84 |
+ |
static int matchrule(RULEHD *rp); |
| 85 |
+ |
static int cvtndx(VNDX vi, char *vs); |
| 86 |
+ |
static int nonplanar(int ac, char **av); |
| 87 |
+ |
static int putface(int ac, char **av); |
| 88 |
+ |
static int puttri(char *v1, char *v2, char *v3); |
| 89 |
+ |
static void freeverts(void); |
| 90 |
+ |
static int newv(double x, double y, double z); |
| 91 |
+ |
static int newvn(double x, double y, double z); |
| 92 |
+ |
static int newvt(double x, double y); |
| 93 |
+ |
static void syntax(char *er); |
| 94 |
|
|
| 95 |
< |
main(argc, argv) /* read in .obj file and convert */ |
| 96 |
< |
int argc; |
| 97 |
< |
char *argv[]; |
| 95 |
> |
|
| 96 |
> |
int |
| 97 |
> |
main( /* read in .obj file and convert */ |
| 98 |
> |
int argc, |
| 99 |
> |
char *argv[] |
| 100 |
> |
) |
| 101 |
|
{ |
| 102 |
|
int donames = 0; |
| 103 |
|
int i; |
| 119 |
|
default: |
| 120 |
|
goto userr; |
| 121 |
|
} |
| 122 |
< |
if (i > argc | i < argc-1) |
| 122 |
> |
if ((i > argc) | (i < argc-1)) |
| 123 |
|
goto userr; |
| 124 |
|
if (i == argc) |
| 125 |
|
inpfile = "<stdin>"; |
| 148 |
|
} |
| 149 |
|
|
| 150 |
|
|
| 151 |
< |
getnames(fp) /* get valid qualifier names */ |
| 152 |
< |
FILE *fp; |
| 151 |
> |
void |
| 152 |
> |
getnames( /* get valid qualifier names */ |
| 153 |
> |
FILE *fp |
| 154 |
> |
) |
| 155 |
|
{ |
| 156 |
|
char *argv[MAXARG]; |
| 157 |
|
int argc; |
| 158 |
|
ID tmpid; |
| 159 |
|
register int i; |
| 160 |
|
|
| 161 |
< |
while (argc = getstmt(argv, fp)) |
| 161 |
> |
while ( (argc = getstmt(argv, fp)) ) |
| 162 |
|
switch (argv[0][0]) { |
| 163 |
|
case 'f': /* face */ |
| 164 |
|
if (!argv[0][1]) |
| 199 |
|
} |
| 200 |
|
|
| 201 |
|
|
| 202 |
< |
convert(fp) /* convert a T-mesh */ |
| 203 |
< |
FILE *fp; |
| 202 |
> |
void |
| 203 |
> |
convert( /* convert a T-mesh */ |
| 204 |
> |
FILE *fp |
| 205 |
> |
) |
| 206 |
|
{ |
| 207 |
|
char *argv[MAXARG]; |
| 208 |
|
int argc; |
| 211 |
|
|
| 212 |
|
nstats = nunknown = 0; |
| 213 |
|
/* scan until EOF */ |
| 214 |
< |
while (argc = getstmt(argv, fp)) { |
| 214 |
> |
while ( (argc = getstmt(argv, fp)) ) { |
| 215 |
|
switch (argv[0][0]) { |
| 216 |
|
case 'v': /* vertex */ |
| 217 |
|
switch (argv[0][1]) { |
| 305 |
|
|
| 306 |
|
|
| 307 |
|
int |
| 308 |
< |
getstmt(av, fp) /* read the next statement from fp */ |
| 309 |
< |
register char *av[MAXARG]; |
| 310 |
< |
FILE *fp; |
| 308 |
> |
getstmt( /* read the next statement from fp */ |
| 309 |
> |
register char *av[MAXARG], |
| 310 |
> |
FILE *fp |
| 311 |
> |
) |
| 312 |
|
{ |
| 313 |
|
extern char *fgetline(); |
| 314 |
|
static char sbuf[MAXARG*10]; |
| 340 |
|
|
| 341 |
|
|
| 342 |
|
char * |
| 343 |
< |
getmtl() /* figure material for this face */ |
| 343 |
> |
getmtl(void) /* figure material for this face */ |
| 344 |
|
{ |
| 345 |
|
register RULEHD *rp = ourmapping; |
| 346 |
|
|
| 366 |
|
|
| 367 |
|
|
| 368 |
|
char * |
| 369 |
< |
getonm() /* invent a good name for object */ |
| 369 |
> |
getonm(void) /* invent a good name for object */ |
| 370 |
|
{ |
| 371 |
|
static char name[64]; |
| 372 |
|
register char *cp1, *cp2; |
| 381 |
|
cp2 = group[i]; |
| 382 |
|
if (cp1 > name) |
| 383 |
|
*cp1++ = '.'; |
| 384 |
< |
while (*cp1 = *cp2++) |
| 384 |
> |
while ( (*cp1 = *cp2++) ) |
| 385 |
|
if (++cp1 >= name+sizeof(name)-2) { |
| 386 |
|
*cp1 = '\0'; |
| 387 |
|
return(name); |
| 391 |
|
} |
| 392 |
|
|
| 393 |
|
|
| 394 |
< |
matchrule(rp) /* check for a match on this rule */ |
| 395 |
< |
register RULEHD *rp; |
| 394 |
> |
int |
| 395 |
> |
matchrule( /* check for a match on this rule */ |
| 396 |
> |
register RULEHD *rp |
| 397 |
> |
) |
| 398 |
|
{ |
| 399 |
|
ID tmpid; |
| 400 |
|
int gotmatch; |
| 444 |
|
} |
| 445 |
|
|
| 446 |
|
|
| 447 |
< |
cvtndx(vi, vs) /* convert vertex string to index */ |
| 448 |
< |
register VNDX vi; |
| 449 |
< |
register char *vs; |
| 447 |
> |
int |
| 448 |
> |
cvtndx( /* convert vertex string to index */ |
| 449 |
> |
register VNDX vi, |
| 450 |
> |
register char *vs |
| 451 |
> |
) |
| 452 |
|
{ |
| 453 |
|
/* get point */ |
| 454 |
|
vi[0] = atoi(vs); |
| 493 |
|
} |
| 494 |
|
|
| 495 |
|
|
| 496 |
< |
nonplanar(ac, av) /* are vertices non-planar? */ |
| 497 |
< |
register int ac; |
| 498 |
< |
register char **av; |
| 496 |
> |
int |
| 497 |
> |
nonplanar( /* are vertices non-planar? */ |
| 498 |
> |
register int ac, |
| 499 |
> |
register char **av |
| 500 |
> |
) |
| 501 |
|
{ |
| 502 |
|
VNDX vi; |
| 503 |
< |
FLOAT *p0, *p1; |
| 503 |
> |
RREAL *p0, *p1; |
| 504 |
|
FVECT v1, v2, nsum, newn; |
| 505 |
|
double d; |
| 506 |
|
register int i; |
| 546 |
|
} |
| 547 |
|
|
| 548 |
|
|
| 549 |
< |
putface(ac, av) /* put out an N-sided polygon */ |
| 550 |
< |
int ac; |
| 551 |
< |
register char **av; |
| 549 |
> |
int |
| 550 |
> |
putface( /* put out an N-sided polygon */ |
| 551 |
> |
int ac, |
| 552 |
> |
register char **av |
| 553 |
> |
) |
| 554 |
|
{ |
| 555 |
|
VNDX vi; |
| 556 |
|
char *cp; |
| 581 |
|
} |
| 582 |
|
|
| 583 |
|
|
| 584 |
< |
puttri(v1, v2, v3) /* put out a triangle */ |
| 585 |
< |
char *v1, *v2, *v3; |
| 584 |
> |
int |
| 585 |
> |
puttri( /* put out a triangle */ |
| 586 |
> |
char *v1, |
| 587 |
> |
char *v2, |
| 588 |
> |
char *v3 |
| 589 |
> |
) |
| 590 |
|
{ |
| 591 |
|
char *mod; |
| 592 |
|
VNDX v1i, v2i, v3i; |
| 593 |
|
BARYCCM bvecs; |
| 594 |
< |
FLOAT bcoor[3][3]; |
| 595 |
< |
int texOK, patOK; |
| 594 |
> |
RREAL bcoor[3][3]; |
| 595 |
> |
int texOK = 0, patOK; |
| 596 |
> |
int flatness; |
| 597 |
|
register int i; |
| 598 |
|
|
| 599 |
|
if ((mod = getmtl()) == NULL) |
| 602 |
|
if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) |
| 603 |
|
return(0); |
| 604 |
|
/* compute barycentric coordinates */ |
| 605 |
< |
texOK = !flatten && (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0); |
| 605 |
> |
if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0) |
| 606 |
> |
flatness = flat_tri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]], |
| 607 |
> |
vnlist[v1i[2]], vnlist[v2i[2]], vnlist[v3i[2]]); |
| 608 |
> |
else |
| 609 |
> |
flatness = ISFLAT; |
| 610 |
> |
|
| 611 |
> |
switch (flatness) { |
| 612 |
> |
case DEGEN: /* zero area */ |
| 613 |
> |
return(-1); |
| 614 |
> |
case RVFLAT: /* reversed normals, but flat */ |
| 615 |
> |
case ISFLAT: /* smoothing unnecessary */ |
| 616 |
> |
texOK = 0; |
| 617 |
> |
break; |
| 618 |
> |
case RVBENT: /* reversed normals with smoothing */ |
| 619 |
> |
case ISBENT: /* proper smoothing */ |
| 620 |
> |
texOK = 1; |
| 621 |
> |
break; |
| 622 |
> |
} |
| 623 |
> |
if (flatten) |
| 624 |
> |
texOK = 0; |
| 625 |
|
#ifdef TEXMAPS |
| 626 |
|
patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0); |
| 627 |
|
#else |
| 659 |
|
put_baryc(&bvecs, bcoor, 2); |
| 660 |
|
} |
| 661 |
|
#endif |
| 662 |
< |
/* put out triangle */ |
| 662 |
> |
/* put out (reversed) triangle */ |
| 663 |
|
printf("\n%s polygon %s.%d\n", mod, getonm(), faceno); |
| 664 |
|
printf("0\n0\n9\n"); |
| 665 |
< |
pvect(vlist[v1i[0]]); |
| 666 |
< |
pvect(vlist[v2i[0]]); |
| 667 |
< |
pvect(vlist[v3i[0]]); |
| 668 |
< |
|
| 665 |
> |
if (flatness == RVFLAT || flatness == RVBENT) { |
| 666 |
> |
pvect(vlist[v3i[0]]); |
| 667 |
> |
pvect(vlist[v2i[0]]); |
| 668 |
> |
pvect(vlist[v1i[0]]); |
| 669 |
> |
} else { |
| 670 |
> |
pvect(vlist[v1i[0]]); |
| 671 |
> |
pvect(vlist[v2i[0]]); |
| 672 |
> |
pvect(vlist[v3i[0]]); |
| 673 |
> |
} |
| 674 |
|
return(1); |
| 675 |
|
} |
| 676 |
|
|
| 677 |
|
|
| 678 |
< |
int |
| 679 |
< |
comp_baryc(bcm, v1, v2, v3) /* compute barycentric vectors */ |
| 628 |
< |
register BARYCCM *bcm; |
| 629 |
< |
FLOAT *v1, *v2, *v3; |
| 678 |
> |
void |
| 679 |
> |
freeverts(void) /* free all vertices */ |
| 680 |
|
{ |
| 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 |
– |
|
| 693 |
– |
freeverts() /* free all vertices */ |
| 694 |
– |
{ |
| 681 |
|
if (nvs) { |
| 682 |
< |
free((char *)vlist); |
| 682 |
> |
free((void *)vlist); |
| 683 |
|
nvs = 0; |
| 684 |
|
} |
| 685 |
|
if (nvts) { |
| 686 |
< |
free((char *)vtlist); |
| 686 |
> |
free((void *)vtlist); |
| 687 |
|
nvts = 0; |
| 688 |
|
} |
| 689 |
|
if (nvns) { |
| 690 |
< |
free((char *)vnlist); |
| 690 |
> |
free((void *)vnlist); |
| 691 |
|
nvns = 0; |
| 692 |
|
} |
| 693 |
|
} |
| 694 |
|
|
| 695 |
|
|
| 696 |
|
int |
| 697 |
< |
newv(x, y, z) /* create a new vertex */ |
| 698 |
< |
double x, y, z; |
| 697 |
> |
newv( /* create a new vertex */ |
| 698 |
> |
double x, |
| 699 |
> |
double y, |
| 700 |
> |
double z |
| 701 |
> |
) |
| 702 |
|
{ |
| 703 |
|
if (!(nvs%CHUNKSIZ)) { /* allocate next block */ |
| 704 |
|
if (nvs == 0) |
| 705 |
|
vlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT)); |
| 706 |
|
else |
| 707 |
< |
vlist = (FVECT *)realloc((char *)vlist, |
| 707 |
> |
vlist = (FVECT *)realloc((void *)vlist, |
| 708 |
|
(nvs+CHUNKSIZ)*sizeof(FVECT)); |
| 709 |
|
if (vlist == NULL) { |
| 710 |
|
fprintf(stderr, |
| 721 |
|
|
| 722 |
|
|
| 723 |
|
int |
| 724 |
< |
newvn(x, y, z) /* create a new vertex normal */ |
| 725 |
< |
double x, y, z; |
| 724 |
> |
newvn( /* create a new vertex normal */ |
| 725 |
> |
double x, |
| 726 |
> |
double y, |
| 727 |
> |
double z |
| 728 |
> |
) |
| 729 |
|
{ |
| 730 |
|
if (!(nvns%CHUNKSIZ)) { /* allocate next block */ |
| 731 |
|
if (nvns == 0) |
| 732 |
|
vnlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT)); |
| 733 |
|
else |
| 734 |
< |
vnlist = (FVECT *)realloc((char *)vnlist, |
| 734 |
> |
vnlist = (FVECT *)realloc((void *)vnlist, |
| 735 |
|
(nvns+CHUNKSIZ)*sizeof(FVECT)); |
| 736 |
|
if (vnlist == NULL) { |
| 737 |
|
fprintf(stderr, |
| 750 |
|
|
| 751 |
|
|
| 752 |
|
int |
| 753 |
< |
newvt(x, y) /* create a new texture map vertex */ |
| 754 |
< |
double x, y; |
| 753 |
> |
newvt( /* create a new texture map vertex */ |
| 754 |
> |
double x, |
| 755 |
> |
double y |
| 756 |
> |
) |
| 757 |
|
{ |
| 758 |
|
if (!(nvts%CHUNKSIZ)) { /* allocate next block */ |
| 759 |
|
if (nvts == 0) |
| 760 |
< |
vtlist = (FLOAT (*)[2])malloc(CHUNKSIZ*2*sizeof(FLOAT)); |
| 760 |
> |
vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL)); |
| 761 |
|
else |
| 762 |
< |
vtlist = (FLOAT (*)[2])realloc((char *)vtlist, |
| 763 |
< |
(nvts+CHUNKSIZ)*2*sizeof(FLOAT)); |
| 762 |
> |
vtlist = (RREAL (*)[2])realloc((void *)vtlist, |
| 763 |
> |
(nvts+CHUNKSIZ)*2*sizeof(RREAL)); |
| 764 |
|
if (vtlist == NULL) { |
| 765 |
|
fprintf(stderr, |
| 766 |
|
"Out of memory while allocating texture vertex %d\n", |
| 775 |
|
} |
| 776 |
|
|
| 777 |
|
|
| 778 |
< |
syntax(er) /* report syntax error and exit */ |
| 779 |
< |
char *er; |
| 778 |
> |
void |
| 779 |
> |
syntax( /* report syntax error and exit */ |
| 780 |
> |
char *er |
| 781 |
> |
) |
| 782 |
|
{ |
| 783 |
|
fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n", |
| 784 |
|
inpfile, lineno, er); |