| 1 | 
– | 
/* Copyright (c) 1986 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 | 
+ | 
/* ==================================================================== | 
| 11 | 
+ | 
 * The Radiance Software License, Version 1.0 | 
| 12 | 
+ | 
 * | 
| 13 | 
+ | 
 * Copyright (c) 1990 - 2002 The Regents of the University of California, | 
| 14 | 
+ | 
 * through Lawrence Berkeley National Laboratory.   All rights reserved. | 
| 15 | 
+ | 
 * | 
| 16 | 
+ | 
 * Redistribution and use in source and binary forms, with or without | 
| 17 | 
+ | 
 * modification, are permitted provided that the following conditions | 
| 18 | 
+ | 
 * are met: | 
| 19 | 
+ | 
 * | 
| 20 | 
+ | 
 * 1. Redistributions of source code must retain the above copyright | 
| 21 | 
+ | 
 *         notice, this list of conditions and the following disclaimer. | 
| 22 | 
+ | 
 * | 
| 23 | 
+ | 
 * 2. Redistributions in binary form must reproduce the above copyright | 
| 24 | 
+ | 
 *       notice, this list of conditions and the following disclaimer in | 
| 25 | 
+ | 
 *       the documentation and/or other materials provided with the | 
| 26 | 
+ | 
 *       distribution. | 
| 27 | 
+ | 
 * | 
| 28 | 
+ | 
 * 3. The end-user documentation included with the redistribution, | 
| 29 | 
+ | 
 *           if any, must include the following acknowledgment: | 
| 30 | 
+ | 
 *             "This product includes Radiance software | 
| 31 | 
+ | 
 *                 (http://radsite.lbl.gov/) | 
| 32 | 
+ | 
 *                 developed by the Lawrence Berkeley National Laboratory | 
| 33 | 
+ | 
 *               (http://www.lbl.gov/)." | 
| 34 | 
+ | 
 *       Alternately, this acknowledgment may appear in the software itself, | 
| 35 | 
+ | 
 *       if and wherever such third-party acknowledgments normally appear. | 
| 36 | 
+ | 
 * | 
| 37 | 
+ | 
 * 4. The names "Radiance," "Lawrence Berkeley National Laboratory" | 
| 38 | 
+ | 
 *       and "The Regents of the University of California" must | 
| 39 | 
+ | 
 *       not be used to endorse or promote products derived from this | 
| 40 | 
+ | 
 *       software without prior written permission. For written | 
| 41 | 
+ | 
 *       permission, please contact [email protected]. | 
| 42 | 
+ | 
 * | 
| 43 | 
+ | 
 * 5. Products derived from this software may not be called "Radiance", | 
| 44 | 
+ | 
 *       nor may "Radiance" appear in their name, without prior written | 
| 45 | 
+ | 
 *       permission of Lawrence Berkeley National Laboratory. | 
| 46 | 
+ | 
 * | 
| 47 | 
+ | 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | 
| 48 | 
+ | 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
| 49 | 
+ | 
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
| 50 | 
+ | 
 * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR | 
| 51 | 
+ | 
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
| 52 | 
+ | 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
| 53 | 
+ | 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | 
| 54 | 
+ | 
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 
| 55 | 
+ | 
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
| 56 | 
+ | 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
| 57 | 
+ | 
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
| 58 | 
+ | 
 * SUCH DAMAGE. | 
| 59 | 
+ | 
 * ==================================================================== | 
| 60 | 
+ | 
 * | 
| 61 | 
+ | 
 * This software consists of voluntary contributions made by many | 
| 62 | 
+ | 
 * individuals on behalf of Lawrence Berkeley National Laboratory.   For more | 
| 63 | 
+ | 
 * information on Lawrence Berkeley National Laboratory, please see | 
| 64 | 
+ | 
 * <http://www.lbl.gov/>. | 
| 65 | 
+ | 
 */ | 
| 66 | 
+ | 
 | 
| 67 | 
  | 
#include  "ray.h" | 
| 68 | 
  | 
 | 
| 15 | 
– | 
#include  "octree.h" | 
| 16 | 
– | 
 | 
| 69 | 
  | 
#include  "otypes.h" | 
| 70 | 
  | 
 | 
| 71 | 
< | 
extern CUBE  thescene;                  /* our scene */ | 
| 20 | 
< | 
extern int  maxdepth;                   /* maximum recursion depth */ | 
| 21 | 
< | 
extern double  minweight;               /* minimum ray weight */ | 
| 71 | 
> | 
#include  "otspecial.h" | 
| 72 | 
  | 
 | 
| 73 | 
< | 
long  nrays = 0L;                       /* number of rays traced */ | 
| 73 | 
> | 
#define  MAXCSET        ((MAXSET+1)*2-1)        /* maximum check set size */ | 
| 74 | 
  | 
 | 
| 75 | 
< | 
#define  MAXLOOP        128             /* modifier loop detection */ | 
| 75 | 
> | 
unsigned long  raynum = 0;              /* next unique ray number */ | 
| 76 | 
> | 
unsigned long  nrays = 0;               /* number of calls to localhit */ | 
| 77 | 
  | 
 | 
| 78 | 
+ | 
static FLOAT  Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; | 
| 79 | 
+ | 
OBJREC  Lamb = { | 
| 80 | 
+ | 
        OVOID, MAT_PLASTIC, "Lambertian", | 
| 81 | 
+ | 
        {0, 5, NULL, Lambfa}, NULL, | 
| 82 | 
+ | 
};                                      /* a Lambertian surface */ | 
| 83 | 
+ | 
 | 
| 84 | 
+ | 
OBJREC  Aftplane;                       /* aft clipping plane object */ | 
| 85 | 
+ | 
 | 
| 86 | 
+ | 
static int  raymove(), checkhit(); | 
| 87 | 
+ | 
static void  checkset(); | 
| 88 | 
+ | 
 | 
