ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
Revision: 1.18
Committed: Tue Jun 25 14:06:15 1991 UTC (32 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.17: +1 -1 lines
Log Message:
increased defaults for -dp parameter

File Contents

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