ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
Revision: 2.19
Committed: Tue Feb 25 02:47:23 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.18: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

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