| 89 | 
+ | 
#ifndef  MAXLOOP | 
| 90 | 
+ | 
#define  MAXLOOP        0               /* modifier loop detection */ | 
| 91 | 
+ | 
#endif | 
| 92 | 
+ | 
 | 
| 93 | 
  | 
#define  RAYHIT         (-1)            /* return value for intercepted ray */ | 
| 94 | 
  | 
 | 
| 95 | 
  | 
 | 
| 96 | 
+ | 
int | 
| 97 | 
  | 
rayorigin(r, ro, rt, rw)                /* start new ray from old one */ | 
| 98 | 
  | 
register RAY  *r, *ro; | 
| 99 | 
  | 
int  rt; | 
| 100 | 
  | 
double  rw; | 
| 101 | 
  | 
{ | 
| 102 | 
+ | 
        double  re; | 
| 103 | 
+ | 
 | 
| 104 | 
  | 
        if ((r->parent = ro) == NULL) {         /* primary ray */ | 
| 105 | 
  | 
                r->rlvl = 0; | 
| 106 | 
  | 
                r->rweight = rw; | 
| 107 | 
  | 
                r->crtype = r->rtype = rt; | 
| 108 | 
  | 
                r->rsrc = -1; | 
| 109 | 
  | 
                r->clipset = NULL; | 
| 110 | 
+ | 
                r->revf = raytrace; | 
| 111 | 
+ | 
                copycolor(r->cext, cextinction); | 
| 112 | 
+ | 
                copycolor(r->albedo, salbedo); | 
| 113 | 
+ | 
                r->gecc = seccg; | 
| 114 | 
+ | 
                r->slights = NULL; | 
| 115 | 
  | 
        } else {                                /* spawned ray */ | 
| 116 | 
  | 
                r->rlvl = ro->rlvl; | 
| 117 | 
  | 
                if (rt & RAYREFL) { | 
| 118 | 
  | 
                        r->rlvl++; | 
| 119 | 
  | 
                        r->rsrc = -1; | 
| 120 | 
  | 
                        r->clipset = ro->clipset; | 
| 121 | 
+ | 
                        r->rmax = 0.0; | 
| 122 | 
  | 
                } else { | 
| 123 | 
  | 
                        r->rsrc = ro->rsrc; | 
| 124 | 
  | 
                        r->clipset = ro->newcset; | 
| 125 | 
+ | 
                        r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot; | 
| 126 | 
  | 
                } | 
| 127 | 
< | 
                r->rweight = ro->rweight * rw; | 
| 127 | 
> | 
                r->revf = ro->revf; | 
| 128 | 
> | 
                copycolor(r->cext, ro->cext); | 
| 129 | 
> | 
                copycolor(r->albedo, ro->albedo); | 
| 130 | 
> | 
                r->gecc = ro->gecc; | 
| 131 | 
> | 
                r->slights = ro->slights; | 
| 132 | 
  | 
                r->crtype = ro->crtype | (r->rtype = rt); | 
| 133 | 
  | 
                VCOPY(r->rorg, ro->rop); | 
| 134 | 
+ | 
                r->rweight = ro->rweight * rw; | 
| 135 | 
+ | 
                                                /* estimate absorption */ | 
| 136 | 
+ | 
                re = colval(ro->cext,RED) < colval(ro->cext,GRN) ? | 
| 137 | 
+ | 
                                colval(ro->cext,RED) : colval(ro->cext,GRN); | 
| 138 | 
+ | 
                if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU); | 
| 139 | 
+ | 
                if (re > 0.) | 
| 140 | 
+ | 
                        r->rweight *= exp(-re*ro->rot); | 
| 141 | 
  | 
        } | 
| 142 | 
< | 
        r->rno = nrays; | 
| 142 | 
> | 
        rayclear(r); | 
| 143 | 
> | 
        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); | 
| 144 | 
> | 
} | 
| 145 | 
> | 
 | 
| 146 | 
> | 
 | 
| 147 | 
> | 
void | 
| 148 | 
> | 
rayclear(r)                     /* clear a ray for (re)evaluation */ | 
| 149 | 
> | 
register RAY  *r; | 
| 150 | 
> | 
{ | 
| 151 | 
> | 
        r->rno = raynum++; | 
| 152 | 
  | 
        r->newcset = r->clipset; | 
| 153 | 
+ | 
        r->robj = OVOID; | 
| 154 | 
  | 
        r->ro = NULL; | 
| 155 | 
< | 
        r->rot = FHUGE; | 
| 155 | 
> | 
        r->rox = NULL; | 
| 156 | 
> | 
        r->rt = r->rot = FHUGE; | 
| 157 | 
  | 
        r->pert[0] = r->pert[1] = r->pert[2] = 0.0; | 
| 158 | 
  | 
        setcolor(r->pcol, 1.0, 1.0, 1.0); | 
| 159 | 
  | 
        setcolor(r->rcol, 0.0, 0.0, 0.0); | 
| 62 | 
– | 
        return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); | 
| 160 | 
  | 
} | 
| 161 | 
  | 
 | 
| 162 | 
  | 
 | 
| 163 | 
< | 
rayvalue(r)                     /* compute a ray's value */ | 
| 163 | 
> | 
void | 
| 164 | 
> | 
raytrace(r)                     /* trace a ray and compute its value */ | 
| 165 | 
  | 
