ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
Revision: 2.22
Committed: Thu Jul 3 15:00:19 2003 UTC (20 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.21: +1 -2 lines
Log Message:
Added -N option to rad for parallel rendering (preliminary w/o using -PP)

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rview.c,v 2.21 2003/07/02 01:15:20 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 = 4; /* 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.2; /* ambient accuracy */
66 int ambres = 8; /* ambient resolution */
67 int ambdiv = 32; /* ambient divisions */
68 int ambssamp = 0; /* 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 /* not there, try exec */
124 if ((dev = comm_init(dname, id)) == NULL) {
125 sprintf(errmsg, "cannot start device \"%s\"", dname);
126 error(USER, errmsg);
127 }
128 }
129
130
131 void
132 devclose() /* close our device */
133 {
134 if (dev != NULL)
135 (*dev->close)();
136 dev = NULL;
137 }
138
139
140 void
141 printdevices() /* print list of output devices */
142 {
143 register int i;
144
145 for (i = 0; devtable[i].name; i++)
146 printf("%-16s # %s\n", devtable[i].name, devtable[i].descrip);
147 }
148
149
150 void
151 rview() /* do a view */
152 {
153 char buf[32];
154
155 devopen(dvcname); /* open device */
156 newimage(); /* start image (calls fillreserves) */
157
158 for ( ; ; ) { /* quit in command() */
159 while (hresolu <= 1<<pdepth && vresolu <= 1<<pdepth)
160 command("done: ");
161 while (reserve_mem == NULL)
162 command("out of memory: ");
163 errno = 0;
164 if (hresolu <= psample<<pdepth && vresolu <= psample<<pdepth) {
165 sprintf(buf, "%d sampling...\n", 1<<pdepth);
166 (*dev->comout)(buf);
167 rsample();
168 } else {
169 sprintf(buf, "%d refining...\n", 1<<pdepth);
170 (*dev->comout)(buf);
171 refine(&ptrunk, 0, 0, hresolu, vresolu, pdepth+1);
172 }
173 if (errno == ENOMEM) /* ran out of memory */
174 freereserves();
175 else if (dev->inpready) /* noticed some input */
176 command(": ");
177 else /* finished this depth */
178 pdepth++;
179 }
180 }
181
182
183 void
184 fillreserves() /* fill memory reserves */
185 {
186 if (reserve_mem != NULL)
187 return;
188 reserve_mem = (char *)malloc(RESERVE_AMT);
189 }
190
191
192 void
193 freereserves() /* free memory reserves */
194 {
195 if (reserve_mem == NULL)
196 return;
197 free(reserve_mem);
198 reserve_mem = NULL;
199 }
200
201
202 void
203 command(prompt) /* get/execute command */
204 char *prompt;
205 {
206 #define badcom(s) strncmp(s, inpbuf, args-inpbuf-1)
207 char inpbuf[256];
208 char *args;
209 again:
210 (*dev->comin)(inpbuf, prompt); /* get command + arguments */
211 for (args = inpbuf; *args && *args != ' '; args++)
212 ;
213 if (*args) *args++ = '\0';
214 else *++args = '\0';
215
216 switch (inpbuf[0]) {
217 case 'f': /* new frame (or free mem.) */
218 if (badcom("frame")) {
219 if (badcom("free"))
220 goto commerr;
221 free_objmem();
222 break;
223 }
224 getframe(args);
225 break;
226 case 'v': /* view */
227 if (badcom("view"))
228 goto commerr;
229 getview(args);
230 break;
231 case 'l': /* last view */
232 if (badcom("last"))
233 goto commerr;
234 lastview(args);
235 break;
236 case 'V': /* save view */
237 if (badcom("V"))
238 goto commerr;
239 saveview(args);
240 break;
241 case 'L': /* load view */
242 if (badcom("L"))
243 goto commerr;
244 loadview(args);
245 break;
246 case 'e': /* exposure */
247 if (badcom("exposure"))
248 goto commerr;
249 getexposure(args);
250 break;
251 case 's': /* set a parameter */
252 if (badcom("set")) {
253 #ifdef SIGTSTP
254 if (!badcom("stop"))
255 goto dostop;
256 #endif
257 goto commerr;
258 }
259 setparam(args);
260 break;
261 case 'n': /* new picture */
262 if (badcom("new"))
263 goto commerr;
264 newimage();
265 break;
266 case 't': /* trace a ray */
267 if (badcom("trace"))
268 goto commerr;
269 traceray(args);
270 break;
271 case 'a': /* aim camera */
272 if (badcom("aim"))
273 goto commerr;
274 getaim(args);
275 break;
276 case 'm': /* move camera (or memstats) */
277 if (badcom("move"))
278 #ifdef MSTATS
279 {
280 if (badcom("memory"))
281 goto commerr;
282 printmemstats(stderr);
283 break;
284 }
285 #else
286 goto commerr;
287 #endif
288 getmove(args);
289 break;
290 case 'r': /* rotate/repaint */
291 if (badcom("rotate")) {
292 if (badcom("repaint")) {
293 if (badcom("redraw"))
294 goto commerr;
295 redraw();
296 break;
297 }
298 getrepaint(args);
299 break;
300 }
301 getrotate(args);
302 break;
303 case 'p': /* pivot view */
304 if (badcom("pivot")) {
305 if (badcom("pause"))
306 goto commerr;
307 goto again;
308 }
309 getpivot(args);
310 break;
311 case CTRL('R'): /* redraw */
312 redraw();
313 break;
314 case 'w': /* write */
315 if (badcom("write"))
316 goto commerr;
317 writepict(args);
318 break;
319 case 'q': /* quit */
320 if (badcom("quit"))
321 goto commerr;
322 quit(0);
323 case CTRL('C'): /* interrupt */
324 goto again;
325 #ifdef SIGTSTP
326 case CTRL('Z'):; /* stop */
327 dostop:
328 devclose();
329 kill(0, SIGTSTP);
330 /* pc stops here */
331 devopen(dvcname);
332 redraw();
333 break;
334 #endif
335 case '\0': /* continue */
336 break;
337 default:;
338 commerr:
339 if (iscntrl(inpbuf[0]))
340 sprintf(errmsg, "^%c: unknown control",
341 inpbuf[0]|0100);
342 else
343 sprintf(errmsg, "%s: unknown command", inpbuf);
344 error(COMMAND, errmsg);
345 break;
346 }
347 #undef badcom
348 }
349
350
351 void
352 rsample() /* sample the image */
353 {
354 int xsiz, ysiz, y;
355 RECT r;
356 PNODE *p;
357 register RECT *rl;
358 register PNODE **pl;
359 register int x;
360 /*
361 * We initialize the bottom row in the image at our current
362 * resolution. During sampling, we check super-pixels to the
363 * right and above by calling bigdiff(). If there is a significant
364 * difference, we subsample the super-pixels. The testing process
365 * includes initialization of the next row.
366 */
367 xsiz = (((long)(pframe.r-pframe.l)<<pdepth)+hresolu-1) / hresolu;
368 ysiz = (((long)(pframe.u-pframe.d)<<pdepth)+vresolu-1) / vresolu;
369 rl = (RECT *)malloc(xsiz*sizeof(RECT));
370 if (rl == NULL)
371 return;
372 pl = (PNODE **)malloc(xsiz*sizeof(PNODE *));
373 if (pl == NULL) {
374 free((void *)rl);
375 return;
376 }
377 /*
378 * Initialize the bottom row.
379 */
380 rl[0].l = rl[0].d = 0;
381 rl[0].r = hresolu; rl[0].u = vresolu;
382 pl[0] = findrect(pframe.l, pframe.d, &ptrunk, rl, pdepth);
383 for (x = 1; x < xsiz; x++) {
384 rl[x].l = rl[x].d = 0;
385 rl[x].r = hresolu; rl[x].u = vresolu;
386 pl[x] = findrect(pframe.l+((x*hresolu)>>pdepth),
387 pframe.d, &ptrunk, rl+x, pdepth);
388 }
389 /* sample the image */
390 for (y = 0; /* y < ysiz */ ; y++) {
391 for (x = 0; x < xsiz-1; x++) {
392 if (dev->inpready || errno == ENOMEM)
393 goto escape;
394 /*
395 * Test super-pixel to the right.
396 */
397 if (pl[x] != pl[x+1] && bigdiff(pl[x]->v,
398 pl[x+1]->v, maxdiff)) {
399 refine(pl[x], rl[x].l, rl[x].d,
400 rl[x].r, rl[x].u, 1);
401 refine(pl[x+1], rl[x+1].l, rl[x+1].d,
402 rl[x+1].r, rl[x+1].u, 1);
403 }
404 }
405 if (y >= ysiz-1)
406 break;
407 for (x = 0; x < xsiz; x++) {
408 if (dev->inpready || errno == ENOMEM)
409 goto escape;
410 /*
411 * Find super-pixel at this position in next row.
412 */
413 r.l = r.d = 0;
414 r.r = hresolu; r.u = vresolu;
415 p = findrect(pframe.l+((x*hresolu)>>pdepth),
416 pframe.d+(((y+1)*vresolu)>>pdepth),
417 &ptrunk, &r, pdepth);
418 /*
419 * Test super-pixel in next row.
420 */
421 if (pl[x] != p && bigdiff(pl[x]->v, p->v, maxdiff)) {
422 refine(pl[x], rl[x].l, rl[x].d,
423 rl[x].r, rl[x].u, 1);
424 refine(p, r.l, r.d, r.r, r.u, 1);
425 }
426 /*
427 * Copy into super-pixel array.
428 */
429 rl[x].l = r.l; rl[x].d = r.d;
430 rl[x].r = r.r; rl[x].u = r.u;
431 pl[x] = p;
432 }
433 }
434 escape:
435 free((void *)rl);
436 free((void *)pl);
437 }
438
439
440 int
441 refine(p, xmin, ymin, xmax, ymax, pd) /* refine a node */
442 register PNODE *p;
443 int xmin, ymin, xmax, ymax;
444 int pd;
445 {
446 int growth;
447 int mx, my;
448 int i;
449
450 if (dev->inpready) /* quit for input */
451 return(0);
452
453 if (pd <= 0) /* depth limit */
454 return(0);
455
456 mx = (xmin + xmax) >> 1;
457 my = (ymin + ymax) >> 1;
458 growth = 0;
459
460 if (p->kid == NULL) { /* subdivide */
461
462 if ((p->kid = newptree()) == NULL)
463 return(0);
464 /*
465 * The following paint order can leave a black pixel
466 * if redraw() is called in (*dev->paintr)().
467 */
468 if (p->x >= mx && p->y >= my)
469 pcopy(p, p->kid+UR);
470 else
471 paint(p->kid+UR, mx, my, xmax, ymax);
472 if (p->x < mx && p->y >= my)
473 pcopy(p, p->kid+UL);
474 else
475 paint(p->kid+UL, xmin, my, mx, ymax);
476 if (p->x >= mx && p->y < my)
477 pcopy(p, p->kid+DR);
478 else
479 paint(p->kid+DR, mx, ymin, xmax, my);
480 if (p->x < mx && p->y < my)
481 pcopy(p, p->kid+DL);
482 else
483 paint(p->kid+DL, xmin, ymin, mx, my);
484
485 growth++;
486 }
487 /* do children */
488 if (mx > pframe.l) {
489 if (my > pframe.d)
490 growth += refine(p->kid+DL, xmin, ymin, mx, my, pd-1);
491 if (my < pframe.u)
492 growth += refine(p->kid+UL, xmin, my, mx, ymax, pd-1);
493 }
494 if (mx < pframe.r) {
495 if (my > pframe.d)
496 growth += refine(p->kid+DR, mx, ymin, xmax, my, pd-1);
497 if (my < pframe.u)
498 growth += refine(p->kid+UR, mx, my, xmax, ymax, pd-1);
499 }
500 /* recompute sum */
501 if (growth) {
502 setcolor(p->v, 0.0, 0.0, 0.0);
503 for (i = 0; i < 4; i++)
504 addcolor(p->v, p->kid[i].v);
505 scalecolor(p->v, 0.25);
506 }
507 return(growth);
508 }