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"; |
12 |
|
|
13 |
|
#include "ray.h" |
14 |
|
|
15 |
< |
#include "source.h" |
15 |
> |
#include "octree.h" |
16 |
|
|
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 */ |
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 |
|
|
28 |
– |
SOURCE srcval[MAXSOURCE]; /* our array of sources */ |
29 |
– |
int nsources = 0; /* the number of sources */ |
41 |
|
|
31 |
– |
|
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 && |
47 |
< |
m->otype != MAT_ILLUM && |
48 |
< |
m->otype != MAT_GLOW && |
49 |
< |
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 (nsources >= MAXSOURCE) |
72 |
< |
error(INTERNAL, "too many sources 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(&srcval[nsources], o); |
75 |
> |
if ((ns = newsource()) < 0) |
76 |
> |
goto memerr; |
77 |
|
|
78 |
+ |
setsource(&source[ns], o); |
79 |
+ |
|
80 |
|
if (m->otype == MAT_GLOW) { |
81 |
< |
srcval[nsources].sflags |= SPROX; |
82 |
< |
srcval[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 |
< |
srcval[nsources].sflags |= SSKIP; |
84 |
> |
source[ns].sflags |= SSKIP; |
85 |
|
} else if (m->otype == MAT_SPOT) { |
86 |
< |
srcval[nsources].sflags |= SSPOT; |
87 |
< |
srcval[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 |
|
} |
75 |
– |
nsources++; |
96 |
|
} |
97 |
< |
} |
98 |
< |
|
99 |
< |
|
80 |
< |
newsource(src, so) /* add a source to the array */ |
81 |
< |
register SOURCE *src; |
82 |
< |
register OBJREC *so; |
83 |
< |
{ |
84 |
< |
double cos(), tan(), sqrt(); |
85 |
< |
double theta; |
86 |
< |
FACE *f; |
87 |
< |
CONE *co; |
88 |
< |
int j; |
89 |
< |
register int i; |
90 |
< |
|
91 |
< |
src->sflags = 0; |
92 |
< |
src->so = so; |
93 |
< |
|
94 |
< |
switch (so->otype) { |
95 |
< |
case OBJ_SOURCE: |
96 |
< |
if (so->oargs.nfargs != 4) |
97 |
< |
objerror(so, USER, "bad arguments"); |
98 |
< |
src->sflags |= SDISTANT; |
99 |
< |
VCOPY(src->sloc, so->oargs.farg); |
100 |
< |
if (normalize(src->sloc) == 0.0) |
101 |
< |
objerror(so, USER, "zero direction"); |
102 |
< |
theta = PI/180.0/2.0 * so->oargs.farg[3]; |
103 |
< |
if (theta <= FTINY) |
104 |
< |
objerror(so, USER, "zero size"); |
105 |
< |
src->ss = theta >= PI/4 ? 1.0 : tan(theta); |
106 |
< |
src->ss2 = 2.0*PI * (1.0 - cos(theta)); |
107 |
< |
break; |
108 |
< |
case OBJ_SPHERE: |
109 |
< |
VCOPY(src->sloc, so->oargs.farg); |
110 |
< |
src->ss = so->oargs.farg[3]; |
111 |
< |
src->ss2 = PI * src->ss * src->ss; |
112 |
< |
break; |
113 |
< |
case OBJ_FACE: |
114 |
< |
/* get the face */ |
115 |
< |
f = getface(so); |
116 |
< |
/* find the center */ |
117 |
< |
for (j = 0; j < 3; j++) { |
118 |
< |
src->sloc[j] = 0.0; |
119 |
< |
for (i = 0; i < f->nv; i++) |
120 |
< |
src->sloc[j] += VERTEX(f,i)[j]; |
121 |
< |
src->sloc[j] /= f->nv; |
122 |
< |
} |
123 |
< |
if (!inface(src->sloc, f)) |
124 |
< |
objerror(so, USER, "cannot hit center"); |
125 |
< |
src->ss = sqrt(f->area / PI); |
126 |
< |
src->ss2 = f->area; |
127 |
< |
break; |
128 |
< |
case OBJ_RING: |
129 |
< |
/* get the ring */ |
130 |
< |
co = getcone(so, 0); |
131 |
< |
VCOPY(src->sloc, CO_P0(co)); |
132 |
< |
if (CO_R0(co) > 0.0) |
133 |
< |
objerror(so, USER, "cannot hit center"); |
134 |
< |
src->ss = CO_R1(co); |
135 |
< |
src->ss2 = PI * src->ss * src->ss; |
136 |
< |
break; |
137 |
< |
default: |
138 |
< |
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 |
+ |
/* allocate our contribution arrays */ |
103 |
+ |
maxcntr = nsources + MAXSPART*2; /* 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 |
< |
SPOT * |
144 |
< |
makespot(m) /* make a spotlight */ |
145 |
< |
register OBJREC *m; |
146 |
< |
{ |
147 |
< |
extern double cos(); |
148 |
< |
register SPOT *ns; |
149 |
< |
|
150 |
< |
if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
151 |
< |
error(SYSTEM, "out of memory in makespot"); |
152 |
< |
ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3])); |
153 |
< |
VCOPY(ns->aim, m->oargs.farg+4); |
154 |
< |
if ((ns->flen = normalize(ns->aim)) == 0.0) |
155 |
< |
objerror(m, USER, "zero focus vector"); |
156 |
< |
return(ns); |
157 |
< |
} |
158 |
< |
|
159 |
< |
|
160 |
< |
double |
161 |
< |
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; |
170 |
< |
register int i; |
119 |
> |
double d; /* distance to source */ |
120 |
> |
FVECT vd; |
121 |
> |
register SRCREC *srcp; |
122 |
> |
register int i; |
123 |
|
|
124 |
< |
if (srcval[sn].sflags & SSKIP) |
173 |
< |
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 (srcval[sn].sflags & SDISTANT) |
180 |
< |
/* constant direction */ |
181 |
< |
VCOPY(sr->rdir, srcval[sn].sloc); |
182 |
< |
else { /* compute direction */ |
183 |
< |
for (i = 0; i < 3; i++) |
184 |
< |
sr->rdir[i] = srcval[sn].sloc[i] - sr->rorg[i]; |
185 |
< |
|
186 |
< |
if (srcval[sn].so->otype == OBJ_FACE) |
187 |
< |
norm = getface(srcval[sn].so)->norm; |
188 |
< |
else if (srcval[sn].so->otype == OBJ_RING) |
189 |
< |
norm = getcone(srcval[sn].so,0)->ad; |
190 |
< |
|
191 |
< |
if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY) |
192 |
< |
return(0.0); /* behind surface! */ |
193 |
< |
} |
194 |
< |
if (dstrsrc > FTINY) { |
195 |
< |
/* distribute source direction */ |
196 |
< |
for (i = 0; i < 3; i++) |
197 |
< |
vd[i] = dstrsrc * srcval[sn].ss * (1.0 - 2.0*frandom()); |
198 |
< |
|
199 |
< |
if (norm != NULL) { /* project offset */ |
200 |
< |
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 */ |
141 |
< |
sr->rdir[i] += vd[i]; |
142 |
< |
|
207 |
< |
} else if (srcval[sn].sflags & SDISTANT) |
208 |
< |
/* already normalized */ |
209 |
< |
return(srcval[sn].ss2); |
210 |
< |
|
211 |
< |
if ((d = normalize(sr->rdir)) == 0.0) |
212 |
< |
/* at source! */ |
213 |
< |
return(0.0); |
214 |
< |
|
215 |
< |
if (srcval[sn].sflags & SDISTANT) |
216 |
< |
/* domega constant */ |
217 |
< |
return(srcval[sn].ss2); |
218 |
< |
|
219 |
< |
else { |
140 |
> |
return(1); /* sample OK */ |
141 |
> |
} |
142 |
> |
/* local source */ |
143 |
|
/* check proximity */ |
144 |
< |
if (srcval[sn].sflags & SPROX && |
145 |
< |
d > srcval[sn].sl.prox) |
223 |
< |
return(0.0); |
224 |
< |
|
225 |
< |
if (norm != NULL) |
226 |
< |
ddot /= d; |
227 |
< |
else |
228 |
< |
ddot = 1.0; |
144 |
> |
if (srcp->sflags & SPROX && d > srcp->sl.prox) |
145 |
> |
continue; |
146 |
|
/* check angle */ |
147 |
< |
if (srcval[sn].sflags & SSPOT) { |
148 |
< |
if (srcval[sn].sl.s->siz < 2.0*PI * |
149 |
< |
(1.0 + DOT(srcval[sn].sl.s->aim,sr->rdir))) |
150 |
< |
return(0.0); |
151 |
< |
d += srcval[sn].sl.s->flen; |
152 |
< |
} |
153 |
< |
/* return domega */ |
154 |
< |
return(ddot*srcval[sn].ss2/(d*d)); |
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; |
246 |
< |
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 (srcval[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(srcval[i].sloc,r->rdir)) |
182 |
< |
<= srcval[i].ss2) { |
183 |
< |
r->ro = srcval[i].so; |
184 |
< |
if (!(srcval[i].sflags & SSKIP)) |
185 |
< |
break; |
186 |
< |
} |
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 |
> |
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 |
< |
if (r->ro != NULL) { |
194 |
< |
for (i = 0; i < 3; i++) |
195 |
< |
r->ron[i] = -r->rdir[i]; |
196 |
< |
r->rod = 1.0; |
197 |
< |
r->rofs = 1.0; setident4(r->rofx); |
198 |
< |
r->robs = 1.0; setident4(r->robx); |
193 |
> |
|
194 |
> |
static int |
195 |
> |
cntcmp(sc1, sc2) /* contribution compare (descending) */ |
196 |
> |
register CNTPTR *sc1, *sc2; |
197 |
> |
{ |
198 |
> |
if (sc1->brt > sc2->brt) |
199 |
> |
return(-1); |
200 |
> |
if (sc1->brt < sc2->brt) |
201 |
|
return(1); |
273 |
– |
} |
202 |
|
return(0); |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
< |
#define wrongsource(m, r) (m->otype!=MAT_ILLUM && \ |
207 |
< |
r->rsrc>=0 && \ |
208 |
< |
srcval[r->rsrc].so!=r->ro) |
209 |
< |
|
282 |
< |
#define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \ |
283 |
< |
!(r->rtype&REFLECTED) && /* hack! */\ |
284 |
< |
!(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3])) |
285 |
< |
|
286 |
< |
#define passillum(m, r) (m->otype==MAT_ILLUM && \ |
287 |
< |
!(r->rsrc>=0&&srcval[r->rsrc].so==r->ro)) |
288 |
< |
|
289 |
< |
|
290 |
< |
m_light(m, r) /* ray hit a light source */ |
291 |
< |
register OBJREC *m; |
292 |
< |
register RAY *r; |
206 |
> |
direct(r, f, p) /* add direct component */ |
207 |
> |
RAY *r; /* ray that hit surface */ |
208 |
> |
int (*f)(); /* direct component coefficient function */ |
209 |
> |
char *p; /* data for f */ |
210 |
|
{ |
211 |
< |
/* check for behind */ |
212 |
< |
if (r->rod < 0.0) |
213 |
< |
return; |
214 |
< |
/* check for over-counting */ |
215 |
< |
if (wrongsource(m, r) || badambient(m, r)) |
216 |
< |
return; |
217 |
< |
/* check for passed illum */ |
218 |
< |
if (passillum(m, r)) { |
211 |
> |
extern int (*trace)(); |
212 |
> |
extern double pow(); |
213 |
> |
register int sn; |
214 |
> |
SRCINDEX si; |
215 |
> |
int nshadcheck, ncnts; |
216 |
> |
int nhits; |
217 |
> |
double prob, ourthresh, hwt; |
218 |
> |
RAY sr; |
219 |
> |
/* NOTE: srccnt and cntord global so no recursion */ |
220 |
> |
if (nsources <= 0) |
221 |
> |
return; /* no sources?! */ |
222 |
> |
/* potential contributions */ |
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].coef, p, sr.rdir, si.dom); |
238 |
> |
cntord[sn].brt = bright(srccnt[sn].coef); |
239 |
> |
if (cntord[sn].brt <= 0.0) |
240 |
> |
continue; |
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(cntord, sn, sizeof(CNTPTR), cntcmp); |
251 |
> |
{ /* find last */ |
252 |
> |
register int l, m; |
253 |
|
|
254 |
< |
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
255 |
< |
raytrans(r); |
256 |
< |
else |
257 |
< |
rayshade(r, modifier(m->oargs.sarg[0])); |
258 |
< |
|
259 |
< |
/* otherwise treat as source */ |
260 |
< |
} else { |
261 |
< |
/* get distribution pattern */ |
262 |
< |
raytexture(r, m->omod); |
312 |
< |
/* get source color */ |
313 |
< |
setcolor(r->rcol, m->oargs.farg[0], |
314 |
< |
m->oargs.farg[1], |
315 |
< |
m->oargs.farg[2]); |
316 |
< |
/* modify value */ |
317 |
< |
multcolor(r->rcol, r->pcol); |
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 |
+ |
/* accumulate tail */ |
265 |
+ |
for (sn = ncnts-1; sn > 0; sn--) |
266 |
+ |
cntord[sn-1].brt += cntord[sn].brt; |
267 |
+ |
/* compute number to check */ |
268 |
+ |
nshadcheck = pow((double)ncnts, shadcert) + .5; |
269 |
+ |
/* modify threshold */ |
270 |
+ |
ourthresh = shadthresh / r->rweight; |
271 |
+ |
/* test for shadows */ |
272 |
+ |
nhits = 0; |
273 |
+ |
for (sn = 0; sn < ncnts; sn++) { |
274 |
+ |
/* check threshold */ |
275 |
+ |
if ((sn+nshadcheck>=ncnts ? cntord[sn].brt : |
276 |
+ |
cntord[sn].brt-cntord[sn+nshadcheck].brt) |
277 |
+ |
< ourthresh*bright(r->rcol)) |
278 |
+ |
break; |
279 |
+ |
/* test for hit */ |
280 |
+ |
rayorigin(&sr, r, SHADOW, 1.0); |
281 |
+ |
VCOPY(sr.rdir, srccnt[cntord[sn].sndx].dir); |
282 |
+ |
sr.rsrc = srccnt[cntord[sn].sndx].sno; |
283 |
+ |
source[sr.rsrc].ntests++; /* keep statistics */ |
284 |
+ |
if (localhit(&sr, &thescene) && |
285 |
+ |
( sr.ro != source[sr.rsrc].so || |
286 |
+ |
source[sr.rsrc].sflags & SFOLLOW )) { |
287 |
+ |
/* follow entire path */ |
288 |
+ |
raycont(&sr); |
289 |
+ |
if (trace != NULL) |
290 |
+ |
(*trace)(&sr); /* trace execution */ |
291 |
+ |
if (bright(sr.rcol) <= FTINY) |
292 |
+ |
continue; /* missed! */ |
293 |
+ |
copycolor(srccnt[cntord[sn].sndx].val, sr.rcol); |
294 |
+ |
multcolor(srccnt[cntord[sn].sndx].val, |
295 |
+ |
srccnt[cntord[sn].sndx].coef); |
296 |
+ |
} |
297 |
+ |
/* add contribution if hit */ |
298 |
+ |
addcolor(r->rcol, srccnt[cntord[sn].sndx].val); |
299 |
+ |
nhits++; |
300 |
+ |
source[sr.rsrc].nhits++; |
301 |
+ |
} |
302 |
+ |
/* surface hit rate */ |
303 |
+ |
if (sn > 0) |
304 |
+ |
hwt = (double)nhits / (double)sn; |
305 |
+ |
else |
306 |
+ |
hwt = 0.5; |
307 |
+ |
#ifdef DEBUG |
308 |
+ |
sprintf(errmsg, "%d tested, %d untested, %f hit rate\n", |
309 |
+ |
sn, ncnts-sn, hwt); |
310 |
+ |
eputs(errmsg); |
311 |
+ |
#endif |
312 |
+ |
/* add in untested sources */ |
313 |
+ |
for ( ; sn < ncnts; sn++) { |
314 |
+ |
sr.rsrc = srccnt[cntord[sn].sndx].sno; |
315 |
+ |
prob = hwt * (double)source[sr.rsrc].nhits / |
316 |
+ |
(double)source[sr.rsrc].ntests; |
317 |
+ |
scalecolor(srccnt[cntord[sn].sndx].val, prob); |
318 |
+ |
addcolor(r->rcol, srccnt[cntord[sn].sndx].val); |
319 |
+ |
} |
320 |
|
} |
320 |
– |
|
321 |
– |
|
322 |
– |
o_source() {} /* intersection with a source is done elsewhere */ |