15 |
|
#ifdef BSD |
16 |
|
#include <sys/time.h> |
17 |
|
#include <sys/resource.h> |
18 |
+ |
#else |
19 |
+ |
#include <signal.h> |
20 |
|
#endif |
21 |
|
|
22 |
|
#include "view.h" |
23 |
|
|
24 |
|
#include "random.h" |
25 |
|
|
26 |
< |
VIEW ourview = STDVIEW(512); /* view parameters */ |
26 |
> |
VIEW ourview = STDVIEW; /* view parameters */ |
27 |
> |
int hresolu = 512; /* horizontal resolution */ |
28 |
> |
int vresolu = 512; /* vertical resolution */ |
29 |
> |
double pixaspect = 1.0; /* pixel aspect ratio */ |
30 |
|
|
31 |
|
int psample = 4; /* pixel sample size */ |
32 |
|
double maxdiff = .05; /* max. difference for interpolation */ |
34 |
|
|
35 |
|
double dstrsrc = 0.0; /* square source distribution */ |
36 |
|
double shadthresh = .05; /* shadow threshold */ |
37 |
+ |
double shadcert = .5; /* shadow certainty */ |
38 |
|
|
39 |
|
int maxdepth = 6; /* maximum recursion depth */ |
40 |
|
double minweight = 5e-3; /* minimum ray weight */ |
41 |
|
|
42 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
43 |
|
double ambacc = 0.2; /* ambient accuracy */ |
44 |
< |
int ambres = 128; /* ambient resolution */ |
44 |
> |
int ambres = 32; /* ambient resolution */ |
45 |
|
int ambdiv = 128; /* ambient divisions */ |
46 |
|
int ambssamp = 0; /* ambient super-samples */ |
47 |
|
int ambounce = 0; /* ambient bounces */ |
58 |
|
|
59 |
|
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
60 |
|
|
61 |
+ |
double pixvalue(); |
62 |
|
|
63 |
+ |
|
64 |
|
quit(code) /* quit program */ |
65 |
|
int code; |
66 |
|
{ |
87 |
|
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", |
88 |
|
nrays, pctdone, t/3600.0); |
89 |
|
#else |
90 |
+ |
signal(SIGALRM, report); |
91 |
|
sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone); |
92 |
|
#endif |
93 |
|
eputs(errmsg); |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
< |
render(oldfile) /* render the scene */ |
101 |
< |
char *oldfile; |
100 |
> |
render(zfile, oldfile) /* render the scene */ |
101 |
> |
char *zfile, *oldfile; |
102 |
|
{ |
103 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
104 |
+ |
float *zbar[MAXDIV+1]; /* z values */ |
105 |
|
int ypos; /* current scanline */ |
106 |
+ |
int ystep; /* current y step size */ |
107 |
+ |
FILE *zfp; |
108 |
|
COLOR *colptr; |
109 |
+ |
float *zptr; |
110 |
|
register int i; |
111 |
< |
|
111 |
> |
/* check sampling */ |
112 |
|
if (psample < 1) |
113 |
|
psample = 1; |
114 |
|
else if (psample > MAXDIV) |
115 |
|
psample = MAXDIV; |
116 |
< |
|
104 |
< |
ourview.hresolu -= ourview.hresolu % psample; |
105 |
< |
ourview.vresolu -= ourview.vresolu % psample; |
106 |
< |
|
116 |
> |
/* allocate scanlines */ |
117 |
|
for (i = 0; i <= psample; i++) { |
118 |
< |
scanbar[i] = (COLOR *)malloc((ourview.hresolu+1)*sizeof(COLOR)); |
118 |
> |
scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); |
119 |
|
if (scanbar[i] == NULL) |
120 |
< |
error(SYSTEM, "out of memory in render"); |
120 |
> |
goto memerr; |
121 |
|
} |
122 |
< |
|
122 |
> |
/* open z file */ |
123 |
> |
if (zfile != NULL) { |
124 |
> |
if ((zfp = fopen(zfile, "a+")) == NULL) { |
125 |
> |
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
126 |
> |
error(SYSTEM, errmsg); |
127 |
> |
} |
128 |
> |
for (i = 0; i <= psample; i++) { |
129 |
> |
zbar[i] = (float *)malloc(hresolu*sizeof(float)); |
130 |
> |
if (zbar[i] == NULL) |
131 |
> |
goto memerr; |
132 |
> |
} |
133 |
> |
} else { |
134 |
> |
zfp = NULL; |
135 |
> |
for (i = 0; i <= psample; i++) |
136 |
> |
zbar[i] = NULL; |
137 |
> |
} |
138 |
|
/* write out boundaries */ |
139 |
< |
printf("-Y %d +X %d\n", ourview.vresolu, ourview.hresolu); |
140 |
< |
|
141 |
< |
ypos = ourview.vresolu - salvage(oldfile); /* find top line */ |
142 |
< |
fillscanline(scanbar[0], ypos, psample); /* top scan */ |
143 |
< |
|
144 |
< |
for (ypos -= psample; ypos > -psample; ypos -= psample) { |
145 |
< |
|
146 |
< |
colptr = scanbar[psample]; /* get last scanline */ |
147 |
< |
scanbar[psample] = scanbar[0]; |
139 |
> |
fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); |
140 |
> |
/* recover file and compute first */ |
141 |
> |
i = salvage(oldfile); |
142 |
> |
if (zfp != NULL && fseek(zfp, (long)i*hresolu*sizeof(float), 0) == EOF) |
143 |
> |
error(SYSTEM, "z file seek error in render"); |
144 |
> |
ypos = vresolu-1 - i; |
145 |
> |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
146 |
> |
ystep = psample; |
147 |
> |
/* compute scanlines */ |
148 |
> |
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
149 |
> |
/* record progress */ |
150 |
> |
pctdone = 100.0*(vresolu-ypos-ystep)/vresolu; |
151 |
> |
/* bottom adjust? */ |
152 |
> |
if (ypos < 0) { |
153 |
> |
ystep += ypos; |
154 |
> |
ypos = 0; |
155 |
> |
} |
156 |
> |
colptr = scanbar[ystep]; /* move base to top */ |
157 |
> |
scanbar[ystep] = scanbar[0]; |
158 |
|
scanbar[0] = colptr; |
159 |
< |
|
160 |
< |
fillscanline(scanbar[0], ypos, psample); /* base scan */ |
161 |
< |
|
162 |
< |
fillscanbar(scanbar, ypos, psample); |
163 |
< |
|
164 |
< |
for (i = psample-1; i >= 0; i--) |
165 |
< |
if (fwritescan(scanbar[i], ourview.hresolu, stdout) < 0) |
159 |
> |
zptr = zbar[ystep]; |
160 |
> |
zbar[ystep] = zbar[0]; |
161 |
> |
zbar[0] = zptr; |
162 |
> |
/* fill base line */ |
163 |
> |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
164 |
> |
/* fill bar */ |
165 |
> |
fillscanbar(scanbar, zbar, hresolu, ypos, ystep); |
166 |
> |
/* write it out */ |
167 |
> |
for (i = ystep; i > 0; i--) { |
168 |
> |
if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu) |
169 |
|
goto writerr; |
170 |
+ |
if (fwritescan(scanbar[i],hresolu,stdout) < 0) |
171 |
+ |
goto writerr; |
172 |
+ |
} |
173 |
+ |
if (zfp != NULL && fflush(zfp) == EOF) |
174 |
+ |
goto writerr; |
175 |
|
if (fflush(stdout) == EOF) |
176 |
|
goto writerr; |
134 |
– |
pctdone = 100.0*(ourview.vresolu-ypos)/ourview.vresolu; |
177 |
|
} |
178 |
< |
|
178 |
> |
/* clean up */ |
179 |
> |
if (zfp != NULL) { |
180 |
> |
fwrite(zbar[0], sizeof(float), hresolu, zfp); |
181 |
> |
if (fclose(zfp) == EOF) |
182 |
> |
goto writerr; |
183 |
> |
for (i = 0; i <= psample; i++) |
184 |
> |
free((char *)zbar[i]); |
185 |
> |
} |
186 |
> |
fwritescan(scanbar[0], hresolu, stdout); |
187 |
> |
if (fflush(stdout) == EOF) |
188 |
> |
goto writerr; |
189 |
|
for (i = 0; i <= psample; i++) |
190 |
|
free((char *)scanbar[i]); |
191 |
+ |
pctdone = 100.0; |
192 |
|
return; |
193 |
|
writerr: |
194 |
|
error(SYSTEM, "write error in render"); |
195 |
+ |
memerr: |
196 |
+ |
error(SYSTEM, "out of memory in render"); |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
< |
fillscanline(scanline, y, xstep) /* fill scan line at y */ |
200 |
> |
fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */ |
201 |
|
register COLOR *scanline; |
202 |
< |
int y, xstep; |
202 |
> |
register float *zline; |
203 |
> |
int xres, y, xstep; |
204 |
|
{ |
205 |
|
int b = xstep; |
206 |
+ |
double z; |
207 |
|
register int i; |
208 |
|
|
209 |
< |
pixvalue(scanline[0], 0, y); |
209 |
> |
z = pixvalue(scanline[0], 0, y); |
210 |
> |
if (zline) zline[0] = z; |
211 |
|
|
212 |
< |
for (i = xstep; i <= ourview.hresolu; i += xstep) { |
213 |
< |
|
214 |
< |
pixvalue(scanline[i], i, y); |
212 |
> |
for (i = xstep; i < xres-1+xstep; i += xstep) { |
213 |
> |
if (i >= xres) { |
214 |
> |
xstep += xres-1-i; |
215 |
> |
i = xres-1; |
216 |
> |
} |
217 |
> |
z = pixvalue(scanline[i], i, y); |
218 |
> |
if (zline) zline[i] = z; |
219 |
|
|
220 |
< |
b = fillsample(scanline+i-xstep, i-xstep, y, xstep, 0, b/2); |
220 |
> |
b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, |
221 |
> |
i-xstep, y, xstep, 0, b/2); |
222 |
|
} |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
< |
fillscanbar(scanbar, y, ysize) /* fill interior */ |
226 |
> |
fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ |
227 |
|
register COLOR *scanbar[]; |
228 |
< |
int y, ysize; |
228 |
> |
register float *zbar[]; |
229 |
> |
int xres, y, ysize; |
230 |
|
{ |
231 |
|
COLOR vline[MAXDIV+1]; |
232 |
+ |
float zline[MAXDIV+1]; |
233 |
|
int b = ysize; |
234 |
+ |
double z; |
235 |
|
register int i, j; |
236 |
|
|
237 |
< |
for (i = 0; i < ourview.hresolu; i++) { |
237 |
> |
for (i = 0; i < xres; i++) { |
238 |
|
|
239 |
|
copycolor(vline[0], scanbar[0][i]); |
240 |
|
copycolor(vline[ysize], scanbar[ysize][i]); |
241 |
+ |
if (zbar[0]) { |
242 |
+ |
zline[0] = zbar[0][i]; |
243 |
+ |
zline[ysize] = zbar[ysize][i]; |
244 |
+ |
} |
245 |
|
|
246 |
< |
b = fillsample(vline, i, y, 0, ysize, b/2); |
246 |
> |
b = fillsample(vline, zbar[0] ? zline : NULL, |
247 |
> |
i, y, 0, ysize, b/2); |
248 |
|
|
249 |
|
for (j = 1; j < ysize; j++) |
250 |
|
copycolor(scanbar[j][i], vline[j]); |
251 |
+ |
if (zbar[0]) |
252 |
+ |
for (j = 1; j < ysize; j++) |
253 |
+ |
zbar[j][i] = zline[j]; |
254 |
|
} |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
|
int |
259 |
< |
fillsample(colline, x, y, xlen, ylen, b) /* fill interior points */ |
259 |
> |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
260 |
|
register COLOR *colline; |
261 |
+ |
register float *zline; |
262 |
|
int x, y; |
263 |
|
int xlen, ylen; |
264 |
|
int b; |
265 |
|
{ |
266 |
+ |
extern double fabs(); |
267 |
|
double ratio; |
268 |
+ |
double z; |
269 |
|
COLOR ctmp; |
270 |
|
int ncut; |
271 |
|
register int len; |
278 |
|
if (len <= 1) /* limit recursion */ |
279 |
|
return(0); |
280 |
|
|
281 |
< |
if (b > 0 || bigdiff(colline[0], colline[len], maxdiff)) { |
281 |
> |
if (b > 0 |
282 |
> |
|| (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
283 |
> |
|| bigdiff(colline[0], colline[len], maxdiff)) { |
284 |
|
|
285 |
< |
pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
285 |
> |
z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
286 |
> |
if (zline) zline[len>>1] = z; |
287 |
|
ncut = 1; |
288 |
|
|
289 |
|
} else { /* interpolate */ |
291 |
|
copycolor(colline[len>>1], colline[len]); |
292 |
|
ratio = (double)(len>>1) / len; |
293 |
|
scalecolor(colline[len>>1], ratio); |
294 |
< |
copycolor(ctmp, colline[0]); |
294 |
> |
if (zline) zline[len>>1] = zline[len] * ratio; |
295 |
|
ratio = 1.0 - ratio; |
296 |
+ |
copycolor(ctmp, colline[0]); |
297 |
|
scalecolor(ctmp, ratio); |
298 |
|
addcolor(colline[len>>1], ctmp); |
299 |
+ |
if (zline) zline[len>>1] += zline[0] * ratio; |
300 |
|
ncut = 0; |
301 |
|
} |
302 |
|
/* recurse */ |
303 |
< |
ncut += fillsample(colline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
303 |
> |
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
304 |
|
|
305 |
< |
ncut += fillsample(colline + (len>>1), x + (xlen>>1), y + (ylen>>1), |
306 |
< |
xlen - (xlen>>1), ylen - (ylen>>1), b/2); |
305 |
> |
ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, |
306 |
> |
x+(xlen>>1), y+(ylen>>1), |
307 |
> |
xlen-(xlen>>1), ylen-(ylen>>1), b/2); |
308 |
|
|
309 |
|
return(ncut); |
310 |
|
} |
311 |
|
|
312 |
|
|
313 |
+ |
double |
314 |
|
pixvalue(col, x, y) /* compute pixel value */ |
315 |
|
COLOR col; /* returned color */ |
316 |
|
int x, y; /* pixel position */ |
317 |
|
{ |
318 |
|
static RAY thisray; /* our ray for this pixel */ |
319 |
|
|
320 |
< |
rayview(thisray.rorg, thisray.rdir, &ourview, |
321 |
< |
x + pixjitter(), y + pixjitter()); |
320 |
> |
viewray(thisray.rorg, thisray.rdir, &ourview, |
321 |
> |
(x+pixjitter())/hresolu, (y+pixjitter())/vresolu); |
322 |
|
|
323 |
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
324 |
|
|
325 |
|
rayvalue(&thisray); /* trace ray */ |
326 |
|
|
327 |
|
copycolor(col, thisray.rcol); /* return color */ |
328 |
+ |
|
329 |
+ |
return(thisray.rot); /* return distance */ |
330 |
|
} |
331 |
|
|
332 |
|
|
340 |
|
|
341 |
|
if (oldfile == NULL) |
342 |
|
return(0); |
343 |
< |
else if ((fp = fopen(oldfile, "r")) == NULL) { |
343 |
> |
|
344 |
> |
if ((fp = fopen(oldfile, "r")) == NULL) { |
345 |
|
sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); |
346 |
|
error(WARNING, errmsg); |
347 |
|
return(0); |
349 |
|
/* discard header */ |
350 |
|
getheader(fp, NULL); |
351 |
|
/* get picture size */ |
352 |
< |
if (fscanf(fp, "-Y %d +X %d\n", &y, &x) != 2) { |
352 |
> |
if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) { |
353 |
|
sprintf(errmsg, "bad recover file \"%s\"", oldfile); |
354 |
|
error(WARNING, errmsg); |
355 |
|
fclose(fp); |
356 |
|
return(0); |
357 |
|
} |
358 |
|
|
359 |
< |
if (x != ourview.hresolu || y != ourview.vresolu) { |
359 |
> |
if (x != hresolu || y != vresolu) { |
360 |
|
sprintf(errmsg, "resolution mismatch in recover file \"%s\"", |
361 |
|
oldfile); |
362 |
|
error(USER, errmsg); |
276 |
– |
return(0); |
363 |
|
} |
364 |
|
|
365 |
< |
scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR)); |
365 |
> |
scanline = (COLR *)malloc(hresolu*sizeof(COLR)); |
366 |
|
if (scanline == NULL) |
367 |
|
error(SYSTEM, "out of memory in salvage"); |
368 |
< |
for (y = 0; y < ourview.vresolu; y++) { |
369 |
< |
if (freadcolrs(scanline, ourview.hresolu, fp) < 0) |
368 |
> |
for (y = 0; y < vresolu; y++) { |
369 |
> |
if (freadcolrs(scanline, hresolu, fp) < 0) |
370 |
|
break; |
371 |
< |
if (fwritecolrs(scanline, ourview.hresolu, stdout) < 0) |
371 |
> |
if (fwritecolrs(scanline, hresolu, stdout) < 0) |
372 |
|
goto writerr; |
373 |
|
} |
374 |
|
if (fflush(stdout) == EOF) |