ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
Revision: 2.28
Committed: Mon Jun 13 20:07:56 2005 UTC (18 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1, rad3R8, rad3R9
Changes since 2.27: +3 -1 lines
Log Message:
Added -R option for pure Monte Carlo rendering, replacing -DMC compile option

File Contents

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