--- ray/src/rt/rpict.c 1989/09/13 15:21:41 1.8 +++ ray/src/rt/rpict.c 1990/01/11 08:30:42 1.14 @@ -15,13 +15,18 @@ static char SCCSid[] = "$SunId$ LBL"; #ifdef BSD #include #include +#else +#include #endif #include "view.h" #include "random.h" -VIEW ourview = STDVIEW(512); /* view parameters */ +VIEW ourview = STDVIEW; /* view parameters */ +int hresolu = 512; /* horizontal resolution */ +int vresolu = 512; /* vertical resolution */ +double pixaspect = 1.0; /* pixel aspect ratio */ int psample = 4; /* pixel sample size */ double maxdiff = .05; /* max. difference for interpolation */ @@ -49,13 +54,13 @@ double pctdone = 0.0; /* percentage done */ extern long nrays; /* number of rays traced */ -static int xres, yres; /* output x and y resolution */ - #define MAXDIV 32 /* maximum sample size */ #define pixjitter() (.5+dstrpix*(.5-frandom())) +double pixvalue(); + quit(code) /* quit program */ int code; { @@ -82,6 +87,7 @@ report() /* report progress */ sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", nrays, pctdone, t/3600.0); #else + signal(SIGALRM, report); sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone); #endif eputs(errmsg); @@ -91,106 +97,187 @@ report() /* report progress */ } -render(oldfile) /* render the scene */ -char *oldfile; +render(zfile, oldfile) /* render the scene */ +char *zfile, *oldfile; { COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ + float *zbar[MAXDIV+1]; /* z values */ int ypos; /* current scanline */ + FILE *zfp; COLOR *colptr; + float *zptr; register int i; - + /* check sampling */ if (psample < 1) psample = 1; else if (psample > MAXDIV) psample = MAXDIV; - /* output resolution may be smaller */ - xres = ourview.hresolu - (ourview.hresolu % psample); - yres = ourview.vresolu - (ourview.vresolu % psample); - + /* allocate scanlines */ for (i = 0; i <= psample; i++) { - scanbar[i] = (COLOR *)malloc((xres+1)*sizeof(COLOR)); + scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); if (scanbar[i] == NULL) - error(SYSTEM, "out of memory in render"); + goto memerr; } + /* open z file */ + if (zfile != NULL) { + if ((zfp = fopen(zfile, "a+")) == NULL) { + sprintf(errmsg, "cannot open z file \"%s\"", zfile); + error(SYSTEM, errmsg); + } + for (i = 0; i <= psample; i++) { + zbar[i] = (float *)malloc(hresolu*sizeof(float)); + if (zbar[i] == NULL) + goto memerr; + } + } else { + zfp = NULL; + for (i = 0; i <= psample; i++) + zbar[i] = NULL; + } /* write out boundaries */ - fputresolu(YMAJOR|YDECR, xres, yres, stdout); - - ypos = yres - salvage(oldfile); /* find top line */ - fillscanline(scanbar[0], ypos, psample); /* top scan */ - - for (ypos -= psample; ypos > -psample; ypos -= psample) { + fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); + /* recover file and compute first */ + i = salvage(oldfile); + if (zfp != NULL && fseek(zfp, (long)i*hresolu*sizeof(float), 0) == EOF) + error(SYSTEM, "z file seek error in render"); + ypos = vresolu-1 - i; + fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); + /* compute scanlines */ + for (ypos -= psample; ypos >= 0; ypos -= psample) { - colptr = scanbar[psample]; /* get last scanline */ + pctdone = 100.0*(vresolu-ypos-psample)/vresolu; + + colptr = scanbar[psample]; /* move base to top */ scanbar[psample] = scanbar[0]; scanbar[0] = colptr; - - fillscanline(scanbar[0], ypos, psample); /* base scan */ - - fillscanbar(scanbar, ypos, psample); - - for (i = psample-1; i >= 0; i--) - if (fwritescan(scanbar[i], xres, stdout) < 0) + zptr = zbar[psample]; + zbar[psample] = zbar[0]; + zbar[0] = zptr; + /* fill base line */ + fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); + /* fill bar */ + fillscanbar(scanbar, zbar, hresolu, ypos, psample); + /* write it out */ + for (i = psample; i > 0; i--) { + if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu) goto writerr; + if (fwritescan(scanbar[i],hresolu,stdout) < 0) + goto writerr; + } + if (zfp != NULL && fflush(zfp) == EOF) + goto writerr; if (fflush(stdout) == EOF) goto writerr; - pctdone = 100.0*(yres-ypos)/yres; } - + /* compute residual */ + colptr = scanbar[psample]; + scanbar[psample] = scanbar[0]; + scanbar[0] = colptr; + zptr = zbar[psample]; + zbar[psample] = zbar[0]; + zbar[0] = zptr; + if (ypos > -psample) { + fillscanline(scanbar[-ypos], zbar[-ypos], hresolu, 0, psample); + fillscanbar(scanbar-ypos, zbar-ypos, hresolu, 0, psample+ypos); + } + for (i = psample; i+ypos >= 0; i--) { + if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu) + goto writerr; + if (fwritescan(scanbar[i], hresolu, stdout) < 0) + goto writerr; + } + /* clean up */ + if (zfp != NULL) { + if (fclose(zfp) == EOF) + goto writerr; + for (i = 0; i <= psample; i++) + free((char *)zbar[i]); + } + if (fflush(stdout) == EOF) + goto writerr; for (i = 0; i <= psample; i++) free((char *)scanbar[i]); + pctdone = 100.0; return; writerr: error(SYSTEM, "write error in render"); +memerr: + error(SYSTEM, "out of memory in render"); } -fillscanline(scanline, y, xstep) /* fill scan line at y */ +fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */ register COLOR *scanline; -int y, xstep; +register float *zline; +int xres, y, xstep; { int b = xstep; + double z; register int i; - pixvalue(scanline[0], 0, y); + z = pixvalue(scanline[0], 0, y); + if (zline) zline[0] = z; - for (i = xstep; i <= xres; i += xstep) { + for (i = xstep; i < xres; i += xstep) { - pixvalue(scanline[i], i, y); + z = pixvalue(scanline[i], i, y); + if (zline) zline[i] = z; - b = fillsample(scanline+i-xstep, i-xstep, y, xstep, 0, b/2); + b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, + i-xstep, y, xstep, 0, b/2); } + if (i-xstep < xres-1) { + z = pixvalue(scanline[xres-1], xres-1, y); + if (zline) zline[xres-1] = z; + fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, + i-xstep, y, xres-1-(i-xstep), 0, b/2); + } } -fillscanbar(scanbar, y, ysize) /* fill interior */ +fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ register COLOR *scanbar[]; -int y, ysize; +register float *zbar[]; +int xres, y, ysize; { COLOR vline[MAXDIV+1]; + float zline[MAXDIV+1]; int b = ysize; + double z; register int i, j; for (i = 0; i < xres; i++) { copycolor(vline[0], scanbar[0][i]); copycolor(vline[ysize], scanbar[ysize][i]); + if (zbar[0]) { + zline[0] = zbar[0][i]; + zline[ysize] = zbar[ysize][i]; + } - b = fillsample(vline, i, y, 0, ysize, b/2); + b = fillsample(vline, zbar[0] ? zline : NULL, + i, y, 0, ysize, b/2); for (j = 1; j < ysize; j++) copycolor(scanbar[j][i], vline[j]); + if (zbar[0]) + for (j = 1; j < ysize; j++) + zbar[j][i] = zline[j]; } } int -fillsample(colline, x, y, xlen, ylen, b) /* fill interior points */ +fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ register COLOR *colline; +register float *zline; int x, y; int xlen, ylen; int b; { + extern double fabs(); double ratio; + double z; COLOR ctmp; int ncut; register int len; @@ -203,9 +290,12 @@ int b; if (len <= 1) /* limit recursion */ return(0); - if (b > 0 || bigdiff(colline[0], colline[len], maxdiff)) { + if (b > 0 + || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) + || bigdiff(colline[0], colline[len], maxdiff)) { - pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); + z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); + if (zline) zline[len>>1] = z; ncut = 1; } else { /* interpolate */ @@ -213,36 +303,42 @@ int b; copycolor(colline[len>>1], colline[len]); ratio = (double)(len>>1) / len; scalecolor(colline[len>>1], ratio); - copycolor(ctmp, colline[0]); + if (zline) zline[len>>1] = zline[len] * ratio; ratio = 1.0 - ratio; + copycolor(ctmp, colline[0]); scalecolor(ctmp, ratio); addcolor(colline[len>>1], ctmp); + if (zline) zline[len>>1] += zline[0] * ratio; ncut = 0; } /* recurse */ - ncut += fillsample(colline, x, y, xlen>>1, ylen>>1, (b-1)/2); + ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); - ncut += fillsample(colline + (len>>1), x + (xlen>>1), y + (ylen>>1), - xlen - (xlen>>1), ylen - (ylen>>1), b/2); + ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, + x+(xlen>>1), y+(ylen>>1), + xlen-(xlen>>1), ylen-(ylen>>1), b/2); return(ncut); } +double pixvalue(col, x, y) /* compute pixel value */ COLOR col; /* returned color */ int x, y; /* pixel position */ { static RAY thisray; /* our ray for this pixel */ - rayview(thisray.rorg, thisray.rdir, &ourview, - x + pixjitter(), y + pixjitter()); + viewray(thisray.rorg, thisray.rdir, &ourview, + (x+pixjitter())/hresolu, (y+pixjitter())/vresolu); rayorigin(&thisray, NULL, PRIMARY, 1.0); rayvalue(&thisray); /* trace ray */ copycolor(col, thisray.rcol); /* return color */ + + return(thisray.rot); /* return distance */ } @@ -256,7 +352,8 @@ char *oldfile; if (oldfile == NULL) return(0); - else if ((fp = fopen(oldfile, "r")) == NULL) { + + if ((fp = fopen(oldfile, "r")) == NULL) { sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); error(WARNING, errmsg); return(0); @@ -271,20 +368,19 @@ char *oldfile; return(0); } - if (x != xres || y != yres) { + if (x != hresolu || y != vresolu) { sprintf(errmsg, "resolution mismatch in recover file \"%s\"", oldfile); error(USER, errmsg); - return(0); } - scanline = (COLR *)malloc(xres*sizeof(COLR)); + scanline = (COLR *)malloc(hresolu*sizeof(COLR)); if (scanline == NULL) error(SYSTEM, "out of memory in salvage"); - for (y = 0; y < yres; y++) { - if (freadcolrs(scanline, xres, fp) < 0) + for (y = 0; y < vresolu; y++) { + if (freadcolrs(scanline, hresolu, fp) < 0) break; - if (fwritecolrs(scanline, xres, stdout) < 0) + if (fwritecolrs(scanline, hresolu, stdout) < 0) goto writerr; } if (fflush(stdout) == EOF)