286 |
|
|
287 |
|
static int |
288 |
|
cntcmp(sc1, sc2) /* contribution compare (descending) */ |
289 |
< |
register CONTRIB *sc1, *sc2; |
289 |
> |
register CNTPTR *sc1, *sc2; |
290 |
|
{ |
291 |
|
if (sc1->brt > sc2->brt) |
292 |
|
return(-1); |
303 |
|
{ |
304 |
|
register int sn; |
305 |
|
register CONTRIB *srccnt; |
306 |
+ |
register CNTPTR *cntord; |
307 |
|
int ncnts; |
308 |
|
double ourthresh, prob, hwt, test2, hit2; |
309 |
|
RAY sr; |
310 |
|
|
311 |
< |
if ((srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB))) == NULL) |
311 |
> |
srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB)); |
312 |
> |
cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR)); |
313 |
> |
if (srccnt == NULL || cntord == NULL) |
314 |
|
error(SYSTEM, "out of memory in direct"); |
315 |
|
/* modify threshold */ |
316 |
|
ourthresh = shadthresh / r->rweight; |
317 |
|
/* potential contributions */ |
318 |
|
for (sn = 0; sn < nsources; sn++) { |
319 |
< |
srccnt[sn].sno = sn; |
320 |
< |
setcolor(srccnt[sn].val, 0.0, 0.0, 0.0); |
318 |
< |
srccnt[sn].brt = 0.0; |
319 |
> |
cntord[sn].sno = sn; |
320 |
> |
cntord[sn].brt = 0.0; |
321 |
|
/* get source ray */ |
322 |
|
if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0) |
323 |
|
continue; |
324 |
|
VCOPY(srccnt[sn].dir, sr.rdir); |
325 |
|
/* compute coefficient */ |
326 |
|
(*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom); |
327 |
< |
srccnt[sn].brt = bright(srccnt[sn].val); |
328 |
< |
if (srccnt[sn].brt <= FTINY) |
327 |
> |
cntord[sn].brt = bright(srccnt[sn].val); |
328 |
> |
if (cntord[sn].brt <= FTINY) |
329 |
|
continue; |
330 |
|
/* compute intersection */ |
331 |
|
if (!( source[sn].sflags & SDISTANT ? |
336 |
|
/* compute contribution */ |
337 |
|
rayshade(&sr, sr.ro->omod); |
338 |
|
multcolor(srccnt[sn].val, sr.rcol); |
339 |
< |
srccnt[sn].brt = bright(srccnt[sn].val); |
339 |
> |
cntord[sn].brt = bright(srccnt[sn].val); |
340 |
|
} |
341 |
|
/* sort contributions */ |
342 |
< |
qsort(srccnt, nsources, sizeof(CONTRIB), cntcmp); |
342 |
> |
qsort(cntord, nsources, sizeof(CNTPTR), cntcmp); |
343 |
|
hit2 = 0.0; test2 = FTINY; |
344 |
|
/* find last */ |
345 |
|
sn = 0; ncnts = nsources; |
346 |
|
while (sn < ncnts-1) { |
347 |
|
register int m; |
348 |
|
m = (sn + ncnts) >> 1; |
349 |
< |
if (srccnt[m].brt > 0.0) |
349 |
> |
if (cntord[m].brt > 0.0) |
350 |
|
sn = m; |
351 |
|
else |
352 |
|
ncnts = m; |
353 |
|
} |
354 |
|
/* accumulate tail */ |
355 |
|
for (sn = ncnts-1; sn > 0; sn--) |
356 |
< |
srccnt[sn-1].brt += srccnt[sn].brt; |
356 |
> |
cntord[sn-1].brt += cntord[sn].brt; |
357 |
|
/* shadow testing */ |
358 |
|
for (sn = 0; sn < ncnts; sn++) { |
359 |
|
/* tail below threshold? */ |
360 |
< |
if (srccnt[sn].brt < ourthresh*bright(r->rcol)) |
360 |
> |
if (cntord[sn].brt < ourthresh*bright(r->rcol)) |
361 |
|
break; |
362 |
|
/* get statistics */ |
363 |
< |
hwt = (double)source[srccnt[sn].sno].nhits / |
364 |
< |
(double)source[srccnt[sn].sno].ntests; |
363 |
> |
hwt = (double)source[cntord[sn].sno].nhits / |
364 |
> |
(double)source[cntord[sn].sno].ntests; |
365 |
|
test2 += hwt; |
366 |
< |
source[srccnt[sn].sno].ntests++; |
366 |
> |
source[cntord[sn].sno].ntests++; |
367 |
|
/* test for hit */ |
368 |
|
rayorigin(&sr, r, SHADOW, 1.0); |
369 |
< |
VCOPY(sr.rdir, srccnt[sn].dir); |
369 |
> |
VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir); |
370 |
|
if (localhit(&sr, &thescene) && |
371 |
< |
sr.ro != source[srccnt[sn].sno].so) { |
371 |
> |
sr.ro != source[cntord[sn].sno].so) { |
372 |
|
/* check for transmission */ |
373 |
|
if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod)) |
374 |
|
raytrans(&sr); /* object is clipped */ |
376 |
|
rayshade(&sr, sr.ro->omod); |
377 |
|
if (bright(sr.rcol) <= FTINY) |
378 |
|
continue; /* missed! */ |
379 |
< |
(*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom); |
380 |
< |
multcolor(srccnt[sn].val, sr.rcol); |
379 |
> |
(*f)(srccnt[cntord[sn].sno].val, p, |
380 |
> |
srccnt[cntord[sn].sno].dir, |
381 |
> |
srccnt[cntord[sn].sno].dom); |
382 |
> |
multcolor(srccnt[cntord[sn].sno].val, sr.rcol); |
383 |
|
} |
384 |
|
/* add contribution if hit */ |
385 |
< |
addcolor(r->rcol, srccnt[sn].val); |
385 |
> |
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
386 |
|
hit2 += hwt; |
387 |
< |
source[srccnt[sn].sno].nhits++; |
387 |
> |
source[cntord[sn].sno].nhits++; |
388 |
|
} |
389 |
|
/* weighted hit rate */ |
390 |
|
hwt = hit2 / test2; |
394 |
|
#endif |
395 |
|
/* add in untested sources */ |
396 |
|
for ( ; sn < ncnts; sn++) { |
397 |
< |
prob = hwt * (double)source[srccnt[sn].sno].nhits / |
398 |
< |
(double)source[srccnt[sn].sno].ntests; |
399 |
< |
scalecolor(srccnt[sn].val, prob); |
400 |
< |
addcolor(r->rcol, srccnt[sn].val); |
397 |
> |
prob = hwt * (double)source[cntord[sn].sno].nhits / |
398 |
> |
(double)source[cntord[sn].sno].ntests; |
399 |
> |
scalecolor(srccnt[cntord[sn].sno].val, prob); |
400 |
> |
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
401 |
|
} |
402 |
|
free(srccnt); |
403 |
+ |
free(cntord); |
404 |
|
} |
405 |
|
|
406 |
|
|