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

# User Rev Content
1 greg 1.24 /* Copyright (c) 1991 Regents of the University of California */
2 greg 1.1
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.25 int dimlist[MAXDIM]; /* sampling dimensions */
28     int ndims = 0; /* number of sampling dimensions */
29     int samplendx; /* sample index number */
30    
31 greg 1.12 VIEW ourview = STDVIEW; /* view parameters */
32     int hresolu = 512; /* horizontal resolution */
33     int vresolu = 512; /* vertical resolution */
34 greg 1.13 double pixaspect = 1.0; /* pixel aspect ratio */
35 greg 1.1
36     int psample = 4; /* pixel sample size */
37     double maxdiff = .05; /* max. difference for interpolation */
38 greg 1.3 double dstrpix = 0.67; /* square pixel distribution */
39 greg 1.1
40     double dstrsrc = 0.0; /* square source distribution */
41 greg 1.4 double shadthresh = .05; /* shadow threshold */
42 greg 1.5 double shadcert = .5; /* shadow certainty */
43 greg 1.27 int directrelay = 0; /* number of source relays */
44 greg 1.1
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 greg 1.6 int ambres = 32; /* ambient resolution */
51 greg 1.1 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 greg 1.24 long tlastrept = 0L; /* time at last report */
62    
63     extern long time();
64     extern long tstart; /* starting time */
65    
66 greg 1.1 extern long nrays; /* number of rays traced */
67    
68 greg 1.26 #define MAXDIV 15 /* maximum sample size */
69 greg 1.1
70     #define pixjitter() (.5+dstrpix*(.5-frandom()))
71    
72 greg 1.11 double pixvalue();
73 greg 1.1
74 greg 1.11
75 greg 1.1 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 greg 1.24 #ifdef BSD
86 greg 1.1 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 greg 1.24 eputs(errmsg);
101     tlastrept = time((long *)0);
102     }
103 greg 1.1 #else
104 greg 1.24 report() /* report progress */
105     {
106 greg 1.14 signal(SIGALRM, report);
107 greg 1.24 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 greg 1.1 eputs(errmsg);
111     }
112 greg 1.24 #endif
113 greg 1.1
114    
115 greg 1.11 render(zfile, oldfile) /* render the scene */
116     char *zfile, *oldfile;
117 greg 1.1 {
118 greg 1.16 extern long lseek();
119 greg 1.1 COLOR *scanbar[MAXDIV+1]; /* scanline arrays of pixel values */
120 greg 1.11 float *zbar[MAXDIV+1]; /* z values */
121 greg 1.1 int ypos; /* current scanline */
122 greg 1.15 int ystep; /* current y step size */
123 greg 1.16 int zfd;
124 greg 1.1 COLOR *colptr;
125 greg 1.11 float *zptr;
126 greg 1.1 register int i;
127 greg 1.10 /* check sampling */
128     if (psample < 1)
129 greg 1.1 psample = 1;
130 greg 1.26 else if (psample > MAXDIV) {
131     sprintf(errmsg, "pixel sampling reduced from %d to %d",
132     psample, MAXDIV);
133     error(WARNING, errmsg);
134 greg 1.10 psample = MAXDIV;
135 greg 1.26 }
136 greg 1.9 /* allocate scanlines */
137 greg 1.1 for (i = 0; i <= psample; i++) {
138 greg 1.12 scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
139 greg 1.1 if (scanbar[i] == NULL)
140 greg 1.11 goto memerr;
141 greg 1.1 }
142 greg 1.11 /* open z file */
143     if (zfile != NULL) {
144 greg 1.16 if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
145 greg 1.11 sprintf(errmsg, "cannot open z file \"%s\"", zfile);
146     error(SYSTEM, errmsg);
147     }
148     for (i = 0; i <= psample; i++) {
149 greg 1.12 zbar[i] = (float *)malloc(hresolu*sizeof(float));
150 greg 1.11 if (zbar[i] == NULL)
151     goto memerr;
152     }
153     } else {
154 greg 1.16 zfd = -1;
155 greg 1.11 for (i = 0; i <= psample; i++)
156     zbar[i] = NULL;
157     }
158 greg 1.1 /* write out boundaries */
159 greg 1.12 fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
160 greg 1.10 /* recover file and compute first */
161 greg 1.11 i = salvage(oldfile);
162 greg 1.23 if (zfd != -1 && i > 0 &&
163     lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
164 greg 1.11 error(SYSTEM, "z file seek error in render");
165 greg 1.24 pctdone = 100.0*i/vresolu;
166     if (ralrm > 0) /* report init stats */
167     report();
168 greg 1.12 ypos = vresolu-1 - i;
169     fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
170 greg 1.15 ystep = psample;
171 greg 1.10 /* compute scanlines */
172 greg 1.15 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 greg 1.1 scanbar[0] = colptr;
181 greg 1.15 zptr = zbar[ystep];
182     zbar[ystep] = zbar[0];
183 greg 1.11 zbar[0] = zptr;
184 greg 1.10 /* fill base line */
185 greg 1.12 fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
186 greg 1.10 /* fill bar */
187 greg 1.15 fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
188 greg 1.10 /* write it out */
189 greg 1.15 for (i = ystep; i > 0; i--) {
190 greg 1.17 if (zfd != -1 && write(zfd, (char *)zbar[i],
191     hresolu*sizeof(float))
192 greg 1.16 < hresolu*sizeof(float))
193 greg 1.1 goto writerr;
194 greg 1.17 if (fwritescan(scanbar[i], hresolu, stdout) < 0)
195 greg 1.11 goto writerr;
196     }
197 greg 1.1 if (fflush(stdout) == EOF)
198     goto writerr;
199 greg 1.24 /* record progress */
200     pctdone = 100.0*(vresolu-1-ypos)/vresolu;
201     if (ralrm > 0 && time((long *)0) >= tlastrept+ralrm)
202     report();
203 greg 1.1 }
204 greg 1.11 /* clean up */
205 greg 1.16 if (zfd != -1) {
206 greg 1.17 if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
207 greg 1.16 < hresolu*sizeof(float))
208 greg 1.11 goto writerr;
209 greg 1.20 if (close(zfd) == -1)
210     goto writerr;
211 greg 1.11 for (i = 0; i <= psample; i++)
212     free((char *)zbar[i]);
213     }
214 greg 1.15 fwritescan(scanbar[0], hresolu, stdout);
215 greg 1.10 if (fflush(stdout) == EOF)
216     goto writerr;
217 greg 1.1 for (i = 0; i <= psample; i++)
218     free((char *)scanbar[i]);
219 greg 1.11 pctdone = 100.0;
220 greg 1.1 return;
221     writerr:
222     error(SYSTEM, "write error in render");
223 greg 1.11 memerr:
224     error(SYSTEM, "out of memory in render");
225 greg 1.1 }
226    
227    
228 greg 1.11 fillscanline(scanline, zline, xres, y, xstep) /* fill scan line at y */
229 greg 1.1 register COLOR *scanline;
230 greg 1.11 register float *zline;
231 greg 1.9 int xres, y, xstep;
232 greg 1.1 {
233     int b = xstep;
234 greg 1.11 double z;
235 greg 1.1 register int i;
236    
237 greg 1.11 z = pixvalue(scanline[0], 0, y);
238     if (zline) zline[0] = z;
239 greg 1.1
240 greg 1.15 for (i = xstep; i < xres-1+xstep; i += xstep) {
241     if (i >= xres) {
242     xstep += xres-1-i;
243     i = xres-1;
244     }
245 greg 1.11 z = pixvalue(scanline[i], i, y);
246     if (zline) zline[i] = z;
247 greg 1.1
248 greg 1.11 b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
249     i-xstep, y, xstep, 0, b/2);
250 greg 1.1 }
251     }
252    
253    
254 greg 1.11 fillscanbar(scanbar, zbar, xres, y, ysize) /* fill interior */
255 greg 1.1 register COLOR *scanbar[];
256 greg 1.11 register float *zbar[];
257 greg 1.9 int xres, y, ysize;
258 greg 1.1 {
259     COLOR vline[MAXDIV+1];
260 greg 1.11 float zline[MAXDIV+1];
261 greg 1.1 int b = ysize;
262     register int i, j;
263    
264 greg 1.8 for (i = 0; i < xres; i++) {
265 greg 1.1
266     copycolor(vline[0], scanbar[0][i]);
267     copycolor(vline[ysize], scanbar[ysize][i]);
268 greg 1.11 if (zbar[0]) {
269     zline[0] = zbar[0][i];
270     zline[ysize] = zbar[ysize][i];
271     }
272 greg 1.1
273 greg 1.11 b = fillsample(vline, zbar[0] ? zline : NULL,
274     i, y, 0, ysize, b/2);
275 greg 1.1
276     for (j = 1; j < ysize; j++)
277     copycolor(scanbar[j][i], vline[j]);
278 greg 1.11 if (zbar[0])
279     for (j = 1; j < ysize; j++)
280     zbar[j][i] = zline[j];
281 greg 1.1 }
282     }
283    
284    
285     int
286 greg 1.11 fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
287 greg 1.1 register COLOR *colline;
288 greg 1.11 register float *zline;
289 greg 1.1 int x, y;
290     int xlen, ylen;
291     int b;
292     {
293 greg 1.11 extern double fabs();
294 greg 1.1 double ratio;
295 greg 1.11 double z;
296 greg 1.1 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 greg 1.11 if (b > 0
309     || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
310     || bigdiff(colline[0], colline[len], maxdiff)) {
311 greg 1.1
312 greg 1.11 z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
313     if (zline) zline[len>>1] = z;
314 greg 1.1 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 greg 1.11 if (zline) zline[len>>1] = zline[len] * ratio;
322     ratio = 1.0 - ratio;
323 greg 1.1 copycolor(ctmp, colline[0]);
324     scalecolor(ctmp, ratio);
325     addcolor(colline[len>>1], ctmp);
326 greg 1.11 if (zline) zline[len>>1] += zline[0] * ratio;
327 greg 1.1 ncut = 0;
328     }
329     /* recurse */
330 greg 1.11 ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
331 greg 1.1
332 greg 1.11 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 greg 1.1
336     return(ncut);
337     }
338    
339    
340 greg 1.11 double
341 greg 1.1 pixvalue(col, x, y) /* compute pixel value */
342     COLOR col; /* returned color */
343     int x, y; /* pixel position */
344     {
345 greg 1.25 static RAY thisray;
346 greg 1.1
347 greg 1.22 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 greg 1.1
353     rayorigin(&thisray, NULL, PRIMARY, 1.0);
354 greg 1.25
355     samplendx = 3*y + x; /* set pixel index */
356    
357 greg 1.1 rayvalue(&thisray); /* trace ray */
358    
359     copycolor(col, thisray.rcol); /* return color */
360 greg 1.11
361 greg 1.20 return(thisray.rt); /* return distance */
362 greg 1.1 }
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 greg 1.9
376     if ((fp = fopen(oldfile, "r")) == NULL) {
377 greg 1.1 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 greg 1.7 if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
385 greg 1.2 sprintf(errmsg, "bad recover file \"%s\"", oldfile);
386     error(WARNING, errmsg);
387 greg 1.1 fclose(fp);
388     return(0);
389     }
390    
391 greg 1.12 if (x != hresolu || y != vresolu) {
392 greg 1.1 sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
393     oldfile);
394     error(USER, errmsg);
395     }
396    
397 greg 1.12 scanline = (COLR *)malloc(hresolu*sizeof(COLR));
398 greg 1.1 if (scanline == NULL)
399     error(SYSTEM, "out of memory in salvage");
400 greg 1.12 for (y = 0; y < vresolu; y++) {
401     if (freadcolrs(scanline, hresolu, fp) < 0)
402 greg 1.1 break;
403 greg 1.12 if (fwritecolrs(scanline, hresolu, stdout) < 0)
404 greg 1.1 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     }