--- ray/src/rt/rv2.c 1991/10/21 13:30:43 1.30 +++ ray/src/rt/rv2.c 1994/01/06 15:52:17 2.23 @@ -1,4 +1,4 @@ -/* Copyright (c) 1991 Regents of the University of California */ +/* Copyright (c) 1993 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -18,10 +18,24 @@ static char SCCSid[] = "$SunId$ LBL"; #include "rpaint.h" +#include "resolu.h" + #include -#define CTRL(c) ('c'-'@') +#define CTRL(c) ((c)-'@') +#ifdef SMLFLT +#define sscanvec(s,v) (sscanf(s,"%f %f %f",v,v+1,v+2)==3) +#else +#define sscanvec(s,v) (sscanf(s,"%lf %lf %lf",v,v+1,v+2)==3) +#endif + +extern char *atos(); + +extern FILE *popen(); + +extern char rifname[128]; /* rad input file name */ + extern char VersionID[]; extern char *progname; extern char *octname; @@ -56,6 +70,16 @@ char *s; int change = 0; VIEW nv; + while (isspace(*s)) + s++; + if (*s == '-') { /* command line parameters */ + copystruct(&nv, &ourview); + if (sscanview(&nv, s)) + newview(&nv); + else + error(COMMAND, "bad view option(s)"); + return; + } if (sscanf(s, "%s", buf) == 1) { /* write parameters to a file */ if ((fname = getpath(buf, NULL, 0)) == NULL || (fp = fopen(fname, "a")) == NULL) { @@ -66,14 +90,14 @@ char *s; fputs(progname, fp); fprintview(&ourview, fp); fputs(sskip(s), fp); - fputs("\n", fp); + putc('\n', fp); fclose(fp); return; } sprintf(buf, "view type (%c): ", ourview.type); (*dev->comout)(buf); (*dev->comin)(buf, NULL); - if (buf[0] == CTRL(C)) return; + if (buf[0] == CTRL('C')) return; if (buf[0] && buf[0] != ourview.type) { nv.type = buf[0]; change++; @@ -83,8 +107,8 @@ char *s; ourview.vp[0], ourview.vp[1], ourview.vp[2]); (*dev->comout)(buf); (*dev->comin)(buf, NULL); - if (buf[0] == CTRL(C)) return; - if (sscanf(buf, "%lf %lf %lf", &nv.vp[0], &nv.vp[1], &nv.vp[2]) == 3) + if (buf[0] == CTRL('C')) return; + if (sscanvec(buf, nv.vp)) change++; else VCOPY(nv.vp, ourview.vp); @@ -92,8 +116,8 @@ char *s; ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]); (*dev->comout)(buf); (*dev->comin)(buf, NULL); - if (buf[0] == CTRL(C)) return; - if (sscanf(buf,"%lf %lf %lf",&nv.vdir[0],&nv.vdir[1],&nv.vdir[2]) == 3) + if (buf[0] == CTRL('C')) return; + if (sscanvec(buf, nv.vdir)) change++; else VCOPY(nv.vdir, ourview.vdir); @@ -101,8 +125,8 @@ char *s; ourview.vup[0], ourview.vup[1], ourview.vup[2]); (*dev->comout)(buf); (*dev->comin)(buf, NULL); - if (buf[0] == CTRL(C)) return; - if (sscanf(buf,"%lf %lf %lf",&nv.vup[0],&nv.vup[1],&nv.vup[2]) == 3) + if (buf[0] == CTRL('C')) return; + if (sscanvec(buf, nv.vup)) change++; else VCOPY(nv.vup, ourview.vup); @@ -110,7 +134,7 @@ char *s; ourview.horiz, ourview.vert); (*dev->comout)(buf); (*dev->comin)(buf, NULL); - if (buf[0] == CTRL(C)) return; + if (buf[0] == CTRL('C')) return; if (sscanf(buf, "%lf %lf", &nv.horiz, &nv.vert) == 2) change++; else { @@ -120,7 +144,7 @@ char *s; ourview.hoff, ourview.voff); (*dev->comout)(buf); (*dev->comin)(buf, NULL); - if (buf[0] == CTRL(C)) return; + if (buf[0] == CTRL('C')) return; if (sscanf(buf, "%lf %lf", &nv.hoff, &nv.voff) == 2) change++; else { @@ -141,8 +165,8 @@ char *s; if (sscanf(s, "%s", buf) == 1) { /* get parameters from a file */ copystruct(&nv, &stdview); - if ((fname = getpath(buf, NULL, 0)) == NULL || - (success = viewfile(fname, &nv, 0, 0)) == -1) { + if ((fname = getpath(buf, "", R_OK)) == NULL || + (success = viewfile(fname, &nv, NULL)) == -1) { sprintf(errmsg, "cannot open \"%s\"", buf); error(COMMAND, errmsg); return; @@ -164,10 +188,86 @@ char *s; } +saveview(s) /* save view to rad file */ +char *s; +{ + char view[64]; + char *fname; + FILE *fp; + + if (*atos(view, sizeof(view), s)) { + if (isint(view)) { + error(COMMAND, "cannot write view by number"); + return; + } + s = sskip(s); + } + while (isspace(*s)) + s++; + if (*s) + atos(rifname, sizeof(rifname), s); + else if (rifname[0] == '\0') { + error(COMMAND, "no previous rad file"); + return; + } + if ((fname = getpath(rifname, NULL, 0)) == NULL || + (fp = fopen(fname, "a")) == NULL) { + sprintf(errmsg, "cannot open \"%s\"", rifname); + error(COMMAND, errmsg); + return; + } + fputs("view= ", fp); + fputs(view, fp); + fprintview(&ourview, fp); + putc('\n', fp); + fclose(fp); +} + + +loadview(s) /* load view from rad file */ +char *s; +{ + char buf[512]; + char *fname; + FILE *fp; + VIEW nv; + + strcpy(buf, "rad -n -s -V -v "); + if (sscanf(s, "%s", buf+strlen(buf)) == 1) + s = sskip(s); + else + strcat(buf, "1"); + if (*s) + atos(rifname, sizeof(rifname), s); + else if (rifname[0] == '\0') { + error(COMMAND, "no previous rad file"); + return; + } + if ((fname = getpath(rifname, "", R_OK)) == NULL) { + sprintf(errmsg, "cannot access \"%s\"", rifname); + error(COMMAND, errmsg); + return; + } + sprintf(buf+strlen(buf), " %s", fname); + if ((fp = popen(buf, "r")) == NULL) { + error(COMMAND, "cannot run rad"); + return; + } + buf[0] = '\0'; + fgets(buf, sizeof(buf), fp); + pclose(fp); + copystruct(&nv, &stdview); + if (!sscanview(&nv, buf)) { + error(COMMAND, "rad error -- no such view?"); + return; + } + newview(&nv); +} + + getaim(s) /* aim camera */ char *s; { - extern double tan(), atan(); double zfact; VIEW nv; @@ -198,7 +298,6 @@ char *s; getrotate(s) /* rotate camera */ char *s; { - extern double normalize(), tan(), atan(); VIEW nv; FVECT v1; double angle, elev, zfact; @@ -244,16 +343,20 @@ register char *s; getexposure(s) /* get new exposure */ char *s; { - double atof(), pow(), fabs(); char buf[128]; register char *cp; - register PNODE *p; RECT r; int x, y; - double e; - + register PNODE *p = &ptrunk; + int adapt = 0; + double e = 1.0; +start: for (cp = s; isspace(*cp); cp++) ; + if (*cp == '@') { + adapt++; + goto start; + } if (*cp == '\0') { /* normalize to point */ if (dev->getcur == NULL) return; @@ -263,7 +366,6 @@ char *s; r.l = r.d = 0; r.r = hresolu; r.u = vresolu; p = findrect(x, y, &ptrunk, &r, -1); - e = 1.0; } else { if (*cp == '=') { /* absolute setting */ p = NULL; @@ -271,7 +373,7 @@ char *s; for (cp++; isspace(*cp); cp++) ; if (*cp == '\0') { /* interactive */ - sprintf(buf, "exposure (%lf): ", exposure); + sprintf(buf, "exposure (%f): ", exposure); (*dev->comout)(buf); (*dev->comin)(buf, NULL); for (cp = buf; isspace(*cp); cp++) @@ -279,9 +381,6 @@ char *s; if (*cp == '\0') return; } - } else { /* normalize to average */ - p = &ptrunk; - e = 1.0; } if (*cp == '+' || *cp == '-') /* f-stops */ e *= pow(2.0, atof(cp)); @@ -289,11 +388,14 @@ char *s; e *= atof(cp); } if (p != NULL) { /* relative setting */ - if (bright(p->v) <= FTINY) { + if (bright(p->v) < 1e-15) { error(COMMAND, "cannot normalize to zero"); return; } - e *= 0.5 / bright(p->v); + if (adapt) + e *= 106./pow(1.219+pow(luminance(p->v)/exposure,.4),2.5)/exposure; + else + e *= 0.5 / bright(p->v); } if (e <= FTINY || fabs(1.0 - e) <= FTINY) return; @@ -321,10 +423,10 @@ register union {int i; double d; COLOR C;} *ptr; (*dev->comout)(buf); (*dev->comin)(buf, NULL); if (sscanf(buf, "%d", &i0) != 1) - break; + return(0); } ptr->i = i0; - break; + return(1); case 'r': /* real */ if (sscanf(str, "%lf", &d0) != 1) { (*dev->comout)(dsc); @@ -332,22 +434,22 @@ register union {int i; double d; COLOR C;} *ptr; (*dev->comout)(buf); (*dev->comin)(buf, NULL); if (sscanf(buf, "%lf", &d0) != 1) - break; + return(0); } ptr->d = d0; - break; + return(1); case 'b': /* boolean */ if (sscanf(str, "%1s", buf) != 1) { (*dev->comout)(dsc); - sprintf(buf, " (%c): ", ptr->i ? 'y' : 'n'); + sprintf(buf, "? (%c): ", ptr->i ? 'y' : 'n'); (*dev->comout)(buf); (*dev->comin)(buf, NULL); if (buf[0] == '\0' || index("yY+1tTnN-0fF", buf[0]) == NULL) - break; + return(0); } ptr->i = index("yY+1tT", buf[0]) != NULL; - break; + return(1); case 'C': /* color */ if (sscanf(str, "%lf %lf %lf", &d0, &d1, &d2) != 3) { (*dev->comout)(dsc); @@ -358,10 +460,10 @@ register union {int i; double d; COLOR C;} *ptr; (*dev->comout)(buf); (*dev->comin)(buf, NULL); if (sscanf(buf, "%lf %lf %lf", &d0, &d1, &d2) != 3) - break; + return(0); } setcolor(ptr->C, d0, d1, d2); - break; + return(1); } } @@ -378,19 +480,20 @@ register char *s; extern double shadcert; extern COLOR ambval; extern double ambacc; - extern double minarad; extern int ambres; extern int ambdiv; extern int ambssamp; extern int ambounce; - extern int directinvis; + extern int directvis; extern double srcsizerat; extern int do_irrad; + extern double specjitter; + extern double specthresh; char buf[128]; if (s[0] == '\0') { (*dev->comout)( - "aa ab ad ar as av b dc di dj dt i lr lw sp st: "); + "aa ab ad ar as av b dc di dj ds dt i lr lw ps pt sj st: "); (*dev->comin)(buf, NULL); s = buf; } @@ -418,9 +521,9 @@ register char *s; case 't': /* threshold */ getparam(s+2, "direct threshold", 'r', &shadthresh); break; - case 'i': /* invisibility */ - getparam(s+2, "direct invisibility", - 'b', &directinvis); + case 'v': /* visibility */ + getparam(s+2, "direct visibility", + 'b', &directvis); break; case 's': /* sampling */ getparam(s+2, "direct sampling", 'r', &srcsizerat); @@ -438,10 +541,11 @@ register char *s; case 'a': /* ambient */ switch (s[1]) { case 'v': /* value */ - getparam(s+2, "ambient value", 'C', ambval); + getparam(s+2, "ambient value", 'C', (COLOR *)ambval); break; case 'a': /* accuracy */ - getparam(s+2, "ambient accuracy", 'r', &ambacc); + if (getparam(s+2, "ambient accuracy", 'r', &ambacc)) + setambacc(ambacc); break; case 'd': /* divisions */ getparam(s+2, "ambient divisions", 'i', &ambdiv); @@ -453,27 +557,39 @@ register char *s; getparam(s+2, "ambient bounces", 'i', &ambounce); break; case 'r': - getparam(s+2, "ambient resolution", 'i', &ambres); - minarad = ambres > 0 ? thescene.cusize/ambres : 0.0; + if (getparam(s+2, "ambient resolution", 'i', &ambres)) + setambres(ambres); break; default: goto badparam; } break; - case 's': /* sample */ + case 'p': /* pixel */ switch (s[1]) { - case 'p': /* pixel */ - getparam(s+2, "sample pixel", 'i', &psample); - pdepth = 0; + case 's': /* sample */ + if (getparam(s+2, "pixel sample", 'i', &psample)) + pdepth = 0; break; case 't': /* threshold */ - getparam(s+2, "sample threshold", 'r', &maxdiff); - pdepth = 0; + if (getparam(s+2, "pixel threshold", 'r', &maxdiff)) + pdepth = 0; break; default: goto badparam; } break; + case 's': /* specular */ + switch (s[1]) { + case 'j': /* jitter */ + getparam(s+2, "specular jitter", 'r', &specjitter); + break; + case 't': /* threshold */ + getparam(s+2, "specular threshold", 'r', &specthresh); + break; + default: + goto badparam; + } + break; case '\0': /* nothing */ break; default:; @@ -493,9 +609,8 @@ char *s; int x, y; RAY thisray; - if (sscanf(s, "%lf %lf %lf %lf %lf %lf", - &thisray.rorg[0], &thisray.rorg[1], &thisray.rorg[2], - &thisray.rdir[0], &thisray.rdir[1], &thisray.rdir[2]) != 6) { + if (!sscanvec(s, thisray.rorg) || + !sscanvec(sskip(sskip(sskip(s))), thisray.rdir)) { if (dev->getcur == NULL) return; @@ -521,8 +636,10 @@ char *s; if (thisray.ro == NULL) (*dev->comout)("ray hit nothing"); else { - sprintf(buf, "ray hit %s %s \"%s\"", - objptr(thisray.ro->omod)->oname, + sprintf(buf, "ray hit %s%s %s \"%s\"", + thisray.rod < 0.0 ? "back of " : "", + thisray.ro->omod == OVOID ? VOIDID : + objptr(thisray.ro->omod)->oname, ofun[thisray.ro->otype].funame, thisray.ro->oname); (*dev->comout)(buf); @@ -535,10 +652,11 @@ char *s; (*dev->comout)(buf); } (*dev->comin)(buf, NULL); - sprintf(buf, "with value (%.6g %.6g %.6g)", + sprintf(buf, "value (%.5g %.5g %.5g) (%.1fL)", colval(thisray.rcol,RED), colval(thisray.rcol,GRN), - colval(thisray.rcol,BLU)); + colval(thisray.rcol,BLU), + luminance(thisray.rcol)); (*dev->comout)(buf); } (*dev->comin)(buf, NULL); @@ -554,7 +672,11 @@ char *s; COLR *scanline; int y; - if (sscanf(s, "%s", buf) != 1 && buf[0] == '\0') { + while (isspace(*s)) + s++; + if (*s) + atos(buf, sizeof(buf), s); + else if (buf[0] == '\0') { error(COMMAND, "no file"); return; } @@ -564,6 +686,9 @@ char *s; error(COMMAND, errmsg); return; } +#ifdef MSDOS + setmode(fileno(fp), O_BINARY); +#endif (*dev->comout)("writing \""); (*dev->comout)(fname); (*dev->comout)("\"...\n"); @@ -581,7 +706,7 @@ char *s; fputaspect(dev->pixaspect, fp); fputformat(COLRFMT, fp); putc('\n', fp); - fputresolu(YMAJOR|YDECR, hresolu, vresolu, fp); + fprtresolu(hresolu, vresolu, fp); scanline = (COLR *)malloc(hresolu*sizeof(COLR)); if (scanline == NULL) {