| 1 |
< |
/* Copyright (c) 1993 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) |
| 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 |
|
|
| 338 |
|
source[scp->sno].sflags & SFOLLOW )) { |
| 339 |
|
/* follow entire path */ |
| 340 |
|
raycont(&sr); |
| 341 |
+ |
rayparticipate(&sr); |
| 342 |
|
if (trace != NULL) |
| 343 |
|
(*trace)(&sr); /* trace execution */ |
| 344 |
|
if (bright(sr.rcol) <= FTINY) |
| 374 |
|
} |
| 375 |
|
|
| 376 |
|
|
| 377 |
+ |
srcscatter(r) /* compute source scattering into ray */ |
| 378 |
+ |
register RAY *r; |
| 379 |
+ |
{ |
| 380 |
+ |
int oldsampndx; |
| 381 |
+ |
int nsamps; |
| 382 |
+ |
RAY sr; |
| 383 |
+ |
SRCINDEX si; |
| 384 |
+ |
double t, lastt, d; |
| 385 |
+ |
COLOR cumval, ctmp; |
| 386 |
+ |
int i, j; |
| 387 |
+ |
|
| 388 |
+ |
if (r->slights == NULL || r->slights[0] == 0 |
| 389 |
+ |
|| r->gecc >= 1.-FTINY || r->rot >= FHUGE) |
| 390 |
+ |
return; |
| 391 |
+ |
if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) |
| 392 |
+ |
nsamps = 1; |
| 393 |
+ |
#if MAXSSAMP |
| 394 |
+ |
else if (nsamps > MAXSSAMP) |
| 395 |
+ |
nsamps = MAXSSAMP; |
| 396 |
+ |
#endif |
| 397 |
+ |
oldsampndx = samplendx; |
| 398 |
+ |
samplendx = random()&0x7fff; /* randomize */ |
| 399 |
+ |
for (i = r->slights[0]; i > 0; i--) { /* for each source */ |
| 400 |
+ |
setcolor(cumval, 0., 0., 0.); |
| 401 |
+ |
lastt = r->rot; |
| 402 |
+ |
for (j = nsamps; j-- > 0; ) { /* for each sample position */ |
| 403 |
+ |
samplendx++; |
| 404 |
+ |
t = r->rot * (j+frandom())/nsamps; |
| 405 |
+ |
sr.rorg[0] = r->rorg[0] + r->rdir[0]*t; |
| 406 |
+ |
sr.rorg[1] = r->rorg[1] + r->rdir[1]*t; |
| 407 |
+ |
sr.rorg[2] = r->rorg[2] + r->rdir[2]*t; |
| 408 |
+ |
sr.rmax = 0.; |
| 409 |
+ |
initsrcindex(&si); /* sample ray to this source */ |
| 410 |
+ |
si.sn = r->slights[i]; |
| 411 |
+ |
nopart(&si, &sr); |
| 412 |
+ |
if (!srcray(&sr, NULL, &si) || |
| 413 |
+ |
sr.rsrc != r->slights[i]) |
| 414 |
+ |
continue; /* no path */ |
| 415 |
+ |
copycolor(sr.cext, r->cext); |
| 416 |
+ |
sr.albedo = r->albedo; |
| 417 |
+ |
sr.gecc = r->gecc; |
| 418 |
+ |
rayvalue(&sr); /* eval. source ray */ |
| 419 |
+ |
if (bright(sr.rcol) <= FTINY) |
| 420 |
+ |
continue; |
| 421 |
+ |
/* compute fall-off */ |
| 422 |
+ |
d = lastt - t; |
| 423 |
+ |
setcolor(ctmp, 1.-d*colval(r->cext,RED), |
| 424 |
+ |
1.-d*colval(r->cext,GRN), |
| 425 |
+ |
1.-d*colval(r->cext,BLU)); |
| 426 |
+ |
multcolor(cumval, ctmp); |
| 427 |
+ |
lastt = t; |
| 428 |
+ |
if (r->gecc <= FTINY) /* compute P(theta) */ |
| 429 |
+ |
d = 1.; |
| 430 |
+ |
else { |
| 431 |
+ |
d = DOT(r->rdir, sr.rdir); |
| 432 |
+ |
d = sqrt(1. + r->gecc*r->gecc - 2.*r->gecc*d); |
| 433 |
+ |
d = (1. - r->gecc*r->gecc) / (d*d*d); |
| 434 |
+ |
} |
| 435 |
+ |
/* other factors */ |
| 436 |
+ |
d *= si.dom * r->albedo * r->rot / (4.*PI*nsamps); |
| 437 |
+ |
multcolor(sr.rcol, r->cext); |
| 438 |
+ |
scalecolor(sr.rcol, d); |
| 439 |
+ |
addcolor(cumval, sr.rcol); |
| 440 |
+ |
} |
| 441 |
+ |
/* final fall-off */ |
| 442 |
+ |
setcolor(ctmp, 1.-lastt*colval(r->cext,RED), |
| 443 |
+ |
1.-lastt*colval(r->cext,GRN), |
| 444 |
+ |
1.-lastt*colval(r->cext,BLU)); |
| 445 |
+ |
multcolor(cumval, ctmp); |
| 446 |
+ |
addcolor(r->rcol, cumval); /* sum into ray result */ |
| 447 |
+ |
} |
| 448 |
+ |
samplendx = oldsampndx; |
| 449 |
+ |
} |
| 450 |
+ |
|
| 451 |
+ |
|
| 452 |
|
/**************************************************************** |
| 453 |
|
* The following macros were separated from the m_light() routine |
| 454 |
|
* because they are very nasty and difficult to understand. |
| 490 |
|
* (Glows with negative radii should NEVER participate in illumination.) |
| 491 |
|
*/ |
| 492 |
|
|
| 493 |
< |
#define distglow(m, r) (m->otype==MAT_GLOW && \ |
| 493 |
> |
#define distglow(m, r, d) (m->otype==MAT_GLOW && \ |
| 494 |
|
m->oargs.farg[3] >= -FTINY && \ |
| 495 |
< |
r->rot > m->oargs.farg[3]) |
| 495 |
> |
d > m->oargs.farg[3]) |
| 496 |
|
|
| 497 |
|
/* badcomponent * |
| 498 |
|
* |
| 505 |
|
|
| 506 |
|
#define badcomponent(m, r) (r->crtype&(AMBIENT|SPECULAR) && \ |
| 507 |
|
!(r->crtype&SHADOW || r->rod < 0.0 || \ |
| 508 |
< |
distglow(m, r))) |
| 508 |
> |
/* not 100% correct */ distglow(m, r, r->rot))) |
| 509 |
|
|
| 510 |
|
/* passillum * |
| 511 |
|
* |
| 523 |
|
* The -dv flag is normally on for sources to be visible. |
| 524 |
|
*/ |
| 525 |
|
|
| 526 |
< |
#define srcignore(m, r) (!directvis && !(r->crtype&SHADOW) && \ |
| 527 |
< |
!distglow(m, r)) |
| 526 |
> |
#define srcignore(m, r) !(directvis || r->crtype&SHADOW || \ |
| 527 |
> |
distglow(m, r, raydist(r,PRIMARY))) |
| 528 |
|
|
| 529 |
|
|
| 530 |
|
m_light(m, r) /* ray hit a light source */ |
| 533 |
|
{ |
| 534 |
|
/* check for over-counting */ |
| 535 |
|
if (badcomponent(m, r)) |
| 536 |
< |
return; |
| 537 |
< |
if (wrongsource(m,r)) |
| 538 |
< |
return; |
| 536 |
> |
return(1); |
| 537 |
> |
if (wrongsource(m, r)) |
| 538 |
> |
return(1); |
| 539 |
|
/* check for passed illum */ |
| 540 |
|
if (passillum(m, r)) { |
| 541 |
< |
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
| 542 |
< |
raytrans(r); |
| 543 |
< |
else |
| 544 |
< |
rayshade(r, modifier(m->oargs.sarg[0])); |
| 450 |
< |
return; |
| 541 |
> |
if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) |
| 542 |
> |
return(rayshade(r, modifier(m->oargs.sarg[0]))); |
| 543 |
> |
raytrans(r); |
| 544 |
> |
return(1); |
| 545 |
|
} |
| 546 |
|
/* otherwise treat as source */ |
| 547 |
|
/* check for behind */ |
| 548 |
|
if (r->rod < 0.0) |
| 549 |
< |
return; |
| 549 |
> |
return(1); |
| 550 |
|
/* check for invisibility */ |
| 551 |
|
if (srcignore(m, r)) |
| 552 |
< |
return; |
| 552 |
> |
return(1); |
| 553 |
|
/* check for outside spot */ |
| 554 |
< |
if (m->otype==MAT_SPOT && spotout(r, makespot(m), r->rot>=FHUGE)) |
| 555 |
< |
return; |
| 554 |
> |
if (m->otype==MAT_SPOT && spotout(r, makespot(m))) |
| 555 |
> |
return(1); |
| 556 |
|
/* get distribution pattern */ |
| 557 |
|
raytexture(r, m->omod); |
| 558 |
|
/* get source color */ |
| 561 |
|
m->oargs.farg[2]); |
| 562 |
|
/* modify value */ |
| 563 |
|
multcolor(r->rcol, r->pcol); |
| 564 |
+ |
return(1); |
| 565 |
|
} |