| 1 | 
greg | 
2.1 | 
/*  | 
| 2 | 
  | 
  | 
   ================================================================== | 
| 3 | 
  | 
  | 
   Photon map support routines for scattering by materials.  | 
| 4 | 
  | 
  | 
 | 
| 5 | 
  | 
  | 
   Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) | 
| 6 | 
  | 
  | 
   (c) Fraunhofer Institute for Solar Energy Systems, | 
| 7 | 
rschregle | 
2.3 | 
   (c) Lucerne University of Applied Sciences and Arts, | 
| 8 | 
  | 
  | 
   supported by the Swiss National Science Foundation (SNSF, #147053) | 
| 9 | 
greg | 
2.1 | 
   ================================================================== | 
| 10 | 
  | 
  | 
    | 
| 11 | 
rschregle | 
2.7 | 
   $Id: pmapmat.c,v 2.6 2015/05/22 10:27:40 greg Exp $  | 
| 12 | 
greg | 
2.1 | 
*/ | 
| 13 | 
  | 
  | 
 | 
| 14 | 
  | 
  | 
 | 
| 15 | 
  | 
  | 
 | 
| 16 | 
  | 
  | 
#include "pmapmat.h" | 
| 17 | 
  | 
  | 
#include "pmapdata.h" | 
| 18 | 
  | 
  | 
#include "pmaprand.h" | 
| 19 | 
  | 
  | 
#include "otypes.h" | 
| 20 | 
  | 
  | 
#include "data.h" | 
| 21 | 
  | 
  | 
#include "func.h" | 
| 22 | 
  | 
  | 
#include "bsdf.h" | 
| 23 | 
  | 
  | 
#include <math.h> | 
| 24 | 
  | 
  | 
 | 
| 25 | 
  | 
  | 
 | 
| 26 | 
  | 
  | 
 | 
| 27 | 
  | 
  | 
/* Stuff ripped off from material modules */ | 
| 28 | 
  | 
  | 
#define  MAXITER     10 | 
| 29 | 
  | 
  | 
#define  SP_REFL     01 | 
| 30 | 
  | 
  | 
#define  SP_TRAN     02  | 
| 31 | 
  | 
  | 
#define  SP_PURE     04 | 
| 32 | 
  | 
  | 
#define  SP_FLAT     010 | 
| 33 | 
  | 
  | 
#define  SP_BADU     040 | 
| 34 | 
  | 
  | 
#define  MLAMBDA     500 | 
| 35 | 
  | 
  | 
#define  RINDEX    1.52 | 
| 36 | 
  | 
  | 
#define  FRESNE(ci)  (exp(-5.85*(ci)) - 0.00287989916) | 
| 37 | 
  | 
  | 
 | 
| 38 | 
  | 
  | 
 | 
| 39 | 
  | 
  | 
 | 
| 40 | 
  | 
  | 
typedef struct { | 
| 41 | 
  | 
  | 
   OBJREC   *mp; | 
| 42 | 
  | 
  | 
   RAY      *rp; | 
| 43 | 
  | 
  | 
   short    specfl; | 
| 44 | 
  | 
  | 
   COLOR    mcolor, scolor; | 
| 45 | 
  | 
  | 
   FVECT    vrefl, prdir, pnorm; | 
| 46 | 
  | 
  | 
   double   alpha2, rdiff, rspec, trans, tdiff, tspec, pdot; | 
| 47 | 
  | 
  | 
}  NORMDAT; | 
| 48 | 
  | 
  | 
 | 
| 49 | 
  | 
  | 
typedef struct { | 
| 50 | 
  | 
  | 
   OBJREC   *mp; | 
| 51 | 
  | 
  | 
   RAY      *rp; | 
| 52 | 
  | 
  | 
   short    specfl; | 
| 53 | 
  | 
  | 
   COLOR    mcolor, scolor; | 
| 54 | 
  | 
  | 
   FVECT    vrefl, prdir, u, v, pnorm; | 
| 55 | 
  | 
  | 
   double   u_alpha, v_alpha, rdiff, rspec, trans, tdiff, tspec, pdot; | 
| 56 | 
  | 
  | 
}  ANISODAT; | 
| 57 | 
  | 
  | 
 | 
| 58 | 
  | 
  | 
typedef struct { | 
| 59 | 
  | 
  | 
   OBJREC   *mp; | 
| 60 | 
  | 
  | 
   RAY      *pr;                 | 
| 61 | 
  | 
  | 
   FVECT    pnorm;               | 
| 62 | 
  | 
  | 
   FVECT    vray;                | 
| 63 | 
  | 
  | 
   double   sr_vpsa [2];         | 
| 64 | 
  | 
  | 
   RREAL        toloc [3][3];    | 
| 65 | 
  | 
  | 
   RREAL        fromloc [3][3];  | 
| 66 | 
  | 
  | 
   double   thick;               | 
| 67 | 
  | 
  | 
   SDData   *sd; | 
| 68 | 
  | 
  | 
   COLOR           runsamp; | 
| 69 | 
  | 
  | 
   COLOR           rdiff; | 
| 70 | 
  | 
  | 
   COLOR           tunsamp; | 
| 71 | 
  | 
  | 
   COLOR           tdiff; | 
| 72 | 
  | 
  | 
} BSDFDAT; | 
| 73 | 
  | 
  | 
 | 
| 74 | 
  | 
  | 
 | 
| 75 | 
  | 
  | 
 | 
| 76 | 
  | 
  | 
extern const SDCDst SDemptyCD; | 
| 77 | 
  | 
  | 
 | 
| 78 | 
  | 
  | 
/* Per-material scattering function dispatch table; return value is usually | 
| 79 | 
  | 
  | 
 * zero, indicating photon termination */ | 
| 80 | 
  | 
  | 
int (*photonScatter [NUMOTYPE]) (OBJREC*, RAY*); | 
| 81 | 
  | 
  | 
 | 
| 82 | 
  | 
  | 
/* List of antimatter sensor modifier names and associated object set */ | 
| 83 | 
  | 
  | 
char *photonSensorList [MAXSET + 1] = {NULL}; | 
| 84 | 
  | 
  | 
static OBJECT photonSensorSet [MAXSET + 1] = {0}; | 
| 85 | 
  | 
  | 
 | 
| 86 | 
  | 
  | 
 | 
| 87 | 
  | 
  | 
 | 
| 88 | 
  | 
  | 
/* ================ General support routines ================ */ | 
| 89 | 
  | 
  | 
 | 
| 90 | 
  | 
  | 
 | 
| 91 | 
  | 
  | 
void photonRay (const RAY *rayIn, RAY *rayOut,  | 
| 92 | 
  | 
  | 
                int rayOutType, COLOR fluxAtten) | 
| 93 | 
  | 
  | 
/* Spawn a new photon ray from a previous one; this is effectively a | 
| 94 | 
  | 
  | 
 * customised rayorigin().  | 
| 95 | 
  | 
  | 
 * A SPECULAR rayOutType flags this photon as _caustic_ for subsequent hits. | 
| 96 | 
  | 
  | 
 * It is preserved for transferred rays (of type PMAP_XFER). | 
| 97 | 
  | 
  | 
 * fluxAtten specifies the RGB attenuation of the photon flux effected by | 
| 98 | 
  | 
  | 
 * the scattering material. The outgoing flux is then normalised to maintain | 
| 99 | 
  | 
  | 
 * a uniform average of 1 over RGB. If fluxAtten == NULL, the flux remains | 
| 100 | 
  | 
  | 
 * unchanged for the outgoing photon. fluxAtten is ignored for transferred | 
| 101 | 
  | 
  | 
 * rays. | 
| 102 | 
  | 
  | 
 * The ray direction is preserved for transferred rays, and undefined for | 
| 103 | 
  | 
  | 
 * scattered rays and must be subsequently set by the caller. */ | 
| 104 | 
  | 
  | 
{ | 
| 105 | 
  | 
  | 
   rayorigin(rayOut, rayOutType, rayIn, NULL); | 
| 106 | 
  | 
  | 
    | 
| 107 | 
  | 
  | 
   /* Transfer flux */ | 
| 108 | 
  | 
  | 
   copycolor(rayOut -> rcol, rayIn -> rcol); | 
| 109 | 
  | 
  | 
    | 
| 110 | 
  | 
  | 
   /* Copy caustic flag & direction for transferred rays */ | 
| 111 | 
  | 
  | 
   if (rayOutType == PMAP_XFER) { | 
| 112 | 
  | 
  | 
      /* rayOut -> rtype |= rayIn -> rtype & SPECULAR; */ | 
| 113 | 
  | 
  | 
      rayOut -> rtype |= rayIn -> rtype; | 
| 114 | 
  | 
  | 
      VCOPY(rayOut -> rdir, rayIn -> rdir); | 
| 115 | 
  | 
  | 
   } | 
| 116 | 
  | 
  | 
   else if (fluxAtten) { | 
| 117 | 
rschregle | 
2.2 | 
      /* Attenuate and normalise flux for scattered rays */ | 
| 118 | 
greg | 
2.1 | 
      multcolor(rayOut -> rcol, fluxAtten); | 
| 119 | 
  | 
  | 
      colorNorm(rayOut -> rcol); | 
| 120 | 
  | 
  | 
   } | 
| 121 | 
  | 
  | 
 | 
| 122 | 
  | 
  | 
   /* Propagate index of emitting light source */ | 
| 123 | 
  | 
  | 
   rayOut -> rsrc = rayIn -> rsrc; | 
| 124 | 
  | 
  | 
} | 
| 125 | 
  | 
  | 
 | 
| 126 | 
  | 
  | 
 | 
| 127 | 
  | 
  | 
 | 
| 128 | 
  | 
  | 
static void addPhotons (const RAY *r) | 
| 129 | 
  | 
  | 
/* Insert photon hits, where applicable */ | 
| 130 | 
  | 
  | 
{ | 
| 131 | 
  | 
  | 
   if (!r -> rlvl) | 
| 132 | 
  | 
  | 
      /* Add direct photon map at primary hitpoint */ | 
| 133 | 
  | 
  | 
      addPhoton(directPmap, r); | 
| 134 | 
  | 
  | 
   else { | 
| 135 | 
  | 
  | 
      /* Add global or precomputed photon map at indirect hitpoint */ | 
| 136 | 
  | 
  | 
      addPhoton(preCompPmap ? preCompPmap : globalPmap, r); | 
| 137 | 
  | 
  | 
 | 
| 138 | 
  | 
  | 
      /* Store caustic photon if specular flag set */ | 
| 139 | 
  | 
  | 
      if (PMAP_CAUSTICRAY(r)) | 
| 140 | 
  | 
  | 
         addPhoton(causticPmap, r); | 
| 141 | 
  | 
  | 
          | 
| 142 | 
  | 
  | 
      /* Store in contribution photon map */ | 
| 143 | 
  | 
  | 
      addPhoton(contribPmap, r); | 
| 144 | 
  | 
  | 
   } | 
| 145 | 
  | 
  | 
} | 
| 146 | 
  | 
  | 
 | 
| 147 | 
  | 
  | 
 | 
| 148 | 
  | 
  | 
 | 
| 149 | 
  | 
  | 
void getPhotonSensors (char **sensorList) | 
| 150 | 
  | 
  | 
/* Find antimatter geometry declared as photon sensors */ | 
| 151 | 
  | 
  | 
{ | 
| 152 | 
  | 
  | 
   OBJECT i; | 
| 153 | 
  | 
  | 
   OBJREC *obj; | 
| 154 | 
  | 
  | 
   char **lp; | 
| 155 | 
  | 
  | 
    | 
| 156 | 
  | 
  | 
   /* Init sensor set */ | 
| 157 | 
  | 
  | 
   photonSensorSet [0] = 0; | 
| 158 | 
  | 
  | 
    | 
| 159 | 
  | 
  | 
   if (!sensorList [0]) | 
| 160 | 
  | 
  | 
      return; | 
| 161 | 
  | 
  | 
       | 
| 162 | 
  | 
  | 
   for (i = 0; i < nobjects; i++) { | 
| 163 | 
  | 
  | 
      obj = objptr(i); | 
| 164 | 
  | 
  | 
       | 
| 165 | 
  | 
  | 
      /* Insert object in sensor set if it's in the specified sensor list | 
| 166 | 
  | 
  | 
       * and of type antimatter */ | 
| 167 | 
  | 
  | 
      for (lp = sensorList; *lp; lp++) { | 
| 168 | 
  | 
  | 
         if (!strcmp(obj -> oname, *lp)) { | 
| 169 | 
  | 
  | 
            if (obj -> otype != MAT_CLIP) { | 
| 170 | 
  | 
  | 
               sprintf(errmsg, "photon sensor modifier %s is not antimatter", | 
| 171 | 
  | 
  | 
                       obj -> oname); | 
| 172 | 
  | 
  | 
               error(USER, errmsg); | 
| 173 | 
  | 
  | 
            } | 
| 174 | 
  | 
  | 
             | 
| 175 | 
  | 
  | 
            if (photonSensorSet [0] >= AMBLLEN) | 
| 176 | 
  | 
  | 
               error(USER, "too many photon sensor modifiers"); | 
| 177 | 
  | 
  | 
             | 
| 178 | 
  | 
  | 
            insertelem(photonSensorSet, i); | 
| 179 | 
  | 
  | 
         } | 
| 180 | 
  | 
  | 
      } | 
| 181 | 
  | 
  | 
   } | 
| 182 | 
  | 
  | 
    | 
| 183 | 
  | 
  | 
   if (!photonSensorSet [0]) | 
| 184 | 
  | 
  | 
      error(USER, "no photon sensors found"); | 
| 185 | 
  | 
  | 
} | 
| 186 | 
  | 
  | 
 | 
| 187 | 
  | 
  | 
 | 
| 188 | 
  | 
  | 
 | 
| 189 | 
  | 
  | 
/* ================ Material specific scattering routines ================ */    | 
| 190 | 
  | 
  | 
 | 
| 191 | 
  | 
  | 
 | 
| 192 | 
  | 
  | 
static int isoSpecPhotonScatter (NORMDAT *nd, RAY *rayOut) | 
| 193 | 
  | 
  | 
/* Generate direction for isotropically specularly reflected  | 
| 194 | 
  | 
  | 
   or transmitted ray. Returns 1 if successful. */ | 
| 195 | 
  | 
  | 
{ | 
| 196 | 
  | 
  | 
   FVECT    u, v, h; | 
| 197 | 
  | 
  | 
   RAY      *rayIn = nd -> rp; | 
| 198 | 
  | 
  | 
   double   d, d2, sinp, cosp; | 
| 199 | 
  | 
  | 
   int      niter, i = 0; | 
| 200 | 
  | 
  | 
    | 
| 201 | 
  | 
  | 
   /* Set up sample coordinates */   | 
| 202 | 
greg | 
2.5 | 
   getperpendicular(u, nd -> pnorm, 1);    | 
| 203 | 
greg | 
2.1 | 
   fcross(v, nd -> pnorm, u); | 
| 204 | 
  | 
  | 
    | 
| 205 | 
  | 
  | 
   if (nd -> specfl & SP_REFL) { | 
| 206 | 
  | 
  | 
      /* Specular reflection; make MAXITER attempts at getting a ray */ | 
| 207 | 
  | 
  | 
       | 
| 208 | 
  | 
  | 
      for (niter = 0; niter < MAXITER; niter++) { | 
| 209 | 
  | 
  | 
         d = 2 * PI * pmapRandom(scatterState); | 
| 210 | 
  | 
  | 
         cosp = cos(d); | 
| 211 | 
  | 
  | 
         sinp = sin(d); | 
| 212 | 
  | 
  | 
         d2 = pmapRandom(scatterState); | 
| 213 | 
  | 
  | 
         d = d2 <= FTINY ? 1 : sqrt(nd -> alpha2 * -log(d2)); | 
| 214 | 
  | 
  | 
          | 
| 215 | 
  | 
  | 
         for (i = 0; i < 3; i++) | 
| 216 | 
  | 
  | 
            h [i] = nd -> pnorm [i] + d * (cosp * u [i] + sinp * v [i]); | 
| 217 | 
  | 
  | 
             | 
| 218 | 
  | 
  | 
         d = -2 * DOT(h, rayIn -> rdir) / (1 + d * d); | 
| 219 | 
  | 
  | 
         VSUM(rayOut -> rdir, rayIn -> rdir, h, d); | 
| 220 | 
  | 
  | 
          | 
| 221 | 
  | 
  | 
         if (DOT(rayOut -> rdir, rayIn -> ron) > FTINY)  | 
| 222 | 
  | 
  | 
            return 1; | 
| 223 | 
  | 
  | 
      } | 
| 224 | 
  | 
  | 
       | 
| 225 | 
  | 
  | 
      return 0; | 
| 226 | 
  | 
  | 
   } | 
| 227 | 
  | 
  | 
    | 
| 228 | 
  | 
  | 
   else { | 
| 229 | 
  | 
  | 
      /* Specular transmission; make MAXITER attempts at getting a ray */ | 
| 230 | 
  | 
  | 
       | 
| 231 | 
  | 
  | 
      for (niter = 0; niter < MAXITER; niter++) { | 
| 232 | 
  | 
  | 
         d = 2 * PI * pmapRandom(scatterState); | 
| 233 | 
  | 
  | 
         cosp = cos(d); | 
| 234 | 
  | 
  | 
         sinp = sin(d); | 
| 235 | 
  | 
  | 
         d2 = pmapRandom(scatterState); | 
| 236 | 
  | 
  | 
         d = d2 <= FTINY ? 1 : sqrt(-log(d2) * nd -> alpha2);          | 
| 237 | 
  | 
  | 
          | 
| 238 | 
  | 
  | 
         for (i = 0; i < 3; i++) | 
| 239 | 
  | 
  | 
            rayOut -> rdir [i] = nd -> prdir [i] +  | 
| 240 | 
  | 
  | 
                                 d * (cosp * u [i] + sinp * v [i]); | 
| 241 | 
  | 
  | 
                                  | 
| 242 | 
  | 
  | 
         if (DOT(rayOut -> rdir, rayIn -> ron) < -FTINY) { | 
| 243 | 
  | 
  | 
            normalize(rayOut -> rdir); | 
| 244 | 
  | 
  | 
            return 1; | 
| 245 | 
  | 
  | 
         } | 
| 246 | 
  | 
  | 
      } | 
| 247 | 
  | 
  | 
       | 
| 248 | 
  | 
  | 
      return 0; | 
| 249 | 
  | 
  | 
   } | 
| 250 | 
  | 
  | 
} | 
| 251 | 
  | 
  | 
 | 
| 252 | 
  | 
  | 
 | 
| 253 | 
  | 
  | 
 | 
| 254 | 
  | 
  | 
static void diffPhotonScatter (FVECT normal, RAY* rayOut) | 
| 255 | 
  | 
  | 
/* Generate cosine-weighted direction for diffuse ray */ | 
| 256 | 
  | 
  | 
{ | 
| 257 | 
  | 
  | 
   const RREAL cosThetaSqr = pmapRandom(scatterState),  | 
| 258 | 
  | 
  | 
               cosTheta = sqrt(cosThetaSqr), | 
| 259 | 
  | 
  | 
               sinTheta = sqrt(1 - cosThetaSqr),  | 
| 260 | 
  | 
  | 
               phi = 2 * PI * pmapRandom(scatterState),  | 
| 261 | 
  | 
  | 
               du = cos(phi) * sinTheta, dv = sin(phi) * sinTheta; | 
| 262 | 
  | 
  | 
   FVECT       u, v; | 
| 263 | 
  | 
  | 
   int         i = 0; | 
| 264 | 
  | 
  | 
 | 
| 265 | 
  | 
  | 
   /* Set up sample coordinates */ | 
| 266 | 
greg | 
2.5 | 
   getperpendicular(u, normal, 1); | 
| 267 | 
greg | 
2.1 | 
   fcross(v, normal, u); | 
| 268 | 
  | 
  | 
    | 
| 269 | 
  | 
  | 
   /* Convert theta & phi to cartesian */ | 
| 270 | 
  | 
  | 
   for (i = 0; i < 3; i++) | 
| 271 | 
  | 
  | 
      rayOut -> rdir [i] = du * u [i] + dv * v [i] + cosTheta * normal [i]; | 
| 272 | 
  | 
  | 
       | 
| 273 | 
  | 
  | 
   normalize(rayOut -> rdir); | 
| 274 | 
  | 
  | 
} | 
| 275 | 
  | 
  | 
 | 
| 276 | 
  | 
  | 
 | 
| 277 | 
  | 
  | 
 | 
| 278 | 
  | 
  | 
static int normalPhotonScatter (OBJREC *mat, RAY *rayIn) | 
| 279 | 
  | 
  | 
/* Generate new photon ray for isotropic material and recurse */ | 
| 280 | 
  | 
  | 
{ | 
| 281 | 
  | 
  | 
   NORMDAT  nd; | 
| 282 | 
  | 
  | 
   int      i, hastexture; | 
| 283 | 
  | 
  | 
   float    xi, albedo, prdiff, ptdiff, prspec, ptspec; | 
| 284 | 
  | 
  | 
   double   d, fresnel; | 
| 285 | 
  | 
  | 
   RAY      rayOut; | 
| 286 | 
  | 
  | 
 | 
| 287 | 
  | 
  | 
   if (mat -> oargs.nfargs != (mat -> otype == MAT_TRANS ? 7 : 5)) | 
| 288 | 
  | 
  | 
      objerror(mat, USER, "bad number of arguments"); | 
| 289 | 
  | 
  | 
       | 
| 290 | 
  | 
  | 
   /* Check for back side; reorient if back is visible */ | 
| 291 | 
  | 
  | 
   if (rayIn -> rod < 0) | 
| 292 | 
  | 
  | 
      if (!backvis && mat -> otype != MAT_TRANS)  | 
| 293 | 
  | 
  | 
         return 0; | 
| 294 | 
  | 
  | 
      else { | 
| 295 | 
  | 
  | 
         /* Get modifiers */ | 
| 296 | 
  | 
  | 
         raytexture(rayIn, mat -> omod); | 
| 297 | 
  | 
  | 
         flipsurface(rayIn); | 
| 298 | 
  | 
  | 
      } | 
| 299 | 
  | 
  | 
   else raytexture(rayIn, mat -> omod); | 
| 300 | 
  | 
  | 
    | 
| 301 | 
  | 
  | 
   nd.rp = rayIn; | 
| 302 | 
  | 
  | 
    | 
| 303 | 
  | 
  | 
   /* Get material color */ | 
| 304 | 
  | 
  | 
   copycolor(nd.mcolor, mat -> oargs.farg); | 
| 305 | 
  | 
  | 
    | 
| 306 | 
  | 
  | 
   /* Get roughness */ | 
| 307 | 
  | 
  | 
   nd.specfl = 0; | 
| 308 | 
  | 
  | 
   nd.alpha2 = mat -> oargs.farg [4]; | 
| 309 | 
  | 
  | 
    | 
| 310 | 
  | 
  | 
   if ((nd.alpha2 *= nd.alpha2) <= FTINY)  | 
| 311 | 
  | 
  | 
      nd.specfl |= SP_PURE; | 
| 312 | 
  | 
  | 
       | 
| 313 | 
  | 
  | 
   if (rayIn -> ro != NULL && isflat(rayIn -> ro -> otype))  | 
| 314 | 
  | 
  | 
      nd.specfl |= SP_FLAT;   | 
| 315 | 
  | 
  | 
       | 
| 316 | 
  | 
  | 
   /* Perturb normal */ | 
| 317 | 
greg | 
2.4 | 
   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)) )) | 
| 318 | 
greg | 
2.1 | 
      nd.pdot = raynormal(nd.pnorm, rayIn); | 
| 319 | 
  | 
  | 
   else { | 
| 320 | 
  | 
  | 
      VCOPY(nd.pnorm, rayIn -> ron); | 
| 321 | 
  | 
  | 
      nd.pdot = rayIn -> rod; | 
| 322 | 
  | 
  | 
   } | 
| 323 | 
  | 
  | 
    | 
| 324 | 
  | 
  | 
   nd.pdot = max(nd.pdot, .001); | 
| 325 | 
  | 
  | 
       | 
| 326 | 
  | 
  | 
   /* Modify material color */ | 
| 327 | 
  | 
  | 
   multcolor(nd.mcolor, rayIn -> pcol); | 
| 328 | 
  | 
  | 
   nd.rspec = mat -> oargs.farg [3]; | 
| 329 | 
  | 
  | 
    | 
| 330 | 
  | 
  | 
   /* Approximate Fresnel term */ | 
| 331 | 
  | 
  | 
   if (nd.specfl & SP_PURE && nd.rspec > FTINY) { | 
| 332 | 
  | 
  | 
      fresnel = FRESNE(rayIn -> rod); | 
| 333 | 
  | 
  | 
      nd.rspec += fresnel * (1 - nd.rspec); | 
| 334 | 
  | 
  | 
   }  | 
| 335 | 
  | 
  | 
   else fresnel = 0; | 
| 336 | 
  | 
  | 
    | 
| 337 | 
  | 
  | 
   /* Transmission params */ | 
| 338 | 
  | 
  | 
   if (mat -> otype == MAT_TRANS) { | 
| 339 | 
  | 
  | 
      nd.trans = mat -> oargs.farg [5] * (1 - nd.rspec); | 
| 340 | 
  | 
  | 
      nd.tspec = nd.trans * mat -> oargs.farg [6]; | 
| 341 | 
  | 
  | 
      nd.tdiff = nd.trans - nd.tspec;  | 
| 342 | 
  | 
  | 
   }  | 
| 343 | 
  | 
  | 
   else nd.tdiff = nd.tspec = nd.trans = 0; | 
| 344 | 
  | 
  | 
    | 
| 345 | 
  | 
  | 
   /* Specular reflection params */ | 
| 346 | 
  | 
  | 
   if (nd.rspec > FTINY) {       | 
| 347 | 
  | 
  | 
      /* Specular color */ | 
| 348 | 
  | 
  | 
      if (mat -> otype != MAT_METAL)  | 
| 349 | 
  | 
  | 
         setcolor(nd.scolor, nd.rspec, nd.rspec, nd.rspec); | 
| 350 | 
  | 
  | 
      else if (fresnel > FTINY) { | 
| 351 | 
  | 
  | 
         d = nd.rspec * (1 - fresnel); | 
| 352 | 
  | 
  | 
         for (i = 0; i < 3; i++)  | 
| 353 | 
  | 
  | 
            nd.scolor [i] = fresnel + nd.mcolor [i] * d; | 
| 354 | 
  | 
  | 
      } | 
| 355 | 
  | 
  | 
      else { | 
| 356 | 
  | 
  | 
         copycolor(nd.scolor, nd.mcolor); | 
| 357 | 
  | 
  | 
         scalecolor(nd.scolor, nd.rspec); | 
| 358 | 
  | 
  | 
      } | 
| 359 | 
  | 
  | 
   } | 
| 360 | 
  | 
  | 
   else setcolor(nd.scolor, 0, 0, 0); | 
| 361 | 
  | 
  | 
    | 
| 362 | 
  | 
  | 
   /* Diffuse reflection params */ | 
| 363 | 
  | 
  | 
   nd.rdiff = 1 - nd.trans - nd.rspec; | 
| 364 | 
  | 
  | 
    | 
| 365 | 
  | 
  | 
   /* Set up probabilities */ | 
| 366 | 
  | 
  | 
   prdiff = ptdiff = ptspec = colorAvg(nd.mcolor); | 
| 367 | 
  | 
  | 
   prdiff *= nd.rdiff; | 
| 368 | 
  | 
  | 
   ptdiff *= nd.tdiff; | 
| 369 | 
  | 
  | 
   prspec = colorAvg(nd.scolor); | 
| 370 | 
  | 
  | 
   ptspec *= nd.tspec; | 
| 371 | 
  | 
  | 
   albedo = prdiff + ptdiff + prspec + ptspec; | 
| 372 | 
  | 
  | 
    | 
| 373 | 
  | 
  | 
   /* Insert direct and indirect photon hits if diffuse component */ | 
| 374 | 
  | 
  | 
   if (prdiff > FTINY || ptdiff > FTINY) | 
| 375 | 
  | 
  | 
      addPhotons(rayIn); | 
| 376 | 
  | 
  | 
    | 
| 377 | 
  | 
  | 
   xi = pmapRandom(rouletteState); | 
| 378 | 
  | 
  | 
 | 
| 379 | 
  | 
  | 
   if (xi > albedo)  | 
| 380 | 
  | 
  | 
      /* Absorbed */ | 
| 381 | 
  | 
  | 
      return 0; | 
| 382 | 
  | 
  | 
       | 
| 383 | 
  | 
  | 
   if (xi > (albedo -= prspec)) { | 
| 384 | 
  | 
  | 
      /* Specular reflection */ | 
| 385 | 
  | 
  | 
      nd.specfl |= SP_REFL; | 
| 386 | 
  | 
  | 
       | 
| 387 | 
  | 
  | 
      if (nd.specfl & SP_PURE) { | 
| 388 | 
  | 
  | 
         /* Perfect specular reflection */ | 
| 389 | 
  | 
  | 
         for (i = 0; i < 3; i++) { | 
| 390 | 
  | 
  | 
            /* Reflected ray */ | 
| 391 | 
  | 
  | 
            nd.vrefl [i] = rayIn -> rdir [i] + 2 * nd.pdot * nd.pnorm [i]; | 
| 392 | 
  | 
  | 
         } | 
| 393 | 
  | 
  | 
          | 
| 394 | 
  | 
  | 
         /* Penetration? */ | 
| 395 | 
  | 
  | 
         if (hastexture && DOT(nd.vrefl, rayIn -> ron) <= FTINY) | 
| 396 | 
  | 
  | 
            for (i = 0; i < 3; i++) { | 
| 397 | 
  | 
  | 
               /* Safety measure */ | 
| 398 | 
  | 
  | 
               nd.vrefl [i] = rayIn -> rdir [i] +  | 
| 399 | 
  | 
  | 
                              2 * rayIn -> rod * rayIn -> ron [i]; | 
| 400 | 
  | 
  | 
            } | 
| 401 | 
  | 
  | 
             | 
| 402 | 
  | 
  | 
         VCOPY(rayOut.rdir, nd.vrefl); | 
| 403 | 
  | 
  | 
      } | 
| 404 | 
  | 
  | 
       | 
| 405 | 
  | 
  | 
      else if (!isoSpecPhotonScatter(&nd, &rayOut))  | 
| 406 | 
  | 
  | 
         return 0; | 
| 407 | 
  | 
  | 
       | 
| 408 | 
  | 
  | 
      photonRay(rayIn, &rayOut, PMAP_SPECREFL, nd.scolor); | 
| 409 | 
  | 
  | 
   } | 
| 410 | 
  | 
  | 
    | 
| 411 | 
  | 
  | 
   else if (xi > (albedo -= ptspec)) { | 
| 412 | 
  | 
  | 
      /* Specular transmission */ | 
| 413 | 
  | 
  | 
      nd.specfl |= SP_TRAN; | 
| 414 | 
  | 
  | 
       | 
| 415 | 
  | 
  | 
      if (hastexture) { | 
| 416 | 
  | 
  | 
         /* Perturb */ | 
| 417 | 
  | 
  | 
         for (i = 0; i < 3; i++)  | 
| 418 | 
  | 
  | 
            nd.prdir [i] = rayIn -> rdir [i] - rayIn -> pert [i]; | 
| 419 | 
  | 
  | 
             | 
| 420 | 
  | 
  | 
         if (DOT(nd.prdir, rayIn -> ron) < -FTINY)  | 
| 421 | 
  | 
  | 
            normalize(nd.prdir); | 
| 422 | 
  | 
  | 
         else VCOPY(nd.prdir, rayIn -> rdir); | 
| 423 | 
  | 
  | 
      } | 
| 424 | 
  | 
  | 
      else VCOPY(nd.prdir, rayIn -> rdir); | 
| 425 | 
  | 
  | 
       | 
| 426 | 
  | 
  | 
      if ((nd.specfl & (SP_TRAN | SP_PURE)) == (SP_TRAN | SP_PURE)) | 
| 427 | 
  | 
  | 
         /* Perfect specular transmission */    | 
| 428 | 
  | 
  | 
         VCOPY(rayOut.rdir, nd.prdir); | 
| 429 | 
  | 
  | 
      else if (!isoSpecPhotonScatter(&nd, &rayOut))  | 
| 430 | 
  | 
  | 
         return 0; | 
| 431 | 
  | 
  | 
          | 
| 432 | 
  | 
  | 
      photonRay(rayIn, &rayOut, PMAP_SPECTRANS, nd.mcolor);    | 
| 433 | 
  | 
  | 
   } | 
