--- ray/src/rt/virtuals.c 1992/05/23 08:03:19 2.2 +++ ray/src/rt/virtuals.c 2004/03/30 16:13:01 2.14 @@ -1,18 +1,17 @@ -/* Copyright (c) 1991 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: virtuals.c,v 2.14 2004/03/30 16:13:01 schorsch Exp $"; #endif - /* * Routines for simulating virtual light sources * Thus far, we only support planar mirrors. + * + * External symbols declared in source.h */ +#include "copyright.h" + #include "ray.h" -#include "octree.h" - #include "otypes.h" #include "source.h" @@ -23,13 +22,12 @@ static char SCCSid[] = "$SunId$ LBL"; #define STESTMAX 32 /* maximum seeks per sample */ -double getdisk(); - static OBJECT *vobject; /* virtual source objects */ static int nvobjects = 0; /* number of virtual source objects */ -markvirtuals() /* find and mark virtual sources */ +extern void +markvirtuals(void) /* find and mark virtual sources */ { register OBJREC *o; register int i; @@ -37,7 +35,7 @@ markvirtuals() /* find and mark virtual sources */ if (directrelay <= 0) return; /* find virtual source objects */ - for (i = 0; i < nobjects; i++) { + for (i = 0; i < nsceneobjs; i++) { o = objptr(i); if (!issurface(o->otype) || o->omod == OVOID) continue; @@ -51,7 +49,7 @@ markvirtuals() /* find and mark virtual sources */ if (nvobjects == 0) vobject = (OBJECT *)malloc(sizeof(OBJECT)); else - vobject = (OBJECT *)realloc((char *)vobject, + vobject = (OBJECT *)realloc((void *)vobject, (unsigned)(nvobjects+1)*sizeof(OBJECT)); if (vobject == NULL) error(SYSTEM, "out of memory in addvirtuals"); @@ -66,14 +64,16 @@ markvirtuals() /* find and mark virtual sources */ for (i = nsources; i-- > 0; ) addvirtuals(i, directrelay); /* done with our object list */ - free((char *)vobject); + free((void *)vobject); nvobjects = 0; } -addvirtuals(sn, nr) /* add virtuals associated with source */ -int sn; -int nr; +extern void +addvirtuals( /* add virtuals associated with source */ + int sn, + int nr +) { register int i; /* check relay limit first */ @@ -88,10 +88,12 @@ int nr; } -vproject(o, sn, n) /* create projected source(s) if they exist */ -OBJREC *o; -int sn; -int n; +extern void +vproject( /* create projected source(s) if they exist */ + OBJREC *o, + int sn, + int n +) { register int i; register VSMATERIAL *vsmat; @@ -115,28 +117,32 @@ int n; } -OBJREC * -vsmaterial(o) /* get virtual source material pointer */ -OBJREC *o; +extern OBJREC * +vsmaterial( /* get virtual source material pointer */ + OBJREC *o +) { register int i; register OBJREC *m; i = o->omod; - m = objptr(i); + m = findmaterial(objptr(i)); + if (m == NULL) + return(objptr(i)); if (m->otype != MAT_ILLUM || m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID) || - (i = modifier(m->oargs.sarg[0])) == OVOID) + (i = lastmod(objndx(m), 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; -register int sn; -MAT4 pm; +extern int +makevsrc( /* make virtual source if reasonable */ + OBJREC *op, + register int sn, + MAT4 pm +) { FVECT nsloc, nsnorm, ocent, v; double maxrad2, d; @@ -157,7 +163,7 @@ MAT4 pm; normalize(nsloc); VCOPY(ourspot.aim, ocent); ourspot.siz = PI*maxrad2; - ourspot.flen = 0.; + ourspot.flen = -1.; if (source[sn].sflags & SSPOT) { multp3(theirspot.aim, source[sn].sl.s->aim, pm); /* adjust for source size */ @@ -194,7 +200,7 @@ MAT4 pm; else nsflags &= ~SSPOT; if (source[sn].sflags & SSPOT) { - copystruct(&theirspot, source[sn].sl.s); + theirspot = *(source[sn].sl.s); multv3(theirspot.aim, source[sn].sl.s->aim, pm); normalize(theirspot.aim); if (nsflags & SSPOT) { @@ -204,7 +210,7 @@ MAT4 pm; return(-1); /* no overlap */ } else { nsflags |= SSPOT; - copystruct(&ourspot, &theirspot); + ourspot = theirspot; d = 2.*ourspot.siz; } if (ourspot.siz < d-FTINY) { /* it shrunk */ @@ -244,7 +250,7 @@ MAT4 pm; if (nsflags & SSPOT) { if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL) goto memerr; - copystruct(source[i].sl.s, &ourspot); + *(source[i].sl.s) = ourspot; } if (nsflags & SPROX) source[i].sl.prox = source[sn].sl.prox; @@ -253,14 +259,16 @@ MAT4 pm; return(i); memerr: error(SYSTEM, "out of memory in makevsrc"); + return -1; /* pro forma return */ } -double -getdisk(oc, op, sn) /* get visible object disk */ -FVECT oc; -OBJREC *op; -register int sn; +extern double +getdisk( /* get visible object disk */ + FVECT oc, + OBJREC *op, + register int sn +) { double rad2, roffs, offs, d, rd, rdoto; FVECT rnrm, nrm; @@ -288,20 +296,20 @@ register int sn; } -int -vstestvis(f, o, oc, or2, sn) /* pretest source visibility */ -int f; /* virtual source flags */ -OBJREC *o; /* relay object */ -FVECT oc; /* relay object center */ -double or2; /* relay object radius squared */ -register int sn; /* target source number */ +extern int +vstestvis( /* pretest source visibility */ + int f, /* virtual source flags */ + OBJREC *o, /* relay object */ + FVECT oc, /* relay object center */ + double or2, /* relay object radius squared */ + register int sn /* target source number */ +) { RAY sr; FVECT onorm; FVECT offsdir; SRCINDEX si; double or, d; - int infront; int stestlim, ssn; int nhit, nok; register int i, n; @@ -314,7 +322,6 @@ register int sn; /* target source number */ if (source[sn].sflags & SDISTANT) { /* 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]; @@ -323,7 +330,6 @@ register int sn; /* target source number */ n = 2.*PI * vspretest + .5; else n = 2.*PI * (1.-sqrt(1./(1.+or2/d)))*vspretest + .5; - infront = DOT(onorm, offsdir) > 0.; } if (n < MINSAMPLES) n = MINSAMPLES; #ifdef DEBUG @@ -335,6 +341,7 @@ register int sn; /* target source number */ stestlim = n*STESTMAX; ssn = 0; nhit = nok = 0; + initsrcindex(&si); while (n-- > 0) { /* get sample point */ do { @@ -348,34 +355,29 @@ register int sn; /* target source number */ for (i = 0; i < 3; i++) 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); - if (infront) - for (i = 0; i < 3; i++) { - sr.rorg[i] -= (d-.0001)*onorm[i]; - sr.rdir[i] = -onorm[i]; - } - else - for (i = 0; i < 3; i++) { - sr.rorg[i] -= (d+.0001)*onorm[i]; - sr.rdir[i] = onorm[i]; - } + d = 1. - DOT(offsdir, onorm); + for (i = 0; i < 3; i++) { + sr.rorg[i] = oc[i] + offsdir[i] + d*onorm[i]; + sr.rdir[i] = -onorm[i]; + } + sr.rmax = 0.0; rayorigin(&sr, NULL, PRIMARY, 1.0); } while (!(*ofun[o->otype].funp)(o, &sr)); /* check against source */ - initsrcindex(&si); - si.sn = sn; - nopart(&si, &sr); + VCOPY(sr.rorg, sr.rop); /* starting from intersection */ samplendx++; - if (!srcray(&sr, NULL, &si) || sr.rsrc != sn) - continue; + if (si.sp >= si.np-1 || + !srcray(&sr, NULL, &si) || sr.rsrc != sn) { + si.sn = sn-1; /* reset index to our source */ + si.np = 0; + if (!srcray(&sr, NULL, &si) || sr.rsrc != sn) + continue; /* can't get there from here */ + } sr.revf = srcvalue; - rayvalue(&sr); + rayvalue(&sr); /* check sample validity */ if (bright(sr.rcol) <= FTINY) continue; - nok++; - /* check against obstructions */ + nok++; /* got sample; check obstructions */ rayclear(&sr); sr.revf = raytrace; rayvalue(&sr); @@ -402,9 +404,11 @@ register int sn; /* target source number */ #ifdef DEBUG -virtverb(sn, fp) /* print verbose description of virtual source */ -register int sn; -FILE *fp; +extern void +virtverb( /* print verbose description of virtual source */ + register int sn, + FILE *fp +) { register int i;