--- ray/src/cv/obj2rad.c 1994/06/15 15:07:08 2.13 +++ ray/src/cv/obj2rad.c 2003/11/15 17:54:06 2.22 @@ -1,9 +1,6 @@ -/* Copyright (c) 1994 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: obj2rad.c,v 2.22 2003/11/15 17:54:06 schorsch Exp $"; #endif - /* * Convert a Wavefront .obj file to Radiance format. * @@ -13,34 +10,31 @@ static char SCCSid[] = "$SunId$ LBL"; * I'm not sure they work correctly. (Taken out -- see TEXMAPS defines.) */ -#include "standard.h" +#include +#include +#include +#include "rtmath.h" +#include "rtio.h" +#include "resolu.h" #include "trans.h" +#include "tmesh.h" -#include -#define TCALNAME "tmesh.cal" /* triangle interp. file */ #define PATNAME "M-pat" /* mesh pattern name (reused) */ #define TEXNAME "M-nor" /* mesh texture name (reused) */ #define DEFOBJ "unnamed" /* default object name */ #define DEFMAT "white" /* default material name */ -#define ABS(x) ((x)>=0 ? (x) : -(x)) - #define pvect(v) printf("%18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2]) FVECT *vlist; /* our vertex list */ int nvs; /* number of vertices in our list */ FVECT *vnlist; /* vertex normal list */ int nvns; -FLOAT (*vtlist)[2]; /* map vertex list */ +RREAL (*vtlist)[2]; /* map vertex list */ int nvts; -typedef struct { - int ax; /* major axis */ - FLOAT tm[2][3]; /* transformation */ -} BARYCCM; - typedef int VNDX[3]; /* vertex index (point,map,normal) */ #define CHUNKSIZ 256 /* vertex allocation chunk size */ @@ -74,8 +68,6 @@ char *defobj = DEFOBJ; /* default (starting) object na int flatten = 0; /* discard surface normal information */ -char *getmtl(), *getonm(); - char mapname[128]; /* current picture file */ char matname[64]; /* current material name */ char group[16][32]; /* current group names */ @@ -84,10 +76,28 @@ char *inpfile; /* input file name */ int lineno; /* current line number */ int faceno; /* current face number */ +static void getnames(FILE *fp); +static void convert(FILE *fp); +static int getstmt(char *av[MAXARG], FILE *fp); +static char * getmtl(void); +static char * getonm(void); +static int matchrule(RULEHD *rp); +static int cvtndx(VNDX vi, char *vs); +static int nonplanar(int ac, char **av); +static int putface(int ac, char **av); +static int puttri(char *v1, char *v2, char *v3); +static void freeverts(void); +static int newv(double x, double y, double z); +static int newvn(double x, double y, double z); +static int newvt(double x, double y); +static void syntax(char *er); -main(argc, argv) /* read in .obj file and convert */ -int argc; -char *argv[]; + +int +main( /* read in .obj file and convert */ + int argc, + char *argv[] +) { int donames = 0; int i; @@ -109,7 +119,7 @@ char *argv[]; default: goto userr; } - if (i > argc | i < argc-1) + if ((i > argc) | (i < argc-1)) goto userr; if (i == argc) inpfile = ""; @@ -138,15 +148,17 @@ userr: } -getnames(fp) /* get valid qualifier names */ -FILE *fp; +void +getnames( /* get valid qualifier names */ + FILE *fp +) { char *argv[MAXARG]; int argc; ID tmpid; register int i; - while (argc = getstmt(argv, fp)) + while ( (argc = getstmt(argv, fp)) ) switch (argv[0][0]) { case 'f': /* face */ if (!argv[0][1]) @@ -187,8 +199,10 @@ FILE *fp; } -convert(fp) /* convert a T-mesh */ -FILE *fp; +void +convert( /* convert a T-mesh */ + FILE *fp +) { char *argv[MAXARG]; int argc; @@ -197,7 +211,7 @@ FILE *fp; nstats = nunknown = 0; /* scan until EOF */ - while (argc = getstmt(argv, fp)) { + while ( (argc = getstmt(argv, fp)) ) { switch (argv[0][0]) { case 'v': /* vertex */ switch (argv[0][1]) { @@ -291,9 +305,10 @@ FILE *fp; int -getstmt(av, fp) /* read the next statement from fp */ -register char *av[MAXARG]; -FILE *fp; +getstmt( /* read the next statement from fp */ + register char *av[MAXARG], + FILE *fp +) { extern char *fgetline(); static char sbuf[MAXARG*10]; @@ -325,7 +340,7 @@ FILE *fp; char * -getmtl() /* figure material for this face */ +getmtl(void) /* figure material for this face */ { register RULEHD *rp = ourmapping; @@ -351,7 +366,7 @@ getmtl() /* figure material for this face */ char * -getonm() /* invent a good name for object */ +getonm(void) /* invent a good name for object */ { static char name[64]; register char *cp1, *cp2; @@ -366,7 +381,7 @@ getonm() /* invent a good name for object */ cp2 = group[i]; if (cp1 > name) *cp1++ = '.'; - while (*cp1 = *cp2++) + while ( (*cp1 = *cp2++) ) if (++cp1 >= name+sizeof(name)-2) { *cp1 = '\0'; return(name); @@ -376,8 +391,10 @@ getonm() /* invent a good name for object */ } -matchrule(rp) /* check for a match on this rule */ -register RULEHD *rp; +int +matchrule( /* check for a match on this rule */ + register RULEHD *rp +) { ID tmpid; int gotmatch; @@ -427,9 +444,11 @@ register RULEHD *rp; } -cvtndx(vi, vs) /* convert vertex string to index */ -register VNDX vi; -register char *vs; +int +cvtndx( /* convert vertex string to index */ + register VNDX vi, + register char *vs +) { /* get point */ vi[0] = atoi(vs); @@ -474,12 +493,14 @@ register char *vs; } -nonplanar(ac, av) /* are vertices non-planar? */ -register int ac; -register char **av; +int +nonplanar( /* are vertices non-planar? */ + register int ac, + register char **av +) { VNDX vi; - FLOAT *p0, *p1; + RREAL *p0, *p1; FVECT v1, v2, nsum, newn; double d; register int i; @@ -525,9 +546,11 @@ register char **av; } -putface(ac, av) /* put out an N-sided polygon */ -int ac; -register char **av; +int +putface( /* put out an N-sided polygon */ + int ac, + register char **av +) { VNDX vi; char *cp; @@ -558,14 +581,19 @@ register char **av; } -puttri(v1, v2, v3) /* put out a triangle */ -char *v1, *v2, *v3; +int +puttri( /* put out a triangle */ + char *v1, + char *v2, + char *v3 +) { char *mod; VNDX v1i, v2i, v3i; BARYCCM bvecs; - FVECT bcoor[3]; - int texOK, patOK; + RREAL bcoor[3][3]; + int texOK = 0, patOK; + int flatness; register int i; if ((mod = getmtl()) == NULL) @@ -574,7 +602,26 @@ char *v1, *v2, *v3; if (!cvtndx(v1i, v1) || !cvtndx(v2i, v2) || !cvtndx(v3i, v3)) return(0); /* compute barycentric coordinates */ - texOK = !flatten && (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0); + if (v1i[2]>=0 && v2i[2]>=0 && v3i[2]>=0) + flatness = flat_tri(vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]], + vnlist[v1i[2]], vnlist[v2i[2]], vnlist[v3i[2]]); + else + flatness = ISFLAT; + + switch (flatness) { + case DEGEN: /* zero area */ + return(-1); + case RVFLAT: /* reversed normals, but flat */ + case ISFLAT: /* smoothing unnecessary */ + texOK = 0; + break; + case RVBENT: /* reversed normals with smoothing */ + case ISBENT: /* proper smoothing */ + texOK = 1; + break; + } + if (flatten) + texOK = 0; #ifdef TEXMAPS patOK = mapname[0] && (v1i[1]>=0 && v2i[1]>=0 && v3i[1]>=0); #else @@ -612,110 +659,52 @@ char *v1, *v2, *v3; put_baryc(&bvecs, bcoor, 2); } #endif - /* put out triangle */ + /* put out (reversed) triangle */ printf("\n%s polygon %s.%d\n", mod, getonm(), faceno); printf("0\n0\n9\n"); - pvect(vlist[v1i[0]]); - pvect(vlist[v2i[0]]); - pvect(vlist[v3i[0]]); - + if (flatness == RVFLAT || flatness == RVBENT) { + pvect(vlist[v3i[0]]); + pvect(vlist[v2i[0]]); + pvect(vlist[v1i[0]]); + } else { + pvect(vlist[v1i[0]]); + pvect(vlist[v2i[0]]); + pvect(vlist[v3i[0]]); + } return(1); } -int -comp_baryc(bcm, v1, v2, v3) /* compute barycentric vectors */ -register BARYCCM *bcm; -FLOAT *v1, *v2, *v3; +void +freeverts(void) /* free all vertices */ { - FLOAT *vt; - FVECT va, vab, vcb; - double d; - int ax0, ax1; - register int i, j; - /* compute major axis */ - for (i = 0; i < 3; i++) { - vab[i] = v1[i] - v2[i]; - vcb[i] = v3[i] - v2[i]; - } - fcross(va, vab, vcb); - bcm->ax = ABS(va[0]) > ABS(va[1]) ? 0 : 1; - bcm->ax = ABS(va[bcm->ax]) > ABS(va[2]) ? bcm->ax : 2; - ax0 = (bcm->ax + 1) % 3; - ax1 = (bcm->ax + 2) % 3; - for (j = 0; j < 2; j++) { - vab[0] = v1[ax0] - v2[ax0]; - vcb[0] = v3[ax0] - v2[ax0]; - vab[1] = v1[ax1] - v2[ax1]; - vcb[1] = v3[ax1] - v2[ax1]; - d = vcb[0]*vcb[0] + vcb[1]*vcb[1]; - if (d <= FTINY) - return(-1); - d = (vcb[0]*vab[0]+vcb[1]*vab[1])/d; - va[0] = vab[0] - vcb[0]*d; - va[1] = vab[1] - vcb[1]*d; - d = va[0]*va[0] + va[1]*va[1]; - if (d <= FTINY) - return(-1); - bcm->tm[j][0] = va[0] /= d; - bcm->tm[j][1] = va[1] /= d; - bcm->tm[j][2] = -(v2[ax0]*va[0]+v2[ax1]*va[1]); - /* rotate vertices */ - vt = v1; - v1 = v2; - v2 = v3; - v3 = vt; - } - return(0); -} - - -put_baryc(bcm, com, n) /* put barycentric coord. vectors */ -register BARYCCM *bcm; -register FVECT com[]; -int n; -{ - double a, b; - register int i, j; - - printf("%d\t%d\n", 1+3*n, bcm->ax); - for (i = 0; i < n; i++) { - a = com[i][0] - com[i][2]; - b = com[i][1] - com[i][2]; - printf("%14.8f %14.8f %14.8f\n", - bcm->tm[0][0]*a + bcm->tm[1][0]*b, - bcm->tm[0][1]*a + bcm->tm[1][1]*b, - bcm->tm[0][2]*a + bcm->tm[1][2]*b + com[i][2]); - } -} - - -freeverts() /* free all vertices */ -{ if (nvs) { - free((char *)vlist); + free((void *)vlist); nvs = 0; } if (nvts) { - free((char *)vtlist); + free((void *)vtlist); nvts = 0; } if (nvns) { - free((char *)vnlist); + free((void *)vnlist); nvns = 0; } } int -newv(x, y, z) /* create a new vertex */ -double x, y, z; +newv( /* create a new vertex */ + double x, + double y, + double z +) { if (!(nvs%CHUNKSIZ)) { /* allocate next block */ if (nvs == 0) vlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT)); else - vlist = (FVECT *)realloc((char *)vlist, + vlist = (FVECT *)realloc((void *)vlist, (nvs+CHUNKSIZ)*sizeof(FVECT)); if (vlist == NULL) { fprintf(stderr, @@ -732,14 +721,17 @@ double x, y, z; int -newvn(x, y, z) /* create a new vertex normal */ -double x, y, z; +newvn( /* create a new vertex normal */ + double x, + double y, + double z +) { if (!(nvns%CHUNKSIZ)) { /* allocate next block */ if (nvns == 0) vnlist = (FVECT *)malloc(CHUNKSIZ*sizeof(FVECT)); else - vnlist = (FVECT *)realloc((char *)vnlist, + vnlist = (FVECT *)realloc((void *)vnlist, (nvns+CHUNKSIZ)*sizeof(FVECT)); if (vnlist == NULL) { fprintf(stderr, @@ -758,15 +750,17 @@ double x, y, z; int -newvt(x, y) /* create a new texture map vertex */ -double x, y; +newvt( /* create a new texture map vertex */ + double x, + double y +) { if (!(nvts%CHUNKSIZ)) { /* allocate next block */ if (nvts == 0) - vtlist = (FLOAT (*)[2])malloc(CHUNKSIZ*2*sizeof(FLOAT)); + vtlist = (RREAL (*)[2])malloc(CHUNKSIZ*2*sizeof(RREAL)); else - vtlist = (FLOAT (*)[2])realloc((char *)vtlist, - (nvts+CHUNKSIZ)*2*sizeof(FLOAT)); + vtlist = (RREAL (*)[2])realloc((void *)vtlist, + (nvts+CHUNKSIZ)*2*sizeof(RREAL)); if (vtlist == NULL) { fprintf(stderr, "Out of memory while allocating texture vertex %d\n", @@ -781,8 +775,10 @@ double x, y; } -syntax(er) /* report syntax error and exit */ -char *er; +void +syntax( /* report syntax error and exit */ + char *er +) { fprintf(stderr, "%s: Wavefront syntax error near line %d: %s\n", inpfile, lineno, er);