| 434 | 
  | 
  | 
    | 
| 435 | 
  | 
  | 
   else if (xi > (albedo -= prdiff)) { | 
| 436 | 
  | 
  | 
      /* Diffuse reflection */ | 
| 437 | 
  | 
  | 
      photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.mcolor); | 
| 438 | 
  | 
  | 
      diffPhotonScatter(hastexture ? nd.pnorm : rayIn -> ron, &rayOut); | 
| 439 | 
  | 
  | 
   } | 
| 440 | 
  | 
  | 
    | 
| 441 | 
  | 
  | 
   else { | 
| 442 | 
  | 
  | 
      /* Diffuse transmission */ | 
| 443 | 
  | 
  | 
      flipsurface(rayIn); | 
| 444 | 
  | 
  | 
      photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.mcolor); | 
| 445 | 
  | 
  | 
 | 
| 446 | 
  | 
  | 
      if (hastexture) { | 
| 447 | 
  | 
  | 
         FVECT bnorm; | 
| 448 | 
  | 
  | 
         bnorm [0] = -nd.pnorm [0]; | 
| 449 | 
  | 
  | 
         bnorm [1] = -nd.pnorm [1]; | 
| 450 | 
  | 
  | 
         bnorm [2] = -nd.pnorm [2]; | 
| 451 | 
  | 
  | 
         diffPhotonScatter(bnorm, &rayOut); | 
| 452 | 
  | 
  | 
      }  | 
| 453 | 
  | 
  | 
      else diffPhotonScatter(rayIn -> ron, &rayOut); | 
| 454 | 
  | 
  | 
   } | 
| 455 | 
  | 
  | 
    | 
| 456 | 
  | 
  | 
   tracePhoton(&rayOut); | 
| 457 | 
  | 
  | 
   return 0; | 
| 458 | 
  | 
  | 
} | 
| 459 | 
  | 
  | 
 | 
| 460 | 
  | 
  | 
 | 
| 461 | 
  | 
  | 
 | 
| 462 | 
  | 
  | 
static void getacoords (ANISODAT *np) | 
| 463 | 
  | 
  | 
/* Set up coordinate system for anisotropic sampling; cloned from aniso.c */ | 
| 464 | 
  | 
  | 
{ | 
| 465 | 
  | 
  | 
        MFUNC  *mf; | 
| 466 | 
  | 
  | 
        int  i; | 
| 467 | 
  | 
  | 
 | 
| 468 | 
  | 
  | 
        mf = getfunc(np->mp, 3, 0x7, 1); | 
| 469 | 
  | 
  | 
        setfunc(np->mp, np->rp); | 
| 470 | 
  | 
  | 
        errno = 0; | 
| 471 | 
  | 
  | 
         | 
| 472 | 
  | 
  | 
        for (i = 0; i < 3; i++) | 
| 473 | 
  | 
  | 
           np->u[i] = evalue(mf->ep[i]); | 
| 474 | 
  | 
  | 
            | 
| 475 | 
  | 
  | 
   if ((errno == EDOM) | (errno == ERANGE)) { | 
| 476 | 
  | 
  | 
      objerror(np->mp, WARNING, "compute error"); | 
| 477 | 
  | 
  | 
      np->specfl |= SP_BADU; | 
| 478 | 
  | 
  | 
      return; | 
| 479 | 
  | 
  | 
   } | 
| 480 | 
  | 
  | 
    | 
| 481 | 
  | 
  | 
   if (mf->fxp != &unitxf) | 
| 482 | 
  | 
  | 
      multv3(np->u, np->u, mf->fxp->xfm); | 
| 483 | 
  | 
  | 
       | 
| 484 | 
  | 
  | 
        fcross(np->v, np->pnorm, np->u); | 
| 485 | 
  | 
  | 
         | 
| 486 | 
  | 
  | 
        if (normalize(np->v) == 0.0) { | 
| 487 | 
  | 
  | 
           objerror(np->mp, WARNING, "illegal orientation vector"); | 
| 488 | 
  | 
  | 
           np->specfl |= SP_BADU; | 
| 489 | 
  | 
  | 
           return; | 
| 490 | 
  | 
  | 
   } | 
| 491 | 
  | 
  | 
    | 
| 492 | 
  | 
  | 
   fcross(np->u, np->v, np->pnorm); | 
| 493 | 
  | 
  | 
} | 
| 494 | 
  | 
  | 
 | 
| 495 | 
  | 
  | 
 | 
| 496 | 
  | 
  | 
 | 
| 497 | 
  | 
  | 
static int anisoSpecPhotonScatter (ANISODAT *nd, RAY *rayOut) | 
| 498 | 
  | 
  | 
/* Generate direction for anisotropically specularly reflected  | 
| 499 | 
  | 
  | 
   or transmitted ray. Returns 1 if successful. */ | 
| 500 | 
  | 
  | 
{ | 
| 501 | 
  | 
  | 
   FVECT    h; | 
| 502 | 
  | 
  | 
   double   d, d2, sinp, cosp; | 
| 503 | 
  | 
  | 
   int      niter, i; | 
| 504 | 
  | 
  | 
   RAY      *rayIn = nd -> rp; | 
| 505 | 
  | 
  | 
 | 
| 506 | 
  | 
  | 
   if (rayIn -> ro != NULL && isflat(rayIn -> ro -> otype))  | 
| 507 | 
  | 
  | 
      nd -> specfl |= SP_FLAT; | 
| 508 | 
  | 
  | 
       | 
| 509 | 
  | 
  | 
   /* set up coordinates */ | 
| 510 | 
  | 
  | 
   getacoords(nd); | 
| 511 | 
  | 
  | 
    | 
| 512 | 
  | 
  | 
   if (rayOut -> rtype & TRANS) { | 
| 513 | 
  | 
  | 
      /* Specular transmission */ | 
| 514 | 
  | 
  | 
 | 
| 515 | 
  | 
  | 
      if (DOT(rayIn -> pert, rayIn -> pert) <= FTINY * FTINY)  | 
| 516 | 
  | 
  | 
         VCOPY(nd -> prdir, rayIn -> rdir); | 
| 517 | 
  | 
  | 
      else { | 
| 518 | 
  | 
  | 
         /* perturb */ | 
| 519 | 
  | 
  | 
         for (i = 0; i < 3; i++)  | 
| 520 | 
  | 
  | 
            nd -> prdir [i] = rayIn -> rdir [i] - rayIn -> pert [i]; | 
| 521 | 
  | 
  | 
             | 
| 522 | 
  | 
  | 
         if (DOT(nd -> prdir, rayIn -> ron) < -FTINY)  | 
| 523 | 
  | 
  | 
            normalize(nd -> prdir); | 
| 524 | 
  | 
  | 
         else VCOPY(nd -> prdir, rayIn -> rdir); | 
| 525 | 
  | 
  | 
      } | 
| 526 | 
  | 
  | 
       | 
| 527 | 
  | 
  | 
      /* Make MAXITER attempts at getting a ray */ | 
| 528 | 
  | 
  | 
      for (niter = 0; niter < MAXITER; niter++) { | 
| 529 | 
  | 
  | 
         d = 2 * PI * pmapRandom(scatterState); | 
| 530 | 
  | 
  | 
         cosp = cos(d) * nd -> u_alpha; | 
| 531 | 
  | 
  | 
         sinp = sin(d) * nd -> v_alpha; | 
| 532 | 
  | 
  | 
         d = sqrt(sqr(cosp) + sqr(sinp)); | 
| 533 | 
  | 
  | 
         cosp /= d; | 
| 534 | 
  | 
  | 
         sinp /= d; | 
| 535 | 
  | 
  | 
         d2 = pmapRandom(scatterState); | 
| 536 | 
  | 
  | 
         d = d2 <= FTINY ? 1 | 
| 537 | 
  | 
  | 
                         : sqrt(-log(d2) / | 
| 538 | 
  | 
  | 
                                (sqr(cosp) / sqr(nd -> u_alpha) +  | 
| 539 | 
  | 
  | 
                                 sqr(sinp) / (nd -> v_alpha * nd -> u_alpha))); | 
| 540 | 
  | 
  | 
                                  | 
| 541 | 
  | 
  | 
         for (i = 0; i < 3; i++) | 
| 542 | 
  | 
  | 
            rayOut -> rdir [i] = nd -> prdir [i] + d *  | 
| 543 | 
  | 
  | 
                                 (cosp * nd -> u [i] + sinp * nd -> v [i]); | 
| 544 | 
  | 
  | 
                                  | 
| 545 | 
  | 
  | 
         if (DOT(rayOut -> rdir, rayIn -> ron) < -FTINY) { | 
| 546 | 
  | 
  | 
            normalize(rayOut -> rdir); | 
| 547 | 
  | 
  | 
            return 1; | 
| 548 | 
  | 
  | 
         } | 
| 549 | 
  | 
  | 
      } | 
| 550 | 
  | 
  | 
       | 
| 551 | 
  | 
  | 
      return 0;    | 
| 552 | 
  | 
  | 
   } | 
| 553 | 
  | 
  | 
    | 
| 554 | 
  | 
  | 
   else { | 
| 555 | 
  | 
  | 
      /* Specular reflection */ | 
| 556 | 
  | 
  | 
       | 
| 557 | 
  | 
  | 
      /* Make MAXITER attempts at getting a ray */ | 
| 558 | 
  | 
  | 
      for (niter = 0; niter < MAXITER; niter++) { | 
| 559 | 
  | 
  | 
         d = 2 * PI * pmapRandom(scatterState); | 
| 560 | 
  | 
  | 
         cosp = cos(d) * nd -> u_alpha; | 
| 561 | 
  | 
  | 
         sinp = sin(d) * nd -> v_alpha; | 
| 562 | 
  | 
  | 
         d = sqrt(sqr(cosp) + sqr(sinp)); | 
| 563 | 
  | 
  | 
         cosp /= d; | 
| 564 | 
  | 
  | 
         sinp /= d; | 
| 565 | 
  | 
  | 
         d2 = pmapRandom(scatterState); | 
| 566 | 
  | 
  | 
         d = d2 <= FTINY ? 1  | 
| 567 | 
  | 
  | 
                         : sqrt(-log(d2) /  | 
| 568 | 
  | 
  | 
                                (sqr(cosp) / sqr(nd -> u_alpha) + | 
| 569 | 
  | 
  | 
                                 sqr(sinp) / (nd -> v_alpha * nd -> v_alpha))); | 
| 570 | 
  | 
  | 
                                  | 
| 571 | 
  | 
  | 
         for (i = 0; i < 3; i++) | 
| 572 | 
  | 
  | 
            h [i] = nd -> pnorm [i] +  | 
| 573 | 
  | 
  | 
                    d * (cosp * nd -> u [i] + sinp * nd -> v [i]); | 
| 574 | 
  | 
  | 
                     | 
| 575 | 
  | 
  | 
         d = -2 * DOT(h, rayIn -> rdir) / (1 + d * d); | 
| 576 | 
  | 
  | 
         VSUM(rayOut -> rdir, rayIn -> rdir, h, d); | 
| 577 | 
  | 
  | 
          | 
| 578 | 
  | 
  | 
         if (DOT(rayOut -> rdir, rayIn -> ron) > FTINY)  | 
| 579 | 
  | 
  | 
            return 1; | 
| 580 | 
  | 
  | 
      } | 
| 581 | 
  | 
  | 
       | 
| 582 | 
  | 
  | 
      return 0; | 
| 583 | 
  | 
  | 
   } | 
| 584 | 
  | 
  | 
} | 
| 585 | 
  | 
  | 
 | 
| 586 | 
  | 
  | 
 | 
| 587 | 
  | 
  | 
 | 
| 588 | 
  | 
  | 
static int anisoPhotonScatter (OBJREC *mat, RAY *rayIn) | 
| 589 | 
  | 
  | 
