--- ray/src/rt/rv2.c 1993/06/02 21:04:02 2.16 +++ ray/src/rt/rv2.c 2003/07/21 22:30:19 2.42 @@ -1,27 +1,25 @@ -/* Copyright (c) 1993 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: rv2.c,v 2.42 2003/07/21 22:30:19 schorsch Exp $"; #endif - /* * rv2.c - command routines used in tracing a view. * - * 3/24/87 + * External symbols declared in rpaint.h */ -#include "ray.h" +#include "copyright.h" -#include "octree.h" +#include +#include +#include "platform.h" +#include "ray.h" #include "otypes.h" - #include "rpaint.h" -#include "resolu.h" +extern int psample; /* pixel sample size */ +extern double maxdiff; /* max. sample difference */ -#include - #define CTRL(c) ((c)-'@') #ifdef SMLFLT @@ -30,12 +28,13 @@ static char SCCSid[] = "$SunId$ LBL"; #define sscanvec(s,v) (sscanf(s,"%lf %lf %lf",v,v+1,v+2)==3) #endif +extern char rifname[128]; /* rad input file name */ -extern char VersionID[]; extern char *progname; extern char *octname; +void getframe(s) /* get a new frame */ char *s; { @@ -45,6 +44,7 @@ char *s; } +void getrepaint(s) /* get area and repaint */ char *s; { @@ -56,6 +56,7 @@ char *s; } +void getview(s) /* get/show view parameters */ char *s; { @@ -68,7 +69,7 @@ char *s; while (isspace(*s)) s++; if (*s == '-') { /* command line parameters */ - copystruct(&nv, &ourview); + nv = ourview; if (sscanview(&nv, s)) newview(&nv); else @@ -85,7 +86,7 @@ char *s; fputs(progname, fp); fprintview(&ourview, fp); fputs(sskip(s), fp); - fputs("\n", fp); + putc('\n', fp); fclose(fp); return; } @@ -135,6 +136,16 @@ char *s; else { nv.horiz = ourview.horiz; nv.vert = ourview.vert; } + sprintf(buf, "fore and aft clipping plane (%.6g %.6g): ", + ourview.vfore, ourview.vaft); + (*dev->comout)(buf); + (*dev->comin)(buf, NULL); + if (buf[0] == CTRL('C')) return; + if (sscanf(buf, "%lf %lf", &nv.vfore, &nv.vaft) == 2) + change++; + else { + nv.vfore = ourview.vfore; nv.vaft = ourview.vaft; + } sprintf(buf, "view shift and lift (%.6g %.6g): ", ourview.hoff, ourview.voff); (*dev->comout)(buf); @@ -150,6 +161,7 @@ char *s; } +void lastview(s) /* return to a previous view */ char *s; { @@ -159,8 +171,8 @@ char *s; VIEW nv; if (sscanf(s, "%s", buf) == 1) { /* get parameters from a file */ - copystruct(&nv, &stdview); - if ((fname = getpath(buf, NULL, 0)) == NULL || + nv = stdview; + if ((fname = getpath(buf, "", R_OK)) == NULL || (success = viewfile(fname, &nv, NULL)) == -1) { sprintf(errmsg, "cannot open \"%s\"", buf); error(COMMAND, errmsg); @@ -176,13 +188,93 @@ char *s; error(COMMAND, "no previous view"); return; } - copystruct(&nv, &ourview); - copystruct(&ourview, &oldview); - copystruct(&oldview, &nv); + nv = ourview; + ourview = oldview; + oldview = nv; newimage(); } +void +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); +} + + +void +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); + nv = stdview; + if (!sscanview(&nv, buf)) { + error(COMMAND, "rad error -- no such view?"); + return; + } + newview(&nv); +} + + +void getaim(s) /* aim camera */ char *s; { @@ -194,13 +286,15 @@ char *s; nv.type = ourview.type; VCOPY(nv.vp, ourview.vp); VCOPY(nv.vup, ourview.vup); - nv.hoff = ourview.hoff; nv.voff = ourview.voff; nv.horiz = ourview.horiz; nv.vert = ourview.vert; + nv.vfore = ourview.vfore; nv.vaft = ourview.vaft; + nv.hoff = ourview.hoff; nv.voff = ourview.voff; zoomview(&nv, zfact); newview(&nv); } +void getmove(s) /* move camera */ char *s; { @@ -213,6 +307,7 @@ char *s; } +void getrotate(s) /* rotate camera */ char *s; { @@ -229,6 +324,7 @@ char *s; VCOPY(nv.vp, ourview.vp); VCOPY(nv.vup, ourview.vup); nv.hoff = ourview.hoff; nv.voff = ourview.voff; + nv.vfore = ourview.vfore; nv.vaft = ourview.vaft; spinvector(nv.vdir, ourview.vdir, ourview.vup, angle*(PI/180.)); if (elev != 0.0) { fcross(v1, nv.vdir, ourview.vup); @@ -241,6 +337,7 @@ char *s; } +void getpivot(s) /* pivot viewpoint */ register char *s; { @@ -252,12 +349,13 @@ register char *s; error(COMMAND, "missing angle"); return; } - if (getinterest(sskip(sskip(s)), 0, vc, &mag) < 0) + if (getinterest(sskip2(s,2), 0, vc, &mag) < 0) return; moveview(angle, elev, mag, vc); } +void getexposure(s) /* get new exposure */ char *s; { @@ -268,12 +366,13 @@ char *s; register PNODE *p = &ptrunk; int adapt = 0; double e = 1.0; -start: + for (cp = s; isspace(*cp); cp++) ; if (*cp == '@') { adapt++; - goto start; + while (isspace(*++cp)) + ; } if (*cp == '\0') { /* normalize to point */ if (dev->getcur == NULL) @@ -322,13 +421,15 @@ start: redraw(); } +typedef union {int i; double d; COLOR C;} *MyUptr; -getparam(str, dsc, typ, ptr) /* get variable from user */ +int +getparam(str, dsc, typ, p) /* get variable from user */ char *str, *dsc; int typ; -register union {int i; double d; COLOR C;} *ptr; +void *p; { - extern char *index(); + register MyUptr ptr = (MyUptr)p; int i0; double d0, d1, d2; char buf[48]; @@ -363,10 +464,10 @@ register union {int i; double d; COLOR C;} *ptr; (*dev->comout)(buf); (*dev->comin)(buf, NULL); if (buf[0] == '\0' || - index("yY+1tTnN-0fF", buf[0]) == NULL) + strchr("yY+1tTnN-0fF", buf[0]) == NULL) return(0); } - ptr->i = index("yY+1tT", buf[0]) != NULL; + ptr->i = strchr("yY+1tT", buf[0]) != NULL; return(1); case 'C': /* color */ if (sscanf(str, "%lf %lf %lf", &d0, &d1, &d2) != 3) { @@ -386,32 +487,15 @@ register union {int i; double d; COLOR C;} *ptr; } +void setparam(s) /* get/set program parameter */ register char *s; { - extern int psample; - extern double maxdiff; - extern double minweight; - extern int maxdepth; - extern double dstrsrc; - extern double shadthresh; - extern double shadcert; - extern COLOR ambval; - extern double ambacc; - extern int ambres; - extern int ambdiv; - extern int ambssamp; - extern int ambounce; - 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 ds dt i lr lw ps pt sj st: "); + "aa ab ad ar as av aw b dc dv dj ds dt i lr lw me ma mg ms ps pt sj st bv: "); (*dev->comin)(buf, NULL); s = buf; } @@ -419,10 +503,12 @@ register char *s; case 'l': /* limit */ switch (s[1]) { case 'w': /* weight */ - getparam(s+2, "limit weight", 'r', &minweight); + getparam(s+2, "limit weight", 'r', + (void *)&minweight); break; case 'r': /* reflection */ - getparam(s+2, "limit reflection", 'i', &maxdepth); + getparam(s+2, "limit reflection", 'i', + (void *)&maxdepth); break; default: goto badparam; @@ -431,64 +517,118 @@ register char *s; case 'd': /* direct */ switch (s[1]) { case 'j': /* jitter */ - getparam(s+2, "direct jitter", 'r', &dstrsrc); + getparam(s+2, "direct jitter", 'r', + (void *)&dstrsrc); break; case 'c': /* certainty */ - getparam(s+2, "direct certainty", 'r', &shadcert); + getparam(s+2, "direct certainty", 'r', + (void *)&shadcert); break; case 't': /* threshold */ - getparam(s+2, "direct threshold", 'r', &shadthresh); + getparam(s+2, "direct threshold", 'r', + (void *)&shadthresh); break; case 'v': /* visibility */ - getparam(s+2, "direct visibility", - 'b', &directvis); + getparam(s+2, "direct visibility", 'b', + (void *)&directvis); break; case 's': /* sampling */ - getparam(s+2, "direct sampling", 'r', &srcsizerat); + getparam(s+2, "direct sampling", 'r', + (void *)&srcsizerat); break; default: goto badparam; } break; - case 'b': /* black and white */ - getparam(s+1, "black and white", 'b', &greyscale); + case 'b': /* back faces or black and white */ + switch (s[1]) { + case 'v': /* back face visibility */ + getparam(s+2, "back face visibility", 'b', + (void *)&backvis); + break; + case '\0': /* black and white */ + case ' ': + case 'y': case 'Y': case 't': case 'T': case '1': case '+': + case 'n': case 'N': case 'f': case 'F': case '0': case '-': + getparam(s+1, "black and white", 'b', + (void *)&greyscale); + break; + default: + goto badparam; + } break; case 'i': /* irradiance */ - getparam(s+1, "irradiance", 'b', &do_irrad); + getparam(s+1, "irradiance", 'b', + (void *)&do_irrad); break; case 'a': /* ambient */ switch (s[1]) { case 'v': /* value */ - getparam(s+2, "ambient value", 'C', (COLOR *)ambval); + getparam(s+2, "ambient value", 'C', + (void *)ambval); break; + case 'w': /* weight */ + getparam(s+2, "ambient value weight", 'i', + (void *)&ambvwt); + break; case 'a': /* accuracy */ - getparam(s+2, "ambient accuracy", 'r', &ambacc); + if (getparam(s+2, "ambient accuracy", 'r', + (void *)&ambacc)) + setambacc(ambacc); break; case 'd': /* divisions */ - getparam(s+2, "ambient divisions", 'i', &ambdiv); + getparam(s+2, "ambient divisions", 'i', + (void *)&ambdiv); break; case 's': /* samples */ - getparam(s+2, "ambient super-samples", 'i', &ambssamp); + getparam(s+2, "ambient super-samples", 'i', + (void *)&ambssamp); break; case 'b': /* bounces */ - getparam(s+2, "ambient bounces", 'i', &ambounce); + getparam(s+2, "ambient bounces", 'i', + (void *)&ambounce); break; case 'r': - if (getparam(s+2, "ambient resolution", 'i', &ambres)) + if (getparam(s+2, "ambient resolution", 'i', + (void *)&ambres)) setambres(ambres); break; default: goto badparam; } break; + case 'm': /* medium */ + switch (s[1]) { + case 'e': /* extinction coefficient */ + getparam(s+2, "extinction coefficient", 'C', + (void *)cextinction); + break; + case 'a': /* scattering albedo */ + getparam(s+2, "scattering albedo", 'C', + (void *)salbedo); + break; + case 'g': /* scattering eccentricity */ + getparam(s+2, "scattering eccentricity", 'r', + (void *)&seccg); + break; + case 's': /* sampling distance */ + getparam(s+2, "mist sampling distance", 'r', + (void *)&ssampdist); + break; + default: + goto badparam; + } + break; case 'p': /* pixel */ switch (s[1]) { case 's': /* sample */ - if (getparam(s+2, "pixel sample", 'i', &psample)) + if (getparam(s+2, "pixel sample", 'i', + (void *)&psample)) pdepth = 0; break; case 't': /* threshold */ - if (getparam(s+2, "pixel threshold", 'r', &maxdiff)) + if (getparam(s+2, "pixel threshold", 'r', + (void *)&maxdiff)) pdepth = 0; break; default: @@ -498,10 +638,12 @@ register char *s; case 's': /* specular */ switch (s[1]) { case 'j': /* jitter */ - getparam(s+2, "specular jitter", 'r', &specjitter); + getparam(s+2, "specular jitter", 'r', + (void *)&specjitter); break; case 't': /* threshold */ - getparam(s+2, "specular threshold", 'r', &specthresh); + getparam(s+2, "specular threshold", 'r', + (void *)&specthresh); break; default: goto badparam; @@ -519,15 +661,19 @@ badparam: } +void traceray(s) /* trace a single ray */ char *s; { char buf[128]; int x, y; + OBJREC *ino; RAY thisray; + thisray.rmax = 0.0; + if (!sscanvec(s, thisray.rorg) || - !sscanvec(sskip(sskip(sskip(s))), thisray.rdir)) { + !sscanvec(sskip2(s,3), thisray.rdir)) { if (dev->getcur == NULL) return; @@ -535,8 +681,8 @@ char *s; if ((*dev->getcur)(&x, &y) == ABORT) return; - if (viewray(thisray.rorg, thisray.rdir, &ourview, - (x+.5)/hresolu, (y+.5)/vresolu) < 0) { + if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, + &ourview, (x+.5)/hresolu, (y+.5)/vresolu)) < -FTINY) { error(COMMAND, "not on image"); return; } @@ -553,21 +699,27 @@ 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); + if ((ino = objptr(thisray.robj)) != thisray.ro) + sprintf(buf+strlen(buf), " in %s \"%s\"", + ofun[ino->otype].funame, ino->oname); (*dev->comout)(buf); (*dev->comin)(buf, NULL); if (thisray.rot >= FHUGE) (*dev->comout)("at infinity"); else { - sprintf(buf, "at (%.6g %.6g %.6g)", thisray.rop[0], - thisray.rop[1], thisray.rop[2]); + sprintf(buf, "at (%.6g %.6g %.6g) (%.6g)", + thisray.rop[0], thisray.rop[1], + thisray.rop[2], thisray.rt); (*dev->comout)(buf); } (*dev->comin)(buf, NULL); - sprintf(buf, "value (%.5g %.5g %.5g) (%.1fL)", + sprintf(buf, "value (%.5g %.5g %.5g) (%.3gL)", colval(thisray.rcol,RED), colval(thisray.rcol,GRN), colval(thisray.rcol,BLU), @@ -578,6 +730,7 @@ char *s; } +void writepict(s) /* write the picture to a file */ char *s; { @@ -587,7 +740,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; } @@ -597,13 +754,12 @@ char *s; error(COMMAND, errmsg); return; } -#ifdef MSDOS - setmode(fileno(fp), O_BINARY); -#endif + SET_FILE_BINARY(fp); (*dev->comout)("writing \""); (*dev->comout)(fname); (*dev->comout)("\"...\n"); /* write header */ + newheader("RADIANCE", fp); fputs(progname, fp); fprintview(&ourview, fp); if (octname != NULL) @@ -611,6 +767,7 @@ char *s; else putc('\n', fp); fprintf(fp, "SOFTWARE= %s\n", VersionID); + fputnow(fp); if (exposure != 1.0) fputexpos(exposure, fp); if (dev->pixaspect != 1.0) @@ -631,7 +788,7 @@ char *s; if (fwritecolrs(scanline, hresolu, fp) < 0) break; } - free((char *)scanline); + free((void *)scanline); if (fclose(fp) < 0) error(COMMAND, "write error"); }