ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
Revision: 1.5
Committed: Mon Jul 10 15:21:25 1989 UTC (34 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +1 -1 lines
Log Message:
Reduced default ambient resolution

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