1 |
– |
/* Copyright (c) 1987 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* rview.c - routines and variables for interactive view generation. |
6 |
|
* |
7 |
< |
* 3/24/87 |
7 |
> |
* External symbols declared in rpaint.h |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include "standard.h" |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#include "color.h" |
12 |
> |
#include <signal.h> |
13 |
> |
#include <ctype.h> |
14 |
|
|
15 |
+ |
#include "ray.h" |
16 |
|
#include "rpaint.h" |
17 |
|
|
19 |
– |
#include <signal.h> |
18 |
|
|
19 |
< |
#include <ctype.h> |
19 |
> |
CUBE thescene; /* our scene */ |
20 |
> |
OBJECT nsceneobjs; /* number of objects in our scene */ |
21 |
|
|
22 |
< |
VIEW ourview = STDVIEW(470); /* viewing parameters */ |
22 |
> |
int dimlist[MAXDIM]; /* sampling dimensions */ |
23 |
> |
int ndims = 0; /* number of sampling dimensions */ |
24 |
> |
int samplendx = 0; /* index for this sample */ |
25 |
|
|
26 |
+ |
extern void ambnotify(); |
27 |
+ |
void (*addobjnotify[])() = {ambnotify, NULL}; |
28 |
+ |
|
29 |
+ |
VIEW ourview = STDVIEW; /* viewing parameters */ |
30 |
+ |
int hresolu, vresolu; /* image resolution */ |
31 |
+ |
|
32 |
+ |
void (*trace)() = NULL; /* trace call */ |
33 |
+ |
|
34 |
+ |
int do_irrad = 0; /* compute irradiance? */ |
35 |
+ |
|
36 |
|
int psample = 8; /* pixel sample size */ |
37 |
< |
double maxdiff = .15; /* max. sample difference */ |
37 |
> |
double maxdiff = .15; /* max. sample difference */ |
38 |
|
|
39 |
< |
double exposure = 1.0; /* exposure for scene */ |
39 |
> |
double exposure = 1.0; /* exposure for scene */ |
40 |
|
|
41 |
< |
double dstrsrc = 0.0; /* square source distribution */ |
41 |
> |
double dstrsrc = 0.0; /* square source distribution */ |
42 |
> |
double shadthresh = .1; /* shadow threshold */ |
43 |
> |
double shadcert = .25; /* shadow certainty */ |
44 |
> |
int directrelay = 0; /* number of source relays */ |
45 |
> |
int vspretest = 128; /* virtual source pretest density */ |
46 |
> |
int directvis = 1; /* sources visible? */ |
47 |
> |
double srcsizerat = 0.; /* maximum ratio source size/dist. */ |
48 |
|
|
49 |
< |
int maxdepth = 4; /* maximum recursion depth */ |
50 |
< |
double minweight = 1e-2; /* minimum ray weight */ |
49 |
> |
COLOR cextinction = BLKCOLOR; /* global extinction coefficient */ |
50 |
> |
COLOR salbedo = BLKCOLOR; /* global scattering albedo */ |
51 |
> |
double seccg = 0.; /* global scattering eccentricity */ |
52 |
> |
double ssampdist = 0.; /* scatter sampling distance */ |
53 |
|
|
54 |
+ |
double specthresh = .3; /* specular sampling threshold */ |
55 |
+ |
double specjitter = 1.; /* specular sampling jitter */ |
56 |
+ |
|
57 |
+ |
int backvis = 1; /* back face visibility */ |
58 |
+ |
|
59 |
+ |
int maxdepth = 6; /* maximum recursion depth */ |
60 |
+ |
double minweight = 1e-2; /* minimum ray weight */ |
61 |
+ |
|
62 |
+ |
char *ambfile = NULL; /* ambient file name */ |
63 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
64 |
< |
double ambacc = 0.2; /* ambient accuracy */ |
65 |
< |
int ambres = 64; /* ambient resolution */ |
66 |
< |
int ambdiv = 32; /* ambient divisions */ |
67 |
< |
int ambssamp = 0; /* ambient super-samples */ |
64 |
> |
int ambvwt = 0; /* initial weight for ambient value */ |
65 |
> |
double ambacc = 0.3; /* ambient accuracy */ |
66 |
> |
int ambres = 32; /* ambient resolution */ |
67 |
> |
int ambdiv = 256; /* ambient divisions */ |
68 |
> |
int ambssamp = 64; /* ambient super-samples */ |
69 |
|
int ambounce = 0; /* ambient bounces */ |
70 |
< |
char *amblist[128]; /* ambient include/exclude list */ |
70 |
> |
char *amblist[AMBLLEN]; /* ambient include/exclude list */ |
71 |
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
72 |
|
|
73 |
|
int greyscale = 0; /* map colors to brightness? */ |
74 |
< |
char *devname = "X"; /* output device name */ |
74 |
> |
char *dvcname = dev_default; /* output device name */ |
75 |
|
|
76 |
|
struct driver *dev = NULL; /* driver functions */ |
77 |
|
|
78 |
+ |
char rifname[128]; /* rad input file name */ |
79 |
+ |
|
80 |
|
VIEW oldview; /* previous view parameters */ |
81 |
|
|
82 |
|
PNODE ptrunk; /* the base of our image */ |
83 |
|
RECT pframe; /* current frame boundaries */ |
84 |
|
int pdepth; /* image depth in current frame */ |
85 |
|
|
86 |
< |
#define CTRL(c) ('c'-'@') |
86 |
> |
static char *reserve_mem = NULL; /* pre-allocated reserve memory */ |
87 |
|
|
88 |
+ |
#define RESERVE_AMT 32768 /* amount of memory to reserve */ |
89 |
|
|
90 |
+ |
#define CTRL(c) ((c)-'@') |
91 |
+ |
|
92 |
+ |
|
93 |
+ |
void |
94 |
|
quit(code) /* quit program */ |
95 |
|
int code; |
96 |
|
{ |
97 |
+ |
#ifdef MSTATS |
98 |
+ |
if (code == 2 && errno == ENOMEM) |
99 |
+ |
printmemstats(stderr); |
100 |
+ |
#endif |
101 |
|
devclose(); |
102 |
|
exit(code); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
+ |
void |
107 |
|
devopen(dname) /* open device driver */ |
108 |
|
char *dname; |
109 |
|
{ |
110 |
< |
extern char *progname; |
111 |
< |
char *devargv[3]; |
110 |
> |
extern char *progname, *octname; |
111 |
> |
char *id; |
112 |
|
register int i; |
113 |
+ |
|
114 |
+ |
id = octname!=NULL ? octname : progname; |
115 |
|
/* check device table */ |
116 |
|
for (i = 0; devtable[i].name; i++) |
117 |
< |
if (!strcmp(dname, devtable[i].name)) |
118 |
< |
if ((dev = (*devtable[i].init)(progname)) == NULL) { |
117 |
> |
if (!strcmp(dname, devtable[i].name)) { |
118 |
> |
if ((dev = (*devtable[i].init)(dname, id)) == NULL) { |
119 |
|
sprintf(errmsg, "cannot initialize %s", dname); |
120 |
|
error(USER, errmsg); |
121 |
|
} else |
122 |
|
return; |
123 |
+ |
} |
124 |
|
/* not there, try exec */ |
125 |
< |
devargv[0] = dname; |
82 |
< |
devargv[1] = progname; |
83 |
< |
devargv[2] = NULL; |
84 |
< |
if ((dev = comm_init(devargv)) == NULL) { |
125 |
> |
if ((dev = comm_init(dname, id)) == NULL) { |
126 |
|
sprintf(errmsg, "cannot start device \"%s\"", dname); |
127 |
|
error(USER, errmsg); |
128 |
|
} |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
+ |
void |
133 |
|
devclose() /* close our device */ |
134 |
|
{ |
135 |
|
if (dev != NULL) |
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
+ |
void |
142 |
|
printdevices() /* print list of output devices */ |
143 |
|
{ |
144 |
|
register int i; |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
+ |
void |
152 |
|
rview() /* do a view */ |
153 |
|
{ |
154 |
|
char buf[32]; |
155 |
|
|
156 |
< |
devopen(devname); /* open device */ |
157 |
< |
newimage(); /* set up image */ |
156 |
> |
devopen(dvcname); /* open device */ |
157 |
> |
newimage(); /* start image (calls fillreserves) */ |
158 |
|
|
159 |
|
for ( ; ; ) { /* quit in command() */ |
160 |
< |
while (ourview.hresolu <= 1<<pdepth && |
117 |
< |
ourview.vresolu <= 1<<pdepth) |
160 |
> |
while (hresolu <= 1<<pdepth && vresolu <= 1<<pdepth) |
161 |
|
command("done: "); |
162 |
< |
|
163 |
< |
if (ourview.hresolu <= psample<<pdepth && |
164 |
< |
ourview.vresolu <= psample<<pdepth) { |
162 |
> |
while (reserve_mem == NULL) |
163 |
> |
command("out of memory: "); |
164 |
> |
errno = 0; |
165 |
> |
if (hresolu <= psample<<pdepth && vresolu <= psample<<pdepth) { |
166 |
|
sprintf(buf, "%d sampling...\n", 1<<pdepth); |
167 |
|
(*dev->comout)(buf); |
168 |
|
rsample(); |
169 |
|
} else { |
170 |
|
sprintf(buf, "%d refining...\n", 1<<pdepth); |
171 |
|
(*dev->comout)(buf); |
172 |
< |
refine(&ptrunk, 0, 0, ourview.hresolu, |
129 |
< |
ourview.vresolu, pdepth+1); |
172 |
> |
refine(&ptrunk, 0, 0, hresolu, vresolu, pdepth+1); |
173 |
|
} |
174 |
< |
if (dev->inpready) |
174 |
> |
if (errno == ENOMEM) /* ran out of memory */ |
175 |
> |
freereserves(); |
176 |
> |
else if (dev->inpready) /* noticed some input */ |
177 |
|
command(": "); |
178 |
< |
else |
178 |
> |
else /* finished this depth */ |
179 |
|
pdepth++; |
180 |
|
} |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
+ |
void |
185 |
+ |
fillreserves() /* fill memory reserves */ |
186 |
+ |
{ |
187 |
+ |
if (reserve_mem != NULL) |
188 |
+ |
return; |
189 |
+ |
reserve_mem = (char *)malloc(RESERVE_AMT); |
190 |
+ |
} |
191 |
+ |
|
192 |
+ |
|
193 |
+ |
void |
194 |
+ |
freereserves() /* free memory reserves */ |
195 |
+ |
{ |
196 |
+ |
if (reserve_mem == NULL) |
197 |
+ |
return; |
198 |
+ |
free(reserve_mem); |
199 |
+ |
reserve_mem = NULL; |
200 |
+ |
} |
201 |
+ |
|
202 |
+ |
|
203 |
+ |
void |
204 |
|
command(prompt) /* get/execute command */ |
205 |
|
char *prompt; |
206 |
|
{ |
207 |
< |
#define badcom(s) strncmp(s, inpbuf, args-inpbuf-1) |
143 |
< |
double atof(); |
207 |
> |
#define badcom(s) strncmp(s, inpbuf, args-inpbuf-1) |
208 |
|
char inpbuf[256]; |
209 |
|
char *args; |
210 |
|
again: |
211 |
< |
(*dev->comout)(prompt); /* get command + arguments */ |
148 |
< |
(*dev->comin)(inpbuf); |
211 |
> |
(*dev->comin)(inpbuf, prompt); /* get command + arguments */ |
212 |
|
for (args = inpbuf; *args && *args != ' '; args++) |
213 |
|
; |
214 |
|
if (*args) *args++ = '\0'; |
215 |
|
else *++args = '\0'; |
216 |
|
|
217 |
|
switch (inpbuf[0]) { |
218 |
< |
case 'f': /* new frame */ |
219 |
< |
if (badcom("frame")) |
220 |
< |
goto commerr; |
218 |
> |
case 'f': /* new frame (|focus|free) */ |
219 |
> |
if (badcom("frame")) { |
220 |
> |
if (badcom("focus")) { |
221 |
> |
if (badcom("free")) |
222 |
> |
goto commerr; |
223 |
> |
free_objmem(); |
224 |
> |
break; |
225 |
> |
} |
226 |
> |
getfocus(args); |
227 |
> |
break; |
228 |
> |
} |
229 |
|
getframe(args); |
230 |
|
break; |
231 |
|
case 'v': /* view */ |
238 |
|
goto commerr; |
239 |
|
lastview(args); |
240 |
|
break; |
241 |
+ |
case 'V': /* save view */ |
242 |
+ |
if (badcom("V")) |
243 |
+ |
goto commerr; |
244 |
+ |
saveview(args); |
245 |
+ |
break; |
246 |
+ |
case 'L': /* load view */ |
247 |
+ |
if (badcom("L")) |
248 |
+ |
goto commerr; |
249 |
+ |
loadview(args); |
250 |
+ |
break; |
251 |
|
case 'e': /* exposure */ |
252 |
|
if (badcom("exposure")) |
253 |
|
goto commerr; |
254 |
|
getexposure(args); |
255 |
|
break; |
256 |
|
case 's': /* set a parameter */ |
257 |
< |
if (badcom("set")) |
257 |
> |
if (badcom("set")) { |
258 |
> |
#ifdef SIGTSTP |
259 |
> |
if (!badcom("stop")) |
260 |
> |
goto dostop; |
261 |
> |
#endif |
262 |
|
goto commerr; |
263 |
+ |
} |
264 |
|
setparam(args); |
265 |
|
break; |
266 |
|
case 'n': /* new picture */ |
278 |
|
goto commerr; |
279 |
|
getaim(args); |
280 |
|
break; |
281 |
< |
case 'm': /* move camera */ |
281 |
> |
case 'm': /* move camera (or memstats) */ |
282 |
|
if (badcom("move")) |
283 |
+ |
#ifdef MSTATS |
284 |
+ |
{ |
285 |
+ |
if (badcom("memory")) |
286 |
+ |
goto commerr; |
287 |
+ |
printmemstats(stderr); |
288 |
+ |
break; |
289 |
+ |
} |
290 |
+ |
#else |
291 |
|
goto commerr; |
292 |
+ |
#endif |
293 |
|
getmove(args); |
294 |
|
break; |
295 |
< |
case 'r': /* rotate camera */ |
296 |
< |
if (badcom("rotate")) |
297 |
< |
goto commerr; |
295 |
> |
case 'r': /* rotate/repaint */ |
296 |
> |
if (badcom("rotate")) { |
297 |
> |
if (badcom("repaint")) { |
298 |
> |
if (badcom("redraw")) |
299 |
> |
goto commerr; |
300 |
> |
redraw(); |
301 |
> |
break; |
302 |
> |
} |
303 |
> |
getrepaint(args); |
304 |
> |
break; |
305 |
> |
} |
306 |
|
getrotate(args); |
307 |
|
break; |
308 |
|
case 'p': /* pivot view */ |
309 |
< |
if (badcom("pivot")) |
310 |
< |
goto commerr; |
309 |
> |
if (badcom("pivot")) { |
310 |
> |
if (badcom("pause")) |
311 |
> |
goto commerr; |
312 |
> |
goto again; |
313 |
> |
} |
314 |
|
getpivot(args); |
315 |
|
break; |
316 |
< |
case CTRL(R): /* redraw */ |
316 |
> |
case CTRL('R'): /* redraw */ |
317 |
|
redraw(); |
318 |
|
break; |
319 |
|
case 'w': /* write */ |
325 |
|
if (badcom("quit")) |
326 |
|
goto commerr; |
327 |
|
quit(0); |
328 |
< |
case CTRL(C): /* interrupt */ |
328 |
> |
case CTRL('C'): /* interrupt */ |
329 |
|
goto again; |
330 |
< |
#ifdef SIGTSTP |
331 |
< |
case CTRL(Z): /* stop */ |
330 |
> |
#ifdef SIGTSTP |
331 |
> |
case CTRL('Z'):; /* stop */ |
332 |
> |
dostop: |
333 |
|
devclose(); |
334 |
|
kill(0, SIGTSTP); |
335 |
|
/* pc stops here */ |
336 |
< |
devopen(devname); |
336 |
> |
devopen(dvcname); |
337 |
|
redraw(); |
338 |
|
break; |
339 |
|
#endif |
349 |
|
error(COMMAND, errmsg); |
350 |
|
break; |
351 |
|
} |
352 |
< |
#undef badcom |
352 |
> |
#undef badcom |
353 |
|
} |
354 |
|
|
355 |
|
|
356 |
+ |
void |
357 |
|
rsample() /* sample the image */ |
358 |
|
{ |
359 |
|
int xsiz, ysiz, y; |
360 |
|
RECT r; |
361 |
|
PNODE *p; |
362 |
|
register RECT *rl; |
363 |
< |
register PNODE **pl; |
363 |
> |
register PNODE **pl; |
364 |
|
register int x; |
365 |
|
/* |
366 |
|
* We initialize the bottom row in the image at our current |
367 |
< |
* resolution. During sampling, we check super-pixels to the |
367 |
> |
* resolution. During sampling, we check super-pixels to the |
368 |
|
* right and above by calling bigdiff(). If there is a significant |
369 |
|
* difference, we subsample the super-pixels. The testing process |
370 |
|
* includes initialization of the next row. |
371 |
|
*/ |
372 |
< |
xsiz = (((pframe.r-pframe.l)<<pdepth)+ourview.hresolu-1) / |
373 |
< |
ourview.hresolu; |
266 |
< |
ysiz = (((pframe.u-pframe.d)<<pdepth)+ourview.vresolu-1) / |
267 |
< |
ourview.vresolu; |
372 |
> |
xsiz = (((long)(pframe.r-pframe.l)<<pdepth)+hresolu-1) / hresolu; |
373 |
> |
ysiz = (((long)(pframe.u-pframe.d)<<pdepth)+vresolu-1) / vresolu; |
374 |
|
rl = (RECT *)malloc(xsiz*sizeof(RECT)); |
375 |
+ |
if (rl == NULL) |
376 |
+ |
return; |
377 |
|
pl = (PNODE **)malloc(xsiz*sizeof(PNODE *)); |
378 |
< |
if (rl == NULL || pl == NULL) |
379 |
< |
error(SYSTEM, "out of memory in rsample"); |
378 |
> |
if (pl == NULL) { |
379 |
> |
free((void *)rl); |
380 |
> |
return; |
381 |
> |
} |
382 |
|
/* |
383 |
|
* Initialize the bottom row. |
384 |
|
*/ |
385 |
|
rl[0].l = rl[0].d = 0; |
386 |
< |
rl[0].r = ourview.hresolu; rl[0].u = ourview.vresolu; |
386 |
> |
rl[0].r = hresolu; rl[0].u = vresolu; |
387 |
|
pl[0] = findrect(pframe.l, pframe.d, &ptrunk, rl, pdepth); |
388 |
|
for (x = 1; x < xsiz; x++) { |
389 |
|
rl[x].l = rl[x].d = 0; |
390 |
< |
rl[x].r = ourview.hresolu; rl[x].u = ourview.vresolu; |
391 |
< |
pl[x] = findrect(pframe.l+((x*ourview.hresolu)>>pdepth), |
390 |
> |
rl[x].r = hresolu; rl[x].u = vresolu; |
391 |
> |
pl[x] = findrect(pframe.l+((x*hresolu)>>pdepth), |
392 |
|
pframe.d, &ptrunk, rl+x, pdepth); |
393 |
|
} |
394 |
|
/* sample the image */ |
395 |
|
for (y = 0; /* y < ysiz */ ; y++) { |
396 |
|
for (x = 0; x < xsiz-1; x++) { |
397 |
< |
if (dev->inpready) |
397 |
> |
if (dev->inpready || errno == ENOMEM) |
398 |
|
goto escape; |
399 |
|
/* |
400 |
|
* Test super-pixel to the right. |
410 |
|
if (y >= ysiz-1) |
411 |
|
break; |
412 |
|
for (x = 0; x < xsiz; x++) { |
413 |
< |
if (dev->inpready) |
413 |
> |
if (dev->inpready || errno == ENOMEM) |
414 |
|
goto escape; |
415 |
|
/* |
416 |
|
* Find super-pixel at this position in next row. |
417 |
|
*/ |
418 |
|
r.l = r.d = 0; |
419 |
< |
r.r = ourview.hresolu; r.u = ourview.vresolu; |
420 |
< |
p = findrect(pframe.l+((x*ourview.hresolu)>>pdepth), |
421 |
< |
pframe.d+(((y+1)*ourview.vresolu)>>pdepth), |
419 |
> |
r.r = hresolu; r.u = vresolu; |
420 |
> |
p = findrect(pframe.l+((x*hresolu)>>pdepth), |
421 |
> |
pframe.d+(((y+1)*vresolu)>>pdepth), |
422 |
|
&ptrunk, &r, pdepth); |
423 |
|
/* |
424 |
|
* Test super-pixel in next row. |
437 |
|
} |
438 |
|
} |
439 |
|
escape: |
440 |
< |
free((char *)rl); |
441 |
< |
free((char *)pl); |
440 |
> |
free((void *)rl); |
441 |
> |
free((void *)pl); |
442 |
|
} |
443 |
|
|
444 |
|
|
445 |
|
int |
446 |
|
refine(p, xmin, ymin, xmax, ymax, pd) /* refine a node */ |
447 |
< |
register PNODE *p; |
447 |
> |
register PNODE *p; |
448 |
|
int xmin, ymin, xmax, ymax; |
449 |
|
int pd; |
450 |
|
{ |
465 |
|
if (p->kid == NULL) { /* subdivide */ |
466 |
|
|
467 |
|
if ((p->kid = newptree()) == NULL) |
468 |
< |
error(SYSTEM, "out of memory in refine"); |
468 |
> |
return(0); |
469 |
|
/* |
470 |
|
* The following paint order can leave a black pixel |
471 |
< |
* when redraw() is called in (*dev->paintr)(). |
471 |
> |
* if redraw() is called in (*dev->paintr)(). |
472 |
|
*/ |
473 |
|
if (p->x >= mx && p->y >= my) |
474 |
|
pcopy(p, p->kid+UR); |