/* Generate new photon ray for anisotropic material and recurse */ | 
| 590 | 
  | 
  | 
{ | 
| 591 | 
  | 
  | 
   ANISODAT nd; | 
| 592 | 
  | 
  | 
   float    xi, albedo, prdiff, ptdiff, prspec, ptspec; | 
| 593 | 
  | 
  | 
   RAY      rayOut; | 
| 594 | 
  | 
  | 
    | 
| 595 | 
  | 
  | 
   if (mat -> oargs.nfargs != (mat -> otype == MAT_TRANS2 ? 8 : 6)) | 
| 596 | 
  | 
  | 
      objerror(mat, USER, "bad number of real arguments"); | 
| 597 | 
  | 
  | 
       | 
| 598 | 
  | 
  | 
   nd.rp = rayIn; | 
| 599 | 
  | 
  | 
   nd.mp = objptr(rayIn -> ro -> omod); | 
| 600 | 
  | 
  | 
    | 
| 601 | 
  | 
  | 
   /* get material color */ | 
| 602 | 
  | 
  | 
   copycolor(nd.mcolor, mat -> oargs.farg); | 
| 603 | 
  | 
  | 
    | 
| 604 | 
  | 
  | 
   /* get roughness */ | 
| 605 | 
  | 
  | 
   nd.specfl = 0; | 
| 606 | 
  | 
  | 
   nd.u_alpha = mat -> oargs.farg [4]; | 
| 607 | 
  | 
  | 
   nd.v_alpha = mat -> oargs.farg [5]; | 
| 608 | 
  | 
  | 
   if (nd.u_alpha < FTINY || nd.v_alpha <= FTINY) | 
| 609 | 
  | 
  | 
      objerror(mat, USER, "roughness too small"); | 
| 610 | 
  | 
  | 
       | 
| 611 | 
  | 
  | 
   /* check for back side; reorient if back is visible */ | 
| 612 | 
  | 
  | 
   if (rayIn -> rod < 0) | 
| 613 | 
  | 
  | 
      if (!backvis && mat -> otype != MAT_TRANS2)  | 
| 614 | 
  | 
  | 
         return 0; | 
| 615 | 
  | 
  | 
      else { | 
| 616 | 
  | 
  | 
         /* get modifiers */ | 
| 617 | 
  | 
  | 
         raytexture(rayIn, mat -> omod); | 
| 618 | 
  | 
  | 
         flipsurface(rayIn); | 
| 619 | 
  | 
  | 
      } | 
| 620 | 
  | 
  | 
   else raytexture(rayIn, mat -> omod); | 
| 621 | 
  | 
  | 
    | 
| 622 | 
  | 
  | 
   /* perturb normal */ | 
| 623 | 
  | 
  | 
   nd.pdot = max(raynormal(nd.pnorm, rayIn), .001); | 
| 624 | 
  | 
  | 
    | 
| 625 | 
  | 
  | 
   /* modify material color */ | 
| 626 | 
  | 
  | 
   multcolor(nd.mcolor, rayIn -> pcol); | 
| 627 | 
  | 
  | 
   nd.rspec = mat -> oargs.farg [3]; | 
| 628 | 
  | 
  | 
    | 
| 629 | 
  | 
  | 
   /* transmission params */ | 
| 630 | 
  | 
  | 
   if (mat -> otype == MAT_TRANS2) { | 
| 631 | 
  | 
  | 
      nd.trans = mat -> oargs.farg [6] * (1 - nd.rspec); | 
| 632 | 
  | 
  | 
      nd.tspec = nd.trans * mat -> oargs.farg [7]; | 
| 633 | 
  | 
  | 
      nd.tdiff = nd.trans - nd.tspec; | 
| 634 | 
  | 
  | 
      if (nd.tspec > FTINY)  | 
| 635 | 
  | 
  | 
         nd.specfl |= SP_TRAN; | 
| 636 | 
  | 
  | 
   }  | 
| 637 | 
  | 
  | 
   else nd.tdiff = nd.tspec = nd.trans = 0; | 
| 638 | 
  | 
  | 
    | 
| 639 | 
  | 
  | 
   /* specular reflection params */ | 
| 640 | 
  | 
  | 
   if (nd.rspec > FTINY) { | 
| 641 | 
  | 
  | 
      nd.specfl |= SP_REFL; | 
| 642 | 
  | 
  | 
       | 
| 643 | 
  | 
  | 
      /* comput   e specular color */ | 
| 644 | 
  | 
  | 
      if (mat -> otype == MAT_METAL2)  | 
| 645 | 
  | 
  | 
         copycolor(nd.scolor, nd.mcolor); | 
| 646 | 
  | 
  | 
      else setcolor(nd.scolor, 1, 1, 1); | 
| 647 | 
  | 
  | 
       | 
| 648 | 
  | 
  | 
      scalecolor(nd.scolor, nd.rspec); | 
| 649 | 
  | 
  | 
   } | 
| 650 | 
  | 
  | 
   else setcolor(nd.scolor, 0, 0, 0); | 
| 651 | 
  | 
  | 
    | 
| 652 | 
  | 
  | 
   /* diffuse reflection params */ | 
| 653 | 
  | 
  | 
   nd.rdiff = 1 - nd.trans - nd.rspec; | 
| 654 | 
  | 
  | 
    | 
| 655 | 
  | 
  | 
   /* Set up probabilities */ | 
| 656 | 
  | 
  | 
   prdiff = ptdiff = ptspec = colorAvg(nd.mcolor); | 
| 657 | 
  | 
  | 
   prdiff *= nd.rdiff; | 
| 658 | 
  | 
  | 
   ptdiff *= nd.tdiff; | 
| 659 | 
  | 
  | 
   prspec = colorAvg(nd.scolor); | 
| 660 | 
  | 
  | 
   ptspec *= nd.tspec; | 
| 661 | 
  | 
  | 
   albedo = prdiff + ptdiff + prspec + ptspec; | 
| 662 | 
  | 
  | 
    | 
| 663 | 
  | 
  | 
   /* Insert direct and indirect photon hits if diffuse component */ | 
| 664 | 
  | 
  | 
   if (prdiff > FTINY || ptdiff > FTINY) | 
| 665 | 
  | 
  | 
      addPhotons(rayIn); | 
| 666 | 
  | 
  | 
    | 
| 667 | 
  | 
  | 
   xi = pmapRandom(rouletteState); | 
| 668 | 
  | 
  | 
    | 
| 669 | 
  | 
  | 
   if (xi > albedo)  | 
| 670 | 
  | 
  | 
      /* Absorbed */ | 
| 671 | 
  | 
  | 
      return 0; | 
| 672 | 
  | 
  | 
       | 
| 673 | 
  | 
  | 
   if (xi > (albedo -= prspec)) | 
| 674 | 
  | 
  | 
      /* Specular reflection */ | 
| 675 | 
  | 
  | 
      if (!(nd.specfl & SP_BADU)) { | 
| 676 | 
  | 
  | 
         photonRay(rayIn, &rayOut, PMAP_SPECREFL, nd.scolor); | 
| 677 | 
  | 
  | 
 | 
| 678 | 
  | 
  | 
         if (!anisoSpecPhotonScatter(&nd, &rayOut))  | 
| 679 | 
  | 
  | 
            return 0; | 
| 680 | 
  | 
  | 
      } | 
| 681 | 
  | 
  | 
      else return 0; | 
| 682 | 
  | 
  | 
       | 
| 683 | 
  | 
  | 
   else if (xi > (albedo -= ptspec)) | 
| 684 | 
  | 
  | 
      /* Specular transmission */ | 
| 685 | 
  | 
  | 
       | 
| 686 | 
  | 
  | 
      if (!(nd.specfl & SP_BADU)) { | 
| 687 | 
  | 
  | 
         /* Specular transmission */ | 
| 688 | 
  | 
  | 
         photonRay(rayIn, &rayOut, PMAP_SPECTRANS, nd.mcolor); | 
| 689 | 
  | 
  | 
 | 
| 690 | 
  | 
  | 
         if (!anisoSpecPhotonScatter(&nd, &rayOut))  | 
| 691 | 
  | 
  | 
            return 0; | 
| 692 | 
  | 
  | 
      } | 
| 693 | 
  | 
  | 
      else return 0; | 
| 694 | 
  | 
  | 
       | 
| 695 | 
  | 
  | 
   else if (xi > (albedo -= prdiff)) { | 
| 696 | 
  | 
  | 
      /* Diffuse reflection */    | 
| 697 | 
  | 
  | 
      photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.mcolor); | 
| 698 | 
  | 
  | 
      diffPhotonScatter(nd.pnorm, &rayOut); | 
| 699 | 
  | 
  | 
   } | 
| 700 | 
  | 
  | 
    | 
| 701 | 
  | 
  | 
   else { | 
| 702 | 
  | 
  | 
      /* Diffuse transmission */ | 
| 703 | 
  | 
  | 
      FVECT bnorm; | 
| 704 | 
  | 
  | 
      flipsurface(rayIn); | 
| 705 | 
  | 
  | 
      bnorm [0] = -nd.pnorm [0]; | 
| 706 | 
  | 
  | 
      bnorm [1] = -nd.pnorm [1]; | 
| 707 | 
  | 
  | 
      bnorm [2] = -nd.pnorm [2]; | 
| 708 | 
  | 
  | 
       | 
| 709 | 
  | 
  | 
      photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.mcolor); | 
| 710 | 
  | 
  | 
      diffPhotonScatter(bnorm, &rayOut); | 
| 711 | 
  | 
  | 
   } | 
| 712 | 
  | 
  | 
    | 
| 713 | 
  | 
  | 
   tracePhoton(&rayOut); | 
| 714 | 
  | 
  | 
   return 0; | 
| 715 | 
  | 
  | 
} | 
| 716 | 
  | 
  | 
 | 
| 717 | 
  | 
  | 
 | 
| 718 | 
  | 
  | 
static double mylog (double x) | 
| 719 | 
  | 
  | 
/* special log for extinction coefficients; cloned from dielectric.c */ | 
| 720 | 
  | 
  | 
{ | 
| 721 | 
  | 
  | 
   if (x < 1e-40) | 
| 722 | 
  | 
  | 
      return(-100.); | 
| 723 | 
  | 
  | 
       | 
| 724 | 
  | 
  | 
   if (x >= 1.) | 
| 725 | 
  | 
  | 
      return(0.); | 
| 726 | 
  | 
  | 
       | 
| 727 | 
  | 
  | 
   return(log(x)); | 
| 728 | 
  | 
  | 
} | 
| 729 | 
  | 
  | 
 | 
| 730 | 
  | 
  | 
 | 
| 731 | 
  | 
  | 
static int dielectricPhotonScatter (OBJREC *mat, RAY *rayIn) | 
| 732 | 
  | 
  | 
/* Generate new photon ray for dielectric material and recurse */ | 
| 733 | 
  | 
  | 
{ | 
| 734 | 
  | 
  | 
   double   cos1, cos2, nratio, d1, d2, refl; | 
| 735 | 
  | 
  | 
   COLOR    ctrans, talb; | 
| 736 | 
  | 
  | 
   FVECT    dnorm; | 
| 737 | 
  | 
  | 
   int      hastexture, i; | 
| 738 | 
  | 
  | 
   RAY      rayOut; | 
| 739 | 
  | 
  | 
 | 
| 740 | 
  | 
  | 
   if (mat -> oargs.nfargs != (mat -> otype == MAT_DIELECTRIC ? 5 : 8)) | 
| 741 | 
  | 
  | 
      objerror(mat, USER, "bad arguments"); | 
| 742 | 
  | 
  | 
       | 
| 743 | 
  | 
  | 
   /* get modifiers */ | 
| 744 | 
  | 
  | 
   raytexture(rayIn, mat -> omod);                       | 
| 745 | 
  | 
  | 
    | 
| 746 | 
greg | 
2.4 | 
   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY))) | 
| 747 | 
greg | 
2.1 | 
      /* Perturb normal */ | 
| 748 | 
  | 
  | 
      cos1 = raynormal(dnorm, rayIn); | 
| 749 | 
  | 
  | 
   else { | 
| 750 | 
  | 
  | 
      VCOPY(dnorm, rayIn -> ron); | 
| 751 | 
  | 
  | 
      cos1 = rayIn -> rod; | 
| 752 | 
  | 
  | 
   } | 
| 753 | 
  | 
  | 
    | 
| 754 | 
  | 
  | 
   /* index of refraction */ | 
| 755 | 
  | 
  | 
   nratio = mat -> otype ==  | 
| 756 | 
  | 
  | 
      MAT_DIELECTRIC ? mat -> oargs.farg [3] + mat -> oargs.farg [4] / MLAMBDA | 
| 757 | 
  | 
  | 
                     : mat -> oargs.farg [3] / mat -> oargs.farg [7]; | 
| 758 | 
  | 
  | 
                      | 
| 759 | 
  | 
  | 
   if (cos1 < 0) { | 
| 760 | 
  | 
  | 
      /* inside */ | 
| 761 | 
  | 
  | 
      hastexture = -hastexture; | 
| 762 | 
  | 
  | 
      cos1 = -cos1; | 
| 763 | 
  | 
  | 
      dnorm [0] = -dnorm [0]; | 
| 764 | 
  | 
  | 
      dnorm [1] = -dnorm [1]; | 
| 765 | 
  | 
  | 
      dnorm [2] = -dnorm [2];             | 
| 766 | 
  | 
  | 
      setcolor(rayIn -> cext,  | 
| 767 | 
  | 
  | 
               -mylog(mat -> oargs.farg [0] * rayIn -> pcol [0]), | 
| 768 | 
  | 
  | 
               -mylog(mat -> oargs.farg [1] * rayIn -> pcol [1]), | 
| 769 | 
  | 
  | 
               -mylog(mat -> oargs.farg [2] * rayIn -> pcol [2])); | 
| 770 | 
  | 
  | 
      setcolor(rayIn -> albedo, 0, 0, 0); | 
| 771 | 
  | 
  | 
      rayIn -> gecc = 0; | 
| 772 | 
  | 
  | 
       | 
| 773 | 
  | 
  | 
      if (mat -> otype == MAT_INTERFACE) { | 
| 774 | 
  | 
  | 
         setcolor(ctrans,  | 
| 775 | 
  | 
  | 
                  -mylog(mat -> oargs.farg [4] * rayIn -> pcol [0]), | 
| 776 | 
  | 
  | 
                  -mylog(mat -> oargs.farg [5] * rayIn -> pcol [1]), | 
| 777 | 
  | 
  | 
                  -mylog(mat -> oargs.farg [6] * rayIn -> pcol [2])); | 
| 778 | 
  | 
  | 
         setcolor(talb, 0, 0, 0); | 
| 779 | 
  | 
  | 
      }  | 
| 780 | 
  | 
  | 
      else { | 
| 781 | 
  | 
  | 
         copycolor(ctrans, cextinction); | 
| 782 | 
  | 
  | 
         copycolor(talb, salbedo); | 
| 783 | 
  | 
  | 
      } | 
| 784 | 
  | 
  | 
   }  | 
| 785 | 
  | 
  | 
    | 
| 786 | 
  | 
  | 
   else { | 
| 787 | 
  | 
  | 
      /* outside */ | 
| 788 | 
  | 
  | 
      nratio = 1.0 / nratio; | 
| 789 | 
  | 
  | 
      setcolor(ctrans,  | 
| 790 | 
  | 
  | 
               -mylog(mat -> oargs.farg [0] * rayIn -> pcol [0]), | 
| 791 | 
  | 
  | 
               -mylog(mat -> oargs.farg [1] * rayIn -> pcol [1]), | 
| 792 | 
  | 
  | 
               -mylog(mat -> oargs.farg [2] * rayIn -> pcol [2])); | 
| 793 | 
  | 
  | 
      setcolor(talb, 0, 0, 0); | 
| 794 | 
  | 
  | 
       | 
| 795 | 
  | 
  | 
      if (mat -> otype == MAT_INTERFACE) { | 
| 796 | 
  | 
  | 
         setcolor(rayIn -> cext, | 
| 797 | 
  | 
  | 
                  -mylog(mat -> oargs.farg [4] * rayIn -> pcol [0]), | 
| 798 | 
  | 
  | 
                  -mylog(mat -> oargs.farg [5] * rayIn -> pcol [1]), | 
| 799 | 
  | 
  | 
                  -mylog(mat -> oargs.farg [6] * rayIn -> pcol [2])); | 
| 800 | 
  | 
  | 
         setcolor(rayIn -> albedo, 0, 0, 0); | 
| 801 | 
  | 
  | 
         rayIn -> gecc = 0; | 
| 802 | 
  | 
  | 
      } | 
| 803 | 
  | 
  | 
   }  | 
| 804 | 
  | 
  | 
               | 
| 805 | 
  | 
  | 
   /* compute cos theta2 */ | 
| 806 | 
  | 
  | 
   d2 = 1 - sqr(nratio) * (1 - sqr(cos1)); | 
| 807 | 
  | 
  | 
    | 
| 808 | 
  | 
  | 
   if (d2 < FTINY) { | 
| 809 | 
  | 
  | 
      /* Total reflection */     | 
| 810 | 
  | 
  | 
      refl = cos2 = 1.0; | 
| 811 | 
  | 
  | 
   } | 
| 812 | 
  | 
  | 
   else { | 
| 813 | 
  | 
  | 
      /* Refraction, compute Fresnel's equations */ | 
| 814 | 
  | 
  | 
      cos2 = sqrt(d2); | 
| 815 | 
  | 
  | 
      d1 = cos1; | 
| 816 | 
  | 
  | 
      d2 = nratio * cos2; | 
| 817 | 
  | 
  | 
      d1 = (d1 - d2) / (d1 + d2); | 
| 818 | 
  | 
  | 
      refl = sqr(d1); | 
| 819 | 
  | 
  | 
      d1 = 1 / cos1; | 
| 820 | 
  | 
  | 
      d2 = nratio / cos2; | 
| 821 | 
  | 
  | 
      d1 = (d1 - d2) / (d1 + d2); | 
| 822 | 
  | 
  | 
      refl += sqr(d1); | 
| 823 | 
  | 
  | 
      refl *= 0.5; | 
| 824 | 
  | 
  | 
   } | 
| 825 | 
  | 
  | 
    | 
| 826 | 
  | 
  | 
   if (pmapRandom(rouletteState) > refl) { | 
| 827 | 
  | 
  | 
      /* Refraction */ | 
| 828 | 
  | 
  | 
      photonRay(rayIn, &rayOut, PMAP_REFRACT, NULL); | 
| 829 | 
  | 
  | 
      d1 = nratio * cos1 - cos2; | 
| 830 | 
  | 
  | 
       | 
| 831 | 
  | 
  | 
      for (i = 0; i < 3; i++) | 
| 832 | 
  | 
  | 
         rayOut.rdir [i] = nratio * rayIn -> rdir [i] + d1 * dnorm [i]; | 
| 833 | 
  | 
  | 
          | 
| 834 | 
  | 
  | 
      if (hastexture && DOT(rayOut.rdir, rayIn -> ron) * hastexture >= -FTINY) { | 
| 835 | 
  | 
  | 
         d1 *= hastexture; | 
| 836 | 
  | 
  | 
          | 
| 837 | 
  | 
  | 
         for (i = 0; i < 3; i++) | 
| 838 | 
  | 
  | 
            rayOut.rdir [i] = nratio * rayIn -> rdir [i] +  | 
| 839 | 
  | 
  | 
                              d1 * rayIn -> ron [i]; | 
| 840 | 
  | 
  | 
                               | 
| 841 | 
  | 
  | 
         normalize(rayOut.rdir); | 
| 842 | 
  | 
  | 
      } | 
| 843 | 
  | 
  | 
       | 
| 844 | 
  | 
  | 
      copycolor(rayOut.cext, ctrans); | 
| 845 | 
  | 
  | 
      copycolor(rayOut.albedo, talb); | 
| 846 | 
  | 
  | 
   } | 
