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