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 2.49 by gwlarson, Tue Mar 3 19:14:28 1998 UTC vs.
Revision 2.94 by greg, Mon Jul 20 15:52:30 2020 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1996 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  rpict.c - routines and variables for picture generation.
6   */
7  
8 < #include  "ray.h"
8 > #include "copyright.h"
9  
10   #include  <sys/types.h>
11  
12 < #ifndef NIX
13 < #ifdef BSD
14 < #include  <sys/time.h>
15 < #include  <sys/resource.h>
12 > #include  "platform.h"
13 > #ifdef NON_POSIX
14 > #ifdef MINGW
15 >  #include  <sys/time.h>
16 > #endif
17   #else
18 < #include  <sys/times.h>
19 < #include  <unistd.h>
18 > #ifdef BSD
19 >  #include  <sys/time.h>
20 >  #include  <sys/resource.h>
21 > #else
22 >  #include  <sys/times.h>
23 >  #include  <unistd.h>
24 > #endif
25   #endif
23 #endif
26  
27 < extern time_t   time();
26 <
27 > #include  <time.h>
28   #include  <signal.h>
29  
30 + #include  "ray.h"
31 + #include  "paths.h"
32 + #include  "ambient.h"
33   #include  "view.h"
30
31 #include  "resolu.h"
32
34   #include  "random.h"
34
35   #include  "paths.h"
36 + #include  "hilbert.h"
37 + #include  "pmapbias.h"
38 + #include  "pmapdiag.h"
39  
40   #define  RFTEMPLATE     "rfXXXXXX"
41  
42   #ifndef SIGCONT
43 + #ifdef SIGIO     /* XXX can we live without this? */
44   #define SIGCONT         SIGIO
45   #endif
46 + #endif
47  
48 + CUBE  thescene;                         /* our scene */
49 + OBJECT  nsceneobjs;                     /* number of objects in our scene */
50 +
51   int  dimlist[MAXDIM];                   /* sampling dimensions */
52   int  ndims = 0;                         /* number of sampling dimensions */
53   int  samplendx;                         /* sample index number */
54  
55 + void  (*addobjnotify[])() = {ambnotify, NULL};
56 +
57   VIEW  ourview = STDVIEW;                /* view parameters */
58   int  hresolu = 512;                     /* horizontal resolution */
59   int  vresolu = 512;                     /* vertical resolution */
# Line 55 | Line 65 | double dstrpix = 0.67;                 /* square pixel distribution
65  
66   double  mblur = 0.;                     /* motion blur parameter */
67  
68 + double  dblur = 0.;                     /* depth-of-field blur parameter */
69 +
70 + void  (*trace)() = NULL;                /* trace call */
71 +
72 + int  do_irrad = 0;                      /* compute irradiance? */
73 +
74 + int  rand_samp = 0;                     /* pure Monte Carlo sampling? */
75 +
76   double  dstrsrc = 0.0;                  /* square source distribution */
77   double  shadthresh = .05;               /* shadow threshold */
78   double  shadcert = .5;                  /* shadow certainty */
# Line 73 | Line 91 | double specjitter = 1.;                /* specular sampling jitter *
91  
92   int  backvis = 1;                       /* back face visibility */
93  
94 < int  maxdepth = 6;                      /* maximum recursion depth */
95 < double  minweight = 5e-3;               /* minimum ray weight */
94 > int  maxdepth = 7;                      /* maximum recursion depth */
95 > double  minweight = 1e-3;               /* minimum ray weight */
96  
97 + char  *ambfile = NULL;                  /* ambient file name */
98   COLOR  ambval = BLKCOLOR;               /* ambient value */
99   int  ambvwt = 0;                        /* initial weight for ambient value */
100   double  ambacc = 0.2;                   /* ambient accuracy */
101 < int  ambres = 32;                       /* ambient resolution */
102 < int  ambdiv = 128;                      /* ambient divisions */
103 < int  ambssamp = 0;                      /* ambient super-samples */
101 > int  ambres = 64;                       /* ambient resolution */
102 > int  ambdiv = 512;                      /* ambient divisions */
103 > int  ambssamp = 128;                    /* ambient super-samples */
104   int  ambounce = 0;                      /* ambient bounces */
105 < char  *amblist[128];                    /* ambient include/exclude list */
105 > char  *amblist[AMBLLEN];                /* ambient include/exclude list */
106   int  ambincl = -1;                      /* include == 1, exclude == 0 */
107  
89 #ifdef MSDOS
90 int  ralrm = 60;                        /* seconds between reports */
91 #else
108   int  ralrm = 0;                         /* seconds between reports */
93 #endif
109  
110   double  pctdone = 0.0;                  /* percentage done */
96
111   time_t  tlastrept = 0L;                 /* time at last report */
112 + time_t  tstart;                         /* starting time */
113  
99 extern time_t  tstart;                  /* starting time */
100
101 extern unsigned long  nrays;            /* number of rays traced */
102
114   #define  MAXDIV         16              /* maximum sample size */
115  
116   #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
# Line 108 | Line 119 | int  hres, vres;                       /* resolution for this frame */
119  
120   static VIEW     lastview;               /* the previous view input */
121  
122 < extern char  *mktemp();
122 > static void report(int);
123 > static int nextview(FILE *fp);
124 > static void render(char *zfile, char *oldfile);
125 > static void fillscanline(COLOR *scanline, float *zline, char *sd, int xres,
126 >                int y, int xstep);
127 > static void fillscanbar(COLOR *scanbar[], float *zbar[], int xres,
128 >                int y, int ysize);
129 > static int fillsample(COLOR *colline, float *zline, int x, int y,
130 >                int xlen, int ylen, int b);
131 > static double pixvalue(COLOR  col, int  x, int  y);
132 > static int salvage(char  *oldfile);
133 > static int pixnumber(int  x, int  y, int  xres, int  yres);
134  
113 double  pixvalue();
135  
115 #ifdef NIX
116 #define  file_exists(f) (access(f,F_OK)==0)
117 #else
118 #include  <sys/types.h>
119 #include  <sys/stat.h>
120 int
121 file_exists(fname)                              /* ordinary file exists? */
122 char  *fname;
123 {
124        struct stat  sbuf;
125        if (stat(fname, &sbuf) < 0) return(0);
126        return((sbuf.st_mode & S_IFREG) != 0);
127 }
128 #endif
136  
137 <
138 < quit(code)                      /* quit program */
132 < int  code;
137 > void
138 > quit(int code)                  /* quit program */
139   {
140          if (code)                       /* report status */
141 <                report();
141 >                report(0);
142 > #ifndef NON_POSIX
143          headclean();                    /* delete header file */
144          pfclean();                      /* clean up persist files */
145 + #endif
146          exit(code);
147   }
148  
149  
150 < #ifndef NIX
151 < report()                /* report progress */
150 > #ifndef NON_POSIX
151 > static void
152 > report(int dummy)               /* report progress */
153   {
154 <        extern char  *myhostname();
155 <        double  u, s;
154 >        char                    bcStat [128];
155 >        double          u, s;
156   #ifdef BSD
157 <        struct rusage  rubuf;
157 >        struct rusage   rubuf;
158   #else
159 <        struct tms  tbuf;
160 <        double  period;
159 >        double          period = 1.0 / 60.0;
160 >        struct tms      tbuf;
161   #endif
162  
163          tlastrept = time((time_t *)NULL);
164   #ifdef BSD
165          getrusage(RUSAGE_SELF, &rubuf);
166 <        u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
167 <        s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
166 >        u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6;
167 >        s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6;
168          getrusage(RUSAGE_CHILDREN, &rubuf);
169 <        u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
170 <        s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
169 >        u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6;
170 >        s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6;
171   #else
172          times(&tbuf);
173   #ifdef _SC_CLK_TCK
174          period = 1.0 / sysconf(_SC_CLK_TCK);
166 #else
167        period = 1.0 / 60.0;
175   #endif
176          u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period;
177          s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period;
178   #endif
179  
180 +        /* PMAP: Get photon map bias compensation statistics */
181 +        pmapBiasCompReport(bcStat);
182 +        
183          sprintf(errmsg,
184 <                "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n",
185 <                        nrays, pctdone, u/3600., s/3600.,
186 <                        (tlastrept-tstart)/3600., myhostname());
184 >                        "%lu rays, %s%4.2f%% after %.3fu %.3fs %.3fr hours on %s (PID %d)\n",
185 >                        nrays, bcStat, pctdone, u*(1./3600.), s*(1./3600.),
186 >                        (tlastrept-tstart)*(1./3600.), myhostname(), getpid());
187          eputs(errmsg);
188 < #ifndef BSD
188 > #ifdef SIGCONT
189          signal(SIGCONT, report);
190   #endif
191   }
192   #else
193 < report()                /* report progress */
193 > static void
194 > report(int dummy)               /* report progress */
195   {
196 +        char    bcStat [128];
197 +        
198          tlastrept = time((time_t *)NULL);
199 <        sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n",
200 <                        nrays, pctdone, (tlastrept-tstart)/3600.0);
199 >
200 >        /* PMAP: Get photon map bias compensation statistics */
201 >        pmapBiasCompReport(bcStat);
202 >        
203 >        sprintf(errmsg, "%lu rays, %s%4.2f%% after %5.4f hours\n",
204 >                        nrays, bcStat, pctdone, (tlastrept-tstart)/3600.0);
205          eputs(errmsg);
206   }
207   #endif
208  
209  
210 < rpict(seq, pout, zout, prvr)                    /* generate image(s) */
211 < int  seq;
212 < char  *pout, *zout, *prvr;
210 > void
211 > rpict(                  /* generate image(s) */
212 >        int  seq,
213 >        char  *pout,
214 >        char  *zout,
215 >        char  *prvr
216 > )
217   /*
218   * If seq is greater than zero, then we will render a sequence of
219   * images based on view parameter strings read from the standard input.
# Line 205 | Line 226 | char  *pout, *zout, *prvr;
226   * sequenced file naming.
227   */
228   {
208        extern char  *rindex(), *strncpy(), *strcat(), *strcpy();
229          char  fbuf[128], fbuf2[128];
230          int  npicts;
231 <        register char  *cp;
231 >        char  *cp;
232          RESOLU  rs;
233          double  pa;
234                                          /* check sampling */
# Line 224 | Line 244 | char  *pout, *zout, *prvr;
244          if (seq <= 0)
245                  seq = 0;
246          else if (prvr != NULL && isint(prvr)) {
247 <                register int  rn;               /* skip to specified view */
247 >                int  rn;                /* skip to specified view */
248                  if ((rn = atoi(prvr)) < seq)
249                          error(USER, "recover frame less than start frame");
250                  if (pout == NULL)
# Line 232 | Line 252 | char  *pout, *zout, *prvr;
252                  for ( ; seq < rn; seq++)
253                          if (nextview(stdin) == EOF)
254                                  error(USER, "unexpected EOF on view input");
255 +                setview(&ourview);
256                  prvr = fbuf;                    /* mark for renaming */
257          }
258 <        if (pout != NULL & prvr != NULL) {
258 >        if ((pout != NULL) & (prvr != NULL)) {
259                  sprintf(fbuf, pout, seq);
260                  if (!strcmp(prvr, fbuf)) {      /* rename */
261                          strcpy(fbuf2, fbuf);
# Line 244 | Line 265 | char  *pout, *zout, *prvr;
265                                  cp--;
266                          strcpy(cp, RFTEMPLATE);
267                          prvr = mktemp(fbuf2);
268 <                        if (rename(fbuf, prvr) < 0)
268 >                        if (rename(fbuf, prvr) < 0) {
269                                  if (errno == ENOENT) {  /* ghost file */
270                                          sprintf(errmsg,
271                                                  "new output file \"%s\"",
# Line 257 | Line 278 | char  *pout, *zout, *prvr;
278                                                  fbuf, prvr);
279                                          error(SYSTEM, errmsg);
280                                  }
281 +                        }
282                  }
283          }
284          npicts = 0;                     /* render sequence */
# Line 265 | Line 287 | char  *pout, *zout, *prvr;
287                          break;
288                  pctdone = 0.0;
289                  if (pout != NULL) {
290 +                        int     myfd;
291 +                        close(1);                       /* reassign stdout */
292                          sprintf(fbuf, pout, seq);
293 <                        if (file_exists(fbuf)) {
294 <                                if (prvr != NULL || !strcmp(fbuf, pout)) {
293 >                tryagain:
294 >                        errno = 0;                      /* exclusive open */
295 >                        if ((myfd = open(fbuf, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) {
296 >                                if ((errno != EEXIST) | (prvr != NULL) ||
297 >                                                !strcmp(fbuf, pout)) {
298                                          sprintf(errmsg,
299 <                                                "output file \"%s\" exists",
299 >                                                "cannot open output file \"%s\"",
300                                                  fbuf);
301 <                                        error(USER, errmsg);
301 >                                        error(SYSTEM, errmsg);
302                                  }
303 +                                setview(&ourview);
304                                  continue;               /* don't clobber */
305                          }
306 <                        if (freopen(fbuf, "w", stdout) == NULL) {
307 <                                sprintf(errmsg,
308 <                                        "cannot open output file \"%s\"", fbuf);
281 <                                error(SYSTEM, errmsg);
306 >                        if (myfd != 1) {
307 >                                unlink(fbuf);
308 >                                goto tryagain;          /* leave it open */
309                          }
310 < #ifdef MSDOS
284 <                        setmode(fileno(stdout), O_BINARY);
285 < #endif
310 >                        SET_FD_BINARY(1);
311                          dupheader();
312                  }
313                  hres = hresolu; vres = vresolu; pa = pixaspect;
314 <                if (prvr != NULL)
315 <                        if (viewfile(prvr, &ourview, &rs) <= 0
291 <                                        || rs.or != PIXSTANDARD) {
314 >                if (prvr != NULL) {
315 >                        if (viewfile(prvr, &ourview, &rs) <= 0) {
316                                  sprintf(errmsg,
317                          "cannot recover view parameters from \"%s\"", prvr);
318                                  error(WARNING, errmsg);
# Line 297 | Line 321 | char  *pout, *zout, *prvr;
321                                  hres = scanlen(&rs);
322                                  vres = numscans(&rs);
323                          }
324 +                }
325                  if ((cp = setview(&ourview)) != NULL)
326                          error(USER, cp);
327                  normaspect(viewaspect(&ourview), &pa, &hres, &vres);
# Line 312 | Line 337 | char  *pout, *zout, *prvr;
337                  fputs(VIEWSTR, stdout);
338                  fprintview(&ourview, stdout);
339                  putchar('\n');
340 <                if (pa < .99 || pa > 1.01)
340 >                if ((pa < .99) | (pa > 1.01))
341                          fputaspect(pa, stdout);
342 +                fputnow(stdout);
343                  fputformat(COLRFMT, stdout);
344                  putchar('\n');
345                  if (zout != NULL)
# Line 330 | Line 356 | char  *pout, *zout, *prvr;
356   }
357  
358  
359 < nextview(fp)                            /* get next view from fp */
360 < FILE  *fp;
359 > static int
360 > nextview(                               /* get next view from fp */
361 >        FILE  *fp
362 > )
363   {
364          char  linebuf[256];
365  
366 <        copystruct(&lastview, &ourview);
366 >        lastview = ourview;
367          while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
368                  if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
369                          return(0);
# Line 343 | Line 371 | FILE  *fp;
371   }      
372  
373  
374 < render(zfile, oldfile)                          /* render the scene */
375 < char  *zfile, *oldfile;
374 > static void
375 > render(                         /* render the scene */
376 >        char  *zfile,
377 >        char  *oldfile
378 > )
379   {
349        extern long  lseek();
380          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
381          float  *zbar[MAXDIV+1];         /* z values */
382          char  *sampdens;                /* previous sample density */
# Line 356 | Line 386 | char  *zfile, *oldfile;
386          int  zfd;
387          COLOR  *colptr;
388          float  *zptr;
389 <        register int  i;
389 >        int  i;
390 >                                        /* check for empty image */
391 >        if ((hres <= 0) | (vres <= 0)) {
392 >                error(WARNING, "empty output picture");
393 >                fprtresolu(0, 0, stdout);
394 >                return;
395 >        }
396                                          /* allocate scanlines */
397          for (i = 0; i <= psample; i++) {
398                  scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
# Line 367 | Line 403 | char  *zfile, *oldfile;
403          ystep = (psample*99+70)/140;
404          if (hstep > 2) {
405                  i = hres/hstep + 2;
406 <                if ((sampdens = malloc(i)) == NULL)
406 >                if ((sampdens = (char *)malloc(i)) == NULL)
407                          goto memerr;
408                  while (i--)
409                          sampdens[i] = hstep;
# Line 379 | Line 415 | char  *zfile, *oldfile;
415                          sprintf(errmsg, "cannot open z-file \"%s\"", zfile);
416                          error(SYSTEM, errmsg);
417                  }
418 < #ifdef MSDOS
383 <                setmode(zfd, O_BINARY);
384 < #endif
418 >                SET_FD_BINARY(zfd);
419                  for (i = 0; i <= psample; i++) {
420                          zbar[i] = (float *)malloc(hres*sizeof(float));
421                          if (zbar[i] == NULL)
# Line 398 | Line 432 | char  *zfile, *oldfile;
432          i = salvage(oldfile);
433          if (i >= vres)
434                  goto alldone;
435 <        if (zfd != -1 && i > 0 &&
436 <                        lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
435 >        if ((zfd != -1) & (i > 0) &&
436 >                        lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0)
437                  error(SYSTEM, "z-file seek error in render");
438          pctdone = 100.0*i/vres;
439          if (ralrm > 0)                  /* report init stats */
440 <                report();
441 < #ifndef  BSD
440 >                report(0);
441 > #ifdef SIGCONT
442          else
409 #endif
443          signal(SIGCONT, report);
444 + #endif
445          ypos = vres-1 - i;                      /* initialize sampling */
446          if (directvis)
447                  init_drawsources(psample);
# Line 433 | Line 467 | char  *zfile, *oldfile;
467                  if (directvis)                          /* add bitty sources */
468                          drawsources(scanbar, zbar, 0, hres, ypos, ystep);
469                                                          /* write it out */
470 < #ifndef  BSD
470 > #ifdef SIGCONT
471                  signal(SIGCONT, SIG_IGN);       /* don't interrupt writes */
472   #endif
473                  for (i = ystep; i > 0; i--) {
# Line 449 | Line 483 | char  *zfile, *oldfile;
483                                                          /* record progress */
484                  pctdone = 100.0*(vres-1-ypos)/vres;
485                  if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm)
486 <                        report();
487 < #ifndef  BSD
486 >                        report(0);
487 > #ifdef SIGCONT
488                  else
489                          signal(SIGCONT, report);
490   #endif
491          }
492                                                  /* clean up */
493 + #ifdef SIGCONT
494          signal(SIGCONT, SIG_IGN);
495 + #endif
496          if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float))
497                                  < hres*sizeof(float))
498                  goto writerr;
# Line 468 | Line 504 | alldone:
504                  if (close(zfd) == -1)
505                          goto writerr;
506                  for (i = 0; i <= psample; i++)
507 <                        free((char *)zbar[i]);
507 >                        free((void *)zbar[i]);
508          }
509          for (i = 0; i <= psample; i++)
510 <                free((char *)scanbar[i]);
510 >                free((void *)scanbar[i]);
511          if (sampdens != NULL)
512                  free(sampdens);
513          pctdone = 100.0;
514          if (ralrm > 0)
515 <                report();
515 >                report(0);
516 > #ifdef SIGCONT
517          signal(SIGCONT, SIG_DFL);
518 + #endif
519          return;
520   writerr:
521          error(SYSTEM, "write error in render");
# Line 486 | Line 524 | memerr:
524   }
525  
526  
527 < fillscanline(scanline, zline, sd, xres, y, xstep)       /* fill scan at y */
528 < register COLOR  *scanline;
529 < register float  *zline;
530 < register char  *sd;
531 < int  xres, y, xstep;
527 > static void
528 > fillscanline(   /* fill scan at y */
529 >        COLOR   *scanline,
530 >        float   *zline,
531 >        char  *sd,
532 >        int  xres,
533 >        int  y,
534 >        int  xstep
535 > )
536   {
537          static int  nc = 0;             /* number of calls */
538          int  bl = xstep, b = xstep;
539          double  z;
540 <        register int  i;
540 >        int  i;
541  
542          z = pixvalue(scanline[0], 0, y);
543          if (zline) zline[0] = z;
# Line 521 | Line 563 | int  xres, y, xstep;
563   }
564  
565  
566 < fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
567 < register COLOR  *scanbar[];
568 < register float  *zbar[];
569 < int  xres, y, ysize;
566 > static void
567 > fillscanbar(    /* fill interior */
568 >        COLOR   *scanbar[],
569 >        float   *zbar[],
570 >        int  xres,
571 >        int  y,
572 >        int  ysize
573 > )
574   {
575          COLOR  vline[MAXDIV+1];
576          float  zline[MAXDIV+1];
577          int  b = ysize;
578 <        register int  i, j;
578 >        int  i, j;
579  
580          for (i = 0; i < xres; i++) {
581                  copycolor(vline[0], scanbar[0][i]);
# Line 550 | Line 596 | int  xres, y, ysize;
596   }
597  
598  
599 < int
600 < fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
601 < register COLOR  *colline;
602 < register float  *zline;
603 < int  x, y;
604 < int  xlen, ylen;
605 < int  b;
599 > static int
600 > fillsample( /* fill interior points */
601 >        COLOR   *colline,
602 >        float   *zline,
603 >        int  x,
604 >        int  y,
605 >        int  xlen,
606 >        int  ylen,
607 >        int  b
608 > )
609   {
610          double  ratio;
611          double  z;
612          COLOR  ctmp;
613          int  ncut;
614 <        register int  len;
614 >        int  len;
615  
616          if (xlen > 0)                   /* x or y length is zero */
617                  len = xlen;
# Line 603 | Line 652 | int  b;
652   }
653  
654  
655 < double
656 < pixvalue(col, x, y)             /* compute pixel value */
657 < COLOR  col;                     /* returned color */
658 < int  x, y;                      /* pixel position */
655 > static double
656 > pixvalue(               /* compute pixel value */
657 >        COLOR  col,                     /* returned color */
658 >        int  x,                 /* pixel position */
659 >        int  y
660 > )
661   {
662 +        extern void  SDsquare2disk(double ds[2], double seedx, double seedy);
663          RAY  thisray;
664          FVECT   lorg, ldir;
665 <        double  hpos, vpos, lmax, d;
665 >        double  hpos, vpos, vdist, lmax;
666 >        int     i;
667                                                  /* compute view ray */
668 +        setcolor(col, 0.0, 0.0, 0.0);
669          hpos = (x+pixjitter())/hres;
670          vpos = (y+pixjitter())/vres;
671          if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
672 <                                        &ourview, hpos, vpos)) < -FTINY) {
619 <                setcolor(col, 0.0, 0.0, 0.0);
672 >                                        &ourview, hpos, vpos)) < -FTINY)
673                  return(0.0);
621        }
674  
675 <        samplendx = pixnumber(x,y,hres,vres);   /* set pixel index */
676 <
675 >        vdist = ourview.vdist;
676 >                                                /* set pixel index */
677 >        samplendx = pixnumber(x,y,hres,vres);
678                                                  /* optional motion blur */
679          if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir,
680                                          &lastview, hpos, vpos)) >= -FTINY) {
681 <                register int    i;
629 <                register double  d = mblur*(.5-urand(281+samplendx));
681 >                double  d = mblur*(.5-urand(281+samplendx));
682  
683                  thisray.rmax = (1.-d)*thisray.rmax + d*lmax;
684                  for (i = 3; i--; ) {
# Line 635 | Line 687 | int  x, y;                     /* pixel position */
687                  }
688                  if (normalize(thisray.rdir) == 0.0)
689                          return(0.0);
690 +                vdist = (1.-d)*vdist + d*lastview.vdist;
691          }
692 +                                                /* optional depth-of-field */
693 +        if (dblur > FTINY) {
694 +                double  vc, df[2];
695 +                                                /* random point on disk */
696 +                SDsquare2disk(df, frandom(), frandom());
697 +                df[0] *= .5*dblur;
698 +                df[1] *= .5*dblur;
699 +                if ((ourview.type == VT_PER) | (ourview.type == VT_PAR)) {
700 +                        double  adj = 1.0;
701 +                        if (ourview.type == VT_PER)
702 +                                adj /= DOT(thisray.rdir, ourview.vdir);
703 +                        df[0] /= sqrt(ourview.hn2);
704 +                        df[1] /= sqrt(ourview.vn2);
705 +                        for (i = 3; i--; ) {
706 +                                vc = ourview.vp[i] + adj*vdist*thisray.rdir[i];
707 +                                thisray.rorg[i] += df[0]*ourview.hvec[i] +
708 +                                                        df[1]*ourview.vvec[i] ;
709 +                                thisray.rdir[i] = vc - thisray.rorg[i];
710 +                        }
711 +                } else {                        /* non-standard view case */
712 +                        double  dfd = PI/4.*dblur*(.5 - frandom());
713 +                        if ((ourview.type != VT_ANG) & (ourview.type != VT_PLS)) {
714 +                                if (ourview.type != VT_CYL)
715 +                                        df[0] /= sqrt(ourview.hn2);
716 +                                df[1] /= sqrt(ourview.vn2);
717 +                        }
718 +                        for (i = 3; i--; ) {
719 +                                vc = ourview.vp[i] + vdist*thisray.rdir[i];
720 +                                thisray.rorg[i] += df[0]*ourview.hvec[i] +
721 +                                                        df[1]*ourview.vvec[i] +
722 +                                                        dfd*ourview.vdir[i] ;
723 +                                thisray.rdir[i] = vc - thisray.rorg[i];
724 +                        }
725 +                }
726 +                if (normalize(thisray.rdir) == 0.0)
727 +                        return(0.0);
728 +        }
729  
730 <        rayorigin(&thisray, NULL, PRIMARY, 1.0);
730 >        rayorigin(&thisray, PRIMARY, NULL, NULL);
731  
732          rayvalue(&thisray);                     /* trace ray */
733  
734          copycolor(col, thisray.rcol);           /* return color */
735  
736 <        return(thisray.rt);                     /* return distance */
736 >        return(raydistance(&thisray));          /* return distance */
737   }
738  
739  
740 < int
741 < salvage(oldfile)                /* salvage scanlines from killed program */
742 < char  *oldfile;
740 > static int
741 > salvage(                /* salvage scanlines from killed program */
742 >        char  *oldfile
743 > )
744   {
745          COLR  *scanline;
746          FILE  *fp;
# Line 663 | Line 754 | char  *oldfile;
754                  error(WARNING, errmsg);
755                  goto gotzip;
756          }
757 < #ifdef MSDOS
667 <        setmode(fileno(fp), O_BINARY);
668 < #endif
757 >        SET_FILE_BINARY(fp);
758                                  /* discard header */
759          getheader(fp, NULL, NULL);
760                                  /* get picture size */
# Line 677 | Line 766 | char  *oldfile;
766                  goto gotzip;
767          }
768  
769 <        if (x != hres || y != vres) {
769 >        if ((x != hres) | (y != vres)) {
770                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
771                                  oldfile);
772                  error(USER, errmsg);
# Line 694 | Line 783 | char  *oldfile;
783          }
784          if (fflush(stdout) == EOF)
785                  goto writerr;
786 <        free((char *)scanline);
786 >        free((void *)scanline);
787          fclose(fp);
788          unlink(oldfile);
789          return(y);
# Line 705 | Line 794 | gotzip:
794   writerr:
795          sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
796          error(SYSTEM, errmsg);
797 +        return -1; /* pro forma return */
798   }
799  
800 <
801 < int
802 < pixnumber(x, y, xres, yres)             /* compute pixel index (brushed) */
803 < register int  x, y;
804 < int  xres, yres;
800 > static int
801 > pixnumber(              /* compute pixel index (screen door) */
802 >        int  x,
803 >        int  y,
804 >        int  xres,
805 >        int  yres
806 > )
807   {
808 <        x -= y;
809 <        while (x < 0)
810 <                x += xres;
811 <        return((((x>>2)*yres + y) << 2) + (x & 3));
808 >        unsigned        nbits = 0;
809 >        bitmask_t       coord[2];
810 >
811 >        if (xres < yres) xres = yres;
812 >        while (xres > 0) {
813 >                xres >>= 1;
814 >                ++nbits;
815 >        }
816 >        coord[0] = x; coord[1] = y;
817 >        return ((int)hilbert_c2i(2, nbits, coord));
818   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines