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