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 |
> |
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 */ |
53 |
|
|
54 |
|
double pctdone = 0.0; /* percentage done */ |
55 |
|
|
56 |
+ |
long tlastrept = 0L; /* time at last report */ |
57 |
+ |
|
58 |
+ |
extern long time(); |
59 |
+ |
extern long tstart; /* starting time */ |
60 |
+ |
|
61 |
|
extern long nrays; /* number of rays traced */ |
62 |
|
|
63 |
|
#define MAXDIV 32 /* maximum sample size */ |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
+ |
#ifdef BSD |
81 |
|
report() /* report progress */ |
82 |
|
{ |
71 |
– |
#ifdef BSD |
83 |
|
struct rusage rubuf; |
84 |
|
double t; |
85 |
|
|
92 |
|
|
93 |
|
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", |
94 |
|
nrays, pctdone, t/3600.0); |
95 |
+ |
eputs(errmsg); |
96 |
+ |
tlastrept = time((long *)0); |
97 |
+ |
} |
98 |
|
#else |
99 |
< |
sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone); |
100 |
< |
#endif |
99 |
> |
report() /* report progress */ |
100 |
> |
{ |
101 |
> |
signal(SIGALRM, report); |
102 |
> |
tlastrept = time((long *)0); |
103 |
> |
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n", |
104 |
> |
nrays, pctdone, (tlastrept-tstart)/3600.0); |
105 |
|
eputs(errmsg); |
88 |
– |
|
89 |
– |
if (ralrm > 0) |
90 |
– |
alarm(ralrm); |
106 |
|
} |
107 |
+ |
#endif |
108 |
|
|
109 |
|
|
110 |
|
render(zfile, oldfile) /* render the scene */ |
111 |
|
char *zfile, *oldfile; |
112 |
|
{ |
113 |
+ |
extern long lseek(); |
114 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
115 |
|
float *zbar[MAXDIV+1]; /* z values */ |
116 |
|
int ypos; /* current scanline */ |
117 |
< |
FILE *zfp; |
117 |
> |
int ystep; /* current y step size */ |
118 |
> |
int zfd; |
119 |
|
COLOR *colptr; |
120 |
|
float *zptr; |
121 |
|
register int i; |
126 |
|
psample = MAXDIV; |
127 |
|
/* allocate scanlines */ |
128 |
|
for (i = 0; i <= psample; i++) { |
129 |
< |
scanbar[i] = (COLOR *)malloc(ourview.hresolu*sizeof(COLOR)); |
129 |
> |
scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); |
130 |
|
if (scanbar[i] == NULL) |
131 |
|
goto memerr; |
132 |
|
} |
133 |
|
/* open z file */ |
134 |
|
if (zfile != NULL) { |
135 |
< |
if ((zfp = fopen(zfile, "a+")) == NULL) { |
135 |
> |
if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { |
136 |
|
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
137 |
|
error(SYSTEM, errmsg); |
138 |
|
} |
139 |
|
for (i = 0; i <= psample; i++) { |
140 |
< |
zbar[i] = (float *)malloc(ourview.hresolu*sizeof(float)); |
140 |
> |
zbar[i] = (float *)malloc(hresolu*sizeof(float)); |
141 |
|
if (zbar[i] == NULL) |
142 |
|
goto memerr; |
143 |
|
} |
144 |
|
} else { |
145 |
< |
zfp = NULL; |
145 |
> |
zfd = -1; |
146 |
|
for (i = 0; i <= psample; i++) |
147 |
|
zbar[i] = NULL; |
148 |
|
} |
149 |
|
/* write out boundaries */ |
150 |
< |
fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout); |
150 |
> |
fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); |
151 |
|
/* recover file and compute first */ |
152 |
|
i = salvage(oldfile); |
153 |
< |
if (zfp != NULL && fseek(zfp, (long)i*ourview.hresolu*sizeof(float), 0) == EOF) |
153 |
> |
if (zfd != -1 && i > 0 && |
154 |
> |
lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1) |
155 |
|
error(SYSTEM, "z file seek error in render"); |
156 |
< |
ypos = ourview.vresolu-1 - i; |
157 |
< |
fillscanline(scanbar[0], zbar[0], ourview.hresolu, ypos, psample); |
156 |
> |
pctdone = 100.0*i/vresolu; |
157 |
> |
if (ralrm > 0) /* report init stats */ |
158 |
> |
report(); |
159 |
> |
ypos = vresolu-1 - i; |
160 |
> |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
161 |
> |
ystep = psample; |
162 |
|
/* compute scanlines */ |
163 |
< |
for (ypos -= psample; ypos >= 0; ypos -= psample) { |
164 |
< |
|
165 |
< |
pctdone = 100.0*(ourview.vresolu-ypos-psample)/ourview.vresolu; |
166 |
< |
|
167 |
< |
colptr = scanbar[psample]; /* move base to top */ |
168 |
< |
scanbar[psample] = scanbar[0]; |
163 |
> |
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
164 |
> |
/* bottom adjust? */ |
165 |
> |
if (ypos < 0) { |
166 |
> |
ystep += ypos; |
167 |
> |
ypos = 0; |
168 |
> |
} |
169 |
> |
colptr = scanbar[ystep]; /* move base to top */ |
170 |
> |
scanbar[ystep] = scanbar[0]; |
171 |
|
scanbar[0] = colptr; |
172 |
< |
zptr = zbar[psample]; |
173 |
< |
zbar[psample] = zbar[0]; |
172 |
> |
zptr = zbar[ystep]; |
173 |
> |
zbar[ystep] = zbar[0]; |
174 |
|
zbar[0] = zptr; |
175 |
|
/* fill base line */ |
176 |
< |
fillscanline(scanbar[0], zbar[0], ourview.hresolu, ypos, psample); |
176 |
> |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
177 |
|
/* fill bar */ |
178 |
< |
fillscanbar(scanbar, zbar, ourview.hresolu, ypos, psample); |
178 |
> |
fillscanbar(scanbar, zbar, hresolu, ypos, ystep); |
179 |
|
/* write it out */ |
180 |
< |
for (i = psample; i > 0; i--) { |
181 |
< |
if (zfp != NULL && fwrite(zbar[i],sizeof(float),ourview.hresolu,zfp) != ourview.hresolu) |
180 |
> |
for (i = ystep; i > 0; i--) { |
181 |
> |
if (zfd != -1 && write(zfd, (char *)zbar[i], |
182 |
> |
hresolu*sizeof(float)) |
183 |
> |
< hresolu*sizeof(float)) |
184 |
|
goto writerr; |
185 |
< |
if (fwritescan(scanbar[i],ourview.hresolu,stdout) < 0) |
185 |
> |
if (fwritescan(scanbar[i], hresolu, stdout) < 0) |
186 |
|
goto writerr; |
187 |
|
} |
161 |
– |
if (zfp != NULL && fflush(zfp) == EOF) |
162 |
– |
goto writerr; |
188 |
|
if (fflush(stdout) == EOF) |
189 |
|
goto writerr; |
190 |
+ |
/* record progress */ |
191 |
+ |
pctdone = 100.0*(vresolu-1-ypos)/vresolu; |
192 |
+ |
if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) |
193 |
+ |
report(); |
194 |
|
} |
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 |
– |
} |
195 |
|
/* clean up */ |
196 |
< |
if (zfp != NULL) { |
197 |
< |
if (fclose(zfp) == EOF) |
196 |
> |
if (zfd != -1) { |
197 |
> |
if (write(zfd, (char *)zbar[0], hresolu*sizeof(float)) |
198 |
> |
< hresolu*sizeof(float)) |
199 |
|
goto writerr; |
200 |
+ |
if (close(zfd) == -1) |
201 |
+ |
goto writerr; |
202 |
|
for (i = 0; i <= psample; i++) |
203 |
|
free((char *)zbar[i]); |
204 |
|
} |
205 |
+ |
fwritescan(scanbar[0], hresolu, stdout); |
206 |
|
if (fflush(stdout) == EOF) |
207 |
|
goto writerr; |
208 |
|
for (i = 0; i <= psample; i++) |
228 |
|
z = pixvalue(scanline[0], 0, y); |
229 |
|
if (zline) zline[0] = z; |
230 |
|
|
231 |
< |
for (i = xstep; i < xres; i += xstep) { |
232 |
< |
|
231 |
> |
for (i = xstep; i < xres-1+xstep; i += xstep) { |
232 |
> |
if (i >= xres) { |
233 |
> |
xstep += xres-1-i; |
234 |
> |
i = xres-1; |
235 |
> |
} |
236 |
|
z = pixvalue(scanline[i], i, y); |
237 |
|
if (zline) zline[i] = z; |
238 |
|
|
239 |
|
b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, |
240 |
|
i-xstep, y, xstep, 0, b/2); |
241 |
|
} |
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 |
– |
} |
242 |
|
} |
243 |
|
|
244 |
|
|
250 |
|
COLOR vline[MAXDIV+1]; |
251 |
|
float zline[MAXDIV+1]; |
252 |
|
int b = ysize; |
242 |
– |
double z; |
253 |
|
register int i, j; |
254 |
|
|
255 |
|
for (i = 0; i < xres; i++) { |
335 |
|
{ |
336 |
|
static RAY thisray; /* our ray for this pixel */ |
337 |
|
|
338 |
< |
rayview(thisray.rorg, thisray.rdir, &ourview, |
339 |
< |
x + pixjitter(), y + pixjitter()); |
338 |
> |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
339 |
> |
(x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) { |
340 |
> |
setcolor(col, 0.0, 0.0, 0.0); |
341 |
> |
return(0.0); |
342 |
> |
} |
343 |
|
|
344 |
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
345 |
|
|
347 |
|
|
348 |
|
copycolor(col, thisray.rcol); /* return color */ |
349 |
|
|
350 |
< |
return(thisray.rot); /* return distance */ |
350 |
> |
return(thisray.rt); /* return distance */ |
351 |
|
} |
352 |
|
|
353 |
|
|
377 |
|
return(0); |
378 |
|
} |
379 |
|
|
380 |
< |
if (x != ourview.hresolu || y != ourview.vresolu) { |
380 |
> |
if (x != hresolu || y != vresolu) { |
381 |
|
sprintf(errmsg, "resolution mismatch in recover file \"%s\"", |
382 |
|
oldfile); |
383 |
|
error(USER, errmsg); |
384 |
|
} |
385 |
|
|
386 |
< |
scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR)); |
386 |
> |
scanline = (COLR *)malloc(hresolu*sizeof(COLR)); |
387 |
|
if (scanline == NULL) |
388 |
|
error(SYSTEM, "out of memory in salvage"); |
389 |
< |
for (y = 0; y < ourview.vresolu; y++) { |
390 |
< |
if (freadcolrs(scanline, ourview.hresolu, fp) < 0) |
389 |
> |
for (y = 0; y < vresolu; y++) { |
390 |
> |
if (freadcolrs(scanline, hresolu, fp) < 0) |
391 |
|
break; |
392 |
< |
if (fwritecolrs(scanline, ourview.hresolu, stdout) < 0) |
392 |
> |
if (fwritecolrs(scanline, hresolu, stdout) < 0) |
393 |
|
goto writerr; |
394 |
|
} |
395 |
|
if (fflush(stdout) == EOF) |