| 1 | 
< | 
/* Copyright (c) 1990 Regents of the University of California */ | 
| 1 | 
> | 
/* Copyright (c) 1991 Regents of the University of California */ | 
| 2 | 
  | 
 | 
| 3 | 
  | 
#ifndef lint | 
| 4 | 
  | 
static char SCCSid[] = "$SunId$ LBL"; | 
| 16 | 
  | 
 | 
| 17 | 
  | 
#include  "otypes.h" | 
| 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  nrays = 0L;                       /* number of rays traced */ | 
| 28 | 
> | 
long  raynum = 0L;                      /* next unique ray number */ | 
| 29 | 
> | 
long  nrays = 0L;                       /* number of calls to localhit */ | 
| 30 | 
  | 
 | 
| 31 | 
+ | 
static FLOAT  Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; | 
| 32 | 
+ | 
OBJREC  Lamb = { | 
| 33 | 
+ | 
        OVOID, MAT_PLASTIC, "Lambertian", | 
| 34 | 
+ | 
        {0, 5, NULL, Lambfa}, NULL, | 
| 35 | 
+ | 
};                                      /* a Lambertian surface */ | 
| 36 | 
+ | 
 | 
| 37 | 
  | 
#define  MAXLOOP        128             /* modifier loop detection */ | 
| 38 | 
  | 
 | 
| 39 | 
  | 
#define  RAYHIT         (-1)            /* return value for intercepted ray */ | 
| 50 | 
  | 
                r->crtype = r->rtype = rt; | 
| 51 | 
  | 
                r->rsrc = -1; | 
| 52 | 
  | 
                r->clipset = NULL; | 
| 53 | 
+ | 
                r->revf = raytrace; | 
| 54 | 
  | 
        } else {                                /* spawned ray */ | 
| 55 | 
  | 
                r->rlvl = ro->rlvl; | 
| 56 | 
  | 
                if (rt & RAYREFL) { | 
| 61 | 
  | 
                        r->rsrc = ro->rsrc; | 
| 62 | 
  | 
                        r->clipset = ro->newcset; | 
| 63 | 
  | 
                } | 
| 64 | 
+ | 
                r->revf = ro->revf; | 
| 65 | 
  | 
                r->rweight = ro->rweight * rw; | 
| 66 | 
  | 
                r->crtype = ro->crtype | (r->rtype = rt); | 
| 67 | 
  | 
                VCOPY(r->rorg, ro->rop); | 
| 68 | 
  | 
        } | 
| 69 | 
< | 
        r->rno = nrays; | 
| 69 | 
> | 
        rayclear(r); | 
| 70 | 
> | 
        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); | 
| 71 | 
> | 
} | 
| 72 | 
> | 
 | 
| 73 | 
> | 
 | 
| 74 | 
> | 
rayclear(r)                     /* clear a ray for (re)evaluation */ | 
| 75 | 
> | 
register RAY  *r; | 
| 76 | 
> | 
{ | 
| 77 | 
> | 
        r->rno = raynum++; | 
| 78 | 
  | 
        r->newcset = r->clipset; | 
| 79 | 
  | 
        r->ro = NULL; | 
| 80 | 
  | 
        r->rot = FHUGE; | 
| 82 | 
  | 
        setcolor(r->pcol, 1.0, 1.0, 1.0); | 
| 83 | 
  | 
        setcolor(r->rcol, 0.0, 0.0, 0.0); | 
| 84 | 
  | 
        r->rt = 0.0; | 
| 63 | 
– | 
        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); | 
| 85 | 
  | 
} | 
| 86 | 
  | 
 | 
| 87 | 
  | 
 | 
| 88 | 
< | 
rayvalue(r)                     /* compute a ray's value */ | 
| 88 | 
> | 
raytrace(r)                     /* trace a ray and compute its value */ | 
| 89 | 
  | 
