--- ray/src/rt/rv3.c 2008/09/05 19:45:41 2.27 +++ ray/src/rt/rv3.c 2012/03/20 03:37:08 2.37 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rv3.c,v 2.27 2008/09/05 19:45:41 greg Exp $"; +static const char RCSid[] = "$Id: rv3.c,v 2.37 2012/03/20 03:37:08 greg Exp $"; #endif /* * rv3.c - miscellaneous routines for rview. @@ -16,17 +16,23 @@ static const char RCSid[] = "$Id: rv3.c,v 2.27 2008/09 #include "random.h" #ifndef WFLUSH -#define WFLUSH 64 /* flush after this many rays */ +#define WFLUSH 64 /* flush after this many primary rays */ #endif +#ifndef WFLUSH1 +#define WFLUSH1 512 /* or this many total rays */ +#endif + #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 -static unsigned long niflush; /* flushes since newimage() */ +extern int ray_pnprocs; +static RNUMBER niflush; /* flushes since newimage() */ + int getrect( /* get a box */ char *s, @@ -172,13 +178,10 @@ paint( /* compute and paint a rectangle */ PNODE *p ) { - extern int ray_pnprocs; - static unsigned long lastflush = 0; static RAY thisray; - int flushintvl; double h, v; - if (p->xmax - p->xmin <= 0 || p->ymax - p->ymin <= 0) { /* empty */ + if ((p->xmax <= p->xmin) | (p->ymax <= p->ymin)) { /* empty */ p->x = p->xmin; p->y = p->ymin; setcolor(p->v, 0.0, 0.0, 0.0); @@ -191,15 +194,18 @@ paint( /* compute and paint a rectangle */ if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview, h/hresolu, v/vresolu)) < -FTINY) { setcolor(thisray.rcol, 0.0, 0.0, 0.0); - } else { - int rval; + } else if (!ray_pnprocs) { /* immediate mode */ + ray_trace(&thisray); + } else { /* queuing mode */ + int rval; rayorigin(&thisray, PRIMARY, NULL, NULL); - thisray.rno = (unsigned long)p; + thisray.rno = (RNUMBER)p; rval = ray_pqueue(&thisray); if (!rval) return(0); if (rval < 0) return(-1); + /* get node for returned ray */ p = (PNODE *)thisray.rno; } @@ -208,17 +214,27 @@ paint( /* compute and paint a rectangle */ recolor(p); /* paint it */ - if (ambounce <= 0) /* shall we check for input? */ - flushintvl = ray_pnprocs*WFLUSH; - else if (niflush < WFLUSH) - flushintvl = ray_pnprocs*niflush/(ambounce+1); - else - flushintvl = ray_pnprocs*WFLUSH/(ambounce+1); + if (dev->flush != NULL) { /* shall we check for input? */ + static RNUMBER lastflush = 0; + RNUMBER counter = raynum; + int flushintvl; + if (!ray_pnprocs) { + counter = nrays; + flushintvl = WFLUSH1; + } else if (ambounce == 0) + flushintvl = ray_pnprocs*WFLUSH; + else if (niflush < WFLUSH) + flushintvl = ray_pnprocs*niflush/(ambounce+1); + else + flushintvl = ray_pnprocs*WFLUSH/(ambounce+1); + if (lastflush > counter) + lastflush = 0; /* counter wrapped */ - if (dev->flush != NULL && raynum - lastflush >= flushintvl) { - lastflush = raynum; - (*dev->flush)(); - niflush++; + if (counter - lastflush >= flushintvl) { + lastflush = counter; + (*dev->flush)(); + niflush++; + } } return(1); } @@ -230,7 +246,9 @@ waitrays(void) /* finish up pending rays */ int nwaited = 0; int rval; RAY raydone; - + + if (!ray_pnprocs) /* immediate mode? */ + return(0); while ((rval = ray_presult(&raydone, 0)) > 0) { PNODE *p = (PNODE *)raydone.rno; copycolor(p->v, raydone.rcol); @@ -249,17 +267,10 @@ newimage( /* start a new image */ char *s ) { - int newnp; - /* change in nproc? */ - if (s != NULL && sscanf(s, "%d", &newnp) == 1 && newnp > 0) { - if (!newparam) { - if (newnp < nproc) - ray_pclose(nproc - newnp); - else - ray_popen(newnp - nproc); - } - nproc = newnp; - } + int newnp = 0; + /* # rendering procs arg? */ + if (s != NULL) + sscanf(s, "%d", &newnp); /* free old image */ freepkids(&ptrunk); /* compute resolution */ @@ -274,9 +285,21 @@ newimage( /* start a new image */ (*dev->clear)(hresolu, vresolu); if (newparam) { /* (re)start rendering procs */ - ray_pclose(0); - ray_popen(nproc); + if (ray_pnprocs) + ray_pclose(0); /* should already be closed */ + if (newnp > 0) + nproc = newnp; + if (nproc > 1) + ray_popen(nproc); newparam = 0; + } else if ((newnp > 0) & (newnp != nproc)) { + if (newnp == 1) /* change # rendering procs */ + ray_pclose(0); + else if (newnp < ray_pnprocs) + ray_pclose(ray_pnprocs - newnp); + else + ray_popen(newnp - ray_pnprocs); + nproc = newnp; } niflush = 0; /* get first value */ paint(&ptrunk); @@ -381,18 +404,22 @@ compavg( /* recompute averages */ PNODE *p ) { + int i, navg; + if (p->kid == NULL) return; - compavg(p->kid+DL); - compavg(p->kid+DR); - compavg(p->kid+UL); - compavg(p->kid+UR); + setcolor(p->v, .0, .0, .0); - addcolor(p->v, p->kid[DL].v); - addcolor(p->v, p->kid[DR].v); - addcolor(p->v, p->kid[UL].v); - addcolor(p->v, p->kid[UR].v); - scalecolor(p->v, 0.25); + navg = 0; + for (i = 0; i < 4; i++) { + if (p->kid[i].xmin >= p->kid[i].xmax) continue; + if (p->kid[i].ymin >= p->kid[i].ymax) continue; + compavg(p->kid+i); + addcolor(p->v, p->kid[i].v); + navg++; + } + if (navg > 1) + scalecolor(p->v, 1./navg); }