--- ray/src/rt/rpict.c 1993/11/22 09:34:15 2.32 +++ ray/src/rt/rpict.c 1998/04/13 14:01:08 2.50 @@ -1,4 +1,4 @@ -/* Copyright (c) 1992 Regents of the University of California */ +/* Copyright (c) 1996 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -6,8 +6,6 @@ static char SCCSid[] = "$SunId$ LBL"; /* * rpict.c - routines and variables for picture generation. - * - * 8/14/85 */ #include "ray.h" @@ -20,8 +18,7 @@ static char SCCSid[] = "$SunId$ LBL"; #include #else #include -#include -#include +#include #endif #endif @@ -56,6 +53,8 @@ int psample = 4; /* pixel sample size */ double maxdiff = .05; /* max. difference for interpolation */ double dstrpix = 0.67; /* square pixel distribution */ +double mblur = 0.; /* motion blur parameter */ + double dstrsrc = 0.0; /* square source distribution */ double shadthresh = .05; /* shadow threshold */ double shadcert = .5; /* shadow certainty */ @@ -64,13 +63,21 @@ int vspretest = 512; /* virtual source pretest dens int directvis = 1; /* sources visible? */ double srcsizerat = .25; /* maximum ratio source size/dist. */ +COLOR cextinction = BLKCOLOR; /* global extinction coefficient */ +COLOR salbedo = BLKCOLOR; /* global scattering albedo */ +double seccg = 0.; /* global scattering eccentricity */ +double ssampdist = 0.; /* scatter sampling distance */ + double specthresh = .15; /* specular sampling threshold */ double specjitter = 1.; /* specular sampling jitter */ +int backvis = 1; /* back face visibility */ + int maxdepth = 6; /* maximum recursion depth */ double minweight = 5e-3; /* minimum ray weight */ COLOR ambval = BLKCOLOR; /* ambient value */ +int ambvwt = 0; /* initial weight for ambient value */ double ambacc = 0.2; /* ambient accuracy */ int ambres = 32; /* ambient resolution */ int ambdiv = 128; /* ambient divisions */ @@ -97,8 +104,10 @@ extern unsigned long nrays; /* number of rays traced #define pixjitter() (.5+dstrpix*(.5-frandom())) -static int hres, vres; /* resolution for this frame */ +int hres, vres; /* resolution for this frame */ +static VIEW lastview; /* the previous view input */ + extern char *mktemp(); double pixvalue(); @@ -133,14 +142,13 @@ int code; #ifndef NIX report() /* report progress */ { + extern char *myhostname(); double u, s; #ifdef BSD - char hostname[257]; struct rusage rubuf; #else struct tms tbuf; - struct utsname nambuf; -#define hostname nambuf.sysname + double period; #endif tlastrept = time((time_t *)NULL); @@ -151,20 +159,25 @@ report() /* report progress */ getrusage(RUSAGE_CHILDREN, &rubuf); u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6; s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6; - gethostname(hostname, sizeof(hostname)); #else times(&tbuf); - u = ( tbuf.tms_utime + tbuf.tms_cutime ) / CLK_TCK; - s = ( tbuf.tms_stime + tbuf.tms_cstime ) / CLK_TCK; - uname(&nambuf); +#ifdef _SC_CLK_TCK + period = 1.0 / sysconf(_SC_CLK_TCK); +#else + period = 1.0 / 60.0; #endif + u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period; + s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period; +#endif sprintf(errmsg, "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n", nrays, pctdone, u/3600., s/3600., - (tlastrept-tstart)/3600., hostname); + (tlastrept-tstart)/3600., myhostname()); eputs(errmsg); -#undef hostname +#ifndef BSD + signal(SIGCONT, report); +#endif } #else report() /* report progress */ @@ -173,7 +186,6 @@ report() /* report progress */ sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", nrays, pctdone, (tlastrept-tstart)/3600.0); eputs(errmsg); - signal(SIGCONT, report); } #endif @@ -220,6 +232,7 @@ char *pout, *zout, *prvr; for ( ; seq < rn; seq++) if (nextview(stdin) == EOF) error(USER, "unexpected EOF on view input"); + setview(&ourview); prvr = fbuf; /* mark for renaming */ } if (pout != NULL & prvr != NULL) { @@ -261,6 +274,7 @@ char *pout, *zout, *prvr; fbuf); error(USER, errmsg); } + setview(&ourview); continue; /* don't clobber */ } if (freopen(fbuf, "w", stdout) == NULL) { @@ -323,6 +337,7 @@ FILE *fp; { char linebuf[256]; + copystruct(&lastview, &ourview); while (fgets(linebuf, sizeof(linebuf), fp) != NULL) if (isview(linebuf) && sscanview(&ourview, linebuf) > 0) return(0); @@ -383,6 +398,8 @@ char *zfile, *oldfile; fprtresolu(hres, vres, stdout); /* recover file and compute first */ i = salvage(oldfile); + if (i >= vres) + goto alldone; if (zfd != -1 && i > 0 && lseek(zfd, (long)i*hres*sizeof(float), 0) == -1) error(SYSTEM, "z-file seek error in render"); @@ -393,7 +410,9 @@ char *zfile, *oldfile; else #endif signal(SIGCONT, report); - ypos = vres-1 - i; + ypos = vres-1 - i; /* initialize sampling */ + if (directvis) + init_drawsources(psample); fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep); /* compute scanlines */ for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { @@ -413,6 +432,8 @@ char *zfile, *oldfile; hres, ypos, hstep); /* fill bar */ fillscanbar(scanbar, zbar, hres, ypos, ystep); + if (directvis) /* add bitty sources */ + drawsources(scanbar, zbar, 0, hres, ypos, ystep); /* write it out */ #ifndef BSD signal(SIGCONT, SIG_IGN); /* don't interrupt writes */ @@ -438,18 +459,19 @@ char *zfile, *oldfile; } /* clean up */ signal(SIGCONT, SIG_IGN); - if (zfd != -1) { - if (write(zfd, (char *)zbar[0], hres*sizeof(float)) + if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float)) < hres*sizeof(float)) - goto writerr; + goto writerr; + fwritescan(scanbar[0], hres, stdout); + if (fflush(stdout) == EOF) + goto writerr; +alldone: + if (zfd != -1) { if (close(zfd) == -1) goto writerr; for (i = 0; i <= psample; i++) free((char *)zbar[i]); } - fwritescan(scanbar[0], hres, stdout); - if (fflush(stdout) == EOF) - goto writerr; for (i = 0; i <= psample; i++) free((char *)scanbar[i]); if (sampdens != NULL) @@ -476,7 +498,7 @@ int xres, y, xstep; int bl = xstep, b = xstep; double z; register int i; - + z = pixvalue(scanline[0], 0, y); if (zline) zline[0] = z; /* zig-zag start for quincunx pattern */ @@ -492,7 +514,7 @@ int xres, y, xstep; b = fillsample(scanline, zline, 0, y, i, 0, b/2); else b = fillsample(scanline+i-xstep, - zline ? zline+i-xstep : NULL, + zline ? zline+i-xstep : (float *)NULL, i-xstep, y, xstep, 0, b/2); if (sd) *sd++ = nc & 1 ? bl : b; bl = b; @@ -510,19 +532,17 @@ int xres, y, ysize; float zline[MAXDIV+1]; int b = ysize; 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, zbar[0] ? zline : NULL, + b = fillsample(vline, zbar[0] ? zline : (float *)NULL, i, y, 0, ysize, b/2); - + for (j = 1; j < ysize; j++) copycolor(scanbar[j][i], vline[j]); if (zbar[0]) @@ -545,25 +565,23 @@ int b; COLOR ctmp; int ncut; register int len; - + if (xlen > 0) /* x or y length is zero */ len = xlen; else len = ylen; - + if (len <= 1) /* limit recursion */ return(0); - - if (b > 0 - || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) + + if (b > 0 || + (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) || bigdiff(colline[0], colline[len], maxdiff)) { - + z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); if (zline) zline[len>>1] = z; ncut = 1; - } else { /* interpolate */ - copycolor(colline[len>>1], colline[len]); ratio = (double)(len>>1) / len; scalecolor(colline[len>>1], ratio); @@ -577,8 +595,9 @@ int b; } /* recurse */ ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); - - ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, + + ncut += fillsample(colline+(len>>1), + zline ? zline+(len>>1) : (float *)NULL, x+(xlen>>1), y+(ylen>>1), xlen-(xlen>>1), ylen-(ylen>>1), b/2); @@ -591,22 +610,41 @@ pixvalue(col, x, y) /* compute pixel value */ COLOR col; /* returned color */ int x, y; /* pixel position */ { - static RAY thisray; - - if (viewray(thisray.rorg, thisray.rdir, &ourview, - (x+pixjitter())/hres, (y+pixjitter())/vres) < 0) { + RAY thisray; + FVECT lorg, ldir; + double hpos, vpos, lmax, d; + /* compute view ray */ + hpos = (x+pixjitter())/hres; + vpos = (y+pixjitter())/vres; + if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, + &ourview, hpos, vpos)) < -FTINY) { setcolor(col, 0.0, 0.0, 0.0); return(0.0); } - rayorigin(&thisray, NULL, PRIMARY, 1.0); - samplendx = pixnumber(x,y,hres,vres); /* set pixel index */ + /* optional motion blur */ + if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir, + &lastview, hpos, vpos)) >= -FTINY) { + register int i; + register double d = mblur*(.5-urand(281+samplendx)); + + thisray.rmax = (1.-d)*thisray.rmax + d*lmax; + for (i = 3; i--; ) { + thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i]; + thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i]; + } + if (normalize(thisray.rdir) == 0.0) + return(0.0); + } + + rayorigin(&thisray, NULL, PRIMARY, 1.0); + rayvalue(&thisray); /* trace ray */ copycolor(col, thisray.rcol); /* return color */ - + return(thisray.rt); /* return distance */ } @@ -620,12 +658,12 @@ char *oldfile; int x, y; if (oldfile == NULL) - return(0); - + goto gotzip; + if ((fp = fopen(oldfile, "r")) == NULL) { sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); error(WARNING, errmsg); - return(0); + goto gotzip; } #ifdef MSDOS setmode(fileno(fp), O_BINARY); @@ -638,7 +676,7 @@ char *oldfile; oldfile); error(WARNING, errmsg); fclose(fp); - return(0); + goto gotzip; } if (x != hres || y != vres) { @@ -662,6 +700,10 @@ char *oldfile; fclose(fp); unlink(oldfile); return(y); +gotzip: + if (fflush(stdout) == EOF) + error(SYSTEM, "error writing picture header"); + return(0); writerr: sprintf(errmsg, "write error during recovery of \"%s\"", oldfile); error(SYSTEM, errmsg);