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