ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/source.c
Revision: 1.27
Committed: Mon Dec 17 14:38:12 1990 UTC (33 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.26: +16 -18 lines
Log Message:
minor cleanup in srcray()

File Contents

# Content
1 /* Copyright (c) 1990 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 #include "octree.h"
16
17 #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 extern double shadthresh; /* relative shadow threshold */
30 extern double shadcert; /* shadow testing certainty */
31
32 SRCREC *source = NULL; /* our list of sources */
33 int nsources = 0; /* the number of sources */
34
35 static CONTRIB *srccnt; /* source contributions in direct() */
36 static CNTPTR *cntord; /* source ordering in direct() */
37
38
39 marksources() /* find and mark source objects */
40 {
41 register OBJREC *o, *m;
42 register int i;
43
44 for (i = 0; i < nobjects; i++) {
45
46 o = objptr(i);
47
48 if (o->omod == OVOID)
49 continue;
50
51 m = objptr(o->omod);
52
53 if (!islight(m->otype))
54 continue;
55
56 if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 :
57 m->otype == MAT_SPOT ? 7 : 3))
58 objerror(m, USER, "bad # arguments");
59
60 if (m->otype == MAT_GLOW &&
61 o->otype != OBJ_SOURCE &&
62 m->oargs.farg[3] <= FTINY)
63 continue; /* don't bother */
64
65 if (source == NULL)
66 source = (SRCREC *)malloc(sizeof(SRCREC));
67 else
68 source = (SRCREC *)realloc((char *)source,
69 (unsigned)(nsources+1)*sizeof(SRCREC));
70 if (source == NULL)
71 goto memerr;
72
73 newsource(&source[nsources], o);
74
75 if (m->otype == MAT_GLOW) {
76 source[nsources].sflags |= SPROX;
77 source[nsources].sl.prox = m->oargs.farg[3];
78 if (o->otype == OBJ_SOURCE)
79 source[nsources].sflags |= SSKIP;
80 } else if (m->otype == MAT_SPOT) {
81 source[nsources].sflags |= SSPOT;
82 source[nsources].sl.s = makespot(m);
83 }
84 nsources++;
85 }
86 if (nsources <= 0) {
87 error(WARNING, "no light sources found");
88 return;
89 }
90 srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
91 cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
92 if (srccnt != NULL && cntord != NULL)
93 return;
94 /* fall through */
95 memerr:
96 error(SYSTEM, "out of memory in marksources");
97 }
98
99
100 newsource(src, so) /* add a source to the array */
101 register SRCREC *src;
102 register OBJREC *so;
103 {
104 double cos(), tan(), sqrt();
105 double theta;
106 FACE *f;
107 CONE *co;
108 int j;
109 register int i;
110
111 src->sflags = 0;
112 src->nhits = 1; src->ntests = 2; /* start probability = 1/2 */
113 src->so = so;
114
115 switch (so->otype) {
116 case OBJ_SOURCE:
117 if (so->oargs.nfargs != 4)
118 objerror(so, USER, "bad arguments");
119 src->sflags |= SDISTANT;
120 VCOPY(src->sloc, so->oargs.farg);
121 if (normalize(src->sloc) == 0.0)
122 objerror(so, USER, "zero direction");
123 theta = PI/180.0/2.0 * so->oargs.farg[3];
124 if (theta <= FTINY)
125 objerror(so, USER, "zero size");
126 src->ss = theta >= PI/4 ? 1.0 : tan(theta);
127 src->ss2 = 2.0*PI * (1.0 - cos(theta));
128 break;
129 case OBJ_SPHERE:
130 VCOPY(src->sloc, so->oargs.farg);
131 src->ss = so->oargs.farg[3];
132 src->ss2 = PI * src->ss * src->ss;
133 break;
134 case OBJ_FACE:
135 /* get the face */
136 f = getface(so);
137 /* find the center */
138 for (j = 0; j < 3; j++) {
139 src->sloc[j] = 0.0;
140 for (i = 0; i < f->nv; i++)
141 src->sloc[j] += VERTEX(f,i)[j];
142 src->sloc[j] /= f->nv;
143 }
144 if (!inface(src->sloc, f))
145 objerror(so, USER, "cannot hit center");
146 src->ss = sqrt(f->area / PI);
147 src->ss2 = f->area;
148 break;
149 case OBJ_RING:
150 /* get the ring */
151 co = getcone(so, 0);
152 VCOPY(src->sloc, CO_P0(co));
153 if (CO_R0(co) > 0.0)
154 objerror(so, USER, "cannot hit center");
155 src->ss = CO_R1(co);
156 src->ss2 = PI * src->ss * src->ss;
157 break;
158 default:
159 objerror(so, USER, "illegal material");
160 }
161 }
162
163
164 SPOT *
165 makespot(m) /* make a spotlight */
166 register OBJREC *m;
167 {
168 extern double cos();
169 register SPOT *ns;
170
171 if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
172 error(SYSTEM, "out of memory in makespot");
173 ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
174 VCOPY(ns->aim, m->oargs.farg+4);
175 if ((ns->flen = normalize(ns->aim)) == 0.0)
176 objerror(m, USER, "zero focus vector");
177 return(ns);
178 }
179
180
181 double
182 srcray(sr, r, sn) /* send a ray to a source, return domega */
183 register RAY *sr; /* returned source ray */
184 RAY *r; /* ray which hit object */
185 register int sn; /* source number */
186 {
187 register double *norm = NULL; /* plane normal */
188 double ddot; /* (distance times) cosine */
189 FVECT vd;
190 double d;
191 register int i;
192
193 if (source[sn].sflags & SSKIP)
194 return(0.0); /* skip this source */
195
196 rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */
197
198 sr->rsrc = sn; /* remember source */
199 /* get source direction */
200 if (source[sn].sflags & SDISTANT)
201 /* constant direction */
202 VCOPY(sr->rdir, source[sn].sloc);
203 else { /* compute direction */
204 for (i = 0; i < 3; i++)
205 sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i];
206
207 if (source[sn].so->otype == OBJ_FACE)
208 norm = getface(source[sn].so)->norm;
209 else if (source[sn].so->otype == OBJ_RING)
210 norm = getcone(source[sn].so,0)->ad;
211
212 if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY)
213 return(0.0); /* behind surface! */
214 }
215 if (dstrsrc > FTINY) {
216 /* distribute source direction */
217 for (i = 0; i < 3; i++)
218 vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom());
219
220 if (norm != NULL) { /* project offset */
221 d = DOT(vd, norm);
222 for (i = 0; i < 3; i++)
223 vd[i] -= d * norm[i];
224 }
225 for (i = 0; i < 3; i++) /* offset source direction */
226 sr->rdir[i] += vd[i];
227
228 } else if (source[sn].sflags & SDISTANT)
229 /* already normalized */
230 return(source[sn].ss2);
231
232 if ((d = normalize(sr->rdir)) == 0.0)
233 /* at source! */
234 return(0.0);
235
236 if (source[sn].sflags & SDISTANT)
237 /* domega constant */
238 return(source[sn].ss2);
239
240 /* check proximity */
241 if (source[sn].sflags & SPROX &&
242 d > source[sn].sl.prox)
243 return(0.0);
244 /* compute dot product */
245 if (norm != NULL)
246 ddot /= d;
247 else
248 ddot = 1.0;
249 /* check angle */
250 if (source[sn].sflags & SSPOT) {
251 if (source[sn].sl.s->siz < 2.0*PI *
252 (1.0 + DOT(source[sn].sl.s->aim,sr->rdir)))
253 return(0.0);
254 d += source[sn].sl.s->flen; /* adjust length */
255 }
256 /* compute domega */
257 return(ddot*source[sn].ss2/(d*d));
258 }
259
260
261 sourcehit(r) /* check to see if ray hit distant source */
262 register RAY *r;
263 {
264 int first, last;
265 register int i;
266
267 if (r->rsrc >= 0) { /* check only one if aimed */
268 first = last = r->rsrc;
269 } else { /* otherwise check all */
270 first = 0; last = nsources-1;
271 }
272 for (i = first; i <= last; i++)
273 if (source[i].sflags & SDISTANT)
274 /*
275 * Check to see if ray is within
276 * solid angle of source.
277 */
278 if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
279 <= source[i].ss2) {
280 r->ro = source[i].so;
281 if (!(source[i].sflags & SSKIP))
282 break;
283 }
284
285 if (r->ro != NULL) {
286 for (i = 0; i < 3; i++)
287 r->ron[i] = -r->rdir[i];
288 r->rod = 1.0;
289 r->rox = NULL;
290 return(1);
291 }
292 return(0);
293 }
294
295
296 static int
297 cntcmp(sc1, sc2) /* contribution compare (descending) */
298 register CNTPTR *sc1, *sc2;
299 {
300 if (sc1->brt > sc2->brt)
301 return(-1);
302 if (sc1->brt < sc2->brt)
303 return(1);
304 return(0);
305 }
306
307
308 direct(r, f, p) /* add direct component */
309 RAY *r; /* ray that hit surface */
310 int (*f)(); /* direct component coefficient function */
311 char *p; /* data for f */
312 {
313 extern double pow();
314 register int sn;
315 int nshadcheck, ncnts;
316 double prob, ourthresh, hwt, test2, hit2;
317 RAY sr;
318 /* NOTE: srccnt and cntord global so no recursion */
319 if (nsources <= 0)
320 return; /* no sources?! */
321 /* compute number to check */
322 nshadcheck = pow((double)nsources, shadcert) + .5;
323 /* modify threshold */
324 ourthresh = shadthresh / r->rweight;
325 /* potential contributions */
326 for (sn = 0; sn < nsources; sn++) {
327 cntord[sn].sno = sn;
328 cntord[sn].brt = 0.0;
329 /* get source ray */
330 if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
331 continue;
332 VCOPY(srccnt[sn].dir, sr.rdir);
333 /* compute coefficient */
334 (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
335 cntord[sn].brt = bright(srccnt[sn].val);
336 if (cntord[sn].brt <= 0.0)
337 continue;
338 /* compute intersection */
339 if (!( source[sn].sflags & SDISTANT ?
340 sourcehit(&sr) :
341 (*ofun[source[sn].so->otype].funp)
342 (source[sn].so, &sr) )) {
343 sprintf(errmsg,
344 "aiming failure for light source \"%s\"",
345 source[sn].so->oname);
346 error(WARNING, errmsg);
347 continue;
348 }
349 /* compute contribution */
350 raycont(&sr);
351 multcolor(srccnt[sn].val, sr.rcol);
352 cntord[sn].brt = bright(srccnt[sn].val);
353 }
354 /* sort contributions */
355 qsort(cntord, nsources, sizeof(CNTPTR), cntcmp);
356 { /* find last */
357 register int l, m;
358
359 sn = 0; ncnts = l = nsources;
360 while ((m = (sn + ncnts) >> 1) != l) {
361 if (cntord[m].brt > 0.0)
362 sn = m;
363 else
364 ncnts = m;
365 l = m;
366 }
367 }
368 /* accumulate tail */
369 for (sn = ncnts-1; sn > 0; sn--)
370 cntord[sn-1].brt += cntord[sn].brt;
371 /* start with prob=.5 */
372 hit2 = 0.5; test2 = 1.0;
373 /* test for shadows */
374 for (sn = 0; sn < ncnts; sn++) {
375 /* check threshold */
376 if ((sn+nshadcheck>=ncnts ? cntord[sn].brt :
377 cntord[sn].brt-cntord[sn+nshadcheck].brt)
378 < ourthresh*bright(r->rcol))
379 break;
380 /* get statistics */
381 hwt = (double)source[cntord[sn].sno].nhits /
382 (double)source[cntord[sn].sno].ntests;
383 test2 += hwt;
384 source[cntord[sn].sno].ntests++;
385 /* test for hit */
386 rayorigin(&sr, r, SHADOW, 1.0);
387 VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
388 sr.rsrc = cntord[sn].sno;
389 if (localhit(&sr, &thescene) &&
390 sr.ro != source[cntord[sn].sno].so) {
391 /* check for transmission */
392 raycont(&sr);
393 if (bright(sr.rcol) <= FTINY)
394 continue; /* missed! */
395 (*f)(srccnt[cntord[sn].sno].val, p,
396 srccnt[cntord[sn].sno].dir,
397 srccnt[cntord[sn].sno].dom);
398 multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
399 }
400 /* add contribution if hit */
401 addcolor(r->rcol, srccnt[cntord[sn].sno].val);
402 hit2 += hwt;
403 source[cntord[sn].sno].nhits++;
404 }
405 /* weighted hit rate */
406 hwt = hit2 / test2;
407 #ifdef DEBUG
408 sprintf(errmsg, "%d tested, %d untested, %f hit rate\n",
409 sn, ncnts-sn, hwt);
410 eputs(errmsg);
411 #endif
412 /* add in untested sources */
413 for ( ; sn < ncnts; sn++) {
414 prob = hwt * (double)source[cntord[sn].sno].nhits /
415 (double)source[cntord[sn].sno].ntests;
416 scalecolor(srccnt[cntord[sn].sno].val, prob);
417 addcolor(r->rcol, srccnt[cntord[sn].sno].val);
418 }
419 }
420
421
422 #define wrongsource(m, r) (m->otype!=MAT_ILLUM && \
423 r->rsrc>=0 && \
424 source[r->rsrc].so!=r->ro)
425
426 #define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
427 !(r->rtype&REFLECTED) && /* hack! */\
428 !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
429
430 #define passillum(m, r) (m->otype==MAT_ILLUM && \
431 !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
432
433
434 m_light(m, r) /* ray hit a light source */
435 register OBJREC *m;
436 register RAY *r;
437 {
438 /* check for over-counting */
439 if (wrongsource(m, r) || badambient(m, r))
440 return;
441 /* check for passed illum */
442 if (passillum(m, r)) {
443
444 if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
445 raytrans(r);
446 else
447 rayshade(r, modifier(m->oargs.sarg[0]));
448
449 /* otherwise treat as source */
450 } else {
451 /* check for behind */
452 if (r->rod < 0.0)
453 return;
454 /* get distribution pattern */
455 raytexture(r, m->omod);
456 /* get source color */
457 setcolor(r->rcol, m->oargs.farg[0],
458 m->oargs.farg[1],
459 m->oargs.farg[2]);
460 /* modify value */
461 multcolor(r->rcol, r->pcol);
462 /* assign distance */
463 r->rt = r->rot;
464 }
465 }