ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 1.27
Committed: Wed Jun 19 16:36:34 1991 UTC (32 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.26: +1 -0 lines
Log Message:
added virtual sources

File Contents

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