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 */ |
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 xres, yres; /* rendered x and y resolution */ |
106 |
> |
FILE *zfp; |
107 |
|
COLOR *colptr; |
108 |
+ |
float *zptr; |
109 |
|
register int i; |
110 |
< |
/* set rendered resolution */ |
111 |
< |
xres = ourview.hresolu; |
102 |
< |
yres = ourview.vresolu; |
103 |
< |
/* adjust for sampling */ |
104 |
< |
if (psample <= 1) |
110 |
> |
/* check sampling */ |
111 |
> |
if (psample < 1) |
112 |
|
psample = 1; |
113 |
< |
else { |
114 |
< |
if (psample > MAXDIV) |
108 |
< |
psample = MAXDIV; |
109 |
< |
/* rendered resolution may be larger */ |
110 |
< |
xres += psample-1 - ((ourview.hresolu-2)%psample); |
111 |
< |
yres += psample-1 - ((ourview.vresolu-2)%psample); |
112 |
< |
} |
113 |
> |
else if (psample > MAXDIV) |
114 |
> |
psample = MAXDIV; |
115 |
|
/* allocate scanlines */ |
116 |
|
for (i = 0; i <= psample; i++) { |
117 |
< |
scanbar[i] = (COLOR *)malloc(xres*sizeof(COLOR)); |
117 |
> |
scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); |
118 |
|
if (scanbar[i] == NULL) |
119 |
< |
error(SYSTEM, "out of memory in render"); |
119 |
> |
goto memerr; |
120 |
|
} |
121 |
+ |
/* open z file */ |
122 |
+ |
if (zfile != NULL) { |
123 |
+ |
if ((zfp = fopen(zfile, "a+")) == NULL) { |
124 |
+ |
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
125 |
+ |
error(SYSTEM, errmsg); |
126 |
+ |
} |
127 |
+ |
for (i = 0; i <= psample; i++) { |
128 |
+ |
zbar[i] = (float *)malloc(hresolu*sizeof(float)); |
129 |
+ |
if (zbar[i] == NULL) |
130 |
+ |
goto memerr; |
131 |
+ |
} |
132 |
+ |
} else { |
133 |
+ |
zfp = NULL; |
134 |
+ |
for (i = 0; i <= psample; i++) |
135 |
+ |
zbar[i] = NULL; |
136 |
+ |
} |
137 |
|
/* write out boundaries */ |
138 |
< |
fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout); |
139 |
< |
|
140 |
< |
ypos = ourview.vresolu-1 - salvage(oldfile); /* find top line */ |
141 |
< |
fillscanline(scanbar[0], xres, ypos, psample); /* top scan */ |
142 |
< |
|
143 |
< |
for (ypos -= psample; ypos > -psample; ypos -= psample) { |
138 |
> |
fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); |
139 |
> |
/* recover file and compute first */ |
140 |
> |
i = salvage(oldfile); |
141 |
> |
if (zfp != NULL && fseek(zfp, (long)i*hresolu*sizeof(float), 0) == EOF) |
142 |
> |
error(SYSTEM, "z file seek error in render"); |
143 |
> |
ypos = vresolu-1 - i; |
144 |
> |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
145 |
> |
/* compute scanlines */ |
146 |
> |
for (ypos -= psample; ypos >= 0; ypos -= psample) { |
147 |
|
|
148 |
< |
pctdone = 100.0*(ourview.vresolu-ypos+psample)/ourview.vresolu; |
148 |
> |
pctdone = 100.0*(vresolu-ypos-psample)/vresolu; |
149 |
|
|
150 |
|
colptr = scanbar[psample]; /* move base to top */ |
151 |
|
scanbar[psample] = scanbar[0]; |
152 |
|
scanbar[0] = colptr; |
153 |
< |
|
154 |
< |
fillscanline(scanbar[0], xres, ypos, psample); /* fill base */ |
155 |
< |
|
156 |
< |
fillscanbar(scanbar, xres, ypos, psample); /* fill bar */ |
157 |
< |
|
158 |
< |
for (i = psample; (ypos>0) ? i > 0 : ypos+i >= 0; i--) |
159 |
< |
if (fwritescan(scanbar[i], ourview.hresolu, stdout) < 0) |
153 |
> |
zptr = zbar[psample]; |
154 |
> |
zbar[psample] = zbar[0]; |
155 |
> |
zbar[0] = zptr; |
156 |
> |
/* fill base line */ |
157 |
> |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
158 |
> |
/* fill bar */ |
159 |
> |
fillscanbar(scanbar, zbar, hresolu, ypos, psample); |
160 |
> |
/* write it out */ |
161 |
> |
for (i = psample; i > 0; i--) { |
162 |
> |
if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu) |
163 |
|
goto writerr; |
164 |
+ |
if (fwritescan(scanbar[i],hresolu,stdout) < 0) |
165 |
+ |
goto writerr; |
166 |
+ |
} |
167 |
+ |
if (zfp != NULL && fflush(zfp) == EOF) |
168 |
+ |
goto writerr; |
169 |
|
if (fflush(stdout) == EOF) |
170 |
|
goto writerr; |
171 |
|
} |
172 |
< |
pctdone = 100.0; |
173 |
< |
|
172 |
> |
/* compute residual */ |
173 |
> |
colptr = scanbar[psample]; |
174 |
> |
scanbar[psample] = scanbar[0]; |
175 |
> |
scanbar[0] = colptr; |
176 |
> |
zptr = zbar[psample]; |
177 |
> |
zbar[psample] = zbar[0]; |
178 |
> |
zbar[0] = zptr; |
179 |
> |
if (ypos > -psample) { |
180 |
> |
fillscanline(scanbar[-ypos], zbar[-ypos], hresolu, 0, psample); |
181 |
> |
fillscanbar(scanbar-ypos, zbar-ypos, hresolu, 0, psample+ypos); |
182 |
> |
} |
183 |
> |
for (i = psample; i+ypos >= 0; i--) { |
184 |
> |
if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu) |
185 |
> |
goto writerr; |
186 |
> |
if (fwritescan(scanbar[i], hresolu, stdout) < 0) |
187 |
> |
goto writerr; |
188 |
> |
} |
189 |
> |
/* clean up */ |
190 |
> |
if (zfp != NULL) { |
191 |
> |
if (fclose(zfp) == EOF) |
192 |
> |
goto writerr; |
193 |
> |
for (i = 0; i <= psample; i++) |
194 |
> |
free((char *)zbar[i]); |
195 |
> |
} |
196 |
> |
if (fflush(stdout) == EOF) |
197 |
> |
goto writerr; |
198 |
|
for (i = 0; i <= psample; i++) |
199 |
|
free((char *)scanbar[i]); |
200 |
+ |
pctdone = 100.0; |
201 |
|
return; |
202 |
|
writerr: |
203 |
|
error(SYSTEM, "write error in render"); |
204 |
+ |
memerr: |
205 |
+ |
error(SYSTEM, "out of memory in render"); |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
< |
fillscanline(scanline, xres, y, xstep) /* fill scan line at y */ |
209 |
> |
fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */ |
210 |
|
register COLOR *scanline; |
211 |
+ |
register float *zline; |
212 |
|
int xres, y, xstep; |
213 |
|
{ |
214 |
|
int b = xstep; |
215 |
+ |
double z; |
216 |
|
register int i; |
217 |
|
|
218 |
< |
pixvalue(scanline[0], 0, y); |
218 |
> |
z = pixvalue(scanline[0], 0, y); |
219 |
> |
if (zline) zline[0] = z; |
220 |
|
|
221 |
|
for (i = xstep; i < xres; i += xstep) { |
222 |
|
|
223 |
< |
pixvalue(scanline[i], i, y); |
223 |
> |
z = pixvalue(scanline[i], i, y); |
224 |
> |
if (zline) zline[i] = z; |
225 |
|
|
226 |
< |
b = fillsample(scanline+i-xstep, i-xstep, y, xstep, 0, b/2); |
226 |
> |
b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, |
227 |
> |
i-xstep, y, xstep, 0, b/2); |
228 |
|
} |
229 |
+ |
if (i-xstep < xres-1) { |
230 |
+ |
z = pixvalue(scanline[xres-1], xres-1, y); |
231 |
+ |
if (zline) zline[xres-1] = z; |
232 |
+ |
fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, |
233 |
+ |
i-xstep, y, xres-1-(i-xstep), 0, b/2); |
234 |
+ |
} |
235 |
|
} |
236 |
|
|
237 |
|
|
238 |
< |
fillscanbar(scanbar, xres, y, ysize) /* fill interior */ |
238 |
> |
fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ |
239 |
|
register COLOR *scanbar[]; |
240 |
+ |
register float *zbar[]; |
241 |
|
int xres, y, ysize; |
242 |
|
{ |
243 |
|
COLOR vline[MAXDIV+1]; |
244 |
+ |
float zline[MAXDIV+1]; |
245 |
|
int b = ysize; |
246 |
+ |
double z; |
247 |
|
register int i, j; |
248 |
|
|
249 |
|
for (i = 0; i < xres; i++) { |
250 |
|
|
251 |
|
copycolor(vline[0], scanbar[0][i]); |
252 |
|
copycolor(vline[ysize], scanbar[ysize][i]); |
253 |
+ |
if (zbar[0]) { |
254 |
+ |
zline[0] = zbar[0][i]; |
255 |
+ |
zline[ysize] = zbar[ysize][i]; |
256 |
+ |
} |
257 |
|
|
258 |
< |
b = fillsample(vline, i, y, 0, ysize, b/2); |
258 |
> |
b = fillsample(vline, zbar[0] ? zline : NULL, |
259 |
> |
i, y, 0, ysize, b/2); |
260 |
|
|
261 |
|
for (j = 1; j < ysize; j++) |
262 |
|
copycolor(scanbar[j][i], vline[j]); |
263 |
+ |
if (zbar[0]) |
264 |
+ |
for (j = 1; j < ysize; j++) |
265 |
+ |
zbar[j][i] = zline[j]; |
266 |
|
} |
267 |
|
} |
268 |
|
|
269 |
|
|
270 |
|
int |
271 |
< |
fillsample(colline, x, y, xlen, ylen, b) /* fill interior points */ |
271 |
> |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
272 |
|
register COLOR *colline; |
273 |
+ |
register float *zline; |
274 |
|
int x, y; |
275 |
|
int xlen, ylen; |
276 |
|
int b; |
277 |
|
{ |
278 |
+ |
extern double fabs(); |
279 |
|
double ratio; |
280 |
+ |
double z; |
281 |
|
COLOR ctmp; |
282 |
|
int ncut; |
283 |
|
register int len; |
290 |
|
if (len <= 1) /* limit recursion */ |
291 |
|
return(0); |
292 |
|
|
293 |
< |
if (b > 0 || bigdiff(colline[0], colline[len], maxdiff)) { |
293 |
> |
if (b > 0 |
294 |
> |
|| (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
295 |
> |
|| bigdiff(colline[0], colline[len], maxdiff)) { |
296 |
|
|
297 |
< |
pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
297 |
> |
z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
298 |
> |
if (zline) zline[len>>1] = z; |
299 |
|
ncut = 1; |
300 |
|
|
301 |
|
} else { /* interpolate */ |
303 |
|
copycolor(colline[len>>1], colline[len]); |
304 |
|
ratio = (double)(len>>1) / len; |
305 |
|
scalecolor(colline[len>>1], ratio); |
306 |
< |
copycolor(ctmp, colline[0]); |
306 |
> |
if (zline) zline[len>>1] = zline[len] * ratio; |
307 |
|
ratio = 1.0 - ratio; |
308 |
+ |
copycolor(ctmp, colline[0]); |
309 |
|
scalecolor(ctmp, ratio); |
310 |
|
addcolor(colline[len>>1], ctmp); |
311 |
+ |
if (zline) zline[len>>1] += zline[0] * ratio; |
312 |
|
ncut = 0; |
313 |
|
} |
314 |
|
/* recurse */ |
315 |
< |
ncut += fillsample(colline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
315 |
> |
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
316 |
|
|
317 |
< |
ncut += fillsample(colline + (len>>1), x + (xlen>>1), y + (ylen>>1), |
318 |
< |
xlen - (xlen>>1), ylen - (ylen>>1), b/2); |
317 |
> |
ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, |
318 |
> |
x+(xlen>>1), y+(ylen>>1), |
319 |
> |
xlen-(xlen>>1), ylen-(ylen>>1), b/2); |
320 |
|
|
321 |
|
return(ncut); |
322 |
|
} |
323 |
|
|
324 |
|
|
325 |
+ |
double |
326 |
|
pixvalue(col, x, y) /* compute pixel value */ |
327 |
|
COLOR col; /* returned color */ |
328 |
|
int x, y; /* pixel position */ |
329 |
|
{ |
330 |
|
static RAY thisray; /* our ray for this pixel */ |
331 |
|
|
332 |
< |
rayview(thisray.rorg, thisray.rdir, &ourview, |
333 |
< |
x + pixjitter(), y + pixjitter()); |
332 |
> |
viewray(thisray.rorg, thisray.rdir, &ourview, |
333 |
> |
(x+pixjitter())/hresolu, (y+pixjitter())/vresolu); |
334 |
|
|
335 |
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
336 |
|
|
337 |
|
rayvalue(&thisray); /* trace ray */ |
338 |
|
|
339 |
|
copycolor(col, thisray.rcol); /* return color */ |
340 |
+ |
|
341 |
+ |
return(thisray.rot); /* return distance */ |
342 |
|
} |
343 |
|
|
344 |
|
|
368 |
|
return(0); |
369 |
|
} |
370 |
|
|
371 |
< |
if (x != ourview.hresolu || y != ourview.vresolu) { |
371 |
> |
if (x != hresolu || y != vresolu) { |
372 |
|
sprintf(errmsg, "resolution mismatch in recover file \"%s\"", |
373 |
|
oldfile); |
374 |
|
error(USER, errmsg); |
375 |
|
} |
376 |
|
|
377 |
< |
scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR)); |
377 |
> |
scanline = (COLR *)malloc(hresolu*sizeof(COLR)); |
378 |
|
if (scanline == NULL) |
379 |
|
error(SYSTEM, "out of memory in salvage"); |
380 |
< |
for (y = 0; y < ourview.vresolu; y++) { |
381 |
< |
if (freadcolrs(scanline, ourview.hresolu, fp) < 0) |
380 |
> |
for (y = 0; y < vresolu; y++) { |
381 |
> |
if (freadcolrs(scanline, hresolu, fp) < 0) |
382 |
|
break; |
383 |
< |
if (fwritecolrs(scanline, ourview.hresolu, stdout) < 0) |
383 |
> |
if (fwritecolrs(scanline, hresolu, stdout) < 0) |
384 |
|
goto writerr; |
385 |
|
} |
386 |
|
if (fflush(stdout) == EOF) |