--- ray/src/rt/source.c 1996/03/21 15:33:09 2.23 +++ ray/src/rt/source.c 2004/03/01 18:11:20 2.43 @@ -1,19 +1,14 @@ -/* Copyright (c) 1995 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: source.c,v 2.43 2004/03/01 18:11:20 greg Exp $"; #endif - /* * source.c - routines dealing with illumination sources. * - * 8/20/85 + * External symbols declared in source.h */ #include "ray.h" -#include "octree.h" - #include "otypes.h" #include "source.h" @@ -47,6 +42,31 @@ static CNTPTR *cntord; /* source ordering in direct static int maxcntr = 0; /* size of contribution arrays */ +OBJREC * /* find an object's actual material */ +findmaterial(register OBJREC *o) +{ + while (!ismaterial(o->otype)) { + if (ismixture(o->otype)) + return(NULL); /* reject mixed materials */ + if (o->otype == MOD_ALIAS && o->oargs.nsargs) { + OBJECT aobj; + OBJREC *ao; + aobj = lastmod(objndx(o), o->oargs.sarg[0]); + if (aobj < 0) + objerror(o, USER, "bad reference"); + ao = objptr(aobj); + if (ismaterial(ao->otype)) + return(ao); + } + if (o->omod == OVOID) + return(NULL); + o = objptr(o->omod); + } + return(o); +} + + +void marksources() /* find and mark source objects */ { int foundsource = 0; @@ -56,17 +76,16 @@ marksources() /* find and mark source objects */ /* initialize dispatch table */ initstypes(); /* find direct sources */ - for (i = 0; i < nobjects; i++) { + for (i = 0; i < nsceneobjs; i++) { o = objptr(i); if (!issurface(o->otype) || o->omod == OVOID) continue; - - m = objptr(o->omod); - - if (!islight(m->otype)) - continue; + /* find material */ + m = findmaterial(objptr(o->omod)); + if (m == NULL || !islight(m->otype)) + continue; /* not source modifier */ if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 : m->otype == MAT_SPOT ? 7 : 3)) @@ -105,6 +124,9 @@ marksources() /* find and mark source objects */ source[ns].sflags |= SSKIP; } } +#if SHADCACHE + source[ns].obscache = NULL; +#endif if (!(source[ns].sflags & SSKIP)) foundsource++; } @@ -117,7 +139,7 @@ marksources() /* find and mark source objects */ maxcntr = nsources + MAXSPART; /* start with this many */ srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB)); cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR)); - if (srccnt == NULL | cntord == NULL) + if ((srccnt == NULL) | (cntord == NULL)) goto memerr; return; memerr: @@ -125,11 +147,35 @@ memerr: } -srcray(sr, r, si) /* send a ray to a source, return domega */ -register RAY *sr; /* returned source ray */ -RAY *r; /* ray which hit object */ -SRCINDEX *si; /* source sample index */ +void +freesources() /* free all source structures */ { + if (nsources > 0) { +#if SHADCACHE + while (nsources--) + freeobscache(&source[nsources]); +#endif + free((void *)source); + source = NULL; + nsources = 0; + } + if (maxcntr <= 0) + return; + free((void *)srccnt); + srccnt = NULL; + free((void *)cntord); + cntord = NULL; + maxcntr = 0; +} + + +int +srcray( /* send a ray to a source, return domega */ +register RAY *sr, /* returned source ray */ +RAY *r, /* ray which hit object */ +SRCINDEX *si /* source sample index */ +) +{ double d; /* distance to source */ register SRCREC *srcp; @@ -162,8 +208,10 @@ SRCINDEX *si; /* source sample index */ } -srcvalue(r) /* punch ray to source and compute value */ -register RAY *r; +void +srcvalue( /* punch ray to source and compute value */ +register RAY *r +) { register SRCREC *sp; @@ -202,8 +250,10 @@ nomat: } -sourcehit(r) /* check to see if ray hit distant source */ -register RAY *r; +int +sourcehit( /* check to see if ray hit distant source */ +register RAY *r +) { int first, last; register int i; @@ -227,9 +277,12 @@ register RAY *r; } if (r->ro != NULL) { + r->robj = objndx(r->ro); for (i = 0; i < 3; i++) r->ron[i] = -r->rdir[i]; r->rod = 1.0; + r->pert[0] = r->pert[1] = r->pert[2] = 0.0; + r->uv[0] = r->uv[1] = 0.0; r->rox = NULL; return(1); } @@ -238,9 +291,14 @@ register RAY *r; static int -cntcmp(sc1, sc2) /* contribution compare (descending) */ -register CNTPTR *sc1, *sc2; +cntcmp( /* contribution compare (descending) */ +const void *p1, +const void *p2 +) { + register const CNTPTR *sc1 = (const CNTPTR *)p1; + register const CNTPTR *sc2 = (const CNTPTR *)p2; + if (sc1->brt > sc2->brt) return(-1); if (sc1->brt < sc2->brt) @@ -249,12 +307,14 @@ register CNTPTR *sc1, *sc2; } -direct(r, f, p) /* add direct component */ -RAY *r; /* ray that hit surface */ -int (*f)(); /* direct component coefficient function */ -char *p; /* data for f */ +void +direct( /* add direct component */ +RAY *r, /* ray that hit surface */ +void (*f)(), /* direct component coefficient function */ +char *p /* data for f */ +) { - extern int (*trace)(); + extern void (*trace)(); register int sn; register CONTRIB *scp; SRCINDEX si; @@ -270,11 +330,11 @@ char *p; /* data for f */ for (sn = 0; srcray(&sr, r, &si); sn++) { if (sn >= maxcntr) { maxcntr = sn + MAXSPART; - srccnt = (CONTRIB *)realloc((char *)srccnt, + srccnt = (CONTRIB *)realloc((void *)srccnt, maxcntr*sizeof(CONTRIB)); - cntord = (CNTPTR *)realloc((char *)cntord, + cntord = (CNTPTR *)realloc((void *)cntord, maxcntr*sizeof(CNTPTR)); - if (srccnt == NULL | cntord == NULL) + if ((srccnt == NULL) | (cntord == NULL)) error(SYSTEM, "out of memory in direct"); } cntord[sn].sndx = sn; @@ -285,6 +345,13 @@ char *p; /* data for f */ cntord[sn].brt = bright(scp->coef); if (cntord[sn].brt <= 0.0) continue; +#if SHADCACHE + /* check shadow cache */ + if (si.np == 1 && srcblocked(&sr)) { + cntord[sn].brt = 0.0; + continue; + } +#endif VCOPY(scp->dir, sr.rdir); /* compute potential */ sr.revf = srcvalue; @@ -332,7 +399,11 @@ char *p; /* data for f */ rayorigin(&sr, r, SHADOW, 1.0); VCOPY(sr.rdir, scp->dir); sr.rsrc = scp->sno; - source[scp->sno].ntests++; /* keep statistics */ + /* keep statistics */ + if (source[scp->sno].ntests++ > 0xfffffff0) { + source[scp->sno].ntests >>= 1; + source[scp->sno].nhits >>= 1; + } if (localhit(&sr, &thescene) && ( sr.ro != source[scp->sno].so || source[scp->sno].sflags & SFOLLOW )) { @@ -341,8 +412,15 @@ char *p; /* data for f */ rayparticipate(&sr); if (trace != NULL) (*trace)(&sr); /* trace execution */ - if (bright(sr.rcol) <= FTINY) + if (bright(sr.rcol) <= FTINY) { +#if SHADCACHE + if ((scp <= srccnt || scp[-1].sno != scp->sno) + && (scp >= srccnt+ncnts-1 || + scp[1].sno != scp->sno)) + srcblocker(&sr); +#endif continue; /* missed! */ + } copycolor(scp->val, sr.rcol); multcolor(scp->val, scp->coef); } @@ -374,15 +452,18 @@ char *p; /* data for f */ } -srcscatter(r) /* compute source scattering into ray */ -register RAY *r; +void +srcscatter( /* compute source scattering into ray */ +register RAY *r +) { int oldsampndx; int nsamps; RAY sr; SRCINDEX si; - double t, lastt, d; - COLOR cumval, ctmp; + double t, d; + double re, ge, be; + COLOR cvext; int i, j; if (r->slights == NULL || r->slights[0] == 0 @@ -397,11 +478,18 @@ register RAY *r; 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 */ + for (j = 0; j < nsamps; j++) { /* for each sample position */ samplendx++; t = r->rot * (j+frandom())/nsamps; + /* extinction */ + re = t*colval(r->cext,RED); + ge = t*colval(r->cext,GRN); + be = t*colval(r->cext,BLU); + setcolor(cvext, re > 92. ? 0. : exp(-re), + ge > 92. ? 0. : exp(-ge), + be > 92. ? 0. : exp(-be)); + if (intens(cvext) <= FTINY) + break; /* too far away */ 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; @@ -413,37 +501,27 @@ register RAY *r; sr.rsrc != r->slights[i]) continue; /* no path */ copycolor(sr.cext, r->cext); - sr.albedo = r->albedo; + copycolor(sr.albedo, r->albedo); sr.gecc = r->gecc; + sr.slights = r->slights; 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); + d = 1. + r->gecc*r->gecc - 2.*r->gecc*d; + d = (1. - r->gecc*r->gecc) / (d*sqrt(d)); } /* other factors */ - d *= si.dom * r->albedo * r->rot / (4.*PI*nsamps); + d *= si.dom * r->rot / (4.*PI*nsamps); multcolor(sr.rcol, r->cext); + multcolor(sr.rcol, r->albedo); scalecolor(sr.rcol, d); - addcolor(cumval, sr.rcol); + multcolor(sr.rcol, cvext); + addcolor(r->rcol, sr.rcol); /* add it in */ } - /* 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; } @@ -465,13 +543,18 @@ register RAY *r; * geometry behind (or inside) an effective radiator. */ -static int weaksrcmod(obj) int obj; /* efficiency booster function */ -{register OBJREC *o = objptr(obj); -return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} +static int +weaksrcmat(OBJECT obj) /* identify material */ +{ + OBJREC *o = findmaterial(objptr(obj)); + + if (o == NULL) return 0; + return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW)); +} #define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ r->rod > 0.0 && \ - weaksrcmod(source[r->rsrc].so->omod)) + weaksrcmat(source[r->rsrc].so->omod)) /* wrongsource * * @@ -527,9 +610,11 @@ return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} distglow(m, r, raydist(r,PRIMARY))) -m_light(m, r) /* ray hit a light source */ -register OBJREC *m; -register RAY *r; +int +m_light( /* ray hit a light source */ +register OBJREC *m, +register RAY *r +) { /* check for over-counting */ if (badcomponent(m, r)) @@ -539,7 +624,7 @@ register RAY *r; /* check for passed illum */ if (passillum(m, r)) { if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) - return(rayshade(r, modifier(m->oargs.sarg[0]))); + return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0]))); raytrans(r); return(1); }