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 */ |
287 |
|
|
288 |
|
static int |
289 |
|
cntcmp(sc1, sc2) /* contribution compare (descending) */ |
290 |
< |
register CONTRIB *sc1, *sc2; |
290 |
> |
register CNTPTR *sc1, *sc2; |
291 |
|
{ |
292 |
|
if (sc1->brt > sc2->brt) |
293 |
|
return(-1); |
302 |
|
int (*f)(); /* direct component coefficient function */ |
303 |
|
char *p; /* data for f */ |
304 |
|
{ |
305 |
+ |
extern double pow(); |
306 |
|
register int sn; |
307 |
|
register CONTRIB *srccnt; |
308 |
< |
double dtmp, hwt, test2, hit2; |
308 |
> |
register CNTPTR *cntord; |
309 |
> |
int nshadcheck, ncnts; |
310 |
> |
double prob, ourthresh, hwt, test2, hit2; |
311 |
|
RAY sr; |
312 |
|
|
313 |
< |
if ((srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB))) == NULL) |
313 |
> |
srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB)); |
314 |
> |
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; |
321 |
|
/* potential contributions */ |
322 |
|
for (sn = 0; sn < nsources; sn++) { |
323 |
< |
srccnt[sn].sno = sn; |
324 |
< |
setcolor(srccnt[sn].val, 0.0, 0.0, 0.0); |
315 |
< |
srccnt[sn].brt = 0.0; |
323 |
> |
cntord[sn].sno = sn; |
324 |
> |
cntord[sn].brt = 0.0; |
325 |
|
/* get source ray */ |
326 |
|
if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0) |
327 |
|
continue; |
328 |
|
VCOPY(srccnt[sn].dir, sr.rdir); |
329 |
|
/* compute coefficient */ |
330 |
|
(*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom); |
331 |
< |
srccnt[sn].brt = bright(srccnt[sn].val); |
332 |
< |
if (srccnt[sn].brt <= FTINY) |
331 |
> |
cntord[sn].brt = bright(srccnt[sn].val); |
332 |
> |
if (cntord[sn].brt <= 0.0) |
333 |
|
continue; |
334 |
|
/* compute intersection */ |
335 |
|
if (!( source[sn].sflags & SDISTANT ? |
340 |
|
/* compute contribution */ |
341 |
|
rayshade(&sr, sr.ro->omod); |
342 |
|
multcolor(srccnt[sn].val, sr.rcol); |
343 |
< |
srccnt[sn].brt = bright(srccnt[sn].val); |
343 |
> |
cntord[sn].brt = bright(srccnt[sn].val); |
344 |
|
} |
345 |
|
/* sort contributions */ |
346 |
< |
qsort(srccnt, nsources, sizeof(CONTRIB), cntcmp); |
347 |
< |
hit2 = test2 = 0.0; |
346 |
> |
qsort(cntord, nsources, sizeof(CNTPTR), cntcmp); |
347 |
> |
{ /* find last */ |
348 |
> |
register int l, m; |
349 |
> |
|
350 |
> |
sn = 0; ncnts = l = nsources; |
351 |
> |
while ((m = (sn + ncnts) >> 1) != l) { |
352 |
> |
if (cntord[m].brt > 0.0) |
353 |
> |
sn = m; |
354 |
> |
else |
355 |
> |
ncnts = m; |
356 |
> |
l = m; |
357 |
> |
} |
358 |
> |
} |
359 |
> |
/* accumulate tail */ |
360 |
> |
for (sn = ncnts-1; sn > 0; sn--) |
361 |
> |
cntord[sn-1].brt += cntord[sn].brt; |
362 |
> |
/* start with prob=.5 */ |
363 |
> |
hit2 = 0.5; test2 = 1.0; |
364 |
|
/* test for shadows */ |
365 |
< |
for (sn = 0; sn < nsources; sn++) { |
365 |
> |
for (sn = 0; sn < ncnts; sn++) { |
366 |
|
/* check threshold */ |
367 |
< |
if (srccnt[sn].brt <= shadthresh*bright(r->rcol)/r->rweight) |
367 |
> |
if ((sn+nshadcheck>=ncnts ? cntord[sn].brt : |
368 |
> |
cntord[sn].brt-cntord[sn+nshadcheck].brt) < |
369 |
> |
ourthresh*bright(r->rcol)) |
370 |
|
break; |
371 |
|
/* get statistics */ |
372 |
< |
hwt = (double)source[srccnt[sn].sno].nhits / |
373 |
< |
(double)source[srccnt[sn].sno].ntests; |
372 |
> |
hwt = (double)source[cntord[sn].sno].nhits / |
373 |
> |
(double)source[cntord[sn].sno].ntests; |
374 |
|
test2 += hwt; |
375 |
< |
source[srccnt[sn].sno].ntests++; |
375 |
> |
source[cntord[sn].sno].ntests++; |
376 |
|
/* test for hit */ |
377 |
|
rayorigin(&sr, r, SHADOW, 1.0); |
378 |
< |
VCOPY(sr.rdir, srccnt[sn].dir); |
378 |
> |
VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir); |
379 |
|
if (localhit(&sr, &thescene) && |
380 |
< |
sr.ro != source[srccnt[sn].sno].so) { |
380 |
> |
sr.ro != source[cntord[sn].sno].so) { |
381 |
|
/* check for transmission */ |
382 |
< |
if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod)) |
356 |
< |
raytrans(&sr); /* object is clipped */ |
357 |
< |
else |
358 |
< |
rayshade(&sr, sr.ro->omod); |
382 |
> |
rayshade(&sr, sr.ro->omod); |
383 |
|
if (bright(sr.rcol) <= FTINY) |
384 |
|
continue; /* missed! */ |
385 |
< |
(*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom); |
386 |
< |
multcolor(srccnt[sn].val, sr.rcol); |
385 |
> |
(*f)(srccnt[cntord[sn].sno].val, p, |
386 |
> |
srccnt[cntord[sn].sno].dir, |
387 |
> |
srccnt[cntord[sn].sno].dom); |
388 |
> |
multcolor(srccnt[cntord[sn].sno].val, sr.rcol); |
389 |
|
} |
390 |
|
/* add contribution if hit */ |
391 |
< |
addcolor(r->rcol, srccnt[sn].val); |
391 |
> |
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
392 |
|
hit2 += hwt; |
393 |
< |
source[srccnt[sn].sno].nhits++; |
393 |
> |
source[cntord[sn].sno].nhits++; |
394 |
|
} |
395 |
< |
if (test2 > FTINY) /* weighted hit rate */ |
396 |
< |
hwt = hit2 / test2; |
371 |
< |
else |
372 |
< |
hwt = 0.0; |
395 |
> |
/* weighted hit rate */ |
396 |
> |
hwt = hit2 / test2; |
397 |
|
#ifdef DEBUG |
398 |
< |
fprintf(stderr, "%d tested, %f hit rate\n", sn, hwt); |
398 |
> |
sprintf(errmsg, "%d tested, %d untested, %f hit rate\n", |
399 |
> |
sn, ncnts-sn, hwt); |
400 |
> |
eputs(errmsg); |
401 |
|
#endif |
402 |
|
/* add in untested sources */ |
403 |
< |
for ( ; sn < nsources; sn++) { |
404 |
< |
if (srccnt[sn].brt <= 0.0) |
405 |
< |
break; |
406 |
< |
dtmp = hwt * (double)source[srccnt[sn].sno].nhits / |
407 |
< |
(double)source[srccnt[sn].sno].ntests; |
382 |
< |
scalecolor(srccnt[sn].val, dtmp); |
383 |
< |
addcolor(r->rcol, srccnt[sn].val); |
403 |
> |
for ( ; sn < ncnts; sn++) { |
404 |
> |
prob = hwt * (double)source[cntord[sn].sno].nhits / |
405 |
> |
(double)source[cntord[sn].sno].ntests; |
406 |
> |
scalecolor(srccnt[cntord[sn].sno].val, prob); |
407 |
> |
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
408 |
|
} |
409 |
|
|
410 |
|
free(srccnt); |
411 |
+ |
free(cntord); |
412 |
|
} |
413 |
|
|
414 |
|
|
428 |
|
register OBJREC *m; |
429 |
|
register RAY *r; |
430 |
|
{ |
406 |
– |
/* check for behind */ |
407 |
– |
if (r->rod < 0.0) |
408 |
– |
return; |
431 |
|
/* check for over-counting */ |
432 |
|
if (wrongsource(m, r) || badambient(m, r)) |
433 |
|
return; |
441 |
|
|
442 |
|
/* otherwise treat as source */ |
443 |
|
} else { |
444 |
+ |
/* check for behind */ |
445 |
+ |
if (r->rod < 0.0) |
446 |
+ |
return; |
447 |
|
/* get distribution pattern */ |
448 |
|
raytexture(r, m->omod); |
449 |
|
/* get source color */ |