| 847 | 
  | 
  | 
    | 
| 848 | 
  | 
  | 
   else { | 
| 849 | 
  | 
  | 
      /* Reflection */ | 
| 850 | 
  | 
  | 
      photonRay(rayIn, &rayOut, PMAP_SPECREFL, NULL); | 
| 851 | 
  | 
  | 
      VSUM(rayOut.rdir, rayIn -> rdir, dnorm, 2 * cos1); | 
| 852 | 
  | 
  | 
       | 
| 853 | 
  | 
  | 
      if (hastexture && DOT(rayOut.rdir, rayIn -> ron) * hastexture <= FTINY) | 
| 854 | 
  | 
  | 
         for (i = 0; i < 3; i++) | 
| 855 | 
  | 
  | 
            rayOut.rdir [i] = rayIn -> rdir [i] +  | 
| 856 | 
  | 
  | 
                              2 * rayIn -> rod * rayIn -> ron [i]; | 
| 857 | 
  | 
  | 
   } | 
| 858 | 
  | 
  | 
    | 
| 859 | 
  | 
  | 
   /* Ray is modified by medium defined by cext and albedo in | 
| 860 | 
  | 
  | 
    * photonParticipate() */ | 
| 861 | 
  | 
  | 
   tracePhoton(&rayOut); | 
| 862 | 
  | 
  | 
    | 
| 863 | 
  | 
  | 
   return 0; | 
| 864 | 
  | 
  | 
} | 
| 865 | 
  | 
  | 
 | 
| 866 | 
  | 
  | 
 | 
| 867 | 
  | 
  | 
 | 
| 868 | 
  | 
  | 
static int glassPhotonScatter (OBJREC *mat, RAY *rayIn) | 
| 869 | 
  | 
  | 
/* Generate new photon ray for glass material and recurse */ | 
| 870 | 
  | 
  | 
{ | 
| 871 | 
  | 
  | 
   float    albedo, xi, ptrans; | 
| 872 | 
  | 
  | 
   COLOR    mcolor, refl, trans; | 
| 873 | 
  | 
  | 
   double   pdot, cos2, d, r1e, r1m, rindex = 0.0; | 
| 874 | 
  | 
  | 
   FVECT    pnorm, pdir; | 
| 875 | 
  | 
  | 
   int      hastexture, i; | 
| 876 | 
  | 
  | 
   RAY      rayOut; | 
| 877 | 
  | 
  | 
 | 
| 878 | 
  | 
  | 
   /* check arguments */ | 
| 879 | 
  | 
  | 
   if (mat -> oargs.nfargs == 3) | 
| 880 | 
  | 
  | 
           rindex = RINDEX; | 
| 881 | 
  | 
  | 
   else if (mat -> oargs.nfargs == 4) | 
| 882 | 
  | 
  | 
           rindex = mat -> oargs.farg [3]; | 
| 883 | 
  | 
  | 
   else objerror(mat, USER, "bad arguments"); | 
| 884 | 
  | 
  | 
 | 
| 885 | 
  | 
  | 
   copycolor(mcolor, mat -> oargs.farg);    | 
| 886 | 
  | 
  | 
    | 
| 887 | 
  | 
  | 
   /* get modifiers */ | 
| 888 | 
  | 
  | 
   raytexture(rayIn, mat -> omod); | 
| 889 | 
  | 
  | 
    | 
| 890 | 
  | 
  | 
   /* reorient if necessary */ | 
| 891 | 
  | 
  | 
   if (rayIn -> rod < 0)  | 
| 892 | 
  | 
  | 
      flipsurface(rayIn); | 
| 893 | 
greg | 
2.4 | 
   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY) )) | 
| 894 | 
greg | 
2.1 | 
      pdot = raynormal(pnorm, rayIn); | 
| 895 | 
  | 
  | 
   else { | 
| 896 | 
  | 
  | 
      VCOPY(pnorm, rayIn -> ron); | 
| 897 | 
  | 
  | 
      pdot = rayIn -> rod; | 
| 898 | 
  | 
  | 
   } | 
| 899 | 
  | 
  | 
    | 
| 900 | 
  | 
  | 
   /* Modify material color */ | 
| 901 | 
  | 
  | 
   multcolor(mcolor, rayIn -> pcol); | 
| 902 | 
  | 
  | 
    | 
| 903 | 
  | 
  | 
   /* angular transmission */ | 
| 904 | 
  | 
  | 
   cos2 = sqrt((1 - 1 / sqr(rindex)) + sqr(pdot / rindex)); | 
| 905 | 
  | 
  | 
   setcolor(mcolor, pow(mcolor [0], 1 / cos2), pow(mcolor [1], 1 / cos2),  | 
| 906 | 
  | 
  | 
            pow(mcolor [2], 1 / cos2)); | 
| 907 | 
  | 
  | 
             | 
| 908 | 
  | 
  | 
   /* compute reflection */ | 
| 909 | 
  | 
  | 
   r1e = (pdot - rindex * cos2) / (pdot + rindex * cos2); | 
| 910 | 
  | 
  | 
   r1e *= r1e; | 
| 911 | 
  | 
  | 
   r1m = (1 / pdot - rindex / cos2) / (1 / pdot + rindex / cos2); | 
| 912 | 
  | 
  | 
   r1m *= r1m; | 
| 913 | 
  | 
  | 
    | 
| 914 | 
  | 
  | 
   for (i = 0; i < 3; i++) { | 
| 915 | 
  | 
  | 
      double r1ed2, r1md2, d2; | 
| 916 | 
  | 
  | 
       | 
| 917 | 
  | 
  | 
      d = mcolor [i]; | 
| 918 | 
  | 
  | 
      d2 = sqr(d); | 
| 919 | 
  | 
  | 
      r1ed2 = sqr(r1e) * d2; | 
| 920 | 
  | 
  | 
      r1md2 = sqr(r1m) * d2; | 
| 921 | 
  | 
  | 
       | 
| 922 | 
  | 
  | 
      /* compute transmittance */ | 
| 923 | 
  | 
  | 
      trans [i] = 0.5 * d *  | 
| 924 | 
  | 
  | 
                  (sqr(1 - r1e) / (1 - r1ed2) + sqr(1 - r1m) / (1 - r1md2)); | 
| 925 | 
  | 
  | 
                              | 
| 926 | 
  | 
  | 
      /* compute reflectance */ | 
| 927 | 
  | 
  | 
      refl [i] = 0.5 * (r1e * (1 + (1 - 2 * r1e) * d2) / (1 - r1ed2) + | 
| 928 | 
  | 
  | 
                        r1m * (1 + (1 - 2 * r1m) * d2) / (1 - r1md2)); | 
| 929 | 
  | 
  | 
   } | 
| 930 | 
  | 
  | 
    | 
| 931 | 
  | 
  | 
   /* Set up probabilities */ | 
| 932 | 
  | 
  | 
   ptrans = colorAvg(trans); | 
| 933 | 
  | 
  | 
   albedo = colorAvg(refl) + ptrans; | 
| 934 | 
  | 
  | 
   xi = pmapRandom(rouletteState); | 
| 935 | 
  | 
  | 
    | 
| 936 | 
  | 
  | 
 | 
| 937 | 
  | 
  | 
   if (xi > albedo)  | 
| 938 | 
  | 
  | 
      /* Absorbed */ | 
| 939 | 
  | 
  | 
      return 0; | 
| 940 | 
  | 
  | 
       | 
| 941 | 
  | 
  | 
   if (xi > (albedo -= ptrans)) { | 
| 942 | 
  | 
  | 
      /* Transmitted */ | 
| 943 | 
  | 
  | 
 | 
| 944 | 
  | 
  | 
      if (hastexture) { | 
| 945 | 
  | 
  | 
         /* perturb direction */ | 
| 946 | 
  | 
  | 
         VSUM(pdir, rayIn -> rdir, rayIn -> pert, 2 * (1 - rindex)); | 
| 947 | 
  | 
  | 
          | 
| 948 | 
  | 
  | 
         if (normalize(pdir) == 0) { | 
| 949 | 
  | 
  | 
            objerror(mat, WARNING, "bad perturbation"); | 
| 950 | 
  | 
  | 
            VCOPY(pdir, rayIn -> rdir); | 
| 951 | 
  | 
  | 
         } | 
| 952 | 
  | 
  | 
      }  | 
| 953 | 
  | 
  | 
      else VCOPY(pdir, rayIn -> rdir); | 
| 954 | 
  | 
  | 
       | 
| 955 | 
  | 
  | 
      VCOPY(rayOut.rdir, pdir); | 
| 956 | 
  | 
  | 
      photonRay(rayIn, &rayOut, PMAP_SPECTRANS, mcolor);       | 
| 957 | 
  | 
  | 
   } | 
| 958 | 
  | 
  | 
    | 
| 959 | 
  | 
  | 
   else { | 
| 960 | 
  | 
  | 
      /* reflected ray */ | 
| 961 | 
  | 
  | 
      VSUM(rayOut.rdir, rayIn -> rdir, pnorm, 2 * pdot); | 
| 962 | 
  | 
  | 
      photonRay(rayIn, &rayOut, PMAP_SPECREFL, mcolor);       | 
| 963 | 
  | 
  | 
   } | 
| 964 | 
  | 
  | 
 | 
| 965 | 
  | 
  | 
   tracePhoton(&rayOut); | 
| 966 | 
  | 
  | 
   return 0; | 
| 967 | 
  | 
  | 
} | 
| 968 | 
  | 
  | 
 | 
| 969 | 
  | 
  | 
 | 
| 970 | 
  | 
  | 
 | 
| 971 | 
  | 
  | 
static int aliasPhotonScatter (OBJREC *mat, RAY *rayIn) | 
| 972 | 
  | 
  | 
/* Transfer photon scattering to alias target */ | 
| 973 | 
  | 
  | 
{ | 
| 974 | 
  | 
  | 
   OBJECT   aliasObj; | 
| 975 | 
  | 
  | 
   OBJREC   aliasRec; | 
| 976 | 
  | 
  | 
    | 
| 977 | 
  | 
  | 
   /* Straight replacement? */ | 
| 978 | 
  | 
  | 
   if (!mat -> oargs.nsargs) { | 
| 979 | 
  | 
  | 
      mat = objptr(mat -> omod); | 
| 980 | 
  | 
  | 
      photonScatter [mat -> otype] (mat, rayIn); | 
| 981 | 
  | 
  | 
       | 
| 982 | 
  | 
  | 
      return 0; | 
| 983 | 
  | 
  | 
   } | 
| 984 | 
  | 
  | 
    | 
| 985 | 
  | 
  | 
   /* Else replace alias */ | 
| 986 | 
  | 
  | 
   if (mat -> oargs.nsargs != 1) | 
| 987 | 
  | 
  | 
      objerror(mat, INTERNAL, "bad # string arguments"); | 
| 988 | 
  | 
  | 
       | 
| 989 | 
  | 
  | 
   aliasObj = lastmod(objndx(mat), mat -> oargs.sarg [0]); | 
| 990 | 
  | 
  | 
       | 
| 991 | 
  | 
  | 
   if (aliasObj < 0)  | 
| 992 | 
  | 
  | 
      objerror(mat, USER, "bad reference"); | 
| 993 | 
  | 
  | 
       | 
| 994 | 
  | 
  | 
   memcpy(&aliasRec, objptr(aliasObj), sizeof(OBJREC)); | 
| 995 | 
  | 
  | 
    | 
| 996 | 
  | 
  | 
   /* Substitute modifier */ | 
| 997 | 
  | 
  | 
   aliasRec.omod = mat -> omod; | 
| 998 | 
  | 
  | 
    | 
| 999 | 
  | 
  | 
   /* Replacement scattering routine */ | 
| 1000 | 
  | 
  | 
   photonScatter [aliasRec.otype] (&aliasRec, rayIn); | 
| 1001 | 
  | 
  | 
   return 0; | 
| 1002 | 
  | 
  | 
} | 
| 1003 | 
  | 
  | 
 | 
| 1004 | 
  | 
  | 
 | 
| 1005 | 
  | 
  | 
 | 
| 1006 | 
  | 
  | 
static int clipPhotonScatter (OBJREC *mat, RAY *rayIn) | 
| 1007 | 
  | 
  | 
