1 |
greg |
1.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 |
greg |
1.14 |
#else |
19 |
|
|
#include <signal.h> |
20 |
greg |
1.1 |
#endif |
21 |
greg |
1.18 |
#include <fcntl.h> |
22 |
greg |
1.1 |
|
23 |
|
|
#include "view.h" |
24 |
|
|
|
25 |
|
|
#include "random.h" |
26 |
|
|
|
27 |
greg |
1.12 |
VIEW ourview = STDVIEW; /* view parameters */ |
28 |
|
|
int hresolu = 512; /* horizontal resolution */ |
29 |
|
|
int vresolu = 512; /* vertical resolution */ |
30 |
greg |
1.13 |
double pixaspect = 1.0; /* pixel aspect ratio */ |
31 |
greg |
1.1 |
|
32 |
|
|
int psample = 4; /* pixel sample size */ |
33 |
|
|
double maxdiff = .05; /* max. difference for interpolation */ |
34 |
greg |
1.3 |
double dstrpix = 0.67; /* square pixel distribution */ |
35 |
greg |
1.1 |
|
36 |
|
|
double dstrsrc = 0.0; /* square source distribution */ |
37 |
greg |
1.4 |
double shadthresh = .05; /* shadow threshold */ |
38 |
greg |
1.5 |
double shadcert = .5; /* shadow certainty */ |
39 |
greg |
1.1 |
|
40 |
|
|
int maxdepth = 6; /* maximum recursion depth */ |
41 |
|
|
double minweight = 5e-3; /* minimum ray weight */ |
42 |
|
|
|
43 |
|
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
44 |
|
|
double ambacc = 0.2; /* ambient accuracy */ |
45 |
greg |
1.6 |
int ambres = 32; /* ambient resolution */ |
46 |
greg |
1.1 |
int ambdiv = 128; /* ambient divisions */ |
47 |
|
|
int ambssamp = 0; /* ambient super-samples */ |
48 |
|
|
int ambounce = 0; /* ambient bounces */ |
49 |
|
|
char *amblist[128]; /* ambient include/exclude list */ |
50 |
|
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
51 |
|
|
|
52 |
|
|
int ralrm = 0; /* seconds between reports */ |
53 |
|
|
|
54 |
|
|
double pctdone = 0.0; /* percentage done */ |
55 |
|
|
|
56 |
|
|
extern long nrays; /* number of rays traced */ |
57 |
|
|
|
58 |
|
|
#define MAXDIV 32 /* maximum sample size */ |
59 |
|
|
|
60 |
|
|
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
61 |
|
|
|
62 |
greg |
1.11 |
double pixvalue(); |
63 |
greg |
1.1 |
|
64 |
greg |
1.11 |
|
65 |
greg |
1.1 |
quit(code) /* quit program */ |
66 |
|
|
int code; |
67 |
|
|
{ |
68 |
|
|
if (code || ralrm > 0) /* report status */ |
69 |
|
|
report(); |
70 |
|
|
|
71 |
|
|
exit(code); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
|
75 |
|
|
report() /* report progress */ |
76 |
|
|
{ |
77 |
|
|
#ifdef BSD |
78 |
|
|
struct rusage rubuf; |
79 |
|
|
double t; |
80 |
|
|
|
81 |
|
|
getrusage(RUSAGE_SELF, &rubuf); |
82 |
|
|
t = (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
83 |
|
|
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
84 |
|
|
getrusage(RUSAGE_CHILDREN, &rubuf); |
85 |
|
|
t += (rubuf.ru_utime.tv_usec + rubuf.ru_stime.tv_usec) / 1e6; |
86 |
|
|
t += rubuf.ru_utime.tv_sec + rubuf.ru_stime.tv_sec; |
87 |
|
|
|
88 |
|
|
sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n", |
89 |
|
|
nrays, pctdone, t/3600.0); |
90 |
|
|
#else |
91 |
greg |
1.14 |
signal(SIGALRM, report); |
92 |
greg |
1.1 |
sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone); |
93 |
|
|
#endif |
94 |
|
|
eputs(errmsg); |
95 |
|
|
|
96 |
|
|
if (ralrm > 0) |
97 |
|
|
alarm(ralrm); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
|
101 |
greg |
1.11 |
render(zfile, oldfile) /* render the scene */ |
102 |
|
|
char *zfile, *oldfile; |
103 |
greg |
1.1 |
{ |
104 |
greg |
1.16 |
extern long lseek(); |
105 |
greg |
1.1 |
COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */ |
106 |
greg |
1.11 |
float *zbar[MAXDIV+1]; /* z values */ |
107 |
greg |
1.1 |
int ypos; /* current scanline */ |
108 |
greg |
1.15 |
int ystep; /* current y step size */ |
109 |
greg |
1.16 |
int zfd; |
110 |
greg |
1.1 |
COLOR *colptr; |
111 |
greg |
1.11 |
float *zptr; |
112 |
greg |
1.1 |
register int i; |
113 |
greg |
1.10 |
/* check sampling */ |
114 |
|
|
if (psample < 1) |
115 |
greg |
1.1 |
psample = 1; |
116 |
greg |
1.10 |
else if (psample > MAXDIV) |
117 |
|
|
psample = MAXDIV; |
118 |
greg |
1.9 |
/* allocate scanlines */ |
119 |
greg |
1.1 |
for (i = 0; i <= psample; i++) { |
120 |
greg |
1.12 |
scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR)); |
121 |
greg |
1.1 |
if (scanbar[i] == NULL) |
122 |
greg |
1.11 |
goto memerr; |
123 |
greg |
1.1 |
} |
124 |
greg |
1.11 |
/* open z file */ |
125 |
|
|
if (zfile != NULL) { |
126 |
greg |
1.16 |
if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) { |
127 |
greg |
1.11 |
sprintf(errmsg, "cannot open z file \"%s\"", zfile); |
128 |
|
|
error(SYSTEM, errmsg); |
129 |
|
|
} |
130 |
|
|
for (i = 0; i <= psample; i++) { |
131 |
greg |
1.12 |
zbar[i] = (float *)malloc(hresolu*sizeof(float)); |
132 |
greg |
1.11 |
if (zbar[i] == NULL) |
133 |
|
|
goto memerr; |
134 |
|
|
} |
135 |
|
|
} else { |
136 |
greg |
1.16 |
zfd = -1; |
137 |
greg |
1.11 |
for (i = 0; i <= psample; i++) |
138 |
|
|
zbar[i] = NULL; |
139 |
|
|
} |
140 |
greg |
1.1 |
/* write out boundaries */ |
141 |
greg |
1.12 |
fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout); |
142 |
greg |
1.10 |
/* recover file and compute first */ |
143 |
greg |
1.11 |
i = salvage(oldfile); |
144 |
greg |
1.23 |
if (zfd != -1 && i > 0 && |
145 |
|
|
lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1) |
146 |
greg |
1.11 |
error(SYSTEM, "z file seek error in render"); |
147 |
greg |
1.12 |
ypos = vresolu-1 - i; |
148 |
|
|
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
149 |
greg |
1.15 |
ystep = psample; |
150 |
greg |
1.10 |
/* compute scanlines */ |
151 |
greg |
1.15 |
for (ypos -= ystep; ypos > -ystep; ypos -= ystep) { |
152 |
|
|
/* record progress */ |
153 |
greg |
1.19 |
pctdone = 100.0*(vresolu-1-ypos-ystep)/vresolu; |
154 |
greg |
1.15 |
/* 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 |
greg |
1.1 |
scanbar[0] = colptr; |
162 |
greg |
1.15 |
zptr = zbar[ystep]; |
163 |
|
|
zbar[ystep] = zbar[0]; |
164 |
greg |
1.11 |
zbar[0] = zptr; |
165 |
greg |
1.10 |
/* fill base line */ |
166 |
greg |
1.12 |
fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample); |
167 |
greg |
1.10 |
/* fill bar */ |
168 |
greg |
1.15 |
fillscanbar(scanbar, zbar, hresolu, ypos, ystep); |
169 |
greg |
1.10 |
/* write it out */ |
170 |
greg |
1.15 |
for (i = ystep; i > 0; i--) { |
171 |
greg |
1.17 |
if (zfd != -1 && write(zfd, (char *)zbar[i], |
172 |
|
|
hresolu*sizeof(float)) |
173 |
greg |
1.16 |
< hresolu*sizeof(float)) |
174 |
greg |
1.1 |
goto writerr; |
175 |
greg |
1.17 |
if (fwritescan(scanbar[i], hresolu, stdout) < 0) |
176 |
greg |
1.11 |
goto writerr; |
177 |
|
|
} |
178 |
greg |
1.1 |
if (fflush(stdout) == EOF) |
179 |
|
|
goto writerr; |
180 |
|
|
} |
181 |
greg |
1.11 |
/* clean up */ |
182 |
greg |
1.16 |
if (zfd != -1) { |
183 |
greg |
1.17 |
if (write(zfd, (char *)zbar[0], hresolu*sizeof(float)) |
184 |
greg |
1.16 |
< hresolu*sizeof(float)) |
185 |
greg |
1.11 |
goto writerr; |
186 |
greg |
1.20 |
if (close(zfd) == -1) |
187 |
|
|
goto writerr; |
188 |
greg |
1.11 |
for (i = 0; i <= psample; i++) |
189 |
|
|
free((char *)zbar[i]); |
190 |
|
|
} |
191 |
greg |
1.15 |
fwritescan(scanbar[0], hresolu, stdout); |
192 |
greg |
1.10 |
if (fflush(stdout) == EOF) |
193 |
|
|
goto writerr; |
194 |
greg |
1.1 |
for (i = 0; i <= psample; i++) |
195 |
|
|
free((char *)scanbar[i]); |
196 |
greg |
1.11 |
pctdone = 100.0; |
197 |
greg |
1.1 |
return; |
198 |
|
|
writerr: |
199 |
|
|
error(SYSTEM, "write error in render"); |
200 |
greg |
1.11 |
memerr: |
201 |
|
|
error(SYSTEM, "out of memory in render"); |
202 |
greg |
1.1 |
} |
203 |
|
|
|
204 |
|
|
|
205 |
greg |
1.11 |
fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */ |
206 |
greg |
1.1 |
register COLOR *scanline; |
207 |
greg |
1.11 |
register float *zline; |
208 |
greg |
1.9 |
int xres, y, xstep; |
209 |
greg |
1.1 |
{ |
210 |
|
|
int b = xstep; |
211 |
greg |
1.11 |
double z; |
212 |
greg |
1.1 |
register int i; |
213 |
|
|
|
214 |
greg |
1.11 |
z = pixvalue(scanline[0], 0, y); |
215 |
|
|
if (zline) zline[0] = z; |
216 |
greg |
1.1 |
|
217 |
greg |
1.15 |
for (i = xstep; i < xres-1+xstep; i += xstep) { |
218 |
|
|
if (i >= xres) { |
219 |
|
|
xstep += xres-1-i; |
220 |
|
|
i = xres-1; |
221 |
|
|
} |
222 |
greg |
1.11 |
z = pixvalue(scanline[i], i, y); |
223 |
|
|
if (zline) zline[i] = z; |
224 |
greg |
1.1 |
|
225 |
greg |
1.11 |
b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL, |
226 |
|
|
i-xstep, y, xstep, 0, b/2); |
227 |
greg |
1.1 |
} |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
|
231 |
greg |
1.11 |
fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */ |
232 |
greg |
1.1 |
register COLOR *scanbar[]; |
233 |
greg |
1.11 |
register float *zbar[]; |
234 |
greg |
1.9 |
int xres, y, ysize; |
235 |
greg |
1.1 |
{ |
236 |
|
|
COLOR vline[MAXDIV+1]; |
237 |
greg |
1.11 |
float zline[MAXDIV+1]; |
238 |
greg |
1.1 |
int b = ysize; |
239 |
|
|
register int i, j; |
240 |
|
|
|
241 |
greg |
1.8 |
for (i = 0; i < xres; i++) { |
242 |
greg |
1.1 |
|
243 |
|
|
copycolor(vline[0], scanbar[0][i]); |
244 |
|
|
copycolor(vline[ysize], scanbar[ysize][i]); |
245 |
greg |
1.11 |
if (zbar[0]) { |
246 |
|
|
zline[0] = zbar[0][i]; |
247 |
|
|
zline[ysize] = zbar[ysize][i]; |
248 |
|
|
} |
249 |
greg |
1.1 |
|
250 |
greg |
1.11 |
b = fillsample(vline, zbar[0] ? zline : NULL, |
251 |
|
|
i, y, 0, ysize, b/2); |
252 |
greg |
1.1 |
|
253 |
|
|
for (j = 1; j < ysize; j++) |
254 |
|
|
copycolor(scanbar[j][i], vline[j]); |
255 |
greg |
1.11 |
if (zbar[0]) |
256 |
|
|
for (j = 1; j < ysize; j++) |
257 |
|
|
zbar[j][i] = zline[j]; |
258 |
greg |
1.1 |
} |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
|
262 |
|
|
int |
263 |
greg |
1.11 |
fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */ |
264 |
greg |
1.1 |
register COLOR *colline; |
265 |
greg |
1.11 |
register float *zline; |
266 |
greg |
1.1 |
int x, y; |
267 |
|
|
int xlen, ylen; |
268 |
|
|
int b; |
269 |
|
|
{ |
270 |
greg |
1.11 |
extern double fabs(); |
271 |
greg |
1.1 |
double ratio; |
272 |
greg |
1.11 |
double z; |
273 |
greg |
1.1 |
COLOR ctmp; |
274 |
|
|
int ncut; |
275 |
|
|
register int len; |
276 |
|
|
|
277 |
|
|
if (xlen > 0) /* x or y length is zero */ |
278 |
|
|
len = xlen; |
279 |
|
|
else |
280 |
|
|
len = ylen; |
281 |
|
|
|
282 |
|
|
if (len <= 1) /* limit recursion */ |
283 |
|
|
return(0); |
284 |
|
|
|
285 |
greg |
1.11 |
if (b > 0 |
286 |
|
|
|| (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len])) |
287 |
|
|
|| bigdiff(colline[0], colline[len], maxdiff)) { |
288 |
greg |
1.1 |
|
289 |
greg |
1.11 |
z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1)); |
290 |
|
|
if (zline) zline[len>>1] = z; |
291 |
greg |
1.1 |
ncut = 1; |
292 |
|
|
|
293 |
|
|
} else { /* interpolate */ |
294 |
|
|
|
295 |
|
|
copycolor(colline[len>>1], colline[len]); |
296 |
|
|
ratio = (double)(len>>1) / len; |
297 |
|
|
scalecolor(colline[len>>1], ratio); |
298 |
greg |
1.11 |
if (zline) zline[len>>1] = zline[len] * ratio; |
299 |
|
|
ratio = 1.0 - ratio; |
300 |
greg |
1.1 |
copycolor(ctmp, colline[0]); |
301 |
|
|
scalecolor(ctmp, ratio); |
302 |
|
|
addcolor(colline[len>>1], ctmp); |
303 |
greg |
1.11 |
if (zline) zline[len>>1] += zline[0] * ratio; |
304 |
greg |
1.1 |
ncut = 0; |
305 |
|
|
} |
306 |
|
|
/* recurse */ |
307 |
greg |
1.11 |
ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2); |
308 |
greg |
1.1 |
|
309 |
greg |
1.11 |
ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL, |
310 |
|
|
x+(xlen>>1), y+(ylen>>1), |
311 |
|
|
xlen-(xlen>>1), ylen-(ylen>>1), b/2); |
312 |
greg |
1.1 |
|
313 |
|
|
return(ncut); |
314 |
|
|
} |
315 |
|
|
|
316 |
|
|
|
317 |
greg |
1.11 |
double |
318 |
greg |
1.1 |
pixvalue(col, x, y) /* compute pixel value */ |
319 |
|
|
COLOR col; /* returned color */ |
320 |
|
|
int x, y; /* pixel position */ |
321 |
|
|
{ |
322 |
|
|
static RAY thisray; /* our ray for this pixel */ |
323 |
|
|
|
324 |
greg |
1.22 |
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 |
greg |
1.1 |
|
330 |
|
|
rayorigin(&thisray, NULL, PRIMARY, 1.0); |
331 |
|
|
|
332 |
|
|
rayvalue(&thisray); /* trace ray */ |
333 |
|
|
|
334 |
|
|
copycolor(col, thisray.rcol); /* return color */ |
335 |
greg |
1.11 |
|
336 |
greg |
1.20 |
return(thisray.rt); /* return distance */ |
337 |
greg |
1.1 |
} |
338 |
|
|
|
339 |
|
|
|
340 |
|
|
int |
341 |
|
|
salvage(oldfile) /* salvage scanlines from killed program */ |
342 |
|
|
char *oldfile; |
343 |
|
|
{ |
344 |
|
|
COLR *scanline; |
345 |
|
|
FILE *fp; |
346 |
|
|
int x, y; |
347 |
|
|
|
348 |
|
|
if (oldfile == NULL) |
349 |
|
|
return(0); |
350 |
greg |
1.9 |
|
351 |
|
|
if ((fp = fopen(oldfile, "r")) == NULL) { |
352 |
greg |
1.1 |
sprintf(errmsg, "cannot open recover file \"%s\"", oldfile); |
353 |
|
|
error(WARNING, errmsg); |
354 |
|
|
return(0); |
355 |
|
|
} |
356 |
|
|
/* discard header */ |
357 |
|
|
getheader(fp, NULL); |
358 |
|
|
/* get picture size */ |
359 |
greg |
1.7 |
if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) { |
360 |
greg |
1.2 |
sprintf(errmsg, "bad recover file \"%s\"", oldfile); |
361 |
|
|
error(WARNING, errmsg); |
362 |
greg |
1.1 |
fclose(fp); |
363 |
|
|
return(0); |
364 |
|
|
} |
365 |
|
|
|
366 |
greg |
1.12 |
if (x != hresolu || y != vresolu) { |
367 |
greg |
1.1 |
sprintf(errmsg, "resolution mismatch in recover file \"%s\"", |
368 |
|
|
oldfile); |
369 |
|
|
error(USER, errmsg); |
370 |
|
|
} |
371 |
|
|
|
372 |
greg |
1.12 |
scanline = (COLR *)malloc(hresolu*sizeof(COLR)); |
373 |
greg |
1.1 |
if (scanline == NULL) |
374 |
|
|
error(SYSTEM, "out of memory in salvage"); |
375 |
greg |
1.12 |
for (y = 0; y < vresolu; y++) { |
376 |
|
|
if (freadcolrs(scanline, hresolu, fp) < 0) |
377 |
greg |
1.1 |
break; |
378 |
greg |
1.12 |
if (fwritecolrs(scanline, hresolu, stdout) < 0) |
379 |
greg |
1.1 |
goto writerr; |
380 |
|
|
} |
381 |
|
|
if (fflush(stdout) == EOF) |
382 |
|
|
goto writerr; |
383 |
|
|
free((char *)scanline); |
384 |
|
|
fclose(fp); |
385 |
|
|
unlink(oldfile); |
386 |
|
|
return(y); |
387 |
|
|
writerr: |
388 |
|
|
error(SYSTEM, "write error in salvage"); |
389 |
|
|
} |