RAY  *r; | 
| 166 | 
  | 
{ | 
| 167 | 
< | 
        extern int  (*trace)(); | 
| 167 | 
> | 
        if (localhit(r, &thescene)) | 
| 168 | 
> | 
                raycont(r);             /* hit local surface, evaluate */ | 
| 169 | 
> | 
        else if (r->ro == &Aftplane) { | 
| 170 | 
> | 
                r->ro = NULL;           /* hit aft clipping plane */ | 
| 171 | 
> | 
                r->rot = FHUGE; | 
| 172 | 
> | 
        } else if (sourcehit(r)) | 
| 173 | 
> | 
                rayshade(r, r->ro->omod);       /* distant source */ | 
| 174 | 
  | 
 | 
| 175 | 
< | 
        if (localhit(r, &thescene) || sourcehit(r)) | 
| 72 | 
< | 
                raycont(r); | 
| 175 | 
> | 
        rayparticipate(r);              /* for participating medium */ | 
| 176 | 
  | 
 | 
| 177 | 
  | 
        if (trace != NULL) | 
| 178 | 
  | 
                (*trace)(r);            /* trace execution */ | 
| 179 | 
  | 
} | 
| 180 | 
  | 
 | 
| 181 | 
  | 
 | 
| 182 | 
+ | 
void | 
| 183 | 
  | 
raycont(r)                      /* check for clipped object and continue */ | 
| 184 | 
  | 
register RAY  *r; | 
| 185 | 
  | 
{ | 
| 186 | 
< | 
        if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) | 
| 186 | 
> | 
        if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || | 
| 187 | 
> | 
                        !rayshade(r, r->ro->omod)) | 
| 188 | 
  | 
                raytrans(r); | 
| 84 | 
– | 
        else | 
| 85 | 
– | 
                rayshade(r, r->ro->omod); | 
| 189 | 
  | 
} | 
| 190 | 
  | 
 | 
| 191 | 
  | 
 | 
| 192 | 
+ | 
void | 
| 193 | 
  | 
raytrans(r)                     /* transmit ray as is */ | 
| 194 | 
  | 
register RAY  *r; | 
| 195 | 
  | 
{ | 
| 199 | 
  | 
                VCOPY(tr.rdir, r->rdir); | 
| 200 | 
  | 
                rayvalue(&tr); | 
| 201 | 
  | 
                copycolor(r->rcol, tr.rcol); | 
| 202 | 
+ | 
                r->rt = r->rot + tr.rt; | 
| 203 | 
  | 
        } | 
| 204 | 
  | 
} | 
| 205 | 
  | 
 | 
| 206 | 
  | 
 | 
| 207 | 
+ | 
int | 
| 208 | 
  | 
rayshade(r, mod)                /* shade ray r with material mod */ | 
| 209 | 
  | 
register RAY  *r; | 
| 210 | 
  | 
int  mod; | 
| 211 | 
  | 
{ | 
| 212 | 
< | 
        static int  depth = 0; | 
| 212 | 
> | 
        int  gotmat; | 
| 213 | 
  | 
        register OBJREC  *m; | 
| 214 | 
+ | 
#if  MAXLOOP | 
| 215 | 
+ | 
        static int  depth = 0; | 
| 216 | 
  | 
                                        /* check for infinite loop */ | 
| 217 | 
  | 
        if (depth++ >= MAXLOOP) | 
| 218 | 
  | 
                objerror(r->ro, USER, "possible modifier loop"); | 
| 219 | 
< | 
        for ( ; mod != OVOID; mod = m->omod) { | 
| 219 | 
> | 
#endif | 
| 220 | 
> | 
        r->rt = r->rot;                 /* set effective ray length */ | 
| 221 | 
> | 
        for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { | 
| 222 | 
  | 
                m = objptr(mod); | 
| 223 | 
  | 
                /****** unnecessary test since modifier() is always called | 
| 224 | 
  | 
                if (!ismodifier(m->otype)) { | 
| 226 | 
  | 
                        error(USER, errmsg); | 
| 227 | 
  | 
                } | 
| 228 | 
  | 
                ******/ | 
| 229 | 
< | 
                (*ofun[m->otype].funp)(m, r);   /* execute function */ | 
| 230 | 
< | 
                m->lastrno = r->rno; | 
| 231 | 
< | 
                if (ismaterial(m->otype)) {     /* materials call raytexture */ | 
| 232 | 
< | 
                        depth--; | 
| 233 | 
< | 
                        return;         /* we're done */ | 
| 229 | 
> | 
                                        /* hack for irradiance calculation */ | 
| 230 | 
> | 
                if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { | 
| 231 | 
> | 
                        if (irr_ignore(m->otype)) { | 
| 232 | 
> | 
#if  MAXLOOP | 
| 233 | 
> | 
                                depth--; | 
| 234 | 
> | 
#endif | 
| 235 | 
> | 
                                raytrans(r); | 
| 236 | 
> | 
                                return(1); | 
| 237 | 
> | 
                        } | 
| 238 | 
> | 
                        if (!islight(m->otype)) | 
| 239 | 
> | 
                                m = &Lamb; | 
| 240 | 
  | 
                } | 
| 241 | 
+ | 
                                        /* materials call raytexture */ | 
| 242 | 
+ | 
                gotmat = (*ofun[m->otype].funp)(m, r); | 
| 243 | 
  | 
        } | 
| 244 | 
< | 
        objerror(r->ro, USER, "material not found"); | 
| 244 | 
> | 
#if  MAXLOOP | 
| 245 | 
> | 
        depth--; | 
| 246 | 
> | 
#endif | 
| 247 | 
> | 
        return(gotmat); | 
| 248 | 
  | 
} | 
| 249 | 
  | 
 | 
| 250 | 
  | 
 | 
| 251 | 
+ | 
void | 
| 252 | 
+ | 
rayparticipate(r)                       /* compute ray medium participation */ | 
| 253 | 
+ | 
register RAY  *r; | 
| 254 | 
+ | 
{ | 
| 255 | 
+ | 
        COLOR   ce, ca; | 
| 256 | 
+ | 
        double  re, ge, be; | 
| 257 | 
+ | 
 | 
| 258 | 
+ | 
        if (intens(r->cext) <= 1./FHUGE) | 
| 259 | 
+ | 
                return;                         /* no medium */ | 
| 260 | 
+ | 
        re = r->rot*colval(r->cext,RED); | 
| 261 | 
+ | 
        ge = r->rot*colval(r->cext,GRN); | 
| 262 | 
+ | 
        be = r->rot*colval(r->cext,BLU); | 
| 263 | 
+ | 
        if (r->crtype & SHADOW) {               /* no scattering for sources */ | 
| 264 | 
+ | 
                re *= 1. - colval(r->albedo,RED); | 
| 265 | 
+ | 
                ge *= 1. - colval(r->albedo,GRN); | 
| 266 | 
+ | 
                be *= 1. - colval(r->albedo,BLU); | 
| 267 | 
+ | 
        } | 
| 268 | 
+ | 
        setcolor(ce,    re<=0. ? 1. : re>92. ? 0. : exp(-re), | 
| 269 | 
+ | 
                        ge<=0. ? 1. : ge>92. ? 0. : exp(-ge), | 
| 270 | 
+ | 
                        be<=0. ? 1. : be>92. ? 0. : exp(-be)); | 
| 271 | 
+ | 
        multcolor(r->rcol, ce);                 /* path absorption */ | 
| 272 | 
+ | 
        if (r->crtype & SHADOW || intens(r->albedo) <= FTINY) | 
| 273 | 
+ | 
                return;                         /* no scattering */ | 
| 274 | 
+ | 
        setcolor(ca, | 
| 275 | 
+ | 
                colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)), | 
| 276 | 
+ | 
                colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)), | 
