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; /* viewing parameters */ |
24 |
int hresolu, vresolu; /* image resolution */ |
25 |
|
26 |
int psample = 8; /* pixel sample size */ |
27 |
double maxdiff = .15; /* max. sample difference */ |
28 |
|
29 |
double exposure = 1.0; /* exposure for scene */ |
30 |
|
31 |
double dstrsrc = 0.0; /* square source distribution */ |
32 |
double shadthresh = .1; /* shadow threshold */ |
33 |
double shadcert = .25; /* shadow certainty */ |
34 |
|
35 |
int maxdepth = 4; /* maximum recursion depth */ |
36 |
double minweight = 1e-2; /* minimum ray weight */ |
37 |
|
38 |
COLOR ambval = BLKCOLOR; /* ambient value */ |
39 |
double ambacc = 0.2; /* ambient accuracy */ |
40 |
int ambres = 8; /* ambient resolution */ |
41 |
int ambdiv = 32; /* ambient divisions */ |
42 |
int ambssamp = 0; /* ambient super-samples */ |
43 |
int ambounce = 0; /* ambient bounces */ |
44 |
char *amblist[128]; /* ambient include/exclude list */ |
45 |
int ambincl = -1; /* include == 1, exclude == 0 */ |
46 |
|
47 |
int greyscale = 0; /* map colors to brightness? */ |
48 |
char *devname = dev_default; /* output device name */ |
49 |
|
50 |
struct driver *dev = NULL; /* driver functions */ |
51 |
|
52 |
VIEW oldview; /* previous view parameters */ |
53 |
|
54 |
PNODE ptrunk; /* the base of our image */ |
55 |
RECT pframe; /* current frame boundaries */ |
56 |
int pdepth; /* image depth in current frame */ |
57 |
|
58 |
#define CTRL(c) ('c'-'@') |
59 |
|
60 |
|
61 |
quit(code) /* quit program */ |
62 |
int code; |
63 |
{ |
64 |
devclose(); |
65 |
exit(code); |
66 |
} |
67 |
|
68 |
|
69 |
devopen(dname) /* open device driver */ |
70 |
char *dname; |
71 |
{ |
72 |
extern char *progname, *octname; |
73 |
char *id; |
74 |
register int i; |
75 |
|
76 |
id = octname!=NULL ? octname : progname; |
77 |
/* check device table */ |
78 |
for (i = 0; devtable[i].name; i++) |
79 |
if (!strcmp(dname, devtable[i].name)) |
80 |
if ((dev = (*devtable[i].init)(dname, id)) == NULL) { |
81 |
sprintf(errmsg, "cannot initialize %s", dname); |
82 |
error(USER, errmsg); |
83 |
} else |
84 |
return; |
85 |
/* not there, try exec */ |
86 |
if ((dev = comm_init(dname, id)) == 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 (hresolu <= 1<<pdepth && |
119 |
vresolu <= 1<<pdepth) |
120 |
command("done: "); |
121 |
|
122 |
if (hresolu <= psample<<pdepth && |
123 |
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, hresolu, 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->comin)(inpbuf, prompt); /* get command + arguments */ |
149 |
for (args = inpbuf; *args && *args != ' '; args++) |
150 |
; |
151 |
if (*args) *args++ = '\0'; |
152 |
else *++args = '\0'; |
153 |
|
154 |
switch (inpbuf[0]) { |
155 |
case 'f': /* new frame */ |
156 |
if (badcom("frame")) |
157 |
goto commerr; |
158 |
getframe(args); |
159 |
break; |
160 |
case 'v': /* view */ |
161 |
if (badcom("view")) |
162 |
goto commerr; |
163 |
getview(args); |
164 |
break; |
165 |
case 'l': /* last view */ |
166 |
if (badcom("last")) |
167 |
goto commerr; |
168 |
lastview(args); |
169 |
break; |
170 |
case 'e': /* exposure */ |
171 |
if (badcom("exposure")) |
172 |
goto commerr; |
173 |
getexposure(args); |
174 |
break; |
175 |
case 's': /* set a parameter */ |
176 |
if (badcom("set")) |
177 |
goto commerr; |
178 |
setparam(args); |
179 |
break; |
180 |
case 'n': /* new picture */ |
181 |
if (badcom("new")) |
182 |
goto commerr; |
183 |
newimage(); |
184 |
break; |
185 |
case 't': /* trace a ray */ |
186 |
if (badcom("trace")) |
187 |
goto commerr; |
188 |
traceray(args); |
189 |
break; |
190 |
case 'a': /* aim camera */ |
191 |
if (badcom("aim")) |
192 |
goto commerr; |
193 |
getaim(args); |
194 |
break; |
195 |
case 'm': /* move camera */ |
196 |
if (badcom("move")) |
197 |
goto commerr; |
198 |
getmove(args); |
199 |
break; |
200 |
case 'r': /* rotate/repaint */ |
201 |
if (badcom("rotate")) { |
202 |
if (badcom("repaint")) |
203 |
goto commerr; |
204 |
getrepaint(args); |
205 |
break; |
206 |
} |
207 |
getrotate(args); |
208 |
break; |
209 |
case 'p': /* pivot view */ |
210 |
if (badcom("pivot")) |
211 |
goto commerr; |
212 |
getpivot(args); |
213 |
break; |
214 |
case CTRL(R): /* redraw */ |
215 |
redraw(); |
216 |
break; |
217 |
case 'w': /* write */ |
218 |
if (badcom("write")) |
219 |
goto commerr; |
220 |
writepict(args); |
221 |
break; |
222 |
case 'q': /* quit */ |
223 |
if (badcom("quit")) |
224 |
goto commerr; |
225 |
quit(0); |
226 |
case CTRL(C): /* interrupt */ |
227 |
goto again; |
228 |
#ifdef SIGTSTP |
229 |
case CTRL(Z): /* stop */ |
230 |
devclose(); |
231 |
kill(0, SIGTSTP); |
232 |
/* pc stops here */ |
233 |
devopen(devname); |
234 |
redraw(); |
235 |
break; |
236 |
#endif |
237 |
case '\0': /* continue */ |
238 |
break; |
239 |
default:; |
240 |
commerr: |
241 |
if (iscntrl(inpbuf[0])) |
242 |
sprintf(errmsg, "^%c: unknown control", |
243 |
inpbuf[0]|0100); |
244 |
else |
245 |
sprintf(errmsg, "%s: unknown command", inpbuf); |
246 |
error(COMMAND, errmsg); |
247 |
break; |
248 |
} |
249 |
#undef badcom |
250 |
} |
251 |
|
252 |
|
253 |
rsample() /* sample the image */ |
254 |
{ |
255 |
int xsiz, ysiz, y; |
256 |
RECT r; |
257 |
PNODE *p; |
258 |
register RECT *rl; |
259 |
register PNODE **pl; |
260 |
register int x; |
261 |
/* |
262 |
* We initialize the bottom row in the image at our current |
263 |
* resolution. During sampling, we check super-pixels to the |
264 |
* right and above by calling bigdiff(). If there is a significant |
265 |
* difference, we subsample the super-pixels. The testing process |
266 |
* includes initialization of the next row. |
267 |
*/ |
268 |
xsiz = (((pframe.r-pframe.l)<<pdepth)+hresolu-1) / hresolu; |
269 |
ysiz = (((pframe.u-pframe.d)<<pdepth)+vresolu-1) / 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 = hresolu; rl[0].u = 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 = hresolu; rl[x].u = vresolu; |
283 |
pl[x] = findrect(pframe.l+((x*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 = hresolu; r.u = vresolu; |
312 |
p = findrect(pframe.l+((x*hresolu)>>pdepth), |
313 |
pframe.d+(((y+1)*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 |
} |