1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1990 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
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; |
50 |
|
|
51 |
|
m = objptr(o->omod); |
52 |
|
|
53 |
< |
if (m->otype != MAT_LIGHT && |
50 |
< |
m->otype != MAT_ILLUM && |
51 |
< |
m->otype != MAT_GLOW && |
52 |
< |
m->otype != MAT_SPOT) |
53 |
> |
if (!islight(m->otype)) |
54 |
|
continue; |
55 |
|
|
56 |
|
if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 : |
68 |
|
source = (SRCREC *)realloc((char *)source, |
69 |
|
(unsigned)(nsources+1)*sizeof(SRCREC)); |
70 |
|
if (source == NULL) |
71 |
< |
error(SYSTEM, "out of memory in marksources"); |
71 |
> |
goto memerr; |
72 |
|
|
73 |
|
newsource(&source[nsources], o); |
74 |
|
|
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 |
|
|
237 |
|
/* domega constant */ |
238 |
|
return(source[sn].ss2); |
239 |
|
|
228 |
– |
else { |
240 |
|
/* check proximity */ |
241 |
< |
if (source[sn].sflags & SPROX && |
242 |
< |
d > source[sn].sl.prox) |
243 |
< |
return(0.0); |
244 |
< |
|
245 |
< |
if (norm != NULL) |
246 |
< |
ddot /= d; |
247 |
< |
else |
248 |
< |
ddot = 1.0; |
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 * |
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; |
244 |
< |
} |
245 |
< |
/* return domega */ |
246 |
< |
return(ddot*source[sn].ss2/(d*d)); |
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 |
|
|
286 |
|
for (i = 0; i < 3; i++) |
287 |
|
r->ron[i] = -r->rdir[i]; |
288 |
|
r->rod = 1.0; |
289 |
< |
r->rofs = 1.0; setident4(r->rofx); |
280 |
< |
r->robs = 1.0; setident4(r->robx); |
289 |
> |
r->rox = NULL; |
290 |
|
return(1); |
291 |
|
} |
292 |
|
return(0); |
295 |
|
|
296 |
|
static int |
297 |
|
cntcmp(sc1, sc2) /* contribution compare (descending) */ |
298 |
< |
register CONTRIB *sc1, *sc2; |
298 |
> |
register CNTPTR *sc1, *sc2; |
299 |
|
{ |
300 |
|
if (sc1->brt > sc2->brt) |
301 |
|
return(-1); |
310 |
|
int (*f)(); /* direct component coefficient function */ |
311 |
|
char *p; /* data for f */ |
312 |
|
{ |
313 |
+ |
extern double pow(); |
314 |
|
register int sn; |
315 |
< |
register CONTRIB *srccnt; |
316 |
< |
double dtmp, hwt, test2, hit2; |
315 |
> |
int nshadcheck, ncnts; |
316 |
> |
double prob, ourthresh, hwt, test2, hit2; |
317 |
|
RAY sr; |
318 |
< |
|
319 |
< |
if ((srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB))) == NULL) |
320 |
< |
error(SYSTEM, "out of memory in direct"); |
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 |
< |
srccnt[sn].sno = sn; |
328 |
< |
setcolor(srccnt[sn].val, 0.0, 0.0, 0.0); |
315 |
< |
srccnt[sn].brt = 0.0; |
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 |
< |
srccnt[sn].brt = bright(srccnt[sn].val); |
336 |
< |
if (srccnt[sn].brt <= FTINY) |
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) )) |
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 |
< |
rayshade(&sr, sr.ro->omod); |
350 |
> |
raycont(&sr); |
351 |
|
multcolor(srccnt[sn].val, sr.rcol); |
352 |
< |
srccnt[sn].brt = bright(srccnt[sn].val); |
352 |
> |
cntord[sn].brt = bright(srccnt[sn].val); |
353 |
|
} |
354 |
|
/* sort contributions */ |
355 |
< |
qsort(srccnt, nsources, sizeof(CONTRIB), cntcmp); |
356 |
< |
hit2 = 0.0; test2 = FTINY; |
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 < nsources; sn++) { |
374 |
> |
for (sn = 0; sn < ncnts; sn++) { |
375 |
|
/* check threshold */ |
376 |
< |
if (srccnt[sn].brt <= shadthresh*bright(r->rcol)/r->rweight) |
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[srccnt[sn].sno].nhits / |
382 |
< |
(double)source[srccnt[sn].sno].ntests; |
381 |
> |
hwt = (double)source[cntord[sn].sno].nhits / |
382 |
> |
(double)source[cntord[sn].sno].ntests; |
383 |
|
test2 += hwt; |
384 |
< |
source[srccnt[sn].sno].ntests++; |
384 |
> |
source[cntord[sn].sno].ntests++; |
385 |
|
/* test for hit */ |
386 |
|
rayorigin(&sr, r, SHADOW, 1.0); |
387 |
< |
VCOPY(sr.rdir, srccnt[sn].dir); |
387 |
> |
VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir); |
388 |
> |
sr.rsrc = cntord[sn].sno; |
389 |
|
if (localhit(&sr, &thescene) && |
390 |
< |
sr.ro != source[srccnt[sn].sno].so) { |
390 |
> |
sr.ro != source[cntord[sn].sno].so) { |
391 |
|
/* check for transmission */ |
392 |
< |
if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod)) |
356 |
< |
raytrans(&sr); /* object is clipped */ |
357 |
< |
else |
358 |
< |
rayshade(&sr, sr.ro->omod); |
392 |
> |
raycont(&sr); |
393 |
|
if (bright(sr.rcol) <= FTINY) |
394 |
|
continue; /* missed! */ |
395 |
< |
(*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom); |
396 |
< |
multcolor(srccnt[sn].val, sr.rcol); |
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[sn].val); |
401 |
> |
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
402 |
|
hit2 += hwt; |
403 |
< |
source[srccnt[sn].sno].nhits++; |
403 |
> |
source[cntord[sn].sno].nhits++; |
404 |
|
} |
405 |
|
/* weighted hit rate */ |
406 |
|
hwt = hit2 / test2; |
407 |
|
#ifdef DEBUG |
408 |
< |
{ |
409 |
< |
int ntested = sn; |
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 < nsources; sn++) { |
414 |
< |
if (srccnt[sn].brt <= 0.0) |
415 |
< |
break; |
416 |
< |
dtmp = hwt * (double)source[srccnt[sn].sno].nhits / |
417 |
< |
(double)source[srccnt[sn].sno].ntests; |
381 |
< |
scalecolor(srccnt[sn].val, dtmp); |
382 |
< |
addcolor(r->rcol, srccnt[sn].val); |
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 |
|
} |
384 |
– |
#ifdef DEBUG |
385 |
– |
fprintf(stderr, "%d tested, %d untested, %f hit rate\n", |
386 |
– |
ntested, sn-ntested, hwt); |
387 |
– |
} |
388 |
– |
#endif |
389 |
– |
|
390 |
– |
free(srccnt); |
419 |
|
} |
420 |
|
|
421 |
|
|
435 |
|
register OBJREC *m; |
436 |
|
register RAY *r; |
437 |
|
{ |
410 |
– |
/* check for behind */ |
411 |
– |
if (r->rod < 0.0) |
412 |
– |
return; |
438 |
|
/* check for over-counting */ |
439 |
|
if (wrongsource(m, r) || badambient(m, r)) |
440 |
|
return; |
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 */ |
459 |
|
m->oargs.farg[2]); |
460 |
|
/* modify value */ |
461 |
|
multcolor(r->rcol, r->pcol); |
462 |
+ |
/* assign distance */ |
463 |
+ |
r->rt = r->rot; |
464 |
|
} |
465 |
|
} |
436 |
– |
|
437 |
– |
|
438 |
– |
o_source() {} /* intersection with a source is done elsewhere */ |