| 277 | 
+ | 
                colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU))); | 
| 278 | 
+ | 
        addcolor(r->rcol, ca);                  /* ambient in scattering */ | 
| 279 | 
+ | 
        srcscatter(r);                          /* source in scattering */ | 
| 280 | 
+ | 
} | 
| 281 | 
+ | 
 | 
| 282 | 
+ | 
 | 
| 283 | 
  | 
raytexture(r, mod)                      /* get material modifiers */ | 
| 284 | 
  | 
RAY  *r; | 
| 285 | 
  | 
int  mod; | 
| 286 | 
  | 
{ | 
| 134 | 
– | 
        static int  depth = 0; | 
| 287 | 
  | 
        register OBJREC  *m; | 
| 288 | 
+ | 
#if  MAXLOOP | 
| 289 | 
+ | 
        static int  depth = 0; | 
| 290 | 
  | 
                                        /* check for infinite loop */ | 
| 291 | 
  | 
        if (depth++ >= MAXLOOP) | 
| 292 | 
  | 
                objerror(r->ro, USER, "modifier loop"); | 
| 293 | 
+ | 
#endif | 
| 294 | 
  | 
                                        /* execute textures and patterns */ | 
| 295 | 
  | 
        for ( ; mod != OVOID; mod = m->omod) { | 
| 296 | 
  | 
                m = objptr(mod); | 
| 297 | 
< | 
                if (!istexture(m->otype)) { | 
| 297 | 
> | 
                /****** unnecessary test since modifier() is always called | 
| 298 | 
> | 
                if (!ismodifier(m->otype)) { | 
| 299 | 
  | 
                        sprintf(errmsg, "illegal modifier \"%s\"", m->oname); | 
| 300 | 
  | 
                        error(USER, errmsg); | 
| 301 | 
  | 
                } | 
| 302 | 
< | 
                (*ofun[m->otype].funp)(m, r); | 
| 303 | 
< | 
                m->lastrno = r->rno; | 
| 302 | 
> | 
                ******/ | 
| 303 | 
> | 
                if ((*ofun[m->otype].funp)(m, r)) { | 
| 304 | 
> | 
                        sprintf(errmsg, "conflicting material \"%s\"", | 
| 305 | 
> | 
                                        m->oname); | 
| 306 | 
> | 
                        objerror(r->ro, USER, errmsg); | 
| 307 | 
> | 
                } | 
| 308 | 
  | 
        } | 
| 309 | 
+ | 
#if  MAXLOOP | 
| 310 | 
  | 
        depth--;                        /* end here */ | 
| 311 | 
+ | 
#endif | 
| 312 | 
  | 
} | 
| 313 | 
  | 
 | 
| 314 | 
  | 
 | 
| 315 | 
+ | 
int | 
| 316 | 
  | 
raymixture(r, fore, back, coef)         /* mix modifiers */ | 
| 317 | 
  | 
register RAY  *r; | 
| 318 | 
  | 
OBJECT  fore, back; | 
| 319 | 
  | 
double  coef; | 
| 320 | 
  | 
{ | 
| 321 | 
< | 
        FVECT  curpert, forepert, backpert; | 
| 322 | 
< | 
        COLOR  curpcol, forepcol, backpcol; | 
| 321 | 
> | 
        RAY  fr, br; | 
| 322 | 
> | 
        int  foremat, backmat; | 
| 323 | 
  | 
        register int  i; | 
| 324 | 
< | 
                                        /* clip coefficient */ | 
| 324 | 
> | 
                                        /* bound coefficient */ | 
| 325 | 
  | 
        if (coef > 1.0) | 
| 326 | 
  | 
                coef = 1.0; | 
| 327 | 
  | 
        else if (coef < 0.0) | 
| 328 | 
  | 
                coef = 0.0; | 
| 329 | 
< | 
                                        /* save current mods */ | 
| 330 | 
< | 
        VCOPY(curpert, r->pert); | 
| 331 | 
< | 
        copycolor(curpcol, r->pcol); | 
| 332 | 
< | 
                                        /* compute new mods */ | 
| 333 | 
< | 
                                                /* foreground */ | 
| 334 | 
< | 
        r->pert[0] = r->pert[1] = r->pert[2] = 0.0; | 
| 335 | 
< | 
        setcolor(r->pcol, 1.0, 1.0, 1.0); | 
| 336 | 
< | 
        if (fore != OVOID && coef > FTINY) | 
| 337 | 
< | 
                raytexture(r, fore); | 
| 338 | 
< | 
        VCOPY(forepert, r->pert); | 
| 339 | 
< | 
        copycolor(forepcol, r->pcol); | 
| 340 | 
< | 
                                                /* background */ | 
| 341 | 
< | 
        r->pert[0] = r->pert[1] = r->pert[2] = 0.0; | 
| 342 | 
< | 
        setcolor(r->pcol, 1.0, 1.0, 1.0); | 
| 343 | 
< | 
        if (back != OVOID && coef < 1.0-FTINY) | 
| 344 | 
< | 
                raytexture(r, back); | 
| 345 | 
< | 
        VCOPY(backpert, r->pert); | 
| 183 | 
< | 
        copycolor(backpcol, r->pcol); | 
| 184 | 
< | 
                                        /* sum perturbations */ | 
| 329 | 
> | 
                                        /* compute foreground and background */ | 
| 330 | 
> | 
        foremat = backmat = 0; | 
| 331 | 
> | 
                                        /* foreground */ | 
| 332 | 
> | 
        copystruct(&fr, r); | 
| 333 | 
> | 
        if (coef > FTINY) | 
| 334 | 
> | 
                foremat = rayshade(&fr, fore); | 
| 335 | 
> | 
                                        /* background */ | 
| 336 | 
> | 
        copystruct(&br, r); | 
| 337 | 
> | 
        if (coef < 1.0-FTINY) | 
| 338 | 
> | 
                backmat = rayshade(&br, back); | 
| 339 | 
> | 
                                        /* check for transparency */ | 
| 340 | 
> | 
        if (backmat ^ foremat) | 
| 341 | 
> | 
                if (backmat && coef > FTINY) | 
| 342 | 
> | 
                        raytrans(&fr); | 
| 343 | 
> | 
                else if (foremat && coef < 1.0-FTINY) | 
| 344 | 
> | 
                        raytrans(&br); | 
| 345 | 
> | 
                                        /* mix perturbations */ | 
| 346 | 
  | 
        for (i = 0; i < 3; i++) | 
| 347 | 
< | 
                r->pert[i] = curpert[i] + coef*forepert[i] + | 
| 348 | 
< | 
                                (1.0-coef)*backpert[i]; | 
| 349 | 
< | 
                                        /* multiply colors */ | 
| 350 | 
< | 
        setcolor(r->pcol, coef*colval(forepcol,RED) + | 
| 351 | 
< | 
                                (1.0-coef)*colval(backpcol,RED), | 
| 352 | 
< | 
                        coef*colval(forepcol,GRN) + | 
| 353 | 
< | 
                                (1.0-coef)*colval(backpcol,GRN), | 
| 354 | 
< | 
                        coef*colval(forepcol,BLU) + | 
| 355 | 
< | 
                                (1.0-coef)*colval(backpcol,BLU)); | 
| 356 | 
< | 
        multcolor(r->pcol, curpcol); | 
| 347 | 
> | 
                r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; | 
| 348 | 
> | 
                                        /* mix pattern colors */ | 
| 349 | 
> | 
        scalecolor(fr.pcol, coef); | 
| 350 | 
> | 
        scalecolor(br.pcol, 1.0-coef); | 
| 351 | 
> | 
        copycolor(r->pcol, fr.pcol); | 
| 352 | 
> | 
        addcolor(r->pcol, br.pcol); | 
| 353 | 
> | 
                                        /* return value tells if material */ | 
| 354 | 
> | 
        if (!foremat & !backmat) | 
| 355 | 
> | 
                return(0); | 
| 356 | 
> | 
                                        /* mix returned ray values */ | 
| 357 | 
> | 
        scalecolor(fr.rcol, coef); | 
| 358 | 
> | 
        scalecolor(br.rcol, 1.0-coef); | 
| 359 | 
> | 
        copycolor(r->rcol, fr.rcol); | 
| 360 | 
> | 
        addcolor(r->rcol, br.rcol); | 
| 361 | 
> | 
        r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt; | 
| 362 | 
> | 
        return(1); | 
| 363 | 
  | 
} | 
| 364 | 
  | 
 | 
| 365 | 
  | 
 | 
| 366 | 
  | 
double | 
| 367 | 
+ | 
raydist(r, flags)               /* compute (cumulative) ray distance */ | 
| 368 | 
+ | 
register RAY  *r; | 
| 369 | 
+ | 
register int  flags; | 
| 370 | 
+ | 
{ | 
| 371 | 
+ | 
        double  sum = 0.0; | 
| 372 | 
+ | 
 | 
| 373 | 
+ | 
        while (r != NULL && r->crtype&flags) { | 
| 374 | 
+ | 
                sum += r->rot; | 
| 375 | 
+ | 
                r = r->parent; | 
| 376 | 
+ | 
        } | 
| 377 | 
+ | 
        return(sum); | 
| 378 | 
+ | 
} | 
| 379 | 
+ | 
 | 
| 380 | 
+ | 
 | 
| 381 | 
+ | 
double | 
| 382 | 
  | 
raynormal(norm, r)              /* compute perturbed normal for ray */ | 
| 383 | 
  | 
FVECT  norm; | 
| 384 | 
  | 
register RAY  *r; | 
| 392 | 
  | 
         *  still fraught with problems since reflected rays and similar | 
| 393 | 
  | 
         *  directions calculated from the surface normal may spawn rays behind | 
| 394 | 
  | 
         *  the surface.  The only solution is to curb textures at high | 
| 395 | 
< | 
         *  incidence (Rdot << 1). | 
| 395 | 
> | 
         *  incidence (namely, keep DOT(rdir,pert) < Rdot). | 
| 396 | 
  | 
         */ | 
| 397 | 
  | 
 | 
| 398 | 
  | 
        for (i = 0; i < 3; i++) | 
| 413 | 
  | 
} | 
| 414 | 
  | 
 | 
