--- ray/src/cv/rad2mgf.c 1998/09/04 09:09:58 2.14 +++ ray/src/cv/rad2mgf.c 2019/12/28 18:05:14 2.28 @@ -1,29 +1,23 @@ -/* Copyright (c) 1995 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: rad2mgf.c,v 2.28 2019/12/28 18:05:14 greg Exp $"; #endif - /* * Convert Radiance scene description to MGF */ -#include "standard.h" #include -#include + +#include "platform.h" +#include "rtmath.h" +#include "rtio.h" +#include "rtprocess.h" #include "object.h" #include "color.h" #include "lookup.h" #define C_1SIDEDTHICK 0.005 -int o_face(), o_cone(), o_sphere(), o_ring(), o_cylinder(); -int o_instance(), o_illum(); -int o_plastic(), o_metal(), o_glass(), o_dielectric(), - o_mirror(), o_trans(), o_light(); -extern int free(); - LUTAB rmats = LU_SINIT(free,NULL); /* defined material table */ LUTAB rdispatch = LU_SINIT(NULL,NULL); /* function dispatch table */ @@ -55,10 +49,39 @@ struct vert { LUTAB vertab = LU_SINIT(free,NULL); /* our vertex lookup table */ +void rad2mgf(char *inp); +void cvtprim(char *inp, char *mod, char *typ, char *id, FUNARGS *fa); +void newmat(char *id, char *alias); +void setmat(char *id); +void setobj(char *id); +void init(void); +void uninit(void); +void clrverts(void); +void unspace(char *s); +void add2dispatch(char *name, int (*func)()); +char *getvertid(char *vname, FVECT vp); +int o_unsupported(char *mod, char *typ, char *id, FUNARGS *fa); +int o_face(char *mod, char *typ, char *id, FUNARGS *fa); +int o_cone(char *mod, char *typ, char *id, FUNARGS *fa); +int o_sphere(char *mod, char *typ, char *id, FUNARGS *fa); +int o_cylinder(char *mod, char *typ, char *id, FUNARGS *fa); +int o_ring(char *mod, char *typ, char *id, FUNARGS *fa); +int o_instance(char *mod, char *typ, char *id, FUNARGS *fa); +int o_illum(char *mod, char *typ, char *id, FUNARGS *fa); +int o_plastic(char *mod, char *typ, char *id, FUNARGS *fa); +int o_metal(char *mod, char *typ, char *id, FUNARGS *fa); +int o_glass(char *mod, char *typ, char *id, FUNARGS *fa); +int o_dielectric(char *mod, char *typ, char *id, FUNARGS *fa); +int o_mirror(char *mod, char *typ, char *id, FUNARGS *fa); +int o_trans(char *mod, char *typ, char *id, FUNARGS *fa); +int o_light(char *mod, char *typ, char *id, FUNARGS *fa); -main(argc, argv) -int argc; -char **argv; + +int +main( + int argc, + char **argv +) { int i; @@ -99,14 +122,13 @@ 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) -#define id (buf+256) -#define alias (buf+384) - char buf[512]; + char buf[512]; + char mod[128], typ[32], id[128], alias[128]; FUNARGS fa; register FILE *fp; register int c; @@ -145,14 +167,19 @@ char *inp; break; default: /* Radiance primitive */ ungetc(c, fp); - if (fscanf(fp, "%s %s %s", mod, typ, id) != 3) { + if (fgetword(mod, sizeof(mod), fp) == NULL || + fgetword(typ, sizeof(typ), fp) == NULL || + fgetword(id, sizeof(id), fp) == NULL) { fputs(inp, stderr); fputs(": unexpected EOF\n", stderr); exit(1); } + unspace(mod); + unspace(id); if (!strcmp(typ, "alias")) { strcpy(alias, "EOF"); - fscanf(fp, "%s", alias); + fgetword(alias, sizeof(alias), fp); + unspace(alias); newmat(id, alias); } else { if (!readfargs(&fa, fp)) { @@ -171,23 +198,37 @@ char *inp; pclose(fp); else fclose(fp); -#undef mod -#undef typ -#undef id -#undef alias } -cvtprim(inp, mod, typ, id, fa) /* process Radiance primitive */ -char *inp, *mod, *typ, *id; -FUNARGS *fa; +void +unspace( /* replace spaces with underscores in s */ + char *s +) { + while (*s) { + if (isspace(*s)) + *s = '_'; + ++s; + } +} + + +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", "rad2mgf", typ, id); exit(1); } } else { /* unsupported */ @@ -198,9 +239,11 @@ FUNARGS *fa; } -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; @@ -234,8 +277,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; @@ -246,8 +291,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; @@ -265,7 +312,7 @@ char *id; *cp2++ = 'O'; } for (cp = id; cp < end; *cp2++ = *cp++) { - if (*cp < '!' | *cp > '~') /* limit to visible chars */ + if ((*cp < '!') | (*cp > '~')) /* limit to visible chars */ *cp = '?'; diff += *cp != *cp2; } @@ -277,7 +324,8 @@ char *id; } -init() /* initialize dispatch table and output */ +void +init(void) /* initialize dispatch table and output */ { lu_init(&vertab, NVERTS); lu_init(&rdispatch, 22); @@ -290,6 +338,7 @@ 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); @@ -310,7 +359,8 @@ init() /* initialize dispatch table and output */ } -uninit() /* mark end of MGF file */ +void +uninit(void) /* mark end of MGF file */ { puts("o"); if (hasmult) @@ -322,7 +372,8 @@ uninit() /* mark end of MGF file */ } -clrverts() /* clear vertex table */ +void +clrverts(void) /* clear vertex table */ { register int i; @@ -333,9 +384,11 @@ clrverts() /* clear vertex table */ } -add2dispatch(name, func) /* add function to dispatch table */ -char *name; -int (*func)(); +void +add2dispatch( /* add function to dispatch table */ + char *name, + int (*func)() +) { register LUENT *lp; @@ -351,9 +404,10 @@ int (*func)(); 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 +) { static char vkey[VKLEN]; register LUENT *lp; @@ -395,9 +449,12 @@ memerr: int -o_unsupported(mod, typ, id, fa) /* mark unsupported primitive */ -char *mod, *typ, *id; -FUNARGS *fa; +o_unsupported( /* mark unsupported primitive */ + char *mod, + char *typ, + char *id, + FUNARGS *fa +) { register int i; @@ -422,22 +479,30 @@ FUNARGS *fa; int -o_face(mod, typ, id, fa) /* print out a polygon */ -char *mod, *typ, *id; -FUNARGS *fa; +o_face( /* print out a polygon */ + char *mod, + char *typ, + char *id, + FUNARGS *fa +) { - char entbuf[2048]; + 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++; @@ -448,9 +513,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]; @@ -471,9 +539,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]; @@ -488,9 +559,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]; @@ -507,9 +581,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); @@ -530,9 +607,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; @@ -571,9 +651,12 @@ FUNARGS *fa; int -o_illum(mod, typ, id, fa) /* convert an illum material */ -char *mod, *typ, *id; -FUNARGS *fa; +o_illum( /* convert an illum material */ + char *mod, + char *typ, + char *id, + FUNARGS *fa +) { if (fa->nsargs == 1 && strcmp(fa->sarg[0], VOIDID)) { newmat(id, fa->sarg[0]); /* just create alias */ @@ -587,9 +670,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; @@ -602,11 +688,11 @@ register FUNARGS *fa; puts("\tc"); /* put diffuse component */ d = cxyz[0] + cxyz[1] + cxyz[2]; if (d > FTINY) - printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); - printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])); + printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); + printf("\trd %.6f\n", cxyz[1]*(1. - fa->farg[3])); if (fa->farg[3] > FTINY) { /* put specular component */ puts("\tc"); - printf("\trs %.4f %.4f\n", fa->farg[3], + printf("\trs %.6f %.6f\n", fa->farg[3], typ[7]=='2' ? .5*(fa->farg[4] + fa->farg[5]) : fa->farg[4]); } @@ -615,9 +701,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; @@ -630,10 +719,10 @@ register FUNARGS *fa; puts("\tc"); /* put diffuse component */ d = cxyz[0] + cxyz[1] + cxyz[2]; if (d > FTINY) - printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); - printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])); + printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); + printf("\trd %.6f\n", cxyz[1]*(1. - fa->farg[3])); /* put specular component */ - printf("\trs %.4f %.4f\n", cxyz[1]*fa->farg[3], + printf("\trs %.6f %.6f\n", cxyz[1]*fa->farg[3], typ[5]=='2' ? .5*(fa->farg[4] + fa->farg[5]) : fa->farg[4]); return(0); @@ -641,9 +730,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; @@ -667,22 +759,25 @@ register FUNARGS *fa; 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("\trs %.4f 0\n", cxyz[1]); + printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); + printf("\trs %.6f 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]); + printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); + printf("\tts %.6f 0\n", cxyz[1]); return(0); } int -o_dielectric(mod, typ, id, fa) /* convert a dielectric 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; @@ -698,21 +793,24 @@ register FUNARGS *fa; 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); + printf("\trs %.6f 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]); + printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); + printf("\tts %.6f 0\n", cxyz[1]); return(0); } int -o_mirror(mod, typ, id, fa) /* convert a mirror material */ -char *mod, *typ, *id; -register FUNARGS *fa; +o_mirror( /* convert a mirror material */ + char *mod, + char *typ, + char *id, + register FUNARGS *fa +) { COLOR cxyz, rrgb; double d; @@ -729,21 +827,24 @@ register FUNARGS *fa; puts("\tc"); /* put specular component */ d = cxyz[0] + cxyz[1] + cxyz[2]; if (d > FTINY) - printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); - printf("\trs %.4f 0\n", cxyz[1]); + printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); + printf("\trs %.6f 0\n", cxyz[1]); return(0); } 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; - if (typ[4] == '2') { /* trans2 */ + if (typ[5] == '2') { /* trans2 */ if (fa->nfargs != 8) return(-1); rough = .5*(fa->farg[4] + fa->farg[5]); @@ -762,22 +863,25 @@ register FUNARGS *fa; puts("\tc"); /* put transmitted diffuse */ d = cxyz[0] + cxyz[1] + cxyz[2]; if (d > FTINY) - printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); - printf("\ttd %.4f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec)); + printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); + printf("\ttd %.6f\n", cxyz[1]*trans*(1. - fa->farg[3])*(1. - tspec)); /* put transmitted specular */ - printf("\tts %.4f %.4f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough); + printf("\tts %.6f %.6f\n", cxyz[1]*trans*tspec*(1. - fa->farg[3]), rough); /* put reflected diffuse */ - printf("\trd %.4f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans)); + printf("\trd %.6f\n", cxyz[1]*(1. - fa->farg[3])*(1. - trans)); puts("\tc"); /* put reflected specular */ - printf("\trs %.4f %.4f\n", fa->farg[3], rough); + printf("\trs %.6f %.6f\n", fa->farg[3], rough); return(0); } 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; @@ -790,7 +894,7 @@ register FUNARGS *fa; d = cxyz[0] + cxyz[1] + cxyz[2]; puts("\tc"); if (d > FTINY) - printf("\t\tcxy %.4f %.4f\n", cxyz[0]/d, cxyz[1]/d); + printf("\t\tcxy %.6f %.6f\n", cxyz[0]/d, cxyz[1]/d); printf("\ted %.4g\n", cxyz[1]*(PI*WHTEFFICACY)); return(0); }