| 1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
| 1 |
> |
/* Copyright (c) 1991 Regents of the University of California */ |
| 2 |
|
|
| 3 |
|
#ifndef lint |
| 4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
| 14 |
|
|
| 15 |
|
#include "octree.h" |
| 16 |
|
|
| 17 |
– |
#include "source.h" |
| 18 |
– |
|
| 17 |
|
#include "otypes.h" |
| 18 |
|
|
| 19 |
< |
#include "cone.h" |
| 19 |
> |
#include "source.h" |
| 20 |
|
|
| 21 |
< |
#include "face.h" |
| 21 |
> |
/* |
| 22 |
> |
* Structures used by direct() |
| 23 |
> |
*/ |
| 24 |
|
|
| 25 |
< |
#include "random.h" |
| 25 |
> |
typedef struct { |
| 26 |
> |
int sno; /* source number */ |
| 27 |
> |
FVECT dir; /* source direction */ |
| 28 |
> |
COLOR coef; /* material coefficient */ |
| 29 |
> |
COLOR val; /* contribution */ |
| 30 |
> |
} CONTRIB; /* direct contribution */ |
| 31 |
|
|
| 32 |
+ |
typedef struct { |
| 33 |
+ |
int sndx; /* source index (to CONTRIB array) */ |
| 34 |
+ |
float brt; /* brightness (for comparison) */ |
| 35 |
+ |
} CNTPTR; /* contribution pointer */ |
| 36 |
|
|
| 37 |
< |
extern double dstrsrc; /* source distribution amount */ |
| 38 |
< |
extern double shadthresh; /* relative shadow threshold */ |
| 37 |
> |
static CONTRIB *srccnt; /* source contributions in direct() */ |
| 38 |
> |
static CNTPTR *cntord; /* source ordering in direct() */ |
| 39 |
> |
static int maxcntr = 0; /* size of contribution arrays */ |
| 40 |
|
|
| 31 |
– |
SRCREC *source = NULL; /* our list of sources */ |
| 32 |
– |
int nsources = 0; /* the number of sources */ |
| 41 |
|
|
| 34 |
– |
|
| 42 |
|
marksources() /* find and mark source objects */ |
| 43 |
|
{ |
| 44 |
+ |
int i; |
| 45 |
|
register OBJREC *o, *m; |
| 46 |
< |
register int i; |
| 47 |
< |
|
| 46 |
> |
register int ns; |
| 47 |
> |
/* initialize dispatch table */ |
| 48 |
> |
initstypes(); |
| 49 |
> |
/* find direct sources */ |
| 50 |
|
for (i = 0; i < nobjects; i++) { |
| 51 |
|
|
| 52 |
|
o = objptr(i); |
| 53 |
|
|
| 54 |
< |
if (o->omod == OVOID) |
| 54 |
> |
if (!issurface(o->otype) || o->omod == OVOID) |
| 55 |
|
continue; |
| 56 |
|
|
| 57 |
|
m = objptr(o->omod); |
| 58 |
|
|
| 59 |
< |
if (m->otype != MAT_LIGHT && |
| 50 |
< |
m->otype != MAT_ILLUM && |
| 51 |
< |
m->otype != MAT_GLOW) |
| 59 |
> |
if (!islight(m->otype)) |
| 60 |
|
continue; |
| 61 |
|
|
| 62 |
< |
if (m->oargs.nfargs != 3) |
| 62 |
> |
if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 : |
| 63 |
> |
m->otype == MAT_SPOT ? 7 : 3)) |
| 64 |
|
objerror(m, USER, "bad # arguments"); |
| 65 |
|
|
| 66 |
< |
if (m->otype == MAT_GLOW && o->otype != OBJ_SOURCE) |
| 66 |
> |
if (m->otype == MAT_GLOW && |
| 67 |
> |
o->otype != OBJ_SOURCE && |
| 68 |
> |
m->oargs.farg[3] <= FTINY) |
| 69 |
|
continue; /* don't bother */ |
| 70 |
|
|
| 71 |
< |
if (source == NULL) |
| 72 |
< |
source = (SRCREC *)malloc(sizeof(SRCREC)); |
| 73 |
< |
else |
| 63 |
< |
source = (SRCREC *)realloc((char *)source, |
| 64 |
< |
(unsigned)(nsources+1)*sizeof(SRCREC)); |
| 65 |
< |
if (source == NULL) |
| 66 |
< |
error(SYSTEM, "out of memory in marksources"); |
| 71 |
> |
if (sfun[o->otype].of == NULL || |
| 72 |
> |
sfun[o->otype].of->setsrc == NULL) |
| 73 |
> |
objerror(o, USER, "illegal material"); |
| 74 |
|
|
| 75 |
< |
newsource(&source[nsources], o); |
| 75 |
> |
if ((ns = newsource()) < 0) |
| 76 |
> |
goto memerr; |
| 77 |
|
|
| 78 |
< |
if (m->otype == MAT_GLOW) |
| 71 |
< |
source[nsources].sflags |= SSKIP; |
| 78 |
> |
setsource(&source[ns], o); |
| 79 |
|
|
| 80 |
< |
nsources++; |
| 81 |
< |
} |
| 82 |
< |
} |
| 83 |
< |
|
| 84 |
< |
|
| 85 |
< |
newsource(src, so) /* add a source to the array */ |
| 86 |
< |
register SRCREC *src; |
| 87 |
< |
register OBJREC *so; |
| 88 |
< |
{ |
| 89 |
< |
double cos(), tan(), sqrt(); |
| 90 |
< |
double theta; |
| 91 |
< |
FACE *f; |
| 92 |
< |
CONE *co; |
| 93 |
< |
int j; |
| 94 |
< |
register int i; |
| 88 |
< |
|
| 89 |
< |
src->sflags = 0; |
| 90 |
< |
src->nhits = src->ntests = 1; /* start with hit probability = 1 */ |
| 91 |
< |
src->so = so; |
| 92 |
< |
|
| 93 |
< |
switch (so->otype) { |
| 94 |
< |
case OBJ_SOURCE: |
| 95 |
< |
if (so->oargs.nfargs != 4) |
| 96 |
< |
objerror(so, USER, "bad arguments"); |
| 97 |
< |
src->sflags |= SDISTANT; |
| 98 |
< |
VCOPY(src->sloc, so->oargs.farg); |
| 99 |
< |
if (normalize(src->sloc) == 0.0) |
| 100 |
< |
objerror(so, USER, "zero direction"); |
| 101 |
< |
theta = PI/180.0/2.0 * so->oargs.farg[3]; |
| 102 |
< |
if (theta <= FTINY) |
| 103 |
< |
objerror(so, USER, "zero size"); |
| 104 |
< |
src->ss = theta >= PI/4 ? 1.0 : tan(theta); |
| 105 |
< |
src->ss2 = 2.0*PI * (1.0 - cos(theta)); |
| 106 |
< |
break; |
| 107 |
< |
case OBJ_SPHERE: |
| 108 |
< |
VCOPY(src->sloc, so->oargs.farg); |
| 109 |
< |
src->ss = so->oargs.farg[3]; |
| 110 |
< |
src->ss2 = PI * src->ss * src->ss; |
| 111 |
< |
break; |
| 112 |
< |
case OBJ_FACE: |
| 113 |
< |
/* get the face */ |
| 114 |
< |
f = getface(so); |
| 115 |
< |
/* find the center */ |
| 116 |
< |
for (j = 0; j < 3; j++) { |
| 117 |
< |
src->sloc[j] = 0.0; |
| 118 |
< |
for (i = 0; i < f->nv; i++) |
| 119 |
< |
src->sloc[j] += VERTEX(f,i)[j]; |
| 120 |
< |
src->sloc[j] /= f->nv; |
| 80 |
> |
if (m->otype == MAT_GLOW) { |
| 81 |
> |
source[ns].sflags |= SPROX; |
| 82 |
> |
source[ns].sl.prox = m->oargs.farg[3]; |
| 83 |
> |
if (o->otype == OBJ_SOURCE) |
| 84 |
> |
source[ns].sflags |= SSKIP; |
| 85 |
> |
} else if (m->otype == MAT_SPOT) { |
| 86 |
> |
source[ns].sflags |= SSPOT; |
| 87 |
> |
if ((source[ns].sl.s = makespot(m)) == NULL) |
| 88 |
> |
goto memerr; |
| 89 |
> |
if (source[ns].sflags & SFLAT && |
| 90 |
> |
!checkspot(source[ns].sl.s,source[ns].snorm)) { |
| 91 |
> |
objerror(o, WARNING, |
| 92 |
> |
"invalid spotlight direction"); |
| 93 |
> |
source[ns].sflags |= SSKIP; |
| 94 |
> |
} |
| 95 |
|
} |
| 122 |
– |
if (!inface(src->sloc, f)) |
| 123 |
– |
objerror(so, USER, "cannot hit center"); |
| 124 |
– |
src->ss = sqrt(f->area / PI); |
| 125 |
– |
src->ss2 = f->area; |
| 126 |
– |
break; |
| 127 |
– |
case OBJ_RING: |
| 128 |
– |
/* get the ring */ |
| 129 |
– |
co = getcone(so, 0); |
| 130 |
– |
VCOPY(src->sloc, CO_P0(co)); |
| 131 |
– |
if (CO_R0(co) > 0.0) |
| 132 |
– |
objerror(so, USER, "cannot hit center"); |
| 133 |
– |
src->ss = CO_R1(co); |
| 134 |
– |
src->ss2 = PI * src->ss * src->ss; |
| 135 |
– |
break; |
| 136 |
– |
default: |
| 137 |
– |
objerror(so, USER, "illegal material"); |
| 96 |
|
} |
| 97 |
+ |
if (nsources <= 0) { |
| 98 |
+ |
error(WARNING, "no light sources found"); |
| 99 |
+ |
return; |
| 100 |
+ |
} |
| 101 |
+ |
markvirtuals(); /* find and add virtual sources */ |
| 102 |
+ |
/* allocate our contribution arrays */ |
| 103 |
+ |
maxcntr = nsources + MAXSPART; /* start with this many */ |
| 104 |
+ |
srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB)); |
| 105 |
+ |
cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR)); |
| 106 |
+ |
if (srccnt == NULL | cntord == NULL) |
| 107 |
+ |
goto memerr; |
| 108 |
+ |
return; |
| 109 |
+ |
memerr: |
| 110 |
+ |
error(SYSTEM, "out of memory in marksources"); |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
|
| 114 |
< |
double |
| 143 |
< |
srcray(sr, r, sn) /* send a ray to a source, return domega */ |
| 114 |
> |
srcray(sr, r, si) /* send a ray to a source, return domega */ |
| 115 |
|
register RAY *sr; /* returned source ray */ |
| 116 |
|
RAY *r; /* ray which hit object */ |
| 117 |
< |
register int sn; /* source number */ |
| 117 |
> |
SRCINDEX *si; /* source sample index */ |
| 118 |
|
{ |
| 119 |
< |
register double *norm = NULL; /* plane normal */ |
| 120 |
< |
double ddot; /* (distance times) cosine */ |
| 121 |
< |
FVECT vd; |
| 122 |
< |
double d; |
| 152 |
< |
register int i; |
| 119 |
> |
double d; /* distance to source */ |
| 120 |
> |
FVECT vd; |
| 121 |
> |
register SRCREC *srcp; |
| 122 |
> |
register int i; |
| 123 |
|
|
| 124 |
< |
if (source[sn].sflags & SSKIP) |
| 155 |
< |
return(0.0); /* skip this source */ |
| 124 |
> |
rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */ |
| 125 |
|
|
| 126 |
< |
rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */ |
| 127 |
< |
|
| 128 |
< |
sr->rsrc = sn; /* remember source */ |
| 129 |
< |
/* get source direction */ |
| 130 |
< |
if (source[sn].sflags & SDISTANT) |
| 162 |
< |
/* constant direction */ |
| 163 |
< |
VCOPY(sr->rdir, source[sn].sloc); |
| 164 |
< |
else { /* compute direction */ |
| 165 |
< |
for (i = 0; i < 3; i++) |
| 166 |
< |
sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i]; |
| 167 |
< |
|
| 168 |
< |
if (source[sn].so->otype == OBJ_FACE) |
| 169 |
< |
norm = getface(source[sn].so)->norm; |
| 170 |
< |
else if (source[sn].so->otype == OBJ_RING) |
| 171 |
< |
norm = getcone(source[sn].so,0)->ad; |
| 172 |
< |
|
| 173 |
< |
if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY) |
| 174 |
< |
return(0.0); /* behind surface! */ |
| 175 |
< |
} |
| 176 |
< |
if (dstrsrc > FTINY) { |
| 177 |
< |
/* distribute source direction */ |
| 178 |
< |
for (i = 0; i < 3; i++) |
| 179 |
< |
vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom()); |
| 180 |
< |
|
| 181 |
< |
if (norm != NULL) { /* project offset */ |
| 182 |
< |
d = DOT(vd, norm); |
| 126 |
> |
while ((d = nextssamp(sr, si)) != 0.0) { |
| 127 |
> |
sr->rsrc = si->sn; /* remember source */ |
| 128 |
> |
srcp = source + si->sn; |
| 129 |
> |
if (srcp->sflags & SDISTANT) { |
| 130 |
> |
if (srcp->sflags & SSPOT) { /* check location */ |
| 131 |
|
for (i = 0; i < 3; i++) |
| 132 |
< |
vd[i] -= d * norm[i]; |
| 132 |
> |
vd[i] = srcp->sl.s->aim[i] - sr->rorg[i]; |
| 133 |
> |
d = DOT(sr->rdir,vd); |
| 134 |
> |
if (d <= FTINY) |
| 135 |
> |
continue; |
| 136 |
> |
d = DOT(vd,vd) - d*d; |
| 137 |
> |
if (PI*d > srcp->sl.s->siz) |
| 138 |
> |
continue; |
| 139 |
|
} |
| 140 |
< |
for (i = 0; i < 3; i++) /* offset source direction */ |
| 187 |
< |
sr->rdir[i] += vd[i]; |
| 188 |
< |
|
| 189 |
< |
} else if (source[sn].sflags & SDISTANT) |
| 190 |
< |
/* already normalized */ |
| 191 |
< |
return(source[sn].ss2); |
| 192 |
< |
|
| 193 |
< |
if ((d = normalize(sr->rdir)) == 0.0) |
| 194 |
< |
/* at source! */ |
| 195 |
< |
return(0.0); |
| 196 |
< |
|
| 197 |
< |
if (source[sn].sflags & SDISTANT) |
| 198 |
< |
/* domega constant */ |
| 199 |
< |
return(source[sn].ss2); |
| 200 |
< |
|
| 201 |
< |
else { |
| 202 |
< |
|
| 203 |
< |
if (norm != NULL) |
| 204 |
< |
ddot /= d; |
| 205 |
< |
else |
| 206 |
< |
ddot = 1.0; |
| 207 |
< |
/* return domega */ |
| 208 |
< |
return(ddot*source[sn].ss2/(d*d)); |
| 140 |
> |
return(1); /* sample OK */ |
| 141 |
|
} |
| 142 |
+ |
/* local source */ |
| 143 |
+ |
/* check proximity */ |
| 144 |
+ |
if (srcp->sflags & SPROX && d > srcp->sl.prox) |
| 145 |
+ |
continue; |
| 146 |
+ |
/* check angle */ |
| 147 |
+ |
if (srcp->sflags & SSPOT) { |
| 148 |
+ |
if (srcp->sl.s->siz < 2.0*PI * |
| 149 |
+ |
(1.0 + DOT(srcp->sl.s->aim,sr->rdir))) |
| 150 |
+ |
continue; |
| 151 |
+ |
/* adjust solid angle */ |
| 152 |
+ |
si->dom *= d*d; |
| 153 |
+ |
d += srcp->sl.s->flen; |
| 154 |
+ |
si->dom /= d*d; |
| 155 |
+ |
} |
| 156 |
+ |
return(1); /* sample OK */ |
| 157 |
+ |
} |
| 158 |
+ |
return(0); /* no more samples */ |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
|
| 162 |
< |
sourcehit(r) /* check to see if ray hit distant source */ |
| 163 |
< |
register RAY *r; |
| 162 |
> |
srcvalue(r) /* punch ray to source and compute value */ |
| 163 |
> |
RAY *r; |
| 164 |
|
{ |
| 165 |
< |
int first, last; |
| 217 |
< |
register int i; |
| 165 |
> |
register SRCREC *sp; |
| 166 |
|
|
| 167 |
< |
if (r->rsrc >= 0) { /* check only one if aimed */ |
| 168 |
< |
first = last = r->rsrc; |
| 169 |
< |
} else { /* otherwise check all */ |
| 170 |
< |
first = 0; last = nsources-1; |
| 167 |
> |
sp = &source[r->rsrc]; |
| 168 |
> |
if (sp->sflags & SVIRTUAL) { /* virtual source */ |
| 169 |
> |
/* check intersection */ |
| 170 |
> |
if (!(*ofun[sp->so->otype].funp)(sp->so, r)) |
| 171 |
> |
return; |
| 172 |
> |
raycont(r); /* compute contribution */ |
| 173 |
> |
return; |
| 174 |
|
} |
| 175 |
< |
for (i = first; i <= last; i++) |
| 176 |
< |
if (source[i].sflags & SDISTANT) |
| 177 |
< |
/* |
| 178 |
< |
* Check to see if ray is within |
| 179 |
< |
* solid angle of source. |
| 180 |
< |
*/ |
| 181 |
< |
if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir)) |
| 231 |
< |
<= source[i].ss2) { |
| 232 |
< |
r->ro = source[i].so; |
| 233 |
< |
if (!(source[i].sflags & SSKIP)) |
| 234 |
< |
break; |
| 235 |
< |
} |
| 236 |
< |
|
| 237 |
< |
if (r->ro != NULL) { |
| 238 |
< |
for (i = 0; i < 3; i++) |
| 239 |
< |
r->ron[i] = -r->rdir[i]; |
| 240 |
< |
r->rod = 1.0; |
| 241 |
< |
r->rofs = 1.0; setident4(r->rofx); |
| 242 |
< |
r->robs = 1.0; setident4(r->robx); |
| 243 |
< |
return(1); |
| 175 |
> |
/* compute intersection */ |
| 176 |
> |
if (sp->sflags & SDISTANT ? sourcehit(r) : |
| 177 |
> |
(*ofun[sp->so->otype].funp)(sp->so, r)) { |
| 178 |
> |
if (sp->sa.success >= 0) |
| 179 |
> |
sp->sa.success++; |
| 180 |
> |
raycont(r); /* compute contribution */ |
| 181 |
> |
return; |
| 182 |
|
} |
| 183 |
< |
return(0); |
| 183 |
> |
if (sp->sa.success < 0) |
| 184 |
> |
return; /* bitched already */ |
| 185 |
> |
sp->sa.success -= AIMREQT; |
| 186 |
> |
if (sp->sa.success >= 0) |
| 187 |
> |
return; /* leniency */ |
| 188 |
> |
sprintf(errmsg, "aiming failure for light source \"%s\"", |
| 189 |
> |
sp->so->oname); |
| 190 |
> |
error(WARNING, errmsg); /* issue warning */ |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
|
| 194 |
|
static int |
| 195 |
|
cntcmp(sc1, sc2) /* contribution compare (descending) */ |
| 196 |
< |
register CONTRIB *sc1, *sc2; |
| 196 |
> |
register CNTPTR *sc1, *sc2; |
| 197 |
|
{ |
| 198 |
|
if (sc1->brt > sc2->brt) |
| 199 |
|
return(-1); |
| 208 |
|
int (*f)(); /* direct component coefficient function */ |
| 209 |
|
char *p; /* data for f */ |
| 210 |
|
{ |
| 211 |
+ |
extern int (*trace)(); |
| 212 |
+ |
extern double pow(); |
| 213 |
|
register int sn; |
| 214 |
< |
register CONTRIB *srccnt; |
| 215 |
< |
double dtmp, hwt, test2, hit2; |
| 214 |
> |
SRCINDEX si; |
| 215 |
> |
int nshadcheck, ncnts; |
| 216 |
> |
int nhits; |
| 217 |
> |
double prob, ourthresh, hwt; |
| 218 |
|
RAY sr; |
| 219 |
< |
|
| 220 |
< |
if ((srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB))) == NULL) |
| 221 |
< |
error(SYSTEM, "out of memory in direct"); |
| 219 |
> |
/* NOTE: srccnt and cntord global so no recursion */ |
| 220 |
> |
if (nsources <= 0) |
| 221 |
> |
return; /* no sources?! */ |
| 222 |
|
/* potential contributions */ |
| 223 |
< |
for (sn = 0; sn < nsources; sn++) { |
| 224 |
< |
srccnt[sn].sno = sn; |
| 225 |
< |
setcolor(srccnt[sn].val, 0.0, 0.0, 0.0); |
| 226 |
< |
/* get source ray */ |
| 227 |
< |
if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0) |
| 228 |
< |
continue; |
| 229 |
< |
VCOPY(srccnt[sn].dir, sr.rdir); |
| 223 |
> |
initsrcindex(&si); |
| 224 |
> |
for (sn = 0; srcray(&sr, r, &si); sn++) { |
| 225 |
> |
if (sn >= maxcntr) { |
| 226 |
> |
maxcntr = sn + MAXSPART; |
| 227 |
> |
srccnt = (CONTRIB *)realloc((char *)srccnt, |
| 228 |
> |
maxcntr*sizeof(CONTRIB)); |
| 229 |
> |
cntord = (CNTPTR *)realloc((char *)cntord, |
| 230 |
> |
maxcntr*sizeof(CNTPTR)); |
| 231 |
> |
if (srccnt == NULL | cntord == NULL) |
| 232 |
> |
error(SYSTEM, "out of memory in direct"); |
| 233 |
> |
} |
| 234 |
> |
cntord[sn].sndx = sn; |
| 235 |
> |
srccnt[sn].sno = sr.rsrc; |
| 236 |
|
/* compute coefficient */ |
| 237 |
< |
(*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom); |
| 238 |
< |
srccnt[sn].brt = bright(srccnt[sn].val); |
| 239 |
< |
if (srccnt[sn].brt <= FTINY) |
| 237 |
> |
(*f)(srccnt[sn].coef, p, sr.rdir, si.dom); |
| 238 |
> |
cntord[sn].brt = bright(srccnt[sn].coef); |
| 239 |
> |
if (cntord[sn].brt <= 0.0) |
| 240 |
|
continue; |
| 241 |
< |
/* compute intersection */ |
| 242 |
< |
if (!( source[sn].sflags & SDISTANT ? |
| 243 |
< |
sourcehit(&sr) : |
| 244 |
< |
(*ofun[source[sn].so->otype].funp) |
| 245 |
< |
(source[sn].so, &sr) )) |
| 246 |
< |
continue; |
| 247 |
< |
/* compute contribution */ |
| 293 |
< |
rayshade(&sr, sr.ro->omod); |
| 294 |
< |
multcolor(srccnt[sn].val, sr.rcol); |
| 295 |
< |
srccnt[sn].brt = bright(srccnt[sn].val); |
| 241 |
> |
VCOPY(srccnt[sn].dir, sr.rdir); |
| 242 |
> |
/* compute potential */ |
| 243 |
> |
sr.revf = srcvalue; |
| 244 |
> |
rayvalue(&sr); |
| 245 |
> |
copycolor(srccnt[sn].val, sr.rcol); |
| 246 |
> |
multcolor(srccnt[sn].val, srccnt[sn].coef); |
| 247 |
> |
cntord[sn].brt = bright(srccnt[sn].val); |
| 248 |
|
} |
| 249 |
|
/* sort contributions */ |
| 250 |
< |
qsort(srccnt, nsources, sizeof(CONTRIB), cntcmp); |
| 251 |
< |
hit2 = test2 = 0.0; |
| 250 |
> |
qsort(cntord, sn, sizeof(CNTPTR), cntcmp); |
| 251 |
> |
{ /* find last */ |
| 252 |
> |
register int l, m; |
| 253 |
> |
|
| 254 |
> |
ncnts = l = sn; |
| 255 |
> |
sn = 0; |
| 256 |
> |
while ((m = (sn + ncnts) >> 1) != l) { |
| 257 |
> |
if (cntord[m].brt > 0.0) |
| 258 |
> |
sn = m; |
| 259 |
> |
else |
| 260 |
> |
ncnts = m; |
| 261 |
> |
l = m; |
| 262 |
> |
} |
| 263 |
> |
} |
| 264 |
> |
if (ncnts == 0) |
| 265 |
> |
return; /* no contributions! */ |
| 266 |
> |
/* accumulate tail */ |
| 267 |
> |
for (sn = ncnts-1; sn > 0; sn--) |
| 268 |
> |
cntord[sn-1].brt += cntord[sn].brt; |
| 269 |
> |
/* compute number to check */ |
| 270 |
> |
nshadcheck = pow((double)ncnts, shadcert) + .5; |
| 271 |
> |
/* modify threshold */ |
| 272 |
> |
ourthresh = shadthresh / r->rweight; |
| 273 |
|
/* test for shadows */ |
| 274 |
< |
for (sn = 0; sn < nsources; sn++) { |
| 274 |
> |
nhits = 0; |
| 275 |
> |
for (sn = 0; sn < ncnts; sn++) { |
| 276 |
|
/* check threshold */ |
| 277 |
< |
if (srccnt[sn].brt <= shadthresh*bright(r->rcol)/r->rweight) |
| 277 |
> |
if ((sn+nshadcheck>=ncnts ? cntord[sn].brt : |
| 278 |
> |
cntord[sn].brt-cntord[sn+nshadcheck].brt) |
| 279 |
> |
< ourthresh*bright(r->rcol)) |
| 280 |
|
break; |
| 305 |
– |
/* get statistics */ |
| 306 |
– |
hwt = (double)source[srccnt[sn].sno].nhits / |
| 307 |
– |
(double)source[srccnt[sn].sno].ntests; |
| 308 |
– |
test2 += hwt; |
| 309 |
– |
source[srccnt[sn].sno].ntests++; |
| 281 |
|
/* test for hit */ |
| 282 |
|
rayorigin(&sr, r, SHADOW, 1.0); |
| 283 |
< |
VCOPY(sr.rdir, srccnt[sn].dir); |
| 283 |
> |
VCOPY(sr.rdir, srccnt[cntord[sn].sndx].dir); |
| 284 |
> |
sr.rsrc = srccnt[cntord[sn].sndx].sno; |
| 285 |
> |
source[sr.rsrc].ntests++; /* keep statistics */ |
| 286 |
|
if (localhit(&sr, &thescene) && |
| 287 |
< |
sr.ro != source[srccnt[sn].sno].so) { |
| 288 |
< |
/* check for transmission */ |
| 289 |
< |
if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod)) |
| 290 |
< |
raytrans(&sr); /* object is clipped */ |
| 291 |
< |
else |
| 292 |
< |
rayshade(&sr, sr.ro->omod); |
| 287 |
> |
( sr.ro != source[sr.rsrc].so || |
| 288 |
> |
source[sr.rsrc].sflags & SFOLLOW )) { |
| 289 |
> |
/* follow entire path */ |
| 290 |
> |
raycont(&sr); |
| 291 |
> |
if (trace != NULL) |
| 292 |
> |
(*trace)(&sr); /* trace execution */ |
| 293 |
|
if (bright(sr.rcol) <= FTINY) |
| 294 |
|
continue; /* missed! */ |
| 295 |
< |
(*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom); |
| 296 |
< |
multcolor(srccnt[sn].val, sr.rcol); |
| 295 |
> |
copycolor(srccnt[cntord[sn].sndx].val, sr.rcol); |
| 296 |
> |
multcolor(srccnt[cntord[sn].sndx].val, |
| 297 |
> |
srccnt[cntord[sn].sndx].coef); |
| 298 |
|
} |
| 299 |
|
/* add contribution if hit */ |
| 300 |
< |
addcolor(r->rcol, srccnt[sn].val); |
| 301 |
< |
hit2 += hwt; |
| 302 |
< |
source[srccnt[sn].sno].nhits++; |
| 300 |
> |
addcolor(r->rcol, srccnt[cntord[sn].sndx].val); |
| 301 |
> |
nhits++; |
| 302 |
> |
source[sr.rsrc].nhits++; |
| 303 |
|
} |
| 304 |
< |
if (test2 > FTINY) /* weighted hit rate */ |
| 305 |
< |
hwt = hit2 / test2; |
| 304 |
> |
/* surface hit rate */ |
| 305 |
> |
if (sn > 0) |
| 306 |
> |
hwt = (double)nhits / (double)sn; |
| 307 |
|
else |
| 308 |
< |
hwt = 0.0; |
| 308 |
> |
hwt = 0.5; |
| 309 |
|
#ifdef DEBUG |
| 310 |
< |
fprintf(stderr, "%d tested, %f hit rate\n", sn, hwt); |
| 310 |
> |
sprintf(errmsg, "%d tested, %d untested, %f hit rate\n", |
| 311 |
> |
sn, ncnts-sn, hwt); |
| 312 |
> |
eputs(errmsg); |
| 313 |
|
#endif |
| 314 |
|
/* add in untested sources */ |
| 315 |
< |
for ( ; sn < nsources; sn++) { |
| 316 |
< |
if (srccnt[sn].brt <= 0.0) |
| 317 |
< |
break; |
| 318 |
< |
dtmp = hwt * (double)source[srccnt[sn].sno].nhits / |
| 319 |
< |
(double)source[srccnt[sn].sno].ntests; |
| 320 |
< |
scalecolor(srccnt[sn].val, dtmp); |
| 344 |
< |
addcolor(r->rcol, srccnt[sn].val); |
| 315 |
> |
for ( ; sn < ncnts; sn++) { |
| 316 |
> |
sr.rsrc = srccnt[cntord[sn].sndx].sno; |
| 317 |
> |
prob = hwt * (double)source[sr.rsrc].nhits / |
| 318 |
> |
(double)source[sr.rsrc].ntests; |
| 319 |
> |
scalecolor(srccnt[cntord[sn].sndx].val, prob); |
| 320 |
> |
addcolor(r->rcol, srccnt[cntord[sn].sndx].val); |
| 321 |
|
} |
| 346 |
– |
|
| 347 |
– |
free(srccnt); |
| 322 |
|
} |
| 349 |
– |
|
| 350 |
– |
|
| 351 |
– |
#define wrongsource(m, r) (m->otype!=MAT_ILLUM && \ |
| 352 |
– |
r->rsrc>=0 && \ |
| 353 |
– |
source[r->rsrc].so!=r->ro) |
| 354 |
– |
|
| 355 |
– |
#define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \ |
| 356 |
– |
!(r->rtype&REFLECTED)) /* hack! */ |
| 357 |
– |
|
| 358 |
– |
#define passillum(m, r) (m->otype==MAT_ILLUM && \ |
| 359 |
– |
!(r->rsrc>=0&&source[r->rsrc].so==r->ro)) |
| 360 |
– |
|
| 361 |
– |
|
| 362 |
– |
m_light(m, r) /* ray hit a light source */ |
| 363 |
– |
register OBJREC *m; |
| 364 |
– |
register RAY *r; |
| 365 |
– |
{ |
| 366 |
– |
/* check for behind */ |
| 367 |
– |
if (r->rod < 0.0) |
| 368 |
– |
return; |
| 369 |
– |
/* check for over-counting */ |
| 370 |
– |
if (wrongsource(m, r) || badambient(m, r)) |
| 371 |
– |
return; |
| 372 |
– |
/* check for passed illum */ |
| 373 |
– |
if (passillum(m, r)) { |
| 374 |
– |
|
| 375 |
– |
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
| 376 |
– |
raytrans(r); |
| 377 |
– |
else |
| 378 |
– |
rayshade(r, modifier(m->oargs.sarg[0])); |
| 379 |
– |
|
| 380 |
– |
/* otherwise treat as source */ |
| 381 |
– |
} else { |
| 382 |
– |
/* get distribution pattern */ |
| 383 |
– |
raytexture(r, m->omod); |
| 384 |
– |
/* get source color */ |
| 385 |
– |
setcolor(r->rcol, m->oargs.farg[0], |
| 386 |
– |
m->oargs.farg[1], |
| 387 |
– |
m->oargs.farg[2]); |
| 388 |
– |
/* modify value */ |
| 389 |
– |
multcolor(r->rcol, r->pcol); |
| 390 |
– |
} |
| 391 |
– |
} |
| 392 |
– |
|
| 393 |
– |
|
| 394 |
– |
o_source() {} /* intersection with a source is done elsewhere */ |