| 415 | 
  | 
 | 
| 416 | 
+ | 
void | 
| 417 | 
+ | 
newrayxf(r)                     /* get new tranformation matrix for ray */ | 
| 418 | 
+ | 
RAY  *r; | 
| 419 | 
+ | 
{ | 
| 420 | 
+ | 
        static struct xfn { | 
| 421 | 
+ | 
                struct xfn  *next; | 
| 422 | 
+ | 
                FULLXF  xf; | 
| 423 | 
+ | 
        }  xfseed = { &xfseed }, *xflast = &xfseed; | 
| 424 | 
+ | 
        register struct xfn  *xp; | 
| 425 | 
+ | 
        register RAY  *rp; | 
| 426 | 
+ | 
 | 
| 427 | 
+ | 
        /* | 
| 428 | 
+ | 
         * Search for transform in circular list that | 
| 429 | 
+ | 
         * has no associated ray in the tree. | 
| 430 | 
+ | 
         */ | 
| 431 | 
+ | 
        xp = xflast; | 
| 432 | 
+ | 
        for (rp = r->parent; rp != NULL; rp = rp->parent) | 
| 433 | 
+ | 
                if (rp->rox == &xp->xf) {               /* xp in use */ | 
| 434 | 
+ | 
                        xp = xp->next;                  /* move to next */ | 
| 435 | 
+ | 
                        if (xp == xflast) {             /* need new one */ | 
| 436 | 
+ | 
                                xp = (struct xfn *)malloc(sizeof(struct xfn)); | 
| 437 | 
+ | 
                                if (xp == NULL) | 
| 438 | 
+ | 
                                        error(SYSTEM, | 
| 439 | 
+ | 
                                                "out of memory in newrayxf"); | 
| 440 | 
+ | 
                                                        /* insert in list */ | 
| 441 | 
+ | 
                                xp->next = xflast->next; | 
| 442 | 
+ | 
                                xflast->next = xp; | 
| 443 | 
+ | 
                                break;                  /* we're done */ | 
| 444 | 
+ | 
                        } | 
| 445 | 
+ | 
                        rp = r;                 /* start check over */ | 
| 446 | 
+ | 
                } | 
| 447 | 
+ | 
                                        /* got it */ | 
| 448 | 
+ | 
        r->rox = &xp->xf; | 
| 449 | 
+ | 
        xflast = xp; | 
| 450 | 
+ | 
} | 
| 451 | 
+ | 
 | 
