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> |
18 |
|
#endif |
19 |
|
|
20 |
+ |
#include <signal.h> |
21 |
+ |
#include <fcntl.h> |
22 |
+ |
|
23 |
|
#include "view.h" |
24 |
|
|
25 |
+ |
#include "resolu.h" |
26 |
+ |
|
27 |
|
#include "random.h" |
28 |
|
|
29 |
+ |
int dimlist[MAXDIM]; /* sampling dimensions */ |
30 |
+ |
int ndims = 0; /* number of sampling dimensions */ |
31 |
+ |
int samplendx; /* sample index number */ |
32 |
+ |
|
33 |
|
VIEW ourview = STDVIEW; /* view parameters */ |
34 |
|
int hresolu = 512; /* horizontal resolution */ |
35 |
|
int vresolu = 512; /* vertical resolution */ |
42 |
|
double dstrsrc = 0.0; /* square source distribution */ |
43 |
|
double shadthresh = .05; /* shadow threshold */ |
44 |
|
double shadcert = .5; /* shadow certainty */ |
45 |
+ |
int directrelay = 1; /* number of source relays */ |
46 |
+ |
int vspretest = 512; /* virtual source pretest density */ |
47 |
+ |
int directinvis = 0; /* sources invisible? */ |
48 |
+ |
double srcsizerat = .25; /* maximum ratio source size/dist. */ |
49 |
|
|
50 |
+ |
double specthresh = .15; /* specular sampling threshold */ |
51 |
+ |
double specjitter = 1.; /* specular sampling jitter */ |
52 |
+ |
|
53 |
|
int maxdepth = 6; /* maximum recursion depth */ |
54 |
|
double minweight = 5e-3; /* minimum ray weight */ |
55 |
|
|
66 |
|
|
67 |
|
double pctdone = 0.0; /* percentage done */ |
68 |
|
|
69 |
+ |
long tlastrept = 0L; /* time at last report */ |
70 |
+ |
|
71 |
+ |
extern long time(); |
72 |
+ |
extern long tstart; /* starting time */ |
73 |
+ |
|
74 |
|
extern long nrays; /* number of rays traced */ |
75 |
|
|
76 |
< |
#define MAXDIV 32 /* maximum sample size */ |
76 |
> |
#define MAXDIV 16 /* maximum sample size */ |
77 |
|
|
78 |
|
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
79 |
|
|
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
+ |
#ifdef BSD |
94 |
|
report() /* report progress */ |
95 |
|
{ |
76 |
– |
#ifdef BSD |
96 |
|
struct rusage rubuf; |
97 |
|
double t; |
98 |
|
|
105 |
|
|
106 |
|
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", |
107 |
|
nrays, pctdone, t/3600.0); |
108 |
+ |
eputs(errmsg); |
109 |
+ |
tlastrept = time((long *)0); |
110 |
+ |
} |
111 |
|
#else |
112 |
+ |
report() /* report progress */ |
113 |
+ |
{ |
114 |
+ |
tlastrept = time((long *)0); |
115 |
+ |
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n", |
116 |
+ |
nrays, pctdone, (tlastrept-tstart)/3600.0); |
117 |
+ |
eputs(errmsg); |
118 |
|
signal(SIGALRM, report); |
119 |
< |
sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone); |
119 |
> |
} |
120 |
|
#endif |
93 |
– |
eputs(errmsg); |
121 |
|
|
95 |
– |
if (ralrm > 0) |
96 |
– |
alarm(ralrm); |
97 |
– |
} |
122 |
|
|
99 |
– |
|
123 |
|
render(zfile, oldfile) /* render the scene */ |
124 |
|
char *zfile, *oldfile; |
125 |
|
{ |
126 |
+ |
extern long lseek(); |
127 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
128 |
|
float *zbar[MAXDIV+1]; /* z values */ |
129 |
+ |
char *sampdens; /* previous sample density */ |
130 |
|
int ypos; /* current scanline */ |
131 |
< |
FILE *zfp; |
131 |
> |
int ystep; /* current y step size */ |
132 |
> |
int hstep; /* h step size */ |
133 |
> |
int zfd; |
134 |
|
COLOR *colptr; |
135 |
|
float *zptr; |
136 |
|
register int i; |
137 |
|
/* check sampling */ |
138 |
|
if (psample < 1) |
139 |
|
psample = 1; |
140 |
< |
else if (psample > MAXDIV) |
140 |
> |
else if (psample > MAXDIV) { |
141 |
> |
sprintf(errmsg, "pixel sampling reduced from %d to %d", |
142 |
> |
psample, MAXDIV); |
143 |
> |
error(WARNING, errmsg); |
144 |
|
psample = MAXDIV; |
145 |
+ |
} |
146 |
|
/* allocate scanlines */ |
147 |
|
for (i = 0; i <= psample; i++) { |
148 |
|
scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); |
149 |
|
if (scanbar[i] == NULL) |
150 |
|
goto memerr; |
151 |
|
} |
152 |
+ |
hstep = (psample*140+49)/99; /* quincunx sampling */ |
153 |
+ |
ystep = (psample*99+70)/140; |
154 |
+ |
if (hstep > 2) { |
155 |
+ |
i = hresolu/hstep + 2; |
156 |
+ |
if ((sampdens = malloc(i)) == NULL) |
157 |
+ |
goto memerr; |
158 |
+ |
while (i--) |
159 |
+ |
sampdens[i] = hstep; |
160 |
+ |
} else |
161 |
+ |
sampdens = NULL; |
162 |
|
/* open z file */ |
163 |
|
if (zfile != NULL) { |
164 |
< |
if ((zfp = fopen(zfile, "a+")) == NULL) { |
164 |
> |
if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { |
165 |
|
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
166 |
|
error(SYSTEM, errmsg); |
167 |
|
} |
171 |
|
goto memerr; |
172 |
|
} |
173 |
|
} else { |
174 |
< |
zfp = NULL; |
174 |
> |
zfd = -1; |
175 |
|
for (i = 0; i <= psample; i++) |
176 |
|
zbar[i] = NULL; |
177 |
|
} |
178 |
|
/* write out boundaries */ |
179 |
< |
fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); |
179 |
> |
fprtresolu(hresolu, vresolu, stdout); |
180 |
|
/* recover file and compute first */ |
181 |
|
i = salvage(oldfile); |
182 |
< |
if (zfp != NULL && fseek(zfp, (long)i*hresolu*sizeof(float), 0) == EOF) |
182 |
> |
if (zfd != -1 && i > 0 && |
183 |
> |
lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1) |
184 |
|
error(SYSTEM, "z file seek error in render"); |
185 |
+ |
pctdone = 100.0*i/vresolu; |
186 |
+ |
if (ralrm > 0) /* report init stats */ |
187 |
+ |
report(); |
188 |
+ |
#ifndef BSD |
189 |
+ |
else |
190 |
+ |
#endif |
191 |
+ |
signal(SIGALRM, report); |
192 |
|
ypos = vresolu-1 - i; |
193 |
< |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
193 |
> |
fillscanline(scanbar[0], zbar[0], sampdens, hresolu, ypos, hstep); |
194 |
|
/* compute scanlines */ |
195 |
< |
for (ypos -= psample; ypos >= 0; ypos -= psample) { |
196 |
< |
|
197 |
< |
pctdone = 100.0*(vresolu-ypos-psample)/vresolu; |
198 |
< |
|
199 |
< |
colptr = scanbar[psample]; /* move base to top */ |
200 |
< |
scanbar[psample] = scanbar[0]; |
195 |
> |
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
196 |
> |
/* bottom adjust? */ |
197 |
> |
if (ypos < 0) { |
198 |
> |
ystep += ypos; |
199 |
> |
ypos = 0; |
200 |
> |
} |
201 |
> |
colptr = scanbar[ystep]; /* move base to top */ |
202 |
> |
scanbar[ystep] = scanbar[0]; |
203 |
|
scanbar[0] = colptr; |
204 |
< |
zptr = zbar[psample]; |
205 |
< |
zbar[psample] = zbar[0]; |
204 |
> |
zptr = zbar[ystep]; |
205 |
> |
zbar[ystep] = zbar[0]; |
206 |
|
zbar[0] = zptr; |
207 |
|
/* fill base line */ |
208 |
< |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
208 |
> |
fillscanline(scanbar[0], zbar[0], sampdens, |
209 |
> |
hresolu, ypos, hstep); |
210 |
|
/* fill bar */ |
211 |
< |
fillscanbar(scanbar, zbar, hresolu, ypos, psample); |
211 |
> |
fillscanbar(scanbar, zbar, hresolu, ypos, ystep); |
212 |
|
/* write it out */ |
213 |
< |
for (i = psample; i > 0; i--) { |
214 |
< |
if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu) |
213 |
> |
#ifndef BSD |
214 |
> |
signal(SIGALRM, SIG_IGN); /* don't interrupt writes */ |
215 |
> |
#endif |
216 |
> |
for (i = ystep; i > 0; i--) { |
217 |
> |
if (zfd != -1 && write(zfd, (char *)zbar[i], |
218 |
> |
hresolu*sizeof(float)) |
219 |
> |
< hresolu*sizeof(float)) |
220 |
|
goto writerr; |
221 |
< |
if (fwritescan(scanbar[i],hresolu,stdout) < 0) |
221 |
> |
if (fwritescan(scanbar[i], hresolu, stdout) < 0) |
222 |
|
goto writerr; |
223 |
|
} |
167 |
– |
if (zfp != NULL && fflush(zfp) == EOF) |
168 |
– |
goto writerr; |
224 |
|
if (fflush(stdout) == EOF) |
225 |
|
goto writerr; |
226 |
+ |
/* record progress */ |
227 |
+ |
pctdone = 100.0*(vresolu-1-ypos)/vresolu; |
228 |
+ |
if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) |
229 |
+ |
report(); |
230 |
+ |
#ifndef BSD |
231 |
+ |
else |
232 |
+ |
signal(SIGALRM, report); |
233 |
+ |
#endif |
234 |
|
} |
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 |
– |
} |
235 |
|
/* clean up */ |
236 |
< |
if (zfp != NULL) { |
237 |
< |
if (fclose(zfp) == EOF) |
236 |
> |
signal(SIGALRM, SIG_IGN); |
237 |
> |
if (zfd != -1) { |
238 |
> |
if (write(zfd, (char *)zbar[0], hresolu*sizeof(float)) |
239 |
> |
< hresolu*sizeof(float)) |
240 |
|
goto writerr; |
241 |
+ |
if (close(zfd) == -1) |
242 |
+ |
goto writerr; |
243 |
|
for (i = 0; i <= psample; i++) |
244 |
|
free((char *)zbar[i]); |
245 |
|
} |
246 |
+ |
fwritescan(scanbar[0], hresolu, stdout); |
247 |
|
if (fflush(stdout) == EOF) |
248 |
|
goto writerr; |
249 |
|
for (i = 0; i <= psample; i++) |
250 |
|
free((char *)scanbar[i]); |
251 |
+ |
if (sampdens != NULL) |
252 |
+ |
free(sampdens); |
253 |
|
pctdone = 100.0; |
254 |
|
return; |
255 |
|
writerr: |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
< |
fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */ |
262 |
> |
fillscanline(scanline, zline, sd, xres, y, xstep) /* fill scan at y */ |
263 |
|
register COLOR *scanline; |
264 |
|
register float *zline; |
265 |
+ |
register char *sd; |
266 |
|
int xres, y, xstep; |
267 |
|
{ |
268 |
< |
int b = xstep; |
268 |
> |
static int nc = 0; /* number of calls */ |
269 |
> |
int bl = xstep, b = xstep; |
270 |
|
double z; |
271 |
|
register int i; |
272 |
|
|
273 |
|
z = pixvalue(scanline[0], 0, y); |
274 |
|
if (zline) zline[0] = z; |
275 |
< |
|
276 |
< |
for (i = xstep; i < xres; i += xstep) { |
277 |
< |
|
275 |
> |
/* zig-zag start for quincunx pattern */ |
276 |
> |
for (i = ++nc & 1 ? xstep : xstep/2; i < xres-1+xstep; i += xstep) { |
277 |
> |
if (i >= xres) { |
278 |
> |
xstep += xres-1-i; |
279 |
> |
i = xres-1; |
280 |
> |
} |
281 |
|
z = pixvalue(scanline[i], i, y); |
282 |
|
if (zline) zline[i] = z; |
283 |
< |
|
284 |
< |
b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, |
285 |
< |
i-xstep, y, xstep, 0, b/2); |
283 |
> |
if (sd) b = sd[0] > sd[1] ? sd[0] : sd[1]; |
284 |
> |
if (i <= xstep) |
285 |
> |
b = fillsample(scanline, zline, 0, y, i, 0, b/2); |
286 |
> |
else |
287 |
> |
b = fillsample(scanline+i-xstep, |
288 |
> |
zline ? zline+i-xstep : NULL, |
289 |
> |
i-xstep, y, xstep, 0, b/2); |
290 |
> |
if (sd) *sd++ = nc & 1 ? bl : b; |
291 |
> |
bl = b; |
292 |
|
} |
293 |
< |
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 |
< |
} |
293 |
> |
if (sd && nc & 1) *sd = bl; |
294 |
|
} |
295 |
|
|
296 |
|
|
302 |
|
COLOR vline[MAXDIV+1]; |
303 |
|
float zline[MAXDIV+1]; |
304 |
|
int b = ysize; |
246 |
– |
double z; |
305 |
|
register int i, j; |
306 |
|
|
307 |
|
for (i = 0; i < xres; i++) { |
385 |
|
COLOR col; /* returned color */ |
386 |
|
int x, y; /* pixel position */ |
387 |
|
{ |
388 |
< |
static RAY thisray; /* our ray for this pixel */ |
388 |
> |
static RAY thisray; |
389 |
|
|
390 |
< |
viewray(thisray.rorg, thisray.rdir, &ourview, |
391 |
< |
(x+pixjitter())/hresolu, (y+pixjitter())/vresolu); |
390 |
> |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
391 |
> |
(x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) { |
392 |
> |
setcolor(col, 0.0, 0.0, 0.0); |
393 |
> |
return(0.0); |
394 |
> |
} |
395 |
|
|
396 |
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
397 |
< |
|
397 |
> |
|
398 |
> |
samplendx = pixnumber(x,y,hresolu,vresolu); /* set pixel index */ |
399 |
> |
|
400 |
|
rayvalue(&thisray); /* trace ray */ |
401 |
|
|
402 |
|
copycolor(col, thisray.rcol); /* return color */ |
403 |
|
|
404 |
< |
return(thisray.rot); /* return distance */ |
404 |
> |
return(thisray.rt); /* return distance */ |
405 |
|
} |
406 |
|
|
407 |
|
|
424 |
|
/* discard header */ |
425 |
|
getheader(fp, NULL); |
426 |
|
/* get picture size */ |
427 |
< |
if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) { |
427 |
> |
if (!fscnresolu(&x, &y, fp)) { |
428 |
|
sprintf(errmsg, "bad recover file \"%s\"", oldfile); |
429 |
|
error(WARNING, errmsg); |
430 |
|
fclose(fp); |
454 |
|
return(y); |
455 |
|
writerr: |
456 |
|
error(SYSTEM, "write error in salvage"); |
457 |
+ |
} |
458 |
+ |
|
459 |
+ |
|
460 |
+ |
int |
461 |
+ |
pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */ |
462 |
+ |
register int x, y; |
463 |
+ |
int xres, yres; |
464 |
+ |
{ |
465 |
+ |
x -= y; |
466 |
+ |
while (x < 0) |
467 |
+ |
x += xres; |
468 |
+ |
return((((x>>2)*yres + y) << 2) + (x & 3)); |
469 |
|
} |