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 |
double atof(); |
174 |
char inpbuf[256]; |
175 |
char *args; |
176 |
again: |
177 |
(*dev->comin)(inpbuf, prompt); /* get command + arguments */ |
178 |
for (args = inpbuf; *args && *args != ' '; args++) |
179 |
; |
180 |
if (*args) *args++ = '\0'; |
181 |
else *++args = '\0'; |
182 |
|
183 |
switch (inpbuf[0]) { |
184 |
case 'f': /* new frame */ |
185 |
if (badcom("frame")) |
186 |
goto commerr; |
187 |
getframe(args); |
188 |
break; |
189 |
case 'v': /* view */ |
190 |
if (badcom("view")) |
191 |
goto commerr; |
192 |
getview(args); |
193 |
break; |
194 |
case 'l': /* last view */ |
195 |
if (badcom("last")) |
196 |
goto commerr; |
197 |
lastview(args); |
198 |
break; |
199 |
case 'e': /* exposure */ |
200 |
if (badcom("exposure")) |
201 |
goto commerr; |
202 |
getexposure(args); |
203 |
break; |
204 |
case 's': /* set a parameter */ |
205 |
if (badcom("set")) |
206 |
goto commerr; |
207 |
setparam(args); |
208 |
break; |
209 |
case 'n': /* new picture */ |
210 |
if (badcom("new")) |
211 |
goto commerr; |
212 |
newimage(); |
213 |
break; |
214 |
case 't': /* trace a ray */ |
215 |
if (badcom("trace")) |
216 |
goto commerr; |
217 |
traceray(args); |
218 |
break; |
219 |
case 'a': /* aim camera */ |
220 |
if (badcom("aim")) |
221 |
goto commerr; |
222 |
getaim(args); |
223 |
break; |
224 |
case 'm': /* move camera */ |
225 |
if (badcom("move")) |
226 |
goto commerr; |
227 |
getmove(args); |
228 |
break; |
229 |
case 'r': /* rotate/repaint */ |
230 |
if (badcom("rotate")) { |
231 |
if (badcom("repaint")) |
232 |
goto commerr; |
233 |
getrepaint(args); |
234 |
break; |
235 |
} |
236 |
getrotate(args); |
237 |
break; |
238 |
case 'p': /* pivot view */ |
239 |
if (badcom("pivot")) |
240 |
goto commerr; |
241 |
getpivot(args); |
242 |
break; |
243 |
case CTRL(R): /* redraw */ |
244 |
redraw(); |
245 |
break; |
246 |
case 'w': /* write */ |
247 |
if (badcom("write")) |
248 |
goto commerr; |
249 |
writepict(args); |
250 |
break; |
251 |
case 'q': /* quit */ |
252 |
if (badcom("quit")) |
253 |
goto commerr; |
254 |
quit(0); |
255 |
case CTRL(C): /* interrupt */ |
256 |
goto again; |
257 |
#ifdef SIGTSTP |
258 |
case CTRL(Z): /* stop */ |
259 |
devclose(); |
260 |
kill(0, SIGTSTP); |
261 |
/* pc stops here */ |
262 |
devopen(devname); |
263 |
redraw(); |
264 |
break; |
265 |
#endif |
266 |
case '\0': /* continue */ |
267 |
break; |
268 |
default:; |
269 |
commerr: |
270 |
if (iscntrl(inpbuf[0])) |
271 |
sprintf(errmsg, "^%c: unknown control", |
272 |
inpbuf[0]|0100); |
273 |
else |
274 |
sprintf(errmsg, "%s: unknown command", inpbuf); |
275 |
error(COMMAND, errmsg); |
276 |
break; |
277 |
} |
278 |
#undef badcom |
279 |
} |
280 |
|
281 |
|
282 |
rsample() /* sample the image */ |
283 |
{ |
284 |
int xsiz, ysiz, y; |
285 |
RECT r; |
286 |
PNODE *p; |
287 |
register RECT *rl; |
288 |
register PNODE **pl; |
289 |
register int x; |
290 |
/* |
291 |
* We initialize the bottom row in the image at our current |
292 |
* resolution. During sampling, we check super-pixels to the |
293 |
* right and above by calling bigdiff(). If there is a significant |
294 |
* difference, we subsample the super-pixels. The testing process |
295 |
* includes initialization of the next row. |
296 |
*/ |
297 |
xsiz = (((pframe.r-pframe.l)<<pdepth)+hresolu-1) / hresolu; |
298 |
ysiz = (((pframe.u-pframe.d)<<pdepth)+vresolu-1) / vresolu; |
299 |
rl = (RECT *)malloc(xsiz*sizeof(RECT)); |
300 |
if (rl == NULL) |
301 |
return; |
302 |
pl = (PNODE **)malloc(xsiz*sizeof(PNODE *)); |
303 |
if (pl == NULL) |
304 |
return; |
305 |
/* |
306 |
* Initialize the bottom row. |
307 |
*/ |
308 |
rl[0].l = rl[0].d = 0; |
309 |
rl[0].r = hresolu; rl[0].u = vresolu; |
310 |
pl[0] = findrect(pframe.l, pframe.d, &ptrunk, rl, pdepth); |
311 |
for (x = 1; x < xsiz; x++) { |
312 |
rl[x].l = rl[x].d = 0; |
313 |
rl[x].r = hresolu; rl[x].u = vresolu; |
314 |
pl[x] = findrect(pframe.l+((x*hresolu)>>pdepth), |
315 |
pframe.d, &ptrunk, rl+x, pdepth); |
316 |
} |
317 |
/* sample the image */ |
318 |
for (y = 0; /* y < ysiz */ ; y++) { |
319 |
for (x = 0; x < xsiz-1; x++) { |
320 |
if (dev->inpready || errno == ENOMEM) |
321 |
goto escape; |
322 |
/* |
323 |
* Test super-pixel to the right. |
324 |
*/ |
325 |
if (pl[x] != pl[x+1] && bigdiff(pl[x]->v, |
326 |
pl[x+1]->v, maxdiff)) { |
327 |
refine(pl[x], rl[x].l, rl[x].d, |
328 |
rl[x].r, rl[x].u, 1); |
329 |
refine(pl[x+1], rl[x+1].l, rl[x+1].d, |
330 |
rl[x+1].r, rl[x+1].u, 1); |
331 |
} |
332 |
} |
333 |
if (y >= ysiz-1) |
334 |
break; |
335 |
for (x = 0; x < xsiz; x++) { |
336 |
if (dev->inpready || errno == ENOMEM) |
337 |
goto escape; |
338 |
/* |
339 |
* Find super-pixel at this position in next row. |
340 |
*/ |
341 |
r.l = r.d = 0; |
342 |
r.r = hresolu; r.u = vresolu; |
343 |
p = findrect(pframe.l+((x*hresolu)>>pdepth), |
344 |
pframe.d+(((y+1)*vresolu)>>pdepth), |
345 |
&ptrunk, &r, pdepth); |
346 |
/* |
347 |
* Test super-pixel in next row. |
348 |
*/ |
349 |
if (pl[x] != p && bigdiff(pl[x]->v, p->v, maxdiff)) { |
350 |
refine(pl[x], rl[x].l, rl[x].d, |
351 |
rl[x].r, rl[x].u, 1); |
352 |
refine(p, r.l, r.d, r.r, r.u, 1); |
353 |
} |
354 |
/* |
355 |
* Copy into super-pixel array. |
356 |
*/ |
357 |
rl[x].l = r.l; rl[x].d = r.d; |
358 |
rl[x].r = r.r; rl[x].u = r.u; |
359 |
pl[x] = p; |
360 |
} |
361 |
} |
362 |
escape: |
363 |
free((char *)rl); |
364 |
free((char *)pl); |
365 |
} |
366 |
|
367 |
|
368 |
int |
369 |
refine(p, xmin, ymin, xmax, ymax, pd) /* refine a node */ |
370 |
register PNODE *p; |
371 |
int xmin, ymin, xmax, ymax; |
372 |
int pd; |
373 |
{ |
374 |
int growth; |
375 |
int mx, my; |
376 |
int i; |
377 |
|
378 |
if (dev->inpready) /* quit for input */ |
379 |
return(0); |
380 |
|
381 |
if (pd <= 0) /* depth limit */ |
382 |
return(0); |
383 |
|
384 |
mx = (xmin + xmax) >> 1; |
385 |
my = (ymin + ymax) >> 1; |
386 |
growth = 0; |
387 |
|
388 |
if (p->kid == NULL) { /* subdivide */ |
389 |
|
390 |
if ((p->kid = newptree()) == NULL) |
391 |
return(0); |
392 |
/* |
393 |
* The following paint order can leave a black pixel |
394 |
* when redraw() is called in (*dev->paintr)(). |
395 |
*/ |
396 |
if (p->x >= mx && p->y >= my) |
397 |
pcopy(p, p->kid+UR); |
398 |
else |
399 |
paint(p->kid+UR, mx, my, xmax, ymax); |
400 |
if (p->x < mx && p->y >= my) |
401 |
pcopy(p, p->kid+UL); |
402 |
else |
403 |
paint(p->kid+UL, xmin, my, mx, ymax); |
404 |
if (p->x >= mx && p->y < my) |
405 |
pcopy(p, p->kid+DR); |
406 |
else |
407 |
paint(p->kid+DR, mx, ymin, xmax, my); |
408 |
if (p->x < mx && p->y < my) |
409 |
pcopy(p, p->kid+DL); |
410 |
else |
411 |
paint(p->kid+DL, xmin, ymin, mx, my); |
412 |
|
413 |
growth++; |
414 |
} |
415 |
/* do children */ |
416 |
if (mx > pframe.l) { |
417 |
if (my > pframe.d) |
418 |
growth += refine(p->kid+DL, xmin, ymin, mx, my, pd-1); |
419 |
if (my < pframe.u) |
420 |
growth += refine(p->kid+UL, xmin, my, mx, ymax, pd-1); |
421 |
} |
422 |
if (mx < pframe.r) { |
423 |
if (my > pframe.d) |
424 |
growth += refine(p->kid+DR, mx, ymin, xmax, my, pd-1); |
425 |
if (my < pframe.u) |
426 |
growth += refine(p->kid+UR, mx, my, xmax, ymax, pd-1); |
427 |
} |
428 |
/* recompute sum */ |
429 |
if (growth) { |
430 |
setcolor(p->v, 0.0, 0.0, 0.0); |
431 |
for (i = 0; i < 4; i++) |
432 |
addcolor(p->v, p->kid[i].v); |
433 |
scalecolor(p->v, 0.25); |
434 |
} |
435 |
return(growth); |
436 |
} |