--- ray/src/rt/source.c 1996/04/17 14:02:18 2.24 +++ ray/src/rt/source.c 2003/02/25 02:47:23 2.30 @@ -1,19 +1,16 @@ -/* 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.30 2003/02/25 02:47:23 greg Exp $"; #endif - /* * source.c - routines dealing with illumination sources. * - * 8/20/85 + * External symbols declared in source.h */ +#include "copyright.h" + #include "ray.h" -#include "octree.h" - #include "otypes.h" #include "source.h" @@ -47,6 +44,7 @@ static CNTPTR *cntord; /* source ordering in direct static int maxcntr = 0; /* size of contribution arrays */ +void marksources() /* find and mark source objects */ { int foundsource = 0; @@ -125,6 +123,25 @@ memerr: } +void +freesources() /* free all source structures */ +{ + if (nsources > 0) { + 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(sr, r, si) /* send a ray to a source, return domega */ register RAY *sr; /* returned source ray */ RAY *r; /* ray which hit object */ @@ -162,6 +179,7 @@ SRCINDEX *si; /* source sample index */ } +void srcvalue(r) /* punch ray to source and compute value */ register RAY *r; { @@ -202,6 +220,7 @@ nomat: } +int sourcehit(r) /* check to see if ray hit distant source */ register RAY *r; { @@ -227,6 +246,7 @@ 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; @@ -249,12 +269,13 @@ register CNTPTR *sc1, *sc2; } +void direct(r, f, p) /* add direct component */ RAY *r; /* ray that hit surface */ -int (*f)(); /* direct component coefficient function */ +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; @@ -374,6 +395,7 @@ char *p; /* data for f */ } +void srcscatter(r) /* compute source scattering into ray */ register RAY *r; { @@ -381,8 +403,9 @@ register RAY *r; 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 +420,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; @@ -415,36 +445,25 @@ register RAY *r; copycolor(sr.cext, r->cext); 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->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; } @@ -528,6 +547,7 @@ return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} distglow(m, r, raydist(r,PRIMARY))) +int m_light(m, r) /* ray hit a light source */ register OBJREC *m; register RAY *r; @@ -540,7 +560,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); }