RAY  *r; | 
| 90 | 
  | 
{ | 
| 91 | 
  | 
        extern int  (*trace)(); | 
| 92 | 
  | 
 | 
| 93 | 
< | 
        if (localhit(r, &thescene) || sourcehit(r)) | 
| 93 | 
> | 
        if (localhit(r, &thescene)) | 
| 94 | 
  | 
                raycont(r); | 
| 95 | 
+ | 
        else if (sourcehit(r)) | 
| 96 | 
+ | 
                rayshade(r, r->ro->omod); | 
| 97 | 
  | 
 | 
| 98 | 
  | 
        if (trace != NULL) | 
| 99 | 
  | 
                (*trace)(r);            /* trace execution */ | 
| 133 | 
  | 
                                        /* check for infinite loop */ | 
| 134 | 
  | 
        if (depth++ >= MAXLOOP) | 
| 135 | 
  | 
                objerror(r->ro, USER, "possible modifier loop"); | 
| 136 | 
+ | 
        r->rt = r->rot;                 /* set effective ray length */ | 
| 137 | 
  | 
        for ( ; mod != OVOID; mod = m->omod) { | 
| 138 | 
  | 
                m = objptr(mod); | 
| 139 | 
  | 
                /****** unnecessary test since modifier() is always called | 
| 142 | 
  | 
                        error(USER, errmsg); | 
| 143 | 
  | 
                } | 
| 144 | 
  | 
                ******/ | 
| 145 | 
+ | 
                                        /* hack for irradiance calculation */ | 
| 146 | 
+ | 
                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { | 
| 147 | 
+ | 
                        if (irr_ignore(m->otype)) { | 
| 148 | 
+ | 
                                depth--; | 
| 149 | 
+ | 
                                raytrans(r); | 
| 150 | 
+ | 
                                return; | 
| 151 | 
+ | 
                        } | 
| 152 | 
+ | 
                        if (!islight(m->otype)) | 
| 153 | 
+ | 
                                m = &Lamb; | 
| 154 | 
+ | 
                } | 
| 155 | 
  | 
                (*ofun[m->otype].funp)(m, r);   /* execute function */ | 
| 122 | 
– | 
                m->lastrno = r->rno; | 
| 156 | 
  | 
                if (ismaterial(m->otype)) {     /* materials call raytexture */ | 
| 157 | 
  | 
                        depth--; | 
| 158 | 
  | 
                        return;         /* we're done */ | 
| 179 | 
  | 
                        error(USER, errmsg); | 
| 180 | 
  | 
                } | 
| 181 | 
  | 
                (*ofun[m->otype].funp)(m, r); | 
| 149 | 
– | 
                m->lastrno = r->rno; | 
| 182 | 
  | 
        } | 
| 183 | 
  | 
        depth--;                        /* end here */ | 
| 184 | 
  | 
} | 
| 284 | 
  | 
                if (rp->rox == &xp->xf) {               /* xp in use */ | 
| 285 | 
  | 
                        xp = xp->next;                  /* move to next */ | 
| 286 | 
  | 
                        if (xp == xflast) {             /* need new one */ | 
| 287 | 
< | 
                                xp = (struct xfn *)malloc(sizeof(struct xfn)); | 
| 287 | 
> | 
                                xp = (struct xfn *)bmalloc(sizeof(struct xfn)); | 
| 288 | 
  | 
                                if (xp == NULL) | 
| 289 | 
  | 
                                        error(SYSTEM, | 
| 290 | 
  | 
                                                "out of memory in newrayxf"); | 
| 318 | 
  | 
register RAY  *r; | 
| 319 | 
  | 
register CUBE  *scene; | 
| 320 | 
  | 
{ | 
| 321 | 
+ | 
        OBJECT  cxset[MAXCSET+1];       /* set of checked objects */ | 
| 322 | 
  | 
        FVECT  curpos;                  /* current cube position */ | 
| 323 | 
  | 
        int  sflags;                    /* sign flags */ | 
| 324 | 
  | 
        double  t, dt; | 
| 325 | 
  | 
        register int  i; | 
| 326 | 
  | 
 | 
| 327 | 
  | 
        nrays++;                        /* increment trace counter */ | 
| 295 | 
– | 
 | 
| 328 | 
  | 
        sflags = 0; | 
| 329 | 
  | 
        for (i = 0; i < 3; i++) { | 
| 330 | 
  | 
                curpos[i] = r->rorg[i]; | 
| 333 | 
  | 
                else if (r->rdir[i] < -FTINY) | 
| 334 | 
  | 
                        sflags |= 0x10 << i; | 
| 335 | 
  | 
        } | 
| 336 | 
+ | 
        if (sflags == 0) | 
| 337 | 
+ | 
                error(CONSISTENCY, "zero ray direction in localhit"); | 
| 338 | 
  | 
        t = 0.0; | 
| 339 | 
  | 
        if (!incube(scene, curpos)) { | 
| 340 | 
  | 
                                        /* find distance to entry */ | 
| 359 | 
  | 
                if (!incube(scene, curpos))     /* non-intersecting ray */ | 
| 360 | 
  | 
                        return(0); | 
| 361 | 
  | 
        } | 
| 362 | 
< | 
        return(raymove(curpos, sflags, r, scene) == RAYHIT); | 
| 362 | 
> | 
        cxset[0] = 0; | 
| 363 | 
> | 
        return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT); | 
| 364 | 
  | 
} | 
| 365 | 
  | 
 | 
| 366 | 
  | 
 | 
| 367 | 
  | 
static int | 
| 368 | 
< | 
raymove(pos, dirf, r, cu)               /* check for hit as we move */ | 
| 369 | 
< | 
FVECT  pos;                     /* modified */ | 
| 368 | 
> | 
raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */ | 
| 369 | 
> | 
FVECT  pos;                     /* current position, modified herein */ | 
| 370 | 
> | 
OBJECT  *cxs;                   /* checked objects, modified by checkhit */ | 
| 371 | 
  | 
int  dirf;                      /* direction indicators to speed tests */ | 
| 372 | 
  | 
register RAY  *r; | 
| 373 | 
  | 
register CUBE  *cu; | 
| 396 | 
  | 
                } | 
| 397 | 
  | 
                for ( ; ; ) { | 
| 398 | 
  | 
                        cukid.cutree = octkid(cu->cutree, br); | 
| 399 | 
< | 
                        if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT) | 
