1 |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* rpict.c - routines and variables for picture generation. |
9 |
* |
10 |
* 8/14/85 |
11 |
*/ |
12 |
|
13 |
#include "ray.h" |
14 |
|
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; /* 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 */ |
33 |
double dstrpix = 0.67; /* square pixel distribution */ |
34 |
|
35 |
double dstrsrc = 0.0; /* square source distribution */ |
36 |
double shadthresh = .05; /* shadow threshold */ |
37 |
double shadcert = .5; /* shadow certainty */ |
38 |
|
39 |
int maxdepth = 6; /* maximum recursion depth */ |
40 |
double minweight = 5e-3; /* minimum ray weight */ |
41 |
|
42 |
COLOR ambval = BLKCOLOR; /* ambient value */ |
43 |
double ambacc = 0.2; /* ambient accuracy */ |
44 |
int ambres = 32; /* ambient resolution */ |
45 |
int ambdiv = 128; /* ambient divisions */ |
46 |
int ambssamp = 0; /* ambient super-samples */ |
47 |
int ambounce = 0; /* ambient bounces */ |
48 |
char *amblist[128]; /* ambient include/exclude list */ |
49 |
int ambincl = -1; /* include == 1, exclude == 0 */ |
50 |
|
51 |
int ralrm = 0; /* seconds between reports */ |
52 |
|
53 |
double pctdone = 0.0; /* percentage done */ |
54 |
|
55 |
extern long nrays; /* number of rays traced */ |
56 |
|
57 |
#define MAXDIV 32 /* maximum sample size */ |
58 |
|
59 |
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
60 |
|
61 |
double pixvalue(); |
62 |
|
63 |
|
64 |
quit(code) /* quit program */ |
65 |
int code; |
66 |
{ |
67 |
if (code || ralrm > 0) /* report status */ |
68 |
report(); |
69 |
|
70 |
exit(code); |
71 |
} |
72 |
|
73 |
|
74 |
report() /* report progress */ |
75 |
{ |
76 |
#ifdef BSD |
77 |
struct rusage rubuf; |
78 |
double t; |
79 |
|
80 |
getrusage(RUSAGE_SELF, &rubuf); |
81 |
t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
82 |
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
83 |
getrusage(RUSAGE_CHILDREN, &rubuf); |
84 |
t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
85 |
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
86 |
|
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); |
94 |
|
95 |
if (ralrm > 0) |
96 |
alarm(ralrm); |
97 |
} |
98 |
|
99 |
|
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 |
FILE *zfp; |
107 |
COLOR *colptr; |
108 |
float *zptr; |
109 |
register int i; |
110 |
/* check sampling */ |
111 |
if (psample < 1) |
112 |
psample = 1; |
113 |
else if (psample > MAXDIV) |
114 |
psample = MAXDIV; |
115 |
/* allocate scanlines */ |
116 |
for (i = 0; i <= psample; i++) { |
117 |
scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); |
118 |
if (scanbar[i] == NULL) |
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, 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*(vresolu-ypos-psample)/vresolu; |
149 |
|
150 |
colptr = scanbar[psample]; /* move base to top */ |
151 |
scanbar[psample] = scanbar[0]; |
152 |
scanbar[0] = colptr; |
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 |
/* 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, 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 |
z = pixvalue(scanline[0], 0, y); |
219 |
if (zline) zline[0] = z; |
220 |
|
221 |
for (i = xstep; i < xres; i += xstep) { |
222 |
|
223 |
z = pixvalue(scanline[i], i, y); |
224 |
if (zline) zline[i] = z; |
225 |
|
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, 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, 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, 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; |
284 |
|
285 |
if (xlen > 0) /* x or y length is zero */ |
286 |
len = xlen; |
287 |
else |
288 |
len = ylen; |
289 |
|
290 |
if (len <= 1) /* limit recursion */ |
291 |
return(0); |
292 |
|
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 |
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 */ |
302 |
|
303 |
copycolor(colline[len>>1], colline[len]); |
304 |
ratio = (double)(len>>1) / len; |
305 |
scalecolor(colline[len>>1], ratio); |
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, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
316 |
|
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 |
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 |
|
345 |
int |
346 |
salvage(oldfile) /* salvage scanlines from killed program */ |
347 |
char *oldfile; |
348 |
{ |
349 |
COLR *scanline; |
350 |
FILE *fp; |
351 |
int x, y; |
352 |
|
353 |
if (oldfile == NULL) |
354 |
return(0); |
355 |
|
356 |
if ((fp = fopen(oldfile, "r")) == NULL) { |
357 |
sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); |
358 |
error(WARNING, errmsg); |
359 |
return(0); |
360 |
} |
361 |
/* discard header */ |
362 |
getheader(fp, NULL); |
363 |
/* get picture size */ |
364 |
if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) { |
365 |
sprintf(errmsg, "bad recover file \"%s\"", oldfile); |
366 |
error(WARNING, errmsg); |
367 |
fclose(fp); |
368 |
return(0); |
369 |
} |
370 |
|
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(hresolu*sizeof(COLR)); |
378 |
if (scanline == NULL) |
379 |
error(SYSTEM, "out of memory in salvage"); |
380 |
for (y = 0; y < vresolu; y++) { |
381 |
if (freadcolrs(scanline, hresolu, fp) < 0) |
382 |
break; |
383 |
if (fwritecolrs(scanline, hresolu, stdout) < 0) |
384 |
goto writerr; |
385 |
} |
386 |
if (fflush(stdout) == EOF) |
387 |
goto writerr; |
388 |
free((char *)scanline); |
389 |
fclose(fp); |
390 |
unlink(oldfile); |
391 |
return(y); |
392 |
writerr: |
393 |
error(SYSTEM, "write error in salvage"); |
394 |
} |