ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 1.12
Committed: Mon Jan 8 13:38:01 1990 UTC (34 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.11: +25 -25 lines
Log Message:
Changed handling of view parameters

File Contents

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