/* Generate new photon ray for antimatter material and recurse */ | 
| 1008 | 
  | 
  | 
{ | 
| 1009 | 
  | 
  | 
   OBJECT      obj = objndx(mat), mod, cset [MAXSET + 1], *modset; | 
| 1010 | 
  | 
  | 
   int         entering, inside = 0, i; | 
| 1011 | 
  | 
  | 
   const RAY   *rp; | 
| 1012 | 
  | 
  | 
   RAY         rayOut; | 
| 1013 | 
  | 
  | 
 | 
| 1014 | 
  | 
  | 
   if ((modset = (OBJECT*)mat -> os) == NULL) {    | 
| 1015 | 
  | 
  | 
      if (mat -> oargs.nsargs < 1 || mat -> oargs.nsargs > MAXSET) | 
| 1016 | 
  | 
  | 
         objerror(mat, USER, "bad # arguments"); | 
| 1017 | 
  | 
  | 
          | 
| 1018 | 
  | 
  | 
      modset = (OBJECT*)malloc((mat -> oargs.nsargs + 1) * sizeof(OBJECT)); | 
| 1019 | 
  | 
  | 
       | 
| 1020 | 
  | 
  | 
      if (modset == NULL)  | 
| 1021 | 
  | 
  | 
         error(SYSTEM, "out of memory in clipPhotonScatter"); | 
| 1022 | 
  | 
  | 
      modset [0] = 0; | 
| 1023 | 
  | 
  | 
       | 
| 1024 | 
  | 
  | 
      for (i = 0; i < mat -> oargs.nsargs; i++) { | 
| 1025 | 
  | 
  | 
         if (!strcmp(mat -> oargs.sarg [i], VOIDID)) | 
| 1026 | 
  | 
  | 
            continue; | 
| 1027 | 
  | 
  | 
             | 
| 1028 | 
  | 
  | 
         if ((mod = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) { | 
| 1029 | 
  | 
  | 
            sprintf(errmsg, "unknown modifier \"%s\"", mat -> oargs.sarg [i]); | 
| 1030 | 
  | 
  | 
            objerror(mat, WARNING, errmsg); | 
| 1031 | 
  | 
  | 
            continue; | 
| 1032 | 
  | 
  | 
         } | 
| 1033 | 
  | 
  | 
          | 
| 1034 | 
  | 
  | 
         if (inset(modset, mod)) { | 
| 1035 | 
  | 
  | 
            objerror(mat, WARNING, "duplicate modifier"); | 
| 1036 | 
  | 
  | 
            continue; | 
| 1037 | 
  | 
  | 
         } | 
| 1038 | 
  | 
  | 
          | 
| 1039 | 
  | 
  | 
         insertelem(modset, mod); | 
| 1040 | 
  | 
  | 
      } | 
| 1041 | 
  | 
  | 
       | 
| 1042 | 
  | 
  | 
      mat -> os = (char*)modset; | 
| 1043 | 
  | 
  | 
   } | 
| 1044 | 
  | 
  | 
    | 
| 1045 | 
  | 
  | 
   if (rayIn -> clipset != NULL)  | 
| 1046 | 
  | 
  | 
      setcopy(cset, rayIn -> clipset); | 
| 1047 | 
  | 
  | 
   else cset [0] = 0; | 
| 1048 | 
  | 
  | 
    | 
| 1049 | 
  | 
  | 
   entering = rayIn -> rod > 0; | 
| 1050 | 
  | 
  | 
    | 
| 1051 | 
  | 
  | 
   /* Store photon incident from front if material defined as sensor */ | 
| 1052 | 
  | 
  | 
   if (entering && inset(photonSensorSet, obj)) | 
| 1053 | 
  | 
  | 
      addPhotons(rayIn); | 
| 1054 | 
  | 
  | 
 | 
| 1055 | 
  | 
  | 
   for (i = modset [0]; i > 0; i--) { | 
| 1056 | 
  | 
  | 
      if (entering) { | 
| 1057 | 
  | 
  | 
         if (!inset(cset, modset [i])) { | 
| 1058 | 
  | 
  | 
            if (cset [0] >= MAXSET)  | 
| 1059 | 
  | 
  | 
               error(INTERNAL, "set overflow in clipPhotonScatter"); | 
| 1060 | 
  | 
  | 
            insertelem(cset, modset [i]); | 
| 1061 | 
  | 
  | 
         } | 
| 1062 | 
  | 
  | 
      }  | 
| 1063 | 
  | 
  | 
      else if (inset(cset, modset [i]))  | 
| 1064 | 
  | 
  | 
         deletelem(cset, modset [i]); | 
| 1065 | 
  | 
  | 
   } | 
| 1066 | 
  | 
  | 
    | 
| 1067 | 
  | 
  | 
   rayIn -> newcset = cset; | 
| 1068 | 
  | 
  | 
    | 
| 1069 | 
  | 
  | 
   if (strcmp(mat -> oargs.sarg [0], VOIDID)) {    | 
| 1070 | 
  | 
  | 
           for (rp = rayIn; rp -> parent != NULL; rp = rp -> parent) {      | 
| 1071 | 
  | 
  | 
                   if ( !(rp -> rtype & RAYREFL) && rp->parent->ro != NULL &&  | 
| 1072 | 
  | 
  | 
                                inset(modset, rp -> parent -> ro -> omod)) {     | 
| 1073 | 
  | 
  | 
 | 
| 1074 | 
  | 
  | 
                           if (rp -> parent -> rod > 0) | 
| 1075 | 
  | 
  | 
                                   inside++; | 
| 1076 | 
  | 
  | 
                           else inside--; | 
| 1077 | 
  | 
  | 
                   } | 
| 1078 | 
  | 
  | 
           } | 
| 1079 | 
  | 
  | 
            | 
| 1080 | 
  | 
  | 
           if (inside > 0) { | 
| 1081 | 
  | 
  | 
                   flipsurface(rayIn); | 
| 1082 | 
  | 
  | 
                   mat = objptr(lastmod(obj, mat -> oargs.sarg [0])); | 
| 1083 | 
  | 
  | 
                   photonScatter [mat -> otype] (mat, rayIn); | 
| 1084 | 
  | 
  | 
                   return 0; | 
| 1085 | 
  | 
  | 
           } | 
| 1086 | 
  | 
  | 
   } | 
| 1087 | 
  | 
  | 
    | 
| 1088 | 
  | 
  | 
   /* Else transfer ray */ | 
| 1089 | 
  | 
  | 
   photonRay(rayIn, &rayOut, PMAP_XFER, NULL); | 
| 1090 | 
  | 
  | 
   tracePhoton(&rayOut); | 
| 1091 | 
  | 
  | 
    | 
| 1092 | 
  | 
  | 
   return 0; | 
| 1093 | 
  | 
  | 
} | 
| 1094 | 
  | 
  | 
 | 
| 1095 | 
  | 
  | 
 | 
| 1096 | 
  | 
  | 
 | 
| 1097 | 
  | 
  | 
static int mirrorPhotonScatter (OBJREC *mat, RAY *rayIn) | 
| 1098 | 
  | 
  | 
/* Generate new photon ray for mirror material and recurse */ | 
| 1099 | 
  | 
  | 
{ | 
| 1100 | 
  | 
  | 
   RAY      rayOut; | 
| 1101 | 
  | 
  | 
   int      rpure = 1, i; | 
| 1102 | 
  | 
  | 
   FVECT    pnorm; | 
| 1103 | 
  | 
  | 
   double   pdot; | 
| 1104 | 
  | 
  | 
   float    albedo; | 
| 1105 | 
  | 
  | 
   COLOR    mcolor; | 
| 1106 | 
  | 
  | 
 | 
| 1107 | 
  | 
  | 
   /* check arguments */ | 
| 1108 | 
  | 
  | 
   if (mat -> oargs.nfargs != 3 || mat -> oargs.nsargs > 1) | 
| 1109 | 
  | 
  | 
      objerror(mat, USER, "bad number of arguments"); | 
| 1110 | 
  | 
  | 
       | 
| 1111 | 
  | 
  | 
   /* back is black */ | 
| 1112 | 
  | 
  | 
   if (rayIn -> rod < 0)  | 
| 1113 | 
  | 
  | 
      return 0; | 
| 1114 | 
  | 
  | 
       | 
| 1115 | 
  | 
  | 
   /* get modifiers */ | 
| 1116 | 
  | 
  | 
   raytexture(rayIn, mat -> omod); | 
| 1117 | 
  | 
  | 
    | 
| 1118 | 
  | 
  | 
   /* assign material color */ | 
| 1119 | 
  | 
  | 
   copycolor(mcolor, mat -> oargs.farg);  | 
| 1120 | 
  | 
  | 
   multcolor(mcolor, rayIn -> pcol); | 
| 1121 | 
  | 
  | 
    | 
| 1122 | 
  | 
  | 
   /* Set up probabilities */ | 
| 1123 | 
  | 
  | 
   albedo = colorAvg(mcolor); | 
| 1124 | 
  | 
  | 
    | 
| 1125 | 
  | 
  | 
   if (pmapRandom(rouletteState) > albedo)  | 
| 1126 | 
  | 
  | 
      /* Absorbed */ | 
| 1127 | 
  | 
  | 
      return 0; | 
| 1128 | 
  | 
  | 
       | 
| 1129 | 
  | 
  | 
   /* compute reflected ray */ | 
| 1130 | 
  | 
  | 
   photonRay(rayIn, &rayOut, PMAP_SPECREFL, mcolor); | 
| 1131 | 
  | 
  | 
 | 
| 1132 | 
  | 
  | 
   if (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)) { | 
| 1133 | 
  | 
  | 
      /* use textures */ | 
| 1134 | 
  | 
  | 
      pdot = raynormal(pnorm, rayIn);    | 
| 1135 | 
  | 
  | 
       | 
| 1136 | 
  | 
  | 
      for (i = 0; i < 3; i++) | 
| 1137 | 
  | 
  | 
         rayOut.rdir [i] = rayIn -> rdir [i] + 2 * pdot * pnorm [i]; | 
| 1138 | 
  | 
  | 
          | 
| 1139 | 
  | 
  | 
      rpure = 0; | 
| 1140 | 
  | 
  | 
   } | 
| 1141 | 
  | 
  | 
    | 
| 1142 | 
  | 
  | 
   /* Check for penetration */ | 
| 1143 | 
  | 
  | 
   if (rpure || DOT(rayOut.rdir, rayIn -> ron) <= FTINY) | 
| 1144 | 
  | 
  | 
      for (i = 0; i < 3; i++) | 
| 1145 | 
  | 
  | 
         rayOut.rdir [i] = rayIn -> rdir [i] +  | 
| 1146 | 
  | 
  | 
                           2 * rayIn -> rod * rayIn -> ron [i]; | 
| 1147 | 
  | 
  | 
                            | 
| 1148 | 
  | 
  | 
   tracePhoton(&rayOut); | 
| 1149 | 
  | 
  | 
   return 0; | 
| 1150 | 
  | 
  | 
} | 
| 1151 | 
  | 
  | 
 | 
| 1152 | 
  | 
  | 
 | 
| 1153 | 
  | 
  | 
 | 
| 1154 | 
  | 
  | 
static int mistPhotonScatter (OBJREC *mat, RAY *rayIn) | 
| 1155 | 
  | 
  | 
/* Generate new photon ray within mist and recurse */ | 
| 1156 | 
  | 
  | 
{ | 
| 1157 | 
  | 
  | 
   COLOR    mext; | 
| 1158 | 
  | 
  | 
   RREAL    re, ge, be; | 
| 1159 | 
  | 
  | 
   RAY      rayOut; | 
| 1160 | 
  | 
  | 
 | 
| 1161 | 
  | 
  | 
   /* check arguments */ | 
| 1162 | 
  | 
  | 
   if (mat -> oargs.nfargs > 7)  | 
| 1163 | 
  | 
  | 
      objerror(mat, USER, "bad arguments"); | 
| 1164 | 
  | 
  | 
       | 
| 1165 | 
  | 
  | 
   if (mat -> oargs.nfargs > 2) { | 
| 1166 | 
  | 
  | 
      /* compute extinction */ | 
| 1167 | 
  | 
  | 
      copycolor(mext, mat -> oargs.farg); | 
| 1168 | 
  | 
  | 
      /* get modifiers */ | 
| 1169 | 
  | 
  | 
      raytexture(rayIn, mat -> omod); | 
| 1170 | 
  | 
  | 
      multcolor(mext, rayIn -> pcol); | 
| 1171 | 
  | 
  | 
   }  | 
| 1172 | 
  | 
  | 
   else setcolor(mext, 0, 0, 0); | 
| 1173 | 
  | 
  | 
       | 
| 1174 | 
  | 
  | 
   photonRay(rayIn, &rayOut, PMAP_XFER, NULL); | 
| 1175 | 
  | 
  | 
    | 
| 1176 | 
  | 
  | 
   if (rayIn -> rod > 0) { | 
| 1177 | 
  | 
  | 
      /* entering ray */ | 
| 1178 | 
  | 
  | 
      addcolor(rayOut.cext, mext); | 
| 1179 | 
  | 
  | 
       | 
| 1180 | 
  | 
  | 
      if (mat -> oargs.nfargs > 5)  | 
| 1181 | 
  | 
  | 
         copycolor(rayOut.albedo, mat -> oargs.farg + 3); | 
| 1182 | 
  | 
  | 
      if (mat -> oargs.nfargs > 6)  | 
| 1183 | 
  | 
  | 
         rayOut.gecc = mat -> oargs.farg [6]; | 
| 1184 | 
  | 
  | 
   }  | 
| 1185 | 
  | 
  | 
    | 
| 1186 | 
  | 
  | 
   else { | 
| 1187 | 
  | 
  | 
      /* leaving ray */ | 
| 1188 | 
  | 
  | 
      re = max(rayIn -> cext [0] - mext [0], cextinction [0]); | 
| 1189 | 
  | 
  | 
      ge = max(rayIn -> cext [1] - mext [1], cextinction [1]); | 
| 1190 | 
  | 
  | 
      be = max(rayIn -> cext [2] - mext [2], cextinction [2]); | 
| 1191 | 
  | 
  | 
      setcolor(rayOut.cext, re, ge, be); | 
| 1192 | 
  | 
  | 
       | 
| 1193 | 
  | 
  | 
      if (mat -> oargs.nfargs > 5)  | 
| 1194 | 
  | 
  | 
         copycolor(rayOut.albedo, salbedo); | 
| 1195 | 
  | 
  | 
      if (mat -> oargs.nfargs > 6) | 
| 1196 | 
  | 
  | 
         rayOut.gecc = seccg; | 
| 1197 | 
  | 
  | 
   } | 
| 1198 | 
  | 
  | 
    | 
| 1199 | 
  | 
  | 
   tracePhoton(&rayOut); | 
| 1200 | 
  | 
  | 
    | 
| 1201 | 
  | 
  | 
   return 0; | 
| 1202 | 
  | 
  | 
} | 
| 1203 | 
  | 
  | 
 | 
| 1204 | 
  | 
  | 
 | 
| 1205 | 
  | 
  | 
 | 
| 1206 | 
  | 
  | 
static int mx_dataPhotonScatter (OBJREC *mat, RAY *rayIn) | 
| 1207 | 
  | 
  | 
/* Pass photon on to materials selected by mixture data */ | 
| 1208 | 
  | 
  | 
{ | 
| 1209 | 
  | 
  | 
   OBJECT   obj; | 
| 1210 | 
  | 
  | 
   double   coef, pt [MAXDIM]; | 
| 1211 | 
  | 
  | 
   DATARRAY *dp; | 
| 1212 | 
  | 
  | 
   OBJECT   mod [2]; | 
| 1213 | 
  | 
  | 
   MFUNC    *mf; | 
| 1214 | 
  | 
  | 
   int      i; | 
| 1215 | 
  | 
  | 
 | 
| 1216 | 
  | 
  | 
   if (mat -> oargs.nsargs < 6)  | 
| 1217 | 
  | 
  | 
      objerror(mat, USER, "bad # arguments"); | 
| 1218 | 
  | 
  | 
       | 
| 1219 | 
  | 
  | 
   obj = objndx(mat); | 
| 1220 | 
  | 
  | 
    | 
| 1221 | 
  | 
  | 
   for (i = 0; i < 2; i++) | 
| 1222 | 
  | 
  | 
      if (!strcmp(mat -> oargs.sarg [i], VOIDID))  | 
| 1223 | 
  | 
  | 
         mod [i] = OVOID; | 
| 1224 | 
  | 
  | 
      else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) { | 
| 1225 | 
  | 
  | 
         sprintf(errmsg, "undefined modifier \"%s\"", mat -> oargs.sarg [i]); | 
| 1226 | 
  | 
  | 
         objerror(mat, USER, errmsg); | 
| 1227 | 
  | 
  | 
      } | 
| 1228 | 
  | 
  | 
       | 
| 1229 | 
  | 
  | 
   dp = getdata(mat -> oargs.sarg [3]); | 
| 1230 | 
  | 
  | 
   i = (1 << dp -> nd) - 1; | 
| 1231 | 
  | 
  | 
   mf = getfunc(mat, 4, i << 5, 0); | 
| 1232 | 
  | 
  | 
   setfunc(mat, rayIn); | 
| 1233 | 
  | 
  | 
   errno = 0; | 
| 1234 | 
  | 
  | 
    | 
| 1235 | 
  | 
  | 
   for (i = 0; i < dp -> nd; i++) { | 
| 1236 | 
  | 
  | 
      pt [i] = evalue(mf -> ep [i]); | 
| 1237 | 
  | 
  | 
       | 
| 1238 | 
  | 
  | 
      if (errno) { | 
| 1239 | 
  | 
  | 
         objerror(mat, WARNING, "compute error"); | 
| 1240 | 
  | 
  | 
         return 0; | 
| 1241 | 
  | 
  | 
      } | 
| 1242 | 
  | 
  | 
   } | 
| 1243 | 
  | 
  | 
    | 
| 1244 | 
  | 
  | 
   coef = datavalue(dp, pt); | 
| 1245 | 
  | 
  | 
   errno = 0; | 
| 1246 | 
  | 
  | 
   coef = funvalue(mat -> oargs.sarg [2], 1, &coef); | 
| 1247 | 
  | 
  | 
    | 
| 1248 | 
  | 
  | 
   if (errno)  | 
| 1249 | 
  | 
  | 
      objerror(mat, WARNING, "compute error"); | 
| 1250 | 
  | 
  | 
   else { | 
| 1251 | 
  | 
  | 
      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]); | 
| 1252 | 
  | 
  | 
      photonScatter [mat -> otype] (mat, rayIn); | 
| 1253 | 
  | 
  | 
   } | 
| 1254 | 
  | 
  | 
    | 
| 1255 | 
  | 
  | 
   return 0; | 
| 1256 | 
  | 
  | 
} | 
| 1257 | 
  | 
  | 
 | 
| 1258 | 
  | 
  | 
 | 
| 1259 | 
  | 
  | 
 | 
| 1260 | 
  | 
  | 
static int mx_pdataPhotonScatter (OBJREC *mat, RAY *rayIn) | 
| 1261 | 
  | 
  | 