| 452 | 
+ | 
 | 
| 453 | 
+ | 
void | 
| 454 | 
  | 
flipsurface(r)                  /* reverse surface orientation */ | 
| 455 | 
  | 
register RAY  *r; | 
| 456 | 
  | 
{ | 
| 464 | 
  | 
} | 
| 465 | 
  | 
 | 
| 466 | 
  | 
 | 
| 467 | 
+ | 
int | 
| 468 | 
  | 
localhit(r, scene)              /* check for hit in the octree */ | 
| 469 | 
  | 
register RAY  *r; | 
| 470 | 
  | 
register CUBE  *scene; | 
| 471 | 
  | 
{ | 
| 472 | 
+ | 
        OBJECT  cxset[MAXCSET+1];       /* set of checked objects */ | 
| 473 | 
  | 
        FVECT  curpos;                  /* current cube position */ | 
| 474 | 
< | 
        int  mpos, mneg;                /* sign flags */ | 
| 474 | 
> | 
        int  sflags;                    /* sign flags */ | 
| 475 | 
  | 
        double  t, dt; | 
| 476 | 
  | 
        register int  i; | 
| 477 | 
  | 
 | 
| 478 | 
  | 
        nrays++;                        /* increment trace counter */ | 
| 479 | 
< | 
 | 
| 258 | 
< | 
        mpos = mneg = 0; | 
| 479 | 
> | 
        sflags = 0; | 
| 480 | 
  | 
        for (i = 0; i < 3; i++) { | 
| 481 | 
  | 
                curpos[i] = r->rorg[i]; | 
| 482 | 
< | 
                if (r->rdir[i] > FTINY) | 
| 483 | 
< | 
                        mpos |= 1 << i; | 
| 484 | 
< | 
                else if (r->rdir[i] < -FTINY) | 
| 485 | 
< | 
                        mneg |= 1 << i; | 
| 482 | 
> | 
                if (r->rdir[i] > 1e-7) | 
| 483 | 
> | 
                        sflags |= 1 << i; | 
| 484 | 
> | 
                else if (r->rdir[i] < -1e-7) | 
| 485 | 
> | 
                        sflags |= 0x10 << i; | 
| 486 | 
  | 
        } | 
| 487 | 
+ | 
        if (sflags == 0) | 
| 488 | 
+ | 
                error(CONSISTENCY, "zero ray direction in localhit"); | 
| 489 | 
+ | 
                                        /* start off assuming nothing hit */ | 
| 490 | 
+ | 
        if (r->rmax > FTINY) {          /* except aft plane if one */ | 
| 491 | 
+ | 
                r->ro = &Aftplane; | 
| 492 | 
+ | 
                r->rot = r->rmax; | 
| 493 | 
+ | 
                for (i = 0; i < 3; i++) | 
| 494 | 
+ | 
                        r->rop[i] = r->rorg[i] + r->rot*r->rdir[i]; | 
| 495 | 
+ | 
        } | 
| 496 | 
+ | 
                                        /* find global cube entrance point */ | 
| 497 | 
  | 
        t = 0.0; | 
| 498 | 
  | 
        if (!incube(scene, curpos)) { | 
| 499 | 
  | 
                                        /* find distance to entry */ | 
| 500 | 
  | 
                for (i = 0; i < 3; i++) { | 
| 501 | 
  | 
                                        /* plane in our direction */ | 
| 502 | 
< | 
                        if (mpos & 1<<i) | 
| 502 | 
> | 
                        if (sflags & 1<<i) | 
| 503 | 
  | 
                                dt = scene->cuorg[i]; | 
| 504 | 
< | 
                        else if (mneg & 1<<i) | 
| 504 | 
> | 
                        else if (sflags & 0x10<<i) | 
| 505 | 
  | 
                                dt = scene->cuorg[i] + scene->cusize; | 
| 506 | 
  | 
                        else | 
| 507 | 
  | 
                                continue; | 
| 511 | 
  | 
                                t = dt; /* farthest face is the one */ | 
| 512 | 
  | 
                } | 
| 513 | 
  | 
                t += FTINY;             /* fudge to get inside cube */ | 
| 514 | 
+ | 
                if (t >= r->rot)        /* clipped already */ | 
| 515 | 
+ | 
                        return(0); | 
| 516 | 
  | 
                                        /* advance position */ | 
| 517 | 
  | 
                for (i = 0; i < 3; i++) | 
| 518 | 
  | 
                        curpos[i] += r->rdir[i]*t; | 
| 520 | 
  | 
                if (!incube(scene, curpos))     /* non-intersecting ray */ | 
| 521 | 
  | 
                        return(0); | 
| 522 | 
  | 
        } | 
| 523 | 
< | 
        return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT); | 
