--- ray/src/cv/rad2mgf.c 1994/07/08 16:10:06 2.2 +++ ray/src/cv/rad2mgf.c 2003/11/15 17:54:06 2.23 @@ -1,26 +1,24 @@ -/* 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.23 2003/11/15 17:54:06 schorsch Exp $"; #endif - /* * Convert Radiance scene description to MGF */ -#include +#include #include -#include "fvect.h" +#include + +#include "platform.h" +#include "rtmath.h" +#include "rtio.h" +#include "rtprocess.h" #include "object.h" #include "color.h" #include "lookup.h" -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(); +#define C_1SIDEDTHICK 0.005 -extern void free(); -extern char *malloc(); LUTAB rmats = LU_SINIT(free,NULL); /* defined material table */ @@ -33,10 +31,58 @@ double unit_mult = 1.; /* units multiplier */ #define hasmult (unit_mult < .999 || unit_mult > 1.001) +/* + * Stuff for tracking and reusing vertices: + */ -main(argc, argv) -int argc; -char **argv; +char VKFMT[] = "%+16.9e %+16.9e %+16.9e"; +#define VKLEN 64 + +#define mkvkey(k,v) sprintf(k, VKFMT, (v)[0], (v)[1], (v)[2]) + +#define NVERTS 256 + +long vclock; /* incremented at each vertex request */ + +struct vert { + long lused; /* when last used (0 if unassigned) */ + FVECT p; /* track point position only */ +} vert[NVERTS]; /* our vertex cache */ + +LUTAB vertab = LU_SINIT(free,NULL); /* our vertex lookup table */ + +static void rad2mgf(char *inp); +static void cvtprim(char *inp, char *mod, char *typ, char *id, FUNARGS *fa); +static void newmat(char *id, char *alias); +static void setmat(char *id); +static void setobj(char *id); +static void init(void); +static void uninit(void); +static void clrverts(void); +static void add2dispatch(char *name, int (*func)()); +static char *getvertid(char *vname, FVECT vp); +static int o_unsupported(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_face(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_cone(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_sphere(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_cylinder(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_ring(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_instance(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_illum(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_plastic(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_metal(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_glass(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_dielectric(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_mirror(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_trans(char *mod, char *typ, char *id, FUNARGS *fa); +static int o_light(char *mod, char *typ, char *id, FUNARGS *fa); + + +int +main( + int argc, + char **argv +) { int i; @@ -60,6 +106,8 @@ char **argv; goto unkopt; } break; + default: + goto unkopt; } init(); if (i >= argc) @@ -75,8 +123,10 @@ unkopt: } -rad2mgf(inp) /* convert a Radiance file to MGF */ -char *inp; +void +rad2mgf( /* convert a Radiance file to MGF */ + char *inp +) { #define mod buf #define typ (buf+128) @@ -154,26 +204,36 @@ char *inp; } -cvtprim(inp, mod, typ, id, fa) /* process Radiance primitive */ -char *inp, *mod, *typ, *id; -FUNARGS *fa; +void +cvtprim( /* process Radiance primitive */ + char *inp, + char *mod, + char *typ, + char *id, + FUNARGS *fa +) { int (*df)(); df = (int (*)())lu_find(&rdispatch, typ)->data; if (df != NULL) { /* convert */ if ((*df)(mod, typ, id, fa) < 0) { - fprintf(stderr, "%s: bad %s \"%s\"\n", typ, id); + fprintf(stderr, "%s: bad %s \"%s\"\n", "rat2mgf", 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); + } } -newmat(id, alias) /* add a modifier to the alias list */ -char *id; -char *alias; +void +newmat( /* add a modifier to the alias list */ + char *id, + char *alias +) { register LUENT *lp, *lpa; @@ -207,8 +267,10 @@ memerr: } -setmat(id) /* set material to this one */ -char *id; +void +setmat( /* set material to this one */ + char *id +) { if (!strcmp(id, curmat)) /* already set? */ return; @@ -219,8 +281,10 @@ char *id; } -setobj(id) /* set object name to this one */ -char *id; +void +setobj( /* set object name to this one */ + char *id +) { register char *cp, *cp2; char *end = NULL; @@ -232,8 +296,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'; @@ -242,8 +314,10 @@ char *id; } -init() /* initialize dispatch table and output */ +void +init(void) /* initialize dispatch table and output */ { + lu_init(&vertab, NVERTS); lu_init(&rdispatch, 22); add2dispatch("polygon", o_face); add2dispatch("cone", o_cone); @@ -254,11 +328,13 @@ init() /* initialize dispatch table and output */ add2dispatch("tube", o_cylinder); add2dispatch("ring", o_ring); add2dispatch("instance", o_instance); + add2dispatch("mesh", o_instance); add2dispatch("plastic", o_plastic); add2dispatch("plastic2", o_plastic); 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); @@ -266,28 +342,44 @@ 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); } -uninit() /* mark end of MGF file */ +void +uninit(void) /* 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); } -add2dispatch(name, func) /* add function to dispatch table */ -char *name; -int (*func)(); +void +clrverts(void) /* clear vertex table */ { + register int i; + + lu_done(&vertab); + for (i = 0; i < NVERTS; i++) + vert[i].lused = 0; + lu_init(&vertab, NVERTS); +} + + +void +add2dispatch( /* add function to dispatch table */ + char *name, + int (*func)() +) +{ register LUENT *lp; lp = lu_find(&rdispatch, name); @@ -301,39 +393,17 @@ int (*func)(); } -/* - * Stuff for tracking and reusing vertices: - */ - -char VKFMT[] = "%+1.9e %+1.9e %+1.9e"; -#define VKLEN 64 - -#define mkvkey(k,v) sprintf(k, VKFMT, (v)[0], (v)[1], (v)[2]) - -#define NVERTS 256 - -long clock; /* incremented at each vertex request */ - -struct vert { - long lused; /* when last used (0 if unassigned) */ - FVECT p; /* track point position only */ -} vert[NVERTS]; - -LUTAB vertab = LU_SINIT(free,NULL); /* our vertex lookup table */ - - char * -getvertid(vname, vp) /* get/set vertex ID for this point */ -char *vname; -FVECT vp; +getvertid( /* get/set vertex ID for this point */ + char *vname, + FVECT vp +) { - char vkey[VKLEN]; + static char vkey[VKLEN]; register LUENT *lp; register int i, vndx; - if (!vertab.tsiz && !lu_init(&vertab, NVERTS)) - goto memerr; - clock++; /* increment counter */ + vclock++; /* increment counter */ mkvkey(vkey, vp); if ((lp = lu_find(&vertab, vkey)) == NULL) goto memerr; @@ -353,13 +423,13 @@ FVECT vp; mkvkey(vkey, vert[vndx].p); lu_delete(&vertab, vkey); } - vert[vndx].lused = clock; /* assign it */ - VCOPY(vert[vndx].p, vp); + VCOPY(vert[vndx].p, vp); /* assign it */ printf("v v%d =\n\tp %.15g %.15g %.15g\n", /* print it */ vndx, vp[0], vp[1], vp[2]); lp->data = (char *)&vert[vndx]; /* set it */ } else vndx = (struct vert *)lp->data - vert; + vert[vndx].lused = vclock; /* record this use */ sprintf(vname, "v%d", vndx); return(vname); memerr: @@ -369,22 +439,60 @@ memerr: int -o_face(mod, typ, id, fa) /* print out a polygon */ -char *mod, *typ, *id; -FUNARGS *fa; +o_unsupported( /* mark unsupported primitive */ + char *mod, + char *typ, + char *id, + FUNARGS *fa +) { - char entbuf[512]; + 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( /* print out a polygon */ + char *mod, + char *typ, + char *id, + FUNARGS *fa +) +{ + char entbuf[2048], *linestart; register char *cp; register int i; - if (fa->nfargs < 9 | fa->nfargs % 3) + if ((fa->nfargs < 9) | (fa->nfargs % 3)) 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++; @@ -395,9 +503,12 @@ FUNARGS *fa; int -o_cone(mod, typ, id, fa) /* print out a cone */ -char *mod, *typ, *id; -register FUNARGS *fa; +o_cone( /* print out a cone */ + char *mod, + char *typ, + char *id, + register FUNARGS *fa +) { char v1[6], v2[6]; @@ -418,9 +529,12 @@ register FUNARGS *fa; int -o_sphere(mod, typ, id, fa) /* print out a sphere */ -char *mod, *typ, *id; -register FUNARGS *fa; +o_sphere( /* print out a sphere */ + char *mod, + char *typ, + char *id, + register FUNARGS *fa +) { char cent[6]; @@ -435,9 +549,12 @@ register FUNARGS *fa; int -o_cylinder(mod, typ, id, fa) /* print out a cylinder */ -char *mod, *typ, *id; -register FUNARGS *fa; +o_cylinder( /* print out a cylinder */ + char *mod, + char *typ, + char *id, + register FUNARGS *fa +) { char v1[6], v2[6]; @@ -454,9 +571,12 @@ register FUNARGS *fa; int -o_ring(mod, typ, id, fa) /* print out a ring */ -char *mod, *typ, *id; -register FUNARGS *fa; +o_ring( /* print out a ring */ + char *mod, + char *typ, + char *id, + register FUNARGS *fa +) { if (fa->nfargs != 8) return(-1); @@ -477,9 +597,12 @@ register FUNARGS *fa; int -o_instance(mod, typ, id, fa) /* convert an instance */ -char *mod, *typ, *id; -FUNARGS *fa; +o_instance( /* convert an instance (or mesh) */ + char *mod, + char *typ, + char *id, + FUNARGS *fa +) { register int i; register char *cp; @@ -512,24 +635,19 @@ FUNARGS *fa; fputs(fa->sarg[i], stdout); } putchar('\n'); + clrverts(); /* vertex id's no longer reliable */ return(0); } int -o_source(mod, typ, id, fa) /* convert a source */ -char *mod, *typ, *id; -FUNARGS *fa; +o_illum( /* convert an illum material */ + char *mod, + char *typ, + char *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; -{ if (fa->nsargs == 1 && strcmp(fa->sarg[0], VOIDID)) { newmat(id, fa->sarg[0]); /* just create alias */ return(0); @@ -542,9 +660,12 @@ FUNARGS *fa; int -o_plastic(mod, typ, id, fa) /* convert a plastic material */ -char *mod, *typ, *id; -register FUNARGS *fa; +o_plastic( /* convert a plastic material */ + char *mod, + char *typ, + char *id, + register FUNARGS *fa +) { COLOR cxyz, rrgb; double d; @@ -570,9 +691,12 @@ register FUNARGS *fa; int -o_metal(mod, typ, id, fa) /* convert a metal material */ -char *mod, *typ, *id; -register FUNARGS *fa; +o_metal( /* convert a metal material */ + char *mod, + char *typ, + char *id, + register FUNARGS *fa +) { COLOR cxyz, rrgb; double d; @@ -596,9 +720,12 @@ register FUNARGS *fa; int -o_glass(mod, typ, id, fa) /* convert a glass material */ -char *mod, *typ, *id; -register FUNARGS *fa; +o_glass( /* convert a glass material */ + char *mod, + char *typ, + char *id, + register FUNARGS *fa +) { COLOR cxyz, rrgb, trgb; double nrfr = 1.52, F, d; @@ -609,6 +736,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++) { @@ -634,10 +762,46 @@ register FUNARGS *fa; int -o_mirror(mod, typ, id, fa) /* convert a mirror material */ -char *mod, *typ, *id; -register FUNARGS *fa; +o_dielectric( /* convert a dielectric material */ + char *mod, + char *typ, + char *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]; + 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_mirror( /* convert a mirror material */ + char *mod, + char *typ, + char *id, + register FUNARGS *fa +) +{ COLOR cxyz, rrgb; double d; @@ -660,9 +824,12 @@ register FUNARGS *fa; int -o_trans(mod, typ, id, fa) /* convert a trans material */ -char *mod, *typ, *id; -register FUNARGS *fa; +o_trans( /* convert a trans material */ + char *mod, + char *typ, + char *id, + register FUNARGS *fa +) { COLOR cxyz, rrgb; double rough, trans, tspec, d; @@ -699,9 +866,12 @@ register FUNARGS *fa; int -o_light(mod, typ, id, fa) /* convert a light type */ -char *mod, *typ, *id; -register FUNARGS *fa; +o_light( /* convert a light type */ + char *mod, + char *typ, + char *id, + register FUNARGS *fa +) { COLOR cxyz, rrgb; double d; @@ -715,6 +885,6 @@ register FUNARGS *fa; puts("\tc"); if (d > FTINY) printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); - printf("\ted %.4g\n", cxyz[1]*WHTEFFICACY); + printf("\ted %.4g\n", cxyz[1]*(PI*WHTEFFICACY)); return(0); }