ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
Revision: 2.25
Committed: Sun Mar 28 16:31:14 2004 UTC (20 years ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6, rad3R6P1
Changes since 2.24: +4 -4 lines
Log Message:
Increased -aa default settings

File Contents

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