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