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.2 by greg, Mon Jan 25 15:16:59 1993 UTC vs.
Revision 2.16 by greg, Tue Dec 20 20:18:22 1994 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1994 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "otspecial.h"
20  
21 + #define  MAXCSET        ((MAXSET+1)*2-1)        /* maximum check set size */
22 +
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  
28 < long  raynum = 0L;                      /* next unique ray number */
29 < long  nrays = 0L;                       /* number of calls to localhit */
28 > unsigned long  raynum = 0;              /* next unique ray number */
29 > unsigned long  nrays = 0;               /* number of calls to localhit */
30  
31   static FLOAT  Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
32   OBJREC  Lamb = {
# Line 32 | Line 34 | OBJREC  Lamb = {
34          {0, 5, NULL, Lambfa}, NULL,
35   };                                      /* a Lambertian surface */
36  
37 + static OBJREC  Aftplane;                /* aft clipping plane object */
38 +
39 + static int  raymove(), checkset(), checkhit();
40 +
41   #define  MAXLOOP        128             /* modifier loop detection */
42  
43   #define  RAYHIT         (-1)            /* return value for intercepted ray */
# Line 63 | Line 69 | double  rw;
69                  r->rweight = ro->rweight * rw;
70                  r->crtype = ro->crtype | (r->rtype = rt);
71                  VCOPY(r->rorg, ro->rop);
72 +                r->rmax = 0.0;
73          }
74          rayclear(r);
75          return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
# Line 73 | Line 80 | rayclear(r)                    /* clear a ray for (re)evaluation */
80   register RAY  *r;
81   {
82          r->rno = raynum++;
76        r->cxs[0] = 0;
83          r->newcset = r->clipset;
84 <        r->ro = NULL;
85 <        r->rot = FHUGE;
84 >        if (r->rmax > FTINY) {
85 >                r->ro = &Aftplane;
86 >                r->rot = r->rmax;
87 >                r->rop[0] = r->rorg[0] + r->rot*r->rdir[0];
88 >                r->rop[1] = r->rorg[1] + r->rot*r->rdir[1];
89 >                r->rop[2] = r->rorg[2] + r->rot*r->rdir[2];
90 >        } else {
91 >                r->ro = NULL;
92 >                r->rot = FHUGE;
93 >        }
94          r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
95          setcolor(r->pcol, 1.0, 1.0, 1.0);
96          setcolor(r->rcol, 0.0, 0.0, 0.0);
# Line 88 | Line 102 | raytrace(r)                    /* trace a ray and compute its value */
102   RAY  *r;
103   {
104          extern int  (*trace)();
105 +        int  gotmat;
106  
107          if (localhit(r, &thescene))
108 <                raycont(r);
109 <        else if (sourcehit(r))
110 <                rayshade(r, r->ro->omod);
108 >                gotmat = raycont(r);
109 >        else if (r->ro == &Aftplane) {
110 >                r->ro = NULL;
111 >                r->rot = FHUGE;
112 >        } else if (sourcehit(r))
113 >                gotmat = rayshade(r, r->ro->omod);
114  
115 +        if (r->ro != NULL && !gotmat)
116 +                objerror(r->ro, USER, "material not found");
117 +
118          if (trace != NULL)
119                  (*trace)(r);            /* trace execution */
120   }
# Line 102 | Line 123 | RAY  *r;
123   raycont(r)                      /* check for clipped object and continue */
124   register RAY  *r;
125   {
126 <        if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
126 >        if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) ||
127 >                        r->ro->omod == OVOID) {
128                  raytrans(r);
129 <        else
130 <                rayshade(r, r->ro->omod);
129 >                return(1);
130 >        }
131 >        return(rayshade(r, r->ro->omod));
132   }
133  
134  
# Line 128 | Line 151 | register RAY  *r;
151   int  mod;
152   {
153          static int  depth = 0;
154 +        int  gotmat;
155          register OBJREC  *m;
156                                          /* check for infinite loop */
157          if (depth++ >= MAXLOOP)
158                  objerror(r->ro, USER, "possible modifier loop");
159          r->rt = r->rot;                 /* set effective ray length */
160 <        for ( ; mod != OVOID; mod = m->omod) {
160 >        for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) {
161                  m = objptr(mod);
162                  /****** unnecessary test since modifier() is always called
163                  if (!ismodifier(m->otype)) {
# Line 146 | Line 170 | int  mod;
170                          if (irr_ignore(m->otype)) {
171                                  depth--;
172                                  raytrans(r);
173 <                                return;
173 >                                return(1);
174                          }
175                          if (!islight(m->otype))
176                                  m = &Lamb;
177                  }
178 <                (*ofun[m->otype].funp)(m, r);   /* execute function */
179 <                if (ismaterial(m->otype)) {     /* materials call raytexture */
156 <                        depth--;
157 <                        return;         /* we're done */
158 <                }
178 >                                        /* materials call raytexture */
179 >                gotmat = (*ofun[m->otype].funp)(m, r);
180          }
181 <        objerror(r->ro, USER, "material not found");
181 >        depth--;
182 >        return(gotmat);
183   }
184  
185  
# Line 173 | Line 195 | int  mod;
195                                          /* execute textures and patterns */
196          for ( ; mod != OVOID; mod = m->omod) {
197                  m = objptr(mod);
198 <                if (!istexture(m->otype)) {
198 >                /****** unnecessary test since modifier() is always called
199 >                if (!ismodifier(m->otype)) {
200                          sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
201                          error(USER, errmsg);
202                  }
203 <                (*ofun[m->otype].funp)(m, r);
203 >                ******/
204 >                if ((*ofun[m->otype].funp)(m, r))
205 >                        objerror(r->ro, USER, "conflicting materials");
206          }
207          depth--;                        /* end here */
208   }
# Line 188 | Line 213 | register RAY  *r;
213   OBJECT  fore, back;
214   double  coef;
215   {
216 <        FVECT  curpert, forepert, backpert;
217 <        COLOR  curpcol, forepcol, backpcol;
216 >        RAY  fr, br;
217 >        int  foremat, backmat;
218          register int  i;
219                                          /* clip coefficient */
220          if (coef > 1.0)
221                  coef = 1.0;
222          else if (coef < 0.0)
223                  coef = 0.0;
224 <                                        /* save current mods */
225 <        VCOPY(curpert, r->pert);
226 <        copycolor(curpcol, r->pcol);
227 <                                        /* compute new mods */
203 <                                                /* foreground */
204 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
205 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
224 >                                        /* compute foreground and background */
225 >        foremat = backmat = -1;
226 >                                        /* foreground */
227 >        copystruct(&fr, r);
228          if (fore != OVOID && coef > FTINY)
229 <                raytexture(r, fore);
230 <        VCOPY(forepert, r->pert);
231 <        copycolor(forepcol, r->pcol);
210 <                                                /* background */
211 <        r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
212 <        setcolor(r->pcol, 1.0, 1.0, 1.0);
229 >                foremat = rayshade(&fr, fore);
230 >                                        /* background */
231 >        copystruct(&br, r);
232          if (back != OVOID && coef < 1.0-FTINY)
233 <                raytexture(r, back);
234 <        VCOPY(backpert, r->pert);
235 <        copycolor(backpcol, r->pcol);
236 <                                        /* sum perturbations */
233 >                backmat = rayshade(&br, back);
234 >                                        /* check */
235 >        if (foremat < 0)
236 >                if (backmat < 0)
237 >                        foremat = backmat = 0;
238 >                else
239 >                        foremat = backmat;
240 >        else if (backmat < 0)
241 >                backmat = foremat;
242 >        if ((foremat==0) != (backmat==0))
243 >                objerror(r->ro, USER, "mixing material with non-material");
244 >                                        /* mix perturbations */
245          for (i = 0; i < 3; i++)
246 <                r->pert[i] = curpert[i] + coef*forepert[i] +
247 <                                (1.0-coef)*backpert[i];
248 <                                        /* multiply colors */
249 <        setcolor(r->pcol, coef*colval(forepcol,RED) +
250 <                                (1.0-coef)*colval(backpcol,RED),
251 <                        coef*colval(forepcol,GRN) +
252 <                                (1.0-coef)*colval(backpcol,GRN),
253 <                        coef*colval(forepcol,BLU) +
254 <                                (1.0-coef)*colval(backpcol,BLU));
255 <        multcolor(r->pcol, curpcol);
246 >                r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i];
247 >                                        /* mix pattern colors */
248 >        scalecolor(fr.pcol, coef);
249 >        scalecolor(br.pcol, 1.0-coef);
250 >        copycolor(r->pcol, fr.pcol);
251 >        addcolor(r->pcol, br.pcol);
252 >                                        /* mix returned ray values */
253 >        if (foremat) {
254 >                scalecolor(fr.rcol, coef);
255 >                scalecolor(br.rcol, 1.0-coef);
256 >                copycolor(r->rcol, fr.rcol);
257 >                addcolor(r->rcol, br.rcol);
258 >                r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt;
259 >        }
260 >                                        /* return value tells if material */
261 >        return(foremat);
262   }
263  
264  
# Line 317 | Line 350 | localhit(r, scene)             /* check for hit in the octree */
350   register RAY  *r;
351   register CUBE  *scene;
352   {
353 +        OBJECT  cxset[MAXCSET+1];       /* set of checked objects */
354          FVECT  curpos;                  /* current cube position */
355          int  sflags;                    /* sign flags */
356          double  t, dt;
# Line 326 | Line 360 | register CUBE  *scene;
360          sflags = 0;
361          for (i = 0; i < 3; i++) {
362                  curpos[i] = r->rorg[i];
363 <                if (r->rdir[i] > FTINY)
363 >                if (r->rdir[i] > 1e-7)
364                          sflags |= 1 << i;
365 <                else if (r->rdir[i] < -FTINY)
365 >                else if (r->rdir[i] < -1e-7)
366                          sflags |= 0x10 << i;
367          }
368          if (sflags == 0)
# Line 357 | Line 391 | register CUBE  *scene;
391                  if (!incube(scene, curpos))     /* non-intersecting ray */
392                          return(0);
393          }
394 <        return(raymove(curpos, sflags, r, scene) == RAYHIT);
394 >        cxset[0] = 0;
395 >        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT &&
396 >                        r->ro != &Aftplane);
397   }
398  
399  
400   static int
401 < raymove(pos, dirf, r, cu)               /* check for hit as we move */
402 < FVECT  pos;                     /* modified */
401 > raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */
402 > FVECT  pos;                     /* current position, modified herein */
403 > OBJECT  *cxs;                   /* checked objects, modified by checkhit */
404   int  dirf;                      /* direction indicators to speed tests */
405   register RAY  *r;
406   register CUBE  *cu;
# Line 392 | Line 429 | register CUBE  *cu;
429                  }
430                  for ( ; ; ) {
431                          cukid.cutree = octkid(cu->cutree, br);
432 <                        if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT)
432 >                        if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT)
433                                  return(RAYHIT);
434                          sgn = 1 << ax;
435                          if (sgn & dirf)                 /* positive axis? */
# Line 411 | Line 448 | register CUBE  *cu;
448                  }
449                  /*NOTREACHED*/
450          }
451 <        if (isfull(cu->cutree) && checkhit(r, cu))
451 >        if (isfull(cu->cutree) && checkhit(r, cu, cxs))
452                  return(RAYHIT);
453                                          /* advance to next cube */
454          if (dirf&0x11) {
# Line 444 | Line 481 | register CUBE  *cu;
481  
482  
483   static
484 < checkhit(r, cu)                 /* check for hit in full cube */
484 > checkhit(r, cu, cxs)            /* check for hit in full cube */
485   register RAY  *r;
486   CUBE  *cu;
487 + OBJECT  *cxs;
488   {
489          OBJECT  oset[MAXSET+1];
490          register OBJREC  *o;
491          register int  i;
492  
493          objset(oset, cu->cutree);
494 <        checkset(oset, r->cxs);                 /* eliminate double-checking */
494 >        checkset(oset, cxs);                    /* eliminate double-checking */
495          for (i = oset[0]; i > 0; i--) {
496                  o = objptr(oset[i]);
497                  (*ofun[o->otype].funp)(o, r);
# Line 467 | Line 505 | CUBE  *cu;
505  
506   static
507   checkset(os, cs)                /* modify checked set and set to check */
508 < register OBJECT  os[MAXSET+1];          /* os' = os - cs */
509 < register OBJECT  cs[MAXCSET+1];         /* cs' = cs + os */
508 > register OBJECT  *os;                   /* os' = os - cs */
509 > register OBJECT  *cs;                   /* cs' = cs + os */
510   {
511          OBJECT  cset[MAXCSET+MAXSET+1];
512 <        register int  i, j, k;
512 >        register int  i, j;
513 >        int  k;
514                                          /* copy os in place, cset <- cs */
515          cset[0] = 0;
516          k = 0;
# Line 483 | Line 522 | register OBJECT  cs[MAXCSET+1];                /* cs' = cs + os */
522                          cset[++cset[0]] = os[i];
523                  }
524          }
525 +        if (!(os[0] = k))               /* new "to check" set size */
526 +                return;                 /* special case */
527          while (j <= cs[0])              /* get the rest of cs */
528                  cset[++cset[0]] = cs[j++];
529 <        if (cset[0] > MAXCSET)          /* truncate if necessary */
529 >        if (cset[0] > MAXCSET)          /* truncate "checked" set if nec. */
530                  cset[0] = MAXCSET;
531 <        setcopy(cs, cset);              /* copy new "checked" set back */
532 <        os[0] = k;                      /* new "to check" set size */
531 >        /* setcopy(cs, cset); */        /* copy cset back to cs */
532 >        os = cset;
533 >        for (i = os[0]; i-- >= 0; )
534 >                *cs++ = *os++;
535   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines