--- ray/src/cv/rad2mgf.c 1995/04/11 13:33:10 2.9 +++ ray/src/cv/rad2mgf.c 2003/06/08 12:03:09 2.17 @@ -1,29 +1,27 @@ -/* Copyright (c) 1994 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: rad2mgf.c,v 2.17 2003/06/08 12:03:09 schorsch Exp $"; #endif - /* * Convert Radiance scene description to MGF */ -#include +#include "standard.h" +#include #include -#include "fvect.h" +#include + +#include "platform.h" #include "object.h" #include "color.h" #include "lookup.h" -#define PI 3.14159265358979323846 +#define C_1SIDEDTHICK 0.005 int o_face(), o_cone(), o_sphere(), o_ring(), o_cylinder(); -int o_instance(), o_source(), o_illum(); -int o_plastic(), o_metal(), o_glass(), o_mirror(), o_trans(), o_light(); +int o_instance(), o_illum(); +int o_plastic(), o_metal(), o_glass(), o_dielectric(), + o_mirror(), o_trans(), o_light(); -extern void free(); -extern char *malloc(); - LUTAB rmats = LU_SINIT(free,NULL); /* defined material table */ LUTAB rdispatch = LU_SINIT(NULL,NULL); /* function dispatch table */ @@ -190,8 +188,11 @@ FUNARGS *fa; fprintf(stderr, "%s: bad %s \"%s\"\n", typ, id); exit(1); } - } else if (lu_find(&rmats, mod)->data != NULL) /* make alias */ - newmat(id, mod); + } else { /* unsupported */ + o_unsupported(mod, typ, id, fa); + if (lu_find(&rmats, mod)->data != NULL) /* make alias */ + newmat(id, mod); + } } @@ -256,8 +257,16 @@ char *id; if (end == NULL) end = cp; /* copy to current object */ - for (cp = id, cp2 = curobj; cp < end; *cp2++ = *cp++) + cp2 = curobj; + if (!isalpha(*id)) { /* start with letter */ + diff = *cp2 != 'O'; + *cp2++ = 'O'; + } + for (cp = id; cp < end; *cp2++ = *cp++) { + if (*cp < '!' | *cp > '~') /* limit to visible chars */ + *cp = '?'; diff += *cp != *cp2; + } if (!diff && !*cp2) return; *cp2 = '\0'; @@ -284,6 +293,7 @@ init() /* initialize dispatch table and output */ add2dispatch("metal", o_metal); add2dispatch("metal2", o_metal); add2dispatch("glass", o_glass); + add2dispatch("dielectric", o_dielectric); add2dispatch("trans", o_trans); add2dispatch("trans2", o_trans); add2dispatch("mirror", o_mirror); @@ -291,7 +301,7 @@ init() /* initialize dispatch table and output */ add2dispatch("spotlight", o_light); add2dispatch("glow", o_light); add2dispatch("illum", o_illum); - puts("# The following was converted from Radiance scene input"); + puts("# The following was converted from RADIANCE scene input"); if (hasmult) printf("xf -s %.4e\n", unit_mult); printf("o %s\n", curobj); @@ -303,7 +313,7 @@ uninit() /* mark end of MGF file */ puts("o"); if (hasmult) puts("xf"); - puts("# End of data converted from Radiance scene input"); + puts("# End of data converted from RADIANCE scene input"); lu_done(&rdispatch); lu_done(&rmats); lu_done(&vertab); @@ -383,11 +393,38 @@ memerr: int +o_unsupported(mod, typ, id, fa) /* mark unsupported primitive */ +char *mod, *typ, *id; +FUNARGS *fa; +{ + register int i; + + fputs("\n# Unsupported RADIANCE primitive:\n", stdout); + printf("# %s %s %s", mod, typ, id); + printf("\n# %d", fa->nsargs); + for (i = 0; i < fa->nsargs; i++) + printf(" %s", fa->sarg[i]); +#ifdef IARGS + printf("\n# %d", fa->niargs); + for (i = 0; i < fa->niargs; i++) + printf(" %ld", fa->iarg[i]); +#else + fputs("\n# 0", stdout); +#endif + printf("\n# %d", fa->nfargs); + for (i = 0; i < fa->nfargs; i++) + printf(" %g", fa->farg[i]); + fputs("\n\n", stdout); + return(0); +} + + +int o_face(mod, typ, id, fa) /* print out a polygon */ char *mod, *typ, *id; FUNARGS *fa; { - char entbuf[512]; + char entbuf[2048], *linestart; register char *cp; register int i; @@ -395,10 +432,15 @@ FUNARGS *fa; return(-1); setmat(mod); setobj(id); - cp = entbuf; + cp = linestart = entbuf; *cp++ = 'f'; for (i = 0; i < fa->nfargs; i += 3) { *cp++ = ' '; + if (cp - linestart > 72) { + *cp++ = '\\'; *cp++ = '\n'; + linestart = cp; + *cp++ = ' '; *cp++ = ' '; + } getvertid(cp, fa->farg + i); while (*cp) cp++; @@ -532,15 +574,6 @@ FUNARGS *fa; int -o_source(mod, typ, id, fa) /* convert a source */ -char *mod, *typ, *id; -FUNARGS *fa; -{ - return(0); /* there is no MGF equivalent! */ -} - - -int o_illum(mod, typ, id, fa) /* convert an illum material */ char *mod, *typ, *id; FUNARGS *fa; @@ -624,6 +657,7 @@ register FUNARGS *fa; newmat(id, NULL); if (fa->nfargs == 4) nrfr = fa->farg[3]; + printf("\tir %f 0\n", nrfr); F = (1. - nrfr)/(1. + nrfr); /* use normal incidence */ F *= F; for (i = 0; i < 3; i++) { @@ -638,6 +672,36 @@ register FUNARGS *fa; if (d > FTINY) printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); printf("\trs %.4f 0\n", cxyz[1]); + rgb_cie(cxyz, trgb); /* put transmitted component */ + puts("\tc"); + d = cxyz[0] + cxyz[1] + cxyz[2]; + if (d > FTINY) + printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); + printf("\tts %.4f 0\n", cxyz[1]); + return(0); +} + + +int +o_dielectric(mod, typ, id, fa) /* convert a dielectric material */ +char *mod, *typ, *id; +register FUNARGS *fa; +{ + COLOR cxyz, trgb; + double F, d; + register int i; + + if (fa->nfargs != 5) + return(-1); + newmat(id, NULL); + F = (1. - fa->farg[3])/(1. + fa->farg[3]); /* normal incidence */ + F *= F; + for (i = 0; i < 3; i++) + trgb[i] = (1. - F)*pow(fa->farg[i], C_1SIDEDTHICK/unit_mult); + printf("\tir %f 0\n", fa->farg[3]); /* put index of refraction */ + printf("\tsides 1\n"); + puts("\tc"); /* put reflected component */ + printf("\trs %.4f 0\n", F); rgb_cie(cxyz, trgb); /* put transmitted component */ puts("\tc"); d = cxyz[0] + cxyz[1] + cxyz[2];