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