ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rview.c
Revision: 1.8
Committed: Fri Oct 27 11:48:25 1989 UTC (34 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.7: +7 -3 lines
Log Message:
added repaint command

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