ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
Revision: 1.14
Committed: Thu Jan 11 08:30:42 1990 UTC (34 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.13: +3 -0 lines
Log Message:
added reset to report signal for SYS V

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