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.41 by greg, Fri Dec 8 18:49:20 1995 UTC vs.
Revision 2.107 by greg, Sat Jan 25 04:57:56 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1995 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.
9 *
10 *     8/14/85
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  <sys/utsname.h>
20 < #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
26 #endif
26  
27 < extern time_t   time();
29 <
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"
33
34 #include  "resolu.h"
35
34   #include  "random.h"
37
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 + extern RGBPRIMP  out_prims;             /* output color primitives (NULL if spectral) */
52 +
53   int  dimlist[MAXDIM];                   /* sampling dimensions */
54   int  ndims = 0;                         /* number of sampling dimensions */
55   int  samplendx;                         /* sample index number */
56  
57 + void  (*addobjnotify[])() = {ambnotify, NULL};
58 +
59   VIEW  ourview = STDVIEW;                /* view parameters */
60   int  hresolu = 512;                     /* horizontal resolution */
61   int  vresolu = 512;                     /* vertical resolution */
# Line 56 | Line 65 | int  psample = 4;                      /* pixel sample size */
65   double  maxdiff = .05;                  /* max. difference for interpolation */
66   double  dstrpix = 0.67;                 /* square pixel distribution */
67  
68 + double  mblur = 0.;                     /* motion blur parameter */
69 +
70 + double  dblur = 0.;                     /* depth-of-field blur parameter */
71 +
72 + void  (*trace)() = NULL;                /* trace call */
73 +
74 + int  do_irrad = 0;                      /* compute irradiance? */
75 +
76 + int  rand_samp = 0;                     /* pure Monte Carlo sampling? */
77 +
78   double  dstrsrc = 0.0;                  /* square source distribution */
79   double  shadthresh = .05;               /* shadow threshold */
80   double  shadcert = .5;                  /* shadow certainty */
# Line 65 | Line 84 | int  directvis = 1;                    /* sources visible? */
84   double  srcsizerat = .25;               /* maximum ratio source size/dist. */
85  
86   COLOR  cextinction = BLKCOLOR;          /* global extinction coefficient */
87 < double  salbedo = 0.;                   /* global scattering albedo */
87 > COLOR  salbedo = BLKCOLOR;              /* global scattering albedo */
88   double  seccg = 0.;                     /* global scattering eccentricity */
89   double  ssampdist = 0.;                 /* scatter sampling distance */
90  
# Line 74 | Line 93 | double specjitter = 1.;                /* specular sampling jitter *
93  
94   int  backvis = 1;                       /* back face visibility */
95  
96 < int  maxdepth = 6;                      /* maximum recursion depth */
97 < double  minweight = 5e-3;               /* minimum ray weight */
96 > int  maxdepth = 7;                      /* maximum recursion depth */
97 > double  minweight = 1e-4;               /* minimum ray weight */
98  
99 + char  *ambfile = NULL;                  /* ambient file name */
100   COLOR  ambval = BLKCOLOR;               /* ambient value */
101 + int  ambvwt = 0;                        /* initial weight for ambient value */
102   double  ambacc = 0.2;                   /* ambient accuracy */
103 < int  ambres = 32;                       /* ambient resolution */
104 < int  ambdiv = 128;                      /* ambient divisions */
105 < int  ambssamp = 0;                      /* ambient super-samples */
103 > int  ambres = 64;                       /* ambient resolution */
104 > int  ambdiv = 512;                      /* ambient divisions */
105 > int  ambssamp = 128;                    /* ambient super-samples */
106   int  ambounce = 0;                      /* ambient bounces */
107 < char  *amblist[128];                    /* ambient include/exclude list */
107 > char  *amblist[AMBLLEN];                /* ambient include/exclude list */
108   int  ambincl = -1;                      /* include == 1, exclude == 0 */
109  
89 #ifdef MSDOS
90 int  ralrm = 60;                        /* seconds between reports */
91 #else
110   int  ralrm = 0;                         /* seconds between reports */
93 #endif
111  
112   double  pctdone = 0.0;                  /* percentage done */
96
113   time_t  tlastrept = 0L;                 /* time at last report */
114 + time_t  tstart;                         /* starting time */
115  
99 extern time_t  tstart;                  /* starting time */
100
101 extern unsigned long  nrays;            /* number of rays traced */
102
116   #define  MAXDIV         16              /* maximum sample size */
117  
118   #define  pixjitter()    (.5+dstrpix*(.5-frandom()))
119  
120 < static int  hres, vres;                 /* resolution for this frame */
120 > int  hres, vres;                        /* resolution for this frame */
121  
122 < extern char  *mktemp();
122 > extern void     sskip_ray(RAY *r, double h, double v);
123  
124 < double  pixvalue();
124 > static VIEW     lastview;               /* the previous view input */
125  
126 < #ifdef NIX
127 < #define  file_exists(f) (access(f,F_OK)==0)
128 < #else
129 < #include  <sys/types.h>
130 < #include  <sys/stat.h>
131 < int
132 < file_exists(fname)                              /* ordinary file exists? */
133 < char  *fname;
134 < {
135 <        struct stat  sbuf;
136 <        if (stat(fname, &sbuf) < 0) return(0);
137 <        return((sbuf.st_mode & S_IFREG) != 0);
125 < }
126 < #endif
126 > static void report(int);
127 > static int nextview(FILE *fp);
128 > static void render(char *zfile, char *oldfile);
129 > static void fillscanline(COLOR *scanline, float *zline, char *sd, int xres,
130 >                int y, int xstep);
131 > static void fillscanbar(COLOR *scanbar[], float *zbar[], int xres,
132 >                int y, int ysize);
133 > static int fillsample(COLOR *colline, float *zline, int x, int y,
134 >                int xlen, int ylen, int b);
135 > static double pixvalue(COLOR  col, int  x, int  y);
136 > static int salvage(char  *oldfile);
137 > static int pixnumber(int  x, int  y, int  xres, int  yres);
138  
139  
140 < quit(code)                      /* quit program */
141 < int  code;
140 >
141 > void
142 > quit(int code)                  /* quit program */
143   {
144          if (code)                       /* report status */
145 <                report();
145 >                report(0);
146 > #ifndef NON_POSIX
147          headclean();                    /* delete header file */
148          pfclean();                      /* clean up persist files */
149 + #endif
150          exit(code);
151   }
152  
153  
154 < #ifndef NIX
155 < report()                /* report progress */
154 > #ifndef NON_POSIX
155 > static void
156 > report(int dummy)               /* report progress */
157   {
158 <        double  u, s;
158 >        char                    bcStat [128];
159 >        double          u, s;
160   #ifdef BSD
161 <        char  hostname[257];
146 <        struct rusage  rubuf;
161 >        struct rusage   rubuf;
162   #else
163 <        struct tms  tbuf;
164 <        struct utsname  nambuf;
150 <        double  period;
151 < #define hostname  nambuf.nodename
163 >        double          period = 1.0 / 60.0;
164 >        struct tms      tbuf;
165   #endif
166  
167          tlastrept = time((time_t *)NULL);
168   #ifdef BSD
169          getrusage(RUSAGE_SELF, &rubuf);
170 <        u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
171 <        s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
170 >        u = rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6;
171 >        s = rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6;
172          getrusage(RUSAGE_CHILDREN, &rubuf);
173 <        u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec/1e6;
174 <        s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec/1e6;
162 <        gethostname(hostname, sizeof(hostname));
173 >        u += rubuf.ru_utime.tv_sec + rubuf.ru_utime.tv_usec*1e-6;
174 >        s += rubuf.ru_stime.tv_sec + rubuf.ru_stime.tv_usec*1e-6;
175   #else
176          times(&tbuf);
177   #ifdef _SC_CLK_TCK
178          period = 1.0 / sysconf(_SC_CLK_TCK);
167 #else
168        period = 1.0 / 60.0;
179   #endif
180          u = ( tbuf.tms_utime + tbuf.tms_cutime ) * period;
181          s = ( tbuf.tms_stime + tbuf.tms_cstime ) * period;
172        uname(&nambuf);
182   #endif
183  
184 +        /* PMAP: Get photon map bias compensation statistics */
185 +        pmapBiasCompReport(bcStat);
186 +        
187          sprintf(errmsg,
188 <                "%lu rays, %4.2f%% after %.3fu %.3fs %.3fr hours on %s\n",
189 <                        nrays, pctdone, u/3600., s/3600.,
190 <                        (tlastrept-tstart)/3600., hostname);
188 >                        "%lu rays, %s%4.2f%% after %.3fu %.3fs %.3fr hours on %s (PID %d)\n",
189 >                        nrays, bcStat, pctdone, u*(1./3600.), s*(1./3600.),
190 >                        (tlastrept-tstart)*(1./3600.), myhostname(), getpid());
191          eputs(errmsg);
192 < #ifndef BSD
192 > #ifdef SIGCONT
193          signal(SIGCONT, report);
182 #undef hostname
194   #endif
195   }
196   #else
197 < report()                /* report progress */
197 > static void
198 > report(int dummy)               /* report progress */
199   {
200 +        char    bcStat [128];
201 +        
202          tlastrept = time((time_t *)NULL);
203 <        sprintf(errmsg, "%lu rays, %4.2f%% after %5.4f hours\n",
204 <                        nrays, pctdone, (tlastrept-tstart)/3600.0);
203 >
204 >        /* PMAP: Get photon map bias compensation statistics */
205 >        pmapBiasCompReport(bcStat);
206 >        
207 >        sprintf(errmsg, "%lu rays, %s%4.2f%% after %5.4f hours\n",
208 >                        nrays, bcStat, pctdone, (tlastrept-tstart)/3600.0);
209          eputs(errmsg);
210   }
211   #endif
212  
213  
214 < rpict(seq, pout, zout, prvr)                    /* generate image(s) */
215 < int  seq;
216 < char  *pout, *zout, *prvr;
214 > void
215 > rpict(                  /* generate image(s) */
216 >        int  seq,
217 >        char  *pout,
218 >        char  *zout,
219 >        char  *prvr
220 > )
221   /*
222   * If seq is greater than zero, then we will render a sequence of
223   * images based on view parameter strings read from the standard input.
# Line 208 | Line 230 | char  *pout, *zout, *prvr;
230   * sequenced file naming.
231   */
232   {
211        extern char  *rindex(), *strncpy(), *strcat(), *strcpy();
233          char  fbuf[128], fbuf2[128];
234          int  npicts;
235 <        register char  *cp;
235 >        char  *cp;
236          RESOLU  rs;
237          double  pa;
238                                          /* check sampling */
# Line 227 | Line 248 | char  *pout, *zout, *prvr;
248          if (seq <= 0)
249                  seq = 0;
250          else if (prvr != NULL && isint(prvr)) {
251 <                register int  rn;               /* skip to specified view */
251 >                int  rn;                /* skip to specified view */
252                  if ((rn = atoi(prvr)) < seq)
253                          error(USER, "recover frame less than start frame");
254                  if (pout == NULL)
# Line 235 | Line 256 | char  *pout, *zout, *prvr;
256                  for ( ; seq < rn; seq++)
257                          if (nextview(stdin) == EOF)
258                                  error(USER, "unexpected EOF on view input");
259 +                setview(&ourview);
260                  prvr = fbuf;                    /* mark for renaming */
261          }
262 <        if (pout != NULL & prvr != NULL) {
262 >        if ((pout != NULL) & (prvr != NULL)) {
263                  sprintf(fbuf, pout, seq);
264                  if (!strcmp(prvr, fbuf)) {      /* rename */
265                          strcpy(fbuf2, fbuf);
# Line 247 | Line 269 | char  *pout, *zout, *prvr;
269                                  cp--;
270                          strcpy(cp, RFTEMPLATE);
271                          prvr = mktemp(fbuf2);
272 <                        if (rename(fbuf, prvr) < 0)
272 >                        if (rename(fbuf, prvr) < 0) {
273                                  if (errno == ENOENT) {  /* ghost file */
274                                          sprintf(errmsg,
275                                                  "new output file \"%s\"",
# Line 260 | Line 282 | char  *pout, *zout, *prvr;
282                                                  fbuf, prvr);
283                                          error(SYSTEM, errmsg);
284                                  }
285 +                        }
286                  }
287          }
288          npicts = 0;                     /* render sequence */
289          do {
290                  if (seq && nextview(stdin) == EOF)
291                          break;
292 +                lastview.type *= seq > 1;
293                  pctdone = 0.0;
294                  if (pout != NULL) {
295 +                        int     myfd;
296 +                        close(1);                       /* reassign stdout */
297                          sprintf(fbuf, pout, seq);
298 <                        if (file_exists(fbuf)) {
299 <                                if (prvr != NULL || !strcmp(fbuf, pout)) {
298 >                tryagain:
299 >                        errno = 0;                      /* exclusive open */
300 >                        if ((myfd = open(fbuf, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) {
301 >                                if ((errno != EEXIST) | (prvr != NULL) ||
302 >                                                !strcmp(fbuf, pout)) {
303                                          sprintf(errmsg,
304 <                                                "output file \"%s\" exists",
304 >                                                "cannot open output file \"%s\"",
305                                                  fbuf);
306 <                                        error(USER, errmsg);
306 >                                        error(SYSTEM, errmsg);
307                                  }
308 +                                setview(&ourview);
309                                  continue;               /* don't clobber */
310                          }
311 <                        if (freopen(fbuf, "w", stdout) == NULL) {
312 <                                sprintf(errmsg,
313 <                                        "cannot open output file \"%s\"", fbuf);
284 <                                error(SYSTEM, errmsg);
311 >                        if (myfd != 1) {
312 >                                unlink(fbuf);
313 >                                goto tryagain;          /* leave it open */
314                          }
315 < #ifdef MSDOS
287 <                        setmode(fileno(stdout), O_BINARY);
288 < #endif
315 >                        SET_FD_BINARY(1);
316                          dupheader();
317                  }
318                  hres = hresolu; vres = vresolu; pa = pixaspect;
319 <                if (prvr != NULL)
320 <                        if (viewfile(prvr, &ourview, &rs) <= 0
294 <                                        || rs.or != PIXSTANDARD) {
319 >                if (prvr != NULL) {
320 >                        if (viewfile(prvr, &ourview, &rs) <= 0) {
321                                  sprintf(errmsg,
322                          "cannot recover view parameters from \"%s\"", prvr);
323                                  error(WARNING, errmsg);
# Line 300 | Line 326 | char  *pout, *zout, *prvr;
326                                  hres = scanlen(&rs);
327                                  vres = numscans(&rs);
328                          }
329 +                }
330                  if ((cp = setview(&ourview)) != NULL)
331                          error(USER, cp);
332                  normaspect(viewaspect(&ourview), &pa, &hres, &vres);
# Line 315 | Line 342 | char  *pout, *zout, *prvr;
342                  fputs(VIEWSTR, stdout);
343                  fprintview(&ourview, stdout);
344                  putchar('\n');
345 <                if (pa < .99 || pa > 1.01)
345 >                if ((pa < .99) | (pa > 1.01))
346                          fputaspect(pa, stdout);
347 <                fputformat(COLRFMT, stdout);
348 <                putchar('\n');
347 >                fputnow(stdout);
348 >                if (out_prims == xyzprims) {
349 >                        fputformat(CIEFMT, stdout);
350 >                } else {
351 >                        fputprims(out_prims, stdout);
352 >                        fputformat(COLRFMT, stdout);
353 >                }
354 >                putchar('\n');          /* close header */
355                  if (zout != NULL)
356                          sprintf(cp=fbuf, zout, seq);
357                  else
# Line 333 | Line 366 | char  *pout, *zout, *prvr;
366   }
367  
368  
369 < nextview(fp)                            /* get next view from fp */
370 < FILE  *fp;
369 > static int
370 > nextview(                               /* get next view from fp */
371 >        FILE  *fp
372 > )
373   {
374          char  linebuf[256];
375  
376 +        lastview = ourview;
377          while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
378                  if (isview(linebuf) && sscanview(&ourview, linebuf) > 0)
379                          return(0);
# Line 345 | Line 381 | FILE  *fp;
381   }      
382  
383  
384 < render(zfile, oldfile)                          /* render the scene */
385 < char  *zfile, *oldfile;
384 > static void
385 > render(                         /* render the scene */
386 >        char  *zfile,
387 >        char  *oldfile
388 > )
389   {
390 <        extern long  lseek();
390 >        const int  srcdrawing =         /* manually draw tiny light sources? */
391 >                (directvis && dblur <= FTINY && (mblur <= FTINY) | !lastview.type);
392          COLOR  *scanbar[MAXDIV+1];      /* scanline arrays of pixel values */
393          float  *zbar[MAXDIV+1];         /* z values */
394          char  *sampdens;                /* previous sample density */
# Line 358 | Line 398 | char  *zfile, *oldfile;
398          int  zfd;
399          COLOR  *colptr;
400          float  *zptr;
401 <        register int  i;
401 >        int  i;
402 >                                        /* check for empty image */
403 >        if ((hres <= 0) | (vres <= 0)) {
404 >                error(WARNING, "empty output picture");
405 >                fprtresolu(0, 0, stdout);
406 >                return;
407 >        }
408                                          /* allocate scanlines */
409          for (i = 0; i <= psample; i++) {
410                  scanbar[i] = (COLOR *)malloc(hres*sizeof(COLOR));
# Line 369 | Line 415 | char  *zfile, *oldfile;
415          ystep = (psample*99+70)/140;
416          if (hstep > 2) {
417                  i = hres/hstep + 2;
418 <                if ((sampdens = malloc(i)) == NULL)
418 >                if ((sampdens = (char *)malloc(i)) == NULL)
419                          goto memerr;
420                  while (i--)
421                          sampdens[i] = hstep;
# Line 381 | Line 427 | char  *zfile, *oldfile;
427                          sprintf(errmsg, "cannot open z-file \"%s\"", zfile);
428                          error(SYSTEM, errmsg);
429                  }
430 < #ifdef MSDOS
385 <                setmode(zfd, O_BINARY);
386 < #endif
430 >                SET_FD_BINARY(zfd);
431                  for (i = 0; i <= psample; i++) {
432                          zbar[i] = (float *)malloc(hres*sizeof(float));
433                          if (zbar[i] == NULL)
# Line 398 | Line 442 | char  *zfile, *oldfile;
442          fprtresolu(hres, vres, stdout);
443                                          /* recover file and compute first */
444          i = salvage(oldfile);
445 <        if (zfd != -1 && i > 0 &&
446 <                        lseek(zfd, (long)i*hres*sizeof(float), 0) == -1)
445 >        if (i >= vres)
446 >                goto alldone;
447 >        if ((zfd != -1) & (i > 0) &&
448 >                        lseek(zfd, (off_t)i*hres*sizeof(float), SEEK_SET) < 0)
449                  error(SYSTEM, "z-file seek error in render");
450          pctdone = 100.0*i/vres;
451          if (ralrm > 0)                  /* report init stats */
452 <                report();
453 < #ifndef  BSD
452 >                report(0);
453 > #ifdef SIGCONT
454          else
409 #endif
455          signal(SIGCONT, report);
456 <        ypos = vres-1 - i;
456 > #endif
457 >        ypos = vres-1 - i;                      /* initialize sampling */
458 >        if (srcdrawing)
459 >                init_drawsources(psample);
460          fillscanline(scanbar[0], zbar[0], sampdens, hres, ypos, hstep);
461                                                  /* compute scanlines */
462          for (ypos -= ystep; ypos > -ystep; ypos -= ystep) {
# Line 428 | Line 476 | char  *zfile, *oldfile;
476                                  hres, ypos, hstep);
477                                                          /* fill bar */
478                  fillscanbar(scanbar, zbar, hres, ypos, ystep);
479 +                if (srcdrawing)                         /* add bitty sources */
480 +                        drawsources((COLORV **)scanbar, out_prims, zbar, 0, hres, ypos, ystep);
481                                                          /* write it out */
482 < #ifndef  BSD
482 > #ifdef SIGCONT
483                  signal(SIGCONT, SIG_IGN);       /* don't interrupt writes */
484   #endif
485                  for (i = ystep; i > 0; i--) {
# Line 445 | Line 495 | char  *zfile, *oldfile;
495                                                          /* record progress */
496                  pctdone = 100.0*(vres-1-ypos)/vres;
497                  if (ralrm > 0 && time((time_t *)NULL) >= tlastrept+ralrm)
498 <                        report();
499 < #ifndef  BSD
498 >                        report(0);
499 > #ifdef SIGCONT
500                  else
501                          signal(SIGCONT, report);
502   #endif
503          }
504                                                  /* clean up */
505 + #ifdef SIGCONT
506          signal(SIGCONT, SIG_IGN);
507 <        if (zfd != -1) {
508 <                if (write(zfd, (char *)zbar[0], hres*sizeof(float))
507 > #endif
508 >        if (zfd != -1 && write(zfd, (char *)zbar[0], hres*sizeof(float))
509                                  < hres*sizeof(float))
510 <                        goto writerr;
510 >                goto writerr;
511 >        fwritescan(scanbar[0], hres, stdout);
512 >        if (fflush(stdout) == EOF)
513 >                goto writerr;
514 > alldone:
515 >        if (zfd != -1) {
516                  if (close(zfd) == -1)
517                          goto writerr;
518                  for (i = 0; i <= psample; i++)
519 <                        free((char *)zbar[i]);
519 >                        free((void *)zbar[i]);
520          }
465        fwritescan(scanbar[0], hres, stdout);
466        if (fflush(stdout) == EOF)
467                goto writerr;
521          for (i = 0; i <= psample; i++)
522 <                free((char *)scanbar[i]);
522 >                free((void *)scanbar[i]);
523          if (sampdens != NULL)
524                  free(sampdens);
525          pctdone = 100.0;
526          if (ralrm > 0)
527 <                report();
527 >                report(0);
528 > #ifdef SIGCONT
529          signal(SIGCONT, SIG_DFL);
530 + #endif
531          return;
532   writerr:
533          error(SYSTEM, "write error in render");
# Line 481 | Line 536 | memerr:
536   }
537  
538  
539 < fillscanline(scanline, zline, sd, xres, y, xstep)       /* fill scan at y */
540 < register COLOR  *scanline;
541 < register float  *zline;
542 < register char  *sd;
543 < int  xres, y, xstep;
539 > static void
540 > fillscanline(   /* fill scan at y */
541 >        COLOR   *scanline,
542 >        float   *zline,
543 >        char  *sd,
544 >        int  xres,
545 >        int  y,
546 >        int  xstep
547 > )
548   {
549          static int  nc = 0;             /* number of calls */
550          int  bl = xstep, b = xstep;
551          double  z;
552 <        register int  i;
553 <        
552 >        int  i;
553 >
554          z = pixvalue(scanline[0], 0, y);
555          if (zline) zline[0] = z;
556                                  /* zig-zag start for quincunx pattern */
# Line 516 | Line 575 | int  xres, y, xstep;
575   }
576  
577  
578 < fillscanbar(scanbar, zbar, xres, y, ysize)      /* fill interior */
579 < register COLOR  *scanbar[];
580 < register float  *zbar[];
581 < int  xres, y, ysize;
578 > static void
579 > fillscanbar(    /* fill interior */
580 >        COLOR   *scanbar[],
581 >        float   *zbar[],
582 >        int  xres,
583 >        int  y,
584 >        int  ysize
585 > )
586   {
587          COLOR  vline[MAXDIV+1];
588          float  zline[MAXDIV+1];
589          int  b = ysize;
590 <        register int  i, j;
591 <        
590 >        int  i, j;
591 >
592          for (i = 0; i < xres; i++) {
530                
593                  copycolor(vline[0], scanbar[0][i]);
594                  copycolor(vline[ysize], scanbar[ysize][i]);
595                  if (zbar[0]) {
596                          zline[0] = zbar[0][i];
597                          zline[ysize] = zbar[ysize][i];
598                  }
537                
599                  b = fillsample(vline, zbar[0] ? zline : (float *)NULL,
600                                  i, y, 0, ysize, b/2);
601 <                
601 >
602                  for (j = 1; j < ysize; j++)
603                          copycolor(scanbar[j][i], vline[j]);
604                  if (zbar[0])
# Line 547 | Line 608 | int  xres, y, ysize;
608   }
609  
610  
611 < int
612 < fillsample(colline, zline, x, y, xlen, ylen, b) /* fill interior points */
613 < register COLOR  *colline;
614 < register float  *zline;
615 < int  x, y;
616 < int  xlen, ylen;
617 < int  b;
611 > static int
612 > fillsample( /* fill interior points */
613 >        COLOR   *colline,
614 >        float   *zline,
615 >        int  x,
616 >        int  y,
617 >        int  xlen,
618 >        int  ylen,
619 >        int  b
620 > )
621   {
622          double  ratio;
623          double  z;
624          COLOR  ctmp;
625          int  ncut;
626 <        register int  len;
627 <        
626 >        int  len;
627 >
628          if (xlen > 0)                   /* x or y length is zero */
629                  len = xlen;
630          else
631                  len = ylen;
632 <                
632 >
633          if (len <= 1)                   /* limit recursion */
634                  return(0);
635 <        
636 <        if (b > 0
637 <        || (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
635 >
636 >        if (b > 0 ||
637 >        (zline && 2.*fabs(zline[0]-zline[len]) > maxdiff*(zline[0]+zline[len]))
638                          || bigdiff(colline[0], colline[len], maxdiff)) {
639 <        
639 >
640                  z = pixvalue(colline[len>>1], x + (xlen>>1), y + (ylen>>1));
641                  if (zline) zline[len>>1] = z;
642                  ncut = 1;
579                
643          } else {                                        /* interpolate */
581        
644                  copycolor(colline[len>>1], colline[len]);
645                  ratio = (double)(len>>1) / len;
646                  scalecolor(colline[len>>1], ratio);
# Line 592 | Line 654 | int  b;
654          }
655                                                          /* recurse */
656          ncut += fillsample(colline, zline, x, y, xlen>>1, ylen>>1, (b-1)/2);
657 <        
657 >
658          ncut += fillsample(colline+(len>>1),
659                          zline ? zline+(len>>1) : (float *)NULL,
660                          x+(xlen>>1), y+(ylen>>1),
# Line 602 | Line 664 | int  b;
664   }
665  
666  
667 < double
668 < pixvalue(col, x, y)             /* compute pixel value */
669 < COLOR  col;                     /* returned color */
670 < int  x, y;                      /* pixel position */
667 > static double
668 > pixvalue(               /* compute pixel value */
669 >        COLOR  col,                     /* returned color */
670 >        int  x,                 /* pixel position */
671 >        int  y
672 > )
673   {
674 <        static RAY  thisray;
675 <
676 <        if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir, &ourview,
677 <                        (x+pixjitter())/hres, (y+pixjitter())/vres)) < -FTINY) {
678 <                setcolor(col, 0.0, 0.0, 0.0);
674 >        RAY  thisray;
675 >        FVECT   lorg, ldir;
676 >        double  hpos, vpos, lmax;
677 >        int     i;
678 >                                                /* compute view ray */
679 >        setcolor(col, 0.0, 0.0, 0.0);
680 >        hpos = (x+pixjitter())/hres;
681 >        vpos = (y+pixjitter())/vres;
682 >        if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
683 >                                        &ourview, hpos, vpos)) < -FTINY)
684                  return(0.0);
685 +                                                /* set pixel index */
686 +        samplendx = pixnumber(x,y,hres,vres);
687 +                                                /* optional motion blur */
688 +        if (lastview.type && mblur > FTINY && (lmax = viewray(lorg, ldir,
689 +                                        &lastview, hpos, vpos)) >= -FTINY) {
690 +                double  d = mblur*(.5-urand(281+samplendx));
691 +
692 +                thisray.rmax = (1.-d)*thisray.rmax + d*lmax;
693 +                for (i = 3; i--; ) {
694 +                        thisray.rorg[i] = (1.-d)*thisray.rorg[i] + d*lorg[i];
695 +                        thisray.rdir[i] = (1.-d)*thisray.rdir[i] + d*ldir[i];
696 +                }
697 +                if (normalize(thisray.rdir) == 0.0)
698 +                        return(0.0);
699          }
700 +                                                /* depth-of-field */
701 +        if (!jitteraperture(thisray.rorg, thisray.rdir, &ourview, dblur))
702 +                return(0.0);
703  
704 <        rayorigin(&thisray, NULL, PRIMARY, 1.0);
704 >        rayorigin(&thisray, PRIMARY, NULL, NULL);
705  
620        samplendx = pixnumber(x,y,hres,vres);   /* set pixel index */
621
706          rayvalue(&thisray);                     /* trace ray */
707 +                                                /* -> color */
708 +        scolor_out(col, out_prims, thisray.rcol);
709  
710 <        copycolor(col, thisray.rcol);           /* return color */
625 <        
626 <        return(thisray.rt);                     /* return distance */
710 >        return(raydistance(&thisray));          /* return distance */
711   }
712  
713  
714 < int
715 < salvage(oldfile)                /* salvage scanlines from killed program */
716 < char  *oldfile;
714 > static int
715 > salvage(                /* salvage scanlines from killed program */
716 >        char  *oldfile
717 > )
718   {
719          COLR  *scanline;
720          FILE  *fp;
# Line 643 | Line 728 | char  *oldfile;
728                  error(WARNING, errmsg);
729                  goto gotzip;
730          }
731 < #ifdef MSDOS
647 <        setmode(fileno(fp), O_BINARY);
648 < #endif
731 >        SET_FILE_BINARY(fp);
732                                  /* discard header */
733          getheader(fp, NULL, NULL);
734                                  /* get picture size */
# Line 657 | Line 740 | char  *oldfile;
740                  goto gotzip;
741          }
742  
743 <        if (x != hres || y != vres) {
743 >        if ((x != hres) | (y != vres)) {
744                  sprintf(errmsg, "resolution mismatch in recover file \"%s\"",
745                                  oldfile);
746                  error(USER, errmsg);
# Line 674 | Line 757 | char  *oldfile;
757          }
758          if (fflush(stdout) == EOF)
759                  goto writerr;
760 <        free((char *)scanline);
760 >        free((void *)scanline);
761          fclose(fp);
762          unlink(oldfile);
763          return(y);
# Line 685 | Line 768 | gotzip:
768   writerr:
769          sprintf(errmsg, "write error during recovery of \"%s\"", oldfile);
770          error(SYSTEM, errmsg);
771 +        return -1; /* pro forma return */
772   }
773  
774 <
775 < int
776 < pixnumber(x, y, xres, yres)             /* compute pixel index (brushed) */
777 < register int  x, y;
778 < int  xres, yres;
774 > static int
775 > pixnumber(              /* compute pixel index (screen door) */
776 >        int  x,
777 >        int  y,
778 >        int  xres,
779 >        int  yres
780 > )
781   {
782 <        x -= y;
783 <        while (x < 0)
784 <                x += xres;
785 <        return((((x>>2)*yres + y) << 2) + (x & 3));
782 >        unsigned        nbits = 0;
783 >        bitmask_t       coord[2];
784 >
785 >        if (xres < yres) xres = yres;
786 >        while (xres > 0) {
787 >                xres >>= 1;
788 >                ++nbits;
789 >        }
790 >        coord[0] = x; coord[1] = y;
791 >        return ((int)hilbert_c2i(2, nbits, coord));
792   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines