--- ray/src/rt/source.c 1995/11/02 17:37:58 2.17 +++ ray/src/rt/source.c 1996/03/21 15:33:09 2.23 @@ -18,6 +18,14 @@ static char SCCSid[] = "$SunId$ LBL"; #include "source.h" +#include "random.h" + +extern double ssampdist; /* scatter sampling distance */ + +#ifndef MAXSSAMP +#define MAXSSAMP 16 /* maximum samples per ray */ +#endif + /* * Structures used by direct() */ @@ -131,7 +139,7 @@ SRCINDEX *si; /* source sample index */ sr->rsrc = si->sn; /* remember source */ srcp = source + si->sn; if (srcp->sflags & SDISTANT) { - if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s, 1)) + if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) continue; return(1); /* sample OK */ } @@ -141,7 +149,7 @@ SRCINDEX *si; /* source sample index */ continue; /* check angle */ if (srcp->sflags & SSPOT) { - if (spotout(sr, srcp->sl.s, 0)) + if (spotout(sr, srcp->sl.s)) continue; /* adjust solid angle */ si->dom *= d*d; @@ -166,6 +174,7 @@ register RAY *r; return; if (!rayshade(r, r->ro->omod)) /* compute contribution */ goto nomat; + rayparticipate(r); return; } /* compute intersection */ @@ -175,6 +184,7 @@ register RAY *r; sp->sa.success++; if (!rayshade(r, r->ro->omod)) /* compute contribution */ goto nomat; + rayparticipate(r); return; } /* we missed our mark! */ @@ -327,8 +337,8 @@ char *p; /* data for f */ ( sr.ro != source[scp->sno].so || source[scp->sno].sflags & SFOLLOW )) { /* follow entire path */ - if (!raycont(&sr)) - objerror(sr.ro, USER, "material not found"); + raycont(&sr); + rayparticipate(&sr); if (trace != NULL) (*trace)(&sr); /* trace execution */ if (bright(sr.rcol) <= FTINY) @@ -364,6 +374,81 @@ char *p; /* data for f */ } +srcscatter(r) /* compute source scattering into ray */ +register RAY *r; +{ + int oldsampndx; + int nsamps; + RAY sr; + SRCINDEX si; + double t, lastt, d; + COLOR cumval, ctmp; + int i, j; + + if (r->slights == NULL || r->slights[0] == 0 + || r->gecc >= 1.-FTINY || r->rot >= FHUGE) + return; + if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) + nsamps = 1; +#if MAXSSAMP + else if (nsamps > MAXSSAMP) + nsamps = MAXSSAMP; +#endif + oldsampndx = samplendx; + samplendx = random()&0x7fff; /* randomize */ + for (i = r->slights[0]; i > 0; i--) { /* for each source */ + setcolor(cumval, 0., 0., 0.); + lastt = r->rot; + for (j = nsamps; j-- > 0; ) { /* for each sample position */ + samplendx++; + t = r->rot * (j+frandom())/nsamps; + sr.rorg[0] = r->rorg[0] + r->rdir[0]*t; + sr.rorg[1] = r->rorg[1] + r->rdir[1]*t; + sr.rorg[2] = r->rorg[2] + r->rdir[2]*t; + sr.rmax = 0.; + initsrcindex(&si); /* sample ray to this source */ + si.sn = r->slights[i]; + nopart(&si, &sr); + if (!srcray(&sr, NULL, &si) || + sr.rsrc != r->slights[i]) + continue; /* no path */ + copycolor(sr.cext, r->cext); + sr.albedo = r->albedo; + sr.gecc = r->gecc; + rayvalue(&sr); /* eval. source ray */ + if (bright(sr.rcol) <= FTINY) + continue; + /* compute fall-off */ + d = lastt - t; + setcolor(ctmp, 1.-d*colval(r->cext,RED), + 1.-d*colval(r->cext,GRN), + 1.-d*colval(r->cext,BLU)); + multcolor(cumval, ctmp); + lastt = t; + if (r->gecc <= FTINY) /* compute P(theta) */ + d = 1.; + else { + d = DOT(r->rdir, sr.rdir); + d = sqrt(1. + r->gecc*r->gecc - 2.*r->gecc*d); + d = (1. - r->gecc*r->gecc) / (d*d*d); + } + /* other factors */ + d *= si.dom * r->albedo * r->rot / (4.*PI*nsamps); + multcolor(sr.rcol, r->cext); + scalecolor(sr.rcol, d); + addcolor(cumval, sr.rcol); + } + /* final fall-off */ + setcolor(ctmp, 1.-lastt*colval(r->cext,RED), + 1.-lastt*colval(r->cext,GRN), + 1.-lastt*colval(r->cext,BLU)); + multcolor(cumval, ctmp); + addcolor(r->rcol, cumval); /* sum into ray result */ + } + samplendx = oldsampndx; +} + + /**************************************************************** * The following macros were separated from the m_light() routine * because they are very nasty and difficult to understand. @@ -466,7 +551,7 @@ register RAY *r; if (srcignore(m, r)) return(1); /* check for outside spot */ - if (m->otype==MAT_SPOT && spotout(r, makespot(m), r->rot>=FHUGE)) + if (m->otype==MAT_SPOT && spotout(r, makespot(m))) return(1); /* get distribution pattern */ raytexture(r, m->omod);