--- ray/src/rt/rv2.c 1995/12/08 21:27:34 2.32 +++ ray/src/rt/rv2.c 2008/08/21 16:13:00 2.56 @@ -1,27 +1,27 @@ -/* Copyright (c) 1995 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: rv2.c,v 2.56 2008/08/21 16:13:00 greg 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 "source.h" +#include "ambient.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,19 +30,16 @@ static char SCCSid[] = "$SunId$ LBL"; #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; +void +getframe( /* get a new frame */ + char *s +) { if (getrect(s, &pframe) < 0) return; @@ -50,30 +47,33 @@ char *s; } -getrepaint(s) /* get area and repaint */ -char *s; +void +getrepaint( /* get area and repaint */ + char *s +) { RECT box; if (getrect(s, &box) < 0) return; - paintrect(&ptrunk, 0, 0, hresolu, vresolu, &box); + paintrect(&ptrunk, &box); } -getview(s) /* get/show view parameters */ -char *s; +void +getview( /* get/show view parameters */ + char *s +) { FILE *fp; char buf[128]; char *fname; int change = 0; - VIEW nv; + VIEW nv = ourview; while (isspace(*s)) s++; if (*s == '-') { /* command line parameters */ - copystruct(&nv, &ourview); if (sscanview(&nv, s)) newview(&nv); else @@ -101,8 +101,7 @@ char *s; if (buf[0] && buf[0] != ourview.type) { nv.type = buf[0]; change++; - } else - nv.type = ourview.type; + } sprintf(buf, "view point (%.6g %.6g %.6g): ", ourview.vp[0], ourview.vp[1], ourview.vp[2]); (*dev->comout)(buf); @@ -110,17 +109,17 @@ char *s; 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]); + ourview.vdir[0]*ourview.vdist, + ourview.vdir[1]*ourview.vdist, + ourview.vdir[2]*ourview.vdist); (*dev->comout)(buf); (*dev->comin)(buf, NULL); if (buf[0] == CTRL('C')) return; - if (sscanvec(buf, nv.vdir)) + if (sscanvec(buf, nv.vdir)) { + nv.vdist = 1.; 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); @@ -128,8 +127,6 @@ char *s; 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); @@ -137,9 +134,6 @@ char *s; 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, "fore and aft clipping plane (%.6g %.6g): ", ourview.vfore, ourview.vaft); (*dev->comout)(buf); @@ -147,9 +141,6 @@ char *s; 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); @@ -157,16 +148,15 @@ char *s; if (buf[0] == CTRL('C')) return; if (sscanf(buf, "%lf %lf", &nv.hoff, &nv.voff) == 2) change++; - else { - nv.hoff = ourview.hoff; nv.voff = ourview.voff; - } if (change) newview(&nv); } -lastview(s) /* return to a previous view */ -char *s; +void +lastview( /* return to a previous view */ + char *s +) { char buf[128]; char *fname; @@ -174,7 +164,7 @@ char *s; VIEW nv; if (sscanf(s, "%s", buf) == 1) { /* get parameters from a file */ - copystruct(&nv, &stdview); + nv = stdview; if ((fname = getpath(buf, "", R_OK)) == NULL || (success = viewfile(fname, &nv, NULL)) == -1) { sprintf(errmsg, "cannot open \"%s\"", buf); @@ -191,15 +181,17 @@ char *s; error(COMMAND, "no previous view"); return; } - copystruct(&nv, &ourview); - copystruct(&ourview, &oldview); - copystruct(&oldview, &nv); - newimage(); + nv = ourview; + ourview = oldview; + oldview = nv; + newimage(NULL); } -saveview(s) /* save view to rad file */ -char *s; +void +saveview( /* save view to rad file */ + char *s +) { char view[64]; char *fname; @@ -234,8 +226,10 @@ char *s; } -loadview(s) /* load view from rad file */ -char *s; +void +loadview( /* load view from rad file */ + char *s +) { char buf[512]; char *fname; @@ -266,7 +260,7 @@ char *s; buf[0] = '\0'; fgets(buf, sizeof(buf), fp); pclose(fp); - copystruct(&nv, &stdview); + nv = stdview; if (!sscanview(&nv, buf)) { error(COMMAND, "rad error -- no such view?"); return; @@ -275,28 +269,63 @@ char *s; } -getaim(s) /* aim camera */ -char *s; +void +getaim( /* aim camera */ + char *s +) { + VIEW nv = ourview; 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.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); } -getmove(s) /* move camera */ -char *s; +void +getfocus( /* set focus distance */ + char *s +) { + char buf[64]; + double dist; + + if (sscanf(s, "%lf", &dist) < 1) { + int x, y; + RAY thisray; + if (dev->getcur == NULL) + return; + (*dev->comout)("Pick focus point\n"); + if ((*dev->getcur)(&x, &y) == ABORT) + return; + if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, + &ourview, (x+.5)/hresolu, (y+.5)/vresolu)) < -FTINY) { + error(COMMAND, "not on image"); + return; + } + rayorigin(&thisray, PRIMARY, NULL, NULL); + if (!localhit(&thisray, &thescene)) { + error(COMMAND, "not a local object"); + return; + } + dist = thisray.rot; + } else if (dist <= .0) { + error(COMMAND, "focus distance must be positive"); + return; + } + ourview.vdist = dist; + sprintf(buf, "Focus distance set to %f\n", dist); + (*dev->comout)(buf); +} + + +void +getmove( /* move camera */ + char *s +) +{ FVECT vc; double mag; @@ -306,10 +335,12 @@ char *s; } -getrotate(s) /* rotate camera */ -char *s; +void +getrotate( /* rotate camera */ + char *s +) { - VIEW nv; + VIEW nv = ourview; FVECT v1; double angle, elev, zfact; @@ -318,25 +349,21 @@ char *s; error(COMMAND, "missing angle"); return; } - nv.type = ourview.type; - 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); normalize(v1); spinvector(nv.vdir, nv.vdir, v1, elev*(PI/180.)); } - nv.horiz = ourview.horiz; nv.vert = ourview.vert; zoomview(&nv, zfact); newview(&nv); } -getpivot(s) /* pivot viewpoint */ -register char *s; +void +getpivot( /* pivot viewpoint */ + char *s +) { FVECT vc; double angle, elev, mag; @@ -346,20 +373,21 @@ 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); } -getexposure(s) /* get new exposure */ -char *s; +void +getexposure( /* get new exposure */ + char *s +) { char buf[128]; - register char *cp; - RECT r; + char *cp; int x, y; - register PNODE *p = &ptrunk; + PNODE *p = &ptrunk; int adapt = 0; double e = 1.0; @@ -376,9 +404,7 @@ char *s; (*dev->comout)("Pick point for exposure\n"); if ((*dev->getcur)(&x, &y) == ABORT) return; - r.l = r.d = 0; - r.r = hresolu; r.u = vresolu; - p = findrect(x, y, &ptrunk, &r, -1); + p = findrect(x, y, &ptrunk, -1); } else { if (*cp == '=') { /* absolute setting */ p = NULL; @@ -417,13 +443,17 @@ char *s; redraw(); } +typedef union {int i; double d; COLOR C;} *MyUptr; -getparam(str, dsc, typ, ptr) /* get variable from user */ -char *str, *dsc; -int typ; -register union {int i; double d; COLOR C;} *ptr; +int +getparam( /* get variable from user */ + char *str, + char *dsc, + int typ, + void *p +) { - extern char *index(); + MyUptr ptr = (MyUptr)p; int i0; double d0, d1, d2; char buf[48]; @@ -439,7 +469,7 @@ register union {int i; double d; COLOR C;} *ptr; return(0); } ptr->i = i0; - return(1); + break; case 'r': /* real */ if (sscanf(str, "%lf", &d0) != 1) { (*dev->comout)(dsc); @@ -450,7 +480,7 @@ register union {int i; double d; COLOR C;} *ptr; return(0); } ptr->d = d0; - return(1); + break; case 'b': /* boolean */ if (sscanf(str, "%1s", buf) != 1) { (*dev->comout)(dsc); @@ -458,11 +488,11 @@ 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; - return(1); + ptr->i = strchr("yY+1tT", buf[0]) != NULL; + break; case 'C': /* color */ if (sscanf(str, "%lf %lf %lf", &d0, &d1, &d2) != 3) { (*dev->comout)(dsc); @@ -476,53 +506,42 @@ register union {int i; double d; COLOR C;} *ptr; return(0); } setcolor(ptr->C, d0, d1, d2); - return(1); + break; + default: + return(0); /* shouldn't happen */ } + newparam++; + return(1); } -setparam(s) /* get/set program parameter */ -register char *s; +void +setparam( /* get/set program parameter */ + 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 COLOR cextinction; - extern double salbedo; - extern double seccg; - extern double ssampdist; - extern int directvis; - extern double srcsizerat; - extern int do_irrad; - extern double specjitter; - extern double specthresh; - extern int backvis; char buf[128]; if (s[0] == '\0') { (*dev->comout)( - "aa ab ad ar as av b dc dv dj ds dt i lr lw me ma mg ms ps pt sj st bv: "); + "aa ab ad ar as av aw b bv dc dv dj ds dt i lr lw me ma mg ms ps pt sj st u: "); (*dev->comin)(buf, NULL); s = buf; } switch (s[0]) { + case 'u': /* uncorrelated sampling */ + getparam(s+1, "uncorrelated sampling", 'b', + (void *)&rand_samp); + break; 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; @@ -531,20 +550,24 @@ 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; @@ -553,41 +576,54 @@ register char *s; case 'b': /* back faces or black and white */ switch (s[1]) { case 'v': /* back face visibility */ - getparam(s+2, "back face visibility", 'b', &backvis); + 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', &greyscale); + 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 */ - if (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: @@ -598,17 +634,19 @@ register char *s; switch (s[1]) { case 'e': /* extinction coefficient */ getparam(s+2, "extinction coefficient", 'C', - (COLOR *)cextinction); + (void *)cextinction); break; case 'a': /* scattering albedo */ - getparam(s+2, "scattering albedo", 'r', &salbedo); + getparam(s+2, "scattering albedo", 'C', + (void *)salbedo); break; case 'g': /* scattering eccentricity */ - getparam(s+2, "scattering eccentricity", 'r', &seccg); + getparam(s+2, "scattering eccentricity", 'r', + (void *)&seccg); break; case 's': /* sampling distance */ getparam(s+2, "mist sampling distance", 'r', - &ssampdist); + (void *)&ssampdist); break; default: goto badparam; @@ -617,11 +655,13 @@ register char *s; 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: @@ -631,10 +671,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; @@ -652,17 +694,20 @@ badparam: } -traceray(s) /* trace a single ray */ -char *s; +void +traceray( /* 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; @@ -681,19 +726,34 @@ char *s; return; } - rayorigin(&thisray, NULL, PRIMARY, 1.0); - - rayvalue(&thisray); + ray_trace(&thisray); if (thisray.ro == NULL) (*dev->comout)("ray hit nothing"); else { - sprintf(buf, "ray hit %s%s %s \"%s\"", - thisray.rod < 0.0 ? "back of " : "", - thisray.ro->omod == OVOID ? VOIDID : - objptr(thisray.ro->omod)->oname, + OBJREC *mat = NULL; + OBJREC *mod = NULL; + char matspec[256]; + matspec[0] = '\0'; + if (thisray.ro->omod != OVOID) { + mod = objptr(thisray.ro->omod); + mat = findmaterial(mod); + } + if (thisray.rod < 0.0) + strcpy(matspec, "back of "); + if (mod != NULL) { + strcat(matspec, mod->oname); + if (mat != mod && mat != NULL) + sprintf(matspec+strlen(matspec), + " (%s)", mat->oname); + } else + strcat(matspec, VOIDID); + sprintf(buf, "ray hit %s %s \"%s\"", matspec, 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) @@ -705,7 +765,7 @@ char *s; (*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), @@ -716,8 +776,10 @@ char *s; } -writepict(s) /* write the picture to a file */ -char *s; +void +writepict( /* write the picture to a file */ + char *s +) { static char buf[128]; char *fname; @@ -739,9 +801,7 @@ 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"); @@ -754,6 +814,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) @@ -774,7 +835,7 @@ char *s; if (fwritecolrs(scanline, hresolu, fp) < 0) break; } - free((char *)scanline); + free((void *)scanline); if (fclose(fp) < 0) error(COMMAND, "write error"); }