| 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 |
|
|
| 23 |
– |
#include "face.h" |
| 24 |
– |
|
| 21 |
|
#include "random.h" |
| 22 |
|
|
| 23 |
+ |
/* |
| 24 |
+ |
* Structures used by direct() |
| 25 |
+ |
*/ |
| 26 |
|
|
| 27 |
< |
extern double dstrsrc; /* source distribution amount */ |
| 28 |
< |
extern double shadthresh; /* relative shadow threshold */ |
| 27 |
> |
typedef struct { |
| 28 |
> |
FVECT dir; /* source direction */ |
| 29 |
> |
COLOR coef; /* material coefficient */ |
| 30 |
> |
COLOR val; /* contribution */ |
| 31 |
> |
} CONTRIB; /* direct contribution */ |
| 32 |
|
|
| 33 |
< |
SRCREC *source = NULL; /* our list of sources */ |
| 34 |
< |
int nsources = 0; /* the number of sources */ |
| 33 |
> |
typedef struct { |
| 34 |
> |
int sno; /* source number */ |
| 35 |
> |
float brt; /* brightness (for comparison) */ |
| 36 |
> |
} CNTPTR; /* contribution pointer */ |
| 37 |
|
|
| 38 |
+ |
static CONTRIB *srccnt; /* source contributions in direct() */ |
| 39 |
+ |
static CNTPTR *cntord; /* source ordering in direct() */ |
| 40 |
|
|
| 41 |
+ |
|
| 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 && |
| 52 |
< |
m->otype != MAT_SPOT) |
| 59 |
> |
if (!islight(m->otype)) |
| 60 |
|
continue; |
| 61 |
|
|
| 62 |
|
if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 : |
| 68 |
|
m->oargs.farg[3] <= FTINY) |
| 69 |
|
continue; /* don't bother */ |
| 70 |
|
|
| 71 |
< |
if (source == NULL) |
| 72 |
< |
source = (SRCREC *)malloc(sizeof(SRCREC)); |
| 73 |
< |
else |
| 67 |
< |
source = (SRCREC *)realloc((char *)source, |
| 68 |
< |
(unsigned)(nsources+1)*sizeof(SRCREC)); |
| 69 |
< |
if (source == NULL) |
| 70 |
< |
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 |
+ |
setsource(&source[ns], o); |
| 79 |
+ |
|
| 80 |
|
if (m->otype == MAT_GLOW) { |
| 81 |
< |
source[nsources].sflags |= SPROX; |
| 82 |
< |
source[nsources].sl.prox = m->oargs.farg[3]; |
| 81 |
> |
source[ns].sflags |= SPROX; |
| 82 |
> |
source[ns].sl.prox = m->oargs.farg[3]; |
| 83 |
|
if (o->otype == OBJ_SOURCE) |
| 84 |
< |
source[nsources].sflags |= SSKIP; |
| 84 |
> |
source[ns].sflags |= SSKIP; |
| 85 |
|
} else if (m->otype == MAT_SPOT) { |
| 86 |
< |
source[nsources].sflags |= SSPOT; |
| 87 |
< |
source[nsources].sl.s = makespot(m); |
| 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 |
|
} |
| 83 |
– |
nsources++; |
| 96 |
|
} |
| 97 |
< |
} |
| 98 |
< |
|
| 99 |
< |
|
| 88 |
< |
newsource(src, so) /* add a source to the array */ |
| 89 |
< |
register SRCREC *src; |
| 90 |
< |
register OBJREC *so; |
| 91 |
< |
{ |
| 92 |
< |
double cos(), tan(), sqrt(); |
| 93 |
< |
double theta; |
| 94 |
< |
FACE *f; |
| 95 |
< |
CONE *co; |
| 96 |
< |
int j; |
| 97 |
< |
register int i; |
| 98 |
< |
|
| 99 |
< |
src->sflags = 0; |
| 100 |
< |
src->nhits = 1; src->ntests = 2; /* start probability = 1/2 */ |
| 101 |
< |
src->so = so; |
| 102 |
< |
|
| 103 |
< |
switch (so->otype) { |
| 104 |
< |
case OBJ_SOURCE: |
| 105 |
< |
if (so->oargs.nfargs != 4) |
| 106 |
< |
objerror(so, USER, "bad arguments"); |
| 107 |
< |
src->sflags |= SDISTANT; |
| 108 |
< |
VCOPY(src->sloc, so->oargs.farg); |
| 109 |
< |
if (normalize(src->sloc) == 0.0) |
| 110 |
< |
objerror(so, USER, "zero direction"); |
| 111 |
< |
theta = PI/180.0/2.0 * so->oargs.farg[3]; |
| 112 |
< |
if (theta <= FTINY) |
| 113 |
< |
objerror(so, USER, "zero size"); |
| 114 |
< |
src->ss = theta >= PI/4 ? 1.0 : tan(theta); |
| 115 |
< |
src->ss2 = 2.0*PI * (1.0 - cos(theta)); |
| 116 |
< |
break; |
| 117 |
< |
case OBJ_SPHERE: |
| 118 |
< |
VCOPY(src->sloc, so->oargs.farg); |
| 119 |
< |
src->ss = so->oargs.farg[3]; |
| 120 |
< |
src->ss2 = PI * src->ss * src->ss; |
| 121 |
< |
break; |
| 122 |
< |
case OBJ_FACE: |
| 123 |
< |
/* get the face */ |
| 124 |
< |
f = getface(so); |
| 125 |
< |
/* find the center */ |
| 126 |
< |
for (j = 0; j < 3; j++) { |
| 127 |
< |
src->sloc[j] = 0.0; |
| 128 |
< |
for (i = 0; i < f->nv; i++) |
| 129 |
< |
src->sloc[j] += VERTEX(f,i)[j]; |
| 130 |
< |
src->sloc[j] /= f->nv; |
| 131 |
< |
} |
| 132 |
< |
if (!inface(src->sloc, f)) |
| 133 |
< |
objerror(so, USER, "cannot hit center"); |
| 134 |
< |
src->ss = sqrt(f->area / PI); |
| 135 |
< |
src->ss2 = f->area; |
| 136 |
< |
break; |
| 137 |
< |
case OBJ_RING: |
| 138 |
< |
/* get the ring */ |
| 139 |
< |
co = getcone(so, 0); |
| 140 |
< |
VCOPY(src->sloc, CO_P0(co)); |
| 141 |
< |
if (CO_R0(co) > 0.0) |
| 142 |
< |
objerror(so, USER, "cannot hit center"); |
| 143 |
< |
src->ss = CO_R1(co); |
| 144 |
< |
src->ss2 = PI * src->ss * src->ss; |
| 145 |
< |
break; |
| 146 |
< |
default: |
| 147 |
< |
objerror(so, USER, "illegal material"); |
| 97 |
> |
if (nsources <= 0) { |
| 98 |
> |
error(WARNING, "no light sources found"); |
| 99 |
> |
return; |
| 100 |
|
} |
| 101 |
+ |
markvirtuals(); /* find and add virtual sources */ |
| 102 |
+ |
srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB)); |
| 103 |
+ |
cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR)); |
| 104 |
+ |
if (srccnt == NULL || cntord == NULL) |
| 105 |
+ |
goto memerr; |
| 106 |
+ |
return; |
| 107 |
+ |
memerr: |
| 108 |
+ |
error(SYSTEM, "out of memory in marksources"); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 152 |
– |
SPOT * |
| 153 |
– |
makespot(m) /* make a spotlight */ |
| 154 |
– |
register OBJREC *m; |
| 155 |
– |
{ |
| 156 |
– |
extern double cos(); |
| 157 |
– |
register SPOT *ns; |
| 158 |
– |
|
| 159 |
– |
if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
| 160 |
– |
error(SYSTEM, "out of memory in makespot"); |
| 161 |
– |
ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3])); |
| 162 |
– |
VCOPY(ns->aim, m->oargs.farg+4); |
| 163 |
– |
if ((ns->flen = normalize(ns->aim)) == 0.0) |
| 164 |
– |
objerror(m, USER, "zero focus vector"); |
| 165 |
– |
return(ns); |
| 166 |
– |
} |
| 167 |
– |
|
| 168 |
– |
|
| 112 |
|
double |
| 113 |
|
srcray(sr, r, sn) /* send a ray to a source, return domega */ |
| 114 |
|
register RAY *sr; /* returned source ray */ |
| 115 |
|
RAY *r; /* ray which hit object */ |
| 116 |
|
register int sn; /* source number */ |
| 117 |
|
{ |
| 175 |
– |
register double *norm = NULL; /* plane normal */ |
| 118 |
|
double ddot; /* (distance times) cosine */ |
| 119 |
|
FVECT vd; |
| 120 |
|
double d; |
| 127 |
|
|
| 128 |
|
sr->rsrc = sn; /* remember source */ |
| 129 |
|
/* get source direction */ |
| 130 |
< |
if (source[sn].sflags & SDISTANT) |
| 130 |
> |
if (source[sn].sflags & SDISTANT) { |
| 131 |
|
/* constant direction */ |
| 132 |
|
VCOPY(sr->rdir, source[sn].sloc); |
| 133 |
< |
else { /* compute direction */ |
| 133 |
> |
} else { /* compute direction */ |
| 134 |
|
for (i = 0; i < 3; i++) |
| 135 |
|
sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i]; |
| 136 |
|
|
| 137 |
< |
if (source[sn].so->otype == OBJ_FACE) |
| 138 |
< |
norm = getface(source[sn].so)->norm; |
| 197 |
< |
else if (source[sn].so->otype == OBJ_RING) |
| 198 |
< |
norm = getcone(source[sn].so,0)->ad; |
| 199 |
< |
|
| 200 |
< |
if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY) |
| 137 |
> |
if (source[sn].sflags & SFLAT && |
| 138 |
> |
(ddot = -DOT(sr->rdir, source[sn].snorm)) <= FTINY) |
| 139 |
|
return(0.0); /* behind surface! */ |
| 140 |
|
} |
| 141 |
|
if (dstrsrc > FTINY) { |
| 142 |
|
/* distribute source direction */ |
| 143 |
+ |
dimlist[ndims] = sn + 8831; |
| 144 |
+ |
peano(vd, 3, urand(ilhash(dimlist,ndims+1)+samplendx), .01); |
| 145 |
|
for (i = 0; i < 3; i++) |
| 146 |
< |
vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom()); |
| 147 |
< |
|
| 148 |
< |
if (norm != NULL) { /* project offset */ |
| 209 |
< |
d = DOT(vd, norm); |
| 146 |
> |
vd[i] = dstrsrc * source[sn].ss * (1. - 2.*vd[i]); |
| 147 |
> |
if (source[sn].sflags & SFLAT) { /* project offset */ |
| 148 |
> |
d = DOT(vd, source[sn].snorm); |
| 149 |
|
for (i = 0; i < 3; i++) |
| 150 |
< |
vd[i] -= d * norm[i]; |
| 150 |
> |
vd[i] -= d * source[sn].snorm[i]; |
| 151 |
|
} |
| 152 |
|
for (i = 0; i < 3; i++) /* offset source direction */ |
| 153 |
|
sr->rdir[i] += vd[i]; |
| 154 |
+ |
/* normalize */ |
| 155 |
+ |
d = normalize(sr->rdir); |
| 156 |
|
|
| 157 |
< |
} else if (source[sn].sflags & SDISTANT) |
| 158 |
< |
/* already normalized */ |
| 159 |
< |
return(source[sn].ss2); |
| 157 |
> |
} else if (!(source[sn].sflags & SDISTANT)) |
| 158 |
> |
/* normalize direction */ |
| 159 |
> |
d = normalize(sr->rdir); |
| 160 |
|
|
| 161 |
< |
if ((d = normalize(sr->rdir)) == 0.0) |
| 162 |
< |
/* at source! */ |
| 161 |
> |
if (source[sn].sflags & SDISTANT) { |
| 162 |
> |
if (source[sn].sflags & SSPOT) { /* check location */ |
| 163 |
> |
for (i = 0; i < 3; i++) |
| 164 |
> |
vd[i] = source[sn].sl.s->aim[i] - sr->rorg[i]; |
| 165 |
> |
d = DOT(sr->rdir,vd); |
| 166 |
> |
if (d <= FTINY) |
| 167 |
> |
return(0.0); |
| 168 |
> |
d = DOT(vd,vd) - d*d; |
| 169 |
> |
if (PI*d > source[sn].sl.s->siz) |
| 170 |
> |
return(0.0); |
| 171 |
> |
} |
| 172 |
> |
return(source[sn].ss2); /* domega constant */ |
| 173 |
> |
} |
| 174 |
> |
/* check direction */ |
| 175 |
> |
if (d == 0.0) |
| 176 |
|
return(0.0); |
| 223 |
– |
|
| 224 |
– |
if (source[sn].sflags & SDISTANT) |
| 225 |
– |
/* domega constant */ |
| 226 |
– |
return(source[sn].ss2); |
| 227 |
– |
|
| 228 |
– |
else { |
| 177 |
|
/* check proximity */ |
| 178 |
< |
if (source[sn].sflags & SPROX && |
| 179 |
< |
d > source[sn].sl.prox) |
| 180 |
< |
return(0.0); |
| 181 |
< |
|
| 182 |
< |
if (norm != NULL) |
| 183 |
< |
ddot /= d; |
| 184 |
< |
else |
| 185 |
< |
ddot = 1.0; |
| 178 |
> |
if (source[sn].sflags & SPROX && |
| 179 |
> |
d > source[sn].sl.prox) |
| 180 |
> |
return(0.0); |
| 181 |
> |
/* compute dot product */ |
| 182 |
> |
if (source[sn].sflags & SFLAT) |
| 183 |
> |
ddot /= d; |
| 184 |
> |
else |
| 185 |
> |
ddot = 1.0; |
| 186 |
|
/* check angle */ |
| 187 |
< |
if (source[sn].sflags & SSPOT) { |
| 188 |
< |
if (source[sn].sl.s->siz < 2.0*PI * |
| 187 |
> |
if (source[sn].sflags & SSPOT) { |
| 188 |
> |
if (source[sn].sl.s->siz < 2.0*PI * |
| 189 |
|
(1.0 + DOT(source[sn].sl.s->aim,sr->rdir))) |
| 190 |
< |
return(0.0); |
| 191 |
< |
d += source[sn].sl.s->flen; |
| 244 |
< |
} |
| 245 |
< |
/* return domega */ |
| 246 |
< |
return(ddot*source[sn].ss2/(d*d)); |
| 190 |
> |
return(0.0); |
| 191 |
> |
d += source[sn].sl.s->flen; /* adjust length */ |
| 192 |
|
} |
| 193 |
+ |
/* compute domega */ |
| 194 |
+ |
return(ddot*source[sn].ss2/(d*d)); |
| 195 |
|
} |
| 196 |
|
|
| 197 |
|
|
| 198 |
< |
sourcehit(r) /* check to see if ray hit distant source */ |
| 199 |
< |
register RAY *r; |
| 198 |
> |
srcvalue(r) /* punch ray to source and compute value */ |
| 199 |
> |
RAY *r; |
| 200 |
|
{ |
| 201 |
< |
int first, last; |
| 255 |
< |
register int i; |
| 201 |
> |
register SRCREC *sp; |
| 202 |
|
|
| 203 |
< |
if (r->rsrc >= 0) { /* check only one if aimed */ |
| 204 |
< |
first = last = r->rsrc; |
| 205 |
< |
} else { /* otherwise check all */ |
| 206 |
< |
first = 0; last = nsources-1; |
| 203 |
> |
sp = &source[r->rsrc]; |
| 204 |
> |
if (sp->sflags & SVIRTUAL) { /* virtual source */ |
| 205 |
> |
/* check intersection */ |
| 206 |
> |
if (!(*ofun[sp->so->otype].funp)(sp->so, r)) |
| 207 |
> |
return; |
| 208 |
> |
raycont(r); /* compute contribution */ |
| 209 |
> |
return; |
| 210 |
|
} |
| 211 |
< |
for (i = first; i <= last; i++) |
| 212 |
< |
if (source[i].sflags & SDISTANT) |
| 213 |
< |
/* |
| 214 |
< |
* Check to see if ray is within |
| 215 |
< |
* solid angle of source. |
| 216 |
< |
*/ |
| 217 |
< |
if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir)) |
| 269 |
< |
<= source[i].ss2) { |
| 270 |
< |
r->ro = source[i].so; |
| 271 |
< |
if (!(source[i].sflags & SSKIP)) |
| 272 |
< |
break; |
| 273 |
< |
} |
| 274 |
< |
|
| 275 |
< |
if (r->ro != NULL) { |
| 276 |
< |
for (i = 0; i < 3; i++) |
| 277 |
< |
r->ron[i] = -r->rdir[i]; |
| 278 |
< |
r->rod = 1.0; |
| 279 |
< |
r->rofs = 1.0; setident4(r->rofx); |
| 280 |
< |
r->robs = 1.0; setident4(r->robx); |
| 281 |
< |
return(1); |
| 211 |
> |
/* compute intersection */ |
| 212 |
> |
if (sp->sflags & SDISTANT ? sourcehit(r) : |
| 213 |
> |
(*ofun[sp->so->otype].funp)(sp->so, r)) { |
| 214 |
> |
if (sp->sa.success >= 0) |
| 215 |
> |
sp->sa.success++; |
| 216 |
> |
raycont(r); /* compute contribution */ |
| 217 |
> |
return; |
| 218 |
|
} |
| 219 |
< |
return(0); |
| 219 |
> |
if (sp->sa.success < 0) |
| 220 |
> |
return; /* bitched already */ |
| 221 |
> |
sp->sa.success -= AIMREQT; |
| 222 |
> |
if (sp->sa.success >= 0) |
| 223 |
> |
return; /* leniency */ |
| 224 |
> |
sprintf(errmsg, "aiming failure for light source \"%s\"", |
| 225 |
> |
sp->so->oname); |
| 226 |
> |
error(WARNING, errmsg); /* issue warning */ |
| 227 |
|
} |
| 228 |
|
|
| 229 |
|
|
| 230 |
|
static int |
| 231 |
|
cntcmp(sc1, sc2) /* contribution compare (descending) */ |
| 232 |
< |
register CONTRIB *sc1, *sc2; |
| 232 |
> |
register CNTPTR *sc1, *sc2; |
| 233 |
|
{ |
| 234 |
|
if (sc1->brt > sc2->brt) |
| 235 |
|
return(-1); |
| 244 |
|
int (*f)(); /* direct component coefficient function */ |
| 245 |
|
char *p; /* data for f */ |
| 246 |
|
{ |
| 247 |
+ |
extern int (*trace)(); |
| 248 |
+ |
extern double pow(); |
| 249 |
|
register int sn; |
| 250 |
< |
register CONTRIB *srccnt; |
| 251 |
< |
double dtmp, hwt, test2, hit2; |
| 250 |
> |
int nshadcheck, ncnts; |
| 251 |
> |
int nhits; |
| 252 |
> |
double dom, prob, ourthresh, hwt; |
| 253 |
|
RAY sr; |
| 254 |
< |
|
| 255 |
< |
if ((srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB))) == NULL) |
| 256 |
< |
error(SYSTEM, "out of memory in direct"); |
| 254 |
> |
/* NOTE: srccnt and cntord global so no recursion */ |
| 255 |
> |
if (nsources <= 0) |
| 256 |
> |
return; /* no sources?! */ |
| 257 |
> |
/* compute number to check */ |
| 258 |
> |
nshadcheck = pow((double)nsources, shadcert) + .5; |
| 259 |
> |
/* modify threshold */ |
| 260 |
> |
ourthresh = shadthresh / r->rweight; |
| 261 |
|
/* potential contributions */ |
| 262 |
|
for (sn = 0; sn < nsources; sn++) { |
| 263 |
< |
srccnt[sn].sno = sn; |
| 264 |
< |
setcolor(srccnt[sn].val, 0.0, 0.0, 0.0); |
| 315 |
< |
srccnt[sn].brt = 0.0; |
| 263 |
> |
cntord[sn].sno = sn; |
| 264 |
> |
cntord[sn].brt = 0.0; |
| 265 |
|
/* get source ray */ |
| 266 |
< |
if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0) |
| 266 |
> |
if ((dom = srcray(&sr, r, sn)) == 0.0) |
| 267 |
|
continue; |
| 268 |
|
VCOPY(srccnt[sn].dir, sr.rdir); |
| 269 |
|
/* compute coefficient */ |
| 270 |
< |
(*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom); |
| 271 |
< |
srccnt[sn].brt = bright(srccnt[sn].val); |
| 272 |
< |
if (srccnt[sn].brt <= FTINY) |
| 270 |
> |
(*f)(srccnt[sn].coef, p, srccnt[sn].dir, dom); |
| 271 |
> |
cntord[sn].brt = bright(srccnt[sn].coef); |
| 272 |
> |
if (cntord[sn].brt <= 0.0) |
| 273 |
|
continue; |
| 274 |
< |
/* compute intersection */ |
| 275 |
< |
if (!( source[sn].sflags & SDISTANT ? |
| 276 |
< |
sourcehit(&sr) : |
| 277 |
< |
(*ofun[source[sn].so->otype].funp) |
| 278 |
< |
(source[sn].so, &sr) )) |
| 279 |
< |
continue; |
| 331 |
< |
/* compute contribution */ |
| 332 |
< |
rayshade(&sr, sr.ro->omod); |
| 333 |
< |
multcolor(srccnt[sn].val, sr.rcol); |
| 334 |
< |
srccnt[sn].brt = bright(srccnt[sn].val); |
| 274 |
> |
/* compute potential */ |
| 275 |
> |
sr.revf = srcvalue; |
| 276 |
> |
rayvalue(&sr); |
| 277 |
> |
copycolor(srccnt[sn].val, sr.rcol); |
| 278 |
> |
multcolor(srccnt[sn].val, srccnt[sn].coef); |
| 279 |
> |
cntord[sn].brt = bright(srccnt[sn].val); |
| 280 |
|
} |
| 281 |
|
/* sort contributions */ |
| 282 |
< |
qsort(srccnt, nsources, sizeof(CONTRIB), cntcmp); |
| 283 |
< |
hit2 = 0.0; test2 = FTINY; |
| 282 |
> |
qsort(cntord, nsources, sizeof(CNTPTR), cntcmp); |
| 283 |
> |
{ /* find last */ |
| 284 |
> |
register int l, m; |
| 285 |
> |
|
| 286 |
> |
sn = 0; ncnts = l = nsources; |
| 287 |
> |
while ((m = (sn + ncnts) >> 1) != l) { |
| 288 |
> |
if (cntord[m].brt > 0.0) |
| 289 |
> |
sn = m; |
| 290 |
> |
else |
| 291 |
> |
ncnts = m; |
| 292 |
> |
l = m; |
| 293 |
> |
} |
| 294 |
> |
} |
| 295 |
> |
/* accumulate tail */ |
| 296 |
> |
for (sn = ncnts-1; sn > 0; sn--) |
| 297 |
> |
cntord[sn-1].brt += cntord[sn].brt; |
| 298 |
|
/* test for shadows */ |
| 299 |
< |
for (sn = 0; sn < nsources; sn++) { |
| 299 |
> |
nhits = 0; |
| 300 |
> |
for (sn = 0; sn < ncnts; sn++) { |
| 301 |
|
/* check threshold */ |
| 302 |
< |
if (srccnt[sn].brt <= shadthresh*bright(r->rcol)/r->rweight) |
| 302 |
> |
if ((sn+nshadcheck>=ncnts ? cntord[sn].brt : |
| 303 |
> |
cntord[sn].brt-cntord[sn+nshadcheck].brt) |
| 304 |
> |
< ourthresh*bright(r->rcol)) |
| 305 |
|
break; |
| 306 |
|
/* get statistics */ |
| 307 |
< |
hwt = (double)source[srccnt[sn].sno].nhits / |
| 346 |
< |
(double)source[srccnt[sn].sno].ntests; |
| 347 |
< |
test2 += hwt; |
| 348 |
< |
source[srccnt[sn].sno].ntests++; |
| 307 |
> |
source[cntord[sn].sno].ntests++; |
| 308 |
|
/* test for hit */ |
| 309 |
|
rayorigin(&sr, r, SHADOW, 1.0); |
| 310 |
< |
VCOPY(sr.rdir, srccnt[sn].dir); |
| 310 |
> |
VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir); |
| 311 |
> |
sr.rsrc = cntord[sn].sno; |
| 312 |
|
if (localhit(&sr, &thescene) && |
| 313 |
< |
sr.ro != source[srccnt[sn].sno].so) { |
| 314 |
< |
/* check for transmission */ |
| 315 |
< |
if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod)) |
| 316 |
< |
raytrans(&sr); /* object is clipped */ |
| 317 |
< |
else |
| 318 |
< |
rayshade(&sr, sr.ro->omod); |
| 313 |
> |
( sr.ro != source[cntord[sn].sno].so || |
| 314 |
> |
source[cntord[sn].sno].sflags & SFOLLOW )) { |
| 315 |
> |
/* follow entire path */ |
| 316 |
> |
raycont(&sr); |
| 317 |
> |
if (trace != NULL) |
| 318 |
> |
(*trace)(&sr); /* trace execution */ |
| 319 |
|
if (bright(sr.rcol) <= FTINY) |
| 320 |
|
continue; /* missed! */ |
| 321 |
< |
(*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom); |
| 322 |
< |
multcolor(srccnt[sn].val, sr.rcol); |
| 321 |
> |
copycolor(srccnt[cntord[sn].sno].val, sr.rcol); |
| 322 |
> |
multcolor(srccnt[cntord[sn].sno].val, |
| 323 |
> |
srccnt[cntord[sn].sno].coef); |
| 324 |
|
} |
| 325 |
|
/* add contribution if hit */ |
| 326 |
< |
addcolor(r->rcol, srccnt[sn].val); |
| 327 |
< |
hit2 += hwt; |
| 328 |
< |
source[srccnt[sn].sno].nhits++; |
| 326 |
> |
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
| 327 |
> |
nhits++; |
| 328 |
> |
source[cntord[sn].sno].nhits++; |
| 329 |
|
} |
| 330 |
< |
/* weighted hit rate */ |
| 331 |
< |
hwt = hit2 / test2; |
| 330 |
> |
/* surface hit rate */ |
| 331 |
> |
if (sn > 0) |
| 332 |
> |
hwt = (double)nhits / (double)sn; |
| 333 |
> |
else |
| 334 |
> |
hwt = 0.5; |
| 335 |
|
#ifdef DEBUG |
| 336 |
< |
{ |
| 337 |
< |
int ntested = sn; |
| 336 |
> |
sprintf(errmsg, "%d tested, %d untested, %f hit rate\n", |
| 337 |
> |
sn, ncnts-sn, hwt); |
| 338 |
> |
eputs(errmsg); |
| 339 |
|
#endif |
| 340 |
|
/* add in untested sources */ |
| 341 |
< |
for ( ; sn < nsources; sn++) { |
| 342 |
< |
if (srccnt[sn].brt <= 0.0) |
| 343 |
< |
break; |
| 344 |
< |
dtmp = hwt * (double)source[srccnt[sn].sno].nhits / |
| 345 |
< |
(double)source[srccnt[sn].sno].ntests; |
| 381 |
< |
scalecolor(srccnt[sn].val, dtmp); |
| 382 |
< |
addcolor(r->rcol, srccnt[sn].val); |
| 341 |
> |
for ( ; sn < ncnts; sn++) { |
| 342 |
> |
prob = hwt * (double)source[cntord[sn].sno].nhits / |
| 343 |
> |
(double)source[cntord[sn].sno].ntests; |
| 344 |
> |
scalecolor(srccnt[cntord[sn].sno].val, prob); |
| 345 |
> |
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
| 346 |
|
} |
| 384 |
– |
#ifdef DEBUG |
| 385 |
– |
fprintf(stderr, "%d tested, %d untested, %f hit rate\n", |
| 386 |
– |
ntested, sn-ntested, hwt); |
| 387 |
– |
} |
| 388 |
– |
#endif |
| 389 |
– |
|
| 390 |
– |
free(srccnt); |
| 347 |
|
} |
| 392 |
– |
|
| 393 |
– |
|
| 394 |
– |
#define wrongsource(m, r) (m->otype!=MAT_ILLUM && \ |
| 395 |
– |
r->rsrc>=0 && \ |
| 396 |
– |
source[r->rsrc].so!=r->ro) |
| 397 |
– |
|
| 398 |
– |
#define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \ |
| 399 |
– |
!(r->rtype&REFLECTED) && /* hack! */\ |
| 400 |
– |
!(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3])) |
| 401 |
– |
|
| 402 |
– |
#define passillum(m, r) (m->otype==MAT_ILLUM && \ |
| 403 |
– |
!(r->rsrc>=0&&source[r->rsrc].so==r->ro)) |
| 404 |
– |
|
| 405 |
– |
|
| 406 |
– |
m_light(m, r) /* ray hit a light source */ |
| 407 |
– |
register OBJREC *m; |
| 408 |
– |
register RAY *r; |
| 409 |
– |
{ |
| 410 |
– |
/* check for behind */ |
| 411 |
– |
if (r->rod < 0.0) |
| 412 |
– |
return; |
| 413 |
– |
/* check for over-counting */ |
| 414 |
– |
if (wrongsource(m, r) || badambient(m, r)) |
| 415 |
– |
return; |
| 416 |
– |
/* check for passed illum */ |
| 417 |
– |
if (passillum(m, r)) { |
| 418 |
– |
|
| 419 |
– |
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
| 420 |
– |
raytrans(r); |
| 421 |
– |
else |
| 422 |
– |
rayshade(r, modifier(m->oargs.sarg[0])); |
| 423 |
– |
|
| 424 |
– |
/* otherwise treat as source */ |
| 425 |
– |
} else { |
| 426 |
– |
/* get distribution pattern */ |
| 427 |
– |
raytexture(r, m->omod); |
| 428 |
– |
/* get source color */ |
| 429 |
– |
setcolor(r->rcol, m->oargs.farg[0], |
| 430 |
– |
m->oargs.farg[1], |
| 431 |
– |
m->oargs.farg[2]); |
| 432 |
– |
/* modify value */ |
| 433 |
– |
multcolor(r->rcol, r->pcol); |
| 434 |
– |
} |
| 435 |
– |
} |
| 436 |
– |
|
| 437 |
– |
|
| 438 |
– |
o_source() {} /* intersection with a source is done elsewhere */ |