--- ray/src/rt/source.c 1993/12/21 15:46:19 2.13 +++ ray/src/rt/source.c 1995/12/17 11:51:49 2.22 @@ -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,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() */ @@ -68,6 +76,9 @@ marksources() /* find and mark source objects */ o->otype != OBJ_SOURCE && m->oargs.farg[3] <= FTINY) continue; /* don't bother */ + if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY && + m->oargs.farg[2] <= FTINY) + continue; /* don't bother */ if (sfun[o->otype].of == NULL || sfun[o->otype].of->setsrc == NULL) @@ -128,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 */ } @@ -138,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; @@ -152,7 +163,7 @@ SRCINDEX *si; /* source sample index */ srcvalue(r) /* punch ray to source and compute value */ -RAY *r; +register RAY *r; { register SRCREC *sp; @@ -161,7 +172,9 @@ RAY *r; /* check intersection */ if (!(*ofun[sp->so->otype].funp)(sp->so, r)) return; - raycont(r); /* compute contribution */ + if (!rayshade(r, r->ro->omod)) /* compute contribution */ + goto nomat; + rayparticipate(r); return; } /* compute intersection */ @@ -169,9 +182,12 @@ RAY *r; (*ofun[sp->so->otype].funp)(sp->so, r)) { if (sp->sa.success >= 0) sp->sa.success++; - raycont(r); /* compute contribution */ + if (!rayshade(r, r->ro->omod)) /* compute contribution */ + goto nomat; + rayparticipate(r); return; } + /* we missed our mark! */ if (sp->sa.success < 0) return; /* bitched already */ sp->sa.success -= AIMREQT; @@ -180,6 +196,9 @@ RAY *r; sprintf(errmsg, "aiming failure for light source \"%s\"", sp->so->oname); error(WARNING, errmsg); /* issue warning */ + return; +nomat: + objerror(r->ro, USER, "material not found"); } @@ -318,7 +337,9 @@ char *p; /* data for f */ ( sr.ro != source[scp->sno].so || source[scp->sno].sflags & SFOLLOW )) { /* follow entire path */ - raycont(&sr); + if (!raycont(&sr)) + objerror(sr.ro, USER, "material not found"); + rayparticipate(&sr); if (trace != NULL) (*trace)(&sr); /* trace execution */ if (bright(sr.rcol) <= FTINY) @@ -354,6 +375,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. @@ -395,9 +491,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 * * @@ -410,7 +506,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 * * @@ -428,8 +524,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 */ @@ -438,27 +534,26 @@ register RAY *r; { /* check for over-counting */ if (badcomponent(m, r)) - return; - if (wrongsource(m,r)) - return; + return(1); + if (wrongsource(m, r)) + return(1); /* check for passed illum */ if (passillum(m, r)) { - if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) - raytrans(r); - else - rayshade(r, modifier(m->oargs.sarg[0])); - return; + if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) + return(rayshade(r, modifier(m->oargs.sarg[0]))); + raytrans(r); + return(1); } /* otherwise treat as source */ /* check for behind */ if (r->rod < 0.0) - return; + return(1); /* check for invisibility */ if (srcignore(m, r)) - return; + return(1); /* check for outside spot */ - if (m->otype==MAT_SPOT && spotout(r, makespot(m), r->rot>=FHUGE)) - return; + if (m->otype==MAT_SPOT && spotout(r, makespot(m))) + return(1); /* get distribution pattern */ raytexture(r, m->omod); /* get source color */ @@ -467,4 +562,5 @@ register RAY *r; m->oargs.farg[2]); /* modify value */ multcolor(r->rcol, r->pcol); + return(1); }