/* Pass photon on to materials selected by mixture picture */ | 
| 1262 | 
  | 
  | 
{ | 
| 1263 | 
  | 
  | 
   OBJECT   obj; | 
| 1264 | 
  | 
  | 
   double   col [3], coef, pt [MAXDIM]; | 
| 1265 | 
  | 
  | 
   DATARRAY *dp; | 
| 1266 | 
  | 
  | 
   OBJECT   mod [2]; | 
| 1267 | 
  | 
  | 
   MFUNC    *mf; | 
| 1268 | 
  | 
  | 
   int      i; | 
| 1269 | 
  | 
  | 
 | 
| 1270 | 
  | 
  | 
   if (mat -> oargs.nsargs < 7)  | 
| 1271 | 
  | 
  | 
      objerror(mat, USER, "bad # arguments"); | 
| 1272 | 
  | 
  | 
       | 
| 1273 | 
  | 
  | 
   obj = objndx(mat); | 
| 1274 | 
  | 
  | 
    | 
| 1275 | 
  | 
  | 
   for (i = 0; i < 2; i++) | 
| 1276 | 
  | 
  | 
      if (!strcmp(mat -> oargs.sarg [i], VOIDID))  | 
| 1277 | 
  | 
  | 
         mod [i] = OVOID; | 
| 1278 | 
  | 
  | 
      else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) { | 
| 1279 | 
  | 
  | 
         sprintf(errmsg, "undefined modifier \"%s\"", mat -> oargs.sarg [i]); | 
| 1280 | 
  | 
  | 
         objerror(mat, USER, errmsg); | 
| 1281 | 
  | 
  | 
      } | 
| 1282 | 
  | 
  | 
       | 
| 1283 | 
  | 
  | 
   dp = getpict(mat -> oargs.sarg [3]); | 
| 1284 | 
  | 
  | 
   mf = getfunc(mat, 4, 0x3 << 5, 0); | 
| 1285 | 
  | 
  | 
   setfunc(mat, rayIn); | 
| 1286 | 
  | 
  | 
   errno = 0; | 
| 1287 | 
  | 
  | 
   pt [1] = evalue(mf -> ep [0]); | 
| 1288 | 
  | 
  | 
   pt [0] = evalue(mf -> ep [1]); | 
| 1289 | 
  | 
  | 
    | 
| 1290 | 
  | 
  | 
   if (errno) { | 
| 1291 | 
  | 
  | 
      objerror(mat, WARNING, "compute error"); | 
| 1292 | 
  | 
  | 
      return 0; | 
| 1293 | 
  | 
  | 
   } | 
| 1294 | 
  | 
  | 
    | 
| 1295 | 
  | 
  | 
   for (i = 0; i < 3; i++)  | 
| 1296 | 
  | 
  | 
      col [i] = datavalue(dp + i, pt); | 
| 1297 | 
  | 
  | 
       | 
| 1298 | 
  | 
  | 
   errno = 0; | 
| 1299 | 
  | 
  | 
   coef = funvalue(mat -> oargs.sarg [2], 3, col); | 
| 1300 | 
  | 
  | 
    | 
| 1301 | 
  | 
  | 
   if (errno)  | 
| 1302 | 
  | 
  | 
      objerror(mat, WARNING, "compute error"); | 
| 1303 | 
  | 
  | 
   else { | 
| 1304 | 
  | 
  | 
      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]); | 
| 1305 | 
  | 
  | 
      photonScatter [mat -> otype] (mat, rayIn); | 
| 1306 | 
  | 
  | 
   }    | 
| 1307 | 
  | 
  | 
    | 
| 1308 | 
  | 
  | 
   return 0; | 
| 1309 | 
  | 
  | 
} | 
| 1310 | 
  | 
  | 
 | 
| 1311 | 
  | 
  | 
 | 
| 1312 | 
  | 
  | 
 | 
| 1313 | 
  | 
  | 
static int mx_funcPhotonScatter (OBJREC *mat, RAY *rayIn) | 
| 1314 | 
  | 
  | 
/* Pass photon on to materials selected by mixture function */ | 
| 1315 | 
  | 
  | 
{ | 
| 1316 | 
  | 
  | 
   OBJECT   obj, mod [2]; | 
| 1317 | 
  | 
  | 
   int      i; | 
| 1318 | 
  | 
  | 
   double   coef; | 
| 1319 | 
  | 
  | 
   MFUNC    *mf; | 
| 1320 | 
  | 
  | 
 | 
| 1321 | 
  | 
  | 
   if (mat -> oargs.nsargs < 4)  | 
| 1322 | 
  | 
  | 
      objerror(mat, USER, "bad # arguments"); | 
| 1323 | 
  | 
  | 
       | 
| 1324 | 
  | 
  | 
   obj = objndx(mat); | 
| 1325 | 
  | 
  | 
    | 
| 1326 | 
  | 
  | 
   for (i = 0; i < 2; i++) | 
| 1327 | 
  | 
  | 
      if (!strcmp(mat -> oargs.sarg [i], VOIDID)) | 
| 1328 | 
  | 
  | 
         mod [i] = OVOID; | 
| 1329 | 
  | 
  | 
      else if ((mod [i] = lastmod(obj, mat -> oargs.sarg [i])) == OVOID) { | 
| 1330 | 
  | 
  | 
         sprintf(errmsg, "undefined modifier \"%s\"", mat -> oargs.sarg [i]); | 
| 1331 | 
  | 
  | 
         objerror(mat, USER, errmsg); | 
| 1332 | 
  | 
  | 
      } | 
| 1333 | 
  | 
  | 
       | 
| 1334 | 
  | 
  | 
   mf = getfunc(mat, 3, 0x4, 0); | 
| 1335 | 
  | 
  | 
   setfunc(mat, rayIn); | 
| 1336 | 
  | 
  | 
   errno = 0; | 
| 1337 | 
  | 
  | 
    | 
| 1338 | 
  | 
  | 
   /* bound coefficient */ | 
| 1339 | 
  | 
  | 
   coef = min(1, max(0, evalue(mf -> ep [0]))); | 
| 1340 | 
  | 
  | 
    | 
| 1341 | 
  | 
  | 
   if (errno)  | 
| 1342 | 
  | 
  | 
      objerror(mat, WARNING, "compute error"); | 
| 1343 | 
  | 
  | 
   else {          | 
| 1344 | 
  | 
  | 
      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]); | 
| 1345 | 
  | 
  | 
      photonScatter [mat -> otype] (mat, rayIn); | 
| 1346 | 
  | 
  | 
   } | 
| 1347 | 
  | 
  | 
    | 
| 1348 | 
  | 
  | 
   return 0; | 
| 1349 | 
  | 
  | 
} | 
| 1350 | 
  | 
  | 
 | 
| 1351 | 
  | 
  | 
 | 
| 1352 | 
  | 
  | 
 | 
| 1353 | 
  | 
  | 
static int pattexPhotonScatter (OBJREC *mat, RAY *rayIn) | 
| 1354 | 
  | 
  | 
/* Generate new photon ray for pattern or texture modifier and recurse. | 
| 1355 | 
  | 
  | 
   This code is brought to you by Henkel! :^) */ | 
| 1356 | 
  | 
  | 
{ | 
| 1357 | 
  | 
  | 
   RAY   rayOut; | 
| 1358 | 
  | 
  | 
    | 
| 1359 | 
  | 
  | 
   /* Get pattern */ | 
| 1360 | 
  | 
  | 
   ofun [mat -> otype].funp(mat, rayIn); | 
| 1361 | 
  | 
  | 
   if (mat -> omod != OVOID) { | 
| 1362 | 
  | 
  | 
      /* Scatter using modifier (if any) */ | 
| 1363 | 
  | 
  | 
      mat = objptr(mat -> omod); | 
| 1364 | 
  | 
  | 
      photonScatter [mat -> otype] (mat, rayIn); | 
| 1365 | 
  | 
  | 
   } | 
| 1366 | 
  | 
  | 
   else { | 
| 1367 | 
  | 
  | 
      /* Transfer ray if no modifier */ | 
| 1368 | 
  | 
  | 
      photonRay(rayIn, &rayOut, PMAP_XFER, NULL); | 
| 1369 | 
  | 
  | 
      tracePhoton(&rayOut); | 
| 1370 | 
  | 
  | 
   } | 
| 1371 | 
  | 
  | 
    | 
| 1372 | 
  | 
  | 
   return 0; | 
| 1373 | 
  | 
  | 
} | 
| 1374 | 
  | 
  | 
 | 
| 1375 | 
  | 
  | 
 | 
| 1376 | 
  | 
  | 
 | 
| 1377 | 
rschregle | 
2.3 | 
/*  | 
| 1378 | 
rschregle | 
2.7 | 
   ================================================================== | 
| 1379 | 
rschregle | 
2.3 | 
   The following code is | 
| 1380 | 
  | 
  | 
   (c) Lucerne University of Applied Sciences and Arts, | 
| 1381 | 
  | 
  | 
   supported by the Swiss National Science Foundation (SNSF, #147053) | 
| 1382 | 
rschregle | 
2.7 | 
   ================================================================== | 
| 1383 | 
rschregle | 
2.3 | 
*/    | 
| 1384 | 
  | 
  | 
 | 
| 1385 | 
greg | 
2.1 | 
static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn) | 
| 1386 | 
  | 
  | 
/* Generate new photon ray for BSDF modifier and recurse. */ | 
| 1387 | 
  | 
  | 
{ | 
| 1388 | 
  | 
  | 
   int      hitFront; | 
| 1389 | 
  | 
  | 
   SDError  err; | 
| 1390 | 
rschregle | 
2.2 | 
   SDValue  bsdfVal; | 
| 1391 | 
greg | 
2.1 | 
   FVECT           upvec; | 
| 1392 | 
  | 
  | 
   MFUNC           *mf; | 
| 1393 | 
  | 
  | 
   BSDFDAT      nd; | 
| 1394 | 
  | 
  | 
   RAY      rayOut; | 
| 1395 | 
rschregle | 
2.2 | 
   COLOR    bsdfRGB; | 
| 1396 | 
greg | 
2.6 | 
   int      transmitted; | 
| 1397 | 
rschregle | 
2.2 | 
   double   prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD,  | 
| 1398 | 
greg | 
2.6 | 
            albedo, xi; | 
| 1399 | 
  | 
  | 
   const double patAlb = bright(rayIn -> pcol); | 
| 1400 | 
rschregle | 
2.2 | 
    | 
| 1401 | 
greg | 
2.1 | 
   /* Following code adapted from m_bsdf() */ | 
| 1402 | 
  | 
  | 
   /* Check arguments */ | 
| 1403 | 
  | 
  | 
   if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 || | 
| 1404 | 
  | 
  | 
       mat -> oargs.nfargs % 3) | 
| 1405 | 
  | 
  | 
      objerror(mat, USER, "bad # arguments"); | 
| 1406 | 
  | 
  | 
       | 
| 1407 | 
  | 
  | 
        hitFront = (rayIn -> rod > 0); | 
| 1408 | 
  | 
  | 
 | 
| 1409 | 
  | 
  | 
        /* Load cal file */ | 
| 1410 | 
  | 
  | 
        mf = getfunc(mat, 5, 0x1d, 1); | 
| 1411 | 
  | 
  | 
         | 
| 1412 | 
  | 
  | 
        /* Get thickness */ | 
| 1413 | 
  | 
  | 
        nd.thick = evalue(mf -> ep [0]); | 
| 1414 | 
  | 
  | 
        if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) | 
| 1415 | 
  | 
  | 
                nd.thick = .0; | 
| 1416 | 
rschregle | 
2.7 | 
 | 
| 1417 | 
greg | 
2.1 | 
   /* Get BSDF data */ | 
| 1418 | 
  | 
  | 
   nd.sd = loadBSDF(mat -> oargs.sarg [1]); | 
| 1419 | 
  | 
  | 
    | 
| 1420 | 
rschregle | 
2.2 | 
   /* Extra diffuse reflectance from material def */ | 
| 1421 | 
greg | 
2.1 | 
   if (hitFront) { | 
| 1422 | 
  | 
  | 
      if (mat -> oargs.nfargs < 3) | 
| 1423 | 
  | 
  | 
         setcolor(nd.rdiff, .0, .0, .0); | 
| 1424 | 
  | 
  | 
      else setcolor(nd.rdiff, mat -> oargs.farg [0], mat -> oargs.farg [1],  | 
| 1425 | 
  | 
  | 
                    mat -> oargs.farg [2]); | 
| 1426 | 
  | 
  | 
   }     | 
| 1427 | 
  | 
  | 
   else if (mat -> oargs.nfargs < 6) { | 
| 1428 | 
  | 
  | 
        /* Check for absorbing backside */ | 
| 1429 | 
  | 
  | 
        if (!backvis && !nd.sd -> rb && !nd.sd -> tf) { | 
| 1430 | 
  | 
  | 
           SDfreeCache(nd.sd);                      | 
| 1431 | 
  | 
  | 
           return 0; | 
| 1432 | 
  | 
  | 
      } | 
| 1433 | 
  | 
  | 
       | 
| 1434 | 
  | 
  | 
      setcolor(nd.rdiff, .0, .0, .0); | 
| 1435 | 
  | 
  | 
   }  | 
| 1436 | 
  | 
  | 
   else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4],  | 
| 1437 | 
  | 
  | 
                 mat -> oargs.farg [5]); | 
| 1438 | 
  | 
  | 
 | 
| 1439 | 
rschregle | 
2.2 | 
        /* Extra diffuse transmittance from material def */ | 
| 1440 | 
greg | 
2.1 | 
        if (mat -> oargs.nfargs < 9) | 
| 1441 | 
  | 
  | 
           setcolor(nd.tdiff, .0, .0, .0); | 
| 1442 | 
  | 
  | 
   else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],  | 
| 1443 | 
  | 
  | 
                 mat -> oargs.farg [8]); | 
| 1444 | 
  | 
  | 
                | 
| 1445 | 
  | 
  | 
   nd.mp = mat; | 
| 1446 | 
  | 
  | 
   nd.pr = rayIn; | 
| 1447 | 
  | 
  | 
         | 
| 1448 | 
  | 
  | 
   /* Get modifiers */ | 
| 1449 | 
  | 
  | 
   raytexture(rayIn, mat -> omod); | 
| 1450 | 
  | 
  | 
    | 
| 1451 | 
  | 
  | 
   /* Modify diffuse values */ | 
| 1452 | 
  | 
  | 
   multcolor(nd.rdiff, rayIn -> pcol); | 
| 1453 | 
  | 
  | 
   multcolor(nd.tdiff, rayIn -> pcol); | 
| 1454 | 
  | 
  | 
                 | 
| 1455 | 
  | 
  | 
   /* Get up vector & xform to world coords */ | 
| 1456 | 
  | 
  | 
   upvec [0] = evalue(mf -> ep [1]); | 
| 1457 | 
  | 
  | 
   upvec [1] = evalue(mf -> ep [2]); | 
| 1458 | 
  | 
  | 
   upvec [2] = evalue(mf -> ep [3]); | 
| 1459 | 
  | 
  | 
    | 
| 1460 | 
  | 
  | 
   if (mf -> fxp != &unitxf) { | 
| 1461 | 
  | 
  | 
      multv3(upvec, upvec, mf -> fxp -> xfm); | 
| 1462 | 
  | 
  | 
      nd.thick *= mf -> fxp -> sca; | 
| 1463 | 
  | 
  | 
   } | 
| 1464 | 
  | 
  | 
    | 
| 1465 | 
  | 
  | 
   if (rayIn -> rox) { | 
| 1466 | 
  | 
  | 
      multv3(upvec, upvec, rayIn -> rox -> f.xfm); | 
| 1467 | 
  | 
  | 
      nd.thick *= rayIn -> rox -> f.sca; | 
| 1468 | 
  | 
  | 
   } | 
| 1469 | 
  | 
  | 
    | 
| 1470 | 
  | 
  | 
   /* Perturb normal */ | 
| 1471 | 
  | 
  | 
   raynormal(nd.pnorm, rayIn); | 
| 1472 | 
  | 
  | 
    | 
| 1473 | 
  | 
  | 
   /* Xform incident dir to local BSDF coords */ | 
| 1474 | 
  | 
  | 
   err = SDcompXform(nd.toloc, nd.pnorm, upvec); | 
| 1475 | 
  | 
  | 
    | 
| 1476 | 
  | 
  | 
   if (!err) { | 
| 1477 | 
  | 
  | 
      nd.vray [0] = -rayIn -> rdir [0]; | 
| 1478 | 
  | 
  | 
      nd.vray [1] = -rayIn -> rdir [1]; | 
| 1479 | 
  | 
  | 
      nd.vray [2] = -rayIn -> rdir [2]; | 
| 1480 | 
  | 
  | 
      err = SDmapDir(nd.vray, nd.toloc, nd.vray); | 
| 1481 | 
  | 
  | 
   } | 
