| 1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1995 Regents of the University of California */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
| 18 |
|
|
| 19 |
|
#include "source.h" |
| 20 |
|
|
| 21 |
+ |
#include "random.h" |
| 22 |
+ |
|
| 23 |
+ |
extern double ssampdist; /* scatter sampling distance */ |
| 24 |
+ |
|
| 25 |
+ |
#ifndef MAXSSAMP |
| 26 |
+ |
#define MAXSSAMP 16 /* maximum samples per ray */ |
| 27 |
+ |
#endif |
| 28 |
+ |
|
| 29 |
|
/* |
| 30 |
|
* Structures used by direct() |
| 31 |
|
*/ |
| 76 |
|
o->otype != OBJ_SOURCE && |
| 77 |
|
m->oargs.farg[3] <= FTINY) |
| 78 |
|
continue; /* don't bother */ |
| 79 |
+ |
if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY && |
| 80 |
+ |
m->oargs.farg[2] <= FTINY) |
| 81 |
+ |
continue; /* don't bother */ |
| 82 |
|
|
| 83 |
|
if (sfun[o->otype].of == NULL || |
| 84 |
|
sfun[o->otype].of->setsrc == NULL) |
| 92 |
|
if (m->otype == MAT_GLOW) { |
| 93 |
|
source[ns].sflags |= SPROX; |
| 94 |
|
source[ns].sl.prox = m->oargs.farg[3]; |
| 95 |
< |
if (o->otype == OBJ_SOURCE) |
| 95 |
> |
if (source[ns].sflags & SDISTANT) |
| 96 |
|
source[ns].sflags |= SSKIP; |
| 97 |
|
} else if (m->otype == MAT_SPOT) { |
| 98 |
|
source[ns].sflags |= SSPOT; |
| 139 |
|
sr->rsrc = si->sn; /* remember source */ |
| 140 |
|
srcp = source + si->sn; |
| 141 |
|
if (srcp->sflags & SDISTANT) { |
| 142 |
< |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s, 1)) |
| 142 |
> |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) |
| 143 |
|
continue; |
| 144 |
|
return(1); /* sample OK */ |
| 145 |
|
} |
| 149 |
|
continue; |
| 150 |
|
/* check angle */ |
| 151 |
|
if (srcp->sflags & SSPOT) { |
| 152 |
< |
if (spotout(sr, srcp->sl.s, 0)) |
| 152 |
> |
if (spotout(sr, srcp->sl.s)) |
| 153 |
|
continue; |
| 154 |
|
/* adjust solid angle */ |
| 155 |
|
si->dom *= d*d; |
| 163 |
|
|
| 164 |
|
|
| 165 |
|
srcvalue(r) /* punch ray to source and compute value */ |
| 166 |
< |
RAY *r; |
| 166 |
> |
register RAY *r; |
| 167 |
|
{ |
| 168 |
|
register SRCREC *sp; |
| 169 |
|
|
| 172 |
|
/* check intersection */ |
| 173 |
|
if (!(*ofun[sp->so->otype].funp)(sp->so, r)) |
| 174 |
|
return; |
| 175 |
< |
raycont(r); /* compute contribution */ |
| 175 |
> |
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
| 176 |
> |
goto nomat; |
| 177 |
> |
rayparticipate(r); |
| 178 |
|
return; |
| 179 |
|
} |
| 180 |
|
/* compute intersection */ |
| 182 |
|
(*ofun[sp->so->otype].funp)(sp->so, r)) { |
| 183 |
|
if (sp->sa.success >= 0) |
| 184 |
|
sp->sa.success++; |
| 185 |
< |
raycont(r); /* compute contribution */ |
| 185 |
> |
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
| 186 |
> |
goto nomat; |
| 187 |
> |
rayparticipate(r); |
| 188 |
|
return; |
| 189 |
|
} |
| 190 |
+ |
/* we missed our mark! */ |
| 191 |
|
if (sp->sa.success < 0) |
| 192 |
|
return; /* bitched already */ |
| 193 |
|
sp->sa.success -= AIMREQT; |
| 196 |
|
sprintf(errmsg, "aiming failure for light source \"%s\"", |
| 197 |
|
sp->so->oname); |
| 198 |
|
error(WARNING, errmsg); /* issue warning */ |
| 199 |
+ |
return; |
| 200 |
+ |
nomat: |
| 201 |
+ |
objerror(r->ro, USER, "material not found"); |
| 202 |
|
} |
| 203 |
|
|
| 204 |
|
|
| 227 |
|
} |
| 228 |
|
|
| 229 |
|
if (r->ro != NULL) { |
| 230 |
+ |
r->robj = objndx(r->ro); |
| 231 |
|
for (i = 0; i < 3; i++) |
| 232 |
|
r->ron[i] = -r->rdir[i]; |
| 233 |
|
r->rod = 1.0; |
| 256 |
|
char *p; /* data for f */ |
| 257 |
|
{ |
| 258 |
|
extern int (*trace)(); |
| 239 |
– |
extern double pow(); |
| 259 |
|
register int sn; |
| 260 |
+ |
register CONTRIB *scp; |
| 261 |
|
SRCINDEX si; |
| 262 |
|
int nshadcheck, ncnts; |
| 263 |
|
int nhits; |
| 279 |
|
error(SYSTEM, "out of memory in direct"); |
| 280 |
|
} |
| 281 |
|
cntord[sn].sndx = sn; |
| 282 |
< |
srccnt[sn].sno = sr.rsrc; |
| 282 |
> |
scp = srccnt + sn; |
| 283 |
> |
scp->sno = sr.rsrc; |
| 284 |
|
/* compute coefficient */ |
| 285 |
< |
(*f)(srccnt[sn].coef, p, sr.rdir, si.dom); |
| 286 |
< |
cntord[sn].brt = bright(srccnt[sn].coef); |
| 285 |
> |
(*f)(scp->coef, p, sr.rdir, si.dom); |
| 286 |
> |
cntord[sn].brt = bright(scp->coef); |
| 287 |
|
if (cntord[sn].brt <= 0.0) |
| 288 |
|
continue; |
| 289 |
< |
VCOPY(srccnt[sn].dir, sr.rdir); |
| 289 |
> |
VCOPY(scp->dir, sr.rdir); |
| 290 |
|
/* compute potential */ |
| 291 |
|
sr.revf = srcvalue; |
| 292 |
|
rayvalue(&sr); |
| 293 |
< |
copycolor(srccnt[sn].val, sr.rcol); |
| 294 |
< |
multcolor(srccnt[sn].val, srccnt[sn].coef); |
| 295 |
< |
cntord[sn].brt = bright(srccnt[sn].val); |
| 293 |
> |
copycolor(scp->val, sr.rcol); |
| 294 |
> |
multcolor(scp->val, scp->coef); |
| 295 |
> |
cntord[sn].brt = bright(scp->val); |
| 296 |
|
} |
| 297 |
|
/* sort contributions */ |
| 298 |
|
qsort(cntord, sn, sizeof(CNTPTR), cntcmp); |
| 319 |
|
/* modify threshold */ |
| 320 |
|
ourthresh = shadthresh / r->rweight; |
| 321 |
|
/* test for shadows */ |
| 322 |
< |
nhits = 0; |
| 323 |
< |
for (sn = 0; sn < ncnts; sn++) { |
| 322 |
> |
for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts; |
| 323 |
> |
hwt += (double)source[scp->sno].nhits / |
| 324 |
> |
(double)source[scp->sno].ntests, |
| 325 |
> |
sn++) { |
| 326 |
|
/* check threshold */ |
| 327 |
|
if ((sn+nshadcheck>=ncnts ? cntord[sn].brt : |
| 328 |
|
cntord[sn].brt-cntord[sn+nshadcheck].brt) |
| 329 |
|
< ourthresh*bright(r->rcol)) |
| 330 |
|
break; |
| 331 |
+ |
scp = srccnt + cntord[sn].sndx; |
| 332 |
|
/* test for hit */ |
| 333 |
|
rayorigin(&sr, r, SHADOW, 1.0); |
| 334 |
< |
VCOPY(sr.rdir, srccnt[cntord[sn].sndx].dir); |
| 335 |
< |
sr.rsrc = srccnt[cntord[sn].sndx].sno; |
| 336 |
< |
source[sr.rsrc].ntests++; /* keep statistics */ |
| 334 |
> |
VCOPY(sr.rdir, scp->dir); |
| 335 |
> |
sr.rsrc = scp->sno; |
| 336 |
> |
source[scp->sno].ntests++; /* keep statistics */ |
| 337 |
|
if (localhit(&sr, &thescene) && |
| 338 |
< |
( sr.ro != source[sr.rsrc].so || |
| 339 |
< |
source[sr.rsrc].sflags & SFOLLOW )) { |
| 338 |
> |
( sr.ro != source[scp->sno].so || |
| 339 |
> |
source[scp->sno].sflags & SFOLLOW )) { |
| 340 |
|
/* follow entire path */ |
| 341 |
|
raycont(&sr); |
| 342 |
+ |
rayparticipate(&sr); |
| 343 |
|
if (trace != NULL) |
| 344 |
|
(*trace)(&sr); /* trace execution */ |
| 345 |
|
if (bright(sr.rcol) <= FTINY) |
| 346 |
|
continue; /* missed! */ |
| 347 |
< |
copycolor(srccnt[cntord[sn].sndx].val, sr.rcol); |
| 348 |
< |
multcolor(srccnt[cntord[sn].sndx].val, |
| 324 |
< |
srccnt[cntord[sn].sndx].coef); |
| 347 |
> |
copycolor(scp->val, sr.rcol); |
| 348 |
> |
multcolor(scp->val, scp->coef); |
| 349 |
|
} |
| 350 |
|
/* add contribution if hit */ |
| 351 |
< |
addcolor(r->rcol, srccnt[cntord[sn].sndx].val); |
| 351 |
> |
addcolor(r->rcol, scp->val); |
| 352 |
|
nhits++; |
| 353 |
< |
source[sr.rsrc].nhits++; |
| 353 |
> |
source[scp->sno].nhits++; |
| 354 |
|
} |
| 355 |
< |
/* surface hit rate */ |
| 356 |
< |
if (sn > 0) |
| 357 |
< |
hwt = (double)nhits / (double)sn; |
| 355 |
> |
/* source hit rate */ |
| 356 |
> |
if (hwt > FTINY) |
| 357 |
> |
hwt = (double)nhits / hwt; |
| 358 |
|
else |
| 359 |
|
hwt = 0.5; |
| 360 |
|
#ifdef DEBUG |
| 361 |
< |
sprintf(errmsg, "%d tested, %d untested, %f hit rate\n", |
| 361 |
> |
sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n", |
| 362 |
|
sn, ncnts-sn, hwt); |
| 363 |
|
eputs(errmsg); |
| 364 |
|
#endif |
| 365 |
|
/* add in untested sources */ |
| 366 |
|
for ( ; sn < ncnts; sn++) { |
| 367 |
< |
sr.rsrc = srccnt[cntord[sn].sndx].sno; |
| 368 |
< |
prob = hwt * (double)source[sr.rsrc].nhits / |
| 369 |
< |
(double)source[sr.rsrc].ntests; |
| 370 |
< |
scalecolor(srccnt[cntord[sn].sndx].val, prob); |
| 371 |
< |
addcolor(r->rcol, srccnt[cntord[sn].sndx].val); |
| 367 |
> |
scp = srccnt + cntord[sn].sndx; |
| 368 |
> |
prob = hwt * (double)source[scp->sno].nhits / |
| 369 |
> |
(double)source[scp->sno].ntests; |
| 370 |
> |
if (prob > 1.0) |
| 371 |
> |
prob = 1.0; |
| 372 |
> |
scalecolor(scp->val, prob); |
| 373 |
> |
addcolor(r->rcol, scp->val); |
| 374 |
|
} |
| 375 |
|
} |
| 376 |
|
|
| 377 |
|
|
| 378 |
+ |
srcscatter(r) /* compute source scattering into ray */ |
| 379 |
+ |
register RAY *r; |
| 380 |
+ |
{ |
| 381 |
+ |
int oldsampndx; |
| 382 |
+ |
int nsamps; |
| 383 |
+ |
RAY sr; |
| 384 |
+ |
SRCINDEX si; |
| 385 |
+ |
double t, d; |
| 386 |
+ |
double re, ge, be; |
| 387 |
+ |
COLOR cvext; |
| 388 |
+ |
int i, j; |
| 389 |
+ |
|
| 390 |
+ |
if (r->slights == NULL || r->slights[0] == 0 |
| 391 |
+ |
|| r->gecc >= 1.-FTINY || r->rot >= FHUGE) |
| 392 |
+ |
return; |
| 393 |
+ |
if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) |
| 394 |
+ |
nsamps = 1; |
| 395 |
+ |
#if MAXSSAMP |
| 396 |
+ |
else if (nsamps > MAXSSAMP) |
| 397 |
+ |
nsamps = MAXSSAMP; |
| 398 |
+ |
#endif |
| 399 |
+ |
oldsampndx = samplendx; |
| 400 |
+ |
samplendx = random()&0x7fff; /* randomize */ |
| 401 |
+ |
for (i = r->slights[0]; i > 0; i--) { /* for each source */ |
| 402 |
+ |
for (j = 0; j < nsamps; j++) { /* for each sample position */ |
| 403 |
+ |
samplendx++; |
| 404 |
+ |
t = r->rot * (j+frandom())/nsamps; |
| 405 |
+ |
/* extinction */ |
| 406 |
+ |
re = t*colval(r->cext,RED); |
| 407 |
+ |
ge = t*colval(r->cext,GRN); |
| 408 |
+ |
be = t*colval(r->cext,BLU); |
| 409 |
+ |
setcolor(cvext, re > 92. ? 0. : exp(-re), |
| 410 |
+ |
ge > 92. ? 0. : exp(-ge), |
| 411 |
+ |
be > 92. ? 0. : exp(-be)); |
| 412 |
+ |
if (intens(cvext) <= FTINY) |
| 413 |
+ |
break; /* too far away */ |
| 414 |
+ |
sr.rorg[0] = r->rorg[0] + r->rdir[0]*t; |
| 415 |
+ |
sr.rorg[1] = r->rorg[1] + r->rdir[1]*t; |
| 416 |
+ |
sr.rorg[2] = r->rorg[2] + r->rdir[2]*t; |
| 417 |
+ |
sr.rmax = 0.; |
| 418 |
+ |
initsrcindex(&si); /* sample ray to this source */ |
| 419 |
+ |
si.sn = r->slights[i]; |
| 420 |
+ |
nopart(&si, &sr); |
| 421 |
+ |
if (!srcray(&sr, NULL, &si) || |
| 422 |
+ |
sr.rsrc != r->slights[i]) |
| 423 |
+ |
continue; /* no path */ |
| 424 |
+ |
copycolor(sr.cext, r->cext); |
| 425 |
+ |
copycolor(sr.albedo, r->albedo); |
| 426 |
+ |
sr.gecc = r->gecc; |
| 427 |
+ |
sr.slights = r->slights; |
| 428 |
+ |
rayvalue(&sr); /* eval. source ray */ |
| 429 |
+ |
if (bright(sr.rcol) <= FTINY) |
| 430 |
+ |
continue; |
| 431 |
+ |
if (r->gecc <= FTINY) /* compute P(theta) */ |
| 432 |
+ |
d = 1.; |
| 433 |
+ |
else { |
| 434 |
+ |
d = DOT(r->rdir, sr.rdir); |
| 435 |
+ |
d = 1. + r->gecc*r->gecc - 2.*r->gecc*d; |
| 436 |
+ |
d = (1. - r->gecc*r->gecc) / (d*sqrt(d)); |
| 437 |
+ |
} |
| 438 |
+ |
/* other factors */ |
| 439 |
+ |
d *= si.dom * r->rot / (4.*PI*nsamps); |
| 440 |
+ |
multcolor(sr.rcol, r->cext); |
| 441 |
+ |
multcolor(sr.rcol, r->albedo); |
| 442 |
+ |
scalecolor(sr.rcol, d); |
| 443 |
+ |
multcolor(sr.rcol, cvext); |
| 444 |
+ |
addcolor(r->rcol, sr.rcol); /* add it in */ |
| 445 |
+ |
} |
| 446 |
+ |
} |
| 447 |
+ |
samplendx = oldsampndx; |
| 448 |
+ |
} |
| 449 |
+ |
|
| 450 |
+ |
|
| 451 |
|
/**************************************************************** |
| 452 |
|
* The following macros were separated from the m_light() routine |
| 453 |
|
* because they are very nasty and difficult to understand. |
| 454 |
|
*/ |
| 455 |
|
|
| 456 |
< |
/* wrongillum * |
| 456 |
> |
/* illumblock * |
| 457 |
|
* |
| 458 |
|
* We cannot allow an illum to pass to another illum, because that |
| 459 |
|
* would almost certainly constitute overcounting. |
| 460 |
|
* However, we do allow an illum to pass to another illum |
| 461 |
|
* that is actually going to relay to a virtual light source. |
| 462 |
+ |
* We also prevent an illum from passing to a glow; this provides a |
| 463 |
+ |
* convenient mechanism for defining detailed light source |
| 464 |
+ |
* geometry behind (or inside) an effective radiator. |
| 465 |
|
*/ |
| 466 |
|
|
| 467 |
< |
#define wrongillum(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ |
| 468 |
< |
objptr(source[r->rsrc].so->omod)->otype==MAT_ILLUM) |
| 467 |
> |
static int weaksrcmod(obj) int obj; /* efficiency booster function */ |
| 468 |
> |
{register OBJREC *o = objptr(obj); |
| 469 |
> |
return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} |
| 470 |
|
|
| 471 |
+ |
#define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ |
| 472 |
+ |
r->rod > 0.0 && \ |
| 473 |
+ |
weaksrcmod(source[r->rsrc].so->omod)) |
| 474 |
+ |
|
| 475 |
|
/* wrongsource * |
| 476 |
|
* |
| 477 |
|
* This source is the wrong source (ie. overcounted) if we are |
| 478 |
|
* aimed to a different source than the one we hit and the one |
| 479 |
< |
* we hit is not an illum which should be passed. |
| 479 |
> |
* we hit is not an illum that should be passed. |
| 480 |
|
*/ |
| 481 |
|
|
| 482 |
|
#define wrongsource(m, r) (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \ |
| 483 |
< |
(m->otype!=MAT_ILLUM || wrongillum(m,r))) |
| 483 |
> |
(m->otype!=MAT_ILLUM || illumblock(m,r))) |
| 484 |
|
|
| 485 |
|
/* distglow * |
| 486 |
|
* |
| 487 |
|
* A distant glow is an object that sometimes acts as a light source, |
| 488 |
|
* but is too far away from the test point to be one in this case. |
| 489 |
+ |
* (Glows with negative radii should NEVER participate in illumination.) |
| 490 |
|
*/ |
| 491 |
|
|
| 492 |
< |
#define distglow(m, r) (m->otype==MAT_GLOW && \ |
| 493 |
< |
r->rot > m->oargs.farg[3]) |
| 492 |
> |
#define distglow(m, r, d) (m->otype==MAT_GLOW && \ |
| 493 |
> |
m->oargs.farg[3] >= -FTINY && \ |
| 494 |
> |
d > m->oargs.farg[3]) |
| 495 |
|
|
| 496 |
|
/* badcomponent * |
| 497 |
|
* |
| 504 |
|
|
| 505 |
|
#define badcomponent(m, r) (r->crtype&(AMBIENT|SPECULAR) && \ |
| 506 |
|
!(r->crtype&SHADOW || r->rod < 0.0 || \ |
| 507 |
< |
distglow(m, r))) |
| 507 |
> |
/* not 100% correct */ distglow(m, r, r->rot))) |
| 508 |
|
|
| 400 |
– |
/* overcount * |
| 401 |
– |
* |
| 402 |
– |
* All overcounting possibilities are contained here. |
| 403 |
– |
*/ |
| 404 |
– |
|
| 405 |
– |
#define overcount(m, r) (badcomponent(m,r) || wrongsource(m,r)) |
| 406 |
– |
|
| 509 |
|
/* passillum * |
| 510 |
|
* |
| 511 |
|
* An illum passes to another material type when we didn't hit it |
| 519 |
|
|
| 520 |
|
/* srcignore * |
| 521 |
|
* |
| 522 |
< |
* The -di flag renders light sources invisible, and here is the test. |
| 522 |
> |
* The -dv flag is normally on for sources to be visible. |
| 523 |
|
*/ |
| 524 |
|
|
| 525 |
< |
#define srcignore(m, r) (directinvis && !(r->crtype&SHADOW) && \ |
| 526 |
< |
!distglow(m, r)) |
| 525 |
> |
#define srcignore(m, r) !(directvis || r->crtype&SHADOW || \ |
| 526 |
> |
distglow(m, r, raydist(r,PRIMARY))) |
| 527 |
|
|
| 528 |
|
|
| 529 |
|
m_light(m, r) /* ray hit a light source */ |
| 531 |
|
register RAY *r; |
| 532 |
|
{ |
| 533 |
|
/* check for over-counting */ |
| 534 |
< |
if (overcount(m, r)) |
| 535 |
< |
return; |
| 534 |
> |
if (badcomponent(m, r)) |
| 535 |
> |
return(1); |
| 536 |
> |
if (wrongsource(m, r)) |
| 537 |
> |
return(1); |
| 538 |
|
/* check for passed illum */ |
| 539 |
|
if (passillum(m, r)) { |
| 540 |
< |
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
| 541 |
< |
raytrans(r); |
| 542 |
< |
else |
| 543 |
< |
rayshade(r, modifier(m->oargs.sarg[0])); |
| 440 |
< |
return; |
| 540 |
> |
if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) |
| 541 |
> |
return(rayshade(r, modifier(m->oargs.sarg[0]))); |
| 542 |
> |
raytrans(r); |
| 543 |
> |
return(1); |
| 544 |
|
} |
| 545 |
|
/* otherwise treat as source */ |
| 546 |
|
/* check for behind */ |
| 547 |
|
if (r->rod < 0.0) |
| 548 |
< |
return; |
| 548 |
> |
return(1); |
| 549 |
|
/* check for invisibility */ |
| 550 |
|
if (srcignore(m, r)) |
| 551 |
< |
return; |
| 551 |
> |
return(1); |
| 552 |
|
/* check for outside spot */ |
| 553 |
< |
if (m->otype==MAT_SPOT && spotout(r, (SPOT *)m->os, r->rot>=FHUGE)) |
| 554 |
< |
return; |
| 553 |
> |
if (m->otype==MAT_SPOT && spotout(r, makespot(m))) |
| 554 |
> |
return(1); |
| 555 |
|
/* get distribution pattern */ |
| 556 |
|
raytexture(r, m->omod); |
| 557 |
|
/* get source color */ |
| 560 |
|
m->oargs.farg[2]); |
| 561 |
|
/* modify value */ |
| 562 |
|
multcolor(r->rcol, r->pcol); |
| 563 |
+ |
return(1); |
| 564 |
|
} |