| 1 | #ifndef lint | 
| 2 | static const char RCSid[] = "$Id: source.c,v 2.87 2025/06/20 03:43:17 greg Exp $"; | 
| 3 | #endif | 
| 4 | /* | 
| 5 | *  source.c - routines dealing with illumination sources. | 
| 6 | * | 
| 7 | *  External symbols declared in source.h | 
| 8 | */ | 
| 9 |  | 
| 10 | #include  "ray.h" | 
| 11 | #include  "otypes.h" | 
| 12 | #include  "otspecial.h" | 
| 13 | #include  "rtotypes.h" | 
| 14 | #include  "source.h" | 
| 15 | #include  "random.h" | 
| 16 | #include  "pmapsrc.h" | 
| 17 | #include  "pmapmat.h" | 
| 18 |  | 
| 19 | #ifndef MAXSSAMP | 
| 20 | #define MAXSSAMP        16              /* maximum samples per ray */ | 
| 21 | #endif | 
| 22 |  | 
| 23 | /* | 
| 24 | * Structures used by direct() | 
| 25 | */ | 
| 26 |  | 
| 27 | typedef struct { | 
| 28 | int  sno;               /* source number */ | 
| 29 | FVECT  dir;             /* source direction */ | 
| 30 | SCOLOR  coef;           /* material coefficient */ | 
| 31 | SCOLOR  val;            /* contribution */ | 
| 32 | }  CONTRIB;             /* direct contribution */ | 
| 33 |  | 
| 34 | typedef struct { | 
| 35 | int  sndx;              /* source index (to CONTRIB array) */ | 
| 36 | float  brt;             /* brightness (for comparison) */ | 
| 37 | }  CNTPTR;              /* contribution pointer */ | 
| 38 |  | 
| 39 | static CONTRIB  *srccnt = NULL;         /* source contributions in direct() */ | 
| 40 | static CNTPTR  *cntord = NULL;          /* source ordering in direct() */ | 
| 41 | static int  maxcntr = 0;                /* size of contribution arrays */ | 
| 42 |  | 
| 43 | static int cntcmp(const void *p1, const void *p2); | 
| 44 |  | 
| 45 |  | 
| 46 | void | 
| 47 | marksources(void)                       /* find and mark source objects */ | 
| 48 | { | 
| 49 | int  indirect = 0; | 
| 50 | int  i; | 
| 51 | OBJREC  *o, *m; | 
| 52 | int  ns; | 
| 53 | /* call us only once! */ | 
| 54 | if (nsources) | 
| 55 | error(CONSISTENCY, "multiple calls to marksources!"); | 
| 56 | /* initialize dispatch table */ | 
| 57 | initstypes(); | 
| 58 | /* find direct sources */ | 
| 59 | for (i = 0; i < nsceneobjs; i++) { | 
| 60 |  | 
| 61 | o = objptr(i); | 
| 62 |  | 
| 63 | if (!issurface(o->otype) || o->omod == OVOID) | 
| 64 | continue; | 
| 65 | /* find material */ | 
| 66 | m = findmaterial(o); | 
| 67 | if (m == NULL) | 
| 68 | continue; | 
| 69 | if (m->otype == MAT_CLIP) { | 
| 70 | markclip(m);    /* special case for antimatter */ | 
| 71 | continue; | 
| 72 | } | 
| 73 | if (!islight(m->otype)) | 
| 74 | continue;       /* not source modifier */ | 
| 75 |  | 
| 76 | if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 : | 
| 77 | m->otype == MAT_SPOT ? 7 : 3)) | 
| 78 | objerror(m, USER, "bad # arguments"); | 
| 79 |  | 
| 80 | if (m->oargs.farg[0] <= FTINY && (m->oargs.farg[1] <= FTINY) & | 
| 81 | (m->oargs.farg[2] <= FTINY)) | 
| 82 | continue;                       /* don't bother */ | 
| 83 | if (m->otype == MAT_GLOW && | 
| 84 | o->otype != OBJ_SOURCE && | 
| 85 | m->oargs.farg[3] <= FTINY) { | 
| 86 | indirect += (ambounce > 0); | 
| 87 | continue;                       /* don't track these */ | 
| 88 | } | 
| 89 | if (sfun[o->otype].of == NULL || | 
| 90 | sfun[o->otype].of->setsrc == NULL) | 
| 91 | objerror(o, USER, "illegal material"); | 
| 92 |  | 
| 93 | if ((ns = newsource()) < 0) | 
| 94 | goto memerr; | 
| 95 |  | 
| 96 | setsource(&source[ns], o); | 
| 97 |  | 
| 98 | if (m->otype == MAT_GLOW) { | 
| 99 | source[ns].sflags |= SPROX; | 
| 100 | source[ns].sl.prox = m->oargs.farg[3]; | 
| 101 | if (source[ns].sflags & SDISTANT) { | 
| 102 | source[ns].sflags |= SSKIP; | 
| 103 | indirect += (ambounce > 0); | 
| 104 | } | 
| 105 | } else if (m->otype == MAT_SPOT) { | 
| 106 | if (source[ns].sflags & SDISTANT) | 
| 107 | objerror(o, WARNING, | 
| 108 | "distant source is a spotlight"); | 
| 109 | source[ns].sflags |= SSPOT; | 
| 110 | if ((source[ns].sl.s = makespot(m)) == NULL) | 
| 111 | goto memerr; | 
| 112 | if (source[ns].sflags & SFLAT && | 
| 113 | !checkspot(source[ns].sl.s,source[ns].snorm)) { | 
| 114 | objerror(o, WARNING, | 
| 115 | "invalid spotlight direction"); | 
| 116 | source[ns].sflags |= SSKIP; | 
| 117 | } | 
| 118 | } | 
| 119 | maxcntr += !(source[ns].sflags & SSKIP); | 
| 120 | } | 
| 121 | if (!maxcntr) { | 
| 122 | if (!indirect) | 
| 123 | error(WARNING, "no light sources found"); | 
| 124 | return;         /* no direct calculation, it seems */ | 
| 125 | } | 
| 126 | #if  SHADCACHE | 
| 127 | for (ns = 0; ns < nsources; ns++)       /* initialize obstructor cache */ | 
| 128 | initobscache(ns); | 
| 129 | #endif | 
| 130 | /* PMAP: disable virtual sources */ | 
| 131 | if (!photonMapping) | 
| 132 | markvirtuals();                 /* find and add virtual sources */ | 
| 133 |  | 
| 134 | /* allocate our contribution arrays */ | 
| 135 | maxcntr += MAXSPART;    /* start with this many */ | 
| 136 | srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB)); | 
| 137 | cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR)); | 
| 138 | if ((srccnt != NULL) & (cntord != NULL)) | 
| 139 | return; | 
| 140 | memerr: | 
| 141 | error(SYSTEM, "out of memory in marksources"); | 
| 142 | } | 
| 143 |  | 
| 144 |  | 
| 145 | void | 
| 146 | distantsources(void)                    /* only mark distant sources */ | 
| 147 | { | 
| 148 | int  i; | 
| 149 | OBJREC  *o, *m; | 
| 150 | int  ns; | 
| 151 | /* call us only once! */ | 
| 152 | if (nsources) | 
| 153 | error(CONSISTENCY, "multiple calls to distantsources!"); | 
| 154 | /* initialize dispatch table */ | 
| 155 | initstypes(); | 
| 156 | /* sources needed for sourcehit() */ | 
| 157 | for (i = 0; i < nsceneobjs; i++) { | 
| 158 |  | 
| 159 | o = objptr(i); | 
| 160 |  | 
| 161 | if ((o->otype != OBJ_SOURCE) | (o->omod == OVOID)) | 
| 162 | continue; | 
| 163 | /* find material */ | 
| 164 | m = findmaterial(o); | 
| 165 | if (m == NULL) | 
| 166 | continue; | 
| 167 | if (!islight(m->otype)) | 
| 168 | continue;       /* not source modifier */ | 
| 169 |  | 
| 170 | if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 : | 
| 171 | m->otype == MAT_SPOT ? 7 : 3)) | 
| 172 | objerror(m, USER, "bad # arguments"); | 
| 173 |  | 
| 174 | if (m->oargs.farg[0] <= FTINY && (m->oargs.farg[1] <= FTINY) & | 
| 175 | (m->oargs.farg[2] <= FTINY)) | 
| 176 | continue;                       /* don't bother */ | 
| 177 | if (sfun[o->otype].of == NULL || | 
| 178 | sfun[o->otype].of->setsrc == NULL) | 
| 179 | objerror(o, USER, "illegal material"); | 
| 180 |  | 
| 181 | if ((ns = newsource()) < 0) | 
| 182 | error(SYSTEM, "out of memory in distantsources"); | 
| 183 |  | 
| 184 | setsource(&source[ns], o); | 
| 185 |  | 
| 186 | if (m->otype == MAT_GLOW) { | 
| 187 | source[ns].sflags |= SPROX|SSKIP; | 
| 188 | source[ns].sl.prox = m->oargs.farg[3]; | 
| 189 | } else if (m->otype == MAT_SPOT) | 
| 190 | objerror(o, WARNING, "distant source is a spotlight"); | 
| 191 | } | 
| 192 | } | 
| 193 |  | 
| 194 |  | 
| 195 | void | 
| 196 | freesources(void)                       /* free all source structures */ | 
| 197 | { | 
| 198 | if (nsources > 0) { | 
| 199 | #if SHADCACHE | 
| 200 | while (nsources--) | 
| 201 | freeobscache(&source[nsources]); | 
| 202 | #endif | 
| 203 | free(source); | 
| 204 | source = NULL; | 
| 205 | nsources = 0; | 
| 206 | } | 
| 207 | markclip(NULL); | 
| 208 | if (maxcntr <= 0) | 
| 209 | return; | 
| 210 | free(srccnt); | 
| 211 | srccnt = NULL; | 
| 212 | free(cntord); | 
| 213 | cntord = NULL; | 
| 214 | maxcntr = 0; | 
| 215 | } | 
| 216 |  | 
| 217 |  | 
| 218 | int | 
| 219 | srcray(                         /* aim a ray at a source, return domega */ | 
| 220 | RAY  *sr,               /* prepared source ray */ | 
| 221 | RAY  *r,                        /* originating ray (or NULL) */ | 
| 222 | SRCINDEX  *si                   /* source sample index */ | 
| 223 | ) | 
| 224 | { | 
| 225 | double  d;                              /* distance to source */ | 
| 226 | SRCREC  *srcp; | 
| 227 |  | 
| 228 | rayorigin(sr, SHADOW, r, NULL);         /* ignore limits */ | 
| 229 |  | 
| 230 | if (r == NULL) | 
| 231 | sr->rmax = 0.0; | 
| 232 |  | 
| 233 | while ((d = nextssamp(sr, si)) != 0.0) { | 
| 234 | sr->rsrc = si->sn;              /* remember source */ | 
| 235 | srcp = source + si->sn; | 
| 236 | if (srcp->sflags & SDISTANT) { | 
| 237 | if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) | 
| 238 | continue; | 
| 239 | return(1);              /* sample OK */ | 
| 240 | } | 
| 241 | /* local source */ | 
| 242 | /* check proximity */ | 
| 243 | if (srcp->sflags & SPROX && d > srcp->sl.prox) | 
| 244 | continue; | 
| 245 | /* check angle */ | 
| 246 | if (srcp->sflags & SSPOT) { | 
| 247 | if (spotout(sr, srcp->sl.s)) | 
| 248 | continue; | 
| 249 | /* adjust solid angle */ | 
| 250 | si->dom *= d*d; | 
| 251 | d += srcp->sl.s->flen; | 
| 252 | si->dom /= d*d; | 
| 253 | } | 
| 254 | return(1);                      /* sample OK */ | 
| 255 | } | 
| 256 | return(0);                      /* no more samples */ | 
| 257 | } | 
| 258 |  | 
| 259 |  | 
| 260 | void | 
| 261 | srcvalue(                       /* punch ray to source and compute value */ | 
| 262 | RAY  *r | 
| 263 | ) | 
| 264 | { | 
| 265 | SRCREC  *sp; | 
| 266 |  | 
| 267 | sp = &source[r->rsrc]; | 
| 268 | if (sp->sflags & SVIRTUAL) {    /* virtual source */ | 
| 269 | /* check intersection */ | 
| 270 | if (!(*ofun[sp->so->otype].funp)(sp->so, r)) | 
| 271 | return; | 
| 272 | if (!rayshade(r, r->ro->omod))  /* compute contribution */ | 
| 273 | goto nomat; | 
| 274 | rayparticipate(r); | 
| 275 | return; | 
| 276 | } | 
| 277 | /* compute intersection */ | 
| 278 | if (sp->sflags & SDISTANT ? sourcehit(r) : | 
| 279 | (*ofun[sp->so->otype].funp)(sp->so, r)) { | 
| 280 | if (sp->sa.success >= 0) | 
| 281 | sp->sa.success++; | 
| 282 | if (!rayshade(r, r->ro->omod))  /* compute contribution */ | 
| 283 | goto nomat; | 
| 284 | rayparticipate(r); | 
| 285 | return; | 
| 286 | } | 
| 287 | /* we missed our mark! */ | 
| 288 | if (sp->sa.success < 0) | 
| 289 | return;                 /* bitched already */ | 
| 290 | sp->sa.success -= AIMREQT; | 
| 291 | if (sp->sa.success >= 0) | 
| 292 | return;                 /* leniency */ | 
| 293 | sprintf(errmsg, "aiming failure for light source \"%s\"", | 
| 294 | sp->so->oname); | 
| 295 | error(WARNING, errmsg);         /* issue warning */ | 
| 296 | return; | 
| 297 | nomat: | 
| 298 | objerror(r->ro, USER, "material not found"); | 
| 299 | } | 
| 300 |  | 
| 301 |  | 
| 302 | static int | 
| 303 | transillum(                     /* check if material is transparent illum */ | 
| 304 | OBJREC *m | 
| 305 | ) | 
| 306 | { | 
| 307 | m = findmaterial(m); | 
| 308 | if (m == NULL) | 
| 309 | return(1); | 
| 310 | if (m->otype != MAT_ILLUM) | 
| 311 | return(0); | 
| 312 | return(!m->oargs.nsargs || !strcmp(m->oargs.sarg[0], VOIDID)); | 
| 313 | } | 
| 314 |  | 
| 315 |  | 
| 316 | int | 
| 317 | sourcehit(                      /* check to see if ray hit distant source */ | 
| 318 | RAY  *r | 
| 319 | ) | 
| 320 | { | 
| 321 | int  glowsrc = -1; | 
| 322 | int  transrc = -1; | 
| 323 | int  first, last; | 
| 324 | int  i; | 
| 325 |  | 
| 326 | if (r->rsrc >= 0) {             /* check only one if aimed */ | 
| 327 | first = last = r->rsrc; | 
| 328 | } else {                        /* otherwise check all */ | 
| 329 | first = 0; last = nsources-1; | 
| 330 | } | 
| 331 | for (i = first; i <= last; i++) { | 
| 332 | if ((source[i].sflags & (SDISTANT|SVIRTUAL)) != SDISTANT) | 
| 333 | continue; | 
| 334 | /* | 
| 335 | * Check to see if ray is within | 
| 336 | * solid angle of source. | 
| 337 | */ | 
| 338 | if (2.*PI*(1. - DOT(source[i].sloc,r->rdir)) > source[i].ss2) | 
| 339 | continue; | 
| 340 | /* is it what we aimed for? */ | 
| 341 | if (i == r->rsrc) { | 
| 342 | r->ro = source[i].so; | 
| 343 | break; | 
| 344 | } | 
| 345 | /* | 
| 346 | * If it's a glow or transparent illum, just remember it. | 
| 347 | */ | 
| 348 | if (source[i].sflags & SSKIP) { | 
| 349 | if (glowsrc < 0) | 
| 350 | glowsrc = i; | 
| 351 | continue; | 
| 352 | } | 
| 353 | if (transillum(source[i].so)) { | 
| 354 | if (transrc < 0) | 
| 355 | transrc = i; | 
| 356 | continue; | 
| 357 | } | 
| 358 | r->ro = source[i].so;   /* otherwise, use first hit */ | 
| 359 | break; | 
| 360 | } | 
| 361 | /* | 
| 362 | * Do we need fallback? | 
| 363 | */ | 
| 364 | if (r->ro == NULL) { | 
| 365 | if (transrc >= 0 && r->crtype & (AMBIENT|SPECULAR)) | 
| 366 | return(0);      /* avoid overcounting */ | 
| 367 | if (glowsrc >= 0) | 
| 368 | r->ro = source[glowsrc].so; | 
| 369 | else | 
| 370 | return(0);      /* nothing usable */ | 
| 371 | } | 
| 372 | /* | 
| 373 | * Assign object index | 
| 374 | */ | 
| 375 | r->robj = objndx(r->ro); | 
| 376 | return(1); | 
| 377 | } | 
| 378 |  | 
| 379 |  | 
| 380 | static int | 
| 381 | cntcmp(                         /* contribution compare (descending) */ | 
| 382 | const void *p1, | 
| 383 | const void *p2 | 
| 384 | ) | 
| 385 | { | 
| 386 | const CNTPTR  *sc1 = (const CNTPTR *)p1; | 
| 387 | const CNTPTR  *sc2 = (const CNTPTR *)p2; | 
| 388 |  | 
| 389 | if (sc1->brt > sc2->brt) | 
| 390 | return(-1); | 
| 391 | if (sc1->brt < sc2->brt) | 
| 392 | return(1); | 
| 393 | return(0); | 
| 394 | } | 
| 395 |  | 
| 396 |  | 
| 397 | void | 
| 398 | direct(                                 /* add direct component */ | 
| 399 | RAY  *r,                        /* ray that hit surface */ | 
| 400 | srcdirf_t *f,                   /* direct component coefficient function */ | 
| 401 | void  *p                        /* data for f */ | 
| 402 | ) | 
| 403 | { | 
| 404 | int  sn; | 
| 405 | CONTRIB  *scp; | 
| 406 | SRCINDEX  si; | 
| 407 | int  nshadcheck, ncnts; | 
| 408 | int  nhits; | 
| 409 | double  prob, ourthresh, hwt; | 
| 410 | RAY  sr; | 
| 411 |  | 
| 412 | /* PMAP: Factor in direct photons (primarily for debugging/validation) */ | 
| 413 | if (directPhotonMapping) { | 
| 414 | (*f)(r -> rcol, p, r -> ron, PI); | 
| 415 | multDirectPmap(r); | 
| 416 | return; | 
| 417 | } | 
| 418 | /* NOTE: srccnt and cntord global so no recursion */ | 
| 419 | if (maxcntr <= 0) | 
| 420 | return;         /* no direct?! */ | 
| 421 |  | 
| 422 | initsrcindex(&si);                      /* potential contributions */ | 
| 423 | for (sn = 0; srcray(&sr, r, &si); sn++) { | 
| 424 | if (sn >= maxcntr) { | 
| 425 | maxcntr = sn + MAXSPART; | 
| 426 | srccnt = (CONTRIB *)realloc((void *)srccnt, | 
| 427 | maxcntr*sizeof(CONTRIB)); | 
| 428 | cntord = (CNTPTR *)realloc((void *)cntord, | 
| 429 | maxcntr*sizeof(CNTPTR)); | 
| 430 | if ((srccnt == NULL) | (cntord == NULL)) | 
| 431 | error(SYSTEM, "out of memory in direct"); | 
| 432 | } | 
| 433 | cntord[sn].sndx = sn; | 
| 434 | scp = srccnt + sn; | 
| 435 | scp->sno = sr.rsrc; | 
| 436 | #if SHADCACHE                                   /* check shadow cache */ | 
| 437 | if (si.np == 1 && srcblocked(&sr)) { | 
| 438 | cntord[sn].brt = 0.0;   /* & count as test */ | 
| 439 | if (source[scp->sno].ntests++ > 0xfffffff0) { | 
| 440 | source[scp->sno].ntests >>= 1; | 
| 441 | source[scp->sno].nhits >>= 1; | 
| 442 | } | 
| 443 | continue; | 
| 444 | } | 
| 445 | #endif | 
| 446 | /* compute coefficient */ | 
| 447 | (*f)(scp->coef, p, sr.rdir, si.dom); | 
| 448 | cntord[sn].brt = sintens(scp->coef); | 
| 449 | if (cntord[sn].brt <= 0.0) | 
| 450 | continue; | 
| 451 | VCOPY(scp->dir, sr.rdir); | 
| 452 | copyscolor(sr.rcoef, scp->coef); | 
| 453 | /* compute potential */ | 
| 454 | sr.revf = srcvalue; | 
| 455 | rayvalue(&sr); | 
| 456 | smultscolor(sr.rcol, sr.rcoef); | 
| 457 | copyscolor(scp->val, sr.rcol); | 
| 458 | cntord[sn].brt = pbright(sr.rcol); | 
| 459 | } | 
| 460 | /* sort contributions */ | 
| 461 | qsort(cntord, sn, sizeof(CNTPTR), cntcmp); | 
| 462 | {                                       /* find last */ | 
| 463 | int  l, m; | 
| 464 |  | 
| 465 | ncnts = l = sn; | 
| 466 | sn = 0; | 
| 467 | while ((m = (sn + ncnts) >> 1) != l) { | 
| 468 | if (cntord[m].brt > 0.0) | 
| 469 | sn = m; | 
| 470 | else | 
| 471 | ncnts = m; | 
| 472 | l = m; | 
| 473 | } | 
| 474 | } | 
| 475 | if (ncnts == 0) | 
| 476 | return;         /* no contributions! */ | 
| 477 | /* accumulate tail */ | 
| 478 | for (sn = ncnts-1; sn > 0; sn--) | 
| 479 | cntord[sn-1].brt += cntord[sn].brt; | 
| 480 | /* compute number to check */ | 
| 481 | nshadcheck = pow((double)ncnts, shadcert) + .5; | 
| 482 | /* modify threshold */ | 
| 483 | ourthresh = shadthresh / r->rweight; | 
| 484 | /* test for shadows */ | 
| 485 | for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts; | 
| 486 | hwt += (double)source[scp->sno].nhits / | 
| 487 | (double)source[scp->sno].ntests, | 
| 488 | sn++) { | 
| 489 | /* check threshold */ | 
| 490 | if (sn >= MINSHADCNT && | 
| 491 | (sn+nshadcheck>=ncnts ? cntord[sn].brt : | 
| 492 | cntord[sn].brt-cntord[sn+nshadcheck].brt) | 
| 493 | < ourthresh*pbright(r->rcol)) | 
| 494 | break; | 
| 495 | scp = srccnt + cntord[sn].sndx; | 
| 496 | /* test for hit */ | 
| 497 | rayorigin(&sr, thrudir(r,scp->dir) ? TSHADOW : RSHADOW, r, NULL); | 
| 498 | copyscolor(sr.rcoef, scp->coef); | 
| 499 | VCOPY(sr.rdir, scp->dir); | 
| 500 | sr.rsrc = scp->sno; | 
| 501 | /* keep statistics */ | 
| 502 | if (source[scp->sno].ntests++ > 0xfffffff0) { | 
| 503 | source[scp->sno].ntests >>= 1; | 
| 504 | source[scp->sno].nhits >>= 1; | 
| 505 | } | 
| 506 | if (localhit(&sr, &thescene) && | 
| 507 | ( sr.ro != source[scp->sno].so || | 
| 508 | source[scp->sno].sflags & SFOLLOW )) { | 
| 509 | /* follow entire path */ | 
| 510 | raycont(&sr); | 
| 511 | if (trace != NULL) | 
| 512 | (*trace)(&sr);  /* trace execution */ | 
| 513 | if (scolor_mean(sr.rcol) <= FTINY) { | 
| 514 | #if SHADCACHE | 
| 515 | if ((scp <= srccnt || scp[-1].sno != scp->sno) | 
| 516 | && (scp >= srccnt+ncnts-1 || | 
| 517 | scp[1].sno != scp->sno)) | 
| 518 | srcblocker(&sr); | 
| 519 | #endif | 
| 520 | continue;       /* missed! */ | 
| 521 | } | 
| 522 | rayparticipate(&sr); | 
| 523 | smultscolor(sr.rcol, sr.rcoef); | 
| 524 | copyscolor(scp->val, sr.rcol); | 
| 525 | } else if (trace != NULL && | 
| 526 | (source[scp->sno].sflags & (SDISTANT|SVIRTUAL|SFOLLOW)) | 
| 527 | == (SDISTANT|SFOLLOW) && | 
| 528 | sourcehit(&sr) && rayshade(&sr, sr.ro->omod)) { | 
| 529 | (*trace)(&sr);          /* trace execution */ | 
| 530 | /* skip call to rayparticipate() & scp->val update */ | 
| 531 | } | 
| 532 | /* add contribution if hit */ | 
| 533 | saddscolor(r->rcol, scp->val); | 
| 534 | nhits++; | 
| 535 | source[scp->sno].nhits++; | 
| 536 | } | 
| 537 | /* source hit rate */ | 
| 538 | if (hwt > FTINY) | 
| 539 | hwt = (double)nhits / hwt; | 
| 540 | else | 
| 541 | hwt = 0.5; | 
| 542 | #ifdef DEBUG | 
| 543 | sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n", | 
| 544 | sn, ncnts-sn, hwt); | 
| 545 | eputs(errmsg); | 
| 546 | #endif | 
| 547 | /* add in untested sources */ | 
| 548 | for ( ; sn < ncnts; sn++) { | 
| 549 | scp = srccnt + cntord[sn].sndx; | 
| 550 | prob = hwt * (double)source[scp->sno].nhits / | 
| 551 | (double)source[scp->sno].ntests; | 
| 552 | if (prob < 1.0) | 
| 553 | scalescolor(scp->val, prob); | 
| 554 | saddscolor(r->rcol, scp->val); | 
| 555 | } | 
| 556 | } | 
| 557 |  | 
| 558 |  | 
| 559 | void | 
| 560 | srcscatter(                     /* compute source scattering into ray */ | 
| 561 | RAY  *r | 
| 562 | ) | 
| 563 | { | 
| 564 | unsigned long  oldsampndx; | 
| 565 | int  nsamps; | 
| 566 | RAY  sr; | 
| 567 | SRCINDEX  si; | 
| 568 | double  t, d; | 
| 569 | double  re, ge, be; | 
| 570 | COLOR  cvext; | 
| 571 | int  i, j; | 
| 572 |  | 
| 573 | if (r->rot >= FHUGE*.99 || r->gecc >= 1.-FTINY) | 
| 574 | return;         /* this can never work */ | 
| 575 | /* PMAP: do unconditional inscattering for volume photons */ | 
| 576 | if (!volumePhotonMapping && (r->slights == NULL || r->slights[0] == 0)) | 
| 577 | return; | 
| 578 |  | 
| 579 | if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) | 
| 580 | nsamps = 1; | 
| 581 | #if MAXSSAMP | 
| 582 | else if (nsamps > MAXSSAMP) | 
| 583 | nsamps = MAXSSAMP; | 
| 584 | #endif | 
| 585 | oldsampndx = samplendx; | 
| 586 | samplendx = random()&0x7ffff;           /* randomize */ | 
| 587 | for (i = volumePhotonMapping ? 1 : r->slights[0]; i > 0; i--) { | 
| 588 | /* for each source OR once if volume photon map enabled */ | 
| 589 | for (j = 0; j < nsamps; j++) {  /* for each sample position */ | 
| 590 | samplendx++; | 
| 591 | t = r->rot * (j+frandom())/nsamps; | 
| 592 | /* extinction */ | 
| 593 | re = t*colval(r->cext,RED); | 
| 594 | ge = t*colval(r->cext,GRN); | 
| 595 | be = t*colval(r->cext,BLU); | 
| 596 | setcolor(cvext, re > 92. ? 0. : exp(-re), | 
| 597 | ge > 92. ? 0. : exp(-ge), | 
| 598 | be > 92. ? 0. : exp(-be)); | 
| 599 | if (intens(cvext) <= FTINY*FTINY) | 
| 600 | break;                  /* too far away */ | 
| 601 | sr.rorg[0] = r->rorg[0] + r->rdir[0]*t; | 
| 602 | sr.rorg[1] = r->rorg[1] + r->rdir[1]*t; | 
| 603 | sr.rorg[2] = r->rorg[2] + r->rdir[2]*t; | 
| 604 |  | 
| 605 | if (!volumePhotonMapping) { | 
| 606 | sr.parent = r;          /* hack for preemptive test */ | 
| 607 | if (srcskip(r->slights[i], &sr)) | 
| 608 | continue; | 
| 609 | initsrcindex(&si);      /* sample ray to this source */ | 
| 610 | si.sn = r->slights[i]; | 
| 611 | nopart(&si, &sr); | 
| 612 | if (!srcray(&sr, NULL, &si) || | 
| 613 | sr.rsrc != r->slights[i]) | 
| 614 | continue;       /* no path */ | 
| 615 | #if SHADCACHE | 
| 616 | if (srcblocked(&sr))    /* check shadow cache */ | 
| 617 | continue; | 
| 618 | #endif | 
| 619 | copycolor(sr.cext, r->cext); | 
| 620 | copycolor(sr.albedo, r->albedo); | 
| 621 | sr.gecc = r->gecc; | 
| 622 | sr.slights = r->slights; | 
| 623 | rayvalue(&sr);          /* eval. source ray */ | 
| 624 | if (pbright(sr.rcol) <= FTINY) { | 
| 625 | #if SHADCACHE | 
| 626 | srcblocker(&sr); /* add blocker to cache */ | 
| 627 | #endif | 
| 628 | continue; | 
| 629 | } | 
| 630 | if (r->gecc <= FTINY)   /* compute P(theta) */ | 
| 631 | d = 1.; | 
| 632 | else { | 
| 633 | d = DOT(r->rdir, sr.rdir); | 
| 634 | d = 1. + r->gecc*r->gecc - 2.*r->gecc*d; | 
| 635 | d = (1. - r->gecc*r->gecc) / (d*sqrt(d)); | 
| 636 | } | 
| 637 | /* other factors */ | 
| 638 | d *= si.dom * r->rot / (4.*PI*nsamps); | 
| 639 | scalescolor(sr.rcol, d); | 
| 640 | } else { | 
| 641 | /* PMAP: Add ambient inscattering from | 
| 642 | * volume photons; note we reverse the | 
| 643 | * incident ray direction since we're | 
| 644 | * now in *backward* raytracing mode! */ | 
| 645 | sr.rdir [0] = -r -> rdir [0]; | 
| 646 | sr.rdir [1] = -r -> rdir [1]; | 
| 647 | sr.rdir [2] = -r -> rdir [2]; | 
| 648 | sr.gecc = r -> gecc; | 
| 649 | inscatterVolumePmap(&sr, sr.rcol); | 
| 650 | scalescolor(sr.rcol, r -> rot / nsamps); | 
| 651 | } | 
| 652 | smultcolor(sr.rcol, r->cext); | 
| 653 | smultcolor(sr.rcol, r->albedo); | 
| 654 | smultcolor(sr.rcol, cvext); | 
| 655 | saddscolor(r->rcol, sr.rcol);   /* add it in */ | 
| 656 | } | 
| 657 | } | 
| 658 | samplendx = oldsampndx; | 
| 659 | } | 
| 660 |  | 
| 661 |  | 
| 662 | /**************************************************************** | 
| 663 | * The following macros were separated from the m_light() routine | 
| 664 | * because they are very nasty and difficult to understand. | 
| 665 | */ | 
| 666 |  | 
| 667 | /* illumblock * | 
| 668 | * | 
| 669 | * We cannot allow an illum to pass to another illum, because that | 
| 670 | * would almost certainly constitute overcounting. | 
| 671 | * However, we do allow an illum to pass to another illum | 
| 672 | * that is actually going to relay to a virtual light source. | 
| 673 | * We also prevent an illum from passing to a glow; this provides a | 
| 674 | * convenient mechanism for defining detailed light source | 
| 675 | * geometry behind (or inside) an effective radiator. | 
| 676 | */ | 
| 677 |  | 
| 678 | static int | 
| 679 | weaksrcmat(OBJREC *m)           /* identify material */ | 
| 680 | { | 
| 681 | m = findmaterial(m); | 
| 682 | if (m == NULL) return(0); | 
| 683 | return((m->otype==MAT_ILLUM) | (m->otype==MAT_GLOW)); | 
| 684 | } | 
| 685 |  | 
| 686 | #define  illumblock(m, r)       (!(source[r->rsrc].sflags&SVIRTUAL) && \ | 
| 687 | r->rod > 0.0 && \ | 
| 688 | weaksrcmat(source[r->rsrc].so)) | 
| 689 |  | 
| 690 | /* wrongsource * | 
| 691 | * | 
| 692 | * This source is the wrong source (ie. overcounted) if we are | 
| 693 | * aimed to a different source than the one we hit and the one | 
| 694 | * we hit is not an illum that should be passed. | 
| 695 | */ | 
| 696 |  | 
| 697 | #define  wrongsource(m, r)      (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \ | 
| 698 | (m->otype!=MAT_ILLUM || illumblock(m,r))) | 
| 699 |  | 
| 700 | /* distglow * | 
| 701 | * | 
| 702 | * A distant glow is an object that sometimes acts as a light source, | 
| 703 | * but is too far away from the test point to be one in this case. | 
| 704 | * (Glows with negative radii should NEVER participate in illumination.) | 
| 705 | */ | 
| 706 |  | 
| 707 | #define  distglow(m, r, d)      (m->otype==MAT_GLOW && \ | 
| 708 | m->oargs.farg[3] >= -FTINY && \ | 
| 709 | d > m->oargs.farg[3]) | 
| 710 |  | 
| 711 | /* badcomponent * | 
| 712 | * | 
| 713 | * We must avoid counting light sources in the ambient calculation, | 
| 714 | * since the direct component is handled separately.  Therefore, any | 
| 715 | * ambient ray which hits an active light source must be discarded. | 
| 716 | * The same is true for stray specular samples, since the specular | 
| 717 | * contribution from light sources is calculated separately. | 
| 718 | */ | 
| 719 | /* PMAP: Also avoid counting sources via transferred ambient rays (e.g. | 
| 720 | * through glass) when photon mapping is enabled, as these indirect | 
| 721 | * components are already accounted for. | 
| 722 | */ | 
| 723 | #define  badcomponent(m, r)   (srcRayInPmap(r) || \ | 
| 724 | (r->crtype&(AMBIENT|SPECULAR) && \ | 
| 725 | !(r->crtype&SHADOW || r->rod < 0.0 || \ | 
| 726 | /* not 100% correct */  distglow(m, r, r->rot)))) | 
| 727 |  | 
| 728 | /* passillum * | 
| 729 | * | 
| 730 | * An illum passes to another material type when we didn't hit it | 
| 731 | * on purpose (as part of a direct calculation), or it is relaying | 
| 732 | * a virtual light source. | 
| 733 | */ | 
| 734 |  | 
| 735 | #define  passillum(m, r)        (m->otype==MAT_ILLUM && \ | 
| 736 | (r->rsrc<0 || source[r->rsrc].so!=r->ro || \ | 
| 737 | source[r->rsrc].sflags&SVIRTUAL)) | 
| 738 |  | 
| 739 | /* srcignore * | 
| 740 | * | 
| 741 | * The -dv flag is normally on for sources to be visible. | 
| 742 | */ | 
| 743 |  | 
| 744 | #define  srcignore(m, r)        !(directvis || r->crtype&SHADOW || \ | 
| 745 | distglow(m, r, raydist(r,PRIMARY))) | 
| 746 |  | 
| 747 |  | 
| 748 | int | 
| 749 | m_light(                                /* ray hit a light source */ | 
| 750 | OBJREC  *m, | 
| 751 | RAY  *r | 
| 752 | ) | 
| 753 | { | 
| 754 | /* check for over-counting */ | 
| 755 | if (badcomponent(m, r)) { | 
| 756 | scolorblack(r->rcoef); | 
| 757 | return(1); | 
| 758 | } | 
| 759 | if (wrongsource(m, r)) { | 
| 760 | scolorblack(r->rcoef); | 
| 761 | return(1); | 
| 762 | } | 
| 763 | /* check for passed illum */ | 
| 764 | if (passillum(m, r)) { | 
| 765 | if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) | 
| 766 | return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0]))); | 
| 767 | raytrans(r); | 
| 768 | return(1); | 
| 769 | } | 
| 770 | /* check for invisibility */ | 
| 771 | if (srcignore(m, r)) { | 
| 772 | scolorblack(r->rcoef); | 
| 773 | return(1); | 
| 774 | } | 
| 775 | /* otherwise treat as source */ | 
| 776 | if (r->rod < 0.0) {                     /* check for behind */ | 
| 777 | if (!backvis) | 
| 778 | raytrans(r);            /* used to return black */ | 
| 779 | return(1); | 
| 780 | } | 
| 781 | /* check for outside spot */ | 
| 782 | if (m->otype==MAT_SPOT && spotout(r, makespot(m))) | 
| 783 | return(1); | 
| 784 | /* get distribution pattern */ | 
| 785 | raytexture(r, m->omod); | 
| 786 | /* get source color */ | 
| 787 | setscolor(r->rcol, m->oargs.farg[0], | 
| 788 | m->oargs.farg[1], | 
| 789 | m->oargs.farg[2]); | 
| 790 | /* modify value */ | 
| 791 | smultscolor(r->rcol, r->pcol); | 
| 792 | return(1); | 
| 793 | } |