--- ray/src/rt/rview.c 1989/02/02 10:41:39 1.1 +++ ray/src/rt/rview.c 1996/04/17 14:02:16 2.17 @@ -1,4 +1,4 @@ -/* Copyright (c) 1987 Regents of the University of California */ +/* Copyright (c) 1995 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -10,31 +10,51 @@ static char SCCSid[] = "$SunId$ LBL"; * 3/24/87 */ -#include "standard.h" +#include "ray.h" -#include "color.h" - #include "rpaint.h" #include #include -VIEW ourview = STDVIEW(470); /* viewing parameters */ +VIEW ourview = STDVIEW; /* viewing parameters */ +int hresolu, vresolu; /* image resolution */ +int dimlist[MAXDIM]; /* sampling dimensions */ +int ndims = 0; /* number of sampling dimensions */ +int samplendx = 0; /* index for this sample */ + int psample = 8; /* pixel sample size */ -double maxdiff = .15; /* max. sample difference */ +double maxdiff = .15; /* max. sample difference */ -double exposure = 1.0; /* exposure for scene */ +double exposure = 1.0; /* exposure for scene */ -double dstrsrc = 0.0; /* square source distribution */ +double dstrsrc = 0.0; /* square source distribution */ +double shadthresh = .1; /* shadow threshold */ +double shadcert = .25; /* shadow certainty */ +int directrelay = 0; /* number of source relays */ +int vspretest = 128; /* virtual source pretest density */ +int directvis = 1; /* sources visible? */ +double srcsizerat = 0.; /* 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 = .3; /* specular sampling threshold */ +double specjitter = 1.; /* specular sampling jitter */ + +int backvis = 1; /* back face visibility */ + int maxdepth = 4; /* maximum recursion depth */ -double minweight = 1e-2; /* minimum ray weight */ +double minweight = 1e-2; /* minimum ray weight */ COLOR ambval = BLKCOLOR; /* ambient value */ -double ambacc = 0.2; /* ambient accuracy */ -int ambres = 64; /* ambient resolution */ +int ambvwt = 0; /* initial weight for ambient value */ +double ambacc = 0.2; /* ambient accuracy */ +int ambres = 8; /* ambient resolution */ int ambdiv = 32; /* ambient divisions */ int ambssamp = 0; /* ambient super-samples */ int ambounce = 0; /* ambient bounces */ @@ -42,22 +62,32 @@ char *amblist[128]; /* ambient include/exclude list int ambincl = -1; /* include == 1, exclude == 0 */ int greyscale = 0; /* map colors to brightness? */ -char *devname = "X"; /* output device name */ +char *devname = dev_default; /* output device name */ struct driver *dev = NULL; /* driver functions */ +char rifname[128]; /* rad input file name */ + VIEW oldview; /* previous view parameters */ PNODE ptrunk; /* the base of our image */ RECT pframe; /* current frame boundaries */ int pdepth; /* image depth in current frame */ -#define CTRL(c) ('c'-'@') +static char *reserve_mem = NULL; /* pre-allocated reserve memory */ +#define RESERVE_AMT 32768 /* amount of memory to reserve */ +#define CTRL(c) ((c)-'@') + + quit(code) /* quit program */ int code; { +#ifdef MSTATS + if (code == 2 && errno == ENOMEM) + printmemstats(stderr); +#endif devclose(); exit(code); } @@ -66,25 +96,26 @@ int code; devopen(dname) /* open device driver */ char *dname; { - extern char *progname; - char *devargv[3]; + extern char *progname, *octname; + char *id; register int i; + + id = octname!=NULL ? octname : progname; /* check device table */ for (i = 0; devtable[i].name; i++) if (!strcmp(dname, devtable[i].name)) - if ((dev = (*devtable[i].init)(progname)) == NULL) { + if ((dev = (*devtable[i].init)(dname, id)) == NULL) { sprintf(errmsg, "cannot initialize %s", dname); error(USER, errmsg); } else return; +#ifndef NIX /* not there, try exec */ - devargv[0] = dname; - devargv[1] = progname; - devargv[2] = NULL; - if ((dev = comm_init(devargv)) == NULL) { + if ((dev = comm_init(dname, id)) == NULL) { sprintf(errmsg, "cannot start device \"%s\"", dname); error(USER, errmsg); } +#endif } @@ -110,51 +141,71 @@ rview() /* do a view */ char buf[32]; devopen(devname); /* open device */ - newimage(); /* set up image */ + newimage(); /* start image (calls fillreserves) */ for ( ; ; ) { /* quit in command() */ - while (ourview.hresolu <= 1<comout)(buf); rsample(); } else { sprintf(buf, "%d refining...\n", 1<comout)(buf); - refine(&ptrunk, 0, 0, ourview.hresolu, - ourview.vresolu, pdepth+1); + refine(&ptrunk, 0, 0, hresolu, vresolu, pdepth+1); } - if (dev->inpready) + if (errno == ENOMEM) /* ran out of memory */ + freereserves(); + else if (dev->inpready) /* noticed some input */ command(": "); - else + else /* finished this depth */ pdepth++; } } +fillreserves() /* fill memory reserves */ +{ + if (reserve_mem != NULL) + return; + reserve_mem = malloc(RESERVE_AMT); +} + + +freereserves() /* free memory reserves */ +{ + if (reserve_mem == NULL) + return; + free(reserve_mem); + reserve_mem = NULL; +} + + command(prompt) /* get/execute command */ char *prompt; { -#define badcom(s) strncmp(s, inpbuf, args-inpbuf-1) - double atof(); +#define badcom(s) strncmp(s, inpbuf, args-inpbuf-1) char inpbuf[256]; char *args; again: - (*dev->comout)(prompt); /* get command + arguments */ - (*dev->comin)(inpbuf); + (*dev->comin)(inpbuf, prompt); /* get command + arguments */ for (args = inpbuf; *args && *args != ' '; args++) ; if (*args) *args++ = '\0'; else *++args = '\0'; switch (inpbuf[0]) { - case 'f': /* new frame */ - if (badcom("frame")) - goto commerr; + case 'f': /* new frame (or free mem.) */ + if (badcom("frame")) { + if (badcom("free")) + goto commerr; + free_objmem(); + break; + } getframe(args); break; case 'v': /* view */ @@ -167,14 +218,29 @@ again: goto commerr; lastview(args); break; + case 'V': /* save view */ + if (badcom("V")) + goto commerr; + saveview(args); + break; + case 'L': /* load view */ + if (badcom("L")) + goto commerr; + loadview(args); + break; case 'e': /* exposure */ if (badcom("exposure")) goto commerr; getexposure(args); break; case 's': /* set a parameter */ - if (badcom("set")) + if (badcom("set")) { +#ifdef SIGTSTP + if (!badcom("stop")) + goto dostop; +#endif goto commerr; + } setparam(args); break; case 'n': /* new picture */ @@ -192,22 +258,42 @@ again: goto commerr; getaim(args); break; - case 'm': /* move camera */ + case 'm': /* move camera (or memstats) */ if (badcom("move")) +#ifdef MSTATS + { + if (badcom("memory")) + goto commerr; + printmemstats(stderr); + break; + } +#else goto commerr; +#endif getmove(args); break; - case 'r': /* rotate camera */ - if (badcom("rotate")) - goto commerr; + case 'r': /* rotate/repaint */ + if (badcom("rotate")) { + if (badcom("repaint")) { + if (badcom("redraw")) + goto commerr; + redraw(); + break; + } + getrepaint(args); + break; + } getrotate(args); break; case 'p': /* pivot view */ - if (badcom("pivot")) - goto commerr; + if (badcom("pivot")) { + if (badcom("pause")) + goto commerr; + goto again; + } getpivot(args); break; - case CTRL(R): /* redraw */ + case CTRL('R'): /* redraw */ redraw(); break; case 'w': /* write */ @@ -219,10 +305,11 @@ again: if (badcom("quit")) goto commerr; quit(0); - case CTRL(C): /* interrupt */ + case CTRL('C'): /* interrupt */ goto again; -#ifdef SIGTSTP - case CTRL(Z): /* stop */ +#ifdef SIGTSTP + case CTRL('Z'):; /* stop */ +dostop: devclose(); kill(0, SIGTSTP); /* pc stops here */ @@ -242,7 +329,7 @@ commerr: error(COMMAND, errmsg); break; } -#undef badcom +#undef badcom } @@ -252,39 +339,41 @@ rsample() /* sample the image */ RECT r; PNODE *p; register RECT *rl; - register PNODE **pl; + register PNODE **pl; register int x; /* * We initialize the bottom row in the image at our current - * resolution. During sampling, we check super-pixels to the + * resolution. During sampling, we check super-pixels to the * right and above by calling bigdiff(). If there is a significant * difference, we subsample the super-pixels. The testing process * includes initialization of the next row. */ - xsiz = (((pframe.r-pframe.l)<>pdepth), + rl[x].r = hresolu; rl[x].u = vresolu; + pl[x] = findrect(pframe.l+((x*hresolu)>>pdepth), pframe.d, &ptrunk, rl+x, pdepth); } /* sample the image */ for (y = 0; /* y < ysiz */ ; y++) { for (x = 0; x < xsiz-1; x++) { - if (dev->inpready) + if (dev->inpready || errno == ENOMEM) goto escape; /* * Test super-pixel to the right. @@ -300,15 +389,15 @@ rsample() /* sample the image */ if (y >= ysiz-1) break; for (x = 0; x < xsiz; x++) { - if (dev->inpready) + if (dev->inpready || errno == ENOMEM) goto escape; /* * Find super-pixel at this position in next row. */ r.l = r.d = 0; - r.r = ourview.hresolu; r.u = ourview.vresolu; - p = findrect(pframe.l+((x*ourview.hresolu)>>pdepth), - pframe.d+(((y+1)*ourview.vresolu)>>pdepth), + r.r = hresolu; r.u = vresolu; + p = findrect(pframe.l+((x*hresolu)>>pdepth), + pframe.d+(((y+1)*vresolu)>>pdepth), &ptrunk, &r, pdepth); /* * Test super-pixel in next row. @@ -334,7 +423,7 @@ escape: int refine(p, xmin, ymin, xmax, ymax, pd) /* refine a node */ -register PNODE *p; +register PNODE *p; int xmin, ymin, xmax, ymax; int pd; { @@ -355,10 +444,10 @@ int pd; if (p->kid == NULL) { /* subdivide */ if ((p->kid = newptree()) == NULL) - error(SYSTEM, "out of memory in refine"); + return(0); /* * The following paint order can leave a black pixel - * when redraw() is called in (*dev->paintr)(). + * if redraw() is called in (*dev->paintr)(). */ if (p->x >= mx && p->y >= my) pcopy(p, p->kid+UR);