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.2 by greg, Mon Apr 10 09:32:55 1989 UTC vs.
Revision 1.17 by greg, Fri Jan 19 00:00:19 1990 UTC

# 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  <sys/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 52 | Line 59 | extern long  nrays;                    /* number of rays traced */
59  
60   #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
61  
62 + double  pixvalue();
63  
64 +
65   quit(code)                      /* quit program */
66   int  code;
67   {
# Line 79 | Line 88 | report()               /* report progress */
88          sprintf(errmsg, "%ld rays, %4.2f%% done after %5.4f CPU hours\n",
89                          nrays, pctdone, t/3600.0);
90   #else
91 +        signal(SIGALRM, report);
92          sprintf(errmsg, "%ld rays, %4.2f%% done\n", nrays, pctdone);
93   #endif
94          eputs(errmsg);
# Line 88 | Line 98 | report()               /* report progress */
98   }
99  
100  
101 < render(oldfile)                         /* render the scene */
102 < char  *oldfile;
101 > render(zfile, oldfile)                          /* render the scene */
102 > char  *zfile, *oldfile;
103   {
104 +        extern long  lseek();
105          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
106 +        float  *zbar[MAXDIV+1];         /* z values */
107          int  ypos;                      /* current scanline */
108 +        int  ystep;                     /* current y step size */
109 +        int  zfd;
110          COLOR  *colptr;
111 +        float  *zptr;
112          register int  i;
113 <
113 >                                        /* check sampling */
114          if (psample < 1)
115                  psample = 1;
116          else if (psample > MAXDIV)
117                  psample = MAXDIV;
118 <
104 <        ourview.hresolu -= ourview.hresolu % psample;
105 <        ourview.vresolu -= ourview.vresolu % psample;
106 <                
118 >                                        /* allocate scanlines */
119          for (i = 0; i <= psample; i++) {
120 <                scanbar[i] = (COLOR *)malloc((ourview.hresolu+1)*sizeof(COLOR));
120 >                scanbar[i] = (COLOR *)malloc(hresolu*sizeof(COLOR));
121                  if (scanbar[i] == NULL)
122 <                        error(SYSTEM, "out of memory in render");
122 >                        goto memerr;
123          }
124 <        
124 >                                        /* open z file */
125 >        if (zfile != NULL) {
126 >                if ((zfd = open(zfile, O_WRONLY|O_CREAT, 0666)) == -1) {
127 >                        sprintf(errmsg, "cannot open z file \"%s\"", zfile);
128 >                        error(SYSTEM, errmsg);
129 >                }
130 >                for (i = 0; i <= psample; i++) {
131 >                        zbar[i] = (float *)malloc(hresolu*sizeof(float));
132 >                        if (zbar[i] == NULL)
133 >                                goto memerr;
134 >                }
135 >        } else {
136 >                zfd = -1;
137 >                for (i = 0; i <= psample; i++)
138 >                        zbar[i] = NULL;
139 >        }
140                                          /* write out boundaries */
141 <        printf("-Y %d +X %d\n", ourview.vresolu, ourview.hresolu);
142 <
143 <        ypos = ourview.vresolu - salvage(oldfile);      /* find top line */
144 <        fillscanline(scanbar[0], ypos, psample);        /* top scan */
145 <
146 <        for (ypos -= psample; ypos > -psample; ypos -= psample) {
147 <        
148 <                colptr = scanbar[psample];              /* get last scanline */
149 <                scanbar[psample] = scanbar[0];
141 >        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
142 >                                        /* recover file and compute first */
143 >        i = salvage(oldfile);
144 >        if (zfd != -1 && lseek(zfd, (long)i*hresolu*sizeof(float), 0) == -1)
145 >                error(SYSTEM, "z file seek error in render");
146 >        ypos = vresolu-1 - i;
147 >        fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
148 >        ystep = psample;
149 >                                                /* compute scanlines */
150 >        for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
151 >                                                        /* record progress */
152 >                pctdone = 100.0*(vresolu-ypos-ystep)/vresolu;
153 >                                                        /* bottom adjust? */
154 >                if (ypos < 0) {
155 >                        ystep += ypos;
156 >                        ypos = 0;
157 >                }
158 >                colptr = scanbar[ystep];                /* move base to top */
159 >                scanbar[ystep] = scanbar[0];
160                  scanbar[0] = colptr;
161 <
162 <                fillscanline(scanbar[0], ypos, psample);        /* base scan */
163 <        
164 <                fillscanbar(scanbar, ypos, psample);
165 <                
166 <                for (i = psample-1; i >= 0; i--)
167 <                        if (fwritescan(scanbar[i], ourview.hresolu, stdout) < 0)
161 >                zptr = zbar[ystep];
162 >                zbar[ystep] = zbar[0];
163 >                zbar[0] = zptr;
164 >                                                        /* fill base line */
165 >                fillscanline(scanbar[0], zbar[0], hresolu, ypos, psample);
166 >                                                        /* fill bar */
167 >                fillscanbar(scanbar, zbar, hresolu, ypos, ystep);
168 >                                                        /* write it out */
169 >                for (i = ystep; i > 0; i--) {
170 >                        if (zfd != -1 && write(zfd, (char *)zbar[i],
171 >                                        hresolu*sizeof(float))
172 >                                        < hresolu*sizeof(float))
173                                  goto writerr;
174 +                        if (fwritescan(scanbar[i], hresolu, stdout) < 0)
175 +                                goto writerr;
176 +                }
177                  if (fflush(stdout) == EOF)
178                          goto writerr;
134                pctdone = 100.0*(ourview.vresolu-ypos)/ourview.vresolu;
179          }
180 <                
180 >                                                /* clean up */
181 >        if (zfd != -1) {
182 >                if (write(zfd, (char *)zbar[0], hresolu*sizeof(float))
183 >                                < hresolu*sizeof(float))
184 >                        goto writerr;
185 >                close(zfd);
186 >                for (i = 0; i <= psample; i++)
187 >                        free((char *)zbar[i]);
188 >        }
189 >        fwritescan(scanbar[0], hresolu, stdout);
190 >        if (fflush(stdout) == EOF)
191 >                goto writerr;
192          for (i = 0; i <= psample; i++)
193                  free((char *)scanbar[i]);
194 +        pctdone = 100.0;
195          return;
196   writerr:
197          error(SYSTEM, "write error in render");
198 + memerr:
199 +        error(SYSTEM, "out of memory in render");
200   }
201  
202  
203 < fillscanline(scanline, y, xstep)                /* fill scan line at y */
203 > fillscanline(scanline, zline, xres, y, xstep)   /* fill scan line at y */
204   register COLOR  *scanline;
205 < int  y, xstep;
205 > register float  *zline;
206 > int  xres, y, xstep;
207   {
208          int  b = xstep;
209 +        double  z;
210          register int  i;
211          
212 <        pixvalue(scanline[0], 0, y);
212 >        z = pixvalue(scanline[0], 0, y);
213 >        if (zline) zline[0] = z;
214  
215 <        for (i = xstep; i <= ourview.hresolu; i += xstep) {
216 <        
217 <                pixvalue(scanline[i], i, y);
215 >        for (i = xstep; i < xres-1+xstep; i += xstep) {
216 >                if (i >= xres) {
217 >                        xstep += xres-1-i;
218 >                        i = xres-1;
219 >                }
220 >                z = pixvalue(scanline[i], i, y);
221 >                if (zline) zline[i] = z;
222                  
223 <                b = fillsample(scanline+i-xstep, i-xstep, y, xstep, 0, b/2);
223 >                b = fillsample(scanline+i-xstep, zline ? zline+i-xstep : NULL,
224 >                                i-xstep, y, xstep, 0, b/2);
225          }
226   }
227  
228  
229 < fillscanbar(scanbar, y, ysize)          /* fill interior */
229 > fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
230   register COLOR  *scanbar[];
231 < int  y, ysize;
231 > register float  *zbar[];
232 > int  xres, y, ysize;
233   {
234          COLOR  vline[MAXDIV+1];
235 +        float  zline[MAXDIV+1];
236          int  b = ysize;
237 +        double  z;
238          register int  i, j;
239          
240 <        for (i = 0; i < ourview.hresolu; i++) {
240 >        for (i = 0; i < xres; i++) {
241                  
242                  copycolor(vline[0], scanbar[0][i]);
243                  copycolor(vline[ysize], scanbar[ysize][i]);
244 +                if (zbar[0]) {
245 +                        zline[0] = zbar[0][i];
246 +                        zline[ysize] = zbar[ysize][i];
247 +                }
248                  
249 <                b = fillsample(vline, i, y, 0, ysize, b/2);
249 >                b = fillsample(vline, zbar[0] ? zline : NULL,
250 >                                i, y, 0, ysize, b/2);
251                  
252                  for (j = 1; j < ysize; j++)
253                          copycolor(scanbar[j][i], vline[j]);
254 +                if (zbar[0])
255 +                        for (j = 1; j < ysize; j++)
256 +                                zbar[j][i] = zline[j];
257          }
258   }
259  
260  
261   int
262 < fillsample(colline, x, y, xlen, ylen, b)        /* fill interior points */
262 > fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
263   register COLOR  *colline;
264 + register float  *zline;
265   int  x, y;
266   int  xlen, ylen;
267   int  b;
268   {
269 +        extern double  fabs();
270          double  ratio;
271 +        double  z;
272          COLOR  ctmp;
273          int  ncut;
274          register int  len;
# Line 201 | Line 281 | int  b;
281          if (len <= 1)                   /* limit recursion */
282                  return(0);
283          
284 <        if (b > 0 || bigdiff(colline[0], colline[len], maxdiff)) {
284 >        if (b > 0
285 >        || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
286 >                        || bigdiff(colline[0], colline[len], maxdiff)) {
287          
288 <                pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
288 >                z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
289 >                if (zline) zline[len>>1] = z;
290                  ncut = 1;
291                  
292          } else {                                        /* interpolate */
# Line 211 | Line 294 | int  b;
294                  copycolor(colline[len>>1], colline[len]);
295                  ratio = (double)(len>>1) / len;
296                  scalecolor(colline[len>>1], ratio);
297 <                copycolor(ctmp, colline[0]);
297 >                if (zline) zline[len>>1] = zline[len] * ratio;
298                  ratio = 1.0 - ratio;
299 +                copycolor(ctmp, colline[0]);
300                  scalecolor(ctmp, ratio);
301                  addcolor(colline[len>>1], ctmp);
302 +                if (zline) zline[len>>1] += zline[0] * ratio;
303                  ncut = 0;
304          }
305                                                          /* recurse */
306 <        ncut += fillsample(colline, x, y, xlen>>1, ylen>>1, (b-1)/2);
306 >        ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
307          
308 <        ncut += fillsample(colline + (len>>1), x + (xlen>>1), y + (ylen>>1),
309 <                                xlen - (xlen>>1), ylen - (ylen>>1), b/2);
308 >        ncut += fillsample(colline+(len>>1), zline ? zline+(len>>1) : NULL,
309 >                        x+(xlen>>1), y+(ylen>>1),
310 >                        xlen-(xlen>>1), ylen-(ylen>>1), b/2);
311  
312          return(ncut);
313   }
314  
315  
316 + double
317   pixvalue(col, x, y)             /* compute pixel value */
318   COLOR  col;                     /* returned color */
319   int  x, y;                      /* pixel position */
320   {
321          static RAY  thisray;    /* our ray for this pixel */
322  
323 <        rayview(thisray.rorg, thisray.rdir, &ourview,
324 <                        x + pixjitter(), y + pixjitter());
323 >        viewray(thisray.rorg, thisray.rdir, &ourview,
324 >                        (x+pixjitter())/hresolu, (y+pixjitter())/vresolu);
325  
326          rayorigin(&thisray, NULL, PRIMARY, 1.0);
327          
328          rayvalue(&thisray);                     /* trace ray */
329  
330          copycolor(col, thisray.rcol);           /* return color */
331 +        
332 +        return(thisray.rot);                    /* return distance */
333   }
334  
335  
# Line 254 | Line 343 | char  *oldfile;
343  
344          if (oldfile == NULL)
345                  return(0);
346 <        else if ((fp = fopen(oldfile, "r")) == NULL) {
346 >        
347 >        if ((fp = fopen(oldfile, "r")) == NULL) {
348                  sprintf(errmsg, "cannot open recover file \"%s\"", oldfile);
349                  error(WARNING, errmsg);
350                  return(0);
# Line 262 | Line 352 | char  *oldfile;
352                                  /* discard header */
353          getheader(fp, NULL);
354                                  /* get picture size */
355 <        if (fscanf(fp, "-Y %d +X %d\n", &y, &x) != 2) {
355 >        if (fgetresolu(&x, &y, fp) != (YMAJOR|YDECR)) {
356                  sprintf(errmsg, "bad recover file \"%s\"", oldfile);
357                  error(WARNING, errmsg);
358                  fclose(fp);
359                  return(0);
360          }
361  
362 <        if (x != ourview.hresolu || y != ourview.vresolu) {
362 >        if (x != hresolu || y != vresolu) {
363                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
364                                  oldfile);
365                  error(USER, errmsg);
276                return(0);
366          }
367  
368 <        scanline = (COLR *)malloc(ourview.hresolu*sizeof(COLR));
368 >        scanline = (COLR *)malloc(hresolu*sizeof(COLR));
369          if (scanline == NULL)
370                  error(SYSTEM, "out of memory in salvage");
371 <        for (y = 0; y < ourview.vresolu; y++) {
372 <                if (freadcolrs(scanline, ourview.hresolu, fp) < 0)
371 >        for (y = 0; y < vresolu; y++) {
372 >                if (freadcolrs(scanline, hresolu, fp) < 0)
373                          break;
374 <                if (fwritecolrs(scanline, ourview.hresolu, stdout) < 0)
374 >                if (fwritecolrs(scanline, hresolu, stdout) < 0)
375                          goto writerr;
376          }
377          if (fflush(stdout) == EOF)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines