--- ray/src/rt/rv2.c 2005/06/14 03:34:14 2.51 +++ ray/src/rt/rv2.c 2025/06/07 05:09:46 2.80 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rv2.c,v 2.51 2005/06/14 03:34:14 greg Exp $"; +static const char RCSid[] = "$Id: rv2.c,v 2.80 2025/06/07 05:09:46 greg Exp $"; #endif /* * rv2.c - command routines used in tracing a view. @@ -13,10 +13,11 @@ static const char RCSid[] = "$Id: rv2.c,v 2.51 2005/06 #include #include "platform.h" +#include "rtprocess.h" /* win_popen() */ #include "ray.h" -#include "source.h" #include "ambient.h" #include "otypes.h" +#include "otspecial.h" #include "rpaint.h" extern int psample; /* pixel sample size */ @@ -24,11 +25,7 @@ extern double maxdiff; /* max. sample difference */ #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 +#define sscanvec(s,v) (sscanf(s,FVFORMAT,v,v+1,v+2)==3) extern char rifname[128]; /* rad input file name */ @@ -36,7 +33,7 @@ extern char *progname; extern char *octname; -extern void +void getframe( /* get a new frame */ char *s ) @@ -47,7 +44,7 @@ getframe( /* get a new frame */ } -extern void +void getrepaint( /* get area and repaint */ char *s ) @@ -56,12 +53,12 @@ getrepaint( /* get area and repaint */ if (getrect(s, &box) < 0) return; - paintrect(&ptrunk, 0, 0, hresolu, vresolu, &box); + paintrect(&ptrunk, &box); } -extern void -getview( /* get/show view parameters */ +void +getview( /* get/show/save view parameters */ char *s ) { @@ -80,7 +77,7 @@ getview( /* get/show view parameters */ error(COMMAND, "bad view option(s)"); return; } - if (sscanf(s, "%s", buf) == 1) { /* write parameters to a file */ + if (nextword(buf, sizeof(buf), s) != NULL) { /* write to a file */ if ((fname = getpath(buf, NULL, 0)) == NULL || (fp = fopen(fname, "a")) == NULL) { sprintf(errmsg, "cannot open \"%s\"", buf); @@ -153,7 +150,7 @@ getview( /* get/show view parameters */ } -extern void +void lastview( /* return to a previous view */ char *s ) @@ -162,8 +159,8 @@ lastview( /* return to a previous view */ char *fname; int success; VIEW nv; - - if (sscanf(s, "%s", buf) == 1) { /* get parameters from a file */ + /* get parameters from a file */ + if (nextword(buf, sizeof(buf), s) != NULL) { nv = stdview; if ((fname = getpath(buf, "", R_OK)) == NULL || (success = viewfile(fname, &nv, NULL)) == -1) { @@ -184,11 +181,11 @@ lastview( /* return to a previous view */ nv = ourview; ourview = oldview; oldview = nv; - newimage(); + newimage(NULL); } -extern void +void saveview( /* save view to rad file */ char *s ) @@ -204,11 +201,7 @@ saveview( /* save view to rad file */ } s = sskip(s); } - while (isspace(*s)) - s++; - if (*s) - atos(rifname, sizeof(rifname), s); - else if (rifname[0] == '\0') { + if (nextword(rifname, sizeof(rifname), s) == NULL && !rifname[0]) { error(COMMAND, "no previous rad file"); return; } @@ -226,7 +219,7 @@ saveview( /* save view to rad file */ } -extern void +void loadview( /* load view from rad file */ char *s ) @@ -241,9 +234,7 @@ loadview( /* load view from rad file */ s = sskip(s); else strcat(buf, "1"); - if (*s) - atos(rifname, sizeof(rifname), s); - else if (rifname[0] == '\0') { + if (nextword(rifname, sizeof(rifname), s) == NULL && !rifname[0]) { error(COMMAND, "no previous rad file"); return; } @@ -269,7 +260,7 @@ loadview( /* load view from rad file */ } -extern void +void getaim( /* aim camera */ char *s ) @@ -284,12 +275,12 @@ getaim( /* aim camera */ } -extern void +void getfocus( /* set focus distance */ char *s ) { - FVECT vc; + char buf[64]; double dist; if (sscanf(s, "%lf", &dist) < 1) { @@ -316,10 +307,12 @@ getfocus( /* set focus distance */ return; } ourview.vdist = dist; + sprintf(buf, "Focus distance set to %f\n", dist); + (*dev->comout)(buf); } -extern void +void getmove( /* move camera */ char *s ) @@ -333,13 +326,12 @@ getmove( /* move camera */ } -extern void +void getrotate( /* rotate camera */ char *s ) { VIEW nv = ourview; - FVECT v1; double angle, elev, zfact; elev = 0.0; zfact = 1.0; @@ -348,19 +340,17 @@ getrotate( /* rotate camera */ return; } 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 (elev != 0.0) + geodesic(nv.vdir, nv.vdir, nv.vup, elev*(PI/180.), GEOD_RAD); + zoomview(&nv, zfact); newview(&nv); } -extern void +void getpivot( /* pivot viewpoint */ - register char *s + char *s ) { FVECT vc; @@ -377,16 +367,69 @@ getpivot( /* pivot viewpoint */ } -extern void +void +getorigin( /* origin viewpoint */ + char *s +) +{ + VIEW nv = ourview; + FVECT voff; + int i; + double d; + /* check for view origin shift */ + if (sscanf(s, "%lf", &d) == 1) { + double d_fore=d, d_right=0, d_up=0; + sscanf(s, "%*f %lf %lf", &d_right, &d_up); + d_right /= sqrt(nv.hn2); + for (i = 3; i--; ) + nv.vp[i] += d_fore*nv.vdir[i] + + d_right*nv.hvec[i] + + d_up*nv.vup[i]; + } else { /* else pick new origin */ + int x, y; + RAY thisray; + if (dev->getcur == NULL) + return; + (*dev->comout)("Pick point on surface for new origin\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; + } + if (thisray.rod < 0.0) /* don't look through other side */ + flipsurface(&thisray); + VSUM(nv.vp, thisray.rop, thisray.ron, 20.0*FTINY); + VCOPY(nv.vdir, thisray.ron); + d = DOT(nv.vdir, nv.vup); + if (d*d >= 1.-2.*FTINY) { + /* need different up vector */ + nv.vup[0] = nv.vup[1] = nv.vup[2] = 0.0; + for (i = 3; i--; ) + if (nv.vdir[i]*nv.vdir[i] < 0.34) + break; + nv.vup[i] = 1.; + } + } + newview(&nv); +} + + +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; @@ -403,9 +446,7 @@ getexposure( /* get new exposure */ (*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; @@ -422,12 +463,13 @@ getexposure( /* get new exposure */ return; } } - if (*cp == '+' || *cp == '-') /* f-stops */ + if ((*cp == '+') | (*cp == '-')) /* f-stops */ e *= pow(2.0, atof(cp)); else /* multiplier */ e *= atof(cp); } if (p != NULL) { /* relative setting */ + compavg(p); if (bright(p->v) < 1e-15) { error(COMMAND, "cannot normalize to zero"); return; @@ -446,7 +488,7 @@ getexposure( /* get new exposure */ typedef union {int i; double d; COLOR C;} *MyUptr; -extern int +int getparam( /* get variable from user */ char *str, char *dsc, @@ -454,7 +496,7 @@ getparam( /* get variable from user */ void *p ) { - register MyUptr ptr = (MyUptr)p; + MyUptr ptr = (MyUptr)p; int i0; double d0, d1, d2; char buf[48]; @@ -469,8 +511,10 @@ getparam( /* get variable from user */ if (sscanf(buf, "%d", &i0) != 1) return(0); } + if (ptr->i == i0) + return(0); ptr->i = i0; - return(1); + break; case 'r': /* real */ if (sscanf(str, "%lf", &d0) != 1) { (*dev->comout)(dsc); @@ -480,20 +524,26 @@ getparam( /* get variable from user */ if (sscanf(buf, "%lf", &d0) != 1) return(0); } + if (FRELEQ(ptr->d, d0)) + return(0); ptr->d = d0; - return(1); + break; 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' || - strchr("yY+1tTnN-0fF", buf[0]) == NULL) + if (buf[0] == '\0') return(0); } - ptr->i = strchr("yY+1tT", buf[0]) != NULL; - return(1); + if (strchr("yY+1tTnN-0fF", buf[0]) == NULL) + return(0); + i0 = strchr("yY+1tT", buf[0]) != NULL; + if (ptr->i == i0) + return(0); + ptr->i = i0; + break; case 'C': /* color */ if (sscanf(str, "%lf %lf %lf", &d0, &d1, &d2) != 3) { (*dev->comout)(dsc); @@ -506,23 +556,31 @@ getparam( /* get variable from user */ if (sscanf(buf, "%lf %lf %lf", &d0, &d1, &d2) != 3) return(0); } + if (FRELEQ(colval(ptr->C,RED), d0) && + FRELEQ(colval(ptr->C,GRN), d1) && + FRELEQ(colval(ptr->C,BLU), d2)) + return(0); setcolor(ptr->C, d0, d1, d2); - return(1); + break; + default: + return(0); /* shouldn't happen */ } - return 0; /* nothing matched */ + newparam++; + return(1); } -extern void +void setparam( /* get/set program parameter */ - register char *s + char *s ) { + int prev_newp = newparam; char buf[128]; if (s[0] == '\0') { (*dev->comout)( - "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: "); + "aa ab ad ar as av aw b bv dc dv dj ds dt i lr lw me ma mg ms ps pt ss st u: "); (*dev->comin)(buf, NULL); s = buf; } @@ -534,12 +592,10 @@ setparam( /* get/set program parameter */ case 'l': /* limit */ switch (s[1]) { case 'w': /* weight */ - getparam(s+2, "limit weight", 'r', - (void *)&minweight); + getparam(s+2, "limit weight", 'r', &minweight); break; case 'r': /* reflection */ - getparam(s+2, "limit reflection", 'i', - (void *)&maxdepth); + getparam(s+2, "limit reflection", 'i', &maxdepth); break; default: goto badparam; @@ -548,24 +604,19 @@ setparam( /* get/set program parameter */ case 'd': /* direct */ switch (s[1]) { case 'j': /* jitter */ - getparam(s+2, "direct jitter", 'r', - (void *)&dstrsrc); + getparam(s+2, "direct jitter", 'r', &dstrsrc); break; case 'c': /* certainty */ - getparam(s+2, "direct certainty", 'r', - (void *)&shadcert); + getparam(s+2, "direct certainty", 'r', &shadcert); break; case 't': /* threshold */ - getparam(s+2, "direct threshold", 'r', - (void *)&shadthresh); + getparam(s+2, "direct threshold", 'r', &shadthresh); break; case 'v': /* visibility */ - getparam(s+2, "direct visibility", 'b', - (void *)&directvis); + getparam(s+2, "direct visibility", 'b', &directvis); break; case 's': /* sampling */ - getparam(s+2, "direct sampling", 'r', - (void *)&srcsizerat); + getparam(s+2, "direct sampling", 'r', &srcsizerat); break; default: goto badparam; @@ -581,47 +632,39 @@ setparam( /* get/set program parameter */ 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); + getparam(s+1, "black and white", 'b', &greyscale); + newparam = prev_newp; break; default: goto badparam; } break; case 'i': /* irradiance */ - getparam(s+1, "irradiance", 'b', - (void *)&do_irrad); + getparam(s+1, "irradiance", 'b', &do_irrad); break; case 'a': /* ambient */ switch (s[1]) { case 'v': /* value */ - getparam(s+2, "ambient value", 'C', - (void *)ambval); + getparam(s+2, "ambient value", 'C', ambval); break; case 'w': /* weight */ - getparam(s+2, "ambient value weight", 'i', - (void *)&ambvwt); + getparam(s+2, "ambient value weight", 'i', &ambvwt); break; case 'a': /* accuracy */ - if (getparam(s+2, "ambient accuracy", 'r', - (void *)&ambacc)) + if (getparam(s+2, "ambient accuracy", 'r', &ambacc)) setambacc(ambacc); break; case 'd': /* divisions */ - getparam(s+2, "ambient divisions", 'i', - (void *)&ambdiv); + getparam(s+2, "ambient divisions", 'i', &ambdiv); break; case 's': /* samples */ - getparam(s+2, "ambient super-samples", 'i', - (void *)&ambssamp); + getparam(s+2, "ambient super-samples", 'i', &ambssamp); break; case 'b': /* bounces */ - getparam(s+2, "ambient bounces", 'i', - (void *)&ambounce); + getparam(s+2, "ambient bounces", 'i', &ambounce); break; case 'r': - if (getparam(s+2, "ambient resolution", 'i', - (void *)&ambres)) + if (getparam(s+2, "ambient resolution", 'i', &ambres)) setambres(ambres); break; default: @@ -631,20 +674,16 @@ setparam( /* get/set program parameter */ case 'm': /* medium */ switch (s[1]) { case 'e': /* extinction coefficient */ - getparam(s+2, "extinction coefficient", 'C', - (void *)cextinction); + getparam(s+2, "extinction coefficient", 'C', cextinction); break; case 'a': /* scattering albedo */ - getparam(s+2, "scattering albedo", 'C', - (void *)salbedo); + getparam(s+2, "scattering albedo", 'C', salbedo); break; case 'g': /* scattering eccentricity */ - getparam(s+2, "scattering eccentricity", 'r', - (void *)&seccg); + getparam(s+2, "scattering eccentricity", 'r', &seccg); break; case 's': /* sampling distance */ - getparam(s+2, "mist sampling distance", 'r', - (void *)&ssampdist); + getparam(s+2, "mist sampling distance", 'r', &ssampdist); break; default: goto badparam; @@ -653,28 +692,25 @@ setparam( /* get/set program parameter */ case 'p': /* pixel */ switch (s[1]) { case 's': /* sample */ - if (getparam(s+2, "pixel sample", 'i', - (void *)&psample)) + if (getparam(s+2, "pixel sample", 'i', &psample)) pdepth = 0; break; case 't': /* threshold */ - if (getparam(s+2, "pixel threshold", 'r', - (void *)&maxdiff)) + if (getparam(s+2, "pixel threshold", 'r', &maxdiff)) pdepth = 0; break; default: goto badparam; } + newparam = prev_newp; break; case 's': /* specular */ switch (s[1]) { - case 'j': /* jitter */ - getparam(s+2, "specular jitter", 'r', - (void *)&specjitter); + case 's': /* sampling */ + getparam(s+2, "specular sampling", 'r', &specjitter); break; case 't': /* threshold */ - getparam(s+2, "specular threshold", 'r', - (void *)&specthresh); + getparam(s+2, "specular threshold", 'r', &specthresh); break; default: goto badparam; @@ -692,19 +728,20 @@ badparam: } -extern void -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; + RAY thisray; + char buf[512]; + COLOR col; thisray.rmax = 0.0; if (!sscanvec(s, thisray.rorg) || !sscanvec(sskip2(s,3), thisray.rdir)) { + int x, y; if (dev->getcur == NULL) return; @@ -723,20 +760,20 @@ char *s; return; } - rayorigin(&thisray, PRIMARY, NULL, NULL); - - rayvalue(&thisray); + ray_trace(&thisray); if (thisray.ro == NULL) (*dev->comout)("ray hit nothing"); else { OBJREC *mat = NULL; - OBJREC *mod = NULL;; + OBJREC *mod = NULL; char matspec[256]; + OBJREC *ino; + matspec[0] = '\0'; if (thisray.ro->omod != OVOID) { mod = objptr(thisray.ro->omod); - mat = findmaterial(mod); + mat = findmaterial(thisray.ro); } if (thisray.rod < 0.0) strcpy(matspec, "back of "); @@ -755,41 +792,39 @@ char *s; ofun[ino->otype].funame, ino->oname); (*dev->comout)(buf); (*dev->comin)(buf, NULL); - if (thisray.rot >= FHUGE) + if (thisray.rot >= FHUGE*.99) (*dev->comout)("at infinity"); else { - sprintf(buf, "at (%.6g %.6g %.6g) (%.6g)", + sprintf(buf, "at (%.3f %.3f %.3f) (%.6g)", thisray.rop[0], thisray.rop[1], - thisray.rop[2], thisray.rt); + thisray.rop[2], raydistance(&thisray)); (*dev->comout)(buf); } (*dev->comin)(buf, NULL); - sprintf(buf, "value (%.5g %.5g %.5g) (%.3gL)", - colval(thisray.rcol,RED), - colval(thisray.rcol,GRN), - colval(thisray.rcol,BLU), - luminance(thisray.rcol)); + scolor_rgb(col, thisray.rcol); + sprintf(buf, "value (%.4g %.4g %.4g) (%.3gL)", + colval(col,RED), + colval(col,GRN), + colval(col,BLU), + luminance(col)); (*dev->comout)(buf); } (*dev->comin)(buf, NULL); } -extern void -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; FILE *fp; COLR *scanline; int y; - - while (isspace(*s)) - s++; - if (*s) - atos(buf, sizeof(buf), s); - else if (buf[0] == '\0') { + /* XXX relies on words.c 2.11 behavior */ + if (nextword(buf, sizeof(buf), s) == NULL && !buf[0]) { error(COMMAND, "no file"); return; } @@ -817,6 +852,7 @@ char *s; fputexpos(exposure, fp); if (dev->pixaspect != 1.0) fputaspect(dev->pixaspect, fp); + fputprims(stdprims, fp); fputformat(COLRFMT, fp); putc('\n', fp); fprtresolu(hresolu, vresolu, fp);