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 |
> |
VIEW ourview = STDVIEW; /* view parameters */ |
28 |
> |
int hresolu = 512; /* horizontal resolution */ |
29 |
> |
int vresolu = 512; /* vertical resolution */ |
30 |
> |
double pixaspect = 1.0; /* pixel aspect ratio */ |
31 |
|
|
32 |
|
int psample = 4; /* pixel sample size */ |
33 |
|
double maxdiff = .05; /* max. difference for interpolation */ |
88 |
|
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", |
89 |
|
nrays, pctdone, t/3600.0); |
90 |
|
#else |
91 |
+ |
signal(SIGALRM, report); |
92 |
|
sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone); |
93 |
|
#endif |
94 |
|
eputs(errmsg); |
101 |
|
render(zfile, oldfile) /* render the scene */ |
102 |
|
char *zfile, *oldfile; |
103 |
|
{ |
104 |
+ |
extern long lseek(); |
105 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
106 |
|
float *zbar[MAXDIV+1]; /* z values */ |
107 |
|
int ypos; /* current scanline */ |
108 |
< |
FILE *zfp; |
108 |
> |
int ystep; /* current y step size */ |
109 |
> |
int zfd; |
110 |
|
COLOR *colptr; |
111 |
|
float *zptr; |
112 |
|
register int i; |
117 |
|
psample = MAXDIV; |
118 |
|
/* allocate scanlines */ |
119 |
|
for (i = 0; i <= psample; i++) { |
120 |
< |
scanbar[i] = (COLOR *)malloc(ourview.hresolu*sizeof(COLOR)); |
120 |
> |
scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); |
121 |
|
if (scanbar[i] == NULL) |
122 |
|
goto memerr; |
123 |
|
} |
124 |
|
/* open z file */ |
125 |
|
if (zfile != NULL) { |
126 |
< |
if ((zfp = fopen(zfile, "a+")) == NULL) { |
126 |
> |
if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { |
127 |
|
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
128 |
|
error(SYSTEM, errmsg); |
129 |
|
} |
130 |
|
for (i = 0; i <= psample; i++) { |
131 |
< |
zbar[i] = (float *)malloc(ourview.hresolu*sizeof(float)); |
131 |
> |
zbar[i] = (float *)malloc(hresolu*sizeof(float)); |
132 |
|
if (zbar[i] == NULL) |
133 |
|
goto memerr; |
134 |
|
} |
135 |
|
} else { |
136 |
< |
zfp = NULL; |
136 |
> |
zfd = -1; |
137 |
|
for (i = 0; i <= psample; i++) |
138 |
|
zbar[i] = NULL; |
139 |
|
} |
140 |
|
/* write out boundaries */ |
141 |
< |
fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout); |
141 |
> |
fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); |
142 |
|
/* recover file and compute first */ |
143 |
|
i = salvage(oldfile); |
144 |
< |
if (zfp != NULL && fseek(zfp, (long)i*ourview.hresolu*sizeof(float), 0) == EOF) |
144 |
> |
if (zfd != -1 && i > 0 && |
145 |
> |
lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1) |
146 |
|
error(SYSTEM, "z file seek error in render"); |
147 |
< |
ypos = ourview.vresolu-1 - i; |
148 |
< |
fillscanline(scanbar[0], zbar[0], ourview.hresolu, ypos, psample); |
147 |
> |
ypos = vresolu-1 - i; |
148 |
> |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
149 |
> |
ystep = psample; |
150 |
|
/* compute scanlines */ |
151 |
< |
for (ypos -= psample; ypos >= 0; ypos -= psample) { |
152 |
< |
|
153 |
< |
pctdone = 100.0*(ourview.vresolu-ypos-psample)/ourview.vresolu; |
154 |
< |
|
155 |
< |
colptr = scanbar[psample]; /* move base to top */ |
156 |
< |
scanbar[psample] = scanbar[0]; |
151 |
> |
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
152 |
> |
/* record progress */ |
153 |
> |
pctdone = 100.0*(vresolu-1-ypos-ystep)/vresolu; |
154 |
> |
/* bottom adjust? */ |
155 |
> |
if (ypos < 0) { |
156 |
> |
ystep += ypos; |
157 |
> |
ypos = 0; |
158 |
> |
} |
159 |
> |
colptr = scanbar[ystep]; /* move base to top */ |
160 |
> |
scanbar[ystep] = scanbar[0]; |
161 |
|
scanbar[0] = colptr; |
162 |
< |
zptr = zbar[psample]; |
163 |
< |
zbar[psample] = zbar[0]; |
162 |
> |
zptr = zbar[ystep]; |
163 |
> |
zbar[ystep] = zbar[0]; |
164 |
|
zbar[0] = zptr; |
165 |
|
/* fill base line */ |
166 |
< |
fillscanline(scanbar[0], zbar[0], ourview.hresolu, ypos, psample); |
166 |
> |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
167 |
|
/* fill bar */ |
168 |
< |
fillscanbar(scanbar, zbar, ourview.hresolu, ypos, psample); |
168 |
> |
fillscanbar(scanbar, zbar, hresolu, ypos, ystep); |
169 |
|
/* write it out */ |
170 |
< |
for (i = psample; i > 0; i--) { |
171 |
< |
if (zfp != NULL && fwrite(zbar[i],sizeof(float),ourview.hresolu,zfp) != ourview.hresolu) |
170 |
> |
for (i = ystep; i > 0; i--) { |
171 |
> |
if (zfd != -1 && write(zfd, (char *)zbar[i], |
172 |
> |
hresolu*sizeof(float)) |
173 |
> |
< hresolu*sizeof(float)) |
174 |
|
goto writerr; |
175 |
< |
if (fwritescan(scanbar[i],ourview.hresolu,stdout) < 0) |
175 |
> |
if (fwritescan(scanbar[i], hresolu, stdout) < 0) |
176 |
|
goto writerr; |
177 |
|
} |
161 |
– |
if (zfp != NULL && fflush(zfp) == EOF) |
162 |
– |
goto writerr; |
178 |
|
if (fflush(stdout) == EOF) |
179 |
|
goto writerr; |
180 |
|
} |
166 |
– |
/* compute residual */ |
167 |
– |
colptr = scanbar[psample]; |
168 |
– |
scanbar[psample] = scanbar[0]; |
169 |
– |
scanbar[0] = colptr; |
170 |
– |
zptr = zbar[psample]; |
171 |
– |
zbar[psample] = zbar[0]; |
172 |
– |
zbar[0] = zptr; |
173 |
– |
if (ypos > -psample) { |
174 |
– |
fillscanline(scanbar[-ypos], zbar[-ypos], ourview.hresolu, |
175 |
– |
0, psample); |
176 |
– |
fillscanbar(scanbar-ypos, zbar-ypos, ourview.hresolu, |
177 |
– |
0, psample+ypos); |
178 |
– |
} |
179 |
– |
for (i = psample; i+ypos >= 0; i--) { |
180 |
– |
if (zfp != NULL && fwrite(zbar[i],sizeof(float),ourview.hresolu,zfp) != ourview.hresolu) |
181 |
– |
goto writerr; |
182 |
– |
if (fwritescan(scanbar[i], ourview.hresolu, stdout) < 0) |
183 |
– |
goto writerr; |
184 |
– |
} |
181 |
|
/* clean up */ |
182 |
< |
if (zfp != NULL) { |
183 |
< |
if (fclose(zfp) == EOF) |
182 |
> |
if (zfd != -1) { |
183 |
> |
if (write(zfd, (char *)zbar[0], hresolu*sizeof(float)) |
184 |
> |
< hresolu*sizeof(float)) |
185 |
|
goto writerr; |
186 |
+ |
if (close(zfd) == -1) |
187 |
+ |
goto writerr; |
188 |
|
for (i = 0; i <= psample; i++) |
189 |
|
free((char *)zbar[i]); |
190 |
|
} |
191 |
+ |
fwritescan(scanbar[0], hresolu, stdout); |
192 |
|
if (fflush(stdout) == EOF) |
193 |
|
goto writerr; |
194 |
|
for (i = 0; i <= psample; i++) |
214 |
|
z = pixvalue(scanline[0], 0, y); |
215 |
|
if (zline) zline[0] = z; |
216 |
|
|
217 |
< |
for (i = xstep; i < xres; i += xstep) { |
218 |
< |
|
217 |
> |
for (i = xstep; i < xres-1+xstep; i += xstep) { |
218 |
> |
if (i >= xres) { |
219 |
> |
xstep += xres-1-i; |
220 |
> |
i = xres-1; |
221 |
> |
} |
222 |
|
z = pixvalue(scanline[i], i, y); |
223 |
|
if (zline) zline[i] = z; |
224 |
|
|
225 |
|
b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, |
226 |
|
i-xstep, y, xstep, 0, b/2); |
227 |
|
} |
225 |
– |
if (i-xstep < xres-1) { |
226 |
– |
z = pixvalue(scanline[xres-1], xres-1, y); |
227 |
– |
if (zline) zline[xres-1] = z; |
228 |
– |
fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, |
229 |
– |
i-xstep, y, xres-1-(i-xstep), 0, b/2); |
230 |
– |
} |
228 |
|
} |
229 |
|
|
230 |
|
|
236 |
|
COLOR vline[MAXDIV+1]; |
237 |
|
float zline[MAXDIV+1]; |
238 |
|
int b = ysize; |
242 |
– |
double z; |
239 |
|
register int i, j; |
240 |
|
|
241 |
|
for (i = 0; i < xres; i++) { |
321 |
|
{ |
322 |
|
static RAY thisray; /* our ray for this pixel */ |
323 |
|
|
324 |
< |
rayview(thisray.rorg, thisray.rdir, &ourview, |
325 |
< |
x + pixjitter(), y + pixjitter()); |
324 |
> |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
325 |
> |
(x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) { |
326 |
> |
setcolor(col, 0.0, 0.0, 0.0); |
327 |
> |
return(0.0); |
328 |
> |
} |
329 |
|
|
330 |
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
331 |
|
|
333 |
|
|
334 |
|
copycolor(col, thisray.rcol); /* return color */ |
335 |
|
|
336 |
< |
return(thisray.rot); /* return distance */ |
336 |
> |
return(thisray.rt); /* return distance */ |
337 |
|
} |
338 |
|
|
339 |
|
|
363 |
|
return(0); |
364 |
|
} |
365 |
|
|
366 |
< |
if (x != ourview.hresolu || y != ourview.vresolu) { |
366 |
> |
if (x != hresolu || y != vresolu) { |
367 |
|
sprintf(errmsg, "resolution mismatch in recover file \"%s\"", |
368 |
|
oldfile); |
369 |
|
error(USER, errmsg); |
370 |
|
} |
371 |
|
|
372 |
< |
scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR)); |
372 |
> |
scanline = (COLR *)malloc(hresolu*sizeof(COLR)); |
373 |
|
if (scanline == NULL) |
374 |
|
error(SYSTEM, "out of memory in salvage"); |
375 |
< |
for (y = 0; y < ourview.vresolu; y++) { |
376 |
< |
if (freadcolrs(scanline, ourview.hresolu, fp) < 0) |
375 |
> |
for (y = 0; y < vresolu; y++) { |
376 |
> |
if (freadcolrs(scanline, hresolu, fp) < 0) |
377 |
|
break; |
378 |
< |
if (fwritecolrs(scanline, ourview.hresolu, stdout) < 0) |
378 |
> |
if (fwritecolrs(scanline, hresolu, stdout) < 0) |
379 |
|
goto writerr; |
380 |
|
} |
381 |
|
if (fflush(stdout) == EOF) |