| 399 | 
> | 
                        if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT) | 
| 400 | 
  | 
                                return(RAYHIT); | 
| 401 | 
  | 
                        sgn = 1 << ax; | 
| 402 | 
  | 
                        if (sgn & dirf)                 /* positive axis? */ | 
| 415 | 
  | 
                } | 
| 416 | 
  | 
                /*NOTREACHED*/ | 
| 417 | 
  | 
        } | 
| 418 | 
< | 
        if (isfull(cu->cutree) && checkhit(r, cu)) | 
| 418 | 
> | 
        if (isfull(cu->cutree) && checkhit(r, cu, cxs)) | 
| 419 | 
  | 
                return(RAYHIT); | 
| 420 | 
  | 
                                        /* advance to next cube */ | 
| 421 | 
  | 
        if (dirf&0x11) { | 
| 448 | 
  | 
 | 
| 449 | 
  | 
 | 
| 450 | 
  | 
static | 
| 451 | 
< | 
checkhit(r, cu)                 /* check for hit in full cube */ | 
| 451 | 
> | 
checkhit(r, cu, cxs)            /* check for hit in full cube */ | 
| 452 | 
  | 
register RAY  *r; | 
| 453 | 
  | 
CUBE  *cu; | 
| 454 | 
+ | 
OBJECT  *cxs; | 
| 455 | 
  | 
{ | 
| 456 | 
  | 
        OBJECT  oset[MAXSET+1]; | 
| 457 | 
  | 
        register OBJREC  *o; | 
| 458 | 
  | 
        register int  i; | 
| 459 | 
  | 
 | 
| 460 | 
  | 
        objset(oset, cu->cutree); | 
| 461 | 
+ | 
        checkset(oset, cxs);                    /* eliminate double-checking */ | 
| 462 | 
  | 
        for (i = oset[0]; i > 0; i--) { | 
| 463 | 
  | 
                o = objptr(oset[i]); | 
| 464 | 
< | 
                if (o->lastrno == r->rno)               /* checked already? */ | 
| 465 | 
< | 
                        continue; | 
| 464 | 
> | 
                if (o->omod == OVOID && issurface(o->otype)) | 
| 465 | 
> | 
                        continue;               /* ignore void surfaces */ | 
| 466 | 
  | 
                (*ofun[o->otype].funp)(o, r); | 
| 429 | 
– | 
                o->lastrno = r->rno; | 
| 467 | 
  | 
        } | 
| 468 | 
  | 
        if (r->ro == NULL) | 
| 469 | 
  | 
                return(0);                      /* no scores yet */ | 
| 470 | 
  | 
 | 
| 471 | 
  | 
        return(incube(cu, r->rop));             /* hit OK if in current cube */ | 
| 472 | 
+ | 
} | 
| 473 | 
+ | 
 | 
| 474 | 
+ | 
 | 
| 475 | 
+ | 
static | 
| 476 | 
+ | 
checkset(os, cs)                /* modify checked set and set to check */ | 
| 477 | 
+ | 
register OBJECT  *os;                   /* os' = os - cs */ | 
| 478 | 
+ | 
register OBJECT  *cs;                   /* cs' = cs + os */ | 
| 479 | 
+ | 
{ | 
| 480 | 
+ | 
        OBJECT  cset[MAXCSET+MAXSET+1]; | 
| 481 | 
+ | 
        register int  i, j; | 
| 482 | 
+ | 
        int  k; | 
| 483 | 
+ | 
                                        /* copy os in place, cset <- cs */ | 
| 484 | 
+ | 
        cset[0] = 0; | 
| 485 | 
+ | 
        k = 0; | 
| 486 | 
+ | 
        for (i = j = 1; i <= os[0]; i++) { | 
| 487 | 
+ | 
                while (j <= cs[0] && cs[j] < os[i]) | 
| 488 | 
+ | 
                        cset[++cset[0]] = cs[j++]; | 
| 489 | 
+ | 
                if (j > cs[0] || os[i] != cs[j]) {      /* object to check */ | 
| 490 | 
+ | 
                        os[++k] = os[i]; | 
| 491 | 
+ | 
                        cset[++cset[0]] = os[i]; | 
| 492 | 
+ | 
                } | 
| 493 | 
+ | 
        } | 
| 494 | 
+ | 
        if (!(os[0] = k))               /* new "to check" set size */ | 
| 495 | 
+ | 
                return;                 /* special case */ | 
| 496 | 
+ | 
        while (j <= cs[0])              /* get the rest of cs */ | 
| 497 | 
+ | 
                cset[++cset[0]] = cs[j++]; | 
| 498 | 
+ | 
        if (cset[0] > MAXCSET)          /* truncate "checked" set if nec. */ | 
| 499 | 
+ | 
                cset[0] = MAXCSET; | 
| 500 | 
+ | 
        /* setcopy(cs, cset); */        /* copy cset back to cs */ | 
| 501 | 
+ | 
        os = cset; | 
| 502 | 
+ | 
        for (i = os[0]; i-- >= 0; ) | 
| 503 | 
+ | 
                *cs++ = *os++; | 
| 504 | 
  | 
} |