| 523 | 
> | 
        cxset[0] = 0; | 
| 524 | 
> | 
        raymove(curpos, cxset, sflags, r, scene); | 
| 525 | 
> | 
        return(r->ro != NULL & r->ro != &Aftplane); | 
| 526 | 
  | 
} | 
| 527 | 
  | 
 | 
| 528 | 
  | 
 | 
| 529 | 
  | 
static int | 
| 530 | 
< | 
raymove(pos, plus, minus, r, cu)        /* check for hit as we move */ | 
| 531 | 
< | 
FVECT  pos;                     /* modified */ | 
| 532 | 
< | 
int  plus, minus;               /* direction indicators to speed tests */ | 
| 530 | 
> | 
raymove(pos, cxs, dirf, r, cu)          /* check for hit as we move */ | 
| 531 | 
> | 
FVECT  pos;                     /* current position, modified herein */ | 
| 532 | 
> | 
OBJECT  *cxs;                   /* checked objects, modified by checkhit */ | 
| 533 | 
> | 
int  dirf;                      /* direction indicators to speed tests */ | 
| 534 | 
  | 
register RAY  *r; | 
| 535 | 
  | 
register CUBE  *cu; | 
| 536 | 
  | 
{ | 
| 537 | 
  | 
        int  ax; | 
| 538 | 
  | 
        double  dt, t; | 
| 303 | 
– | 
        register int  sgn; | 
| 539 | 
  | 
 | 
| 540 | 
  | 
        if (istree(cu->cutree)) {               /* recurse on subcubes */ | 
| 541 | 
  | 
                CUBE  cukid; | 
| 542 | 
< | 
                register int  br; | 
| 542 | 
> | 
                register int  br, sgn; | 
| 543 | 
  | 
 | 
| 544 | 
  | 
                cukid.cusize = cu->cusize * 0.5;        /* find subcube */ | 
| 545 | 
  | 
                VCOPY(cukid.cuorg, cu->cuorg); | 
| 558 | 
  | 
                } | 
| 559 | 
  | 
                for ( ; ; ) { | 
| 560 | 
  | 
                        cukid.cutree = octkid(cu->cutree, br); | 
| 561 | 
< | 
                        if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT) | 
| 561 | 
> | 
                        if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT) | 
| 562 | 
  | 
                                return(RAYHIT); | 
| 563 | 
  | 
                        sgn = 1 << ax; | 
| 564 | 
< | 
                        if (sgn & minus)                /* negative axis? */ | 
| 330 | 
< | 
                                if (sgn & br) { | 
| 331 | 
< | 
                                        cukid.cuorg[ax] -= cukid.cusize; | 
| 332 | 
< | 
                                        br &= ~sgn; | 
| 333 | 
< | 
                                } else | 
| 334 | 
< | 
                                        return(ax);     /* underflow */ | 
| 335 | 
< | 
                        else | 
| 564 | 
> | 
                        if (sgn & dirf)                 /* positive axis? */ | 
| 565 | 
  | 
                                if (sgn & br) | 
| 566 | 
  | 
                                        return(ax);     /* overflow */ | 
| 567 | 
  | 
                                else { | 
| 568 | 
  | 
                                        cukid.cuorg[ax] += cukid.cusize; | 
| 569 | 
  | 
                                        br |= sgn; | 
| 570 | 
  | 
                                } | 
| 571 | 
+ | 
                        else | 
| 572 | 
+ | 
                                if (sgn & br) { | 
| 573 | 
+ | 
                                        cukid.cuorg[ax] -= cukid.cusize; | 
| 574 | 
+ | 
                                        br &= ~sgn; | 
| 575 | 
+ | 
                                } else | 
| 576 | 
+ | 
                                        return(ax);     /* underflow */ | 
| 577 | 
  | 
                } | 
| 578 | 
  | 
                /*NOTREACHED*/ | 
| 579 | 
  | 
        } | 
| 580 | 
< | 
        if (isfull(cu->cutree) && checkhit(r, cu)) | 
| 580 | 
> | 
        if (isfull(cu->cutree)) { | 
| 581 | 
> | 
                if (checkhit(r, cu, cxs)) | 
| 582 | 
> | 
                        return(RAYHIT); | 
| 583 | 
> | 
        } else if (r->ro == &Aftplane && incube(cu, r->rop)) | 
| 584 | 
  | 
                return(RAYHIT); | 
| 585 | 
  | 
                                        /* advance to next cube */ | 
