--- ray/src/rt/rv2.c 2003/02/22 02:07:29 2.38 +++ ray/src/rt/rv2.c 2010/10/08 22:55:49 2.60 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rv2.c,v 2.38 2003/02/22 02:07:29 greg Exp $"; +static const char RCSid[] = "$Id: rv2.c,v 2.60 2010/10/08 22:55:49 greg Exp $"; #endif /* * rv2.c - command routines used in tracing a view. @@ -7,71 +7,18 @@ static const char RCSid[] = "$Id: rv2.c,v 2.38 2003/02 * External symbols declared in rpaint.h */ -/* ==================================================================== - * The Radiance Software License, Version 1.0 - * - * Copyright (c) 1990 - 2002 The Regents of the University of California, - * through Lawrence Berkeley National Laboratory. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. The end-user documentation included with the redistribution, - * if any, must include the following acknowledgment: - * "This product includes Radiance software - * (http://radsite.lbl.gov/) - * developed by the Lawrence Berkeley National Laboratory - * (http://www.lbl.gov/)." - * Alternately, this acknowledgment may appear in the software itself, - * if and wherever such third-party acknowledgments normally appear. - * - * 4. The names "Radiance," "Lawrence Berkeley National Laboratory" - * and "The Regents of the University of California" must - * not be used to endorse or promote products derived from this - * software without prior written permission. For written - * permission, please contact radiance@radsite.lbl.gov. - * - * 5. Products derived from this software may not be called "Radiance", - * nor may "Radiance" appear in their name, without prior written - * permission of Lawrence Berkeley National Laboratory. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of Lawrence Berkeley National Laboratory. For more - * information on Lawrence Berkeley National Laboratory, please see - * . - */ +#include "copyright.h" -#include "ray.h" +#include +#include +#include "platform.h" +#include "ray.h" +#include "source.h" +#include "ambient.h" #include "otypes.h" - #include "rpaint.h" -#include - extern int psample; /* pixel sample size */ extern double maxdiff; /* max. sample difference */ @@ -90,8 +37,9 @@ extern char *octname; void -getframe(s) /* get a new frame */ -char *s; +getframe( /* get a new frame */ + char *s +) { if (getrect(s, &pframe) < 0) return; @@ -100,31 +48,32 @@ char *s; void -getrepaint(s) /* get area and repaint */ -char *s; +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); } void -getview(s) /* get/show view parameters */ -char *s; +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 @@ -152,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); @@ -161,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); @@ -179,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); @@ -188,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); @@ -198,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); @@ -208,17 +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); } void -lastview(s) /* return to a previous view */ -char *s; +lastview( /* return to a previous view */ + char *s +) { char buf[128]; char *fname; @@ -226,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); @@ -243,16 +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); } void -saveview(s) /* save view to rad file */ -char *s; +saveview( /* save view to rad file */ + char *s +) { char view[64]; char *fname; @@ -288,8 +227,9 @@ char *s; void -loadview(s) /* load view from rad file */ -char *s; +loadview( /* load view from rad file */ + char *s +) { char buf[512]; char *fname; @@ -320,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; @@ -330,29 +270,62 @@ char *s; void -getaim(s) /* aim camera */ -char *s; +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); } void -getmove(s) /* move camera */ -char *s; +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; @@ -363,10 +336,11 @@ char *s; void -getrotate(s) /* rotate camera */ -char *s; +getrotate( /* rotate camera */ + char *s +) { - VIEW nv; + VIEW nv = ourview; FVECT v1; double angle, elev, zfact; @@ -375,26 +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); } void -getpivot(s) /* pivot viewpoint */ -register char *s; +getpivot( /* pivot viewpoint */ + char *s +) { FVECT vc; double angle, elev, mag; @@ -411,14 +380,14 @@ register char *s; void -getexposure(s) /* get new exposure */ -char *s; +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; @@ -435,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; @@ -460,6 +427,7 @@ char *s; e *= atof(cp); } if (p != NULL) { /* relative setting */ + compavg(p); if (bright(p->v) < 1e-15) { error(COMMAND, "cannot normalize to zero"); return; @@ -477,14 +445,17 @@ char *s; } typedef union {int i; double d; COLOR C;} *MyUptr; +#define FEQ(x,y) (fabs((x)-(y)) <= FTINY) int -getparam(str, dsc, typ, p) /* get variable from user */ -char *str, *dsc; -int typ; -void *p; +getparam( /* get variable from user */ + char *str, + char *dsc, + int typ, + void *p +) { - register MyUptr ptr = (MyUptr)p; + MyUptr ptr = (MyUptr)p; int i0; double d0, d1, d2; char buf[48]; @@ -499,8 +470,10 @@ void *p; 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); @@ -510,20 +483,26 @@ void *p; if (sscanf(buf, "%lf", &d0) != 1) return(0); } + if (FEQ(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' || - index("yY+1tTnN-0fF", buf[0]) == NULL) + if (buf[0] == '\0') return(0); } - ptr->i = index("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); @@ -536,25 +515,39 @@ void *p; if (sscanf(buf, "%lf %lf %lf", &d0, &d1, &d2) != 3) return(0); } + if (FEQ(colval(ptr->C,RED), d0) && + FEQ(colval(ptr->C,GRN), d1) && + FEQ(colval(ptr->C,BLU), d2)) + return(0); setcolor(ptr->C, d0, d1, d2); - return(1); + break; + default: + return(0); /* shouldn't happen */ } + newparam++; + return(1); } void -setparam(s) /* get/set program parameter */ -register char *s; +setparam( /* get/set program parameter */ + char *s +) { + int prev_newp = newparam; char buf[128]; if (s[0] == '\0') { (*dev->comout)( - "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: "); + "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; } 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 */ @@ -607,6 +600,7 @@ register char *s; case 'n': case 'N': case 'f': case 'F': case '0': case '-': getparam(s+1, "black and white", 'b', (void *)&greyscale); + newparam = prev_newp; break; default: goto badparam; @@ -689,11 +683,12 @@ register char *s; default: goto badparam; } + newparam = prev_newp; break; case 's': /* specular */ switch (s[1]) { - case 'j': /* jitter */ - getparam(s+2, "specular jitter", 'r', + case 's': /* sampling */ + getparam(s+2, "specular sampling", 'r', (void *)&specjitter); break; case 't': /* threshold */ @@ -717,8 +712,9 @@ badparam: void -traceray(s) /* trace a single ray */ -char *s; +traceray( /* trace a single ray */ + char *s +) { char buf[128]; int x, y; @@ -747,17 +743,29 @@ 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) @@ -786,8 +794,9 @@ char *s; void -writepict(s) /* write the picture to a file */ -char *s; +writepict( /* write the picture to a file */ + char *s +) { static char buf[128]; char *fname; @@ -809,9 +818,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");