--- ray/src/cv/obj2rad.c 1994/06/15 09:51:31 2.9 +++ ray/src/cv/obj2rad.c 2003/03/04 01:42:29 2.18 @@ -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.18 2003/03/04 01:42:29 greg Exp $"; #endif - /* * Convert a Wavefront .obj file to Radiance format. * @@ -17,17 +14,15 @@ static char SCCSid[] = "$SunId$ LBL"; #include "trans.h" +#include "tmesh.h" + #include -#define TCALNAME "tmesh.cal" /* triangle interp. file */ -#define QCALNAME "surf.cal" /* quad 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 */ @@ -37,8 +32,6 @@ int nvns; FLOAT (*vtlist)[2]; /* map vertex list */ int nvts; -typedef FLOAT BARYCCM[3][4]; /* barycentric coordinate system */ - typedef int VNDX[3]; /* vertex index (point,map,normal) */ #define CHUNKSIZ 256 /* vertex allocation chunk size */ @@ -237,11 +230,6 @@ FILE *fp; if (!puttri(argv[1], argv[2], argv[3])) syntax("Bad triangle"); break; - case 4: - if (!putquad(argv[1], argv[2], - argv[3], argv[4])) - syntax("Bad quad"); - break; default: if (!putface(argc-1, argv+1)) syntax("Bad face"); @@ -489,7 +477,7 @@ register char **av; if (!cvtndx(vi, av[0])) return(0); - if (vi[2] >= 0) + if (!flatten && vi[2] >= 0) return(1); /* has interpolated normals */ if (ac < 4) return(0); /* it's a triangle! */ @@ -536,18 +524,16 @@ register char **av; char *cp; register int i; - if (nonplanar(ac, av)) { /* break into quads and triangles */ - while (ac > 3) { - if (!putquad(av[0], av[1], av[2], av[3])) + if (nonplanar(ac, av)) { /* break into triangles */ + while (ac > 2) { + if (!puttri(av[0], av[1], av[2])) return(0); - ac -= 2; /* remove two vertices & rotate */ + ac--; /* remove vertex & rotate */ cp = av[0]; for (i = 0; i < ac-1; i++) - av[i] = av[i+3]; + av[i] = av[i+2]; av[i] = cp; } - if (ac == 3 && !puttri(av[0], av[1], av[2])) - return(0); return(1); } if ((cp = getmtl()) == NULL) @@ -569,7 +555,10 @@ char *v1, *v2, *v3; char *mod; VNDX v1i, v2i, v3i; BARYCCM bvecs; + FLOAT bcoor[3][3]; int texOK, patOK; + int flatness; + register int i; if ((mod = getmtl()) == NULL) return(-1); @@ -577,14 +566,33 @@ 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 patOK = 0; #endif if (texOK | patOK) - if (comp_baryc(bvecs, vlist[v1i[0]], vlist[v2i[0]], + if (comp_baryc(&bvecs, vlist[v1i[0]], vlist[v2i[0]], vlist[v3i[0]]) < 0) return(-1); /* put out texture (if any) */ @@ -592,17 +600,13 @@ char *v1, *v2, *v3; printf("\n%s texfunc %s\n", mod, TEXNAME); mod = TEXNAME; printf("4 dx dy dz %s\n", TCALNAME); - printf("0\n21\n"); - put_baryc(bvecs); - printf("\t%14.12g %14.12g %14.12g\n", - vnlist[v1i[2]][0], vnlist[v2i[2]][0], - vnlist[v3i[2]][0]); - printf("\t%14.12g %14.12g %14.12g\n", - vnlist[v1i[2]][1], vnlist[v2i[2]][1], - vnlist[v3i[2]][1]); - printf("\t%14.12g %14.12g %14.12g\n", - vnlist[v1i[2]][2], vnlist[v2i[2]][2], - vnlist[v3i[2]][2]); + printf("0\n"); + for (i = 0; i < 3; i++) { + bcoor[i][0] = vnlist[v1i[2]][i]; + bcoor[i][1] = vnlist[v2i[2]][i]; + bcoor[i][2] = vnlist[v3i[2]][i]; + } + put_baryc(&bvecs, bcoor, 3); } #ifdef TEXMAPS /* put out pattern (if any) */ @@ -610,251 +614,43 @@ char *v1, *v2, *v3; printf("\n%s colorpict %s\n", mod, PATNAME); mod = PATNAME; printf("7 noneg noneg noneg %s %s u v\n", mapname, TCALNAME); - printf("0\n18\n"); - put_baryc(bvecs); - printf("\t%f %f %f\n", vtlist[v1i[1]][0], - vtlist[v2i[1]][0], vtlist[v3i[1]][0]); - printf("\t%f %f %f\n", vtlist[v1i[1]][1], - vtlist[v2i[1]][1], vtlist[v3i[1]][1]); + printf("0\n"); + for (i = 0; i < 2; i++) { + bcoor[i][0] = vtlist[v1i[1]][i]; + bcoor[i][1] = vtlist[v2i[1]][i]; + bcoor[i][2] = vtlist[v3i[1]][i]; + } + 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]]); - - return(1); -} - - -int -comp_baryc(bcm, v1, v2, v3) /* compute barycentric vectors */ -register BARYCCM bcm; -FLOAT *v1, *v2, *v3; -{ - FLOAT *vt; - FVECT va, vab, vcb; - double d; - register int i, j; - - for (j = 0; j < 3; j++) { - for (i = 0; i < 3; i++) { - vab[i] = v1[i] - v2[i]; - vcb[i] = v3[i] - v2[i]; - } - d = DOT(vcb,vcb); - if (d <= FTINY) - return(-1); - d = DOT(vcb,vab)/d; - for (i = 0; i < 3; i++) - va[i] = vab[i] - vcb[i]*d; - d = DOT(va,va); - if (d <= FTINY) - return(-1); - for (i = 0; i < 3; i++) { - va[i] /= d; - bcm[j][i] = va[i]; - } - bcm[j][3] = -DOT(v2,va); - /* rotate vertices */ - vt = v1; - v1 = v2; - v2 = v3; - v3 = vt; + 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(0); -} - - -put_baryc(bcm) /* put barycentric coord. vectors */ -register BARYCCM bcm; -{ - register int i; - - for (i = 0; i < 3; i++) - printf("%14.8f %14.8f %14.8f %14.8f\n", - bcm[i][0], bcm[i][1], bcm[i][2], bcm[i][3]); -} - - -putquad(p0, p1, p3, p2) /* put out a quadrilateral */ -char *p0, *p1, *p3, *p2; /* names correspond to binary pos. */ -{ - VNDX p0i, p1i, p2i, p3i; - FVECT norm[4]; - char *mod, *name; - int axis; - FVECT v1, v2, vc1, vc2; - int ok1, ok2; - -#ifdef TEXMAPS - /* also should output texture index coordinates, - * which will require new .cal file - */ -#endif - if ((mod = getmtl()) == NULL) - return(-1); - name = getonm(); - /* get actual indices */ - if (!cvtndx(p0i,p0) || !cvtndx(p1i,p1) || - !cvtndx(p2i,p2) || !cvtndx(p3i,p3)) - return(0); - /* compute exact normals */ - fvsum(v1, vlist[p1i[0]], vlist[p0i[0]], -1.0); - fvsum(v2, vlist[p2i[0]], vlist[p0i[0]], -1.0); - fcross(vc1, v1, v2); - ok1 = normalize(vc1) != 0.0; - fvsum(v1, vlist[p2i[0]], vlist[p3i[0]], -1.0); - fvsum(v2, vlist[p1i[0]], vlist[p3i[0]], -1.0); - fcross(vc2, v1, v2); - ok2 = normalize(vc2) != 0.0; - if (!(ok1 | ok2)) - return(-1); - /* compute normal interpolation */ - axis = norminterp(norm, p0i, p1i, p2i, p3i); - - /* put out quadrilateral? */ - if (ok1 & ok2 && fabs(fdot(vc1,vc2)) >= 1.0-FTINY) { - printf("\n%s ", mod); - if (axis != -1) { - printf("texfunc %s\n", TEXNAME); - printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); - printf("0\n13\t%d\n", axis); - pvect(norm[0]); - pvect(norm[1]); - pvect(norm[2]); - fvsum(v1, norm[3], vc1, -0.5); - fvsum(v1, v1, vc2, -0.5); - pvect(v1); - printf("\n%s ", TEXNAME); - } - printf("polygon %s.%d\n", name, faceno); - printf("0\n0\n12\n"); - pvect(vlist[p0i[0]]); - pvect(vlist[p1i[0]]); - pvect(vlist[p3i[0]]); - pvect(vlist[p2i[0]]); - return(1); - } - /* put out triangles? */ - if (ok1) { - printf("\n%s ", mod); - if (axis != -1) { - printf("texfunc %s\n", TEXNAME); - printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); - printf("0\n13\t%d\n", axis); - pvect(norm[0]); - pvect(norm[1]); - pvect(norm[2]); - fvsum(v1, norm[3], vc1, -1.0); - pvect(v1); - printf("\n%s ", TEXNAME); - } - printf("polygon %s.%da\n", name, faceno); - printf("0\n0\n9\n"); - pvect(vlist[p0i[0]]); - pvect(vlist[p1i[0]]); - pvect(vlist[p2i[0]]); - } - if (ok2) { - printf("\n%s ", mod); - if (axis != -1) { - printf("texfunc %s\n", TEXNAME); - printf("4 surf_dx surf_dy surf_dz %s\n", QCALNAME); - printf("0\n13\t%d\n", axis); - pvect(norm[0]); - pvect(norm[1]); - pvect(norm[2]); - fvsum(v2, norm[3], vc2, -1.0); - pvect(v2); - printf("\n%s ", TEXNAME); - } - printf("polygon %s.%db\n", name, faceno); - printf("0\n0\n9\n"); - pvect(vlist[p2i[0]]); - pvect(vlist[p1i[0]]); - pvect(vlist[p3i[0]]); - } return(1); } -int -norminterp(resmat, p0i, p1i, p2i, p3i) /* compute normal interpolation */ -register FVECT resmat[4]; -register VNDX p0i, p1i, p2i, p3i; -{ -#define u ((ax+1)%3) -#define v ((ax+2)%3) - - register int ax; - MAT4 eqnmat; - FVECT v1; - register int i, j; - -#ifdef TEXMAPS - /* also check for texture indices */ -#endif - if (flatten || !(p0i[2]>=0 && p1i[2]>=0 && p2i[2]>=0 && p3i[2]>=0)) - return(-1); - /* find dominant axis */ - VCOPY(v1, vnlist[p0i[2]]); - fvsum(v1, v1, vnlist[p1i[2]], 1.0); - fvsum(v1, v1, vnlist[p2i[2]], 1.0); - fvsum(v1, v1, vnlist[p3i[2]], 1.0); - ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1; - ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2; - /* assign equation matrix */ - eqnmat[0][0] = vlist[p0i[0]][u]*vlist[p0i[0]][v]; - eqnmat[0][1] = vlist[p0i[0]][u]; - eqnmat[0][2] = vlist[p0i[0]][v]; - eqnmat[0][3] = 1.0; - eqnmat[1][0] = vlist[p1i[0]][u]*vlist[p1i[0]][v]; - eqnmat[1][1] = vlist[p1i[0]][u]; - eqnmat[1][2] = vlist[p1i[0]][v]; - eqnmat[1][3] = 1.0; - eqnmat[2][0] = vlist[p2i[0]][u]*vlist[p2i[0]][v]; - eqnmat[2][1] = vlist[p2i[0]][u]; - eqnmat[2][2] = vlist[p2i[0]][v]; - eqnmat[2][3] = 1.0; - eqnmat[3][0] = vlist[p3i[0]][u]*vlist[p3i[0]][v]; - eqnmat[3][1] = vlist[p3i[0]][u]; - eqnmat[3][2] = vlist[p3i[0]][v]; - eqnmat[3][3] = 1.0; - /* invert matrix (solve system) */ - if (!invmat4(eqnmat, eqnmat)) - return(-1); /* no solution */ - /* compute result matrix */ - for (j = 0; j < 4; j++) - for (i = 0; i < 3; i++) - resmat[j][i] = eqnmat[j][0]*vnlist[p0i[2]][i] + - eqnmat[j][1]*vnlist[p1i[2]][i] + - eqnmat[j][2]*vnlist[p2i[2]][i] + - eqnmat[j][3]*vnlist[p3i[2]][i]; -#ifdef TEXMAPS - /* compute result matrix for texture indices */ -#endif - return(ax); - -#undef u -#undef v -} - - 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; } }