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 "random.h" |
26 |
|
|
27 |
+ |
int dimlist[MAXDIM]; /* sampling dimensions */ |
28 |
+ |
int ndims = 0; /* number of sampling dimensions */ |
29 |
+ |
int samplendx; /* sample index number */ |
30 |
+ |
|
31 |
|
VIEW ourview = STDVIEW; /* view parameters */ |
32 |
|
int hresolu = 512; /* horizontal resolution */ |
33 |
|
int vresolu = 512; /* vertical resolution */ |
40 |
|
double dstrsrc = 0.0; /* square source distribution */ |
41 |
|
double shadthresh = .05; /* shadow threshold */ |
42 |
|
double shadcert = .5; /* shadow certainty */ |
43 |
+ |
int directrelay = 1; /* number of source relays */ |
44 |
+ |
int vspretest = 512; /* virtual source pretest density */ |
45 |
+ |
int directinvis = 0; /* sources invisible? */ |
46 |
+ |
double srcsizerat = .25; /* maximum ratio source size/dist. */ |
47 |
|
|
48 |
|
int maxdepth = 6; /* maximum recursion depth */ |
49 |
|
double minweight = 5e-3; /* minimum ray weight */ |
61 |
|
|
62 |
|
double pctdone = 0.0; /* percentage done */ |
63 |
|
|
64 |
+ |
long tlastrept = 0L; /* time at last report */ |
65 |
+ |
|
66 |
+ |
extern long time(); |
67 |
+ |
extern long tstart; /* starting time */ |
68 |
+ |
|
69 |
|
extern long nrays; /* number of rays traced */ |
70 |
|
|
71 |
< |
#define MAXDIV 32 /* maximum sample size */ |
71 |
> |
#define MAXDIV 15 /* maximum sample size */ |
72 |
|
|
73 |
|
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
74 |
|
|
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
+ |
#ifdef BSD |
89 |
|
report() /* report progress */ |
90 |
|
{ |
76 |
– |
#ifdef BSD |
91 |
|
struct rusage rubuf; |
92 |
|
double t; |
93 |
|
|
100 |
|
|
101 |
|
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", |
102 |
|
nrays, pctdone, t/3600.0); |
103 |
+ |
eputs(errmsg); |
104 |
+ |
tlastrept = time((long *)0); |
105 |
+ |
} |
106 |
|
#else |
107 |
+ |
report() /* report progress */ |
108 |
+ |
{ |
109 |
|
signal(SIGALRM, report); |
110 |
< |
sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone); |
111 |
< |
#endif |
110 |
> |
tlastrept = time((long *)0); |
111 |
> |
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f hours\n", |
112 |
> |
nrays, pctdone, (tlastrept-tstart)/3600.0); |
113 |
|
eputs(errmsg); |
94 |
– |
|
95 |
– |
if (ralrm > 0) |
96 |
– |
alarm(ralrm); |
114 |
|
} |
115 |
+ |
#endif |
116 |
|
|
117 |
|
|
118 |
|
render(zfile, oldfile) /* render the scene */ |
119 |
|
char *zfile, *oldfile; |
120 |
|
{ |
121 |
+ |
extern long lseek(); |
122 |
|
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
123 |
|
float *zbar[MAXDIV+1]; /* z values */ |
124 |
|
int ypos; /* current scanline */ |
125 |
|
int ystep; /* current y step size */ |
126 |
< |
FILE *zfp; |
126 |
> |
int hstep; /* h step size */ |
127 |
> |
int zfd; |
128 |
|
COLOR *colptr; |
129 |
|
float *zptr; |
130 |
|
register int i; |
131 |
|
/* check sampling */ |
132 |
|
if (psample < 1) |
133 |
|
psample = 1; |
134 |
< |
else if (psample > MAXDIV) |
134 |
> |
else if (psample > MAXDIV) { |
135 |
> |
sprintf(errmsg, "pixel sampling reduced from %d to %d", |
136 |
> |
psample, MAXDIV); |
137 |
> |
error(WARNING, errmsg); |
138 |
|
psample = MAXDIV; |
139 |
+ |
} |
140 |
|
/* allocate scanlines */ |
141 |
|
for (i = 0; i <= psample; i++) { |
142 |
|
scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); |
145 |
|
} |
146 |
|
/* open z file */ |
147 |
|
if (zfile != NULL) { |
148 |
< |
if ((zfp = fopen(zfile, "a+")) == NULL) { |
148 |
> |
if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { |
149 |
|
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
150 |
|
error(SYSTEM, errmsg); |
151 |
|
} |
155 |
|
goto memerr; |
156 |
|
} |
157 |
|
} else { |
158 |
< |
zfp = NULL; |
158 |
> |
zfd = -1; |
159 |
|
for (i = 0; i <= psample; i++) |
160 |
|
zbar[i] = NULL; |
161 |
|
} |
163 |
|
fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); |
164 |
|
/* recover file and compute first */ |
165 |
|
i = salvage(oldfile); |
166 |
< |
if (zfp != NULL && fseek(zfp, (long)i*hresolu*sizeof(float), 0) == EOF) |
166 |
> |
if (zfd != -1 && i > 0 && |
167 |
> |
lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1) |
168 |
|
error(SYSTEM, "z file seek error in render"); |
169 |
+ |
pctdone = 100.0*i/vresolu; |
170 |
+ |
if (ralrm > 0) /* report init stats */ |
171 |
+ |
report(); |
172 |
+ |
#ifndef BSD |
173 |
+ |
else |
174 |
+ |
#endif |
175 |
+ |
signal(SIGALRM, report); |
176 |
|
ypos = vresolu-1 - i; |
177 |
< |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
178 |
< |
ystep = psample; |
177 |
> |
hstep = (psample*140+49)/99; /* quincunx sampling */ |
178 |
> |
ystep = (psample*99+70)/140; |
179 |
> |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, hstep); |
180 |
|
/* compute scanlines */ |
181 |
|
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
149 |
– |
/* record progress */ |
150 |
– |
pctdone = 100.0*(vresolu-ypos-ystep)/vresolu; |
182 |
|
/* bottom adjust? */ |
183 |
|
if (ypos < 0) { |
184 |
|
ystep += ypos; |
191 |
|
zbar[ystep] = zbar[0]; |
192 |
|
zbar[0] = zptr; |
193 |
|
/* fill base line */ |
194 |
< |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
194 |
> |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, hstep); |
195 |
|
/* fill bar */ |
196 |
|
fillscanbar(scanbar, zbar, hresolu, ypos, ystep); |
197 |
|
/* write it out */ |
198 |
+ |
#ifndef BSD |
199 |
+ |
signal(SIGALRM, SIG_IGN); /* don't interrupt writes */ |
200 |
+ |
#endif |
201 |
|
for (i = ystep; i > 0; i--) { |
202 |
< |
if (zfp != NULL && fwrite(zbar[i],sizeof(float),hresolu,zfp) != hresolu) |
202 |
> |
if (zfd != -1 && write(zfd, (char *)zbar[i], |
203 |
> |
hresolu*sizeof(float)) |
204 |
> |
< hresolu*sizeof(float)) |
205 |
|
goto writerr; |
206 |
< |
if (fwritescan(scanbar[i],hresolu,stdout) < 0) |
206 |
> |
if (fwritescan(scanbar[i], hresolu, stdout) < 0) |
207 |
|
goto writerr; |
208 |
|
} |
173 |
– |
if (zfp != NULL && fflush(zfp) == EOF) |
174 |
– |
goto writerr; |
209 |
|
if (fflush(stdout) == EOF) |
210 |
|
goto writerr; |
211 |
+ |
/* record progress */ |
212 |
+ |
pctdone = 100.0*(vresolu-1-ypos)/vresolu; |
213 |
+ |
if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm) |
214 |
+ |
report(); |
215 |
+ |
#ifndef BSD |
216 |
+ |
else |
217 |
+ |
signal(SIGALRM, report); |
218 |
+ |
#endif |
219 |
|
} |
220 |
|
/* clean up */ |
221 |
< |
if (zfp != NULL) { |
222 |
< |
fwrite(zbar[0], sizeof(float), hresolu, zfp); |
223 |
< |
if (fclose(zfp) == EOF) |
221 |
> |
if (zfd != -1) { |
222 |
> |
if (write(zfd, (char *)zbar[0], hresolu*sizeof(float)) |
223 |
> |
< hresolu*sizeof(float)) |
224 |
|
goto writerr; |
225 |
+ |
if (close(zfd) == -1) |
226 |
+ |
goto writerr; |
227 |
|
for (i = 0; i <= psample; i++) |
228 |
|
free((char *)zbar[i]); |
229 |
|
} |
246 |
|
register float *zline; |
247 |
|
int xres, y, xstep; |
248 |
|
{ |
249 |
+ |
static int nc = 0; /* number of calls */ |
250 |
|
int b = xstep; |
251 |
|
double z; |
252 |
|
register int i; |
253 |
|
|
254 |
|
z = pixvalue(scanline[0], 0, y); |
255 |
|
if (zline) zline[0] = z; |
256 |
< |
|
257 |
< |
for (i = xstep; i < xres-1+xstep; i += xstep) { |
256 |
> |
/* zig-zag start for quincunx pattern */ |
257 |
> |
for (i = nc++ & 1 ? xstep/2 : xstep; i < xres-1+xstep; i += xstep) { |
258 |
|
if (i >= xres) { |
259 |
|
xstep += xres-1-i; |
260 |
|
i = xres-1; |
276 |
|
COLOR vline[MAXDIV+1]; |
277 |
|
float zline[MAXDIV+1]; |
278 |
|
int b = ysize; |
234 |
– |
double z; |
279 |
|
register int i, j; |
280 |
|
|
281 |
|
for (i = 0; i < xres; i++) { |
359 |
|
COLOR col; /* returned color */ |
360 |
|
int x, y; /* pixel position */ |
361 |
|
{ |
362 |
< |
static RAY thisray; /* our ray for this pixel */ |
362 |
> |
static RAY thisray; |
363 |
|
|
364 |
< |
viewray(thisray.rorg, thisray.rdir, &ourview, |
365 |
< |
(x+pixjitter())/hresolu, (y+pixjitter())/vresolu); |
364 |
> |
if (viewray(thisray.rorg, thisray.rdir, &ourview, |
365 |
> |
(x+pixjitter())/hresolu, (y+pixjitter())/vresolu) < 0) { |
366 |
> |
setcolor(col, 0.0, 0.0, 0.0); |
367 |
> |
return(0.0); |
368 |
> |
} |
369 |
|
|
370 |
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
371 |
< |
|
371 |
> |
|
372 |
> |
samplendx = pixnumber(x,y,hresolu,vresolu); /* set pixel index */ |
373 |
> |
|
374 |
|
rayvalue(&thisray); /* trace ray */ |
375 |
|
|
376 |
|
copycolor(col, thisray.rcol); /* return color */ |
377 |
|
|
378 |
< |
return(thisray.rot); /* return distance */ |
378 |
> |
return(thisray.rt); /* return distance */ |
379 |
|
} |
380 |
|
|
381 |
|
|
428 |
|
return(y); |
429 |
|
writerr: |
430 |
|
error(SYSTEM, "write error in salvage"); |
431 |
+ |
} |
432 |
+ |
|
433 |
+ |
|
434 |
+ |
int |
435 |
+ |
pixnumber(x, y, xres, yres) /* compute pixel index (brushed) */ |
436 |
+ |
register int x, y; |
437 |
+ |
int xres, yres; |
438 |
+ |
{ |
439 |
+ |
x -= y; |
440 |
+ |
while (x < 0) |
441 |
+ |
x += xres; |
442 |
+ |
return((((x>>2)*yres + y) << 2) + (x & 3)); |
443 |
|
} |