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 */ |
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 |
|
|
45 |
|
int maxdepth = 6; /* maximum recursion depth */ |
46 |
|
double minweight = 5e-3; /* minimum ray weight */ |
58 |
|
|
59 |
|
double pctdone = 0.0; /* percentage done */ |
60 |
|
|
61 |
+ |
long tlastrept = 0L; /* time at last report */ |
62 |
+ |
|
63 |
+ |
extern long time(); |
64 |
+ |
extern long tstart; /* starting time */ |
65 |
+ |
|
66 |
|
extern long nrays; /* number of rays traced */ |
67 |
|
|
68 |
< |
#define MAXDIV 32 /* maximum sample size */ |
68 |
> |
#define MAXDIV 15 /* maximum sample size */ |
69 |
|
|
70 |
|
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
71 |
|
|
72 |
+ |
double pixvalue(); |
73 |
|
|
74 |
+ |
|
75 |
|
quit(code) /* quit program */ |
76 |
|
int code; |
77 |
|
{ |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
+ |
#ifdef BSD |
86 |
|
report() /* report progress */ |
87 |
|
{ |
69 |
– |
#ifdef BSD |
88 |
|
struct rusage rubuf; |
89 |
|
double t; |
90 |
|
|
97 |
|
|
98 |
|
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", |
99 |
|
nrays, pctdone, t/3600.0); |
100 |
+ |
eputs(errmsg); |
101 |
+ |
tlastrept = time((long *)0); |
102 |
+ |
} |
103 |
|
#else |
104 |
< |
sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone); |
105 |
< |
#endif |
104 |
> |
report() /* report progress */ |
105 |
> |
{ |
106 |
> |
signal(SIGALRM, report); |
107 |
> |
tlastrept = time((long *)0); |
108 |
> |
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n", |
109 |
> |
nrays, pctdone, (tlastrept-tstart)/3600.0); |
110 |
|
eputs(errmsg); |
86 |
– |
|
87 |
– |
if (ralrm > 0) |
88 |
– |
alarm(ralrm); |
111 |
|
} |
112 |
+ |
#endif |
113 |
|
|
114 |
|
|
115 |
< |
render(oldfile) /* render the scene */ |
116 |
< |
char *oldfile; |
115 |
> |
render(zfile, oldfile) /* render the scene */ |
116 |
> |
char *zfile, *oldfile; |
117 |
|
{ |
118 |
+ |
extern long lseek(); |
119 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
120 |
+ |
float *zbar[MAXDIV+1]; /* z values */ |
121 |
|
int ypos; /* current scanline */ |
122 |
< |
int xres, yres; /* rendered x and y resolution */ |
122 |
> |
int ystep; /* current y step size */ |
123 |
> |
int zfd; |
124 |
|
COLOR *colptr; |
125 |
+ |
float *zptr; |
126 |
|
register int i; |
127 |
< |
/* set rendered resolution */ |
128 |
< |
xres = ourview.hresolu; |
102 |
< |
yres = ourview.vresolu; |
103 |
< |
/* adjust for sampling */ |
104 |
< |
if (psample <= 1) |
127 |
> |
/* check sampling */ |
128 |
> |
if (psample < 1) |
129 |
|
psample = 1; |
130 |
< |
else { |
131 |
< |
if (psample > MAXDIV) |
132 |
< |
psample = MAXDIV; |
133 |
< |
/* rendered resolution may be larger */ |
134 |
< |
xres += psample-1 - ((ourview.hresolu-2)%psample); |
111 |
< |
yres += psample-1 - ((ourview.vresolu-2)%psample); |
130 |
> |
else if (psample > MAXDIV) { |
131 |
> |
sprintf(errmsg, "pixel sampling reduced from %d to %d", |
132 |
> |
psample, MAXDIV); |
133 |
> |
error(WARNING, errmsg); |
134 |
> |
psample = MAXDIV; |
135 |
|
} |
136 |
|
/* allocate scanlines */ |
137 |
|
for (i = 0; i <= psample; i++) { |
138 |
< |
scanbar[i] = (COLOR *)malloc(xres*sizeof(COLOR)); |
138 |
> |
scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); |
139 |
|
if (scanbar[i] == NULL) |
140 |
< |
error(SYSTEM, "out of memory in render"); |
140 |
> |
goto memerr; |
141 |
|
} |
142 |
+ |
/* open z file */ |
143 |
+ |
if (zfile != NULL) { |
144 |
+ |
if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { |
145 |
+ |
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
146 |
+ |
error(SYSTEM, errmsg); |
147 |
+ |
} |
148 |
+ |
for (i = 0; i <= psample; i++) { |
149 |
+ |
zbar[i] = (float *)malloc(hresolu*sizeof(float)); |
150 |
+ |
if (zbar[i] == NULL) |
151 |
+ |
goto memerr; |
152 |
+ |
} |
153 |
+ |
} else { |
154 |
+ |
zfd = -1; |
155 |
+ |
for (i = 0; i <= psample; i++) |
156 |
+ |
zbar[i] = NULL; |
157 |
+ |
} |
158 |
|
/* write out boundaries */ |
159 |
< |
fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout); |
160 |
< |
|
161 |
< |
ypos = ourview.vresolu-1 - salvage(oldfile); /* find top line */ |
162 |
< |
fillscanline(scanbar[0], xres, ypos, psample); /* top scan */ |
163 |
< |
|
164 |
< |
for (ypos -= psample; ypos > -psample; ypos -= psample) { |
165 |
< |
|
166 |
< |
pctdone = 100.0*(ourview.vresolu-ypos+psample)/ourview.vresolu; |
167 |
< |
|
168 |
< |
colptr = scanbar[psample]; /* move base to top */ |
169 |
< |
scanbar[psample] = scanbar[0]; |
159 |
> |
fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); |
160 |
> |
/* recover file and compute first */ |
161 |
> |
i = salvage(oldfile); |
162 |
> |
if (zfd != -1 && i > 0 && |
163 |
> |
lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1) |
164 |
> |
error(SYSTEM, "z file seek error in render"); |
165 |
> |
pctdone = 100.0*i/vresolu; |
166 |
> |
if (ralrm > 0) /* report init stats */ |
167 |
> |
report(); |
168 |
> |
ypos = vresolu-1 - i; |
169 |
> |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
170 |
> |
ystep = psample; |
171 |
> |
/* compute scanlines */ |
172 |
> |
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
173 |
> |
/* bottom adjust? */ |
174 |
> |
if (ypos < 0) { |
175 |
> |
ystep += ypos; |
176 |
> |
ypos = 0; |
177 |
> |
} |
178 |
> |
colptr = scanbar[ystep]; /* move base to top */ |
179 |
> |
scanbar[ystep] = scanbar[0]; |
180 |
|
scanbar[0] = colptr; |
181 |
< |
|
182 |
< |
fillscanline(scanbar[0], xres, ypos, psample); /* fill base */ |
183 |
< |
|
184 |
< |
fillscanbar(scanbar, xres, ypos, psample); /* fill bar */ |
185 |
< |
|
186 |
< |
for (i = psample; (ypos>0) ? i > 0 : ypos+i >= 0; i--) |
187 |
< |
if (fwritescan(scanbar[i], ourview.hresolu, stdout) < 0) |
181 |
> |
zptr = zbar[ystep]; |
182 |
> |
zbar[ystep] = zbar[0]; |
183 |
> |
zbar[0] = zptr; |
184 |
> |
/* fill base line */ |
185 |
> |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
186 |
> |
/* fill bar */ |
187 |
> |
fillscanbar(scanbar, zbar, hresolu, ypos, ystep); |
188 |
> |
/* write it out */ |
189 |
> |
for (i = ystep; i > 0; i--) { |
190 |
> |
if (zfd != -1 && write(zfd, (char *)zbar[i], |
191 |
> |
hresolu*sizeof(float)) |
192 |
> |
< hresolu*sizeof(float)) |
193 |
|
goto writerr; |
194 |
+ |
if (fwritescan(scanbar[i], hresolu, stdout) < 0) |
195 |
+ |
goto writerr; |
196 |
+ |
} |
197 |
|
if (fflush(stdout) == EOF) |
198 |
|
goto writerr; |
199 |
+ |
/* record progress */ |
200 |
+ |
pctdone = 100.0*(vresolu-1-ypos)/vresolu; |
201 |
+ |
if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) |
202 |
+ |
report(); |
203 |
|
} |
204 |
< |
pctdone = 100.0; |
205 |
< |
|
204 |
> |
/* clean up */ |
205 |
> |
if (zfd != -1) { |
206 |
> |
if (write(zfd, (char *)zbar[0], hresolu*sizeof(float)) |
207 |
> |
< hresolu*sizeof(float)) |
208 |
> |
goto writerr; |
209 |
> |
if (close(zfd) == -1) |
210 |
> |
goto writerr; |
211 |
> |
for (i = 0; i <= psample; i++) |
212 |
> |
free((char *)zbar[i]); |
213 |
> |
} |
214 |
> |
fwritescan(scanbar[0], hresolu, stdout); |
215 |
> |
if (fflush(stdout) == EOF) |
216 |
> |
goto writerr; |
217 |
|
for (i = 0; i <= psample; i++) |
218 |
|
free((char *)scanbar[i]); |
219 |
+ |
pctdone = 100.0; |
220 |
|
return; |
221 |
|
writerr: |
222 |
|
error(SYSTEM, "write error in render"); |
223 |
+ |
memerr: |
224 |
+ |
error(SYSTEM, "out of memory in render"); |
225 |
|
} |
226 |
|
|
227 |
|
|
228 |
< |
fillscanline(scanline, xres, y, xstep) /* fill scan line at y */ |
228 |
> |
fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */ |
229 |
|
register COLOR *scanline; |
230 |
+ |
register float *zline; |
231 |
|
int xres, y, xstep; |
232 |
|
{ |
233 |
|
int b = xstep; |
234 |
+ |
double z; |
235 |
|
register int i; |
236 |
|
|
237 |
< |
pixvalue(scanline[0], 0, y); |
237 |
> |
z = pixvalue(scanline[0], 0, y); |
238 |
> |
if (zline) zline[0] = z; |
239 |
|
|
240 |
< |
for (i = xstep; i < xres; i += xstep) { |
241 |
< |
|
242 |
< |
pixvalue(scanline[i], i, y); |
240 |
> |
for (i = xstep; i < xres-1+xstep; i += xstep) { |
241 |
> |
if (i >= xres) { |
242 |
> |
xstep += xres-1-i; |
243 |
> |
i = xres-1; |
244 |
> |
} |
245 |
> |
z = pixvalue(scanline[i], i, y); |
246 |
> |
if (zline) zline[i] = z; |
247 |
|
|
248 |
< |
b = fillsample(scanline+i-xstep, i-xstep, y, xstep, 0, b/2); |
248 |
> |
b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, |
249 |
> |
i-xstep, y, xstep, 0, b/2); |
250 |
|
} |
251 |
|
} |
252 |
|
|
253 |
|
|
254 |
< |
fillscanbar(scanbar, xres, y, ysize) /* fill interior */ |
254 |
> |
fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ |
255 |
|
register COLOR *scanbar[]; |
256 |
+ |
register float *zbar[]; |
257 |
|
int xres, y, ysize; |
258 |
|
{ |
259 |
|
COLOR vline[MAXDIV+1]; |
260 |
+ |
float zline[MAXDIV+1]; |
261 |
|
int b = ysize; |
262 |
|
register int i, j; |
263 |
|
|
265 |
|
|
266 |
|
copycolor(vline[0], scanbar[0][i]); |
267 |
|
copycolor(vline[ysize], scanbar[ysize][i]); |
268 |
+ |
if (zbar[0]) { |
269 |
+ |
zline[0] = zbar[0][i]; |
270 |
+ |
zline[ysize] = zbar[ysize][i]; |
271 |
+ |
} |
272 |
|
|
273 |
< |
b = fillsample(vline, i, y, 0, ysize, b/2); |
273 |
> |
b = fillsample(vline, zbar[0] ? zline : NULL, |
274 |
> |
i, y, 0, ysize, b/2); |
275 |
|
|
276 |
|
for (j = 1; j < ysize; j++) |
277 |
|
copycolor(scanbar[j][i], vline[j]); |
278 |
+ |
if (zbar[0]) |
279 |
+ |
for (j = 1; j < ysize; j++) |
280 |
+ |
zbar[j][i] = zline[j]; |
281 |
|
} |
282 |
|
} |
283 |
|
|
284 |
|
|
285 |
|
int |
286 |
< |
fillsample(colline, x, y, xlen, ylen, b) /* fill interior points */ |
286 |
> |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
287 |
|
register COLOR *colline; |
288 |
+ |
register float *zline; |
289 |
|
int x, y; |
290 |
|
int xlen, ylen; |
291 |
|
int b; |
292 |
|
{ |
293 |
+ |
extern double fabs(); |
294 |
|
double ratio; |
295 |
+ |
double z; |
296 |
|
COLOR ctmp; |
297 |
|
int ncut; |
298 |
|
register int len; |
305 |
|
if (len <= 1) /* limit recursion */ |
306 |
|
return(0); |
307 |
|
|
308 |
< |
if (b > 0 || bigdiff(colline[0], colline[len], maxdiff)) { |
308 |
> |
if (b > 0 |
309 |
> |
|| (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
310 |
> |
|| bigdiff(colline[0], colline[len], maxdiff)) { |
311 |
|
|
312 |
< |
pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
312 |
> |
z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
313 |
> |
if (zline) zline[len>>1] = z; |
314 |
|
ncut = 1; |
315 |
|
|
316 |
|
} else { /* interpolate */ |
318 |
|
copycolor(colline[len>>1], colline[len]); |
319 |
|
ratio = (double)(len>>1) / len; |
320 |
|
scalecolor(colline[len>>1], ratio); |
321 |
< |
copycolor(ctmp, colline[0]); |
321 |
> |
if (zline) zline[len>>1] = zline[len] * ratio; |
322 |
|
ratio = 1.0 - ratio; |
323 |
+ |
copycolor(ctmp, colline[0]); |
324 |
|
scalecolor(ctmp, ratio); |
325 |
|
addcolor(colline[len>>1], ctmp); |
326 |
+ |
if (zline) zline[len>>1] += zline[0] * ratio; |
327 |
|
ncut = 0; |
328 |
|
} |
329 |
|
/* recurse */ |
330 |
< |
ncut += fillsample(colline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
330 |
> |
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
331 |
|
|
332 |
< |
ncut += fillsample(colline + (len>>1), x + (xlen>>1), y + (ylen>>1), |
333 |
< |
xlen - (xlen>>1), ylen - (ylen>>1), b/2); |
332 |
> |
ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, |
333 |
> |
x+(xlen>>1), y+(ylen>>1), |
334 |
> |
xlen-(xlen>>1), ylen-(ylen>>1), b/2); |
335 |
|
|
336 |
|
return(ncut); |
337 |
|
} |
338 |
|
|
339 |
|
|
340 |
+ |
double |
341 |
|
pixvalue(col, x, y) /* compute pixel value */ |
342 |
|
COLOR col; /* returned color */ |
343 |
|
int x, y; /* pixel position */ |
344 |
|
{ |
345 |
< |
static RAY thisray; /* our ray for this pixel */ |
345 |
> |
static RAY thisray; |
346 |
|
|
347 |
< |
rayview(thisray.rorg, thisray.rdir, &ourview, |
348 |
< |
x + pixjitter(), y + pixjitter()); |
347 |
> |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
348 |
> |
(x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) { |
349 |
> |
setcolor(col, 0.0, 0.0, 0.0); |
350 |
> |
return(0.0); |
351 |
> |
} |
352 |
|
|
353 |
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
354 |
< |
|
354 |
> |
|
355 |
> |
samplendx = 3*y + x; /* set pixel index */ |
356 |
> |
|
357 |
|
rayvalue(&thisray); /* trace ray */ |
358 |
|
|
359 |
|
copycolor(col, thisray.rcol); /* return color */ |
360 |
+ |
|
361 |
+ |
return(thisray.rt); /* return distance */ |
362 |
|
} |
363 |
|
|
364 |
|
|
388 |
|
return(0); |
389 |
|
} |
390 |
|
|
391 |
< |
if (x != ourview.hresolu || y != ourview.vresolu) { |
391 |
> |
if (x != hresolu || y != vresolu) { |
392 |
|
sprintf(errmsg, "resolution mismatch in recover file \"%s\"", |
393 |
|
oldfile); |
394 |
|
error(USER, errmsg); |
395 |
|
} |
396 |
|
|
397 |
< |
scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR)); |
397 |
> |
scanline = (COLR *)malloc(hresolu*sizeof(COLR)); |
398 |
|
if (scanline == NULL) |
399 |
|
error(SYSTEM, "out of memory in salvage"); |
400 |
< |
for (y = 0; y < ourview.vresolu; y++) { |
401 |
< |
if (freadcolrs(scanline, ourview.hresolu, fp) < 0) |
400 |
> |
for (y = 0; y < vresolu; y++) { |
401 |
> |
if (freadcolrs(scanline, hresolu, fp) < 0) |
402 |
|
break; |
403 |
< |
if (fwritecolrs(scanline, ourview.hresolu, stdout) < 0) |
403 |
> |
if (fwritecolrs(scanline, hresolu, stdout) < 0) |
404 |
|
goto writerr; |
405 |
|
} |
406 |
|
if (fflush(stdout) == EOF) |