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