1 |
– |
#ifndef lint |
1 |
|
static const char RCSid[] = "$Id$"; |
3 |
– |
#endif |
2 |
|
/* |
3 |
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
4 |
|
* |
15 |
|
#include "resolu.h" |
16 |
|
#include "ambient.h" |
17 |
|
#include "random.h" |
18 |
+ |
#include "pmapamb.h" |
19 |
|
|
20 |
|
#ifndef OCTSCALE |
21 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
51 |
|
#endif |
52 |
|
|
53 |
|
|
55 |
– |
static double qambacc = 0.; /* ambient accuracy to the 1/4 power */ |
54 |
|
static double avsum = 0.; /* computed ambient value sum (log) */ |
55 |
|
static unsigned int navsum = 0; /* number of values in avsum */ |
56 |
|
static unsigned int nambvals = 0; /* total number of indirect values */ |
76 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
77 |
|
|
78 |
|
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
81 |
– |
#define freeav(av) free((void *)av); |
79 |
|
|
80 |
|
static void initambfile(int creat); |
81 |
|
static void avsave(AMBVAL *av); |
107 |
|
/* set min & max radii */ |
108 |
|
if (ar <= 0) { |
109 |
|
minarad = 0; |
110 |
< |
maxarad = thescene.cusize / 2.0; |
110 |
> |
maxarad = thescene.cusize*0.2; |
111 |
|
} else { |
112 |
|
minarad = thescene.cusize / ar; |
113 |
< |
maxarad = 64 * minarad; /* heuristic */ |
114 |
< |
if (maxarad > thescene.cusize / 2.0) |
115 |
< |
maxarad = thescene.cusize / 2.0; |
113 |
> |
maxarad = 64.0 * minarad; /* heuristic */ |
114 |
> |
if (maxarad > thescene.cusize*0.2) |
115 |
> |
maxarad = thescene.cusize*0.2; |
116 |
|
} |
117 |
|
if (minarad <= FTINY) |
118 |
< |
minarad = 10*FTINY; |
118 |
> |
minarad = 10.0*FTINY; |
119 |
|
if (maxarad <= minarad) |
120 |
< |
maxarad = 64 * minarad; |
120 |
> |
maxarad = 64.0 * minarad; |
121 |
|
} |
122 |
|
|
123 |
|
|
126 |
|
double newa |
127 |
|
) |
128 |
|
{ |
129 |
< |
double olda = qambacc*qambacc*qambacc*qambacc; |
129 |
> |
static double olda; /* remember previous setting here */ |
130 |
|
|
131 |
|
newa *= (newa > 0); |
132 |
|
if (fabs(newa - olda) >= .05*(newa + olda)) { |
133 |
< |
qambacc = sqrt(sqrt(ambacc = newa)); |
133 |
> |
ambacc = newa; |
134 |
|
if (nambvals > 0) |
135 |
|
sortambvals(1); /* rebuild tree */ |
136 |
|
} |
163 |
|
initambfile(0); /* file exists */ |
164 |
|
lastpos = ftell(ambfp); |
165 |
|
while (readambval(&amb, ambfp)) |
166 |
< |
avinsert(avstore(&amb)); |
166 |
> |
avstore(&amb); |
167 |
|
nambshare = nambvals; /* share loaded values */ |
168 |
|
if (readonly) { |
169 |
|
sprintf(errmsg, |
183 |
|
(flen - lastpos)/AMBVALSIZ); |
184 |
|
error(WARNING, errmsg); |
185 |
|
fseek(ambfp, lastpos, SEEK_SET); |
189 |
– |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
186 |
|
ftruncate(fileno(ambfp), (off_t)lastpos); |
191 |
– |
#endif |
187 |
|
} |
188 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
189 |
|
initambfile(1); /* else create new file */ |
193 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
194 |
|
error(SYSTEM, errmsg); |
195 |
|
} |
201 |
– |
#ifdef getc_unlocked |
202 |
– |
flockfile(ambfp); /* application-level lock */ |
203 |
– |
#endif |
196 |
|
#ifdef F_SETLKW |
197 |
|
aflock(F_UNLCK); /* release file */ |
198 |
|
#endif |
213 |
|
lastpos = -1; |
214 |
|
} |
215 |
|
/* free ambient tree */ |
216 |
< |
unloadatree(&atrunk, &avfree); |
216 |
> |
unloadatree(&atrunk, avfree); |
217 |
|
/* reset state variables */ |
218 |
|
avsum = 0.; |
219 |
|
navsum = 0; |
256 |
|
|
257 |
|
/************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/ |
258 |
|
|
259 |
< |
#ifdef NEWAMB |
259 |
> |
#ifndef OLDAMB |
260 |
|
|
261 |
|
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
262 |
|
|
263 |
+ |
static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang); |
264 |
|
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
265 |
|
AMBTREE *at, FVECT c0, double s); |
266 |
|
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
267 |
< |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
267 |
> |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
268 |
|
FVECT uvw[3]); |
269 |
|
|
270 |
|
void |
275 |
|
) |
276 |
|
{ |
277 |
|
static int rdepth = 0; /* ambient recursion */ |
278 |
< |
COLOR acol; |
279 |
< |
int ok; |
278 |
> |
COLOR acol, caustic; |
279 |
> |
int i, ok; |
280 |
|
double d, l; |
281 |
|
|
282 |
+ |
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
283 |
+ |
* ambient. Return as all ambient components accounted for, else |
284 |
+ |
* continue. */ |
285 |
+ |
if (ambPmap(aval, r, rdepth)) |
286 |
+ |
return; |
287 |
+ |
|
288 |
+ |
/* PMAP: Factor in specular-diffuse ambient (caustics) from photon |
289 |
+ |
* map, if enabled and ray is primary, else caustic is zero. Continue |
290 |
+ |
* with RADIANCE ambient calculation */ |
291 |
+ |
copycolor(caustic, aval); |
292 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
293 |
+ |
|
294 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
295 |
|
goto dumbamb; |
296 |
|
/* check number of bounces */ |
302 |
|
goto dumbamb; |
303 |
|
|
304 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
305 |
+ |
FVECT uvd[2]; |
306 |
+ |
float dgrad[2], *dgp = NULL; |
307 |
+ |
|
308 |
+ |
if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999) |
309 |
+ |
dgp = dgrad; /* compute rotational grad. */ |
310 |
|
copycolor(acol, aval); |
311 |
|
rdepth++; |
312 |
< |
ok = doambient(acol, r, r->rweight, NULL, NULL, NULL, NULL); |
312 |
> |
ok = doambient(acol, r, r->rweight, |
313 |
> |
uvd, NULL, NULL, dgp, NULL); |
314 |
|
rdepth--; |
315 |
|
if (!ok) |
316 |
|
goto dumbamb; |
317 |
+ |
if ((ok > 0) & (dgp != NULL)) { /* apply texture */ |
318 |
+ |
FVECT v1; |
319 |
+ |
VCROSS(v1, r->ron, nrm); |
320 |
+ |
d = 1.0; |
321 |
+ |
for (i = 3; i--; ) |
322 |
+ |
d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]); |
323 |
+ |
if (d >= 0.05) |
324 |
+ |
scalecolor(acol, d); |
325 |
+ |
} |
326 |
|
copycolor(aval, acol); |
327 |
+ |
|
328 |
+ |
/* PMAP: add in caustic */ |
329 |
+ |
addcolor(aval, caustic); |
330 |
|
return; |
331 |
|
} |
332 |
|
|
336 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
337 |
|
d = sumambient(acol, r, nrm, rdepth, |
338 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
339 |
+ |
|
340 |
|
if (d > FTINY) { |
341 |
|
d = 1.0/d; |
342 |
|
scalecolor(acol, d); |
343 |
|
multcolor(aval, acol); |
344 |
+ |
|
345 |
+ |
/* PMAP: add in caustic */ |
346 |
+ |
addcolor(aval, caustic); |
347 |
|
return; |
348 |
|
} |
349 |
+ |
|
350 |
|
rdepth++; /* need to cache new value */ |
351 |
|
ok = makeambient(acol, r, nrm, rdepth-1); |
352 |
|
rdepth--; |
353 |
+ |
|
354 |
|
if (ok) { |
355 |
|
multcolor(aval, acol); /* computed new value */ |
356 |
+ |
|
357 |
+ |
/* PMAP: add in caustic */ |
358 |
+ |
addcolor(aval, caustic); |
359 |
|
return; |
360 |
|
} |
361 |
+ |
|
362 |
|
dumbamb: /* return global value */ |
363 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
364 |
|
multcolor(aval, ambval); |
365 |
+ |
|
366 |
+ |
/* PMAP: add in caustic */ |
367 |
+ |
addcolor(aval, caustic); |
368 |
|
return; |
369 |
|
} |
370 |
< |
l = bright(ambval); /* average in computations */ |
370 |
> |
|
371 |
> |
l = bright(ambval); /* average in computations */ |
372 |
|
if (l > FTINY) { |
373 |
|
d = (log(l)*(double)ambvwt + avsum) / |
374 |
|
(double)(ambvwt + navsum); |
382 |
|
} |
383 |
|
|
384 |
|
|
385 |
< |
double |
385 |
> |
/* Plug a potential leak where ambient cache value is occluded */ |
386 |
> |
static int |
387 |
> |
plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang) |
388 |
> |
{ |
389 |
> |
const double cost70sq = 0.1169778; /* cos(70deg)^2 */ |
390 |
> |
RAY rtst; |
391 |
> |
FVECT vdif; |
392 |
> |
double normdot, ndotd, nadotd; |
393 |
> |
double a, b, c, t[2]; |
394 |
> |
|
395 |
> |
ang += 2.*PI*(ang < 0); /* check direction flags */ |
396 |
> |
if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) ) |
397 |
> |
return(0); |
398 |
> |
/* |
399 |
> |
* Generate test ray, targeting 20 degrees above sample point plane |
400 |
> |
* along surface normal from cache position. This should be high |
401 |
> |
* enough to miss local geometry we don't really care about. |
402 |
> |
*/ |
403 |
> |
VSUB(vdif, ap->pos, r->rop); |
404 |
> |
normdot = DOT(anorm, r->ron); |
405 |
> |
ndotd = DOT(vdif, r->ron); |
406 |
> |
nadotd = DOT(vdif, anorm); |
407 |
> |
a = normdot*normdot - cost70sq; |
408 |
> |
b = 2.0*(normdot*ndotd - nadotd*cost70sq); |
409 |
> |
c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq; |
410 |
> |
if (quadratic(t, a, b, c) != 2) |
411 |
> |
return(1); /* should rarely happen */ |
412 |
> |
if (t[1] <= FTINY) |
413 |
> |
return(0); /* should fail behind test */ |
414 |
> |
rayorigin(&rtst, SHADOW, r, NULL); |
415 |
> |
VSUM(rtst.rdir, vdif, anorm, t[1]); /* further dist. > plane */ |
416 |
> |
rtst.rmax = normalize(rtst.rdir); /* short ray test */ |
417 |
> |
while (localhit(&rtst, &thescene)) { /* check for occluder */ |
418 |
> |
if (rtst.ro->omod != OVOID && |
419 |
> |
(rtst.clipset == NULL || |
420 |
> |
!inset(rtst.clipset, rtst.ro->omod))) |
421 |
> |
return(1); /* plug light leak */ |
422 |
> |
VCOPY(rtst.rorg, rtst.rop); /* skip invisible surface */ |
423 |
> |
rtst.rmax -= rtst.rot; |
424 |
> |
rayclear(&rtst); |
425 |
> |
} |
426 |
> |
return(0); /* seems we're OK */ |
427 |
> |
} |
428 |
> |
|
429 |
> |
|
430 |
> |
static double |
431 |
|
sumambient( /* get interpolated ambient value */ |
432 |
|
COLOR acol, |
433 |
|
RAY *r, |
439 |
|
) |
440 |
|
{ /* initial limit is 10 degrees plus ambacc radians */ |
441 |
|
const double minangle = 10.0 * PI/180.; |
442 |
< |
const double maxangle = (minangle+ambacc-PI/2.)*pow(r->rweight,0.13) |
361 |
< |
+ PI/2.; |
442 |
> |
double maxangle = minangle + ambacc; |
443 |
|
double wsum = 0.0; |
444 |
|
FVECT ck0; |
445 |
|
int i, j; |
446 |
|
AMBVAL *av; |
447 |
+ |
|
448 |
+ |
if (at->kid != NULL) { /* sum children first */ |
449 |
+ |
s *= 0.5; |
450 |
+ |
for (i = 0; i < 8; i++) { |
451 |
+ |
for (j = 0; j < 3; j++) { |
452 |
+ |
ck0[j] = c0[j]; |
453 |
+ |
if (1<<j & i) |
454 |
+ |
ck0[j] += s; |
455 |
+ |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
456 |
+ |
break; |
457 |
+ |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
458 |
+ |
break; |
459 |
+ |
} |
460 |
+ |
if (j == 3) |
461 |
+ |
wsum += sumambient(acol, r, rn, al, |
462 |
+ |
at->kid+i, ck0, s); |
463 |
+ |
} |
464 |
+ |
/* good enough? */ |
465 |
+ |
if (wsum >= 0.05 && s > minarad*10.0) |
466 |
+ |
return(wsum); |
467 |
+ |
} |
468 |
+ |
/* adjust maximum angle */ |
469 |
+ |
if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6)) |
470 |
+ |
maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.; |
471 |
|
/* sum this node */ |
472 |
|
for (av = at->alist; av != NULL; av = av->next) { |
473 |
< |
double d, delta_r2, delta_t2; |
473 |
> |
double u, v, d, delta_r2, delta_t2; |
474 |
|
COLOR ct; |
475 |
|
FVECT uvw[3]; |
476 |
|
/* record access */ |
479 |
|
/* |
480 |
|
* Ambient level test |
481 |
|
*/ |
482 |
< |
if (av->lvl > al) /* list sorted, so this works */ |
482 |
> |
if (av->lvl > al || /* list sorted, so this works */ |
483 |
> |
(av->lvl == al) & (av->weight < 0.9*r->rweight)) |
484 |
|
break; |
379 |
– |
if (av->weight < 0.9*r->rweight) |
380 |
– |
continue; |
485 |
|
/* |
486 |
|
* Direction test using unperturbed normal |
487 |
|
*/ |
495 |
|
/* |
496 |
|
* Modified ray behind test |
497 |
|
*/ |
498 |
< |
VSUB(ck0, av->pos, r->rop); |
498 |
> |
VSUB(ck0, r->rop, av->pos); |
499 |
|
d = DOT(ck0, uvw[2]); |
500 |
< |
if (d < -minarad*qambacc-.001) |
500 |
> |
if (d < -minarad*ambacc-.001) |
501 |
|
continue; |
502 |
|
d /= av->rad[0]; |
503 |
|
delta_t2 = d*d; |
504 |
< |
if (delta_t2 >= qambacc*qambacc) |
504 |
> |
if (delta_t2 >= ambacc*ambacc) |
505 |
|
continue; |
506 |
|
/* |
507 |
|
* Elliptical radii test based on Hessian |
508 |
|
*/ |
509 |
|
decodedir(uvw[0], av->udir); |
510 |
|
VCROSS(uvw[1], uvw[2], uvw[0]); |
511 |
< |
d = DOT(ck0, uvw[0]) / av->rad[0]; |
511 |
> |
d = (u = DOT(ck0, uvw[0])) / av->rad[0]; |
512 |
|
delta_t2 += d*d; |
513 |
< |
d = DOT(ck0, uvw[1]) / av->rad[1]; |
513 |
> |
d = (v = DOT(ck0, uvw[1])) / av->rad[1]; |
514 |
|
delta_t2 += d*d; |
515 |
< |
if (delta_t2 >= qambacc*qambacc) |
515 |
> |
if (delta_t2 >= ambacc*ambacc) |
516 |
|
continue; |
517 |
|
/* |
518 |
+ |
* Test for potential light leak |
519 |
+ |
*/ |
520 |
+ |
if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u))) |
521 |
+ |
continue; |
522 |
+ |
/* |
523 |
|
* Extrapolate value and compute final weight (hat function) |
524 |
|
*/ |
525 |
< |
extambient(ct, av, r->rop, rn, uvw); |
525 |
> |
if (!extambient(ct, av, r->rop, rn, uvw)) |
526 |
> |
continue; |
527 |
|
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
528 |
< |
tfunc(qambacc, sqrt(delta_t2), 0.0); |
528 |
> |
tfunc(ambacc, sqrt(delta_t2), 0.0); |
529 |
|
scalecolor(ct, d); |
530 |
|
addcolor(acol, ct); |
531 |
|
wsum += d; |
532 |
|
} |
423 |
– |
if (at->kid == NULL) |
424 |
– |
return(wsum); |
425 |
– |
/* sum children */ |
426 |
– |
s *= 0.5; |
427 |
– |
for (i = 0; i < 8; i++) { |
428 |
– |
for (j = 0; j < 3; j++) { |
429 |
– |
ck0[j] = c0[j]; |
430 |
– |
if (1<<j & i) |
431 |
– |
ck0[j] += s; |
432 |
– |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
433 |
– |
break; |
434 |
– |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
435 |
– |
break; |
436 |
– |
} |
437 |
– |
if (j == 3) |
438 |
– |
wsum += sumambient(acol, r, rn, al, |
439 |
– |
at->kid+i, ck0, s); |
440 |
– |
} |
533 |
|
return(wsum); |
534 |
|
} |
535 |
|
|
536 |
|
|
537 |
< |
int |
537 |
> |
static int |
538 |
|
makeambient( /* make a new ambient value for storage */ |
539 |
|
COLOR acol, |
540 |
|
RAY *r, |
553 |
|
amb.weight = 1.25*r->rweight; |
554 |
|
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
555 |
|
/* compute ambient */ |
556 |
< |
i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir); |
556 |
> |
i = doambient(acol, r, amb.weight, |
557 |
> |
uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral); |
558 |
|
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
559 |
|
if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */ |
560 |
|
return(i); |
574 |
|
} |
575 |
|
|
576 |
|
|
577 |
< |
void |
577 |
> |
static int |
578 |
|
extambient( /* extrapolate value at pv, nv */ |
579 |
|
COLOR cr, |
580 |
|
AMBVAL *ap, |
583 |
|
FVECT uvw[3] |
584 |
|
) |
585 |
|
{ |
586 |
+ |
const double min_d = 0.05; |
587 |
|
static FVECT my_uvw[3]; |
588 |
|
FVECT v1; |
589 |
|
int i; |
603 |
|
for (i = 3; i--; ) |
604 |
|
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
605 |
|
|
606 |
< |
if (d <= 0.0) { |
607 |
< |
setcolor(cr, 0.0, 0.0, 0.0); |
514 |
< |
return; |
515 |
< |
} |
606 |
> |
if (d < min_d) /* should not use if we can avoid it */ |
607 |
> |
d = min_d; |
608 |
|
copycolor(cr, ap->val); |
609 |
|
scalecolor(cr, d); |
610 |
+ |
return(d > min_d); |
611 |
|
} |
612 |
|
|
613 |
|
|
629 |
|
at = &atrunk; |
630 |
|
VCOPY(ck0, thescene.cuorg); |
631 |
|
s = thescene.cusize; |
632 |
< |
while (s*(OCTSCALE/2) > av->rad[1]*qambacc) { |
632 |
> |
while (s*(OCTSCALE/2) > av->rad[1]*ambacc) { |
633 |
|
if (at->kid == NULL) |
634 |
|
if ((at->kid = newambtree()) == NULL) |
635 |
|
error(SYSTEM, "out of memory in avinsert"); |
644 |
|
} |
645 |
|
avh.next = at->alist; /* order by increasing level */ |
646 |
|
for (ap = &avh; ap->next != NULL; ap = ap->next) |
647 |
< |
if (ap->next->lvl >= av->lvl) |
647 |
> |
if ( ap->next->lvl > av->lvl || |
648 |
> |
(ap->next->lvl == av->lvl) & |
649 |
> |
(ap->next->weight <= av->weight) ) |
650 |
|
break; |
651 |
|
av->next = ap->next; |
652 |
|
ap->next = (AMBVAL*)av; |
670 |
|
) |
671 |
|
{ |
672 |
|
static int rdepth = 0; /* ambient recursion */ |
673 |
< |
COLOR acol; |
673 |
> |
COLOR acol, caustic; |
674 |
|
double d, l; |
675 |
|
|
676 |
+ |
/* PMAP: Factor in ambient from global photon map (if enabled) and return |
677 |
+ |
* as all ambient components accounted for */ |
678 |
+ |
if (ambPmap(aval, r, rdepth)) |
679 |
+ |
return; |
680 |
+ |
|
681 |
+ |
/* PMAP: Otherwise factor in ambient from caustic photon map |
682 |
+ |
* (ambPmapCaustic() returns zero if caustic photons disabled) and |
683 |
+ |
* continue with RADIANCE ambient calculation */ |
684 |
+ |
copycolor(caustic, aval); |
685 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
686 |
+ |
|
687 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
688 |
|
goto dumbamb; |
689 |
|
/* check number of bounces */ |
701 |
|
rdepth--; |
702 |
|
if (d <= FTINY) |
703 |
|
goto dumbamb; |
704 |
< |
copycolor(aval, acol); |
704 |
> |
copycolor(aval, acol); |
705 |
> |
|
706 |
> |
/* PMAP: add in caustic */ |
707 |
> |
addcolor(aval, caustic); |
708 |
|
return; |
709 |
|
} |
710 |
|
|
714 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
715 |
|
d = sumambient(acol, r, nrm, rdepth, |
716 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
717 |
+ |
|
718 |
|
if (d > FTINY) { |
719 |
|
d = 1.0/d; |
720 |
|
scalecolor(acol, d); |
721 |
|
multcolor(aval, acol); |
722 |
+ |
|
723 |
+ |
/* PMAP: add in caustic */ |
724 |
+ |
addcolor(aval, caustic); |
725 |
|
return; |
726 |
|
} |
727 |
+ |
|
728 |
|
rdepth++; /* need to cache new value */ |
729 |
|
d = makeambient(acol, r, nrm, rdepth-1); |
730 |
|
rdepth--; |
731 |
+ |
|
732 |
|
if (d > FTINY) { |
733 |
|
multcolor(aval, acol); /* got new value */ |
734 |
+ |
|
735 |
+ |
/* PMAP: add in caustic */ |
736 |
+ |
addcolor(aval, caustic); |
737 |
|
return; |
738 |
|
} |
739 |
+ |
|
740 |
|
dumbamb: /* return global value */ |
741 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
742 |
|
multcolor(aval, ambval); |
743 |
+ |
|
744 |
+ |
/* PMAP: add in caustic */ |
745 |
+ |
addcolor(aval, caustic); |
746 |
|
return; |
747 |
|
} |
748 |
+ |
|
749 |
|
l = bright(ambval); /* average in computations */ |
750 |
|
if (l > FTINY) { |
751 |
|
d = (log(l)*(double)ambvwt + avsum) / |
787 |
|
/* |
788 |
|
* Ambient level test. |
789 |
|
*/ |
790 |
< |
if (av->lvl > al) /* list sorted, so this works */ |
790 |
> |
if (av->lvl > al || /* list sorted, so this works */ |
791 |
> |
(av->lvl == al) & (av->weight < 0.9*r->rweight)) |
792 |
|
break; |
669 |
– |
if (av->weight < 0.9*r->rweight) |
670 |
– |
continue; |
793 |
|
/* |
794 |
|
* Ambient radius test. |
795 |
|
*/ |
971 |
|
} |
972 |
|
avh.next = at->alist; /* order by increasing level */ |
973 |
|
for (ap = &avh; ap->next != NULL; ap = ap->next) |
974 |
< |
if (ap->next->lvl >= av->lvl) |
974 |
> |
if ( ap->next->lvl > av->lvl || |
975 |
> |
(ap->next->lvl == av->lvl) & |
976 |
> |
(ap->next->weight <= av->weight) ) |
977 |
|
break; |
978 |
|
av->next = ap->next; |
979 |
|
ap->next = (AMBVAL*)av; |
1025 |
|
AMBVAL *av |
1026 |
|
) |
1027 |
|
{ |
1028 |
< |
avinsert(avstore(av)); |
1028 |
> |
avstore(av); |
1029 |
|
if (ambfp == NULL) |
1030 |
|
return; |
1031 |
|
if (writambval(av, ambfp) < 0) |
1040 |
|
|
1041 |
|
|
1042 |
|
static AMBVAL * |
1043 |
< |
avstore( /* allocate memory and store aval */ |
1043 |
> |
avstore( /* allocate memory and save aval */ |
1044 |
|
AMBVAL *aval |
1045 |
|
) |
1046 |
|
{ |
1058 |
|
avsum += log(d); |
1059 |
|
navsum++; |
1060 |
|
} |
1061 |
+ |
avinsert(av); /* insert in our cache tree */ |
1062 |
|
return(av); |
1063 |
|
} |
1064 |
|
|
1085 |
|
} |
1086 |
|
atp = atfreelist; |
1087 |
|
atfreelist = atp->kid; |
1088 |
< |
memset((char *)atp, '\0', 8*sizeof(AMBTREE)); |
1088 |
> |
memset(atp, 0, 8*sizeof(AMBTREE)); |
1089 |
|
return(atp); |
1090 |
|
} |
1091 |
|
|
1111 |
|
/* transfer values at this node */ |
1112 |
|
for (av = at->alist; av != NULL; av = at->alist) { |
1113 |
|
at->alist = av->next; |
1114 |
+ |
av->next = NULL; |
1115 |
|
(*f)(av); |
1116 |
|
} |
1117 |
|
if (at->kid == NULL) |
1189 |
|
{ |
1190 |
|
AMBVAL **avlpp; |
1191 |
|
|
1192 |
< |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
1193 |
< |
nambvals, sizeof(AMBVAL *), &aposcmp); |
1192 |
> |
avlpp = (AMBVAL **)bsearch(&avaddr, avlist2, |
1193 |
> |
nambvals, sizeof(AMBVAL *), aposcmp); |
1194 |
|
if (avlpp == NULL) |
1195 |
|
error(CONSISTENCY, "address not found in avlmemi"); |
1196 |
|
return(avlpp - avlist2); |
1233 |
|
} |
1234 |
|
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
1235 |
|
if (avlist2 != NULL) |
1236 |
< |
free((void *)avlist2); |
1236 |
> |
free(avlist2); |
1237 |
|
if (always) { /* rebuild without sorting */ |
1238 |
|
oldatrunk = atrunk; |
1239 |
|
atrunk.alist = NULL; |
1240 |
|
atrunk.kid = NULL; |
1241 |
< |
unloadatree(&oldatrunk, &avinsert); |
1241 |
> |
unloadatree(&oldatrunk, avinsert); |
1242 |
|
} |
1243 |
|
} else { /* sort memory by last access time */ |
1244 |
|
/* |
1255 |
|
eputs(errmsg); |
1256 |
|
#endif |
1257 |
|
i_avlist = 0; |
1258 |
< |
unloadatree(&atrunk, &av2list); /* empty current tree */ |
1258 |
> |
unloadatree(&atrunk, av2list); /* empty current tree */ |
1259 |
|
#ifdef DEBUG |
1260 |
|
if (i_avlist < nambvals) |
1261 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
1262 |
|
#endif |
1263 |
< |
qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp); |
1264 |
< |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp); |
1263 |
> |
qsort(avlist1, nambvals, sizeof(struct avl), alatcmp); |
1264 |
> |
qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
1265 |
|
for (i = 0; i < nambvals; i++) { |
1266 |
|
if (avlist1[i].p == NULL) |
1267 |
|
continue; |
1277 |
|
avinsert(avlist2[j]); |
1278 |
|
avlist1[j].p = NULL; |
1279 |
|
} |
1280 |
< |
free((void *)avlist1); |
1281 |
< |
free((void *)avlist2); |
1280 |
> |
free(avlist1); |
1281 |
> |
free(avlist2); |
1282 |
|
/* compute new sort interval */ |
1283 |
|
sortintvl = ambclock - lastsort; |
1284 |
|
if (sortintvl >= MAX_SORT_INTVL/2) |
1342 |
|
error(WARNING, errmsg); |
1343 |
|
break; |
1344 |
|
} |
1345 |
< |
avinsert(avstore(&avs)); |
1345 |
> |
avstore(&avs); |
1346 |
|
n -= AMBVALSIZ; |
1347 |
|
} |
1348 |
|
lastpos = flen - n; |