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

Comparing ray/src/rt/raytrace.c (file contents):
Revision 2.17 by greg, Wed Dec 21 09:10:23 1994 UTC vs.
Revision 2.45 by schorsch, Tue Mar 30 16:13:01 2004 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   *  raytrace.c - routines for tracing and shading rays.
6   *
7 < *     8/7/85
7 > *  External symbols declared in ray.h
8   */
9  
10 < #include  "ray.h"
10 > #include "copyright.h"
11  
12 < #include  "octree.h"
13 <
12 > #include  "ray.h"
13 > #include  "source.h"
14   #include  "otypes.h"
18
15   #include  "otspecial.h"
16  
17   #define  MAXCSET        ((MAXSET+1)*2-1)        /* maximum check set size */
18  
23 extern CUBE  thescene;                  /* our scene */
24 extern int  maxdepth;                   /* maximum recursion depth */
25 extern double  minweight;               /* minimum ray weight */
26 extern int  do_irrad;                   /* compute irradiance? */
27
19   unsigned long  raynum = 0;              /* next unique ray number */
20   unsigned long  nrays = 0;               /* number of calls to localhit */
21  
22 < static FLOAT  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
22 > static RREAL  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
23   OBJREC  Lamb = {
24          OVOID, MAT_PLASTIC, "Lambertian",
25          {0, 5, NULL, Lambfa}, NULL,
# Line 36 | Line 27 | OBJREC  Lamb = {
27  
28   OBJREC  Aftplane;                       /* aft clipping plane object */
29  
39 static int  raymove(), checkset(), checkhit();
40
41 #define  MAXLOOP        128             /* modifier loop detection */
42
30   #define  RAYHIT         (-1)            /* return value for intercepted ray */
31  
32 + static int raymove(FVECT  pos, OBJECT  *cxs, int  dirf, RAY  *r, CUBE  *cu);
33 + static int checkhit(RAY  *r, CUBE  *cu, OBJECT  *cxs);
34 + static void checkset(OBJECT  *os, OBJECT  *cs);
35  
36 < rayorigin(r, ro, rt, rw)                /* start new ray from old one */
37 < register RAY  *r, *ro;
38 < int  rt;
39 < double  rw;
36 >
37 > extern int
38 > rayorigin(              /* start new ray from old one */
39 >        register RAY  *r,
40 >        register RAY  *ro,
41 >        int  rt,
42 >        double  rw
43 > )
44   {
45 +        double  re;
46 +
47          if ((r->parent = ro) == NULL) {         /* primary ray */
48                  r->rlvl = 0;
49                  r->rweight = rw;
# Line 55 | Line 51 | double  rw;
51                  r->rsrc = -1;
52                  r->clipset = NULL;
53                  r->revf = raytrace;
54 +                copycolor(r->cext, cextinction);
55 +                copycolor(r->albedo, salbedo);
56 +                r->gecc = seccg;
57 +                r->slights = NULL;
58          } else {                                /* spawned ray */
59                  r->rlvl = ro->rlvl;
60                  if (rt & RAYREFL) {
61                          r->rlvl++;
62                          r->rsrc = -1;
63                          r->clipset = ro->clipset;
64 +                        r->rmax = 0.0;
65                  } else {
66                          r->rsrc = ro->rsrc;
67                          r->clipset = ro->newcset;
68 +                        r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot;
69                  }
70                  r->revf = ro->revf;
71 <                r->rweight = ro->rweight * rw;
71 >                copycolor(r->cext, ro->cext);
72 >                copycolor(r->albedo, ro->albedo);
73 >                r->gecc = ro->gecc;
74 >                r->slights = ro->slights;
75                  r->crtype = ro->crtype | (r->rtype = rt);
76                  VCOPY(r->rorg, ro->rop);
77 <                r->rmax = 0.0;
77 >                r->rweight = ro->rweight * rw;
78 >                                                /* estimate absorption */
79 >                re = colval(ro->cext,RED) < colval(ro->cext,GRN) ?
80 >                                colval(ro->cext,RED) : colval(ro->cext,GRN);
81 >                if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU);
82 >                if (re > 0.)
83 >                        r->rweight *= exp(-re*ro->rot);
84          }
85          rayclear(r);
86          return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
87   }
88  
89  
90 < rayclear(r)                     /* clear a ray for (re)evaluation */
91 < register RAY  *r;
90 > extern void
91 > rayclear(                       /* clear a ray for (re)evaluation */
92 >        register RAY  *r
93 > )
94   {
95          r->rno = raynum++;
96          r->newcset = r->clipset;
97 +        r->hitf = rayhit;
98 +        r->robj = OVOID;
99          r->ro = NULL;
100 <        r->rot = FHUGE;
100 >        r->rox = NULL;
101 >        r->rt = r->rot = FHUGE;
102          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
103 +        r->uv[0] = r->uv[1] = 0.0;
104          setcolor(r->pcol, 1.0, 1.0, 1.0);
105          setcolor(r->rcol, 0.0, 0.0, 0.0);
89        r->rt = 0.0;
106   }
107  
108  
109 < raytrace(r)                     /* trace a ray and compute its value */
110 < RAY  *r;
109 > extern void
110 > raytrace(                       /* trace a ray and compute its value */
111 >        RAY  *r
112 > )
113   {
96        extern int  (*trace)();
97        int  gotmat;
98
114          if (localhit(r, &thescene))
115 <                gotmat = raycont(r);
115 >                raycont(r);             /* hit local surface, evaluate */
116          else if (r->ro == &Aftplane) {
117 <                r->ro = NULL;
117 >                r->ro = NULL;           /* hit aft clipping plane */
118                  r->rot = FHUGE;
119          } else if (sourcehit(r))
120 <                gotmat = rayshade(r, r->ro->omod);
120 >                rayshade(r, r->ro->omod);       /* distant source */
121  
122 <        if (r->ro != NULL && !gotmat)
108 <                objerror(r->ro, USER, "material not found");
122 >        rayparticipate(r);              /* for participating medium */
123  
124          if (trace != NULL)
125                  (*trace)(r);            /* trace execution */
126   }
127  
128  
129 < raycont(r)                      /* check for clipped object and continue */
130 < register RAY  *r;
129 > extern void
130 > raycont(                        /* check for clipped object and continue */
131 >        register RAY  *r
132 > )
133   {
134          if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
135 <                        r->ro->omod == OVOID) {
135 >                        !rayshade(r, r->ro->omod))
136                  raytrans(r);
121                return(1);
122        }
123        return(rayshade(r, r->ro->omod));
137   }
138  
139  
140 < raytrans(r)                     /* transmit ray as is */
141 < register RAY  *r;
140 > extern void
141 > raytrans(                       /* transmit ray as is */
142 >        register RAY  *r
143 > )
144   {
145          RAY  tr;
146  
147          if (rayorigin(&tr, r, TRANS, 1.0) == 0) {
148                  VCOPY(tr.rdir, r->rdir);
134                if (r->rmax > FTINY)
135                        tr.rmax = r->rmax - r->rot;
149                  rayvalue(&tr);
150                  copycolor(r->rcol, tr.rcol);
151                  r->rt = r->rot + tr.rt;
# Line 140 | Line 153 | register RAY  *r;
153   }
154  
155  
156 < rayshade(r, mod)                /* shade ray r with material mod */
157 < register RAY  *r;
158 < int  mod;
156 > extern int
157 > rayshade(               /* shade ray r with material mod */
158 >        register RAY  *r,
159 >        int  mod
160 > )
161   {
147        static int  depth = 0;
162          int  gotmat;
163          register OBJREC  *m;
150                                        /* check for infinite loop */
151        if (depth++ >= MAXLOOP)
152                objerror(r->ro, USER, "possible modifier loop");
164          r->rt = r->rot;                 /* set effective ray length */
165          for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
166                  m = objptr(mod);
# Line 160 | Line 171 | int  mod;
171                  }
172                  ******/
173                                          /* hack for irradiance calculation */
174 <                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
174 >                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) &&
175 >                                m->otype != MAT_CLIP &&
176 >                                (ofun[m->otype].flags & (T_M|T_X))) {
177                          if (irr_ignore(m->otype)) {
165                                depth--;
178                                  raytrans(r);
179                                  return(1);
180                          }
# Line 172 | Line 184 | int  mod;
184                                          /* materials call raytexture */
185                  gotmat = (*ofun[m->otype].funp)(m, r);
186          }
175        depth--;
187          return(gotmat);
188   }
189  
190  
191 < raytexture(r, mod)                      /* get material modifiers */
192 < RAY  *r;
193 < int  mod;
191 > extern void
192 > rayparticipate(                 /* compute ray medium participation */
193 >        register RAY  *r
194 > )
195   {
196 <        static int  depth = 0;
196 >        COLOR   ce, ca;
197 >        double  re, ge, be;
198 >
199 >        if (intens(r->cext) <= 1./FHUGE)
200 >                return;                         /* no medium */
201 >        re = r->rot*colval(r->cext,RED);
202 >        ge = r->rot*colval(r->cext,GRN);
203 >        be = r->rot*colval(r->cext,BLU);
204 >        if (r->crtype & SHADOW) {               /* no scattering for sources */
205 >                re *= 1. - colval(r->albedo,RED);
206 >                ge *= 1. - colval(r->albedo,GRN);
207 >                be *= 1. - colval(r->albedo,BLU);
208 >        }
209 >        setcolor(ce,    re<=0. ? 1. : re>92. ? 0. : exp(-re),
210 >                        ge<=0. ? 1. : ge>92. ? 0. : exp(-ge),
211 >                        be<=0. ? 1. : be>92. ? 0. : exp(-be));
212 >        multcolor(r->rcol, ce);                 /* path absorption */
213 >        if (r->crtype & SHADOW || intens(r->albedo) <= FTINY)
214 >                return;                         /* no scattering */
215 >        setcolor(ca,
216 >                colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)),
217 >                colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)),
218 >                colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU)));
219 >        addcolor(r->rcol, ca);                  /* ambient in scattering */
220 >        srcscatter(r);                          /* source in scattering */
221 > }
222 >
223 >
224 > extern void
225 > raytexture(                     /* get material modifiers */
226 >        RAY  *r,
227 >        OBJECT  mod
228 > )
229 > {
230          register OBJREC  *m;
186                                        /* check for infinite loop */
187        if (depth++ >= MAXLOOP)
188                objerror(r->ro, USER, "modifier loop");
231                                          /* execute textures and patterns */
232          for ( ; mod != OVOID; mod = m->omod) {
233                  m = objptr(mod);
# Line 195 | Line 237 | int  mod;
237                          error(USER, errmsg);
238                  }
239                  ******/
240 <                if ((*ofun[m->otype].funp)(m, r))
241 <                        objerror(r->ro, USER, "conflicting materials");
240 >                if ((*ofun[m->otype].funp)(m, r)) {
241 >                        sprintf(errmsg, "conflicting material \"%s\"",
242 >                                        m->oname);
243 >                        objerror(r->ro, USER, errmsg);
244 >                }
245          }
201        depth--;                        /* end here */
246   }
247  
248  
249 < raymixture(r, fore, back, coef)         /* mix modifiers */
250 < register RAY  *r;
251 < OBJECT  fore, back;
252 < double  coef;
249 > extern int
250 > raymixture(             /* mix modifiers */
251 >        register RAY  *r,
252 >        OBJECT  fore,
253 >        OBJECT  back,
254 >        double  coef
255 > )
256   {
257          RAY  fr, br;
258          int  foremat, backmat;
259          register int  i;
260 <                                        /* clip coefficient */
260 >                                        /* bound coefficient */
261          if (coef > 1.0)
262                  coef = 1.0;
263          else if (coef < 0.0)
264                  coef = 0.0;
265                                          /* compute foreground and background */
266 <        foremat = backmat = -1;
266 >        foremat = backmat = 0;
267                                          /* foreground */
268 <        copystruct(&fr, r);
269 <        if (fore != OVOID && coef > FTINY)
268 >        fr = *r;
269 >        if (coef > FTINY)
270                  foremat = rayshade(&fr, fore);
271                                          /* background */
272 <        copystruct(&br, r);
273 <        if (back != OVOID && coef < 1.0-FTINY)
272 >        br = *r;
273 >        if (coef < 1.0-FTINY)
274                  backmat = rayshade(&br, back);
275 <                                        /* check */
276 <        if (foremat < 0)
277 <                if (backmat < 0)
278 <                        foremat = backmat = 0;
279 <                else
280 <                        foremat = backmat;
281 <        else if (backmat < 0)
235 <                backmat = foremat;
236 <        if ((foremat==0) != (backmat==0))
237 <                objerror(r->ro, USER, "mixing material with non-material");
275 >                                        /* check for transparency */
276 >        if (backmat ^ foremat) {
277 >                if (backmat && coef > FTINY)
278 >                        raytrans(&fr);
279 >                else if (foremat && coef < 1.0-FTINY)
280 >                        raytrans(&br);
281 >        }
282                                          /* mix perturbations */
283          for (i = 0; i < 3; i++)
284                  r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
# Line 243 | Line 287 | double  coef;
287          scalecolor(br.pcol, 1.0-coef);
288          copycolor(r->pcol, fr.pcol);
289          addcolor(r->pcol, br.pcol);
290 +                                        /* return value tells if material */
291 +        if (!foremat & !backmat)
292 +                return(0);
293                                          /* mix returned ray values */
294 <        if (foremat) {
295 <                scalecolor(fr.rcol, coef);
296 <                scalecolor(br.rcol, 1.0-coef);
297 <                copycolor(r->rcol, fr.rcol);
298 <                addcolor(r->rcol, br.rcol);
299 <                r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
294 >        scalecolor(fr.rcol, coef);
295 >        scalecolor(br.rcol, 1.0-coef);
296 >        copycolor(r->rcol, fr.rcol);
297 >        addcolor(r->rcol, br.rcol);
298 >        r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
299 >        return(1);
300 > }
301 >
302 >
303 > extern double
304 > raydist(                /* compute (cumulative) ray distance */
305 >        register RAY  *r,
306 >        register int  flags
307 > )
308 > {
309 >        double  sum = 0.0;
310 >
311 >        while (r != NULL && r->crtype&flags) {
312 >                sum += r->rot;
313 >                r = r->parent;
314          }
315 <                                        /* return value tells if material */
255 <        return(foremat);
315 >        return(sum);
316   }
317  
318  
319 < double
320 < raynormal(norm, r)              /* compute perturbed normal for ray */
321 < FVECT  norm;
322 < register RAY  *r;
319 > extern double
320 > raynormal(              /* compute perturbed normal for ray */
321 >        FVECT  norm,
322 >        register RAY  *r
323 > )
324   {
325          double  newdot;
326          register int  i;
# Line 291 | Line 352 | register RAY  *r;
352   }
353  
354  
355 < newrayxf(r)                     /* get new tranformation matrix for ray */
356 < RAY  *r;
355 > extern void
356 > newrayxf(                       /* get new tranformation matrix for ray */
357 >        RAY  *r
358 > )
359   {
360          static struct xfn {
361                  struct xfn  *next;
# Line 310 | Line 373 | RAY  *r;
373                  if (rp->rox == &xp->xf) {               /* xp in use */
374                          xp = xp->next;                  /* move to next */
375                          if (xp == xflast) {             /* need new one */
376 <                                xp = (struct xfn *)bmalloc(sizeof(struct xfn));
376 >                                xp = (struct xfn *)malloc(sizeof(struct xfn));
377                                  if (xp == NULL)
378                                          error(SYSTEM,
379                                                  "out of memory in newrayxf");
# Line 327 | Line 390 | RAY  *r;
390   }
391  
392  
393 < flipsurface(r)                  /* reverse surface orientation */
394 < register RAY  *r;
393 > extern void
394 > flipsurface(                    /* reverse surface orientation */
395 >        register RAY  *r
396 > )
397   {
398          r->rod = -r->rod;
399          r->ron[0] = -r->ron[0];
# Line 340 | Line 405 | register RAY  *r;
405   }
406  
407  
408 < localhit(r, scene)              /* check for hit in the octree */
409 < register RAY  *r;
410 < register CUBE  *scene;
408 > extern void
409 > rayhit(                 /* standard ray hit test */
410 >        OBJECT  *oset,
411 >        RAY  *r
412 > )
413   {
414 +        OBJREC  *o;
415 +        int     i;
416 +
417 +        for (i = oset[0]; i > 0; i--) {
418 +                o = objptr(oset[i]);
419 +                if ((*ofun[o->otype].funp)(o, r))
420 +                        r->robj = oset[i];
421 +        }
422 + }
423 +
424 +
425 + extern int
426 + localhit(               /* check for hit in the octree */
427 +        register RAY  *r,
428 +        register CUBE  *scene
429 + )
430 + {
431          OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
432          FVECT  curpos;                  /* current cube position */
433          int  sflags;                    /* sign flags */
# Line 396 | Line 480 | register CUBE  *scene;
480                          return(0);
481          }
482          cxset[0] = 0;
483 <        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT &&
484 <                        r->ro != &Aftplane);
483 >        raymove(curpos, cxset, sflags, r, scene);
484 >        return((r->ro != NULL) & (r->ro != &Aftplane));
485   }
486  
487  
488   static int
489 < raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
490 < FVECT  pos;                     /* current position, modified herein */
491 < OBJECT  *cxs;                   /* checked objects, modified by checkhit */
492 < int  dirf;                      /* direction indicators to speed tests */
493 < register RAY  *r;
494 < register CUBE  *cu;
489 > raymove(                /* check for hit as we move */
490 >        FVECT  pos,                     /* current position, modified herein */
491 >        OBJECT  *cxs,                   /* checked objects, modified by checkhit */
492 >        int  dirf,                      /* direction indicators to speed tests */
493 >        register RAY  *r,
494 >        register CUBE  *cu
495 > )
496   {
497          int  ax;
498          double  dt, t;
# Line 452 | Line 537 | register CUBE  *cu;
537                  }
538                  /*NOTREACHED*/
539          }
540 <        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
540 >        if (isfull(cu->cutree)) {
541 >                if (checkhit(r, cu, cxs))
542 >                        return(RAYHIT);
543 >        } else if (r->ro == &Aftplane && incube(cu, r->rop))
544                  return(RAYHIT);
545                                          /* advance to next cube */
546          if (dirf&0x11) {
# Line 484 | Line 572 | register CUBE  *cu;
572   }
573  
574  
575 < static
576 < checkhit(r, cu, cxs)            /* check for hit in full cube */
577 < register RAY  *r;
578 < CUBE  *cu;
579 < OBJECT  *cxs;
575 > static int
576 > checkhit(               /* check for hit in full cube */
577 >        register RAY  *r,
578 >        CUBE  *cu,
579 >        OBJECT  *cxs
580 > )
581   {
582          OBJECT  oset[MAXSET+1];
494        register OBJREC  *o;
495        register int  i;
583  
584          objset(oset, cu->cutree);
585 <        checkset(oset, cxs);                    /* eliminate double-checking */
586 <        for (i = oset[0]; i > 0; i--) {
587 <                o = objptr(oset[i]);
588 <                (*ofun[o->otype].funp)(o, r);
589 <        }
503 <        if (r->ro == NULL)
585 >        checkset(oset, cxs);                    /* avoid double-checking */
586 >
587 >        (*r->hitf)(oset, r);                    /* test for hit in set */
588 >
589 >        if (r->robj == OVOID)
590                  return(0);                      /* no scores yet */
591  
592          return(incube(cu, r->rop));             /* hit OK if in current cube */
593   }
594  
595  
596 < static
597 < checkset(os, cs)                /* modify checked set and set to check */
598 < register OBJECT  *os;                   /* os' = os - cs */
599 < register OBJECT  *cs;                   /* cs' = cs + os */
596 > static void
597 > checkset(               /* modify checked set and set to check */
598 >        register OBJECT  *os,                   /* os' = os - cs */
599 >        register OBJECT  *cs                    /* cs' = cs + os */
600 > )
601   {
602          OBJECT  cset[MAXCSET+MAXSET+1];
603          register int  i, j;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines