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