--- ray/src/rt/rv2.c 1989/06/07 08:35:26 1.5 +++ ray/src/rt/rv2.c 1993/12/15 16:59:14 2.22 @@ -1,4 +1,4 @@ -/* Copyright (c) 1987 Regents of the University of California */ +/* Copyright (c) 1993 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -18,68 +18,70 @@ 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; getframe(s) /* get a new frame */ char *s; { - int x0, y0, x1, y1; - - if (!strcmp("all", s)) { - pframe.l = pframe.d = 0; - pframe.r = ourview.hresolu; - pframe.u = ourview.vresolu; - pdepth = 0; + if (getrect(s, &pframe) < 0) return; - } - if (sscanf(s, "%d %d %d %d", &x0, &y0, &x1, &y1) != 4) { - if (dev->getcur == NULL) - return; - (*dev->comout)("Pick first corner\n"); - if ((*dev->getcur)(&x0, &y0) == ABORT) - return; - (*dev->comout)("Pick second corner\n"); - if ((*dev->getcur)(&x1, &y1) == ABORT) - return; - } - if (x0 < x1) { - pframe.l = x0; - pframe.r = x1; - } else { - pframe.l = x1; - pframe.r = x0; - } - if (y0 < y1) { - pframe.d = y0; - pframe.u = y1; - } else { - pframe.d = y1; - pframe.u = y0; - } - if (pframe.l < 0) pframe.l = 0; - if (pframe.d < 0) pframe.d = 0; - if (pframe.r > ourview.hresolu) pframe.r = ourview.hresolu; - if (pframe.u > ourview.vresolu) pframe.u = ourview.vresolu; pdepth = 0; } +getrepaint(s) /* get area and repaint */ +char *s; +{ + RECT box; + + if (getrect(s, &box) < 0) + return; + paintrect(&ptrunk, 0, 0, hresolu, vresolu, &box); +} + + getview(s) /* get/show view parameters */ char *s; { FILE *fp; char buf[128]; - char *fname, *getpath(); + char *fname; 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)) == NULL || + if ((fname = getpath(buf, NULL, 0)) == NULL || (fp = fopen(fname, "a")) == NULL) { sprintf(errmsg, "cannot open \"%s\"", buf); error(COMMAND, errmsg); @@ -87,14 +89,15 @@ char *s; } fputs(progname, fp); fprintview(&ourview, fp); - fputs("\n", fp); + fputs(sskip(s), fp); + putc('\n', fp); fclose(fp); return; } sprintf(buf, "view type (%c): ", ourview.type); (*dev->comout)(buf); - (*dev->comin)(buf); - if (buf[0] == CTRL(C)) return; + (*dev->comin)(buf, NULL); + if (buf[0] == CTRL('C')) return; if (buf[0] && buf[0] != ourview.type) { nv.type = buf[0]; change++; @@ -103,49 +106,49 @@ char *s; sprintf(buf, "view point (%.6g %.6g %.6g): ", ourview.vp[0], ourview.vp[1], ourview.vp[2]); (*dev->comout)(buf); - (*dev->comin)(buf); - if (buf[0] == CTRL(C)) return; - if (sscanf(buf, "%lf %lf %lf", &nv.vp[0], &nv.vp[1], &nv.vp[2]) == 3) + (*dev->comin)(buf, NULL); + if (buf[0] == CTRL('C')) return; + if (sscanvec(buf, nv.vp)) change++; else VCOPY(nv.vp, ourview.vp); sprintf(buf, "view direction (%.6g %.6g %.6g): ", ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]); (*dev->comout)(buf); - (*dev->comin)(buf); - if (buf[0] == CTRL(C)) return; - if (sscanf(buf,"%lf %lf %lf",&nv.vdir[0],&nv.vdir[1],&nv.vdir[2]) == 3) + (*dev->comin)(buf, NULL); + if (buf[0] == CTRL('C')) return; + if (sscanvec(buf, nv.vdir)) change++; else VCOPY(nv.vdir, ourview.vdir); sprintf(buf, "view up (%.6g %.6g %.6g): ", ourview.vup[0], ourview.vup[1], ourview.vup[2]); (*dev->comout)(buf); - (*dev->comin)(buf); - if (buf[0] == CTRL(C)) return; - if (sscanf(buf,"%lf %lf %lf",&nv.vup[0],&nv.vup[1],&nv.vup[2]) == 3) + (*dev->comin)(buf, NULL); + if (buf[0] == CTRL('C')) return; + if (sscanvec(buf, nv.vup)) change++; else VCOPY(nv.vup, ourview.vup); sprintf(buf, "view horiz and vert size (%.6g %.6g): ", ourview.horiz, ourview.vert); (*dev->comout)(buf); - (*dev->comin)(buf); - if (buf[0] == CTRL(C)) return; + (*dev->comin)(buf, NULL); + if (buf[0] == CTRL('C')) return; if (sscanf(buf, "%lf %lf", &nv.horiz, &nv.vert) == 2) change++; else { nv.horiz = ourview.horiz; nv.vert = ourview.vert; } - sprintf(buf, "x and y resolution (%d %d): ", - ourview.hresolu, ourview.vresolu); + sprintf(buf, "view shift and lift (%.6g %.6g): ", + ourview.hoff, ourview.voff); (*dev->comout)(buf); - (*dev->comin)(buf); - if (buf[0] == CTRL(C)) return; - if (sscanf(buf, "%d %d", &nv.hresolu, &nv.vresolu) == 2) + (*dev->comin)(buf, NULL); + if (buf[0] == CTRL('C')) return; + if (sscanf(buf, "%lf %lf", &nv.hoff, &nv.voff) == 2) change++; else { - nv.hresolu = ourview.hresolu; nv.vresolu = ourview.vresolu; + nv.hoff = ourview.hoff; nv.voff = ourview.voff; } if (change) newview(&nv); @@ -156,14 +159,14 @@ lastview(s) /* return to a previous view */ char *s; { char buf[128]; - char *fname, *getpath(); + char *fname; int success; VIEW nv; if (sscanf(s, "%s", buf) == 1) { /* get parameters from a file */ - bcopy(&stdview, &nv, sizeof(VIEW)); - if ((fname = getpath(buf, NULL)) == NULL || - (success = viewfile(fname, &nv)) == -1) { + copystruct(&nv, &stdview); + if ((fname = getpath(buf, "", R_OK)) == NULL || + (success = viewfile(fname, &nv, NULL)) == -1) { sprintf(errmsg, "cannot open \"%s\"", buf); error(COMMAND, errmsg); return; @@ -174,85 +177,108 @@ char *s; newview(&nv); return; } - if (oldview.hresolu == 0) { /* no old view! */ + if (oldview.type == 0) { /* no old view! */ error(COMMAND, "no previous view"); return; } - bcopy(&ourview, &nv, sizeof(VIEW)); - bcopy(&oldview, &ourview, sizeof(VIEW)); - bcopy(&nv, &oldview, sizeof(VIEW)); + copystruct(&nv, &ourview); + copystruct(&ourview, &oldview); + copystruct(&oldview, &nv); newimage(); } -getinterest(s, direc, vec, mp) /* get area of interest */ +saveview(s) /* save view to rad file */ char *s; -int direc; -FVECT vec; -double *mp; { - int x, y; - RAY thisray; - register int i; + char view[64]; + char *fname; + FILE *fp; - if (sscanf(s, "%lf", mp) != 1) - *mp = 1.0; - else if (*mp < -FTINY) /* negative zoom is reduction */ - *mp = -1.0 / *mp; - else if (*mp <= FTINY) { /* too small */ - error(COMMAND, "illegal magnification"); - return(-1); - } - if (sscanf(s, "%*lf %lf %lf %lf", &vec[0], &vec[1], &vec[2]) != 3) { - if (dev->getcur == NULL) - return(-1); - (*dev->comout)("Pick view center\n"); - if ((*dev->getcur)(&x, &y) == ABORT) - return(-1); - rayview(thisray.rorg, thisray.rdir, &ourview, x+.5, y+.5); - if (!direc || ourview.type == VT_PAR) { - rayorigin(&thisray, NULL, PRIMARY, 1.0); - if (!localhit(&thisray, &thescene)) { - error(COMMAND, "not a local object"); - return(-1); - } + if (*atos(view, sizeof(view), s)) { + if (isint(view)) { + error(COMMAND, "cannot write view by number"); + return; } - if (direc) - if (ourview.type == VT_PAR) - for (i = 0; i < 3; i++) - vec[i] = thisray.rop[i] - ourview.vp[i]; - else - VCOPY(vec, thisray.rdir); - else - VCOPY(vec, thisray.rop); - } else if (direc) - for (i = 0; i < 3; i++) - vec[i] -= ourview.vp[i]; - return(0); + 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; if (getinterest(s, 1, nv.vdir, &zfact) < 0) return; + nv.type = ourview.type; VCOPY(nv.vp, ourview.vp); VCOPY(nv.vup, ourview.vup); - nv.hresolu = ourview.hresolu; nv.vresolu = ourview.vresolu; - if ((nv.type = ourview.type) == VT_PAR) { - nv.horiz = ourview.horiz / zfact; - nv.vert = ourview.vert / zfact; - } else { - nv.horiz = atan(tan(ourview.horiz*(PI/180./2.))/zfact) / - (PI/180./2.); - nv.vert = atan(tan(ourview.vert*(PI/180./2.))/zfact) / - (PI/180./2.); - } + nv.hoff = ourview.hoff; nv.voff = ourview.voff; + nv.horiz = ourview.horiz; nv.vert = ourview.vert; + zoomview(&nv, zfact); newview(&nv); } @@ -272,7 +298,6 @@ char *s; getrotate(s) /* rotate camera */ char *s; { - extern double normalize(), tan(), atan(); VIEW nv; FVECT v1; double angle, elev, zfact; @@ -282,24 +307,18 @@ char *s; error(COMMAND, "missing angle"); return; } + nv.type = ourview.type; VCOPY(nv.vp, ourview.vp); VCOPY(nv.vup, ourview.vup); - nv.hresolu = ourview.hresolu; nv.vresolu = ourview.vresolu; + nv.hoff = ourview.hoff; nv.voff = ourview.voff; spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.)); if (elev != 0.0) { fcross(v1, nv.vdir, ourview.vup); normalize(v1); spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.)); } - if ((nv.type = ourview.type) == VT_PAR) { - nv.horiz = ourview.horiz / zfact; - nv.vert = ourview.vert / zfact; - } else { - nv.horiz = atan(tan(ourview.horiz*(PI/180./2.))/zfact) / - (PI/180./2.); - nv.vert = atan(tan(ourview.vert*(PI/180./2.))/zfact) / - (PI/180./2.); - } + nv.horiz = ourview.horiz; nv.vert = ourview.vert; + zoomview(&nv, zfact); newview(&nv); } @@ -324,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; @@ -341,9 +364,8 @@ char *s; if ((*dev->getcur)(&x, &y) == ABORT) return; r.l = r.d = 0; - r.r = ourview.hresolu; r.u = ourview.vresolu; + r.r = hresolu; r.u = vresolu; p = findrect(x, y, &ptrunk, &r, -1); - e = 1.0; } else { if (*cp == '=') { /* absolute setting */ p = NULL; @@ -351,17 +373,14 @@ 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); + (*dev->comin)(buf, NULL); for (cp = buf; isspace(*cp); cp++) ; if (*cp == '\0') return; } - } else { /* normalize to average */ - p = &ptrunk; - e = 1.0; } if (*cp == '+' || *cp == '-') /* f-stops */ e *= pow(2.0, atof(cp)); @@ -369,11 +388,14 @@ char *s; e *= atof(cp); } if (p != NULL) { /* relative setting */ - if (intens(p->v) <= FTINY) { + if (bright(p->v) < 1e-15) { error(COMMAND, "cannot normalize to zero"); return; } - e *= 0.5 / intens(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; @@ -383,6 +405,69 @@ char *s; } +getparam(str, dsc, typ, ptr) /* get variable from user */ +char *str, *dsc; +int typ; +register union {int i; double d; COLOR C;} *ptr; +{ + extern char *index(); + int i0; + double d0, d1, d2; + char buf[48]; + + switch (typ) { + case 'i': /* integer */ + if (sscanf(str, "%d", &i0) != 1) { + (*dev->comout)(dsc); + sprintf(buf, " (%d): ", ptr->i); + (*dev->comout)(buf); + (*dev->comin)(buf, NULL); + if (sscanf(buf, "%d", &i0) != 1) + return(0); + } + ptr->i = i0; + return(1); + case 'r': /* real */ + if (sscanf(str, "%lf", &d0) != 1) { + (*dev->comout)(dsc); + sprintf(buf, " (%.6g): ", ptr->d); + (*dev->comout)(buf); + (*dev->comin)(buf, NULL); + if (sscanf(buf, "%lf", &d0) != 1) + return(0); + } + ptr->d = d0; + return(1); + case 'b': /* boolean */ + if (sscanf(str, "%1s", buf) != 1) { + (*dev->comout)(dsc); + 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) + return(0); + } + ptr->i = index("yY+1tT", buf[0]) != NULL; + return(1); + case 'C': /* color */ + if (sscanf(str, "%lf %lf %lf", &d0, &d1, &d2) != 3) { + (*dev->comout)(dsc); + sprintf(buf, " (%.6g %.6g %.6g): ", + colval(ptr->C,RED), + colval(ptr->C,GRN), + colval(ptr->C,BLU)); + (*dev->comout)(buf); + (*dev->comin)(buf, NULL); + if (sscanf(buf, "%lf %lf %lf", &d0, &d1, &d2) != 3) + return(0); + } + setcolor(ptr->C, d0, d1, d2); + return(1); + } +} + + setparam(s) /* get/set program parameter */ register char *s; { @@ -392,46 +477,34 @@ register char *s; extern int maxdepth; extern double dstrsrc; extern double shadthresh; + extern double shadcert; extern COLOR ambval; extern double ambacc; - extern double minarad; extern int ambres; extern int ambdiv; extern int ambssamp; extern int ambounce; - int i0; - double d0, d1, d2; + 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 dj dt lr lw sd sp: "); - (*dev->comin)(buf); + (*dev->comout)( + "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; } switch (s[0]) { case 'l': /* limit */ switch (s[1]) { case 'w': /* weight */ - if (sscanf(s+2, "%lf", &d0) != 1) { - sprintf(buf, "limit weight (%.6g): ", - minweight); - (*dev->comout)(buf); - (*dev->comin)(buf); - if (sscanf(buf, "%lf", &d0) != 1) - break; - } - minweight = d0; + getparam(s+2, "limit weight", 'r', &minweight); break; case 'r': /* reflection */ - if (sscanf(s+2, "%d", &i0) != 1) { - sprintf(buf, "limit reflection (%d): ", - maxdepth); - (*dev->comout)(buf); - (*dev->comin)(buf); - if (sscanf(buf, "%d", &i0) != 1) - break; - } - maxdepth = i0; + getparam(s+2, "limit reflection", 'i', &maxdepth); break; default: goto badparam; @@ -440,141 +513,88 @@ register char *s; case 'd': /* direct */ switch (s[1]) { case 'j': /* jitter */ - if (sscanf(s+2, "%lf", &d0) != 1) { - sprintf(buf, "direct jitter (%.6g): ", - dstrsrc); - (*dev->comout)(buf); - (*dev->comin)(buf); - if (sscanf(buf, "%lf", &d0) != 1) - break; - } - dstrsrc = d0; + getparam(s+2, "direct jitter", 'r', &dstrsrc); break; + case 'c': /* certainty */ + getparam(s+2, "direct certainty", 'r', &shadcert); + break; case 't': /* threshold */ - if (sscanf(s+2, "%lf", &d0) != 1) { - sprintf(buf, "direct threshold (%.6g): ", - shadthresh); - (*dev->comout)(buf); - (*dev->comin)(buf); - if (sscanf(buf, "%lf", &d0) != 1) - break; - } - shadthresh = d0; + getparam(s+2, "direct threshold", 'r', &shadthresh); break; + case 'v': /* visibility */ + getparam(s+2, "direct visibility", + 'b', &directvis); + break; + case 's': /* sampling */ + getparam(s+2, "direct sampling", 'r', &srcsizerat); + break; default: goto badparam; } break; + case 'b': /* black and white */ + getparam(s+1, "black and white", 'b', &greyscale); + break; + case 'i': /* irradiance */ + getparam(s+1, "irradiance", 'b', &do_irrad); + break; case 'a': /* ambient */ switch (s[1]) { case 'v': /* value */ - if (sscanf(s+2, "%lf %lf %lf", &d0, &d1, &d2) != 3) { - sprintf(buf, - "ambient value (%.6g %.6g %.6g): ", - colval(ambval,RED), - colval(ambval,GRN), - colval(ambval,BLU)); - (*dev->comout)(buf); - (*dev->comin)(buf); - if (sscanf(buf, "%lf %lf %lf", - &d0, &d1, &d2) != 3) - break; - } - setcolor(ambval, d0, d1, d2); + getparam(s+2, "ambient value", 'C', (COLOR *)ambval); break; case 'a': /* accuracy */ - if (sscanf(s+2, "%lf", &d0) != 1) { - sprintf(buf, "ambient accuracy (%.6g): ", - ambacc); - (*dev->comout)(buf); - (*dev->comin)(buf); - if (sscanf(buf, "%lf", &d0) != 1) - break; - } - ambacc = d0; + if (getparam(s+2, "ambient accuracy", 'r', &ambacc)) + setambacc(ambacc); break; case 'd': /* divisions */ - if (sscanf(s+2, "%d", &i0) != 1) { - sprintf(buf, "ambient divisions (%d): ", - ambdiv); - (*dev->comout)(buf); - (*dev->comin)(buf); - if (sscanf(buf, "%d", &i0) != 1) - break; - } - ambdiv = i0; + getparam(s+2, "ambient divisions", 'i', &ambdiv); break; case 's': /* samples */ - if (sscanf(s+2, "%d", &i0) != 1) { - sprintf(buf, "ambient super-samples (%d): ", - ambssamp); - (*dev->comout)(buf); - (*dev->comin)(buf); - if (sscanf(buf, "%d", &i0) != 1) - break; - } - ambssamp = i0; + getparam(s+2, "ambient super-samples", 'i', &ambssamp); break; case 'b': /* bounces */ - if (sscanf(s+2, "%d", &i0) != 1) { - sprintf(buf, "ambient bounces (%d): ", - ambounce); - (*dev->comout)(buf); - (*dev->comin)(buf); - if (sscanf(buf, "%d", &i0) != 1) - break; - } - ambounce = i0; + getparam(s+2, "ambient bounces", 'i', &ambounce); break; case 'r': - if (sscanf(s+2, "%d", &i0) != 1) { - sprintf(buf, "ambient resolution (%d): ", - ambres); - (*dev->comout)(buf); - (*dev->comin)(buf); - if (sscanf(buf, "%d", &i0) != 1) - break; - } - ambres = i0; - 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 */ - if (sscanf(s+2, "%d", &i0) != 1) { - sprintf(buf, "sample pixel (%d): ", psample); - (*dev->comout)(buf); - (*dev->comin)(buf); - if (sscanf(buf, "%d", &i0) != 1) - break; - } - psample = i0; - pdepth = 0; + case 's': /* sample */ + if (getparam(s+2, "pixel sample", 'i', &psample)) + pdepth = 0; break; - case 'd': /* difference */ - if (sscanf(s+2, "%lf", &d0) != 1) { - sprintf(buf, "sample difference (%.6g): ", - maxdiff); - (*dev->comout)(buf); - (*dev->comin)(buf); - if (sscanf(buf, "%lf", &d0) != 1) - break; - } - maxdiff = d0; - pdepth = 0; + case 't': /* threshold */ + 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:; badparam: + *sskip(s) = '\0'; sprintf(errmsg, "%s: unknown variable", s); error(COMMAND, errmsg); break; @@ -589,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; @@ -599,8 +618,12 @@ char *s; if ((*dev->getcur)(&x, &y) == ABORT) return; - rayview(thisray.rorg, thisray.rdir, &ourview, x+.5, y+.5); - + if (viewray(thisray.rorg, thisray.rdir, &ourview, + (x+.5)/hresolu, (y+.5)/vresolu) < 0) { + error(COMMAND, "not on image"); + return; + } + } else if (normalize(thisray.rdir) == 0.0) { error(COMMAND, "zero ray direction"); return; @@ -614,11 +637,12 @@ char *s; (*dev->comout)("ray hit nothing"); else { sprintf(buf, "ray hit %s %s \"%s\"", - objptr(thisray.ro->omod)->oname, + thisray.ro->omod == OVOID ? VOIDID : + objptr(thisray.ro->omod)->oname, ofun[thisray.ro->otype].funame, thisray.ro->oname); (*dev->comout)(buf); - (*dev->comin)(buf); + (*dev->comin)(buf, NULL); if (thisray.rot >= FHUGE) (*dev->comout)("at infinity"); else { @@ -626,14 +650,15 @@ char *s; thisray.rop[1], thisray.rop[2]); (*dev->comout)(buf); } - (*dev->comin)(buf); - sprintf(buf, "with value (%.6g %.6g %.6g)", + (*dev->comin)(buf, NULL); + 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); + (*dev->comin)(buf, NULL); } @@ -641,42 +666,60 @@ writepict(s) /* write the picture to a file */ char *s; { static char buf[128]; - char *fname, *getpath(); + char *fname; FILE *fp; 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; } - if ((fname = getpath(buf, NULL)) == NULL || + if ((fname = getpath(buf, NULL, 0)) == NULL || (fp = fopen(fname, "w")) == NULL) { sprintf(errmsg, "cannot open \"%s\"", buf); error(COMMAND, errmsg); return; } +#ifdef MSDOS + setmode(fileno(fp), O_BINARY); +#endif (*dev->comout)("writing \""); (*dev->comout)(fname); (*dev->comout)("\"...\n"); /* write header */ fputs(progname, fp); fprintview(&ourview, fp); - fputs("\n", fp); + if (octname != NULL) + fprintf(fp, " %s\n", octname); + else + putc('\n', fp); + fprintf(fp, "SOFTWARE= %s\n", VersionID); if (exposure != 1.0) - fprintf(fp, "EXPOSURE=%e\n", exposure); - fprintf(fp, "\n-Y %d +X %d\n", ourview.vresolu, ourview.hresolu); + fputexpos(exposure, fp); + if (dev->pixaspect != 1.0) + fputaspect(dev->pixaspect, fp); + fputformat(COLRFMT, fp); + putc('\n', fp); + fprtresolu(hresolu, vresolu, fp); - scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR)); - if (scanline == NULL) - error(SYSTEM, "out of memory in writepict"); - for (y = ourview.vresolu-1; y >= 0; y--) { - getpictcolrs(y, scanline, &ptrunk, - ourview.hresolu, ourview.vresolu); - if (fwritecolrs(scanline, ourview.hresolu, fp) < 0) + scanline = (COLR *)malloc(hresolu*sizeof(COLR)); + if (scanline == NULL) { + error(COMMAND, "not enough memory!"); + fclose(fp); + unlink(fname); + return; + } + for (y = vresolu-1; y >= 0; y--) { + getpictcolrs(y, scanline, &ptrunk, hresolu, vresolu); + if (fwritecolrs(scanline, hresolu, fp) < 0) break; } + free((char *)scanline); if (fclose(fp) < 0) error(COMMAND, "write error"); - free((char *)scanline); }