--- ray/src/rt/source.c 1995/04/25 19:51:38 2.16 +++ ray/src/rt/source.c 1995/12/09 11:30:19 2.20 @@ -1,4 +1,4 @@ -/* Copyright (c) 1993 Regents of the University of California */ +/* Copyright (c) 1995 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -18,6 +18,10 @@ static char SCCSid[] = "$SunId$ LBL"; #include "source.h" +#include "random.h" + +extern double ssampdist; /* scatter sampling distance */ + /* * Structures used by direct() */ @@ -131,7 +135,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 +145,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 +170,7 @@ register RAY *r; return; if (!rayshade(r, r->ro->omod)) /* compute contribution */ goto nomat; + rayparticipate(r); return; } /* compute intersection */ @@ -175,6 +180,7 @@ register RAY *r; sp->sa.success++; if (!rayshade(r, r->ro->omod)) /* compute contribution */ goto nomat; + rayparticipate(r); return; } /* we missed our mark! */ @@ -329,6 +335,7 @@ char *p; /* data for f */ /* follow entire path */ if (!raycont(&sr)) objerror(sr.ro, USER, "material not found"); + rayparticipate(&sr); if (trace != NULL) (*trace)(&sr); /* trace execution */ if (bright(sr.rcol) <= FTINY) @@ -364,6 +371,80 @@ 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) + return; + if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) + nsamps = 1; + oldsampndx = samplendx; + samplendx = random()&0x7fff; /* randomize */ + initsrcindex(&si); + 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.; + /* sample ray to this source */ + if (si.sp >= si.np-1 || !srcray(&sr, NULL, &si) || + sr.rsrc != r->slights[i]) { + si.sn = r->slights[i]-1; /* reset */ + si.np = 0; + 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. @@ -405,9 +486,9 @@ return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} * (Glows with negative radii should NEVER participate in illumination.) */ -#define distglow(m, r) (m->otype==MAT_GLOW && \ +#define distglow(m, r, d) (m->otype==MAT_GLOW && \ m->oargs.farg[3] >= -FTINY && \ - r->rot > m->oargs.farg[3]) + d > m->oargs.farg[3]) /* badcomponent * * @@ -420,7 +501,7 @@ return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} #define badcomponent(m, r) (r->crtype&(AMBIENT|SPECULAR) && \ !(r->crtype&SHADOW || r->rod < 0.0 || \ - distglow(m, r))) + /* not 100% correct */ distglow(m, r, r->rot))) /* passillum * * @@ -438,8 +519,8 @@ return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} * The -dv flag is normally on for sources to be visible. */ -#define srcignore(m, r) (!directvis && !(r->crtype&SHADOW) && \ - !distglow(m, r)) +#define srcignore(m, r) !(directvis || r->crtype&SHADOW || \ + distglow(m, r, raydist(r,PRIMARY))) m_light(m, r) /* ray hit a light source */ @@ -449,7 +530,7 @@ register RAY *r; /* check for over-counting */ if (badcomponent(m, r)) return(1); - if (wrongsource(m,r)) + if (wrongsource(m, r)) return(1); /* check for passed illum */ if (passillum(m, r)) { @@ -466,7 +547,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);