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