--- ray/src/rt/rpict.c 1990/01/08 13:38:01 1.12 +++ ray/src/rt/rpict.c 1991/08/13 12:16:34 1.31 @@ -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,23 @@ static char SCCSid[] = "$SunId$ LBL"; #ifdef BSD #include #include +#else +#include #endif +#include #include "view.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 */ +double pixaspect = 1.0; /* pixel aspect ratio */ int psample = 4; /* pixel sample size */ double maxdiff = .05; /* max. difference for interpolation */ @@ -32,6 +40,9 @@ 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 = 0; /* number of source relays */ +int vspretest = 512; /* virtual source pretest density */ +int directinvis = 0; /* sources invisible? */ int maxdepth = 6; /* maximum recursion depth */ double minweight = 5e-3; /* minimum ray weight */ @@ -49,9 +60,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 15 /* maximum sample size */ #define pixjitter() (.5+dstrpix*(.5-frandom())) @@ -68,9 +84,9 @@ int code; } +#ifdef BSD report() /* report progress */ { -#ifdef BSD struct rusage rubuf; double t; @@ -83,31 +99,42 @@ 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 - sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone); -#endif +report() /* report progress */ +{ + signal(SIGALRM, report); + tlastrept = time((long *)0); + sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n", + nrays, pctdone, (tlastrept-tstart)/3600.0); eputs(errmsg); - - if (ralrm > 0) - alarm(ralrm); } +#endif 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 */ int ypos; /* current scanline */ - FILE *zfp; + int ystep; /* current y step size */ + int zfd; COLOR *colptr; float *zptr; register int i; /* 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)); @@ -116,7 +143,7 @@ char *zfile, *oldfile; } /* open z file */ if (zfile != NULL) { - if ((zfp = fopen(zfile, "a+")) == NULL) { + if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { sprintf(errmsg, "cannot open z file \"%s\"", zfile); error(SYSTEM, errmsg); } @@ -126,7 +153,7 @@ char *zfile, *oldfile; goto memerr; } } else { - zfp = NULL; + zfd = -1; for (i = 0; i <= psample; i++) zbar[i] = NULL; } @@ -134,61 +161,59 @@ char *zfile, *oldfile; 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) + 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(); ypos = vresolu-1 - i; fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); + ystep = psample; /* compute scanlines */ - for (ypos -= psample; ypos >= 0; ypos -= psample) { - - pctdone = 100.0*(vresolu-ypos-psample)/vresolu; - - colptr = scanbar[psample]; /* move base to top */ - scanbar[psample] = scanbar[0]; + for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { + /* bottom adjust? */ + if (ypos < 0) { + ystep += ypos; + ypos = 0; + } + colptr = scanbar[ystep]; /* move base to top */ + scanbar[ystep] = scanbar[0]; scanbar[0] = colptr; - zptr = zbar[psample]; - zbar[psample] = zbar[0]; + zptr = zbar[ystep]; + zbar[ystep] = zbar[0]; zbar[0] = zptr; /* fill base line */ fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); /* fill bar */ - fillscanbar(scanbar, zbar, hresolu, ypos, psample); + fillscanbar(scanbar, zbar, hresolu, ypos, ystep); /* write it out */ - for (i = psample; i > 0; i--) { - if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu) + for (i = ystep; i > 0; i--) { + if (zfd != -1 && write(zfd, (char *)zbar[i], + hresolu*sizeof(float)) + < hresolu*sizeof(float)) goto writerr; - if (fwritescan(scanbar[i],hresolu,stdout) < 0) + if (fwritescan(scanbar[i], hresolu, stdout) < 0) goto writerr; } - if (zfp != NULL && fflush(zfp) == EOF) - goto writerr; if (fflush(stdout) == EOF) goto writerr; + /* record progress */ + pctdone = 100.0*(vresolu-1-ypos)/vresolu; + if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) + report(); } - /* 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) + if (zfd != -1) { + if (write(zfd, (char *)zbar[0], hresolu*sizeof(float)) + < hresolu*sizeof(float)) goto writerr; + if (close(zfd) == -1) + goto writerr; for (i = 0; i <= psample; i++) free((char *)zbar[i]); } + fwritescan(scanbar[0], hresolu, stdout); if (fflush(stdout) == EOF) goto writerr; for (i = 0; i <= psample; i++) @@ -214,20 +239,17 @@ int xres, y, xstep; z = pixvalue(scanline[0], 0, y); if (zline) zline[0] = z; - for (i = xstep; i < xres; i += xstep) { - + for (i = xstep; 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 (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); - } } @@ -239,7 +261,6 @@ 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++) { @@ -323,18 +344,23 @@ 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; - viewray(thisray.rorg, thisray.rdir, &ourview, - (x+pixjitter())/hresolu, (y+pixjitter())/vresolu); + if (viewray(thisray.rorg, thisray.rdir, &ourview, + (x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) { + setcolor(col, 0.0, 0.0, 0.0); + return(0.0); + } 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 */ - return(thisray.rot); /* return distance */ + return(thisray.rt); /* return distance */ } @@ -387,4 +413,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)); }