--- ray/src/rt/rpict.c 2004/03/28 16:31:14 2.68 +++ ray/src/rt/rpict.c 2014/05/09 23:28:57 2.88 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id"; +static const char RCSid[] = "$Id: rpict.c,v 2.88 2014/05/09 23:28:57 greg Exp $"; #endif /* * rpict.c - routines and variables for picture generation. @@ -7,29 +7,33 @@ static const char RCSid[] = "$Id"; #include "copyright.h" -#include "platform.h" -#include "ray.h" -#include "paths.h" - #include -#ifndef NON_POSIX -#ifdef BSD -#include -#include +#include "platform.h" +#ifdef NON_POSIX + #ifdef MINGW + #include + #endif #else -#include -#include + #ifdef BSD + #include + #include + #else + #include + #include + #endif #endif -#endif #include #include +#include "ray.h" +#include "paths.h" +#include "ambient.h" #include "view.h" #include "random.h" #include "paths.h" -#include "rtmisc.h" /* myhostname() */ +#include "hilbert.h" #define RFTEMPLATE "rfXXXXXX" @@ -47,7 +51,6 @@ int dimlist[MAXDIM]; /* sampling dimensions */ int ndims = 0; /* number of sampling dimensions */ int samplendx; /* sample index number */ -extern void ambnotify(); void (*addobjnotify[])() = {ambnotify, NULL}; VIEW ourview = STDVIEW; /* view parameters */ @@ -61,10 +64,14 @@ double dstrpix = 0.67; /* square pixel distribution double mblur = 0.; /* motion blur parameter */ +double dblur = 0.; /* depth-of-field blur parameter */ + void (*trace)() = NULL; /* trace call */ int do_irrad = 0; /* compute irradiance? */ +int rand_samp = 0; /* pure Monte Carlo sampling? */ + double dstrsrc = 0.0; /* square source distribution */ double shadthresh = .05; /* shadow threshold */ double shadcert = .5; /* shadow certainty */ @@ -84,7 +91,7 @@ double specjitter = 1.; /* specular sampling jitter * int backvis = 1; /* back face visibility */ int maxdepth = 7; /* maximum recursion depth */ -double minweight = 4e-3; /* minimum ray weight */ +double minweight = 1e-3; /* minimum ray weight */ char *ambfile = NULL; /* ambient file name */ COLOR ambval = BLKCOLOR; /* ambient value */ @@ -94,7 +101,7 @@ int ambres = 64; /* ambient resolution */ int ambdiv = 512; /* ambient divisions */ int ambssamp = 128; /* ambient super-samples */ int ambounce = 0; /* ambient bounces */ -char *amblist[128]; /* ambient include/exclude list */ +char *amblist[AMBLLEN]; /* ambient include/exclude list */ int ambincl = -1; /* include == 1, exclude == 0 */ int ralrm = 0; /* seconds between reports */ @@ -111,11 +118,20 @@ int hres, vres; /* resolution for this frame */ static VIEW lastview; /* the previous view input */ -extern char *mktemp(); /* XXX should be in stdlib.h or unistd.h */ +static void report(int); +static int nextview(FILE *fp); +static void render(char *zfile, char *oldfile); +static void fillscanline(COLOR *scanline, float *zline, char *sd, int xres, + int y, int xstep); +static void fillscanbar(COLOR *scanbar[], float *zbar[], int xres, + int y, int ysize); +static int fillsample(COLOR *colline, float *zline, int x, int y, + int xlen, int ylen, int b); +static double pixvalue(COLOR col, int x, int y); +static int salvage(char *oldfile); +static int pixnumber(int x, int y, int xres, int yres); -void report(); -double pixvalue(); #ifdef RHAS_STAT #include @@ -138,7 +154,7 @@ quit(code) /* quit program */ int code; { if (code) /* report status */ - report(); + report(0); #ifndef NON_POSIX headclean(); /* delete header file */ pfclean(); /* clean up persist files */ @@ -148,48 +164,46 @@ int code; #ifndef NON_POSIX -void -report() /* report progress */ +static void +report(int dummy) /* report progress */ { - double u, s; + double u, s; #ifdef BSD - struct rusage rubuf; + struct rusage rubuf; #else - struct tms tbuf; - double period; + double period = 1.0 / 60.0; + struct tms tbuf; #endif tlastrept = time((time_t *)NULL); #ifdef BSD getrusage(RUSAGE_SELF, &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; + u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6; + s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6; 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; + u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6; + s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6; #else times(&tbuf); #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., myhostname()); + "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s (PID %d)\n", + nrays, pctdone, u*(1./3600.), s*(1./3600.), + (tlastrept-tstart)*(1./3600.), myhostname(), getpid()); eputs(errmsg); #ifdef SIGCONT signal(SIGCONT, report); #endif } #else -void -report() /* report progress */ +static void +report(int dummy) /* report progress */ { tlastrept = time((time_t *)NULL); sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n", @@ -200,9 +214,12 @@ report() /* report progress */ void -rpict(seq, pout, zout, prvr) /* generate image(s) */ -int seq; -char *pout, *zout, *prvr; +rpict( /* generate image(s) */ + int seq, + char *pout, + char *zout, + char *prvr +) /* * If seq is greater than zero, then we will render a sequence of * images based on view parameter strings read from the standard input. @@ -217,7 +234,7 @@ char *pout, *zout, *prvr; { char fbuf[128], fbuf2[128]; int npicts; - register char *cp; + char *cp; RESOLU rs; double pa; /* check sampling */ @@ -233,7 +250,7 @@ char *pout, *zout, *prvr; if (seq <= 0) seq = 0; else if (prvr != NULL && isint(prvr)) { - register int rn; /* skip to specified view */ + int rn; /* skip to specified view */ if ((rn = atoi(prvr)) < seq) error(USER, "recover frame less than start frame"); if (pout == NULL) @@ -322,7 +339,7 @@ char *pout, *zout, *prvr; fputs(VIEWSTR, stdout); fprintview(&ourview, stdout); putchar('\n'); - if (pa < .99 || pa > 1.01) + if ((pa < .99) | (pa > 1.01)) fputaspect(pa, stdout); fputnow(stdout); fputformat(COLRFMT, stdout); @@ -341,8 +358,10 @@ char *pout, *zout, *prvr; } -nextview(fp) /* get next view from fp */ -FILE *fp; +static int +nextview( /* get next view from fp */ + FILE *fp +) { char linebuf[256]; @@ -354,8 +373,11 @@ FILE *fp; } -render(zfile, oldfile) /* render the scene */ -char *zfile, *oldfile; +static void +render( /* render the scene */ + char *zfile, + char *oldfile +) { COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ float *zbar[MAXDIV+1]; /* z values */ @@ -366,9 +388,9 @@ char *zfile, *oldfile; int zfd; COLOR *colptr; float *zptr; - register int i; + int i; /* check for empty image */ - if (hres <= 0 || vres <= 0) { + if ((hres <= 0) | (vres <= 0)) { error(WARNING, "empty output picture"); fprtresolu(0, 0, stdout); return; @@ -412,12 +434,12 @@ char *zfile, *oldfile; i = salvage(oldfile); if (i >= vres) goto alldone; - if (zfd != -1 && i > 0 && + if ((zfd != -1) & (i > 0) && lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0) error(SYSTEM, "z-file seek error in render"); pctdone = 100.0*i/vres; if (ralrm > 0) /* report init stats */ - report(); + report(0); #ifdef SIGCONT else signal(SIGCONT, report); @@ -463,7 +485,7 @@ char *zfile, *oldfile; /* record progress */ pctdone = 100.0*(vres-1-ypos)/vres; if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm) - report(); + report(0); #ifdef SIGCONT else signal(SIGCONT, report); @@ -492,7 +514,7 @@ alldone: free(sampdens); pctdone = 100.0; if (ralrm > 0) - report(); + report(0); #ifdef SIGCONT signal(SIGCONT, SIG_DFL); #endif @@ -504,16 +526,20 @@ memerr: } -fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */ -register COLOR *scanline; -register float *zline; -register char *sd; -int xres, y, xstep; +static void +fillscanline( /* fill scan at y */ + COLOR *scanline, + float *zline, + char *sd, + int xres, + int y, + int xstep +) { static int nc = 0; /* number of calls */ int bl = xstep, b = xstep; double z; - register int i; + int i; z = pixvalue(scanline[0], 0, y); if (zline) zline[0] = z; @@ -539,15 +565,19 @@ int xres, y, xstep; } -fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ -register COLOR *scanbar[]; -register float *zbar[]; -int xres, y, ysize; +static void +fillscanbar( /* fill interior */ + COLOR *scanbar[], + float *zbar[], + int xres, + int y, + int ysize +) { COLOR vline[MAXDIV+1]; float zline[MAXDIV+1]; int b = ysize; - register int i, j; + int i, j; for (i = 0; i < xres; i++) { copycolor(vline[0], scanbar[0][i]); @@ -568,19 +598,22 @@ int xres, y, ysize; } -int -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; +static int +fillsample( /* fill interior points */ + COLOR *colline, + float *zline, + int x, + int y, + int xlen, + int ylen, + int b +) { double ratio; double z; COLOR ctmp; int ncut; - register int len; + int len; if (xlen > 0) /* x or y length is zero */ len = xlen; @@ -621,30 +654,33 @@ int b; } -double -pixvalue(col, x, y) /* compute pixel value */ -COLOR col; /* returned color */ -int x, y; /* pixel position */ +static double +pixvalue( /* compute pixel value */ + COLOR col, /* returned color */ + int x, /* pixel position */ + int y +) { + extern void SDsquare2disk(double ds[2], double seedx, double seedy); RAY thisray; FVECT lorg, ldir; - double hpos, vpos, lmax, d; + double hpos, vpos, vdist, lmax; + int i; /* compute view ray */ + setcolor(col, 0.0, 0.0, 0.0); 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); + &ourview, hpos, vpos)) < -FTINY) return(0.0); - } - samplendx = pixnumber(x,y,hres,vres); /* set pixel index */ - + vdist = ourview.vdist; + /* set pixel index */ + samplendx = pixnumber(x,y,hres,vres); /* 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)); + double d = mblur*(.5-urand(281+samplendx)); thisray.rmax = (1.-d)*thisray.rmax + d*lmax; for (i = 3; i--; ) { @@ -653,9 +689,47 @@ int x, y; /* pixel position */ } if (normalize(thisray.rdir) == 0.0) return(0.0); + vdist = (1.-d)*vdist + d*lastview.vdist; } + /* optional depth-of-field */ + if (dblur > FTINY) { + double vc, df[2]; + /* random point on disk */ + SDsquare2disk(df, frandom(), frandom()); + df[0] *= .5*dblur; + df[1] *= .5*dblur; + if ((ourview.type == VT_PER) | (ourview.type == VT_PAR)) { + double adj = 1.0; + if (ourview.type == VT_PER) + adj /= DOT(thisray.rdir, ourview.vdir); + df[0] /= sqrt(ourview.hn2); + df[1] /= sqrt(ourview.vn2); + for (i = 3; i--; ) { + vc = ourview.vp[i] + adj*vdist*thisray.rdir[i]; + thisray.rorg[i] += df[0]*ourview.hvec[i] + + df[1]*ourview.vvec[i] ; + thisray.rdir[i] = vc - thisray.rorg[i]; + } + } else { /* non-standard view case */ + double dfd = PI/4.*dblur*(.5 - frandom()); + if ((ourview.type != VT_ANG) & (ourview.type != VT_PLS)) { + if (ourview.type != VT_CYL) + df[0] /= sqrt(ourview.hn2); + df[1] /= sqrt(ourview.vn2); + } + for (i = 3; i--; ) { + vc = ourview.vp[i] + vdist*thisray.rdir[i]; + thisray.rorg[i] += df[0]*ourview.hvec[i] + + df[1]*ourview.vvec[i] + + dfd*ourview.vdir[i] ; + thisray.rdir[i] = vc - thisray.rorg[i]; + } + } + if (normalize(thisray.rdir) == 0.0) + return(0.0); + } - rayorigin(&thisray, NULL, PRIMARY, 1.0); + rayorigin(&thisray, PRIMARY, NULL, NULL); rayvalue(&thisray); /* trace ray */ @@ -665,9 +739,10 @@ int x, y; /* pixel position */ } -int -salvage(oldfile) /* salvage scanlines from killed program */ -char *oldfile; +static int +salvage( /* salvage scanlines from killed program */ + char *oldfile +) { COLR *scanline; FILE *fp; @@ -693,7 +768,7 @@ char *oldfile; goto gotzip; } - if (x != hres || y != vres) { + if ((x != hres) | (y != vres)) { sprintf(errmsg, "resolution mismatch in recover file \"%s\"", oldfile); error(USER, errmsg); @@ -721,16 +796,25 @@ gotzip: writerr: sprintf(errmsg, "write error during recovery of \"%s\"", oldfile); error(SYSTEM, errmsg); + return -1; /* pro forma return */ } - -int -pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */ -register int x, y; -int xres, yres; +static int +pixnumber( /* compute pixel index (screen door) */ + int x, + int y, + int xres, + int yres +) { - x -= y; - while (x < 0) - x += xres; - return((((x>>2)*yres + y) << 2) + (x & 3)); + unsigned nbits = 0; + bitmask_t coord[2]; + + if (xres < yres) xres = yres; + while (xres > 0) { + xres >>= 1; + ++nbits; + } + coord[0] = x; coord[1] = y; + return ((int)hilbert_c2i(2, nbits, coord)); }