| 18 |
|
|
| 19 |
|
#include "source.h" |
| 20 |
|
|
| 21 |
+ |
#include "random.h" |
| 22 |
+ |
|
| 23 |
+ |
extern double ssampdist; /* scatter sampling distance */ |
| 24 |
+ |
|
| 25 |
|
/* |
| 26 |
|
* Structures used by direct() |
| 27 |
|
*/ |
| 135 |
|
sr->rsrc = si->sn; /* remember source */ |
| 136 |
|
srcp = source + si->sn; |
| 137 |
|
if (srcp->sflags & SDISTANT) { |
| 138 |
< |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s, 1)) |
| 138 |
> |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) |
| 139 |
|
continue; |
| 140 |
|
return(1); /* sample OK */ |
| 141 |
|
} |
| 145 |
|
continue; |
| 146 |
|
/* check angle */ |
| 147 |
|
if (srcp->sflags & SSPOT) { |
| 148 |
< |
if (spotout(sr, srcp->sl.s, 0)) |
| 148 |
> |
if (spotout(sr, srcp->sl.s)) |
| 149 |
|
continue; |
| 150 |
|
/* adjust solid angle */ |
| 151 |
|
si->dom *= d*d; |
| 170 |
|
return; |
| 171 |
|
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
| 172 |
|
goto nomat; |
| 173 |
+ |
rayparticipate(r); |
| 174 |
|
return; |
| 175 |
|
} |
| 176 |
|
/* compute intersection */ |
| 180 |
|
sp->sa.success++; |
| 181 |
|
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
| 182 |
|
goto nomat; |
| 183 |
+ |
rayparticipate(r); |
| 184 |
|
return; |
| 185 |
|
} |
| 186 |
|
/* we missed our mark! */ |
| 335 |
|
/* follow entire path */ |
| 336 |
|
if (!raycont(&sr)) |
| 337 |
|
objerror(sr.ro, USER, "material not found"); |
| 338 |
+ |
rayparticipate(&sr); |
| 339 |
|
if (trace != NULL) |
| 340 |
|
(*trace)(&sr); /* trace execution */ |
| 341 |
|
if (bright(sr.rcol) <= FTINY) |
| 371 |
|
} |
| 372 |
|
|
| 373 |
|
|
| 374 |
+ |
srcscatter(r) /* compute source scattering into ray */ |
| 375 |
+ |
register RAY *r; |
| 376 |
+ |
{ |
| 377 |
+ |
int oldsampndx; |
| 378 |
+ |
int nsamps; |
| 379 |
+ |
RAY sr; |
| 380 |
+ |
SRCINDEX si; |
| 381 |
+ |
double t, lastt, d; |
| 382 |
+ |
COLOR cumval, ctmp; |
| 383 |
+ |
int i, j; |
| 384 |
+ |
|
| 385 |
+ |
if (r->slights == NULL || r->slights[0] == 0 || r->gecc >= 1.-FTINY) |
| 386 |
+ |
return; |
| 387 |
+ |
if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) |
| 388 |
+ |
nsamps = 1; |
| 389 |
+ |
oldsampndx = samplendx; |
| 390 |
+ |
samplendx = random()&0x7fff; /* randomize */ |
| 391 |
+ |
for (i = r->slights[0]; i > 0; i--) { /* for each source */ |
| 392 |
+ |
setcolor(cumval, 0., 0., 0.); |
| 393 |
+ |
lastt = r->rot; |
| 394 |
+ |
for (j = nsamps; j-- > 0; ) { /* for each sample position */ |
| 395 |
+ |
samplendx++; |
| 396 |
+ |
t = r->rot * (j+frandom())/nsamps; |
| 397 |
+ |
sr.rorg[0] = r->rorg[0] + r->rdir[0]*t; |
| 398 |
+ |
sr.rorg[1] = r->rorg[1] + r->rdir[1]*t; |
| 399 |
+ |
sr.rorg[2] = r->rorg[2] + r->rdir[2]*t; |
| 400 |
+ |
sr.rmax = 0.; |
| 401 |
+ |
initsrcindex(&si); /* sample ray to this source */ |
| 402 |
+ |
si.sn = r->slights[i]; |
| 403 |
+ |
nopart(&si, &sr); |
| 404 |
+ |
if (!srcray(&sr, NULL, &si) || |
| 405 |
+ |
sr.rsrc != r->slights[i]) |
| 406 |
+ |
continue; /* no path */ |
| 407 |
+ |
copycolor(sr.cext, r->cext); |
| 408 |
+ |
sr.albedo = r->albedo; |
| 409 |
+ |
sr.gecc = r->gecc; |
| 410 |
+ |
rayvalue(&sr); /* eval. source ray */ |
| 411 |
+ |
if (bright(sr.rcol) <= FTINY) |
| 412 |
+ |
continue; |
| 413 |
+ |
/* compute fall-off */ |
| 414 |
+ |
d = lastt - t; |
| 415 |
+ |
setcolor(ctmp, 1.-d*colval(r->cext,RED), |
| 416 |
+ |
1.-d*colval(r->cext,GRN), |
| 417 |
+ |
1.-d*colval(r->cext,BLU)); |
| 418 |
+ |
multcolor(cumval, ctmp); |
| 419 |
+ |
lastt = t; |
| 420 |
+ |
if (r->gecc <= FTINY) /* compute P(theta) */ |
| 421 |
+ |
d = 1.; |
| 422 |
+ |
else { |
| 423 |
+ |
d = DOT(r->rdir, sr.rdir); |
| 424 |
+ |
d = sqrt(1. + r->gecc*r->gecc - 2.*r->gecc*d); |
| 425 |
+ |
d = (1. - r->gecc*r->gecc) / (d*d*d); |
| 426 |
+ |
} |
| 427 |
+ |
/* other factors */ |
| 428 |
+ |
d *= si.dom * r->albedo * r->rot / (4.*PI*nsamps); |
| 429 |
+ |
multcolor(sr.rcol, r->cext); |
| 430 |
+ |
scalecolor(sr.rcol, d); |
| 431 |
+ |
addcolor(cumval, sr.rcol); |
| 432 |
+ |
} |
| 433 |
+ |
/* final fall-off */ |
| 434 |
+ |
setcolor(ctmp, 1.-lastt*colval(r->cext,RED), |
| 435 |
+ |
1.-lastt*colval(r->cext,GRN), |
| 436 |
+ |
1.-lastt*colval(r->cext,BLU)); |
| 437 |
+ |
multcolor(cumval, ctmp); |
| 438 |
+ |
addcolor(r->rcol, cumval); /* sum into ray result */ |
| 439 |
+ |
} |
| 440 |
+ |
samplendx = oldsampndx; |
| 441 |
+ |
} |
| 442 |
+ |
|
| 443 |
+ |
|
| 444 |
|
/**************************************************************** |
| 445 |
|
* The following macros were separated from the m_light() routine |
| 446 |
|
* because they are very nasty and difficult to understand. |
| 543 |
|
if (srcignore(m, r)) |
| 544 |
|
return(1); |
| 545 |
|
/* check for outside spot */ |
| 546 |
< |
if (m->otype==MAT_SPOT && spotout(r, makespot(m), r->rot>=FHUGE)) |
| 546 |
> |
if (m->otype==MAT_SPOT && spotout(r, makespot(m))) |
| 547 |
|
return(1); |
| 548 |
|
/* get distribution pattern */ |
| 549 |
|
raytexture(r, m->omod); |