1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
4 |
> |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
|
#endif |
6 |
|
|
7 |
|
/* |
14 |
|
|
15 |
|
#include "octree.h" |
16 |
|
|
17 |
+ |
#include "otypes.h" |
18 |
+ |
|
19 |
|
#include "source.h" |
20 |
|
|
21 |
< |
#include "otypes.h" |
21 |
> |
#include "random.h" |
22 |
|
|
23 |
< |
#include "cone.h" |
23 |
> |
extern double ssampdist; /* scatter sampling distance */ |
24 |
|
|
25 |
< |
#include "face.h" |
25 |
> |
#ifndef MAXSSAMP |
26 |
> |
#define MAXSSAMP 16 /* maximum samples per ray */ |
27 |
> |
#endif |
28 |
|
|
29 |
< |
#include "random.h" |
29 |
> |
/* |
30 |
> |
* Structures used by direct() |
31 |
> |
*/ |
32 |
|
|
33 |
+ |
typedef struct { |
34 |
+ |
int sno; /* source number */ |
35 |
+ |
FVECT dir; /* source direction */ |
36 |
+ |
COLOR coef; /* material coefficient */ |
37 |
+ |
COLOR val; /* contribution */ |
38 |
+ |
} CONTRIB; /* direct contribution */ |
39 |
|
|
40 |
< |
extern double dstrsrc; /* source distribution amount */ |
41 |
< |
extern double shadthresh; /* relative shadow threshold */ |
42 |
< |
extern double shadcert; /* shadow testing certainty */ |
40 |
> |
typedef struct { |
41 |
> |
int sndx; /* source index (to CONTRIB array) */ |
42 |
> |
float brt; /* brightness (for comparison) */ |
43 |
> |
} CNTPTR; /* contribution pointer */ |
44 |
|
|
45 |
< |
SRCREC *source = NULL; /* our list of sources */ |
46 |
< |
int nsources = 0; /* the number of sources */ |
45 |
> |
static CONTRIB *srccnt; /* source contributions in direct() */ |
46 |
> |
static CNTPTR *cntord; /* source ordering in direct() */ |
47 |
> |
static int maxcntr = 0; /* size of contribution arrays */ |
48 |
|
|
49 |
|
|
50 |
|
marksources() /* find and mark source objects */ |
51 |
|
{ |
52 |
+ |
int foundsource = 0; |
53 |
+ |
int i; |
54 |
|
register OBJREC *o, *m; |
55 |
< |
register int i; |
56 |
< |
|
55 |
> |
register int ns; |
56 |
> |
/* initialize dispatch table */ |
57 |
> |
initstypes(); |
58 |
> |
/* find direct sources */ |
59 |
|
for (i = 0; i < nobjects; i++) { |
60 |
|
|
61 |
|
o = objptr(i); |
62 |
|
|
63 |
< |
if (o->omod == OVOID) |
63 |
> |
if (!issurface(o->otype) || o->omod == OVOID) |
64 |
|
continue; |
65 |
|
|
66 |
|
m = objptr(o->omod); |
76 |
|
o->otype != OBJ_SOURCE && |
77 |
|
m->oargs.farg[3] <= FTINY) |
78 |
|
continue; /* don't bother */ |
79 |
+ |
if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY && |
80 |
+ |
m->oargs.farg[2] <= FTINY) |
81 |
+ |
continue; /* don't bother */ |
82 |
|
|
83 |
< |
if (source == NULL) |
84 |
< |
source = (SRCREC *)malloc(sizeof(SRCREC)); |
85 |
< |
else |
65 |
< |
source = (SRCREC *)realloc((char *)source, |
66 |
< |
(unsigned)(nsources+1)*sizeof(SRCREC)); |
67 |
< |
if (source == NULL) |
68 |
< |
error(SYSTEM, "out of memory in marksources"); |
83 |
> |
if (sfun[o->otype].of == NULL || |
84 |
> |
sfun[o->otype].of->setsrc == NULL) |
85 |
> |
objerror(o, USER, "illegal material"); |
86 |
|
|
87 |
< |
newsource(&source[nsources], o); |
87 |
> |
if ((ns = newsource()) < 0) |
88 |
> |
goto memerr; |
89 |
|
|
90 |
+ |
setsource(&source[ns], o); |
91 |
+ |
|
92 |
|
if (m->otype == MAT_GLOW) { |
93 |
< |
source[nsources].sflags |= SPROX; |
94 |
< |
source[nsources].sl.prox = m->oargs.farg[3]; |
95 |
< |
if (o->otype == OBJ_SOURCE) |
96 |
< |
source[nsources].sflags |= SSKIP; |
93 |
> |
source[ns].sflags |= SPROX; |
94 |
> |
source[ns].sl.prox = m->oargs.farg[3]; |
95 |
> |
if (source[ns].sflags & SDISTANT) |
96 |
> |
source[ns].sflags |= SSKIP; |
97 |
|
} else if (m->otype == MAT_SPOT) { |
98 |
< |
source[nsources].sflags |= SSPOT; |
99 |
< |
source[nsources].sl.s = makespot(m); |
98 |
> |
source[ns].sflags |= SSPOT; |
99 |
> |
if ((source[ns].sl.s = makespot(m)) == NULL) |
100 |
> |
goto memerr; |
101 |
> |
if (source[ns].sflags & SFLAT && |
102 |
> |
!checkspot(source[ns].sl.s,source[ns].snorm)) { |
103 |
> |
objerror(o, WARNING, |
104 |
> |
"invalid spotlight direction"); |
105 |
> |
source[ns].sflags |= SSKIP; |
106 |
> |
} |
107 |
|
} |
108 |
< |
nsources++; |
108 |
> |
if (!(source[ns].sflags & SSKIP)) |
109 |
> |
foundsource++; |
110 |
|
} |
111 |
< |
} |
112 |
< |
|
113 |
< |
|
86 |
< |
newsource(src, so) /* add a source to the array */ |
87 |
< |
register SRCREC *src; |
88 |
< |
register OBJREC *so; |
89 |
< |
{ |
90 |
< |
double cos(), tan(), sqrt(); |
91 |
< |
double theta; |
92 |
< |
FACE *f; |
93 |
< |
CONE *co; |
94 |
< |
int j; |
95 |
< |
register int i; |
96 |
< |
|
97 |
< |
src->sflags = 0; |
98 |
< |
src->nhits = 1; src->ntests = 2; /* start probability = 1/2 */ |
99 |
< |
src->so = so; |
100 |
< |
|
101 |
< |
switch (so->otype) { |
102 |
< |
case OBJ_SOURCE: |
103 |
< |
if (so->oargs.nfargs != 4) |
104 |
< |
objerror(so, USER, "bad arguments"); |
105 |
< |
src->sflags |= SDISTANT; |
106 |
< |
VCOPY(src->sloc, so->oargs.farg); |
107 |
< |
if (normalize(src->sloc) == 0.0) |
108 |
< |
objerror(so, USER, "zero direction"); |
109 |
< |
theta = PI/180.0/2.0 * so->oargs.farg[3]; |
110 |
< |
if (theta <= FTINY) |
111 |
< |
objerror(so, USER, "zero size"); |
112 |
< |
src->ss = theta >= PI/4 ? 1.0 : tan(theta); |
113 |
< |
src->ss2 = 2.0*PI * (1.0 - cos(theta)); |
114 |
< |
break; |
115 |
< |
case OBJ_SPHERE: |
116 |
< |
VCOPY(src->sloc, so->oargs.farg); |
117 |
< |
src->ss = so->oargs.farg[3]; |
118 |
< |
src->ss2 = PI * src->ss * src->ss; |
119 |
< |
break; |
120 |
< |
case OBJ_FACE: |
121 |
< |
/* get the face */ |
122 |
< |
f = getface(so); |
123 |
< |
/* find the center */ |
124 |
< |
for (j = 0; j < 3; j++) { |
125 |
< |
src->sloc[j] = 0.0; |
126 |
< |
for (i = 0; i < f->nv; i++) |
127 |
< |
src->sloc[j] += VERTEX(f,i)[j]; |
128 |
< |
src->sloc[j] /= f->nv; |
129 |
< |
} |
130 |
< |
if (!inface(src->sloc, f)) |
131 |
< |
objerror(so, USER, "cannot hit center"); |
132 |
< |
src->ss = sqrt(f->area / PI); |
133 |
< |
src->ss2 = f->area; |
134 |
< |
break; |
135 |
< |
case OBJ_RING: |
136 |
< |
/* get the ring */ |
137 |
< |
co = getcone(so, 0); |
138 |
< |
VCOPY(src->sloc, CO_P0(co)); |
139 |
< |
if (CO_R0(co) > 0.0) |
140 |
< |
objerror(so, USER, "cannot hit center"); |
141 |
< |
src->ss = CO_R1(co); |
142 |
< |
src->ss2 = PI * src->ss * src->ss; |
143 |
< |
break; |
144 |
< |
default: |
145 |
< |
objerror(so, USER, "illegal material"); |
111 |
> |
if (!foundsource) { |
112 |
> |
error(WARNING, "no light sources found"); |
113 |
> |
return; |
114 |
|
} |
115 |
+ |
markvirtuals(); /* find and add virtual sources */ |
116 |
+ |
/* allocate our contribution arrays */ |
117 |
+ |
maxcntr = nsources + MAXSPART; /* start with this many */ |
118 |
+ |
srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB)); |
119 |
+ |
cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR)); |
120 |
+ |
if (srccnt == NULL | cntord == NULL) |
121 |
+ |
goto memerr; |
122 |
+ |
return; |
123 |
+ |
memerr: |
124 |
+ |
error(SYSTEM, "out of memory in marksources"); |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
< |
SPOT * |
151 |
< |
makespot(m) /* make a spotlight */ |
152 |
< |
register OBJREC *m; |
153 |
< |
{ |
154 |
< |
extern double cos(); |
155 |
< |
register SPOT *ns; |
156 |
< |
|
157 |
< |
if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
158 |
< |
error(SYSTEM, "out of memory in makespot"); |
159 |
< |
ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3])); |
160 |
< |
VCOPY(ns->aim, m->oargs.farg+4); |
161 |
< |
if ((ns->flen = normalize(ns->aim)) == 0.0) |
162 |
< |
objerror(m, USER, "zero focus vector"); |
163 |
< |
return(ns); |
164 |
< |
} |
165 |
< |
|
166 |
< |
|
167 |
< |
double |
168 |
< |
srcray(sr, r, sn) /* send a ray to a source, return domega */ |
128 |
> |
srcray(sr, r, si) /* send a ray to a source, return domega */ |
129 |
|
register RAY *sr; /* returned source ray */ |
130 |
|
RAY *r; /* ray which hit object */ |
131 |
< |
register int sn; /* source number */ |
131 |
> |
SRCINDEX *si; /* source sample index */ |
132 |
|
{ |
133 |
< |
register double *norm = NULL; /* plane normal */ |
134 |
< |
double ddot; /* (distance times) cosine */ |
175 |
< |
FVECT vd; |
176 |
< |
double d; |
177 |
< |
register int i; |
133 |
> |
double d; /* distance to source */ |
134 |
> |
register SRCREC *srcp; |
135 |
|
|
136 |
< |
if (source[sn].sflags & SSKIP) |
180 |
< |
return(0.0); /* skip this source */ |
136 |
> |
rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */ |
137 |
|
|
138 |
< |
rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */ |
139 |
< |
|
140 |
< |
sr->rsrc = sn; /* remember source */ |
141 |
< |
/* get source direction */ |
142 |
< |
if (source[sn].sflags & SDISTANT) |
143 |
< |
/* constant direction */ |
144 |
< |
VCOPY(sr->rdir, source[sn].sloc); |
189 |
< |
else { /* compute direction */ |
190 |
< |
for (i = 0; i < 3; i++) |
191 |
< |
sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i]; |
192 |
< |
|
193 |
< |
if (source[sn].so->otype == OBJ_FACE) |
194 |
< |
norm = getface(source[sn].so)->norm; |
195 |
< |
else if (source[sn].so->otype == OBJ_RING) |
196 |
< |
norm = getcone(source[sn].so,0)->ad; |
197 |
< |
|
198 |
< |
if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY) |
199 |
< |
return(0.0); /* behind surface! */ |
138 |
> |
while ((d = nextssamp(sr, si)) != 0.0) { |
139 |
> |
sr->rsrc = si->sn; /* remember source */ |
140 |
> |
srcp = source + si->sn; |
141 |
> |
if (srcp->sflags & SDISTANT) { |
142 |
> |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) |
143 |
> |
continue; |
144 |
> |
return(1); /* sample OK */ |
145 |
|
} |
146 |
< |
if (dstrsrc > FTINY) { |
147 |
< |
/* distribute source direction */ |
148 |
< |
for (i = 0; i < 3; i++) |
149 |
< |
vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom()); |
146 |
> |
/* local source */ |
147 |
> |
/* check proximity */ |
148 |
> |
if (srcp->sflags & SPROX && d > srcp->sl.prox) |
149 |
> |
continue; |
150 |
> |
/* check angle */ |
151 |
> |
if (srcp->sflags & SSPOT) { |
152 |
> |
if (spotout(sr, srcp->sl.s)) |
153 |
> |
continue; |
154 |
> |
/* adjust solid angle */ |
155 |
> |
si->dom *= d*d; |
156 |
> |
d += srcp->sl.s->flen; |
157 |
> |
si->dom /= d*d; |
158 |
> |
} |
159 |
> |
return(1); /* sample OK */ |
160 |
> |
} |
161 |
> |
return(0); /* no more samples */ |
162 |
> |
} |
163 |
|
|
206 |
– |
if (norm != NULL) { /* project offset */ |
207 |
– |
d = DOT(vd, norm); |
208 |
– |
for (i = 0; i < 3; i++) |
209 |
– |
vd[i] -= d * norm[i]; |
210 |
– |
} |
211 |
– |
for (i = 0; i < 3; i++) /* offset source direction */ |
212 |
– |
sr->rdir[i] += vd[i]; |
164 |
|
|
165 |
< |
} else if (source[sn].sflags & SDISTANT) |
166 |
< |
/* already normalized */ |
167 |
< |
return(source[sn].ss2); |
165 |
> |
srcvalue(r) /* punch ray to source and compute value */ |
166 |
> |
register RAY *r; |
167 |
> |
{ |
168 |
> |
register SRCREC *sp; |
169 |
|
|
170 |
< |
if ((d = normalize(sr->rdir)) == 0.0) |
171 |
< |
/* at source! */ |
172 |
< |
return(0.0); |
173 |
< |
|
174 |
< |
if (source[sn].sflags & SDISTANT) |
175 |
< |
/* domega constant */ |
176 |
< |
return(source[sn].ss2); |
177 |
< |
|
178 |
< |
else { |
227 |
< |
/* check proximity */ |
228 |
< |
if (source[sn].sflags & SPROX && |
229 |
< |
d > source[sn].sl.prox) |
230 |
< |
return(0.0); |
231 |
< |
|
232 |
< |
if (norm != NULL) |
233 |
< |
ddot /= d; |
234 |
< |
else |
235 |
< |
ddot = 1.0; |
236 |
< |
/* check angle */ |
237 |
< |
if (source[sn].sflags & SSPOT) { |
238 |
< |
if (source[sn].sl.s->siz < 2.0*PI * |
239 |
< |
(1.0 + DOT(source[sn].sl.s->aim,sr->rdir))) |
240 |
< |
return(0.0); |
241 |
< |
d += source[sn].sl.s->flen; |
242 |
< |
} |
243 |
< |
/* return domega */ |
244 |
< |
return(ddot*source[sn].ss2/(d*d)); |
170 |
> |
sp = &source[r->rsrc]; |
171 |
> |
if (sp->sflags & SVIRTUAL) { /* virtual source */ |
172 |
> |
/* check intersection */ |
173 |
> |
if (!(*ofun[sp->so->otype].funp)(sp->so, r)) |
174 |
> |
return; |
175 |
> |
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
176 |
> |
goto nomat; |
177 |
> |
rayparticipate(r); |
178 |
> |
return; |
179 |
|
} |
180 |
+ |
/* compute intersection */ |
181 |
+ |
if (sp->sflags & SDISTANT ? sourcehit(r) : |
182 |
+ |
(*ofun[sp->so->otype].funp)(sp->so, r)) { |
183 |
+ |
if (sp->sa.success >= 0) |
184 |
+ |
sp->sa.success++; |
185 |
+ |
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
186 |
+ |
goto nomat; |
187 |
+ |
rayparticipate(r); |
188 |
+ |
return; |
189 |
+ |
} |
190 |
+ |
/* we missed our mark! */ |
191 |
+ |
if (sp->sa.success < 0) |
192 |
+ |
return; /* bitched already */ |
193 |
+ |
sp->sa.success -= AIMREQT; |
194 |
+ |
if (sp->sa.success >= 0) |
195 |
+ |
return; /* leniency */ |
196 |
+ |
sprintf(errmsg, "aiming failure for light source \"%s\"", |
197 |
+ |
sp->so->oname); |
198 |
+ |
error(WARNING, errmsg); /* issue warning */ |
199 |
+ |
return; |
200 |
+ |
nomat: |
201 |
+ |
objerror(r->ro, USER, "material not found"); |
202 |
|
} |
203 |
|
|
204 |
|
|
214 |
|
first = 0; last = nsources-1; |
215 |
|
} |
216 |
|
for (i = first; i <= last; i++) |
217 |
< |
if (source[i].sflags & SDISTANT) |
217 |
> |
if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT) |
218 |
|
/* |
219 |
|
* Check to see if ray is within |
220 |
|
* solid angle of source. |
227 |
|
} |
228 |
|
|
229 |
|
if (r->ro != NULL) { |
230 |
+ |
r->robj = objndx(r->ro); |
231 |
|
for (i = 0; i < 3; i++) |
232 |
|
r->ron[i] = -r->rdir[i]; |
233 |
|
r->rod = 1.0; |
234 |
< |
r->rofs = 1.0; setident4(r->rofx); |
278 |
< |
r->robs = 1.0; setident4(r->robx); |
234 |
> |
r->rox = NULL; |
235 |
|
return(1); |
236 |
|
} |
237 |
|
return(0); |
255 |
|
int (*f)(); /* direct component coefficient function */ |
256 |
|
char *p; /* data for f */ |
257 |
|
{ |
258 |
< |
extern double pow(); |
258 |
> |
extern int (*trace)(); |
259 |
|
register int sn; |
260 |
< |
register CONTRIB *srccnt; |
261 |
< |
register CNTPTR *cntord; |
260 |
> |
register CONTRIB *scp; |
261 |
> |
SRCINDEX si; |
262 |
|
int nshadcheck, ncnts; |
263 |
< |
double prob, ourthresh, hwt, test2, hit2; |
263 |
> |
int nhits; |
264 |
> |
double prob, ourthresh, hwt; |
265 |
|
RAY sr; |
266 |
< |
|
266 |
> |
/* NOTE: srccnt and cntord global so no recursion */ |
267 |
|
if (nsources <= 0) |
268 |
< |
return; |
312 |
< |
srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB)); |
313 |
< |
cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR)); |
314 |
< |
if (srccnt == NULL || cntord == NULL) |
315 |
< |
error(SYSTEM, "out of memory in direct"); |
316 |
< |
/* compute number to check */ |
317 |
< |
nshadcheck = pow((double)nsources, shadcert) + .5; |
318 |
< |
/* modify threshold */ |
319 |
< |
ourthresh = shadthresh / r->rweight; |
268 |
> |
return; /* no sources?! */ |
269 |
|
/* potential contributions */ |
270 |
< |
for (sn = 0; sn < nsources; sn++) { |
271 |
< |
cntord[sn].sno = sn; |
272 |
< |
cntord[sn].brt = 0.0; |
273 |
< |
/* get source ray */ |
274 |
< |
if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0) |
275 |
< |
continue; |
276 |
< |
VCOPY(srccnt[sn].dir, sr.rdir); |
270 |
> |
initsrcindex(&si); |
271 |
> |
for (sn = 0; srcray(&sr, r, &si); sn++) { |
272 |
> |
if (sn >= maxcntr) { |
273 |
> |
maxcntr = sn + MAXSPART; |
274 |
> |
srccnt = (CONTRIB *)realloc((char *)srccnt, |
275 |
> |
maxcntr*sizeof(CONTRIB)); |
276 |
> |
cntord = (CNTPTR *)realloc((char *)cntord, |
277 |
> |
maxcntr*sizeof(CNTPTR)); |
278 |
> |
if (srccnt == NULL | cntord == NULL) |
279 |
> |
error(SYSTEM, "out of memory in direct"); |
280 |
> |
} |
281 |
> |
cntord[sn].sndx = sn; |
282 |
> |
scp = srccnt + sn; |
283 |
> |
scp->sno = sr.rsrc; |
284 |
|
/* compute coefficient */ |
285 |
< |
(*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom); |
286 |
< |
cntord[sn].brt = bright(srccnt[sn].val); |
285 |
> |
(*f)(scp->coef, p, sr.rdir, si.dom); |
286 |
> |
cntord[sn].brt = bright(scp->coef); |
287 |
|
if (cntord[sn].brt <= 0.0) |
288 |
|
continue; |
289 |
< |
/* compute intersection */ |
290 |
< |
if (!( source[sn].sflags & SDISTANT ? |
291 |
< |
sourcehit(&sr) : |
292 |
< |
(*ofun[source[sn].so->otype].funp) |
293 |
< |
(source[sn].so, &sr) )) |
294 |
< |
continue; |
295 |
< |
/* compute contribution */ |
340 |
< |
raycont(&sr); |
341 |
< |
multcolor(srccnt[sn].val, sr.rcol); |
342 |
< |
cntord[sn].brt = bright(srccnt[sn].val); |
289 |
> |
VCOPY(scp->dir, sr.rdir); |
290 |
> |
/* compute potential */ |
291 |
> |
sr.revf = srcvalue; |
292 |
> |
rayvalue(&sr); |
293 |
> |
copycolor(scp->val, sr.rcol); |
294 |
> |
multcolor(scp->val, scp->coef); |
295 |
> |
cntord[sn].brt = bright(scp->val); |
296 |
|
} |
297 |
|
/* sort contributions */ |
298 |
< |
qsort(cntord, nsources, sizeof(CNTPTR), cntcmp); |
298 |
> |
qsort(cntord, sn, sizeof(CNTPTR), cntcmp); |
299 |
|
{ /* find last */ |
300 |
|
register int l, m; |
301 |
|
|
302 |
< |
sn = 0; ncnts = l = nsources; |
302 |
> |
ncnts = l = sn; |
303 |
> |
sn = 0; |
304 |
|
while ((m = (sn + ncnts) >> 1) != l) { |
305 |
|
if (cntord[m].brt > 0.0) |
306 |
|
sn = m; |
309 |
|
l = m; |
310 |
|
} |
311 |
|
} |
312 |
+ |
if (ncnts == 0) |
313 |
+ |
return; /* no contributions! */ |
314 |
|
/* accumulate tail */ |
315 |
|
for (sn = ncnts-1; sn > 0; sn--) |
316 |
|
cntord[sn-1].brt += cntord[sn].brt; |
317 |
< |
/* start with prob=.5 */ |
318 |
< |
hit2 = 0.5; test2 = 1.0; |
317 |
> |
/* compute number to check */ |
318 |
> |
nshadcheck = pow((double)ncnts, shadcert) + .5; |
319 |
> |
/* modify threshold */ |
320 |
> |
ourthresh = shadthresh / r->rweight; |
321 |
|
/* test for shadows */ |
322 |
< |
for (sn = 0; sn < ncnts; sn++) { |
322 |
> |
for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts; |
323 |
> |
hwt += (double)source[scp->sno].nhits / |
324 |
> |
(double)source[scp->sno].ntests, |
325 |
> |
sn++) { |
326 |
|
/* check threshold */ |
327 |
|
if ((sn+nshadcheck>=ncnts ? cntord[sn].brt : |
328 |
< |
cntord[sn].brt-cntord[sn+nshadcheck].brt) < |
329 |
< |
ourthresh*bright(r->rcol)) |
328 |
> |
cntord[sn].brt-cntord[sn+nshadcheck].brt) |
329 |
> |
< ourthresh*bright(r->rcol)) |
330 |
|
break; |
331 |
< |
/* get statistics */ |
371 |
< |
hwt = (double)source[cntord[sn].sno].nhits / |
372 |
< |
(double)source[cntord[sn].sno].ntests; |
373 |
< |
test2 += hwt; |
374 |
< |
source[cntord[sn].sno].ntests++; |
331 |
> |
scp = srccnt + cntord[sn].sndx; |
332 |
|
/* test for hit */ |
333 |
|
rayorigin(&sr, r, SHADOW, 1.0); |
334 |
< |
VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir); |
335 |
< |
sr.rsrc = cntord[sn].sno; |
334 |
> |
VCOPY(sr.rdir, scp->dir); |
335 |
> |
sr.rsrc = scp->sno; |
336 |
> |
source[scp->sno].ntests++; /* keep statistics */ |
337 |
|
if (localhit(&sr, &thescene) && |
338 |
< |
sr.ro != source[cntord[sn].sno].so) { |
339 |
< |
/* check for transmission */ |
338 |
> |
( sr.ro != source[scp->sno].so || |
339 |
> |
source[scp->sno].sflags & SFOLLOW )) { |
340 |
> |
/* follow entire path */ |
341 |
|
raycont(&sr); |
342 |
+ |
rayparticipate(&sr); |
343 |
+ |
if (trace != NULL) |
344 |
+ |
(*trace)(&sr); /* trace execution */ |
345 |
|
if (bright(sr.rcol) <= FTINY) |
346 |
|
continue; /* missed! */ |
347 |
< |
(*f)(srccnt[cntord[sn].sno].val, p, |
348 |
< |
srccnt[cntord[sn].sno].dir, |
387 |
< |
srccnt[cntord[sn].sno].dom); |
388 |
< |
multcolor(srccnt[cntord[sn].sno].val, sr.rcol); |
347 |
> |
copycolor(scp->val, sr.rcol); |
348 |
> |
multcolor(scp->val, scp->coef); |
349 |
|
} |
350 |
|
/* add contribution if hit */ |
351 |
< |
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
352 |
< |
hit2 += hwt; |
353 |
< |
source[cntord[sn].sno].nhits++; |
351 |
> |
addcolor(r->rcol, scp->val); |
352 |
> |
nhits++; |
353 |
> |
source[scp->sno].nhits++; |
354 |
|
} |
355 |
< |
/* weighted hit rate */ |
356 |
< |
hwt = hit2 / test2; |
355 |
> |
/* source hit rate */ |
356 |
> |
if (hwt > FTINY) |
357 |
> |
hwt = (double)nhits / hwt; |
358 |
> |
else |
359 |
> |
hwt = 0.5; |
360 |
|
#ifdef DEBUG |
361 |
< |
sprintf(errmsg, "%d tested, %d untested, %f hit rate\n", |
361 |
> |
sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n", |
362 |
|
sn, ncnts-sn, hwt); |
363 |
|
eputs(errmsg); |
364 |
|
#endif |
365 |
|
/* add in untested sources */ |
366 |
|
for ( ; sn < ncnts; sn++) { |
367 |
< |
prob = hwt * (double)source[cntord[sn].sno].nhits / |
368 |
< |
(double)source[cntord[sn].sno].ntests; |
369 |
< |
scalecolor(srccnt[cntord[sn].sno].val, prob); |
370 |
< |
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
367 |
> |
scp = srccnt + cntord[sn].sndx; |
368 |
> |
prob = hwt * (double)source[scp->sno].nhits / |
369 |
> |
(double)source[scp->sno].ntests; |
370 |
> |
if (prob > 1.0) |
371 |
> |
prob = 1.0; |
372 |
> |
scalecolor(scp->val, prob); |
373 |
> |
addcolor(r->rcol, scp->val); |
374 |
|
} |
409 |
– |
|
410 |
– |
free((char *)srccnt); |
411 |
– |
free((char *)cntord); |
375 |
|
} |
376 |
|
|
377 |
|
|
378 |
< |
#define wrongsource(m, r) (m->otype!=MAT_ILLUM && \ |
379 |
< |
r->rsrc>=0 && \ |
380 |
< |
source[r->rsrc].so!=r->ro) |
378 |
> |
srcscatter(r) /* compute source scattering into ray */ |
379 |
> |
register RAY *r; |
380 |
> |
{ |
381 |
> |
int oldsampndx; |
382 |
> |
int nsamps; |
383 |
> |
RAY sr; |
384 |
> |
SRCINDEX si; |
385 |
> |
double t, d; |
386 |
> |
double re, ge, be; |
387 |
> |
COLOR cvext; |
388 |
> |
int i, j; |
389 |
|
|
390 |
< |
#define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \ |
391 |
< |
!(r->rtype&REFLECTED) && /* hack! */\ |
392 |
< |
!(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3])) |
390 |
> |
if (r->slights == NULL || r->slights[0] == 0 |
391 |
> |
|| r->gecc >= 1.-FTINY || r->rot >= FHUGE) |
392 |
> |
return; |
393 |
> |
if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) |
394 |
> |
nsamps = 1; |
395 |
> |
#if MAXSSAMP |
396 |
> |
else if (nsamps > MAXSSAMP) |
397 |
> |
nsamps = MAXSSAMP; |
398 |
> |
#endif |
399 |
> |
oldsampndx = samplendx; |
400 |
> |
samplendx = random()&0x7fff; /* randomize */ |
401 |
> |
for (i = r->slights[0]; i > 0; i--) { /* for each source */ |
402 |
> |
for (j = 0; j < nsamps; j++) { /* for each sample position */ |
403 |
> |
samplendx++; |
404 |
> |
t = r->rot * (j+frandom())/nsamps; |
405 |
> |
/* extinction */ |
406 |
> |
re = t*colval(r->cext,RED); |
407 |
> |
ge = t*colval(r->cext,GRN); |
408 |
> |
be = t*colval(r->cext,BLU); |
409 |
> |
setcolor(cvext, re > 92. ? 0. : exp(-re), |
410 |
> |
ge > 92. ? 0. : exp(-ge), |
411 |
> |
be > 92. ? 0. : exp(-be)); |
412 |
> |
if (intens(cvext) <= FTINY) |
413 |
> |
break; /* too far away */ |
414 |
> |
sr.rorg[0] = r->rorg[0] + r->rdir[0]*t; |
415 |
> |
sr.rorg[1] = r->rorg[1] + r->rdir[1]*t; |
416 |
> |
sr.rorg[2] = r->rorg[2] + r->rdir[2]*t; |
417 |
> |
sr.rmax = 0.; |
418 |
> |
initsrcindex(&si); /* sample ray to this source */ |
419 |
> |
si.sn = r->slights[i]; |
420 |
> |
nopart(&si, &sr); |
421 |
> |
if (!srcray(&sr, NULL, &si) || |
422 |
> |
sr.rsrc != r->slights[i]) |
423 |
> |
continue; /* no path */ |
424 |
> |
copycolor(sr.cext, r->cext); |
425 |
> |
copycolor(sr.albedo, r->albedo); |
426 |
> |
sr.gecc = r->gecc; |
427 |
> |
sr.slights = r->slights; |
428 |
> |
rayvalue(&sr); /* eval. source ray */ |
429 |
> |
if (bright(sr.rcol) <= FTINY) |
430 |
> |
continue; |
431 |
> |
if (r->gecc <= FTINY) /* compute P(theta) */ |
432 |
> |
d = 1.; |
433 |
> |
else { |
434 |
> |
d = DOT(r->rdir, sr.rdir); |
435 |
> |
d = 1. + r->gecc*r->gecc - 2.*r->gecc*d; |
436 |
> |
d = (1. - r->gecc*r->gecc) / (d*sqrt(d)); |
437 |
> |
} |
438 |
> |
/* other factors */ |
439 |
> |
d *= si.dom * r->rot / (4.*PI*nsamps); |
440 |
> |
multcolor(sr.rcol, r->cext); |
441 |
> |
multcolor(sr.rcol, r->albedo); |
442 |
> |
scalecolor(sr.rcol, d); |
443 |
> |
multcolor(sr.rcol, cvext); |
444 |
> |
addcolor(r->rcol, sr.rcol); /* add it in */ |
445 |
> |
} |
446 |
> |
} |
447 |
> |
samplendx = oldsampndx; |
448 |
> |
} |
449 |
|
|
450 |
+ |
|
451 |
+ |
/**************************************************************** |
452 |
+ |
* The following macros were separated from the m_light() routine |
453 |
+ |
* because they are very nasty and difficult to understand. |
454 |
+ |
*/ |
455 |
+ |
|
456 |
+ |
/* illumblock * |
457 |
+ |
* |
458 |
+ |
* We cannot allow an illum to pass to another illum, because that |
459 |
+ |
* would almost certainly constitute overcounting. |
460 |
+ |
* However, we do allow an illum to pass to another illum |
461 |
+ |
* that is actually going to relay to a virtual light source. |
462 |
+ |
* We also prevent an illum from passing to a glow; this provides a |
463 |
+ |
* convenient mechanism for defining detailed light source |
464 |
+ |
* geometry behind (or inside) an effective radiator. |
465 |
+ |
*/ |
466 |
+ |
|
467 |
+ |
static int weaksrcmod(obj) int obj; /* efficiency booster function */ |
468 |
+ |
{register OBJREC *o = objptr(obj); |
469 |
+ |
return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} |
470 |
+ |
|
471 |
+ |
#define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ |
472 |
+ |
r->rod > 0.0 && \ |
473 |
+ |
weaksrcmod(source[r->rsrc].so->omod)) |
474 |
+ |
|
475 |
+ |
/* wrongsource * |
476 |
+ |
* |
477 |
+ |
* This source is the wrong source (ie. overcounted) if we are |
478 |
+ |
* aimed to a different source than the one we hit and the one |
479 |
+ |
* we hit is not an illum that should be passed. |
480 |
+ |
*/ |
481 |
+ |
|
482 |
+ |
#define wrongsource(m, r) (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \ |
483 |
+ |
(m->otype!=MAT_ILLUM || illumblock(m,r))) |
484 |
+ |
|
485 |
+ |
/* distglow * |
486 |
+ |
* |
487 |
+ |
* A distant glow is an object that sometimes acts as a light source, |
488 |
+ |
* but is too far away from the test point to be one in this case. |
489 |
+ |
* (Glows with negative radii should NEVER participate in illumination.) |
490 |
+ |
*/ |
491 |
+ |
|
492 |
+ |
#define distglow(m, r, d) (m->otype==MAT_GLOW && \ |
493 |
+ |
m->oargs.farg[3] >= -FTINY && \ |
494 |
+ |
d > m->oargs.farg[3]) |
495 |
+ |
|
496 |
+ |
/* badcomponent * |
497 |
+ |
* |
498 |
+ |
* We must avoid counting light sources in the ambient calculation, |
499 |
+ |
* since the direct component is handled separately. Therefore, any |
500 |
+ |
* ambient ray which hits an active light source must be discarded. |
501 |
+ |
* The same is true for stray specular samples, since the specular |
502 |
+ |
* contribution from light sources is calculated separately. |
503 |
+ |
*/ |
504 |
+ |
|
505 |
+ |
#define badcomponent(m, r) (r->crtype&(AMBIENT|SPECULAR) && \ |
506 |
+ |
!(r->crtype&SHADOW || r->rod < 0.0 || \ |
507 |
+ |
/* not 100% correct */ distglow(m, r, r->rot))) |
508 |
+ |
|
509 |
+ |
/* passillum * |
510 |
+ |
* |
511 |
+ |
* An illum passes to another material type when we didn't hit it |
512 |
+ |
* on purpose (as part of a direct calculation), or it is relaying |
513 |
+ |
* a virtual light source. |
514 |
+ |
*/ |
515 |
+ |
|
516 |
|
#define passillum(m, r) (m->otype==MAT_ILLUM && \ |
517 |
< |
!(r->rsrc>=0&&source[r->rsrc].so==r->ro)) |
517 |
> |
(r->rsrc<0 || source[r->rsrc].so!=r->ro || \ |
518 |
> |
source[r->rsrc].sflags&SVIRTUAL)) |
519 |
|
|
520 |
+ |
/* srcignore * |
521 |
+ |
* |
522 |
+ |
* The -dv flag is normally on for sources to be visible. |
523 |
+ |
*/ |
524 |
|
|
525 |
+ |
#define srcignore(m, r) !(directvis || r->crtype&SHADOW || \ |
526 |
+ |
distglow(m, r, raydist(r,PRIMARY))) |
527 |
+ |
|
528 |
+ |
|
529 |
|
m_light(m, r) /* ray hit a light source */ |
530 |
|
register OBJREC *m; |
531 |
|
register RAY *r; |
532 |
|
{ |
533 |
|
/* check for over-counting */ |
534 |
< |
if (wrongsource(m, r) || badambient(m, r)) |
535 |
< |
return; |
534 |
> |
if (badcomponent(m, r)) |
535 |
> |
return(1); |
536 |
> |
if (wrongsource(m, r)) |
537 |
> |
return(1); |
538 |
|
/* check for passed illum */ |
539 |
|
if (passillum(m, r)) { |
540 |
< |
|
541 |
< |
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
542 |
< |
raytrans(r); |
543 |
< |
else |
544 |
< |
rayshade(r, modifier(m->oargs.sarg[0])); |
545 |
< |
|
442 |
< |
/* otherwise treat as source */ |
443 |
< |
} else { |
540 |
> |
if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) |
541 |
> |
return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0]))); |
542 |
> |
raytrans(r); |
543 |
> |
return(1); |
544 |
> |
} |
545 |
> |
/* otherwise treat as source */ |
546 |
|
/* check for behind */ |
547 |
< |
if (r->rod < 0.0) |
548 |
< |
return; |
547 |
> |
if (r->rod < 0.0) |
548 |
> |
return(1); |
549 |
> |
/* check for invisibility */ |
550 |
> |
if (srcignore(m, r)) |
551 |
> |
return(1); |
552 |
> |
/* check for outside spot */ |
553 |
> |
if (m->otype==MAT_SPOT && spotout(r, makespot(m))) |
554 |
> |
return(1); |
555 |
|
/* get distribution pattern */ |
556 |
< |
raytexture(r, m->omod); |
556 |
> |
raytexture(r, m->omod); |
557 |
|
/* get source color */ |
558 |
< |
setcolor(r->rcol, m->oargs.farg[0], |
559 |
< |
m->oargs.farg[1], |
560 |
< |
m->oargs.farg[2]); |
558 |
> |
setcolor(r->rcol, m->oargs.farg[0], |
559 |
> |
m->oargs.farg[1], |
560 |
> |
m->oargs.farg[2]); |
561 |
|
/* modify value */ |
562 |
< |
multcolor(r->rcol, r->pcol); |
563 |
< |
/* assign distance */ |
456 |
< |
r->rt = r->rot; |
457 |
< |
} |
562 |
> |
multcolor(r->rcol, r->pcol); |
563 |
> |
return(1); |
564 |
|
} |