| 586 | 
< | 
        sgn = plus | minus; | 
| 587 | 
< | 
        if (sgn&1) { | 
| 350 | 
< | 
                dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; | 
| 586 | 
> | 
        if (dirf&0x11) { | 
| 587 | 
> | 
                dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; | 
| 588 | 
  | 
                t = (dt - pos[0])/r->rdir[0]; | 
| 589 | 
  | 
                ax = 0; | 
| 590 | 
  | 
        } else | 
| 591 | 
  | 
                t = FHUGE; | 
| 592 | 
< | 
        if (sgn&2) { | 
| 593 | 
< | 
                dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; | 
| 592 | 
> | 
        if (dirf&0x22) { | 
| 593 | 
> | 
                dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; | 
| 594 | 
  | 
                dt = (dt - pos[1])/r->rdir[1]; | 
| 595 | 
  | 
                if (dt < t) { | 
| 596 | 
  | 
                        t = dt; | 
| 597 | 
  | 
                        ax = 1; | 
| 598 | 
  | 
                } | 
| 599 | 
  | 
        } | 
| 600 | 
< | 
        if (sgn&4) { | 
| 601 | 
< | 
                dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; | 
| 600 | 
> | 
        if (dirf&0x44) { | 
| 601 | 
> | 
                dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; | 
| 602 | 
  | 
                dt = (dt - pos[2])/r->rdir[2]; | 
| 603 | 
  | 
                if (dt < t) { | 
| 604 | 
  | 
                        t = dt; | 
| 612 | 
  | 
} | 
| 613 | 
  | 
 | 
| 614 | 
  | 
 | 
| 615 | 
< | 
static | 
| 616 | 
< | 
checkhit(r, cu)                 /* check for hit in full cube */ | 
| 615 | 
> | 
static int | 
| 616 | 
> | 
checkhit(r, cu, cxs)            /* check for hit in full cube */ | 
| 617 | 
  | 
register RAY  *r; | 
| 618 | 
  | 
CUBE  *cu; | 
| 619 | 
+ | 
OBJECT  *cxs; | 
| 620 | 
  | 
{ | 
| 621 | 
  | 
        OBJECT  oset[MAXSET+1]; | 
| 622 | 
  | 
        register OBJREC  *o; | 
| 623 | 
  | 
        register int  i; | 
| 624 | 
  | 
 | 
| 625 | 
  | 
        objset(oset, cu->cutree); | 
| 626 | 
+ | 
        checkset(oset, cxs);                    /* eliminate double-checking */ | 
| 627 | 
  | 
        for (i = oset[0]; i > 0; i--) { | 
| 628 | 
  | 
                o = objptr(oset[i]); | 
| 629 | 
< | 
                if (o->lastrno == r->rno)               /* checked already? */ | 
| 630 | 
< | 
                        continue; | 
| 392 | 
< | 
                (*ofun[o->otype].funp)(o, r); | 
| 393 | 
< | 
                o->lastrno = r->rno; | 
| 629 | 
> | 
                if ((*ofun[o->otype].funp)(o, r)) | 
| 630 | 
> | 
                        r->robj = oset[i]; | 
| 631 | 
  | 
        } | 
| 632 | 
  | 
        if (r->ro == NULL) | 
| 633 | 
  | 
                return(0);                      /* no scores yet */ | 
| 634 | 
  | 
 | 
| 635 | 
  | 
        return(incube(cu, r->rop));             /* hit OK if in current cube */ | 
| 636 | 
+ | 
} | 
| 637 | 
+ | 
 | 
| 638 | 
+ | 
 | 
| 639 | 
+ | 
static void | 
| 640 | 
+ | 
checkset(os, cs)                /* modify checked set and set to check */ | 
| 641 | 
+ | 
register OBJECT  *os;                   /* os' = os - cs */ | 
| 642 | 
+ | 
register OBJECT  *cs;                   /* cs' = cs + os */ | 
| 643 | 
+ | 
{ | 
| 644 | 
+ | 
        OBJECT  cset[MAXCSET+MAXSET+1]; | 
| 645 | 
+ | 
        register int  i, j; | 
| 646 | 
+ | 
        int  k; | 
| 647 | 
+ | 
                                        /* copy os in place, cset <- cs */ | 
| 648 | 
+ | 
        cset[0] = 0; | 
| 649 | 
+ | 
        k = 0; | 
| 650 | 
+ | 
        for (i = j = 1; i <= os[0]; i++) { | 
| 651 | 
+ | 
                while (j <= cs[0] && cs[j] < os[i]) | 
| 652 | 
+ | 
                        cset[++cset[0]] = cs[j++]; | 
| 653 | 
+ | 
                if (j > cs[0] || os[i] != cs[j]) {      /* object to check */ | 
| 654 | 
+ | 
                        os[++k] = os[i]; | 
| 655 | 
+ | 
                        cset[++cset[0]] = os[i]; | 
| 656 | 
+ | 
                } | 
| 657 | 
+ | 
        } | 
| 658 | 
+ | 
        if (!(os[0] = k))               /* new "to check" set size */ | 
| 659 | 
+ | 
                return;                 /* special case */ | 
| 660 | 
+ | 
        while (j <= cs[0])              /* get the rest of cs */ | 
| 661 | 
+ | 
                cset[++cset[0]] = cs[j++]; | 
| 662 | 
+ | 
        if (cset[0] > MAXCSET)          /* truncate "checked" set if nec. */ | 
| 663 | 
+ | 
                cset[0] = MAXCSET; | 
| 664 | 
+ | 
        /* setcopy(cs, cset); */        /* copy cset back to cs */ | 
| 665 | 
+ | 
        os = cset; | 
| 666 | 
+ | 
        for (i = os[0]; i-- >= 0; ) | 
| 667 | 
+ | 
                *cs++ = *os++; | 
| 668 | 
  | 
} |