| 1 | greg | 2.1 | #ifndef lint | 
| 2 | greg | 2.55 | static const char RCSid[] = "$Id: m_bsdf.c,v 2.54 2018/08/08 04:15:18 greg Exp $"; | 
| 3 | greg | 2.1 | #endif | 
| 4 |  |  | /* | 
| 5 |  |  | *  Shading for materials with BSDFs taken from XML data files | 
| 6 |  |  | */ | 
| 7 |  |  |  | 
| 8 |  |  | #include "copyright.h" | 
| 9 |  |  |  | 
| 10 |  |  | #include  "ray.h" | 
| 11 | greg | 2.50 | #include  "otypes.h" | 
| 12 | greg | 2.1 | #include  "ambient.h" | 
| 13 |  |  | #include  "source.h" | 
| 14 |  |  | #include  "func.h" | 
| 15 |  |  | #include  "bsdf.h" | 
| 16 |  |  | #include  "random.h" | 
| 17 | greg | 2.30 | #include  "pmapmat.h" | 
| 18 | greg | 2.1 |  | 
| 19 |  |  | /* | 
| 20 | greg | 2.50 | *     Arguments to this material include optional diffuse colors. | 
| 21 | greg | 2.1 | *  String arguments include the BSDF and function files. | 
| 22 | greg | 2.50 | *      For the MAT_BSDF type, a non-zero thickness causes the useful behavior | 
| 23 | greg | 2.5 | *  of translating transmitted rays this distance beneath the surface | 
| 24 |  |  | *  (opposite the surface normal) to bypass any intervening geometry. | 
| 25 |  |  | *  Translation only affects scattered, non-source-directed samples. | 
| 26 |  |  | *  A non-zero thickness has the further side-effect that an unscattered | 
| 27 | greg | 2.35 | *  (view) ray will pass right through our material, making the BSDF | 
| 28 |  |  | *  surface invisible and showing the proxied geometry instead. Thickness | 
| 29 |  |  | *  has the further effect of turning off reflection on the reverse side so | 
| 30 |  |  | *  rays heading in the opposite direction pass unimpeded through the BSDF | 
| 31 | greg | 2.5 | *  surface.  A paired surface may be placed on the opposide side of | 
| 32 |  |  | *  the detail geometry, less than this thickness away, if a two-way | 
| 33 |  |  | *  proxy is desired.  Note that the sign of the thickness is important. | 
| 34 |  |  | *  A positive thickness hides geometry behind the BSDF surface and uses | 
| 35 |  |  | *  front reflectance and transmission properties.  A negative thickness | 
| 36 |  |  | *  hides geometry in front of the surface when rays hit from behind, | 
| 37 |  |  | *  and applies only the transmission and backside reflectance properties. | 
| 38 |  |  | *  Reflection is ignored on the hidden side, as those rays pass through. | 
| 39 | greg | 2.52 | *      For the MAT_ABSDF type, we check for a strong "through" component. | 
| 40 | greg | 2.50 | *  Such a component will cause direct rays to pass through unscattered. | 
| 41 | greg | 2.40 | *  A separate test prevents over-counting by dropping samples that are | 
| 42 |  |  | *  too close to this "through" direction.  BSDFs with such a through direction | 
| 43 |  |  | *  will also have a view component, meaning they are somewhat see-through. | 
| 44 | greg | 2.52 | *  A MAT_BSDF type with zero thickness behaves the same as a MAT_ABSDF | 
| 45 | greg | 2.50 | *  type with no strong through component. | 
| 46 | greg | 2.1 | *      The "up" vector for the BSDF is given by three variables, defined | 
| 47 |  |  | *  (along with the thickness) by the named function file, or '.' if none. | 
| 48 |  |  | *  Together with the surface normal, this defines the local coordinate | 
| 49 |  |  | *  system for the BSDF. | 
| 50 |  |  | *      We do not reorient the surface, so if the BSDF has no back-side | 
| 51 | greg | 2.5 | *  reflectance and none is given in the real arguments, a BSDF surface | 
| 52 |  |  | *  with zero thickness will appear black when viewed from behind | 
| 53 | greg | 2.35 | *  unless backface visibility is on, when it becomes invisible. | 
| 54 | greg | 2.5 | *      The diffuse arguments are added to components in the BSDF file, | 
| 55 | greg | 2.1 | *  not multiplied.  However, patterns affect this material as a multiplier | 
| 56 |  |  | *  on everything except non-diffuse reflection. | 
| 57 |  |  | * | 
| 58 | greg | 2.52 | *  Arguments for MAT_ABSDF are: | 
| 59 | greg | 2.50 | *      5+      BSDFfile        ux uy uz        funcfile        transform | 
| 60 |  |  | *      0 | 
| 61 |  |  | *      0|3|6|9 rdf     gdf     bdf | 
| 62 |  |  | *              rdb     gdb     bdb | 
| 63 |  |  | *              rdt     gdt     bdt | 
| 64 |  |  | * | 
| 65 | greg | 2.1 | *  Arguments for MAT_BSDF are: | 
| 66 |  |  | *      6+      thick   BSDFfile        ux uy uz        funcfile        transform | 
| 67 |  |  | *      0 | 
| 68 | greg | 2.8 | *      0|3|6|9 rdf     gdf     bdf | 
| 69 | greg | 2.1 | *              rdb     gdb     bdb | 
| 70 |  |  | *              rdt     gdt     bdt | 
| 71 |  |  | */ | 
| 72 |  |  |  | 
| 73 | greg | 2.4 | /* | 
| 74 |  |  | * Note that our reverse ray-tracing process means that the positions | 
| 75 |  |  | * of incoming and outgoing vectors may be reversed in our calls | 
| 76 | greg | 2.35 | * to the BSDF library.  This is usually fine, since the bidirectional nature | 
| 77 | greg | 2.4 | * of the BSDF (that's what the 'B' stands for) means it all works out. | 
| 78 |  |  | */ | 
| 79 |  |  |  | 
| 80 | greg | 2.1 | typedef struct { | 
| 81 |  |  | OBJREC  *mp;            /* material pointer */ | 
| 82 |  |  | RAY     *pr;            /* intersected ray */ | 
| 83 |  |  | FVECT   pnorm;          /* perturbed surface normal */ | 
| 84 | greg | 2.4 | FVECT   vray;           /* local outgoing (return) vector */ | 
| 85 | greg | 2.9 | double  sr_vpsa[2];     /* sqrt of BSDF projected solid angle extrema */ | 
| 86 | greg | 2.1 | RREAL   toloc[3][3];    /* world to local BSDF coords */ | 
| 87 |  |  | RREAL   fromloc[3][3];  /* local BSDF coords to world */ | 
| 88 |  |  | double  thick;          /* surface thickness */ | 
| 89 | greg | 2.52 | COLOR   cthru;          /* "through" component for MAT_ABSDF */ | 
| 90 | greg | 2.1 | SDData  *sd;            /* loaded BSDF data */ | 
| 91 | greg | 2.31 | COLOR   rdiff;          /* diffuse reflection */ | 
| 92 | greg | 2.39 | COLOR   runsamp;        /* BSDF hemispherical reflection */ | 
| 93 | greg | 2.31 | COLOR   tdiff;          /* diffuse transmission */ | 
| 94 | greg | 2.39 | COLOR   tunsamp;        /* BSDF hemispherical transmission */ | 
| 95 | greg | 2.1 | }  BSDFDAT;             /* BSDF material data */ | 
| 96 |  |  |  | 
| 97 |  |  | #define cvt_sdcolor(cv, svp)    ccy2rgb(&(svp)->spec, (svp)->cieY, cv) | 
| 98 |  |  |  | 
| 99 | greg | 2.52 | /* Compute "through" component color for MAT_ABSDF */ | 
| 100 | greg | 2.34 | static void | 
| 101 |  |  | compute_through(BSDFDAT *ndp) | 
| 102 |  |  | { | 
| 103 |  |  | #define NDIR2CHECK      13 | 
| 104 |  |  | static const float      dir2check[NDIR2CHECK][2] = { | 
| 105 |  |  | {0, 0}, | 
| 106 |  |  | {-0.8, 0}, | 
| 107 |  |  | {0, 0.8}, | 
| 108 |  |  | {0, -0.8}, | 
| 109 |  |  | {0.8, 0}, | 
| 110 |  |  | {-0.8, 0.8}, | 
| 111 |  |  | {-0.8, -0.8}, | 
| 112 |  |  | {0.8, 0.8}, | 
| 113 |  |  | {0.8, -0.8}, | 
| 114 |  |  | {-1.6, 0}, | 
| 115 |  |  | {0, 1.6}, | 
| 116 |  |  | {0, -1.6}, | 
| 117 |  |  | {1.6, 0}, | 
| 118 |  |  | }; | 
| 119 | greg | 2.45 | const double    peak_over = 1.5; | 
| 120 | greg | 2.34 | SDSpectralDF    *dfp; | 
| 121 |  |  | FVECT           pdir; | 
| 122 |  |  | double          tomega, srchrad; | 
| 123 |  |  | COLOR           vpeak, vsum; | 
| 124 | greg | 2.40 | int             i; | 
| 125 | greg | 2.34 | SDError         ec; | 
| 126 |  |  |  | 
| 127 |  |  | if (ndp->pr->rod > 0) | 
| 128 |  |  | dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; | 
| 129 |  |  | else | 
| 130 |  |  | dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; | 
| 131 |  |  |  | 
| 132 |  |  | if (dfp == NULL) | 
| 133 |  |  | return;                         /* no specular transmission */ | 
| 134 |  |  | if (bright(ndp->pr->pcol) <= FTINY) | 
| 135 |  |  | return;                         /* pattern is black, here */ | 
| 136 |  |  | srchrad = sqrt(dfp->minProjSA);         /* else search for peak */ | 
| 137 | greg | 2.40 | setcolor(vpeak, 0, 0, 0); | 
| 138 |  |  | setcolor(vsum, 0, 0, 0); | 
| 139 | greg | 2.42 | pdir[2] = 0.0; | 
| 140 | greg | 2.34 | for (i = 0; i < NDIR2CHECK; i++) { | 
| 141 |  |  | FVECT   tdir; | 
| 142 |  |  | SDValue sv; | 
| 143 |  |  | COLOR   vcol; | 
| 144 |  |  | tdir[0] = -ndp->vray[0] + dir2check[i][0]*srchrad; | 
| 145 |  |  | tdir[1] = -ndp->vray[1] + dir2check[i][1]*srchrad; | 
| 146 |  |  | tdir[2] = -ndp->vray[2]; | 
| 147 | greg | 2.36 | normalize(tdir); | 
| 148 | greg | 2.34 | ec = SDevalBSDF(&sv, tdir, ndp->vray, ndp->sd); | 
| 149 |  |  | if (ec) | 
| 150 |  |  | goto baderror; | 
| 151 |  |  | cvt_sdcolor(vcol, &sv); | 
| 152 |  |  | addcolor(vsum, vcol); | 
| 153 | greg | 2.45 | if (sv.cieY > bright(vpeak)) { | 
| 154 | greg | 2.34 | copycolor(vpeak, vcol); | 
| 155 |  |  | VCOPY(pdir, tdir); | 
| 156 |  |  | } | 
| 157 |  |  | } | 
| 158 | greg | 2.42 | if (pdir[2] == 0.0) | 
| 159 |  |  | return;                         /* zero neighborhood */ | 
| 160 | greg | 2.34 | ec = SDsizeBSDF(&tomega, pdir, ndp->vray, SDqueryMin, ndp->sd); | 
| 161 |  |  | if (ec) | 
| 162 |  |  | goto baderror; | 
| 163 |  |  | if (tomega > 1.5*dfp->minProjSA) | 
| 164 |  |  | return;                         /* not really a peak? */ | 
| 165 | greg | 2.40 | if ((bright(vpeak) - ndp->sd->tLamb.cieY*(1./PI))*tomega <= .001) | 
| 166 |  |  | return;                         /* < 0.1% transmission */ | 
| 167 | greg | 2.34 | for (i = 3; i--; )                      /* remove peak from average */ | 
| 168 |  |  | colval(vsum,i) -= colval(vpeak,i); | 
| 169 | greg | 2.40 | if (peak_over*bright(vsum) >= (NDIR2CHECK-1)*bright(vpeak)) | 
| 170 | greg | 2.34 | return;                         /* not peaky enough */ | 
| 171 |  |  | copycolor(ndp->cthru, vpeak);           /* else use it */ | 
| 172 |  |  | scalecolor(ndp->cthru, tomega); | 
| 173 |  |  | multcolor(ndp->cthru, ndp->pr->pcol);   /* modify by pattern */ | 
| 174 |  |  | return; | 
| 175 |  |  | baderror: | 
| 176 |  |  | objerror(ndp->mp, USER, transSDError(ec)); | 
| 177 |  |  | #undef NDIR2CHECK | 
| 178 |  |  | } | 
| 179 |  |  |  | 
| 180 | greg | 2.4 | /* Jitter ray sample according to projected solid angle and specjitter */ | 
| 181 |  |  | static void | 
| 182 | greg | 2.15 | bsdf_jitter(FVECT vres, BSDFDAT *ndp, double sr_psa) | 
| 183 | greg | 2.4 | { | 
| 184 |  |  | VCOPY(vres, ndp->vray); | 
| 185 |  |  | if (specjitter < 1.) | 
| 186 |  |  | sr_psa *= specjitter; | 
| 187 |  |  | if (sr_psa <= FTINY) | 
| 188 |  |  | return; | 
| 189 |  |  | vres[0] += sr_psa*(.5 - frandom()); | 
| 190 |  |  | vres[1] += sr_psa*(.5 - frandom()); | 
| 191 |  |  | normalize(vres); | 
| 192 |  |  | } | 
| 193 |  |  |  | 
| 194 | greg | 2.33 | /* Get BSDF specular for direct component, returning true if OK to proceed */ | 
| 195 | greg | 2.7 | static int | 
| 196 | greg | 2.33 | direct_specular_OK(COLOR cval, FVECT ldir, double omega, BSDFDAT *ndp) | 
| 197 | greg | 2.7 | { | 
| 198 | greg | 2.43 | int     nsamp; | 
| 199 |  |  | double  wtot = 0; | 
| 200 | greg | 2.13 | FVECT   vsrc, vsmp, vjit; | 
| 201 | greg | 2.36 | double  tomega, tomega2; | 
| 202 | greg | 2.15 | double  sf, tsr, sd[2]; | 
| 203 | greg | 2.32 | COLOR   csmp, cdiff; | 
| 204 |  |  | double  diffY; | 
| 205 | greg | 2.7 | SDValue sv; | 
| 206 |  |  | SDError ec; | 
| 207 | greg | 2.13 | int     i; | 
| 208 | greg | 2.37 | /* in case we fail */ | 
| 209 | greg | 2.40 | setcolor(cval,  0, 0, 0); | 
| 210 | greg | 2.7 | /* transform source direction */ | 
| 211 |  |  | if (SDmapDir(vsrc, ndp->toloc, ldir) != SDEnone) | 
| 212 |  |  | return(0); | 
| 213 | greg | 2.32 | /* will discount diffuse portion */ | 
| 214 |  |  | switch ((vsrc[2] > 0)<<1 | (ndp->vray[2] > 0)) { | 
| 215 |  |  | case 3: | 
| 216 |  |  | if (ndp->sd->rf == NULL) | 
| 217 |  |  | return(0);      /* all diffuse */ | 
| 218 |  |  | sv = ndp->sd->rLambFront; | 
| 219 |  |  | break; | 
| 220 |  |  | case 0: | 
| 221 |  |  | if (ndp->sd->rb == NULL) | 
| 222 |  |  | return(0);      /* all diffuse */ | 
| 223 |  |  | sv = ndp->sd->rLambBack; | 
| 224 |  |  | break; | 
| 225 |  |  | default: | 
| 226 |  |  | if ((ndp->sd->tf == NULL) & (ndp->sd->tb == NULL)) | 
| 227 |  |  | return(0);      /* all diffuse */ | 
| 228 |  |  | sv = ndp->sd->tLamb; | 
| 229 |  |  | break; | 
| 230 |  |  | } | 
| 231 | greg | 2.33 | if (sv.cieY > FTINY) { | 
| 232 |  |  | diffY = sv.cieY *= 1./PI; | 
| 233 | greg | 2.32 | cvt_sdcolor(cdiff, &sv); | 
| 234 |  |  | } else { | 
| 235 | greg | 2.40 | diffY = 0; | 
| 236 |  |  | setcolor(cdiff,  0, 0, 0); | 
| 237 | greg | 2.32 | } | 
| 238 | greg | 2.53 | /* need projected solid angle */ | 
| 239 | greg | 2.37 | omega *= fabs(vsrc[2]); | 
| 240 | greg | 2.13 | /* check indirect over-counting */ | 
| 241 | greg | 2.40 | if ((vsrc[2] > 0) ^ (ndp->vray[2] > 0) && bright(ndp->cthru) > FTINY) { | 
| 242 | greg | 2.53 | double          dx = vsrc[0] + ndp->vray[0]; | 
| 243 |  |  | double          dy = vsrc[1] + ndp->vray[1]; | 
| 244 |  |  | SDSpectralDF    *dfp = (ndp->pr->rod > 0) ? | 
| 245 |  |  | ((ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb) : | 
| 246 |  |  | ((ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf) ; | 
| 247 |  |  |  | 
| 248 |  |  | if (dx*dx + dy*dy <= (2.5*4./PI)*(omega + dfp->minProjSA + | 
| 249 |  |  | 2.*sqrt(omega*dfp->minProjSA))) | 
| 250 | greg | 2.7 | return(0); | 
| 251 |  |  | } | 
| 252 | greg | 2.53 | ec = SDsizeBSDF(&tomega, ndp->vray, vsrc, SDqueryMin, ndp->sd); | 
| 253 |  |  | if (ec) | 
| 254 |  |  | goto baderror; | 
| 255 | greg | 2.37 | /* assign number of samples */ | 
| 256 | greg | 2.15 | sf = specjitter * ndp->pr->rweight; | 
| 257 | greg | 2.40 | if (tomega <= 0) | 
| 258 | greg | 2.24 | nsamp = 1; | 
| 259 |  |  | else if (25.*tomega <= omega) | 
| 260 | greg | 2.15 | nsamp = 100.*sf + .5; | 
| 261 |  |  | else | 
| 262 |  |  | nsamp = 4.*sf*omega/tomega + .5; | 
| 263 |  |  | nsamp += !nsamp; | 
| 264 | greg | 2.37 | sf = sqrt(omega);               /* sample our source area */ | 
| 265 | greg | 2.15 | tsr = sqrt(tomega); | 
| 266 | greg | 2.13 | for (i = nsamp; i--; ) { | 
| 267 |  |  | VCOPY(vsmp, vsrc);      /* jitter query directions */ | 
| 268 |  |  | if (nsamp > 1) { | 
| 269 |  |  | multisamp(sd, 2, (i + frandom())/(double)nsamp); | 
| 270 |  |  | vsmp[0] += (sd[0] - .5)*sf; | 
| 271 |  |  | vsmp[1] += (sd[1] - .5)*sf; | 
| 272 | greg | 2.36 | normalize(vsmp); | 
| 273 | greg | 2.13 | } | 
| 274 | greg | 2.15 | bsdf_jitter(vjit, ndp, tsr); | 
| 275 | greg | 2.37 | /* compute BSDF */ | 
| 276 |  |  | ec = SDevalBSDF(&sv, vjit, vsmp, ndp->sd); | 
| 277 |  |  | if (ec) | 
| 278 |  |  | goto baderror; | 
| 279 |  |  | if (sv.cieY - diffY <= FTINY) | 
| 280 |  |  | continue;       /* no specular part */ | 
| 281 | greg | 2.36 | /* check for variable resolution */ | 
| 282 |  |  | ec = SDsizeBSDF(&tomega2, vjit, vsmp, SDqueryMin, ndp->sd); | 
| 283 |  |  | if (ec) | 
| 284 |  |  | goto baderror; | 
| 285 |  |  | if (tomega2 < .12*tomega) | 
| 286 |  |  | continue;       /* not safe to include */ | 
| 287 | greg | 2.13 | cvt_sdcolor(csmp, &sv); | 
| 288 | greg | 2.44 |  | 
| 289 |  |  | if (sf < 2.5*tsr) {     /* weight by Y for small sources */ | 
| 290 |  |  | scalecolor(csmp, sv.cieY); | 
| 291 |  |  | wtot += sv.cieY; | 
| 292 |  |  | } else | 
| 293 |  |  | wtot += 1.; | 
| 294 | greg | 2.43 | addcolor(cval, csmp); | 
| 295 | greg | 2.13 | } | 
| 296 | greg | 2.43 | if (wtot <= FTINY)              /* no valid specular samples? */ | 
| 297 | greg | 2.37 | return(0); | 
| 298 |  |  |  | 
| 299 | greg | 2.43 | sf = 1./wtot;                   /* weighted average BSDF */ | 
| 300 | greg | 2.13 | scalecolor(cval, sf); | 
| 301 | greg | 2.32 | /* subtract diffuse contribution */ | 
| 302 |  |  | for (i = 3*(diffY > FTINY); i--; ) | 
| 303 | greg | 2.40 | if ((colval(cval,i) -= colval(cdiff,i)) < 0) | 
| 304 |  |  | colval(cval,i) = 0; | 
| 305 | greg | 2.32 | return(1); | 
| 306 | greg | 2.13 | baderror: | 
| 307 |  |  | objerror(ndp->mp, USER, transSDError(ec)); | 
| 308 | greg | 2.17 | return(0);                      /* gratis return */ | 
| 309 | greg | 2.7 | } | 
| 310 |  |  |  | 
| 311 | greg | 2.5 | /* Compute source contribution for BSDF (reflected & transmitted) */ | 
| 312 | greg | 2.1 | static void | 
| 313 | greg | 2.5 | dir_bsdf( | 
| 314 | greg | 2.1 | COLOR  cval,                    /* returned coefficient */ | 
| 315 |  |  | void  *nnp,                     /* material data */ | 
| 316 |  |  | FVECT  ldir,                    /* light source direction */ | 
| 317 |  |  | double  omega                   /* light source size */ | 
| 318 |  |  | ) | 
| 319 |  |  | { | 
| 320 | greg | 2.3 | BSDFDAT         *np = (BSDFDAT *)nnp; | 
| 321 | greg | 2.1 | double          ldot; | 
| 322 |  |  | double          dtmp; | 
| 323 |  |  | COLOR           ctmp; | 
| 324 |  |  |  | 
| 325 | greg | 2.40 | setcolor(cval,  0, 0, 0); | 
| 326 | greg | 2.1 |  | 
| 327 |  |  | ldot = DOT(np->pnorm, ldir); | 
| 328 |  |  | if ((-FTINY <= ldot) & (ldot <= FTINY)) | 
| 329 |  |  | return; | 
| 330 |  |  |  | 
| 331 | greg | 2.9 | if (ldot > 0 && bright(np->rdiff) > FTINY) { | 
| 332 | greg | 2.1 | /* | 
| 333 | greg | 2.39 | *  Compute diffuse reflected component | 
| 334 | greg | 2.1 | */ | 
| 335 |  |  | copycolor(ctmp, np->rdiff); | 
| 336 |  |  | dtmp = ldot * omega * (1./PI); | 
| 337 |  |  | scalecolor(ctmp, dtmp); | 
| 338 |  |  | addcolor(cval, ctmp); | 
| 339 |  |  | } | 
| 340 | greg | 2.9 | if (ldot < 0 && bright(np->tdiff) > FTINY) { | 
| 341 | greg | 2.1 | /* | 
| 342 | greg | 2.39 | *  Compute diffuse transmission | 
| 343 | greg | 2.1 | */ | 
| 344 |  |  | copycolor(ctmp, np->tdiff); | 
| 345 |  |  | dtmp = -ldot * omega * (1.0/PI); | 
| 346 |  |  | scalecolor(ctmp, dtmp); | 
| 347 |  |  | addcolor(cval, ctmp); | 
| 348 |  |  | } | 
| 349 | greg | 2.30 | if (ambRayInPmap(np->pr)) | 
| 350 |  |  | return;         /* specular already in photon map */ | 
| 351 | greg | 2.1 | /* | 
| 352 | greg | 2.39 | *  Compute specular scattering coefficient using BSDF | 
| 353 | greg | 2.1 | */ | 
| 354 | greg | 2.33 | if (!direct_specular_OK(ctmp, ldir, omega, np)) | 
| 355 | greg | 2.1 | return; | 
| 356 | greg | 2.31 | if (ldot < 0) {         /* pattern for specular transmission */ | 
| 357 | greg | 2.1 | multcolor(ctmp, np->pr->pcol); | 
| 358 |  |  | dtmp = -ldot * omega; | 
| 359 | greg | 2.31 | } else | 
| 360 |  |  | dtmp = ldot * omega; | 
| 361 | greg | 2.1 | scalecolor(ctmp, dtmp); | 
| 362 |  |  | addcolor(cval, ctmp); | 
| 363 |  |  | } | 
| 364 |  |  |  | 
| 365 | greg | 2.5 | /* Compute source contribution for BSDF (reflected only) */ | 
| 366 |  |  | static void | 
| 367 |  |  | dir_brdf( | 
| 368 |  |  | COLOR  cval,                    /* returned coefficient */ | 
| 369 |  |  | void  *nnp,                     /* material data */ | 
| 370 |  |  | FVECT  ldir,                    /* light source direction */ | 
| 371 |  |  | double  omega                   /* light source size */ | 
| 372 |  |  | ) | 
| 373 |  |  | { | 
| 374 |  |  | BSDFDAT         *np = (BSDFDAT *)nnp; | 
| 375 |  |  | double          ldot; | 
| 376 |  |  | double          dtmp; | 
| 377 |  |  | COLOR           ctmp, ctmp1, ctmp2; | 
| 378 |  |  |  | 
| 379 | greg | 2.40 | setcolor(cval,  0, 0, 0); | 
| 380 | greg | 2.5 |  | 
| 381 |  |  | ldot = DOT(np->pnorm, ldir); | 
| 382 |  |  |  | 
| 383 |  |  | if (ldot <= FTINY) | 
| 384 |  |  | return; | 
| 385 |  |  |  | 
| 386 |  |  | if (bright(np->rdiff) > FTINY) { | 
| 387 |  |  | /* | 
| 388 | greg | 2.39 | *  Compute diffuse reflected component | 
| 389 | greg | 2.5 | */ | 
| 390 |  |  | copycolor(ctmp, np->rdiff); | 
| 391 |  |  | dtmp = ldot * omega * (1./PI); | 
| 392 |  |  | scalecolor(ctmp, dtmp); | 
| 393 |  |  | addcolor(cval, ctmp); | 
| 394 |  |  | } | 
| 395 | greg | 2.30 | if (ambRayInPmap(np->pr)) | 
| 396 |  |  | return;         /* specular already in photon map */ | 
| 397 | greg | 2.5 | /* | 
| 398 | greg | 2.39 | *  Compute specular reflection coefficient using BSDF | 
| 399 | greg | 2.5 | */ | 
| 400 | greg | 2.33 | if (!direct_specular_OK(ctmp, ldir, omega, np)) | 
| 401 | greg | 2.5 | return; | 
| 402 |  |  | dtmp = ldot * omega; | 
| 403 |  |  | scalecolor(ctmp, dtmp); | 
| 404 |  |  | addcolor(cval, ctmp); | 
| 405 |  |  | } | 
| 406 |  |  |  | 
| 407 |  |  | /* Compute source contribution for BSDF (transmitted only) */ | 
| 408 |  |  | static void | 
| 409 |  |  | dir_btdf( | 
| 410 |  |  | COLOR  cval,                    /* returned coefficient */ | 
| 411 |  |  | void  *nnp,                     /* material data */ | 
| 412 |  |  | FVECT  ldir,                    /* light source direction */ | 
| 413 |  |  | double  omega                   /* light source size */ | 
| 414 |  |  | ) | 
| 415 |  |  | { | 
| 416 |  |  | BSDFDAT         *np = (BSDFDAT *)nnp; | 
| 417 |  |  | double          ldot; | 
| 418 |  |  | double          dtmp; | 
| 419 |  |  | COLOR           ctmp; | 
| 420 |  |  |  | 
| 421 | greg | 2.40 | setcolor(cval,  0, 0, 0); | 
| 422 | greg | 2.5 |  | 
| 423 |  |  | ldot = DOT(np->pnorm, ldir); | 
| 424 |  |  |  | 
| 425 |  |  | if (ldot >= -FTINY) | 
| 426 |  |  | return; | 
| 427 |  |  |  | 
| 428 |  |  | if (bright(np->tdiff) > FTINY) { | 
| 429 |  |  | /* | 
| 430 | greg | 2.39 | *  Compute diffuse transmission | 
| 431 | greg | 2.5 | */ | 
| 432 |  |  | copycolor(ctmp, np->tdiff); | 
| 433 |  |  | dtmp = -ldot * omega * (1.0/PI); | 
| 434 |  |  | scalecolor(ctmp, dtmp); | 
| 435 |  |  | addcolor(cval, ctmp); | 
| 436 |  |  | } | 
| 437 | greg | 2.30 | if (ambRayInPmap(np->pr)) | 
| 438 |  |  | return;         /* specular already in photon map */ | 
| 439 | greg | 2.5 | /* | 
| 440 | greg | 2.39 | *  Compute specular scattering coefficient using BSDF | 
| 441 | greg | 2.5 | */ | 
| 442 | greg | 2.33 | if (!direct_specular_OK(ctmp, ldir, omega, np)) | 
| 443 | greg | 2.5 | return; | 
| 444 |  |  | /* full pattern on transmission */ | 
| 445 |  |  | multcolor(ctmp, np->pr->pcol); | 
| 446 |  |  | dtmp = -ldot * omega; | 
| 447 |  |  | scalecolor(ctmp, dtmp); | 
| 448 |  |  | addcolor(cval, ctmp); | 
| 449 |  |  | } | 
| 450 |  |  |  | 
| 451 | greg | 2.1 | /* Sample separate BSDF component */ | 
| 452 |  |  | static int | 
| 453 | greg | 2.40 | sample_sdcomp(BSDFDAT *ndp, SDComponent *dcp, int xmit) | 
| 454 | greg | 2.1 | { | 
| 455 | greg | 2.47 | const int       hasthru = (xmit && | 
| 456 |  |  | !(ndp->pr->crtype & (SPECULAR|AMBIENT)) | 
| 457 |  |  | && bright(ndp->cthru) > FTINY); | 
| 458 | greg | 2.41 | int             nstarget = 1; | 
| 459 |  |  | int             nsent = 0; | 
| 460 |  |  | int             n; | 
| 461 |  |  | SDError         ec; | 
| 462 |  |  | SDValue         bsv; | 
| 463 |  |  | double          xrand; | 
| 464 |  |  | FVECT           vsmp, vinc; | 
| 465 |  |  | RAY             sr; | 
| 466 | greg | 2.1 | /* multiple samples? */ | 
| 467 |  |  | if (specjitter > 1.5) { | 
| 468 |  |  | nstarget = specjitter*ndp->pr->rweight + .5; | 
| 469 | greg | 2.14 | nstarget += !nstarget; | 
| 470 | greg | 2.1 | } | 
| 471 | greg | 2.11 | /* run through our samples */ | 
| 472 | greg | 2.40 | for (n = 0; n < nstarget; n++) { | 
| 473 | greg | 2.15 | if (nstarget == 1) {            /* stratify random variable */ | 
| 474 | greg | 2.11 | xrand = urand(ilhash(dimlist,ndims)+samplendx); | 
| 475 | greg | 2.15 | if (specjitter < 1.) | 
| 476 |  |  | xrand = .5 + specjitter*(xrand-.5); | 
| 477 |  |  | } else { | 
| 478 | greg | 2.40 | xrand = (n + frandom())/(double)nstarget; | 
| 479 | greg | 2.15 | } | 
| 480 | greg | 2.11 | SDerrorDetail[0] = '\0';        /* sample direction & coef. */ | 
| 481 | greg | 2.15 | bsdf_jitter(vsmp, ndp, ndp->sr_vpsa[0]); | 
| 482 | greg | 2.40 | VCOPY(vinc, vsmp);              /* to compare after */ | 
| 483 | greg | 2.11 | ec = SDsampComponent(&bsv, vsmp, xrand, dcp); | 
| 484 | greg | 2.1 | if (ec) | 
| 485 | greg | 2.2 | objerror(ndp->mp, USER, transSDError(ec)); | 
| 486 | greg | 2.11 | if (bsv.cieY <= FTINY)          /* zero component? */ | 
| 487 | greg | 2.1 | break; | 
| 488 | greg | 2.40 | if (hasthru) {                  /* check for view ray */ | 
| 489 |  |  | double  dx = vinc[0] + vsmp[0]; | 
| 490 |  |  | double  dy = vinc[1] + vsmp[1]; | 
| 491 |  |  | if (dx*dx + dy*dy <= ndp->sr_vpsa[0]*ndp->sr_vpsa[0]) | 
| 492 |  |  | continue;       /* exclude view sample */ | 
| 493 |  |  | } | 
| 494 |  |  | /* map non-view sample->world */ | 
| 495 | greg | 2.4 | if (SDmapDir(sr.rdir, ndp->fromloc, vsmp) != SDEnone) | 
| 496 | greg | 2.1 | break; | 
| 497 |  |  | /* spawn a specular ray */ | 
| 498 |  |  | if (nstarget > 1) | 
| 499 |  |  | bsv.cieY /= (double)nstarget; | 
| 500 | greg | 2.11 | cvt_sdcolor(sr.rcoef, &bsv);    /* use sample color */ | 
| 501 | greg | 2.40 | if (xmit)                       /* apply pattern on transmit */ | 
| 502 | greg | 2.1 | multcolor(sr.rcoef, ndp->pr->pcol); | 
| 503 |  |  | if (rayorigin(&sr, SPECULAR, ndp->pr, sr.rcoef) < 0) { | 
| 504 | greg | 2.48 | if (!n & (nstarget > 1)) { | 
| 505 | greg | 2.49 | n = nstarget;   /* avoid infinitue loop */ | 
| 506 | greg | 2.48 | nstarget = nstarget*sr.rweight/minweight; | 
| 507 | greg | 2.49 | if (n == nstarget) break; | 
| 508 | greg | 2.48 | n = -1;         /* moved target */ | 
| 509 |  |  | } | 
| 510 |  |  | continue;               /* try again */ | 
| 511 | greg | 2.1 | } | 
| 512 | greg | 2.40 | if (xmit && ndp->thick != 0)    /* need to offset origin? */ | 
| 513 | greg | 2.5 | VSUM(sr.rorg, sr.rorg, ndp->pr->ron, -ndp->thick); | 
| 514 | greg | 2.1 | rayvalue(&sr);                  /* send & evaluate sample */ | 
| 515 |  |  | multcolor(sr.rcol, sr.rcoef); | 
| 516 |  |  | addcolor(ndp->pr->rcol, sr.rcol); | 
| 517 | greg | 2.40 | ++nsent; | 
| 518 | greg | 2.1 | } | 
| 519 |  |  | return(nsent); | 
| 520 |  |  | } | 
| 521 |  |  |  | 
| 522 |  |  | /* Sample non-diffuse components of BSDF */ | 
| 523 |  |  | static int | 
| 524 |  |  | sample_sdf(BSDFDAT *ndp, int sflags) | 
| 525 |  |  | { | 
| 526 | greg | 2.46 | int             hasthru = (sflags == SDsampSpT && | 
| 527 | greg | 2.47 | !(ndp->pr->crtype & (SPECULAR|AMBIENT)) | 
| 528 |  |  | && bright(ndp->cthru) > FTINY); | 
| 529 | greg | 2.1 | int             n, ntotal = 0; | 
| 530 | greg | 2.40 | double          b = 0; | 
| 531 | greg | 2.1 | SDSpectralDF    *dfp; | 
| 532 |  |  | COLORV          *unsc; | 
| 533 |  |  |  | 
| 534 |  |  | if (sflags == SDsampSpT) { | 
| 535 | greg | 2.39 | unsc = ndp->tunsamp; | 
| 536 | greg | 2.22 | if (ndp->pr->rod > 0) | 
| 537 |  |  | dfp = (ndp->sd->tf != NULL) ? ndp->sd->tf : ndp->sd->tb; | 
| 538 |  |  | else | 
| 539 |  |  | dfp = (ndp->sd->tb != NULL) ? ndp->sd->tb : ndp->sd->tf; | 
| 540 | greg | 2.1 | } else /* sflags == SDsampSpR */ { | 
| 541 | greg | 2.39 | unsc = ndp->runsamp; | 
| 542 | greg | 2.31 | if (ndp->pr->rod > 0) | 
| 543 | greg | 2.1 | dfp = ndp->sd->rf; | 
| 544 | greg | 2.31 | else | 
| 545 | greg | 2.1 | dfp = ndp->sd->rb; | 
| 546 |  |  | } | 
| 547 | greg | 2.40 | setcolor(unsc,  0, 0, 0); | 
| 548 | greg | 2.1 | if (dfp == NULL)                        /* no specular component? */ | 
| 549 |  |  | return(0); | 
| 550 | greg | 2.40 |  | 
| 551 |  |  | if (hasthru) {                          /* separate view sample? */ | 
| 552 |  |  | RAY     tr; | 
| 553 |  |  | if (rayorigin(&tr, TRANS, ndp->pr, ndp->cthru) == 0) { | 
| 554 |  |  | VCOPY(tr.rdir, ndp->pr->rdir); | 
| 555 |  |  | rayvalue(&tr); | 
| 556 |  |  | multcolor(tr.rcol, tr.rcoef); | 
| 557 |  |  | addcolor(ndp->pr->rcol, tr.rcol); | 
| 558 |  |  | ++ntotal; | 
| 559 |  |  | b = bright(ndp->cthru); | 
| 560 |  |  | } else | 
| 561 |  |  | hasthru = 0; | 
| 562 |  |  | } | 
| 563 | greg | 2.43 | if (dfp->maxHemi - b <= FTINY) {        /* have specular to sample? */ | 
| 564 | greg | 2.40 | b = 0; | 
| 565 |  |  | } else { | 
| 566 |  |  | FVECT   vjit; | 
| 567 |  |  | bsdf_jitter(vjit, ndp, ndp->sr_vpsa[1]); | 
| 568 |  |  | b = SDdirectHemi(vjit, sflags, ndp->sd) - b; | 
| 569 |  |  | if (b < 0) b = 0; | 
| 570 |  |  | } | 
| 571 |  |  | if (b <= specthresh+FTINY) {            /* below sampling threshold? */ | 
| 572 |  |  | if (b > FTINY) {                /* XXX no color from BSDF */ | 
| 573 | greg | 2.1 | if (sflags == SDsampSpT) { | 
| 574 | greg | 2.39 | copycolor(unsc, ndp->pr->pcol); | 
| 575 | greg | 2.40 | scalecolor(unsc, b); | 
| 576 | greg | 2.1 | } else                  /* no pattern on reflection */ | 
| 577 | greg | 2.40 | setcolor(unsc, b, b, b); | 
| 578 | greg | 2.1 | } | 
| 579 | greg | 2.40 | return(ntotal); | 
| 580 | greg | 2.1 | } | 
| 581 | greg | 2.41 | dimlist[ndims] = (int)(size_t)ndp->mp;  /* else sample specular */ | 
| 582 |  |  | ndims += 2; | 
| 583 | greg | 2.1 | for (n = dfp->ncomp; n--; ) {           /* loop over components */ | 
| 584 |  |  | dimlist[ndims-1] = n + 9438; | 
| 585 |  |  | ntotal += sample_sdcomp(ndp, &dfp->comp[n], sflags==SDsampSpT); | 
| 586 |  |  | } | 
| 587 |  |  | ndims -= 2; | 
| 588 |  |  | return(ntotal); | 
| 589 |  |  | } | 
| 590 |  |  |  | 
| 591 |  |  | /* Color a ray that hit a BSDF material */ | 
| 592 |  |  | int | 
| 593 |  |  | m_bsdf(OBJREC *m, RAY *r) | 
| 594 |  |  | { | 
| 595 | greg | 2.50 | int     hasthick = (m->otype == MAT_BSDF); | 
| 596 | greg | 2.6 | int     hitfront; | 
| 597 | greg | 2.1 | COLOR   ctmp; | 
| 598 |  |  | SDError ec; | 
| 599 | greg | 2.5 | FVECT   upvec, vtmp; | 
| 600 | greg | 2.1 | MFUNC   *mf; | 
| 601 |  |  | BSDFDAT nd; | 
| 602 |  |  | /* check arguments */ | 
| 603 | greg | 2.50 | if ((m->oargs.nsargs < hasthick+5) | (m->oargs.nfargs > 9) | | 
| 604 | greg | 2.1 | (m->oargs.nfargs % 3)) | 
| 605 |  |  | objerror(m, USER, "bad # arguments"); | 
| 606 | greg | 2.6 | /* record surface struck */ | 
| 607 | greg | 2.9 | hitfront = (r->rod > 0); | 
| 608 | greg | 2.1 | /* load cal file */ | 
| 609 | greg | 2.50 | mf = hasthick   ? getfunc(m, 5, 0x1d, 1) | 
| 610 |  |  | : getfunc(m, 4, 0xe, 1) ; | 
| 611 | greg | 2.25 | setfunc(m, r); | 
| 612 | greg | 2.50 | nd.thick = 0;                           /* set thickness */ | 
| 613 |  |  | if (hasthick) { | 
| 614 |  |  | nd.thick = evalue(mf->ep[0]); | 
| 615 |  |  | if ((-FTINY <= nd.thick) & (nd.thick <= FTINY)) | 
| 616 |  |  | nd.thick = 0; | 
| 617 |  |  | } | 
| 618 | greg | 2.26 | /* check backface visibility */ | 
| 619 |  |  | if (!hitfront & !backvis) { | 
| 620 |  |  | raytrans(r); | 
| 621 |  |  | return(1); | 
| 622 |  |  | } | 
| 623 | greg | 2.5 | /* check other rays to pass */ | 
| 624 | greg | 2.34 | if (nd.thick != 0 && (r->crtype & SHADOW || | 
| 625 |  |  | !(r->crtype & (SPECULAR|AMBIENT)) || | 
| 626 | greg | 2.29 | (nd.thick > 0) ^ hitfront)) { | 
| 627 | greg | 2.5 | raytrans(r);                    /* hide our proxy */ | 
| 628 | greg | 2.1 | return(1); | 
| 629 |  |  | } | 
| 630 | greg | 2.51 | if (hasthick && r->crtype & SHADOW)     /* early shadow check #1 */ | 
| 631 |  |  | return(1); | 
| 632 | greg | 2.31 | nd.mp = m; | 
| 633 |  |  | nd.pr = r; | 
| 634 | greg | 2.5 | /* get BSDF data */ | 
| 635 | greg | 2.50 | nd.sd = loadBSDF(m->oargs.sarg[hasthick]); | 
| 636 | greg | 2.51 | /* early shadow check #2 */ | 
| 637 | greg | 2.55 | if (r->crtype & SHADOW && (nd.sd->tf == NULL) & (nd.sd->tb == NULL)) { | 
| 638 |  |  | SDfreeCache(nd.sd); | 
| 639 | greg | 2.34 | return(1); | 
| 640 | greg | 2.55 | } | 
| 641 | greg | 2.1 | /* diffuse reflectance */ | 
| 642 | greg | 2.6 | if (hitfront) { | 
| 643 | greg | 2.31 | cvt_sdcolor(nd.rdiff, &nd.sd->rLambFront); | 
| 644 |  |  | if (m->oargs.nfargs >= 3) { | 
| 645 |  |  | setcolor(ctmp, m->oargs.farg[0], | 
| 646 | greg | 2.1 | m->oargs.farg[1], | 
| 647 |  |  | m->oargs.farg[2]); | 
| 648 | greg | 2.31 | addcolor(nd.rdiff, ctmp); | 
| 649 |  |  | } | 
| 650 | greg | 2.1 | } else { | 
| 651 | greg | 2.31 | cvt_sdcolor(nd.rdiff, &nd.sd->rLambBack); | 
| 652 |  |  | if (m->oargs.nfargs >= 6) { | 
| 653 |  |  | setcolor(ctmp, m->oargs.farg[3], | 
| 654 | greg | 2.1 | m->oargs.farg[4], | 
| 655 |  |  | m->oargs.farg[5]); | 
| 656 | greg | 2.31 | addcolor(nd.rdiff, ctmp); | 
| 657 |  |  | } | 
| 658 | greg | 2.1 | } | 
| 659 |  |  | /* diffuse transmittance */ | 
| 660 | greg | 2.31 | cvt_sdcolor(nd.tdiff, &nd.sd->tLamb); | 
| 661 |  |  | if (m->oargs.nfargs >= 9) { | 
| 662 |  |  | setcolor(ctmp, m->oargs.farg[6], | 
| 663 | greg | 2.1 | m->oargs.farg[7], | 
| 664 |  |  | m->oargs.farg[8]); | 
| 665 | greg | 2.31 | addcolor(nd.tdiff, ctmp); | 
| 666 |  |  | } | 
| 667 | greg | 2.1 | /* get modifiers */ | 
| 668 |  |  | raytexture(r, m->omod); | 
| 669 |  |  | /* modify diffuse values */ | 
| 670 |  |  | multcolor(nd.rdiff, r->pcol); | 
| 671 |  |  | multcolor(nd.tdiff, r->pcol); | 
| 672 |  |  | /* get up vector */ | 
| 673 | greg | 2.50 | upvec[0] = evalue(mf->ep[hasthick+0]); | 
| 674 |  |  | upvec[1] = evalue(mf->ep[hasthick+1]); | 
| 675 |  |  | upvec[2] = evalue(mf->ep[hasthick+2]); | 
| 676 | greg | 2.1 | /* return to world coords */ | 
| 677 | greg | 2.21 | if (mf->fxp != &unitxf) { | 
| 678 |  |  | multv3(upvec, upvec, mf->fxp->xfm); | 
| 679 |  |  | nd.thick *= mf->fxp->sca; | 
| 680 | greg | 2.1 | } | 
| 681 | greg | 2.23 | if (r->rox != NULL) { | 
| 682 |  |  | multv3(upvec, upvec, r->rox->f.xfm); | 
| 683 |  |  | nd.thick *= r->rox->f.sca; | 
| 684 |  |  | } | 
| 685 | greg | 2.1 | raynormal(nd.pnorm, r); | 
| 686 |  |  | /* compute local BSDF xform */ | 
| 687 |  |  | ec = SDcompXform(nd.toloc, nd.pnorm, upvec); | 
| 688 |  |  | if (!ec) { | 
| 689 | greg | 2.4 | nd.vray[0] = -r->rdir[0]; | 
| 690 |  |  | nd.vray[1] = -r->rdir[1]; | 
| 691 |  |  | nd.vray[2] = -r->rdir[2]; | 
| 692 |  |  | ec = SDmapDir(nd.vray, nd.toloc, nd.vray); | 
| 693 | greg | 2.20 | } | 
| 694 | greg | 2.19 | if (ec) { | 
| 695 |  |  | objerror(m, WARNING, "Illegal orientation vector"); | 
| 696 | greg | 2.55 | SDfreeCache(nd.sd); | 
| 697 | greg | 2.19 | return(1); | 
| 698 | greg | 2.1 | } | 
| 699 | greg | 2.50 | setcolor(nd.cthru, 0, 0, 0);            /* consider through component */ | 
| 700 | greg | 2.52 | if (m->otype == MAT_ABSDF) { | 
| 701 | greg | 2.50 | compute_through(&nd); | 
| 702 |  |  | if (r->crtype & SHADOW) { | 
| 703 |  |  | RAY     tr;             /* attempt to pass shadow ray */ | 
| 704 | greg | 2.55 | SDfreeCache(nd.sd); | 
| 705 | greg | 2.50 | if (rayorigin(&tr, TRANS, r, nd.cthru) < 0) | 
| 706 |  |  | return(1);      /* no through component */ | 
| 707 |  |  | VCOPY(tr.rdir, r->rdir); | 
| 708 |  |  | rayvalue(&tr);          /* transmit with scaling */ | 
| 709 |  |  | multcolor(tr.rcol, tr.rcoef); | 
| 710 |  |  | copycolor(r->rcol, tr.rcol); | 
| 711 |  |  | return(1);              /* we're done */ | 
| 712 |  |  | } | 
| 713 | greg | 2.34 | } | 
| 714 |  |  | ec = SDinvXform(nd.fromloc, nd.toloc); | 
| 715 |  |  | if (!ec)                                /* determine BSDF resolution */ | 
| 716 |  |  | ec = SDsizeBSDF(nd.sr_vpsa, nd.vray, NULL, | 
| 717 |  |  | SDqueryMin+SDqueryMax, nd.sd); | 
| 718 | greg | 2.20 | if (ec) | 
| 719 |  |  | objerror(m, USER, transSDError(ec)); | 
| 720 |  |  |  | 
| 721 | greg | 2.9 | nd.sr_vpsa[0] = sqrt(nd.sr_vpsa[0]); | 
| 722 |  |  | nd.sr_vpsa[1] = sqrt(nd.sr_vpsa[1]); | 
| 723 | greg | 2.6 | if (!hitfront) {                        /* perturb normal towards hit */ | 
| 724 | greg | 2.1 | nd.pnorm[0] = -nd.pnorm[0]; | 
| 725 |  |  | nd.pnorm[1] = -nd.pnorm[1]; | 
| 726 |  |  | nd.pnorm[2] = -nd.pnorm[2]; | 
| 727 |  |  | } | 
| 728 |  |  | /* sample reflection */ | 
| 729 |  |  | sample_sdf(&nd, SDsampSpR); | 
| 730 |  |  | /* sample transmission */ | 
| 731 |  |  | sample_sdf(&nd, SDsampSpT); | 
| 732 |  |  | /* compute indirect diffuse */ | 
| 733 | greg | 2.39 | copycolor(ctmp, nd.rdiff); | 
| 734 |  |  | addcolor(ctmp, nd.runsamp); | 
| 735 |  |  | if (bright(ctmp) > FTINY) {             /* ambient from reflection */ | 
| 736 | greg | 2.6 | if (!hitfront) | 
| 737 | greg | 2.1 | flipsurface(r); | 
| 738 |  |  | multambient(ctmp, r, nd.pnorm); | 
| 739 |  |  | addcolor(r->rcol, ctmp); | 
| 740 | greg | 2.6 | if (!hitfront) | 
| 741 | greg | 2.1 | flipsurface(r); | 
| 742 |  |  | } | 
| 743 | greg | 2.39 | copycolor(ctmp, nd.tdiff); | 
| 744 |  |  | addcolor(ctmp, nd.tunsamp); | 
| 745 |  |  | if (bright(ctmp) > FTINY) {             /* ambient from other side */ | 
| 746 | greg | 2.1 | FVECT  bnorm; | 
| 747 | greg | 2.6 | if (hitfront) | 
| 748 | greg | 2.1 | flipsurface(r); | 
| 749 |  |  | bnorm[0] = -nd.pnorm[0]; | 
| 750 |  |  | bnorm[1] = -nd.pnorm[1]; | 
| 751 |  |  | bnorm[2] = -nd.pnorm[2]; | 
| 752 | greg | 2.9 | if (nd.thick != 0) {            /* proxy with offset? */ | 
| 753 | greg | 2.5 | VCOPY(vtmp, r->rop); | 
| 754 | greg | 2.18 | VSUM(r->rop, vtmp, r->ron, nd.thick); | 
| 755 | greg | 2.5 | multambient(ctmp, r, bnorm); | 
| 756 |  |  | VCOPY(r->rop, vtmp); | 
| 757 |  |  | } else | 
| 758 |  |  | multambient(ctmp, r, bnorm); | 
| 759 | greg | 2.1 | addcolor(r->rcol, ctmp); | 
| 760 | greg | 2.6 | if (hitfront) | 
| 761 | greg | 2.1 | flipsurface(r); | 
| 762 |  |  | } | 
| 763 |  |  | /* add direct component */ | 
| 764 | greg | 2.22 | if ((bright(nd.tdiff) <= FTINY) & (nd.sd->tf == NULL) & | 
| 765 |  |  | (nd.sd->tb == NULL)) { | 
| 766 | greg | 2.5 | direct(r, dir_brdf, &nd);       /* reflection only */ | 
| 767 | greg | 2.9 | } else if (nd.thick == 0) { | 
| 768 | greg | 2.5 | direct(r, dir_bsdf, &nd);       /* thin surface scattering */ | 
| 769 |  |  | } else { | 
| 770 |  |  | direct(r, dir_brdf, &nd);       /* reflection first */ | 
| 771 |  |  | VCOPY(vtmp, r->rop);            /* offset for transmitted */ | 
| 772 |  |  | VSUM(r->rop, vtmp, r->ron, -nd.thick); | 
| 773 | greg | 2.6 | direct(r, dir_btdf, &nd);       /* separate transmission */ | 
| 774 | greg | 2.5 | VCOPY(r->rop, vtmp); | 
| 775 |  |  | } | 
| 776 | greg | 2.1 | /* clean up */ | 
| 777 |  |  | SDfreeCache(nd.sd); | 
| 778 |  |  | return(1); | 
| 779 |  |  | } |