ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 1.35
Committed: Mon Oct 21 12:57:57 1991 UTC (32 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.34: +2 -1 lines
Log Message:
added source sampling (-ds option)

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