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