ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
Revision: 2.14
Committed: Wed Dec 21 09:52:07 1994 UTC (29 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.13: +2 -0 lines
Log Message:
added -bv option for back face visibility (normally on)

File Contents

# User Rev Content
1 greg 2.7 /* Copyright (c) 1992 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * rview.c - routines and variables for interactive view generation.
9     *
10     * 3/24/87
11     */
12    
13 greg 1.15 #include "ray.h"
14 greg 1.1
15     #include "rpaint.h"
16    
17     #include <signal.h>
18    
19     #include <ctype.h>
20    
21 greg 1.9 VIEW ourview = STDVIEW; /* viewing parameters */
22     int hresolu, vresolu; /* image resolution */
23 greg 1.15
24     int dimlist[MAXDIM]; /* sampling dimensions */
25     int ndims = 0; /* number of sampling dimensions */
26     int samplendx = 0; /* index for this sample */
27 greg 1.1
28     int psample = 8; /* pixel sample size */
29 greg 2.8 double maxdiff = .15; /* max. sample difference */
30 greg 1.1
31 greg 2.8 double exposure = 1.0; /* exposure for scene */
32 greg 1.1
33 greg 2.8 double dstrsrc = 0.0; /* square source distribution */
34     double shadthresh = .1; /* shadow threshold */
35     double shadcert = .25; /* shadow certainty */
36 greg 1.16 int directrelay = 0; /* number of source relays */
37 greg 1.18 int vspretest = 128; /* virtual source pretest density */
38 greg 2.9 int directvis = 1; /* sources visible? */
39 greg 2.8 double srcsizerat = 0.; /* maximum ratio source size/dist. */
40 greg 1.1
41 greg 2.8 double specthresh = .3; /* specular sampling threshold */
42     double specjitter = 1.; /* specular sampling jitter */
43 greg 2.5
44 greg 2.14 int backvis = 1; /* back face visibility */
45    
46 greg 1.1 int maxdepth = 4; /* maximum recursion depth */
47 greg 2.8 double minweight = 1e-2; /* minimum ray weight */
48 greg 1.1
49     COLOR ambval = BLKCOLOR; /* ambient value */
50 greg 2.8 double ambacc = 0.2; /* ambient accuracy */
51 greg 1.5 int ambres = 8; /* ambient resolution */
52 greg 1.1 int ambdiv = 32; /* ambient divisions */
53     int ambssamp = 0; /* ambient super-samples */
54     int ambounce = 0; /* ambient bounces */
55     char *amblist[128]; /* ambient include/exclude list */
56     int ambincl = -1; /* include == 1, exclude == 0 */
57    
58     int greyscale = 0; /* map colors to brightness? */
59 greg 1.11 char *devname = dev_default; /* output device name */
60 greg 1.1
61     struct driver *dev = NULL; /* driver functions */
62    
63 greg 2.11 char rifname[128]; /* rad input file name */
64    
65 greg 1.1 VIEW oldview; /* previous view parameters */
66    
67     PNODE ptrunk; /* the base of our image */
68     RECT pframe; /* current frame boundaries */
69     int pdepth; /* image depth in current frame */
70    
71 greg 1.12 static char *reserve_mem = NULL; /* pre-allocated reserve memory */
72    
73 greg 1.20 #define RESERVE_AMT 32768 /* amount of memory to reserve */
74 greg 1.12
75 greg 2.8 #define CTRL(c) ((c)-'@')
76 greg 1.1
77    
78     quit(code) /* quit program */
79     int code;
80     {
81 greg 2.10 #ifdef MSTATS
82     if (code == 2 && errno == ENOMEM)
83     printmemstats(stderr);
84     #endif
85 greg 1.1 devclose();
86     exit(code);
87     }
88    
89    
90     devopen(dname) /* open device driver */
91     char *dname;
92     {
93 greg 1.6 extern char *progname, *octname;
94 greg 1.7 char *id;
95 greg 1.1 register int i;
96 greg 1.6
97 greg 1.7 id = octname!=NULL ? octname : progname;
98 greg 1.1 /* check device table */
99     for (i = 0; devtable[i].name; i++)
100     if (!strcmp(dname, devtable[i].name))
101 greg 1.7 if ((dev = (*devtable[i].init)(dname, id)) == NULL) {
102 greg 1.1 sprintf(errmsg, "cannot initialize %s", dname);
103     error(USER, errmsg);
104     } else
105     return;
106 greg 2.8 #ifndef NIX
107 greg 1.1 /* not there, try exec */
108 greg 1.7 if ((dev = comm_init(dname, id)) == NULL) {
109 greg 1.1 sprintf(errmsg, "cannot start device \"%s\"", dname);
110     error(USER, errmsg);
111     }
112 greg 2.8 #endif
113 greg 1.1 }
114    
115    
116     devclose() /* close our device */
117     {
118     if (dev != NULL)
119     (*dev->close)();
120     dev = NULL;
121     }
122    
123    
124     printdevices() /* print list of output devices */
125     {
126     register int i;
127    
128     for (i = 0; devtable[i].name; i++)
129     printf("%-16s # %s\n", devtable[i].name, devtable[i].descrip);
130     }
131    
132    
133     rview() /* do a view */
134     {
135     char buf[32];
136    
137     devopen(devname); /* open device */
138 greg 1.14 newimage(); /* start image (calls fillreserves) */
139    
140 greg 1.1 for ( ; ; ) { /* quit in command() */
141 greg 1.14 while (hresolu <= 1<<pdepth && vresolu <= 1<<pdepth)
142 greg 1.1 command("done: ");
143 greg 1.14 while (reserve_mem == NULL)
144     command("out of memory: ");
145     errno = 0;
146     if (hresolu <= psample<<pdepth && vresolu <= psample<<pdepth) {
147 greg 1.1 sprintf(buf, "%d sampling...\n", 1<<pdepth);
148     (*dev->comout)(buf);
149     rsample();
150     } else {
151     sprintf(buf, "%d refining...\n", 1<<pdepth);
152     (*dev->comout)(buf);
153 greg 1.9 refine(&ptrunk, 0, 0, hresolu, vresolu, pdepth+1);
154 greg 1.1 }
155 greg 1.14 if (errno == ENOMEM) /* ran out of memory */
156     freereserves();
157     else if (dev->inpready) /* noticed some input */
158 greg 1.1 command(": ");
159 greg 1.14 else /* finished this depth */
160 greg 1.1 pdepth++;
161     }
162     }
163    
164    
165 greg 1.14 fillreserves() /* fill memory reserves */
166 greg 1.12 {
167     if (reserve_mem != NULL)
168 greg 1.14 return;
169 greg 1.12 reserve_mem = malloc(RESERVE_AMT);
170     }
171    
172    
173 greg 1.14 freereserves() /* free memory reserves */
174 greg 1.12 {
175 greg 1.14 if (reserve_mem == NULL)
176     return;
177 greg 1.12 free(reserve_mem);
178     reserve_mem = NULL;
179     }
180    
181    
182 greg 1.1 command(prompt) /* get/execute command */
183     char *prompt;
184     {
185 greg 2.8 #define badcom(s) strncmp(s, inpbuf, args-inpbuf-1)
186 greg 1.1 char inpbuf[256];
187     char *args;
188     again:
189 greg 1.10 (*dev->comin)(inpbuf, prompt); /* get command + arguments */
190 greg 1.1 for (args = inpbuf; *args && *args != ' '; args++)
191     ;
192     if (*args) *args++ = '\0';
193     else *++args = '\0';
194    
195     switch (inpbuf[0]) {
196 greg 2.10 case 'f': /* new frame (or free mem.) */
197     if (badcom("frame")) {
198     if (badcom("free"))
199     goto commerr;
200     free_objmem();
201     break;
202     }
203 greg 1.1 getframe(args);
204     break;
205     case 'v': /* view */
206     if (badcom("view"))
207     goto commerr;
208     getview(args);
209     break;
210     case 'l': /* last view */
211     if (badcom("last"))
212     goto commerr;
213     lastview(args);
214 greg 2.11 break;
215     case 'V': /* save view */
216     if (badcom("V"))
217     goto commerr;
218     saveview(args);
219     break;
220     case 'L': /* load view */
221     if (badcom("L"))
222     goto commerr;
223     loadview(args);
224 greg 1.1 break;
225     case 'e': /* exposure */
226     if (badcom("exposure"))
227     goto commerr;
228     getexposure(args);
229     break;
230     case 's': /* set a parameter */
231 greg 2.13 if (badcom("set")) {
232     #ifdef SIGTSTP
233     if (!badcom("stop"))
234     goto dostop;
235     #endif
236 greg 1.1 goto commerr;
237 greg 2.13 }
238 greg 1.1 setparam(args);
239     break;
240     case 'n': /* new picture */
241     if (badcom("new"))
242     goto commerr;
243     newimage();
244     break;
245     case 't': /* trace a ray */
246     if (badcom("trace"))
247     goto commerr;
248     traceray(args);
249     break;
250     case 'a': /* aim camera */
251     if (badcom("aim"))
252     goto commerr;
253     getaim(args);
254     break;
255 greg 2.10 case 'm': /* move camera (or memstats) */
256 greg 1.1 if (badcom("move"))
257 greg 2.8 #ifdef MSTATS
258 greg 2.2 {
259     if (badcom("memory"))
260     goto commerr;
261     printmemstats(stderr);
262     break;
263     }
264     #else
265 greg 1.1 goto commerr;
266 greg 2.2 #endif
267 greg 1.1 getmove(args);
268     break;
269 greg 1.8 case 'r': /* rotate/repaint */
270     if (badcom("rotate")) {
271 greg 2.13 if (badcom("repaint")) {
272     if (badcom("redraw"))
273     goto commerr;
274     redraw();
275     break;
276     }
277 greg 1.8 getrepaint(args);
278     break;
279     }
280 greg 1.1 getrotate(args);
281     break;
282     case 'p': /* pivot view */
283 greg 2.13 if (badcom("pivot")) {
284     if (badcom("pause"))
285     goto commerr;
286     goto again;
287     }
288 greg 1.1 getpivot(args);
289     break;
290 greg 2.4 case CTRL('R'): /* redraw */
291 greg 1.1 redraw();
292     break;
293     case 'w': /* write */
294     if (badcom("write"))
295     goto commerr;
296     writepict(args);
297     break;
298     case 'q': /* quit */
299     if (badcom("quit"))
300     goto commerr;
301     quit(0);
302 greg 2.4 case CTRL('C'): /* interrupt */
303 greg 1.1 goto again;
304 greg 2.8 #ifdef SIGTSTP
305 greg 2.13 case CTRL('Z'):; /* stop */
306     dostop:
307 greg 1.1 devclose();
308     kill(0, SIGTSTP);
309     /* pc stops here */
310     devopen(devname);
311     redraw();
312     break;
313     #endif
314     case '\0': /* continue */
315     break;
316     default:;
317     commerr:
318     if (iscntrl(inpbuf[0]))
319     sprintf(errmsg, "^%c: unknown control",
320     inpbuf[0]|0100);
321     else
322     sprintf(errmsg, "%s: unknown command", inpbuf);
323     error(COMMAND, errmsg);
324     break;
325     }
326 greg 2.8 #undef badcom
327 greg 1.1 }
328    
329    
330     rsample() /* sample the image */
331     {
332     int xsiz, ysiz, y;
333     RECT r;
334     PNODE *p;
335     register RECT *rl;
336 greg 2.8 register PNODE **pl;
337 greg 1.1 register int x;
338     /*
339     * We initialize the bottom row in the image at our current
340 greg 2.8 * resolution. During sampling, we check super-pixels to the
341 greg 1.1 * right and above by calling bigdiff(). If there is a significant
342     * difference, we subsample the super-pixels. The testing process
343     * includes initialization of the next row.
344     */
345 greg 2.12 xsiz = (((long)(pframe.r-pframe.l)<<pdepth)+hresolu-1) / hresolu;
346     ysiz = (((long)(pframe.u-pframe.d)<<pdepth)+vresolu-1) / vresolu;
347 greg 1.1 rl = (RECT *)malloc(xsiz*sizeof(RECT));
348 greg 1.12 if (rl == NULL)
349 greg 1.14 return;
350 greg 1.1 pl = (PNODE **)malloc(xsiz*sizeof(PNODE *));
351 greg 2.7 if (pl == NULL) {
352     free((char *)rl);
353 greg 1.14 return;
354 greg 2.7 }
355 greg 1.1 /*
356     * Initialize the bottom row.
357     */
358     rl[0].l = rl[0].d = 0;
359 greg 1.9 rl[0].r = hresolu; rl[0].u = vresolu;
360 greg 1.1 pl[0] = findrect(pframe.l, pframe.d, &ptrunk, rl, pdepth);
361     for (x = 1; x < xsiz; x++) {
362     rl[x].l = rl[x].d = 0;
363 greg 1.9 rl[x].r = hresolu; rl[x].u = vresolu;
364     pl[x] = findrect(pframe.l+((x*hresolu)>>pdepth),
365 greg 1.1 pframe.d, &ptrunk, rl+x, pdepth);
366     }
367     /* sample the image */
368     for (y = 0; /* y < ysiz */ ; y++) {
369     for (x = 0; x < xsiz-1; x++) {
370 greg 1.20 if (dev->inpready || errno == ENOMEM)
371 greg 1.1 goto escape;
372     /*
373     * Test super-pixel to the right.
374     */
375     if (pl[x] != pl[x+1] && bigdiff(pl[x]->v,
376     pl[x+1]->v, maxdiff)) {
377     refine(pl[x], rl[x].l, rl[x].d,
378     rl[x].r, rl[x].u, 1);
379     refine(pl[x+1], rl[x+1].l, rl[x+1].d,
380     rl[x+1].r, rl[x+1].u, 1);
381     }
382     }
383     if (y >= ysiz-1)
384     break;
385     for (x = 0; x < xsiz; x++) {
386 greg 1.20 if (dev->inpready || errno == ENOMEM)
387 greg 1.1 goto escape;
388     /*
389     * Find super-pixel at this position in next row.
390     */
391     r.l = r.d = 0;
392 greg 1.9 r.r = hresolu; r.u = vresolu;
393     p = findrect(pframe.l+((x*hresolu)>>pdepth),
394     pframe.d+(((y+1)*vresolu)>>pdepth),
395 greg 1.1 &ptrunk, &r, pdepth);
396     /*
397     * Test super-pixel in next row.
398     */
399     if (pl[x] != p && bigdiff(pl[x]->v, p->v, maxdiff)) {
400     refine(pl[x], rl[x].l, rl[x].d,
401     rl[x].r, rl[x].u, 1);
402     refine(p, r.l, r.d, r.r, r.u, 1);
403     }
404     /*
405     * Copy into super-pixel array.
406     */
407     rl[x].l = r.l; rl[x].d = r.d;
408     rl[x].r = r.r; rl[x].u = r.u;
409     pl[x] = p;
410     }
411     }
412     escape:
413     free((char *)rl);
414     free((char *)pl);
415     }
416    
417    
418     int
419     refine(p, xmin, ymin, xmax, ymax, pd) /* refine a node */
420 greg 2.8 register PNODE *p;
421 greg 1.1 int xmin, ymin, xmax, ymax;
422     int pd;
423     {
424     int growth;
425     int mx, my;
426     int i;
427    
428     if (dev->inpready) /* quit for input */
429     return(0);
430    
431     if (pd <= 0) /* depth limit */
432     return(0);
433    
434     mx = (xmin + xmax) >> 1;
435     my = (ymin + ymax) >> 1;
436     growth = 0;
437    
438     if (p->kid == NULL) { /* subdivide */
439    
440     if ((p->kid = newptree()) == NULL)
441 greg 1.20 return(0);
442 greg 1.1 /*
443     * The following paint order can leave a black pixel
444 greg 2.7 * if redraw() is called in (*dev->paintr)().
445 greg 1.1 */
446     if (p->x >= mx && p->y >= my)
447     pcopy(p, p->kid+UR);
448     else
449     paint(p->kid+UR, mx, my, xmax, ymax);
450     if (p->x < mx && p->y >= my)
451     pcopy(p, p->kid+UL);
452     else
453     paint(p->kid+UL, xmin, my, mx, ymax);
454     if (p->x >= mx && p->y < my)
455     pcopy(p, p->kid+DR);
456     else
457     paint(p->kid+DR, mx, ymin, xmax, my);
458     if (p->x < mx && p->y < my)
459     pcopy(p, p->kid+DL);
460     else
461     paint(p->kid+DL, xmin, ymin, mx, my);
462    
463     growth++;
464     }
465     /* do children */
466     if (mx > pframe.l) {
467     if (my > pframe.d)
468     growth += refine(p->kid+DL, xmin, ymin, mx, my, pd-1);
469     if (my < pframe.u)
470     growth += refine(p->kid+UL, xmin, my, mx, ymax, pd-1);
471     }
472     if (mx < pframe.r) {
473     if (my > pframe.d)
474     growth += refine(p->kid+DR, mx, ymin, xmax, my, pd-1);
475     if (my < pframe.u)
476     growth += refine(p->kid+UR, mx, my, xmax, ymax, pd-1);
477     }
478     /* recompute sum */
479     if (growth) {
480     setcolor(p->v, 0.0, 0.0, 0.0);
481     for (i = 0; i < 4; i++)
482     addcolor(p->v, p->kid[i].v);
483     scalecolor(p->v, 0.25);
484     }
485     return(growth);
486     }