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 |
|
*/ |
174 |
|
return; |
175 |
|
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
176 |
|
goto nomat; |
177 |
+ |
rayparticipate(r); |
178 |
|
return; |
179 |
|
} |
180 |
|
/* compute intersection */ |
184 |
|
sp->sa.success++; |
185 |
|
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
186 |
|
goto nomat; |
187 |
+ |
rayparticipate(r); |
188 |
|
return; |
189 |
|
} |
190 |
|
/* we missed our mark! */ |
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; |
338 |
|
( sr.ro != source[scp->sno].so || |
339 |
|
source[scp->sno].sflags & SFOLLOW )) { |
340 |
|
/* follow entire path */ |
341 |
< |
if (!raycont(&sr)) |
342 |
< |
objerror(sr.ro, USER, "material not found"); |
341 |
> |
raycont(&sr); |
342 |
> |
rayparticipate(&sr); |
343 |
|
if (trace != NULL) |
344 |
|
(*trace)(&sr); /* trace execution */ |
345 |
|
if (bright(sr.rcol) <= FTINY) |
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 |
|
|