| 1482 | 
  | 
  | 
    | 
| 1483 | 
  | 
  | 
   if (!err) | 
| 1484 | 
  | 
  | 
      err = SDinvXform(nd.fromloc, nd.toloc); | 
| 1485 | 
  | 
  | 
       | 
| 1486 | 
  | 
  | 
   if (err) { | 
| 1487 | 
  | 
  | 
      objerror(mat, WARNING, "Illegal orientation vector"); | 
| 1488 | 
  | 
  | 
      return 0; | 
| 1489 | 
  | 
  | 
   } | 
| 1490 | 
  | 
  | 
    | 
| 1491 | 
  | 
  | 
   /* Determine BSDF resolution */ | 
| 1492 | 
  | 
  | 
   err = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, SDqueryMin + SDqueryMax, nd.sd); | 
| 1493 | 
  | 
  | 
    | 
| 1494 | 
  | 
  | 
   if (err) | 
| 1495 | 
  | 
  | 
      objerror(mat, USER, transSDError(err)); | 
| 1496 | 
  | 
  | 
       | 
| 1497 | 
  | 
  | 
   nd.sr_vpsa [0] = sqrt(nd.sr_vpsa [0]); | 
| 1498 | 
  | 
  | 
   nd.sr_vpsa [1] = sqrt(nd.sr_vpsa [1]); | 
| 1499 | 
  | 
  | 
 | 
| 1500 | 
  | 
  | 
   /* Orient perturbed normal towards incident side */ | 
| 1501 | 
  | 
  | 
   if (!hitFront) {                      | 
| 1502 | 
  | 
  | 
      nd.pnorm [0] = -nd.pnorm [0]; | 
| 1503 | 
  | 
  | 
      nd.pnorm [1] = -nd.pnorm [1]; | 
| 1504 | 
  | 
  | 
      nd.pnorm [2] = -nd.pnorm [2]; | 
| 1505 | 
  | 
  | 
   } | 
| 1506 | 
rschregle | 
2.2 | 
 | 
| 1507 | 
  | 
  | 
   /* Get scatter probabilities (weighted by pattern except for spec refl) | 
| 1508 | 
  | 
  | 
    * prDiff, ptDiff:      extra diffuse component in material def | 
| 1509 | 
  | 
  | 
    * prDiffSD, ptDiffSD:  diffuse (constant) component in SDF | 
| 1510 | 
  | 
  | 
    * prSpecSD, ptSpecSD:  non-diffuse ("specular") component in SDF  | 
| 1511 | 
  | 
  | 
    * albedo:              sum of above, inverse absorption probability */ | 
| 1512 | 
  | 
  | 
   prDiff   = colorAvg(nd.rdiff); | 
| 1513 | 
  | 
  | 
   ptDiff   = colorAvg(nd.tdiff); | 
| 1514 | 
  | 
  | 
   prDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampR, nd.sd); | 
| 1515 | 
  | 
  | 
   ptDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampT, nd.sd); | 
| 1516 | 
  | 
  | 
   prSpecSD = SDdirectHemi(nd.vray, SDsampSp | SDsampR, nd.sd); | 
| 1517 | 
  | 
  | 
   ptSpecSD = patAlb * SDdirectHemi(nd.vray, SDsampSp | SDsampT, nd.sd); | 
| 1518 | 
  | 
  | 
   albedo   = prDiff + ptDiff + prDiffSD + ptDiffSD + prSpecSD + ptSpecSD; | 
| 1519 | 
  | 
  | 
 | 
| 1520 | 
  | 
  | 
   /*     | 
| 1521 | 
  | 
  | 
   if (albedo > 1) | 
| 1522 | 
  | 
  | 
      objerror(mat, WARNING, "Invalid albedo"); | 
| 1523 | 
  | 
  | 
   */ | 
| 1524 | 
  | 
  | 
          | 
| 1525 | 
  | 
  | 
   /* Insert direct and indirect photon hits if diffuse component */ | 
| 1526 | 
  | 
  | 
   if (prDiff + ptDiff + prDiffSD + ptDiffSD > FTINY) | 
| 1527 | 
  | 
  | 
      addPhotons(rayIn);         | 
| 1528 | 
  | 
  | 
 | 
| 1529 | 
greg | 
2.6 | 
   xi = pmapRandom(rouletteState); | 
| 1530 | 
rschregle | 
2.2 | 
       | 
| 1531 | 
  | 
  | 
   if (xi > albedo) | 
| 1532 | 
  | 
  | 
      /* Absorbtion */ | 
| 1533 | 
  | 
  | 
      return 0; | 
| 1534 | 
  | 
  | 
    | 
| 1535 | 
greg | 
2.6 | 
   transmitted = 0; | 
| 1536 | 
  | 
  | 
 | 
| 1537 | 
rschregle | 
2.2 | 
   if ((xi -= prDiff) <= 0) { | 
| 1538 | 
  | 
  | 
      /* Diffuse reflection (extra component in material def) */ | 
| 1539 | 
  | 
  | 
      photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff); | 
| 1540 | 
  | 
  | 
      diffPhotonScatter(nd.pnorm, &rayOut); | 
| 1541 | 
  | 
  | 
   } | 
| 1542 | 
greg | 
2.1 | 
    | 
| 1543 | 
rschregle | 
2.2 | 
   else if ((xi -= ptDiff) <= 0) { | 
| 1544 | 
  | 
  | 
      /* Diffuse transmission (extra component in material def) */ | 
| 1545 | 
  | 
  | 
      flipsurface(rayIn); | 
| 1546 | 
greg | 
2.6 | 
      nd.thick = -nd.thick; | 
| 1547 | 
rschregle | 
2.2 | 
      photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff); | 
| 1548 | 
greg | 
2.6 | 
      diffPhotonScatter(nd.pnorm, &rayOut); | 
| 1549 | 
  | 
  | 
      transmitted = 1; | 
| 1550 | 
rschregle | 
2.2 | 
   } | 
| 1551 | 
greg | 
2.6 | 
   | 
| 1552 | 
rschregle | 
2.2 | 
   else {   /* Sample SDF */ | 
| 1553 | 
  | 
  | 
      if ((xi -= prDiffSD) <= 0) { | 
| 1554 | 
  | 
  | 
         /* Diffuse SDF reflection (constant component) */ | 
| 1555 | 
greg | 
2.6 | 
         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),  | 
| 1556 | 
rschregle | 
2.2 | 
                               SDsampDf | SDsampR, nd.sd))) | 
| 1557 | 
  | 
  | 
            objerror(mat, USER, transSDError(err)); | 
| 1558 | 
  | 
  | 
          | 
| 1559 | 
  | 
  | 
         /* Apply pattern to spectral component */ | 
| 1560 | 
  | 
  | 
         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); | 
| 1561 | 
  | 
  | 
         multcolor(bsdfRGB, rayIn -> pcol); | 
| 1562 | 
  | 
  | 
         photonRay(rayIn, &rayOut, PMAP_DIFFREFL, bsdfRGB); | 
| 1563 | 
greg | 
2.1 | 
      } | 
| 1564 | 
rschregle | 
2.2 | 
 | 
| 1565 | 
  | 
  | 
      else if ((xi -= ptDiffSD) <= 0) { | 
| 1566 | 
  | 
  | 
         /* Diffuse SDF transmission (constant component) */ | 
| 1567 | 
greg | 
2.6 | 
         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),  | 
| 1568 | 
rschregle | 
2.2 | 
                               SDsampDf | SDsampT, nd.sd))) | 
| 1569 | 
  | 
  | 
            objerror(mat, USER, transSDError(err)); | 
| 1570 | 
greg | 
2.1 | 
          | 
| 1571 | 
rschregle | 
2.2 | 
         /* Apply pattern to spectral component */ | 
| 1572 | 
  | 
  | 
         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); | 
| 1573 | 
  | 
  | 
         multcolor(bsdfRGB, rayIn -> pcol); | 
| 1574 | 
  | 
  | 
         addcolor(bsdfRGB, nd.tdiff);       | 
| 1575 | 
  | 
  | 
         flipsurface(rayIn);  /* Necessary? */ | 
| 1576 | 
rschregle | 
2.7 | 
         nd.thick = -nd.thick; | 
| 1577 | 
  | 
  | 
         photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB); | 
| 1578 | 
greg | 
2.6 | 
         transmitted = 1; | 
| 1579 | 
greg | 
2.1 | 
      } | 
| 1580 | 
rschregle | 
2.2 | 
 | 
| 1581 | 
  | 
  | 
      else if ((xi -= prSpecSD) <= 0) { | 
| 1582 | 
  | 
  | 
         /* Non-diffuse ("specular") SDF reflection */ | 
| 1583 | 
greg | 
2.6 | 
         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),  | 
| 1584 | 
rschregle | 
2.2 | 
                               SDsampSp | SDsampR, nd.sd))) | 
| 1585 | 
  | 
  | 
            objerror(mat, USER, transSDError(err)); | 
| 1586 | 
greg | 
2.1 | 
          | 
| 1587 | 
rschregle | 
2.2 | 
         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); | 
| 1588 | 
  | 
  | 
         photonRay(rayIn, &rayOut, PMAP_SPECREFL, bsdfRGB); | 
| 1589 | 
greg | 
2.1 | 
      } | 
| 1590 | 
  | 
  | 
       | 
| 1591 | 
  | 
  | 
      else { | 
| 1592 | 
rschregle | 
2.2 | 
         /* Non-diffuse ("specular") SDF transmission */ | 
| 1593 | 
greg | 
2.6 | 
         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),  | 
| 1594 | 
rschregle | 
2.2 | 
                               SDsampSp | SDsampT, nd.sd))) | 
| 1595 | 
  | 
  | 
            objerror(mat, USER, transSDError(err)); | 
| 1596 | 
greg | 
2.1 | 
 | 
| 1597 | 
rschregle | 
2.2 | 
         /* Apply pattern to spectral component */ | 
| 1598 | 
greg | 
2.1 | 
         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB); | 
| 1599 | 
rschregle | 
2.2 | 
         multcolor(bsdfRGB, rayIn -> pcol); | 
| 1600 | 
  | 
  | 
         flipsurface(rayIn);  /* Necessary? */ | 
| 1601 | 
rschregle | 
2.7 | 
         nd.thick = -nd.thick; | 
| 1602 | 
rschregle | 
2.2 | 
         photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB); | 
| 1603 | 
greg | 
2.6 | 
         transmitted = 1; | 
| 1604 | 
rschregle | 
2.2 | 
      }       | 
| 1605 | 
  | 
  | 
       | 
| 1606 | 
  | 
  | 
      /* Xform outgoing dir to world coords */ | 
| 1607 | 
  | 
  | 
      if ((err = SDmapDir(rayOut.rdir, nd.fromloc, nd.vray))) { | 
| 1608 | 
  | 
  | 
         objerror(mat, USER, transSDError(err)); | 
| 1609 | 
  | 
  | 
         return 0; | 
| 1610 | 
greg | 
2.1 | 
      } | 
| 1611 | 
rschregle | 
2.2 | 
   } | 
| 1612 | 
greg | 
2.1 | 
       | 
| 1613 | 
rschregle | 
2.2 | 
   /* Clean up */ | 
| 1614 | 
greg | 
2.1 | 
   SDfreeCache(nd.sd); | 
| 1615 | 
  | 
  | 
 | 
| 1616 | 
greg | 
2.6 | 
   /* Need to offset ray origin to get past detail geometry? */ | 
| 1617 | 
  | 
  | 
   if (transmitted && nd.thick != 0) | 
| 1618 | 
rschregle | 
2.7 | 
      VSUM(rayOut.rorg, rayOut.rorg, rayIn -> ron, -nd.thick); | 
| 1619 | 
greg | 
2.6 | 
 | 
| 1620 | 
greg | 
2.1 | 
   tracePhoton(&rayOut); | 
| 1621 | 
  | 
  | 
   return 0; | 
| 1622 | 
  | 
  | 
} | 
| 1623 | 
  | 
  | 
 | 
| 1624 | 
  | 
  | 
 | 
| 1625 | 
  | 
  | 
 | 
| 1626 | 
  | 
  | 
static int lightPhotonScatter (OBJREC* mat, RAY* ray) | 
| 1627 | 
  | 
  | 
/* Light sources doan' reflect */ | 
| 1628 | 
  | 
  | 
{ | 
| 1629 | 
  | 
  | 
   return 0; | 
| 1630 | 
  | 
  | 
} | 
| 1631 | 
  | 
  | 
 | 
| 1632 | 
  | 
  | 
 | 
| 1633 | 
  | 
  | 
 | 
| 1634 | 
  | 
  | 
void initPhotonScatterFuncs () | 
| 1635 | 
  | 
  | 
/* Init photonScatter[] dispatch table */ | 
| 1636 | 
  | 
  | 
{ | 
| 1637 | 
  | 
  | 
   int i; | 
| 1638 | 
  | 
  | 
    | 
| 1639 | 
  | 
  | 
   for (i = 0; i < NUMOTYPE; i++)  | 
| 1640 | 
  | 
  | 
      photonScatter [i] = o_default; | 
| 1641 | 
  | 
  | 
       | 
| 1642 | 
  | 
  | 
   photonScatter [MAT_LIGHT] = photonScatter [MAT_ILLUM] = | 
| 1643 | 
  | 
  | 
      photonScatter [MAT_GLOW] = photonScatter [MAT_SPOT] =  | 
| 1644 | 
  | 
  | 
         lightPhotonScatter; | 
| 1645 | 
  | 
  | 
          | 
| 1646 | 
  | 
  | 
   photonScatter [MAT_PLASTIC] = photonScatter [MAT_METAL] = | 
| 1647 | 
  | 
  | 
      photonScatter [MAT_TRANS] = normalPhotonScatter; | 
| 1648 | 
  | 
  | 
       | 
| 1649 | 
  | 
  | 
   photonScatter [MAT_PLASTIC2] = photonScatter [MAT_METAL2] = | 
| 1650 | 
  | 
  | 
      photonScatter [MAT_TRANS2] = anisoPhotonScatter; | 
| 1651 | 
  | 
  | 
       | 
| 1652 | 
  | 
  | 
   photonScatter [MAT_DIELECTRIC] = photonScatter [MAT_INTERFACE] =  | 
| 1653 | 
  | 
  | 
      dielectricPhotonScatter; | 
| 1654 | 
  | 
  | 
       | 
| 1655 | 
  | 
  | 
   photonScatter [MAT_MIST] = mistPhotonScatter; | 
| 1656 | 
  | 
  | 
   photonScatter [MAT_GLASS] = glassPhotonScatter; | 
| 1657 | 
  | 
  | 
   photonScatter [MAT_CLIP] = clipPhotonScatter; | 
| 1658 | 
  | 
  | 
   photonScatter [MAT_MIRROR] = mirrorPhotonScatter; | 
| 1659 | 
  | 
  | 
   photonScatter [MIX_FUNC] = mx_funcPhotonScatter; | 
| 1660 | 
  | 
  | 
   photonScatter [MIX_DATA] = mx_dataPhotonScatter; | 
| 1661 | 
  | 
  | 
   photonScatter [MIX_PICT]= mx_pdataPhotonScatter; | 
| 1662 | 
  | 
  | 
    | 
| 1663 | 
  | 
  | 
   photonScatter [PAT_BDATA] = photonScatter [PAT_CDATA] = | 
| 1664 | 
  | 
  | 
      photonScatter [PAT_BFUNC] = photonScatter [PAT_CFUNC] = | 
| 1665 | 
  | 
  | 
         photonScatter [PAT_CPICT] = photonScatter [TEX_FUNC] =  | 
| 1666 | 
  | 
  | 
            photonScatter [TEX_DATA] = pattexPhotonScatter; | 
| 1667 | 
  | 
  | 
             | 
| 1668 | 
  | 
  | 
   photonScatter [MOD_ALIAS] = aliasPhotonScatter; | 
| 1669 | 
  | 
  | 
   photonScatter [MAT_BSDF] = bsdfPhotonScatter; | 
| 1670 | 
  | 
  | 
} |