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

Comparing ray/src/rt/rtrace.c (file contents):
Revision 2.19 by greg, Mon Aug 21 10:29:20 1995 UTC vs.
Revision 2.50 by greg, Fri Oct 7 03:45:14 2005 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1994 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   *  rtrace.c - program and variables for individual ray tracing.
9 *
10 *     6/11/86
6   */
7  
8 + #include "copyright.h"
9 +
10   /*
11   *  Input is in the form:
12   *
# Line 24 | Line 21 | static char SCCSid[] = "$SunId$ LBL";
21   *  irradiance values are desired.
22   */
23  
24 < #include  "ray.h"
24 > #include  <time.h>
25  
26 < #include  "octree.h"
27 <
26 > #include  "platform.h"
27 > #include  "ray.h"
28 > #include  "ambient.h"
29 > #include  "source.h"
30   #include  "otypes.h"
32
31   #include  "resolu.h"
32 + #include  "random.h"
33  
34 + CUBE  thescene;                         /* our scene */
35 + OBJECT  nsceneobjs;                     /* number of objects in our scene */
36 +
37   int  dimlist[MAXDIM];                   /* sampling dimensions */
38   int  ndims = 0;                         /* number of sampling dimensions */
39   int  samplendx = 0;                     /* index for this sample */
40  
41   int  imm_irrad = 0;                     /* compute immediate irradiance? */
42 + int  lim_dist = 0;                      /* limit distance? */
43  
44   int  inform = 'a';                      /* input format */
45   int  outform = 'a';                     /* output format */
46   char  *outvals = "v";                   /* output specification */
47  
48 + int  do_irrad = 0;                      /* compute irradiance? */
49 +
50 + int  rand_samp = 0;                     /* pure Monte Carlo sampling? */
51 +
52 + void  (*trace)() = NULL;                /* trace call */
53 +
54   char  *tralist[128];                    /* list of modifers to trace (or no) */
55   int  traincl = -1;                      /* include == 1, exclude == 0 */
56 < #define  MAXTSET        511             /* maximum number in trace set */
56 > #ifndef  MAXTSET
57 > #define  MAXTSET        1024            /* maximum number in trace set */
58 > #endif
59   OBJECT  traset[MAXTSET+1]={0};          /* trace include/exclude set */
60  
61   int  hresolu = 0;                       /* horizontal (scan) size */
62   int  vresolu = 0;                       /* vertical resolution */
63  
64   double  dstrsrc = 0.0;                  /* square source distribution */
65 < double  shadthresh = .05;               /* shadow threshold */
66 < double  shadcert = .5;                  /* shadow certainty */
65 > double  shadthresh = .03;               /* shadow threshold */
66 > double  shadcert = .75;                 /* shadow certainty */
67   int  directrelay = 2;                   /* number of source relays */
68   int  vspretest = 512;                   /* virtual source pretest density */
69   int  directvis = 1;                     /* sources visible? */
70   double  srcsizerat = .2;                /* maximum ratio source size/dist. */
71  
72 + COLOR  cextinction = BLKCOLOR;          /* global extinction coefficient */
73 + COLOR  salbedo = BLKCOLOR;              /* global scattering albedo */
74 + double  seccg = 0.;                     /* global scattering eccentricity */
75 + double  ssampdist = 0.;                 /* scatter sampling distance */
76 +
77   double  specthresh = .15;               /* specular sampling threshold */
78   double  specjitter = 1.;                /* specular sampling jitter */
79  
80   int  backvis = 1;                       /* back face visibility */
81  
82 < int  maxdepth = 6;                      /* maximum recursion depth */
83 < double  minweight = 4e-3;               /* minimum ray weight */
82 > int  maxdepth = -10;                    /* maximum recursion depth */
83 > double  minweight = 2e-3;               /* minimum ray weight */
84  
85 + char  *ambfile = NULL;                  /* ambient file name */
86   COLOR  ambval = BLKCOLOR;               /* ambient value */
87 < double  ambacc = 0.2;                   /* ambient accuracy */
88 < int  ambres = 128;                      /* ambient resolution */
89 < int  ambdiv = 512;                      /* ambient divisions */
90 < int  ambssamp = 0;                      /* ambient super-samples */
87 > int  ambvwt = 0;                        /* initial weight for ambient value */
88 > double  ambacc = 0.15;                  /* ambient accuracy */
89 > int  ambres = 256;                      /* ambient resolution */
90 > int  ambdiv = 1024;                     /* ambient divisions */
91 > int  ambssamp = 512;                    /* ambient super-samples */
92   int  ambounce = 0;                      /* ambient bounces */
93 < char  *amblist[128];                    /* ambient include/exclude list */
93 > char  *amblist[AMBLLEN];                /* ambient include/exclude list */
94   int  ambincl = -1;                      /* include == 1, exclude == 0 */
95  
96 < extern OBJREC  Lamb;                    /* a Lambertian surface */
96 > static int  castonly = 0;
97  
98   static RAY  thisray;                    /* for our convenience */
99  
100 < static int  oputo(), oputd(), oputv(), oputl(), oputL(),
101 <                oputp(), oputn(), oputN(), oputs(), oputw(), oputm();
100 > typedef void putf_t(double v);
101 > static putf_t puta, putd, putf;
102  
103 < static int  ourtrace(), tabin();
104 < static int  (*ray_out[16])(), (*every_out[16])();
105 < static int  castonly = 0;
103 > typedef void oputf_t(RAY *r);
104 > static oputf_t  oputo, oputd, oputv, oputl, oputL, oputc, oputp,
105 >                oputn, oputN, oputs, oputw, oputW, oputm, oputM, oputtilde;
106  
107 < static int  puta(), putf(), putd();
107 > static void setoutput(char *vs);
108 > static void tranotify(OBJECT obj);
109 > static void bogusray(void);
110 > static void rad(FVECT  org, FVECT  dir, double  dmax);
111 > static void irrad(FVECT  org, FVECT  dir);
112 > static void printvals(RAY  *r);
113 > static int getvec(FVECT  vec, int  fmt, FILE  *fp);
114 > static void tabin(RAY  *r);
115 > static void ourtrace(RAY  *r);
116  
117 < static int  (*putreal)();
117 > static oputf_t *ray_out[16], *every_out[16];
118 > static putf_t *putreal;
119  
120 + void  (*addobjnotify[])() = {ambnotify, tranotify, NULL};
121  
122 < quit(code)                      /* quit program */
123 < int  code;
122 >
123 > void
124 > quit(                   /* quit program */
125 >        int  code
126 > )
127   {
128 < #ifndef  NIX
128 > #ifndef  NON_POSIX /* XXX we don't clean up elsewhere? */
129          headclean();            /* delete header file */
130          pfclean();              /* clean up persist files */
131   #endif
# Line 102 | Line 133 | int  code;
133   }
134  
135  
136 < char *
137 < formstr(f)                              /* return format identifier */
138 < int  f;
136 > extern char *
137 > formstr(                                /* return format identifier */
138 >        int  f
139 > )
140   {
141          switch (f) {
142          case 'a': return("ascii");
# Line 116 | Line 148 | int  f;
148   }
149  
150  
151 < rtrace(fname)                           /* trace rays from file */
152 < char  *fname;
151 > extern void
152 > rtrace(                         /* trace rays from file */
153 >        char  *fname
154 > )
155   {
156 <        long  vcount = hresolu>1 ? hresolu*vresolu : vresolu;
156 >        unsigned long  vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu
157 >                                              : vresolu;
158          long  nextflush = hresolu;
159          FILE  *fp;
160 +        double  d;
161          FVECT  orig, direc;
162                                          /* set up input */
163          if (fname == NULL)
# Line 130 | Line 166 | char  *fname;
166                  sprintf(errmsg, "cannot open input file \"%s\"", fname);
167                  error(SYSTEM, errmsg);
168          }
133 #ifdef MSDOS
169          if (inform != 'a')
170 <                setmode(fileno(fp), O_BINARY);
136 < #endif
170 >                SET_FILE_BINARY(fp);
171                                          /* set up output */
172          setoutput(outvals);
173          switch (outform) {
# Line 156 | Line 190 | char  *fname;
190          while (getvec(orig, inform, fp) == 0 &&
191                          getvec(direc, inform, fp) == 0) {
192  
193 <                if (normalize(direc) == 0.0) {          /* zero ==> flush */
194 <                        fflush(stdout);
195 <                        continue;
196 <                }
197 <                samplendx++;
193 >                d = normalize(direc);
194 >                if (d == 0.0) {                         /* zero ==> flush */
195 >                        bogusray();
196 >                        if (--nextflush <= 0 || !vcount) {
197 >                                fflush(stdout);
198 >                                nextflush = hresolu;
199 >                        }
200 >                } else {
201 >                        samplendx = rand_samp ? random() : samplendx+1;
202                                                          /* compute and print */
203 <                if (imm_irrad)
204 <                        irrad(orig, direc);
205 <                else
206 <                        rad(orig, direc);
203 >                        if (imm_irrad)
204 >                                irrad(orig, direc);
205 >                        else
206 >                                rad(orig, direc, lim_dist ? d : 0.0);
207                                                          /* flush if time */
208 <                if (--nextflush == 0) {
209 <                        fflush(stdout);
210 <                        nextflush = hresolu;
208 >                        if (!--nextflush) {
209 >                                fflush(stdout);
210 >                                nextflush = hresolu;
211 >                        }
212                  }
213                  if (ferror(stdout))
214                          error(SYSTEM, "write error");
215 <                if (--vcount == 0)                      /* check for end */
215 >                if (vcount && !--vcount)                /* check for end */
216                          break;
217          }
218 <        fflush(stdout);
219 <        if (vcount > 0)
220 <                error(USER, "read error");
218 >        if (fflush(stdout) < 0)
219 >                error(SYSTEM, "write error");
220 >        if (vcount)
221 >                error(USER, "unexpected EOF on input");
222          if (fname != NULL)
223                  fclose(fp);
224   }
225  
226  
227 < setoutput(vs)                           /* set up output tables */
228 < register char  *vs;
227 > static void
228 > trace_sources(void)                     /* trace rays to light sources, also */
229   {
230 <        extern int  (*trace)();
231 <        register int (**table)() = ray_out;
230 >        int     sn;
231 >        
232 >        for (sn = 0; sn < nsources; sn++)
233 >                source[sn].sflags |= SFOLLOW;
234 > }
235  
236 +
237 + static void
238 + setoutput(                              /* set up output tables */
239 +        register char  *vs
240 + )
241 + {
242 +        register oputf_t **table = ray_out;
243 +
244          castonly = 1;
245          while (*vs)
246                  switch (*vs++) {
247 +                case 'T':                               /* trace sources */
248 +                        if (!*vs) break;
249 +                        trace_sources();
250 +                        /* fall through */
251                  case 't':                               /* trace */
252 +                        if (!*vs) break;
253                          *table = NULL;
254                          table = every_out;
255                          trace = ourtrace;
# Line 213 | Line 269 | register char  *vs;
269                          *table++ = oputl;
270                          castonly = 0;
271                          break;
272 +                case 'c':                               /* local coordinates */
273 +                        *table++ = oputc;
274 +                        break;
275                  case 'L':                               /* single ray length */
276                          *table++ = oputL;
277                          break;
# Line 232 | Line 291 | register char  *vs;
291                  case 'w':                               /* weight */
292                          *table++ = oputw;
293                          break;
294 +                case 'W':                               /* coefficient */
295 +                        *table++ = oputW;
296 +                        if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0))
297 +                                error(WARNING,
298 +                                        "-otW accuracy depends on -aa 0 -as 0");
299 +                        break;
300                  case 'm':                               /* modifier */
301                          *table++ = oputm;
302                          break;
303 +                case 'M':                               /* material */
304 +                        *table++ = oputM;
305 +                        break;
306 +                case '~':                               /* tilde */
307 +                        *table++ = oputtilde;
308 +                        break;
309                  }
310          *table = NULL;
311   }
312  
313  
314 < rad(org, dir)                   /* compute and print ray value(s) */
315 < FVECT  org, dir;
314 > static void
315 > bogusray(void)                  /* print out empty record */
316   {
317 +        thisray.rorg[0] = thisray.rorg[1] = thisray.rorg[2] =
318 +        thisray.rdir[0] = thisray.rdir[1] = thisray.rdir[2] = 0.0;
319 +        thisray.rmax = 0.0;
320 +        rayorigin(&thisray, PRIMARY, NULL, NULL);
321 +        printvals(&thisray);
322 + }
323 +
324 +
325 + static void
326 + rad(            /* compute and print ray value(s) */
327 +        FVECT  org,
328 +        FVECT  dir,
329 +        double  dmax
330 + )
331 + {
332          VCOPY(thisray.rorg, org);
333          VCOPY(thisray.rdir, dir);
334 <        thisray.rmax = 0.0;
335 <        rayorigin(&thisray, NULL, PRIMARY, 1.0);
336 <        if (castonly)
337 <                localhit(&thisray, &thescene) || sourcehit(&thisray);
338 <        else
334 >        thisray.rmax = dmax;
335 >        rayorigin(&thisray, PRIMARY, NULL, NULL);
336 >        if (castonly) {
337 >                if (!localhit(&thisray, &thescene)) {
338 >                        if (thisray.ro == &Aftplane) {  /* clipped */
339 >                                thisray.ro = NULL;
340 >                                thisray.rot = FHUGE;
341 >                        } else
342 >                                sourcehit(&thisray);
343 >                }
344 >        } else
345                  rayvalue(&thisray);
346          printvals(&thisray);
347   }
348  
349  
350 < irrad(org, dir)                 /* compute immediate irradiance value */
351 < FVECT  org, dir;
350 > static void
351 > irrad(                  /* compute immediate irradiance value */
352 >        FVECT  org,
353 >        FVECT  dir
354 > )
355   {
356          register int  i;
357  
# Line 264 | Line 359 | FVECT  org, dir;
359                  thisray.rorg[i] = org[i] + dir[i];
360                  thisray.rdir[i] = -dir[i];
361          }
362 <        rayorigin(&thisray, NULL, PRIMARY, 1.0);
362 >        thisray.rmax = 0.0;
363 >        rayorigin(&thisray, PRIMARY, NULL, NULL);
364                                          /* pretend we hit surface */
365          thisray.rot = 1.0-1e-4;
366          thisray.rod = 1.0;
# Line 277 | Line 373 | FVECT  org, dir;
373   }
374  
375  
376 < printvals(r)                    /* print requested ray values */
377 < RAY  *r;
376 > static void
377 > printvals(                      /* print requested ray values */
378 >        RAY  *r
379 > )
380   {
381 <        register int  (**tp)();
381 >        register oputf_t **tp;
382  
383          if (ray_out[0] == NULL)
384                  return;
# Line 291 | Line 389 | RAY  *r;
389   }
390  
391  
392 < getvec(vec, fmt, fp)            /* get a vector from fp */
393 < register FVECT  vec;
394 < int  fmt;
395 < FILE  *fp;
392 > static int
393 > getvec(         /* get a vector from fp */
394 >        register FVECT  vec,
395 >        int  fmt,
396 >        FILE  *fp
397 > )
398   {
299        extern char  *fgetword();
399          static float  vf[3];
400          static double  vd[3];
401          char  buf[32];
# Line 328 | Line 427 | FILE  *fp;
427   }
428  
429  
430 < tranotify(obj)                  /* record new modifier */
431 < OBJECT  obj;
430 > static void
431 > tranotify(                      /* record new modifier */
432 >        OBJECT  obj
433 > )
434   {
435          static int  hitlimit = 0;
436          register OBJREC  *o = objptr(obj);
437          register char  **tralp;
438  
439 +        if (obj == OVOID) {             /* starting over */
440 +                traset[0] = 0;
441 +                hitlimit = 0;
442 +                return;
443 +        }
444          if (hitlimit || !ismodifier(o->otype))
445                  return;
446          for (tralp = tralist; *tralp != NULL; tralp++)
# Line 350 | Line 456 | OBJECT obj;
456   }
457  
458  
459 < static
460 < ourtrace(r)                             /* print ray values */
461 < RAY  *r;
459 > static void
460 > ourtrace(                               /* print ray values */
461 >        RAY  *r
462 > )
463   {
464 <        register int  (**tp)();
464 >        register oputf_t **tp;
465  
466          if (every_out[0] == NULL)
467                  return;
# Line 366 | Line 473 | RAY  *r;
473          tabin(r);
474          for (tp = every_out; *tp != NULL; tp++)
475                  (**tp)(r);
476 <        putchar('\n');
476 >        if (outform == 'a')
477 >                putchar('\n');
478   }
479  
480  
481 < static
482 < tabin(r)                                /* tab in appropriate amount */
483 < RAY  *r;
481 > static void
482 > tabin(                          /* tab in appropriate amount */
483 >        RAY  *r
484 > )
485   {
486 <        register RAY  *rp;
486 >        const RAY  *rp;
487  
488          for (rp = r->parent; rp != NULL; rp = rp->parent)
489                  putchar('\t');
490   }
491  
492  
493 < static
494 < oputo(r)                                /* print origin */
495 < register RAY  *r;
493 > static void
494 > oputo(                          /* print origin */
495 >        RAY  *r
496 > )
497   {
498          (*putreal)(r->rorg[0]);
499          (*putreal)(r->rorg[1]);
# Line 391 | Line 501 | register RAY  *r;
501   }
502  
503  
504 < static
505 < oputd(r)                                /* print direction */
506 < register RAY  *r;
504 > static void
505 > oputd(                          /* print direction */
506 >        RAY  *r
507 > )
508   {
509          (*putreal)(r->rdir[0]);
510          (*putreal)(r->rdir[1]);
# Line 401 | Line 512 | register RAY  *r;
512   }
513  
514  
515 < static
516 < oputv(r)                                /* print value */
517 < register RAY  *r;
515 > static void
516 > oputv(                          /* print value */
517 >        RAY  *r
518 > )
519   {
408        COLR  cout;
409        
520          if (outform == 'c') {
521 +                COLR  cout;
522                  setcolr(cout,   colval(r->rcol,RED),
523                                  colval(r->rcol,GRN),
524                                  colval(r->rcol,BLU));
# Line 420 | Line 531 | register RAY  *r;
531   }
532  
533  
534 < static
535 < oputl(r)                                /* print effective distance */
536 < register RAY  *r;
534 > static void
535 > oputl(                          /* print effective distance */
536 >        RAY  *r
537 > )
538   {
539          (*putreal)(r->rt);
540   }
541  
542  
543 < static
544 < oputL(r)                                /* print single ray length */
545 < register RAY  *r;
543 > static void
544 > oputL(                          /* print single ray length */
545 >        RAY  *r
546 > )
547   {
548          (*putreal)(r->rot);
549   }
550  
551  
552 < static
553 < oputp(r)                                /* print point */
554 < register RAY  *r;
552 > static void
553 > oputc(                          /* print local coordinates */
554 >        RAY  *r
555 > )
556   {
557 +        (*putreal)(r->uv[0]);
558 +        (*putreal)(r->uv[1]);
559 + }
560 +
561 +
562 + static void
563 + oputp(                          /* print point */
564 +        RAY  *r
565 + )
566 + {
567          if (r->rot < FHUGE) {
568                  (*putreal)(r->rop[0]);
569                  (*putreal)(r->rop[1]);
# Line 452 | Line 576 | register RAY  *r;
576   }
577  
578  
579 < static
580 < oputN(r)                                /* print unperturbed normal */
581 < register RAY  *r;
579 > static void
580 > oputN(                          /* print unperturbed normal */
581 >        RAY  *r
582 > )
583   {
584          if (r->rot < FHUGE) {
585                  (*putreal)(r->ron[0]);
# Line 468 | Line 593 | register RAY  *r;
593   }
594  
595  
596 < static
597 < oputn(r)                                /* print perturbed normal */
598 < RAY  *r;
596 > static void
597 > oputn(                          /* print perturbed normal */
598 >        RAY  *r
599 > )
600   {
601          FVECT  pnorm;
602  
# Line 487 | Line 613 | RAY  *r;
613   }
614  
615  
616 < static
617 < oputs(r)                                /* print name */
618 < register RAY  *r;
616 > static void
617 > oputs(                          /* print name */
618 >        RAY  *r
619 > )
620   {
621          if (r->ro != NULL)
622                  fputs(r->ro->oname, stdout);
# Line 499 | Line 626 | register RAY  *r;
626   }
627  
628  
629 < static
630 < oputw(r)                                /* print weight */
631 < register RAY  *r;
629 > static void
630 > oputw(                          /* print weight */
631 >        RAY  *r
632 > )
633   {
634          (*putreal)(r->rweight);
635   }
636  
637  
638 < static
639 < oputm(r)                                /* print modifier */
640 < register RAY  *r;
638 > static void
639 > oputW(                          /* print contribution */
640 >        RAY  *r
641 > )
642   {
643 +        double  contr[3];
644 +
645 +        raycontrib(contr, r, PRIMARY);
646 +        (*putreal)(contr[RED]);
647 +        (*putreal)(contr[GRN]);
648 +        (*putreal)(contr[BLU]);
649 + }
650 +
651 +
652 + static void
653 + oputm(                          /* print modifier */
654 +        RAY  *r
655 + )
656 + {
657          if (r->ro != NULL)
658 <                fputs(objptr(r->ro->omod)->oname, stdout);
658 >                if (r->ro->omod != OVOID)
659 >                        fputs(objptr(r->ro->omod)->oname, stdout);
660 >                else
661 >                        fputs(VOIDID, stdout);
662          else
663                  putchar('*');
664          putchar('\t');
665   }
666  
667  
668 < static
669 < puta(v)                         /* print ascii value */
670 < double  v;
668 > static void
669 > oputM(                          /* print material */
670 >        RAY  *r
671 > )
672   {
673 +        OBJREC  *mat;
674 +
675 +        if (r->ro != NULL) {
676 +                if ((mat = findmaterial(r->ro)) != NULL)
677 +                        fputs(mat->oname, stdout);
678 +                else
679 +                        fputs(VOIDID, stdout);
680 +        } else
681 +                putchar('*');
682 +        putchar('\t');
683 + }
684 +
685 +
686 + static void
687 + oputtilde(                      /* output tilde (spacer) */
688 +        RAY  *r
689 + )
690 + {
691 +        fputs("~\t", stdout);
692 + }
693 +
694 +
695 + static void
696 + puta(                           /* print ascii value */
697 +        double  v
698 + )
699 + {
700          printf("%e\t", v);
701   }
702  
703  
704 < static
704 > static void
705   putd(v)                         /* print binary double */
706   double  v;
707   {
# Line 535 | Line 709 | double  v;
709   }
710  
711  
712 < static
712 > static void
713   putf(v)                         /* print binary float */
714   double  v;
715   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines