1 |
greg |
1.24 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
greg |
1.1 |
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* rpict.c - routines and variables for picture generation. |
9 |
|
|
* |
10 |
|
|
* 8/14/85 |
11 |
|
|
*/ |
12 |
|
|
|
13 |
|
|
#include "ray.h" |
14 |
|
|
|
15 |
|
|
#ifdef BSD |
16 |
|
|
#include <sys/time.h> |
17 |
|
|
#include <sys/resource.h> |
18 |
greg |
1.14 |
#else |
19 |
|
|
#include <signal.h> |
20 |
greg |
1.1 |
#endif |
21 |
greg |
1.18 |
#include <fcntl.h> |
22 |
greg |
1.1 |
|
23 |
|
|
#include "view.h" |
24 |
|
|
|
25 |
|
|
#include "random.h" |
26 |
|
|
|
27 |
greg |
1.25 |
int dimlist[MAXDIM]; /* sampling dimensions */ |
28 |
|
|
int ndims = 0; /* number of sampling dimensions */ |
29 |
|
|
int samplendx; /* sample index number */ |
30 |
|
|
|
31 |
greg |
1.12 |
VIEW ourview = STDVIEW; /* view parameters */ |
32 |
|
|
int hresolu = 512; /* horizontal resolution */ |
33 |
|
|
int vresolu = 512; /* vertical resolution */ |
34 |
greg |
1.13 |
double pixaspect = 1.0; /* pixel aspect ratio */ |
35 |
greg |
1.1 |
|
36 |
|
|
int psample = 4; /* pixel sample size */ |
37 |
|
|
double maxdiff = .05; /* max. difference for interpolation */ |
38 |
greg |
1.3 |
double dstrpix = 0.67; /* square pixel distribution */ |
39 |
greg |
1.1 |
|
40 |
|
|
double dstrsrc = 0.0; /* square source distribution */ |
41 |
greg |
1.4 |
double shadthresh = .05; /* shadow threshold */ |
42 |
greg |
1.5 |
double shadcert = .5; /* shadow certainty */ |
43 |
greg |
1.27 |
int directrelay = 0; /* number of source relays */ |
44 |
greg |
1.29 |
int vspretest = 512; /* virtual source pretest density */ |
45 |
greg |
1.1 |
|
46 |
|
|
int maxdepth = 6; /* maximum recursion depth */ |
47 |
|
|
double minweight = 5e-3; /* minimum ray weight */ |
48 |
|
|
|
49 |
|
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
50 |
|
|
double ambacc = 0.2; /* ambient accuracy */ |
51 |
greg |
1.6 |
int ambres = 32; /* ambient resolution */ |
52 |
greg |
1.1 |
int ambdiv = 128; /* ambient divisions */ |
53 |
|
|
int ambssamp = 0; /* ambient super-samples */ |
54 |
|
|
int ambounce = 0; /* ambient bounces */ |
55 |
|
|
char *amblist[128]; /* ambient include/exclude list */ |
56 |
|
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
57 |
|
|
|
58 |
|
|
int ralrm = 0; /* seconds between reports */ |
59 |
|
|
|
60 |
|
|
double pctdone = 0.0; /* percentage done */ |
61 |
|
|
|
62 |
greg |
1.24 |
long tlastrept = 0L; /* time at last report */ |
63 |
|
|
|
64 |
|
|
extern long time(); |
65 |
|
|
extern long tstart; /* starting time */ |
66 |
|
|
|
67 |
greg |
1.1 |
extern long nrays; /* number of rays traced */ |
68 |
|
|
|
69 |
greg |
1.26 |
#define MAXDIV 15 /* maximum sample size */ |
70 |
greg |
1.1 |
|
71 |
|
|
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
72 |
|
|
|
73 |
greg |
1.11 |
double pixvalue(); |
74 |
greg |
1.1 |
|
75 |
greg |
1.11 |
|
76 |
greg |
1.1 |
quit(code) /* quit program */ |
77 |
|
|
int code; |
78 |
|
|
{ |
79 |
|
|
if (code || ralrm > 0) /* report status */ |
80 |
|
|
report(); |
81 |
|
|
|
82 |
|
|
exit(code); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
greg |
1.24 |
#ifdef BSD |
87 |
greg |
1.1 |
report() /* report progress */ |
88 |
|
|
{ |
89 |
|
|
struct rusage rubuf; |
90 |
|
|
double t; |
91 |
|
|
|
92 |
|
|
getrusage(RUSAGE_SELF, &rubuf); |
93 |
|
|
t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
94 |
|
|
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
95 |
|
|
getrusage(RUSAGE_CHILDREN, &rubuf); |
96 |
|
|
t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
97 |
|
|
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
98 |
|
|
|
99 |
|
|
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", |
100 |
|
|
nrays, pctdone, t/3600.0); |
101 |
greg |
1.24 |
eputs(errmsg); |
102 |
|
|
tlastrept = time((long *)0); |
103 |
|
|
} |
104 |
greg |
1.1 |
#else |
105 |
greg |
1.24 |
report() /* report progress */ |
106 |
|
|
{ |
107 |
greg |
1.14 |
signal(SIGALRM, report); |
108 |
greg |
1.24 |
tlastrept = time((long *)0); |
109 |
|
|
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n", |
110 |
|
|
nrays, pctdone, (tlastrept-tstart)/3600.0); |
111 |
greg |
1.1 |
eputs(errmsg); |
112 |
|
|
} |
113 |
greg |
1.24 |
#endif |
114 |
greg |
1.1 |
|
115 |
|
|
|
116 |
greg |
1.11 |
render(zfile, oldfile) /* render the scene */ |
117 |
|
|
char *zfile, *oldfile; |
118 |
greg |
1.1 |
{ |
119 |
greg |
1.16 |
extern long lseek(); |
120 |
greg |
1.1 |
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
121 |
greg |
1.11 |
float *zbar[MAXDIV+1]; /* z values */ |
122 |
greg |
1.1 |
int ypos; /* current scanline */ |
123 |
greg |
1.15 |
int ystep; /* current y step size */ |
124 |
greg |
1.16 |
int zfd; |
125 |
greg |
1.1 |
COLOR *colptr; |
126 |
greg |
1.11 |
float *zptr; |
127 |
greg |
1.1 |
register int i; |
128 |
greg |
1.10 |
/* check sampling */ |
129 |
|
|
if (psample < 1) |
130 |
greg |
1.1 |
psample = 1; |
131 |
greg |
1.26 |
else if (psample > MAXDIV) { |
132 |
|
|
sprintf(errmsg, "pixel sampling reduced from %d to %d", |
133 |
|
|
psample, MAXDIV); |
134 |
|
|
error(WARNING, errmsg); |
135 |
greg |
1.10 |
psample = MAXDIV; |
136 |
greg |
1.26 |
} |
137 |
greg |
1.9 |
/* allocate scanlines */ |
138 |
greg |
1.1 |
for (i = 0; i <= psample; i++) { |
139 |
greg |
1.12 |
scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); |
140 |
greg |
1.1 |
if (scanbar[i] == NULL) |
141 |
greg |
1.11 |
goto memerr; |
142 |
greg |
1.1 |
} |
143 |
greg |
1.11 |
/* open z file */ |
144 |
|
|
if (zfile != NULL) { |
145 |
greg |
1.16 |
if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { |
146 |
greg |
1.11 |
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
147 |
|
|
error(SYSTEM, errmsg); |
148 |
|
|
} |
149 |
|
|
for (i = 0; i <= psample; i++) { |
150 |
greg |
1.12 |
zbar[i] = (float *)malloc(hresolu*sizeof(float)); |
151 |
greg |
1.11 |
if (zbar[i] == NULL) |
152 |
|
|
goto memerr; |
153 |
|
|
} |
154 |
|
|
} else { |
155 |
greg |
1.16 |
zfd = -1; |
156 |
greg |
1.11 |
for (i = 0; i <= psample; i++) |
157 |
|
|
zbar[i] = NULL; |
158 |
|
|
} |
159 |
greg |
1.1 |
/* write out boundaries */ |
160 |
greg |
1.12 |
fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); |
161 |
greg |
1.10 |
/* recover file and compute first */ |
162 |
greg |
1.11 |
i = salvage(oldfile); |
163 |
greg |
1.23 |
if (zfd != -1 && i > 0 && |
164 |
|
|
lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1) |
165 |
greg |
1.11 |
error(SYSTEM, "z file seek error in render"); |
166 |
greg |
1.24 |
pctdone = 100.0*i/vresolu; |
167 |
|
|
if (ralrm > 0) /* report init stats */ |
168 |
|
|
report(); |
169 |
greg |
1.12 |
ypos = vresolu-1 - i; |
170 |
|
|
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
171 |
greg |
1.15 |
ystep = psample; |
172 |
greg |
1.10 |
/* compute scanlines */ |
173 |
greg |
1.15 |
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
174 |
|
|
/* bottom adjust? */ |
175 |
|
|
if (ypos < 0) { |
176 |
|
|
ystep += ypos; |
177 |
|
|
ypos = 0; |
178 |
|
|
} |
179 |
|
|
colptr = scanbar[ystep]; /* move base to top */ |
180 |
|
|
scanbar[ystep] = scanbar[0]; |
181 |
greg |
1.1 |
scanbar[0] = colptr; |
182 |
greg |
1.15 |
zptr = zbar[ystep]; |
183 |
|
|
zbar[ystep] = zbar[0]; |
184 |
greg |
1.11 |
zbar[0] = zptr; |
185 |
greg |
1.10 |
/* fill base line */ |
186 |
greg |
1.12 |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
187 |
greg |
1.10 |
/* fill bar */ |
188 |
greg |
1.15 |
fillscanbar(scanbar, zbar, hresolu, ypos, ystep); |
189 |
greg |
1.10 |
/* write it out */ |
190 |
greg |
1.15 |
for (i = ystep; i > 0; i--) { |
191 |
greg |
1.17 |
if (zfd != -1 && write(zfd, (char *)zbar[i], |
192 |
|
|
hresolu*sizeof(float)) |
193 |
greg |
1.16 |
< hresolu*sizeof(float)) |
194 |
greg |
1.1 |
goto writerr; |
195 |
greg |
1.17 |
if (fwritescan(scanbar[i], hresolu, stdout) < 0) |
196 |
greg |
1.11 |
goto writerr; |
197 |
|
|
} |
198 |
greg |
1.1 |
if (fflush(stdout) == EOF) |
199 |
|
|
goto writerr; |
200 |
greg |
1.24 |
/* record progress */ |
201 |
|
|
pctdone = 100.0*(vresolu-1-ypos)/vresolu; |
202 |
|
|
if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) |
203 |
|
|
report(); |
204 |
greg |
1.1 |
} |
205 |
greg |
1.11 |
/* clean up */ |
206 |
greg |
1.16 |
if (zfd != -1) { |
207 |
greg |
1.17 |
if (write(zfd, (char *)zbar[0], hresolu*sizeof(float)) |
208 |
greg |
1.16 |
< hresolu*sizeof(float)) |
209 |
greg |
1.11 |
goto writerr; |
210 |
greg |
1.20 |
if (close(zfd) == -1) |
211 |
|
|
goto writerr; |
212 |
greg |
1.11 |
for (i = 0; i <= psample; i++) |
213 |
|
|
free((char *)zbar[i]); |
214 |
|
|
} |
215 |
greg |
1.15 |
fwritescan(scanbar[0], hresolu, stdout); |
216 |
greg |
1.10 |
if (fflush(stdout) == EOF) |
217 |
|
|
goto writerr; |
218 |
greg |
1.1 |
for (i = 0; i <= psample; i++) |
219 |
|
|
free((char *)scanbar[i]); |
220 |
greg |
1.11 |
pctdone = 100.0; |
221 |
greg |
1.1 |
return; |
222 |
|
|
writerr: |
223 |
|
|
error(SYSTEM, "write error in render"); |
224 |
greg |
1.11 |
memerr: |
225 |
|
|
error(SYSTEM, "out of memory in render"); |
226 |
greg |
1.1 |
} |
227 |
|
|
|
228 |
|
|
|
229 |
greg |
1.11 |
fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */ |
230 |
greg |
1.1 |
register COLOR *scanline; |
231 |
greg |
1.11 |
register float *zline; |
232 |
greg |
1.9 |
int xres, y, xstep; |
233 |
greg |
1.1 |
{ |
234 |
|
|
int b = xstep; |
235 |
greg |
1.11 |
double z; |
236 |
greg |
1.1 |
register int i; |
237 |
|
|
|
238 |
greg |
1.11 |
z = pixvalue(scanline[0], 0, y); |
239 |
|
|
if (zline) zline[0] = z; |
240 |
greg |
1.1 |
|
241 |
greg |
1.15 |
for (i = xstep; i < xres-1+xstep; i += xstep) { |
242 |
|
|
if (i >= xres) { |
243 |
|
|
xstep += xres-1-i; |
244 |
|
|
i = xres-1; |
245 |
|
|
} |
246 |
greg |
1.11 |
z = pixvalue(scanline[i], i, y); |
247 |
|
|
if (zline) zline[i] = z; |
248 |
greg |
1.1 |
|
249 |
greg |
1.11 |
b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, |
250 |
|
|
i-xstep, y, xstep, 0, b/2); |
251 |
greg |
1.1 |
} |
252 |
|
|
} |
253 |
|
|
|
254 |
|
|
|
255 |
greg |
1.11 |
fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ |
256 |
greg |
1.1 |
register COLOR *scanbar[]; |
257 |
greg |
1.11 |
register float *zbar[]; |
258 |
greg |
1.9 |
int xres, y, ysize; |
259 |
greg |
1.1 |
{ |
260 |
|
|
COLOR vline[MAXDIV+1]; |
261 |
greg |
1.11 |
float zline[MAXDIV+1]; |
262 |
greg |
1.1 |
int b = ysize; |
263 |
|
|
register int i, j; |
264 |
|
|
|
265 |
greg |
1.8 |
for (i = 0; i < xres; i++) { |
266 |
greg |
1.1 |
|
267 |
|
|
copycolor(vline[0], scanbar[0][i]); |
268 |
|
|
copycolor(vline[ysize], scanbar[ysize][i]); |
269 |
greg |
1.11 |
if (zbar[0]) { |
270 |
|
|
zline[0] = zbar[0][i]; |
271 |
|
|
zline[ysize] = zbar[ysize][i]; |
272 |
|
|
} |
273 |
greg |
1.1 |
|
274 |
greg |
1.11 |
b = fillsample(vline, zbar[0] ? zline : NULL, |
275 |
|
|
i, y, 0, ysize, b/2); |
276 |
greg |
1.1 |
|
277 |
|
|
for (j = 1; j < ysize; j++) |
278 |
|
|
copycolor(scanbar[j][i], vline[j]); |
279 |
greg |
1.11 |
if (zbar[0]) |
280 |
|
|
for (j = 1; j < ysize; j++) |
281 |
|
|
zbar[j][i] = zline[j]; |
282 |
greg |
1.1 |
} |
283 |
|
|
} |
284 |
|
|
|
285 |
|
|
|
286 |
|
|
int |
287 |
greg |
1.11 |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
288 |
greg |
1.1 |
register COLOR *colline; |
289 |
greg |
1.11 |
register float *zline; |
290 |
greg |
1.1 |
int x, y; |
291 |
|
|
int xlen, ylen; |
292 |
|
|
int b; |
293 |
|
|
{ |
294 |
greg |
1.11 |
extern double fabs(); |
295 |
greg |
1.1 |
double ratio; |
296 |
greg |
1.11 |
double z; |
297 |
greg |
1.1 |
COLOR ctmp; |
298 |
|
|
int ncut; |
299 |
|
|
register int len; |
300 |
|
|
|
301 |
|
|
if (xlen > 0) /* x or y length is zero */ |
302 |
|
|
len = xlen; |
303 |
|
|
else |
304 |
|
|
len = ylen; |
305 |
|
|
|
306 |
|
|
if (len <= 1) /* limit recursion */ |
307 |
|
|
return(0); |
308 |
|
|
|
309 |
greg |
1.11 |
if (b > 0 |
310 |
|
|
|| (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
311 |
|
|
|| bigdiff(colline[0], colline[len], maxdiff)) { |
312 |
greg |
1.1 |
|
313 |
greg |
1.11 |
z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
314 |
|
|
if (zline) zline[len>>1] = z; |
315 |
greg |
1.1 |
ncut = 1; |
316 |
|
|
|
317 |
|
|
} else { /* interpolate */ |
318 |
|
|
|
319 |
|
|
copycolor(colline[len>>1], colline[len]); |
320 |
|
|
ratio = (double)(len>>1) / len; |
321 |
|
|
scalecolor(colline[len>>1], ratio); |
322 |
greg |
1.11 |
if (zline) zline[len>>1] = zline[len] * ratio; |
323 |
|
|
ratio = 1.0 - ratio; |
324 |
greg |
1.1 |
copycolor(ctmp, colline[0]); |
325 |
|
|
scalecolor(ctmp, ratio); |
326 |
|
|
addcolor(colline[len>>1], ctmp); |
327 |
greg |
1.11 |
if (zline) zline[len>>1] += zline[0] * ratio; |
328 |
greg |
1.1 |
ncut = 0; |
329 |
|
|
} |
330 |
|
|
/* recurse */ |
331 |
greg |
1.11 |
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
332 |
greg |
1.1 |
|
333 |
greg |
1.11 |
ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, |
334 |
|
|
x+(xlen>>1), y+(ylen>>1), |
335 |
|
|
xlen-(xlen>>1), ylen-(ylen>>1), b/2); |
336 |
greg |
1.1 |
|
337 |
|
|
return(ncut); |
338 |
|
|
} |
339 |
|
|
|
340 |
|
|
|
341 |
greg |
1.11 |
double |
342 |
greg |
1.1 |
pixvalue(col, x, y) /* compute pixel value */ |
343 |
|
|
COLOR col; /* returned color */ |
344 |
|
|
int x, y; /* pixel position */ |
345 |
|
|
{ |
346 |
greg |
1.25 |
static RAY thisray; |
347 |
greg |
1.1 |
|
348 |
greg |
1.22 |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
349 |
|
|
(x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) { |
350 |
|
|
setcolor(col, 0.0, 0.0, 0.0); |
351 |
|
|
return(0.0); |
352 |
|
|
} |
353 |
greg |
1.1 |
|
354 |
|
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
355 |
greg |
1.25 |
|
356 |
|
|
samplendx = 3*y + x; /* set pixel index */ |
357 |
|
|
|
358 |
greg |
1.1 |
rayvalue(&thisray); /* trace ray */ |
359 |
|
|
|
360 |
|
|
copycolor(col, thisray.rcol); /* return color */ |
361 |
greg |
1.11 |
|
362 |
greg |
1.20 |
return(thisray.rt); /* return distance */ |
363 |
greg |
1.1 |
} |
364 |
|
|
|
365 |
|
|
|
366 |
|
|
int |
367 |
|
|
salvage(oldfile) /* salvage scanlines from killed program */ |
368 |
|
|
char *oldfile; |
369 |
|
|
{ |
370 |
|
|
COLR *scanline; |
371 |
|
|
FILE *fp; |
372 |
|
|
int x, y; |
373 |
|
|
|
374 |
|
|
if (oldfile == NULL) |
375 |
|
|
return(0); |
376 |
greg |
1.9 |
|
377 |
|
|
if ((fp = fopen(oldfile, "r")) == NULL) { |
378 |
greg |
1.1 |
sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); |
379 |
|
|
error(WARNING, errmsg); |
380 |
|
|
return(0); |
381 |
|
|
} |
382 |
|
|
/* discard header */ |
383 |
|
|
getheader(fp, NULL); |
384 |
|
|
/* get picture size */ |
385 |
greg |
1.7 |
if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) { |
386 |
greg |
1.2 |
sprintf(errmsg, "bad recover file \"%s\"", oldfile); |
387 |
|
|
error(WARNING, errmsg); |
388 |
greg |
1.1 |
fclose(fp); |
389 |
|
|
return(0); |
390 |
|
|
} |
391 |
|
|
|
392 |
greg |
1.12 |
if (x != hresolu || y != vresolu) { |
393 |
greg |
1.1 |
sprintf(errmsg, "resolution mismatch in recover file \"%s\"", |
394 |
|
|
oldfile); |
395 |
|
|
error(USER, errmsg); |
396 |
|
|
} |
397 |
|
|
|
398 |
greg |
1.12 |
scanline = (COLR *)malloc(hresolu*sizeof(COLR)); |
399 |
greg |
1.1 |
if (scanline == NULL) |
400 |
|
|
error(SYSTEM, "out of memory in salvage"); |
401 |
greg |
1.12 |
for (y = 0; y < vresolu; y++) { |
402 |
|
|
if (freadcolrs(scanline, hresolu, fp) < 0) |
403 |
greg |
1.1 |
break; |
404 |
greg |
1.12 |
if (fwritecolrs(scanline, hresolu, stdout) < 0) |
405 |
greg |
1.1 |
goto writerr; |
406 |
|
|
} |
407 |
|
|
if (fflush(stdout) == EOF) |
408 |
|
|
goto writerr; |
409 |
|
|
free((char *)scanline); |
410 |
|
|
fclose(fp); |
411 |
|
|
unlink(oldfile); |
412 |
|
|
return(y); |
413 |
|
|
writerr: |
414 |
|
|
error(SYSTEM, "write error in salvage"); |
415 |
|
|
} |