--- ray/src/rt/rpict.c 1990/11/01 12:06:01 1.23 +++ ray/src/rt/rpict.c 1992/01/14 16:15:57 2.5 @@ -1,4 +1,4 @@ -/* Copyright (c) 1986 Regents of the University of California */ +/* Copyright (c) 1991 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -15,15 +15,21 @@ static char SCCSid[] = "$SunId$ LBL"; #ifdef BSD #include #include -#else -#include #endif + +#include #include #include "view.h" +#include "resolu.h" + #include "random.h" +int dimlist[MAXDIM]; /* sampling dimensions */ +int ndims = 0; /* number of sampling dimensions */ +int samplendx; /* sample index number */ + VIEW ourview = STDVIEW; /* view parameters */ int hresolu = 512; /* horizontal resolution */ int vresolu = 512; /* vertical resolution */ @@ -36,7 +42,14 @@ double dstrpix = 0.67; /* square pixel distribution double dstrsrc = 0.0; /* square source distribution */ double shadthresh = .05; /* shadow threshold */ double shadcert = .5; /* shadow certainty */ +int directrelay = 1; /* number of source relays */ +int vspretest = 512; /* virtual source pretest density */ +int directinvis = 0; /* sources invisible? */ +double srcsizerat = .25; /* maximum ratio source size/dist. */ +double specthresh = .5; /* specular sampling threshold */ +double specjitter = 1.; /* specular sampling jitter */ + int maxdepth = 6; /* maximum recursion depth */ double minweight = 5e-3; /* minimum ray weight */ @@ -53,9 +66,14 @@ int ralrm = 0; /* seconds between reports */ double pctdone = 0.0; /* percentage done */ +long tlastrept = 0L; /* time at last report */ + +extern long time(); +extern long tstart; /* starting time */ + extern long nrays; /* number of rays traced */ -#define MAXDIV 32 /* maximum sample size */ +#define MAXDIV 16 /* maximum sample size */ #define pixjitter() (.5+dstrpix*(.5-frandom())) @@ -72,9 +90,9 @@ int code; } +#ifdef BSD report() /* report progress */ { -#ifdef BSD struct rusage rubuf; double t; @@ -87,25 +105,31 @@ report() /* report progress */ sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", nrays, pctdone, t/3600.0); + eputs(errmsg); + tlastrept = time((long *)0); +} #else +report() /* report progress */ +{ + tlastrept = time((long *)0); + sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n", + nrays, pctdone, (tlastrept-tstart)/3600.0); + eputs(errmsg); signal(SIGALRM, report); - sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone); +} #endif - eputs(errmsg); - if (ralrm > 0) - alarm(ralrm); -} - render(zfile, oldfile) /* render the scene */ char *zfile, *oldfile; { extern long lseek(); COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ float *zbar[MAXDIV+1]; /* z values */ + char *sampdens; /* previous sample density */ int ypos; /* current scanline */ int ystep; /* current y step size */ + int hstep; /* h step size */ int zfd; COLOR *colptr; float *zptr; @@ -113,14 +137,28 @@ char *zfile, *oldfile; /* check sampling */ if (psample < 1) psample = 1; - else if (psample > MAXDIV) + else if (psample > MAXDIV) { + sprintf(errmsg, "pixel sampling reduced from %d to %d", + psample, MAXDIV); + error(WARNING, errmsg); psample = MAXDIV; + } /* allocate scanlines */ for (i = 0; i <= psample; i++) { scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); if (scanbar[i] == NULL) goto memerr; } + hstep = (psample*140+49)/99; /* quincunx sampling */ + ystep = (psample*99+70)/140; + if (hstep > 2) { + i = hresolu/hstep + 2; + if ((sampdens = malloc(i)) == NULL) + goto memerr; + while (i--) + sampdens[i] = hstep; + } else + sampdens = NULL; /* open z file */ if (zfile != NULL) { if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { @@ -138,19 +176,23 @@ char *zfile, *oldfile; zbar[i] = NULL; } /* write out boundaries */ - fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); + fprtresolu(hresolu, vresolu, stdout); /* recover file and compute first */ i = salvage(oldfile); if (zfd != -1 && i > 0 && lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1) error(SYSTEM, "z file seek error in render"); + pctdone = 100.0*i/vresolu; + if (ralrm > 0) /* report init stats */ + report(); +#ifndef BSD + else +#endif + signal(SIGALRM, report); ypos = vresolu-1 - i; - fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); - ystep = psample; + fillscanline(scanbar[0], zbar[0], sampdens, hresolu, ypos, hstep); /* compute scanlines */ for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { - /* record progress */ - pctdone = 100.0*(vresolu-1-ypos-ystep)/vresolu; /* bottom adjust? */ if (ypos < 0) { ystep += ypos; @@ -163,10 +205,14 @@ char *zfile, *oldfile; zbar[ystep] = zbar[0]; zbar[0] = zptr; /* fill base line */ - fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); + fillscanline(scanbar[0], zbar[0], sampdens, + hresolu, ypos, hstep); /* fill bar */ fillscanbar(scanbar, zbar, hresolu, ypos, ystep); /* write it out */ +#ifndef BSD + signal(SIGALRM, SIG_IGN); /* don't interrupt writes */ +#endif for (i = ystep; i > 0; i--) { if (zfd != -1 && write(zfd, (char *)zbar[i], hresolu*sizeof(float)) @@ -177,6 +223,14 @@ char *zfile, *oldfile; } if (fflush(stdout) == EOF) goto writerr; + /* record progress */ + pctdone = 100.0*(vresolu-1-ypos)/vresolu; + if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) + report(); +#ifndef BSD + else + signal(SIGALRM, report); +#endif } /* clean up */ if (zfd != -1) { @@ -193,6 +247,8 @@ char *zfile, *oldfile; goto writerr; for (i = 0; i <= psample; i++) free((char *)scanbar[i]); + if (sampdens != NULL) + free(sampdens); pctdone = 100.0; return; writerr: @@ -202,29 +258,38 @@ memerr: } -fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */ +fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */ register COLOR *scanline; register float *zline; +register char *sd; int xres, y, xstep; { - int b = xstep; + static int nc = 0; /* number of calls */ + int bl = xstep, b = xstep; double z; register int i; z = pixvalue(scanline[0], 0, y); if (zline) zline[0] = z; - - for (i = xstep; i < xres-1+xstep; i += xstep) { + /* zig-zag start for quincunx pattern */ + for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) { if (i >= xres) { xstep += xres-1-i; i = xres-1; } z = pixvalue(scanline[i], i, y); if (zline) zline[i] = z; - - b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, - i-xstep, y, xstep, 0, b/2); + if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1]; + if (i <= xstep) + b = fillsample(scanline, zline, 0, y, i, 0, b/2); + else + b = fillsample(scanline+i-xstep, + zline ? zline+i-xstep : NULL, + i-xstep, y, xstep, 0, b/2); + if (sd) *sd++ = nc & 1 ? bl : b; + bl = b; } + if (sd && nc & 1) *sd = bl; } @@ -319,7 +384,7 @@ pixvalue(col, x, y) /* compute pixel value */ COLOR col; /* returned color */ int x, y; /* pixel position */ { - static RAY thisray; /* our ray for this pixel */ + static RAY thisray; if (viewray(thisray.rorg, thisray.rdir, &ourview, (x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) { @@ -328,7 +393,9 @@ int x, y; /* pixel position */ } rayorigin(&thisray, NULL, PRIMARY, 1.0); - + + samplendx = pixnumber(x,y,hresolu,vresolu); /* set pixel index */ + rayvalue(&thisray); /* trace ray */ copycolor(col, thisray.rcol); /* return color */ @@ -356,7 +423,7 @@ char *oldfile; /* discard header */ getheader(fp, NULL); /* get picture size */ - if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) { + if (!fscnresolu(&x, &y, fp)) { sprintf(errmsg, "bad recover file \"%s\"", oldfile); error(WARNING, errmsg); fclose(fp); @@ -386,4 +453,16 @@ char *oldfile; return(y); writerr: error(SYSTEM, "write error in salvage"); +} + + +int +pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */ +register int x, y; +int xres, yres; +{ + x -= y; + while (x < 0) + x += xres; + return((((x>>2)*yres + y) << 2) + (x & 3)); }