ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
Revision: 2.4
Committed: Mon Dec 23 23:18:29 1991 UTC (32 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +4 -4 lines
Log Message:
fixed CTRL(c) macro for ANSII compilers

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