ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rpict.c
(Generate patch)

Comparing ray/src/rt/rpict.c (file contents):
Revision 1.1 by greg, Thu Feb 2 10:41:36 1989 UTC vs.
Revision 1.24 by greg, Tue Apr 9 09:31:01 1991 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines