--- ray/src/rt/virtuals.c 1991/06/25 16:42:05 1.12 +++ ray/src/rt/virtuals.c 1992/05/23 08:03:19 2.2 @@ -19,7 +19,10 @@ static char SCCSid[] = "$SunId$ LBL"; #include "random.h" +#define MINSAMPLES 16 /* minimum number of pretest samples */ +#define STESTMAX 32 /* maximum seeks per sample */ + double getdisk(); static OBJECT *vobject; /* virtual source objects */ @@ -38,11 +41,13 @@ markvirtuals() /* find and mark virtual sources */ o = objptr(i); if (!issurface(o->otype) || o->omod == OVOID) continue; - if (!isvlight(objptr(o->omod)->otype)) + if (!isvlight(vsmaterial(o)->otype)) continue; if (sfun[o->otype].of == NULL || - sfun[o->otype].of->getpleq == NULL) - objerror(o, USER, "illegal material"); + sfun[o->otype].of->getpleq == NULL) { + objerror(o,WARNING,"secondary sources not supported"); + continue; + } if (nvobjects == 0) vobject = (OBJECT *)malloc(sizeof(OBJECT)); else @@ -96,11 +101,12 @@ int n; if (o == source[sn].so) /* objects cannot project themselves */ return; /* get virtual source material */ - vsmat = sfun[objptr(o->omod)->otype].mf; + vsmat = sfun[vsmaterial(o)->otype].mf; /* project virtual sources */ for (i = 0; i < vsmat->nproj; i++) if ((*vsmat->vproj)(proj, o, &source[sn], i)) if ((ns = makevsrc(o, sn, proj)) >= 0) { + source[ns].sa.sv.pn = i; #ifdef DEBUG virtverb(ns, stderr); #endif @@ -109,6 +115,23 @@ int n; } +OBJREC * +vsmaterial(o) /* get virtual source material pointer */ +OBJREC *o; +{ + register int i; + register OBJREC *m; + + i = o->omod; + m = objptr(i); + if (m->otype != MAT_ILLUM || m->oargs.nsargs < 1 || + !strcmp(m->oargs.sarg[0], VOIDID) || + (i = modifier(m->oargs.sarg[0])) == OVOID) + return(m); /* direct modifier */ + return(objptr(i)); /* illum alternate */ +} + + int makevsrc(op, sn, pm) /* make virtual source if reasonable */ OBJREC *op; @@ -131,12 +154,17 @@ MAT4 pm; if (source[sn].sflags & SPROX) return(-1); /* should never get here! */ multv3(nsloc, source[sn].sloc, pm); + normalize(nsloc); VCOPY(ourspot.aim, ocent); ourspot.siz = PI*maxrad2; ourspot.flen = 0.; if (source[sn].sflags & SSPOT) { - copystruct(&theirspot, source[sn].sl.s); multp3(theirspot.aim, source[sn].sl.s->aim, pm); + /* adjust for source size */ + d = sqrt(dist2(ourspot.aim, theirspot.aim)); + d = sqrt(source[sn].sl.s->siz/PI) + d*source[sn].srad; + theirspot.siz = PI*d*d; + ourspot.flen = theirspot.flen = source[sn].sl.s->flen; d = ourspot.siz; if (!commonbeam(&ourspot, &theirspot, nsloc)) return(-1); /* no overlap */ @@ -158,14 +186,27 @@ MAT4 pm; return(-1); /* at source!! */ if (source[sn].sflags & SPROX && d > source[sn].sl.prox) return(-1); /* too far away */ - ourspot.siz = 2.*PI*(1. - d/sqrt(d*d+maxrad2)); ourspot.flen = 0.; + /* adjust for source size */ + d = (sqrt(maxrad2) + source[sn].srad) / d; + if (d < 1.-FTINY) + ourspot.siz = 2.*PI*(1. - sqrt(1.-d*d)); + else + nsflags &= ~SSPOT; if (source[sn].sflags & SSPOT) { copystruct(&theirspot, source[sn].sl.s); multv3(theirspot.aim, source[sn].sl.s->aim, pm); - d = ourspot.siz; - if (!commonspot(&ourspot, &theirspot, nsloc)) - return(-1); /* no overlap */ + normalize(theirspot.aim); + if (nsflags & SSPOT) { + ourspot.flen = theirspot.flen; + d = ourspot.siz; + if (!commonspot(&ourspot, &theirspot, nsloc)) + return(-1); /* no overlap */ + } else { + nsflags |= SSPOT; + copystruct(&ourspot, &theirspot); + d = 2.*ourspot.siz; + } if (ourspot.siz < d-FTINY) { /* it shrunk */ d = spotdisk(v, op, &ourspot, nsloc); if (d <= FTINY) @@ -175,11 +216,11 @@ MAT4 pm; VCOPY(ocent, v); } } - ourspot.flen = theirspot.flen; } if (source[sn].sflags & SFLAT) { /* behind source? */ multv3(nsnorm, source[sn].snorm, pm); - if (!checkspot(&ourspot, nsnorm)) + normalize(nsnorm); + if (nsflags & SSPOT && !checkspot(&ourspot, nsnorm)) return(-1); } } @@ -192,15 +233,22 @@ MAT4 pm; goto memerr; source[i].sflags = nsflags; VCOPY(source[i].sloc, nsloc); + multv3(source[i].ss[SU], source[sn].ss[SU], pm); + multv3(source[i].ss[SV], source[sn].ss[SV], pm); if (nsflags & SFLAT) VCOPY(source[i].snorm, nsnorm); - source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2; - if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL) - goto memerr; - copystruct(source[i].sl.s, &ourspot); + else + multv3(source[i].ss[SW], source[sn].ss[SW], pm); + source[i].srad = source[sn].srad; + source[i].ss2 = source[sn].ss2; + if (nsflags & SSPOT) { + if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL) + goto memerr; + copystruct(source[i].sl.s, &ourspot); + } if (nsflags & SPROX) source[i].sl.prox = source[sn].sl.prox; - source[i].sa.svnext = sn; + source[i].sa.sv.sn = sn; source[i].so = op; return(i); memerr: @@ -251,9 +299,10 @@ register int sn; /* target source number */ RAY sr; FVECT onorm; FVECT offsdir; + SRCINDEX si; double or, d; int infront; - int ssn; + int stestlim, ssn; int nhit, nok; register int i, n; /* return if pretesting disabled */ @@ -263,36 +312,42 @@ register int sn; /* target source number */ getplaneq(onorm, o); /* set number of rays to sample */ if (source[sn].sflags & SDISTANT) { - n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)* - vspretest + .5; + /* 32. == heuristic constant */ + n = 32.*or2/(thescene.cusize*thescene.cusize)*vspretest + .5; infront = DOT(onorm, source[sn].sloc) > 0.; } else { for (i = 0; i < 3; i++) offsdir[i] = source[sn].sloc[i] - oc[i]; - n = or2/DOT(offsdir,offsdir)*vspretest + .5; + d = DOT(offsdir,offsdir); + if (d <= FTINY) + n = 2.*PI * vspretest + .5; + else + n = 2.*PI * (1.-sqrt(1./(1.+or2/d)))*vspretest + .5; infront = DOT(onorm, offsdir) > 0.; } - if (n < 1) n = 1; + if (n < MINSAMPLES) n = MINSAMPLES; #ifdef DEBUG fprintf(stderr, "pretesting source %d in object %s with %d rays\n", sn, o->oname, n); #endif /* sample */ or = sqrt(or2); - ssn = 25*n; + stestlim = n*STESTMAX; + ssn = 0; nhit = nok = 0; while (n-- > 0) { /* get sample point */ do { - if (--ssn < 0) { + if (ssn >= stestlim) { #ifdef DEBUG fprintf(stderr, "\ttoo hard to hit\n"); #endif return(f); /* too small a target! */ } + multisamp(offsdir, 3, urand(sn*931+5827+ssn)); for (i = 0; i < 3; i++) - offsdir[i] = or*(1. - - 2.*urand(931*i+5827+ssn)); + offsdir[i] = or*(1. - 2.*offsdir[i]); + ssn++; for (i = 0; i < 3; i++) sr.rorg[i] = oc[i] + offsdir[i]; d = DOT(offsdir,onorm); @@ -309,8 +364,11 @@ register int sn; /* target source number */ rayorigin(&sr, NULL, PRIMARY, 1.0); } while (!(*ofun[o->otype].funp)(o, &sr)); /* check against source */ + initsrcindex(&si); + si.sn = sn; + nopart(&si, &sr); samplendx++; - if (srcray(&sr, NULL, sn) == 0.) + if (!srcray(&sr, NULL, &si) || sr.rsrc != sn) continue; sr.revf = srcvalue; rayvalue(&sr); @@ -318,7 +376,8 @@ register int sn; /* target source number */ continue; nok++; /* check against obstructions */ - srcray(&sr, NULL, sn); + rayclear(&sr); + sr.revf = raytrace; rayvalue(&sr); if (bright(sr.rcol) > FTINY) nhit++; @@ -356,7 +415,7 @@ FILE *fp; fprintf(fp, "\tat (%f,%f,%f)\n", source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]); fprintf(fp, "\tlinked to source %d (%s)\n", - source[sn].sa.svnext, source[source[sn].sa.svnext].so->oname); + source[sn].sa.sv.sn, source[source[sn].sa.sv.sn].so->oname); if (source[sn].sflags & SFOLLOW) fprintf(fp, "\talways followed\n"); else