1 |
– |
#ifndef lint |
1 |
|
static const char RCSid[] = "$Id$"; |
3 |
– |
#endif |
2 |
|
/* |
3 |
|
* ambient.c - routines dealing with ambient (inter-reflected) component. |
4 |
|
* |
12 |
|
#include "platform.h" |
13 |
|
#include "ray.h" |
14 |
|
#include "otypes.h" |
15 |
+ |
#include "otspecial.h" |
16 |
|
#include "resolu.h" |
17 |
|
#include "ambient.h" |
18 |
|
#include "random.h" |
19 |
+ |
#include "pmapamb.h" |
20 |
|
|
21 |
|
#ifndef OCTSCALE |
22 |
|
#define OCTSCALE 1.0 /* ceil((valid rad.)/(cube size)) */ |
52 |
|
#endif |
53 |
|
|
54 |
|
|
55 |
– |
static double qambacc = 0.; /* ambient accuracy to the 1/4 power */ |
55 |
|
static double avsum = 0.; /* computed ambient value sum (log) */ |
56 |
|
static unsigned int navsum = 0; /* number of values in avsum */ |
57 |
|
static unsigned int nambvals = 0; /* total number of indirect values */ |
77 |
|
#define AMBFLUSH (BUFSIZ/AMBVALSIZ) |
78 |
|
|
79 |
|
#define newambval() (AMBVAL *)malloc(sizeof(AMBVAL)) |
81 |
– |
#define freeav(av) free((void *)av); |
80 |
|
|
81 |
+ |
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
82 |
+ |
|
83 |
|
static void initambfile(int creat); |
84 |
|
static void avsave(AMBVAL *av); |
85 |
|
static AMBVAL *avstore(AMBVAL *aval); |
96 |
|
static int avlmemi(AMBVAL *avaddr); |
97 |
|
static void sortambvals(int always); |
98 |
|
|
99 |
+ |
static int plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang); |
100 |
+ |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
101 |
+ |
AMBTREE *at, FVECT c0, double s); |
102 |
+ |
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
103 |
+ |
static int extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
104 |
+ |
FVECT uvw[3]); |
105 |
+ |
|
106 |
|
#ifdef F_SETLKW |
107 |
|
static void aflock(int typ); |
108 |
|
#endif |
117 |
|
/* set min & max radii */ |
118 |
|
if (ar <= 0) { |
119 |
|
minarad = 0; |
120 |
< |
maxarad = thescene.cusize*0.5; |
120 |
> |
maxarad = thescene.cusize*0.2; |
121 |
|
} else { |
122 |
|
minarad = thescene.cusize / ar; |
123 |
|
maxarad = 64.0 * minarad; /* heuristic */ |
124 |
< |
if (maxarad > thescene.cusize*0.5) |
125 |
< |
maxarad = thescene.cusize*0.5; |
124 |
> |
if (maxarad > thescene.cusize*0.2) |
125 |
> |
maxarad = thescene.cusize*0.2; |
126 |
|
} |
127 |
|
if (minarad <= FTINY) |
128 |
|
minarad = 10.0*FTINY; |
136 |
|
double newa |
137 |
|
) |
138 |
|
{ |
139 |
< |
double olda = qambacc*qambacc*qambacc*qambacc; |
139 |
> |
static double olda; /* remember previous setting here */ |
140 |
|
|
141 |
|
newa *= (newa > 0); |
142 |
|
if (fabs(newa - olda) >= .05*(newa + olda)) { |
143 |
< |
qambacc = sqrt(sqrt(ambacc = newa)); |
144 |
< |
if (nambvals > 0) |
143 |
> |
ambacc = newa; |
144 |
> |
if (ambacc > FTINY && nambvals > 0) |
145 |
|
sortambvals(1); /* rebuild tree */ |
146 |
|
} |
147 |
|
} |
173 |
|
initambfile(0); /* file exists */ |
174 |
|
lastpos = ftell(ambfp); |
175 |
|
while (readambval(&amb, ambfp)) |
176 |
< |
avinsert(avstore(&amb)); |
176 |
> |
avstore(&amb); |
177 |
|
nambshare = nambvals; /* share loaded values */ |
178 |
|
if (readonly) { |
179 |
|
sprintf(errmsg, |
193 |
|
(flen - lastpos)/AMBVALSIZ); |
194 |
|
error(WARNING, errmsg); |
195 |
|
fseek(ambfp, lastpos, SEEK_SET); |
189 |
– |
#ifndef _WIN32 /* XXX we need a replacement for that one */ |
196 |
|
ftruncate(fileno(ambfp), (off_t)lastpos); |
191 |
– |
#endif |
197 |
|
} |
198 |
|
} else if ((ambfp = fopen(ambfile, "w+")) != NULL) { |
199 |
|
initambfile(1); /* else create new file */ |
203 |
|
sprintf(errmsg, "cannot open ambient file \"%s\"", ambfile); |
204 |
|
error(SYSTEM, errmsg); |
205 |
|
} |
201 |
– |
#ifdef getc_unlocked |
202 |
– |
flockfile(ambfp); /* application-level lock */ |
203 |
– |
#endif |
206 |
|
#ifdef F_SETLKW |
207 |
|
aflock(F_UNLCK); /* release file */ |
208 |
|
#endif |
223 |
|
lastpos = -1; |
224 |
|
} |
225 |
|
/* free ambient tree */ |
226 |
< |
unloadatree(&atrunk, &avfree); |
226 |
> |
unloadatree(&atrunk, avfree); |
227 |
|
/* reset state variables */ |
228 |
|
avsum = 0.; |
229 |
|
navsum = 0; |
264 |
|
} |
265 |
|
} |
266 |
|
|
265 |
– |
/************ THE FOLLOWING ROUTINES DIFFER BETWEEN NEW & OLD ***************/ |
267 |
|
|
267 |
– |
#ifdef NEWAMB |
268 |
– |
|
269 |
– |
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
270 |
– |
|
271 |
– |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
272 |
– |
AMBTREE *at, FVECT c0, double s); |
273 |
– |
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
274 |
– |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
275 |
– |
FVECT uvw[3]); |
276 |
– |
|
268 |
|
void |
269 |
|
multambient( /* compute ambient component & multiply by coef. */ |
270 |
|
COLOR aval, |
273 |
|
) |
274 |
|
{ |
275 |
|
static int rdepth = 0; /* ambient recursion */ |
276 |
< |
COLOR acol; |
277 |
< |
int ok; |
276 |
> |
COLOR acol, caustic; |
277 |
> |
int i, ok; |
278 |
|
double d, l; |
279 |
|
|
280 |
+ |
/* PMAP: Factor in ambient from photon map, if enabled and ray is |
281 |
+ |
* ambient. Return as all ambient components accounted for, else |
282 |
+ |
* continue. */ |
283 |
+ |
if (ambPmap(aval, r, rdepth)) |
284 |
+ |
return; |
285 |
+ |
|
286 |
+ |
/* PMAP: Factor in specular-diffuse ambient (caustics) from photon |
287 |
+ |
* map, if enabled and ray is primary, else caustic is zero. Continue |
288 |
+ |
* with RADIANCE ambient calculation */ |
289 |
+ |
copycolor(caustic, aval); |
290 |
+ |
ambPmapCaustic(caustic, r, rdepth); |
291 |
+ |
|
292 |
|
if (ambdiv <= 0) /* no ambient calculation */ |
293 |
|
goto dumbamb; |
294 |
|
/* check number of bounces */ |
300 |
|
goto dumbamb; |
301 |
|
|
302 |
|
if (ambacc <= FTINY) { /* no ambient storage */ |
303 |
+ |
FVECT uvd[2]; |
304 |
+ |
float dgrad[2], *dgp = NULL; |
305 |
+ |
|
306 |
+ |
if (nrm != r->ron && DOT(nrm,r->ron) < 0.9999) |
307 |
+ |
dgp = dgrad; /* compute rotational grad. */ |
308 |
|
copycolor(acol, aval); |
309 |
|
rdepth++; |
310 |
< |
ok = doambient(acol, r, r->rweight, NULL, NULL, NULL, NULL); |
310 |
> |
ok = doambient(acol, r, r->rweight, |
311 |
> |
uvd, NULL, NULL, dgp, NULL); |
312 |
|
rdepth--; |
313 |
|
if (!ok) |
314 |
|
goto dumbamb; |
315 |
+ |
if ((ok > 0) & (dgp != NULL)) { /* apply texture */ |
316 |
+ |
FVECT v1; |
317 |
+ |
VCROSS(v1, r->ron, nrm); |
318 |
+ |
d = 1.0; |
319 |
+ |
for (i = 3; i--; ) |
320 |
+ |
d += v1[i] * (dgp[0]*uvd[0][i] + dgp[1]*uvd[1][i]); |
321 |
+ |
if (d >= 0.05) |
322 |
+ |
scalecolor(acol, d); |
323 |
+ |
} |
324 |
|
copycolor(aval, acol); |
325 |
+ |
|
326 |
+ |
/* PMAP: add in caustic */ |
327 |
+ |
addcolor(aval, caustic); |
328 |
|
return; |
329 |
|
} |
330 |
|
|
334 |
|
setcolor(acol, 0.0, 0.0, 0.0); |
335 |
|
d = sumambient(acol, r, nrm, rdepth, |
336 |
|
&atrunk, thescene.cuorg, thescene.cusize); |
337 |
+ |
|
338 |
|
if (d > FTINY) { |
339 |
|
d = 1.0/d; |
340 |
|
scalecolor(acol, d); |
341 |
|
multcolor(aval, acol); |
342 |
+ |
|
343 |
+ |
/* PMAP: add in caustic */ |
344 |
+ |
addcolor(aval, caustic); |
345 |
|
return; |
346 |
|
} |
347 |
+ |
|
348 |
|
rdepth++; /* need to cache new value */ |
349 |
|
ok = makeambient(acol, r, nrm, rdepth-1); |
350 |
|
rdepth--; |
351 |
+ |
|
352 |
|
if (ok) { |
353 |
|
multcolor(aval, acol); /* computed new value */ |
354 |
+ |
|
355 |
+ |
/* PMAP: add in caustic */ |
356 |
+ |
addcolor(aval, caustic); |
357 |
|
return; |
358 |
|
} |
359 |
+ |
|
360 |
|
dumbamb: /* return global value */ |
361 |
|
if ((ambvwt <= 0) | (navsum == 0)) { |
362 |
|
multcolor(aval, ambval); |
363 |
+ |
|
364 |
+ |
/* PMAP: add in caustic */ |
365 |
+ |
addcolor(aval, caustic); |
366 |
|
return; |
367 |
|
} |
368 |
< |
l = bright(ambval); /* average in computations */ |
368 |
> |
|
369 |
> |
l = bright(ambval); /* average in computations */ |
370 |
|
if (l > FTINY) { |
371 |
|
d = (log(l)*(double)ambvwt + avsum) / |
372 |
|
(double)(ambvwt + navsum); |
380 |
|
} |
381 |
|
|
382 |
|
|
383 |
< |
double |
383 |
> |
/* Plug a potential leak where ambient cache value is occluded */ |
384 |
> |
static int |
385 |
> |
plugaleak(RAY *r, AMBVAL *ap, FVECT anorm, double ang) |
386 |
> |
{ |
387 |
> |
const double cost70sq = 0.1169778; /* cos(70deg)^2 */ |
388 |
> |
RAY rtst; |
389 |
> |
FVECT vdif; |
390 |
> |
double normdot, ndotd, nadotd; |
391 |
> |
double a, b, c, t[2]; |
392 |
> |
|
393 |
> |
ang += 2.*PI*(ang < 0); /* check direction flags */ |
394 |
> |
if ( !(ap->corral>>(int)(ang*(16./PI)) & 1) ) |
395 |
> |
return(0); |
396 |
> |
/* |
397 |
> |
* Generate test ray, targeting 20 degrees above sample point plane |
398 |
> |
* along surface normal from cache position. This should be high |
399 |
> |
* enough to miss local geometry we don't really care about. |
400 |
> |
*/ |
401 |
> |
VSUB(vdif, ap->pos, r->rop); |
402 |
> |
normdot = DOT(anorm, r->ron); |
403 |
> |
ndotd = DOT(vdif, r->ron); |
404 |
> |
nadotd = DOT(vdif, anorm); |
405 |
> |
a = normdot*normdot - cost70sq; |
406 |
> |
b = 2.0*(normdot*ndotd - nadotd*cost70sq); |
407 |
> |
c = ndotd*ndotd - DOT(vdif,vdif)*cost70sq; |
408 |
> |
if (quadratic(t, a, b, c) != 2) |
409 |
> |
return(1); /* should rarely happen */ |
410 |
> |
if (t[1] <= FTINY) |
411 |
> |
return(0); /* should fail behind test */ |
412 |
> |
rayorigin(&rtst, SHADOW, r, NULL); |
413 |
> |
VSUM(rtst.rdir, vdif, anorm, t[1]); /* further dist. > plane */ |
414 |
> |
rtst.rmax = normalize(rtst.rdir); /* short ray test */ |
415 |
> |
while (localhit(&rtst, &thescene)) { /* check for occluder */ |
416 |
> |
OBJREC *m = findmaterial(rtst.ro); |
417 |
> |
if (m != NULL && !istransp(m->otype) && !isBSDFproxy(m) && |
418 |
> |
(rtst.clipset == NULL || |
419 |
> |
!inset(rtst.clipset, rtst.ro->omod))) |
420 |
> |
return(1); /* plug light leak */ |
421 |
> |
VCOPY(rtst.rorg, rtst.rop); /* skip invisible surface */ |
422 |
> |
rtst.rmax -= rtst.rot; |
423 |
> |
rayclear(&rtst); |
424 |
> |
} |
425 |
> |
return(0); /* seems we're OK */ |
426 |
> |
} |
427 |
> |
|
428 |
> |
|
429 |
> |
static double |
430 |
|
sumambient( /* get interpolated ambient value */ |
431 |
|
COLOR acol, |
432 |
|
RAY *r, |
438 |
|
) |
439 |
|
{ /* initial limit is 10 degrees plus ambacc radians */ |
440 |
|
const double minangle = 10.0 * PI/180.; |
441 |
< |
const double maxangle = (minangle+ambacc-PI/2.)*pow(r->rweight,0.13) |
361 |
< |
+ PI/2.; |
441 |
> |
double maxangle = minangle + ambacc; |
442 |
|
double wsum = 0.0; |
443 |
|
FVECT ck0; |
444 |
|
int i, j; |
461 |
|
at->kid+i, ck0, s); |
462 |
|
} |
463 |
|
/* good enough? */ |
464 |
< |
if (wsum > 0.04 && s > (minarad*0.8+maxarad*0.2)) |
464 |
> |
if (wsum >= 0.05 && s > minarad*10.0) |
465 |
|
return(wsum); |
466 |
|
} |
467 |
+ |
/* adjust maximum angle */ |
468 |
+ |
if (at->alist != NULL && (at->alist->lvl <= al) & (r->rweight < 0.6)) |
469 |
+ |
maxangle = (maxangle - PI/2.)*pow(r->rweight,0.13) + PI/2.; |
470 |
|
/* sum this node */ |
471 |
|
for (av = at->alist; av != NULL; av = av->next) { |
472 |
< |
double d, delta_r2, delta_t2; |
472 |
> |
double u, v, d, delta_r2, delta_t2; |
473 |
|
COLOR ct; |
474 |
|
FVECT uvw[3]; |
475 |
|
/* record access */ |
478 |
|
/* |
479 |
|
* Ambient level test |
480 |
|
*/ |
481 |
< |
if (av->lvl > al) /* list sorted, so this works */ |
481 |
> |
if (av->lvl > al || /* list sorted, so this works */ |
482 |
> |
(av->lvl == al) & (av->weight < 0.9*r->rweight)) |
483 |
|
break; |
400 |
– |
if (av->weight < 0.9*r->rweight) |
401 |
– |
continue; |
484 |
|
/* |
485 |
|
* Direction test using unperturbed normal |
486 |
|
*/ |
494 |
|
/* |
495 |
|
* Modified ray behind test |
496 |
|
*/ |
497 |
< |
VSUB(ck0, av->pos, r->rop); |
497 |
> |
VSUB(ck0, r->rop, av->pos); |
498 |
|
d = DOT(ck0, uvw[2]); |
499 |
< |
if (d < -minarad*qambacc-.001) |
499 |
> |
if (d < -minarad*ambacc-.001) |
500 |
|
continue; |
501 |
|
d /= av->rad[0]; |
502 |
|
delta_t2 = d*d; |
503 |
< |
if (delta_t2 >= qambacc*qambacc) |
503 |
> |
if (delta_t2 >= ambacc*ambacc) |
504 |
|
continue; |
505 |
|
/* |
506 |
|
* Elliptical radii test based on Hessian |
507 |
|
*/ |
508 |
|
decodedir(uvw[0], av->udir); |
509 |
|
VCROSS(uvw[1], uvw[2], uvw[0]); |
510 |
< |
d = DOT(ck0, uvw[0]) / av->rad[0]; |
510 |
> |
d = (u = DOT(ck0, uvw[0])) / av->rad[0]; |
511 |
|
delta_t2 += d*d; |
512 |
< |
d = DOT(ck0, uvw[1]) / av->rad[1]; |
512 |
> |
d = (v = DOT(ck0, uvw[1])) / av->rad[1]; |
513 |
|
delta_t2 += d*d; |
514 |
< |
if (delta_t2 >= qambacc*qambacc) |
514 |
> |
if (delta_t2 >= ambacc*ambacc) |
515 |
|
continue; |
516 |
|
/* |
517 |
+ |
* Test for potential light leak |
518 |
+ |
*/ |
519 |
+ |
if (av->corral && plugaleak(r, av, uvw[2], atan2a(v,u))) |
520 |
+ |
continue; |
521 |
+ |
/* |
522 |
|
* Extrapolate value and compute final weight (hat function) |
523 |
|
*/ |
524 |
< |
extambient(ct, av, r->rop, rn, uvw); |
524 |
> |
if (!extambient(ct, av, r->rop, rn, uvw)) |
525 |
> |
continue; |
526 |
|
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
527 |
< |
tfunc(qambacc, sqrt(delta_t2), 0.0); |
527 |
> |
tfunc(ambacc, sqrt(delta_t2), 0.0); |
528 |
|
scalecolor(ct, d); |
529 |
|
addcolor(acol, ct); |
530 |
|
wsum += d; |
533 |
|
} |
534 |
|
|
535 |
|
|
536 |
< |
int |
536 |
> |
static int |
537 |
|
makeambient( /* make a new ambient value for storage */ |
538 |
|
COLOR acol, |
539 |
|
RAY *r, |
552 |
|
amb.weight = 1.25*r->rweight; |
553 |
|
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
554 |
|
/* compute ambient */ |
555 |
< |
i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir); |
555 |
> |
i = doambient(acol, r, amb.weight, |
556 |
> |
uvw, amb.rad, amb.gpos, amb.gdir, &amb.corral); |
557 |
|
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
558 |
|
if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */ |
559 |
|
return(i); |
573 |
|
} |
574 |
|
|
575 |
|
|
576 |
< |
void |
576 |
> |
static int |
577 |
|
extambient( /* extrapolate value at pv, nv */ |
578 |
|
COLOR cr, |
579 |
|
AMBVAL *ap, |
582 |
|
FVECT uvw[3] |
583 |
|
) |
584 |
|
{ |
585 |
+ |
const double min_d = 0.05; |
586 |
|
static FVECT my_uvw[3]; |
587 |
|
FVECT v1; |
588 |
|
int i; |
602 |
|
for (i = 3; i--; ) |
603 |
|
d += v1[i] * (ap->gdir[0]*uvw[0][i] + ap->gdir[1]*uvw[1][i]); |
604 |
|
|
605 |
< |
if (d <= 0.0) { |
606 |
< |
setcolor(cr, 0.0, 0.0, 0.0); |
517 |
< |
return; |
518 |
< |
} |
605 |
> |
if (d < min_d) /* should not use if we can avoid it */ |
606 |
> |
d = min_d; |
607 |
|
copycolor(cr, ap->val); |
608 |
|
scalecolor(cr, d); |
609 |
+ |
return(d > min_d); |
610 |
|
} |
611 |
|
|
612 |
|
|
628 |
|
at = &atrunk; |
629 |
|
VCOPY(ck0, thescene.cuorg); |
630 |
|
s = thescene.cusize; |
631 |
< |
while (s*(OCTSCALE/2) > av->rad[1]*qambacc) { |
631 |
> |
while (s*(OCTSCALE/2) > av->rad[1]*ambacc) { |
632 |
|
if (at->kid == NULL) |
633 |
|
if ((at->kid = newambtree()) == NULL) |
634 |
|
error(SYSTEM, "out of memory in avinsert"); |
643 |
|
} |
644 |
|
avh.next = at->alist; /* order by increasing level */ |
645 |
|
for (ap = &avh; ap->next != NULL; ap = ap->next) |
646 |
< |
if (ap->next->lvl >= av->lvl) |
646 |
> |
if ( ap->next->lvl > av->lvl || |
647 |
> |
(ap->next->lvl == av->lvl) & |
648 |
> |
(ap->next->weight <= av->weight) ) |
649 |
|
break; |
650 |
|
av->next = ap->next; |
651 |
|
ap->next = (AMBVAL*)av; |
653 |
|
} |
654 |
|
|
655 |
|
|
565 |
– |
#else /* ! NEWAMB */ |
566 |
– |
|
567 |
– |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
568 |
– |
AMBTREE *at, FVECT c0, double s); |
569 |
– |
static double makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
570 |
– |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv); |
571 |
– |
|
572 |
– |
|
573 |
– |
void |
574 |
– |
multambient( /* compute ambient component & multiply by coef. */ |
575 |
– |
COLOR aval, |
576 |
– |
RAY *r, |
577 |
– |
FVECT nrm |
578 |
– |
) |
579 |
– |
{ |
580 |
– |
static int rdepth = 0; /* ambient recursion */ |
581 |
– |
COLOR acol; |
582 |
– |
double d, l; |
583 |
– |
|
584 |
– |
if (ambdiv <= 0) /* no ambient calculation */ |
585 |
– |
goto dumbamb; |
586 |
– |
/* check number of bounces */ |
587 |
– |
if (rdepth >= ambounce) |
588 |
– |
goto dumbamb; |
589 |
– |
/* check ambient list */ |
590 |
– |
if (ambincl != -1 && r->ro != NULL && |
591 |
– |
ambincl != inset(ambset, r->ro->omod)) |
592 |
– |
goto dumbamb; |
593 |
– |
|
594 |
– |
if (ambacc <= FTINY) { /* no ambient storage */ |
595 |
– |
copycolor(acol, aval); |
596 |
– |
rdepth++; |
597 |
– |
d = doambient(acol, r, r->rweight, NULL, NULL); |
598 |
– |
rdepth--; |
599 |
– |
if (d <= FTINY) |
600 |
– |
goto dumbamb; |
601 |
– |
copycolor(aval, acol); |
602 |
– |
return; |
603 |
– |
} |
604 |
– |
|
605 |
– |
if (tracktime) /* sort to minimize thrashing */ |
606 |
– |
sortambvals(0); |
607 |
– |
/* interpolate ambient value */ |
608 |
– |
setcolor(acol, 0.0, 0.0, 0.0); |
609 |
– |
d = sumambient(acol, r, nrm, rdepth, |
610 |
– |
&atrunk, thescene.cuorg, thescene.cusize); |
611 |
– |
if (d > FTINY) { |
612 |
– |
d = 1.0/d; |
613 |
– |
scalecolor(acol, d); |
614 |
– |
multcolor(aval, acol); |
615 |
– |
return; |
616 |
– |
} |
617 |
– |
rdepth++; /* need to cache new value */ |
618 |
– |
d = makeambient(acol, r, nrm, rdepth-1); |
619 |
– |
rdepth--; |
620 |
– |
if (d > FTINY) { |
621 |
– |
multcolor(aval, acol); /* got new value */ |
622 |
– |
return; |
623 |
– |
} |
624 |
– |
dumbamb: /* return global value */ |
625 |
– |
if ((ambvwt <= 0) | (navsum == 0)) { |
626 |
– |
multcolor(aval, ambval); |
627 |
– |
return; |
628 |
– |
} |
629 |
– |
l = bright(ambval); /* average in computations */ |
630 |
– |
if (l > FTINY) { |
631 |
– |
d = (log(l)*(double)ambvwt + avsum) / |
632 |
– |
(double)(ambvwt + navsum); |
633 |
– |
d = exp(d) / l; |
634 |
– |
scalecolor(aval, d); |
635 |
– |
multcolor(aval, ambval); /* apply color of ambval */ |
636 |
– |
} else { |
637 |
– |
d = exp( avsum / (double)navsum ); |
638 |
– |
scalecolor(aval, d); /* neutral color */ |
639 |
– |
} |
640 |
– |
} |
641 |
– |
|
642 |
– |
|
643 |
– |
static double |
644 |
– |
sumambient( /* get interpolated ambient value */ |
645 |
– |
COLOR acol, |
646 |
– |
RAY *r, |
647 |
– |
FVECT rn, |
648 |
– |
int al, |
649 |
– |
AMBTREE *at, |
650 |
– |
FVECT c0, |
651 |
– |
double s |
652 |
– |
) |
653 |
– |
{ |
654 |
– |
double d, e1, e2, wt, wsum; |
655 |
– |
COLOR ct; |
656 |
– |
FVECT ck0; |
657 |
– |
int i; |
658 |
– |
int j; |
659 |
– |
AMBVAL *av; |
660 |
– |
|
661 |
– |
wsum = 0.0; |
662 |
– |
/* do this node */ |
663 |
– |
for (av = at->alist; av != NULL; av = av->next) { |
664 |
– |
double rn_dot = -2.0; |
665 |
– |
if (tracktime) |
666 |
– |
av->latick = ambclock; |
667 |
– |
/* |
668 |
– |
* Ambient level test. |
669 |
– |
*/ |
670 |
– |
if (av->lvl > al) /* list sorted, so this works */ |
671 |
– |
break; |
672 |
– |
if (av->weight < 0.9*r->rweight) |
673 |
– |
continue; |
674 |
– |
/* |
675 |
– |
* Ambient radius test. |
676 |
– |
*/ |
677 |
– |
VSUB(ck0, av->pos, r->rop); |
678 |
– |
e1 = DOT(ck0, ck0) / (av->rad * av->rad); |
679 |
– |
if (e1 > ambacc*ambacc*1.21) |
680 |
– |
continue; |
681 |
– |
/* |
682 |
– |
* Direction test using closest normal. |
683 |
– |
*/ |
684 |
– |
d = DOT(av->dir, r->ron); |
685 |
– |
if (rn != r->ron) { |
686 |
– |
rn_dot = DOT(av->dir, rn); |
687 |
– |
if (rn_dot > 1.0-FTINY) |
688 |
– |
rn_dot = 1.0-FTINY; |
689 |
– |
if (rn_dot >= d-FTINY) { |
690 |
– |
d = rn_dot; |
691 |
– |
rn_dot = -2.0; |
692 |
– |
} |
693 |
– |
} |
694 |
– |
e2 = (1.0 - d) * r->rweight; |
695 |
– |
if (e2 < 0.0) |
696 |
– |
e2 = 0.0; |
697 |
– |
else if (e1 + e2 > ambacc*ambacc*1.21) |
698 |
– |
continue; |
699 |
– |
/* |
700 |
– |
* Ray behind test. |
701 |
– |
*/ |
702 |
– |
d = 0.0; |
703 |
– |
for (j = 0; j < 3; j++) |
704 |
– |
d += (r->rop[j] - av->pos[j]) * |
705 |
– |
(av->dir[j] + r->ron[j]); |
706 |
– |
if (d*0.5 < -minarad*ambacc-.001) |
707 |
– |
continue; |
708 |
– |
/* |
709 |
– |
* Jittering final test reduces image artifacts. |
710 |
– |
*/ |
711 |
– |
e1 = sqrt(e1); |
712 |
– |
e2 = sqrt(e2); |
713 |
– |
wt = e1 + e2; |
714 |
– |
if (wt > ambacc*(.9+.2*urand(9015+samplendx))) |
715 |
– |
continue; |
716 |
– |
/* |
717 |
– |
* Recompute directional error using perturbed normal |
718 |
– |
*/ |
719 |
– |
if (rn_dot > 0.0) { |
720 |
– |
e2 = sqrt((1.0 - rn_dot)*r->rweight); |
721 |
– |
wt = e1 + e2; |
722 |
– |
} |
723 |
– |
if (wt <= 1e-3) |
724 |
– |
wt = 1e3; |
725 |
– |
else |
726 |
– |
wt = 1.0 / wt; |
727 |
– |
wsum += wt; |
728 |
– |
extambient(ct, av, r->rop, rn); |
729 |
– |
scalecolor(ct, wt); |
730 |
– |
addcolor(acol, ct); |
731 |
– |
} |
732 |
– |
if (at->kid == NULL) |
733 |
– |
return(wsum); |
734 |
– |
/* do children */ |
735 |
– |
s *= 0.5; |
736 |
– |
for (i = 0; i < 8; i++) { |
737 |
– |
for (j = 0; j < 3; j++) { |
738 |
– |
ck0[j] = c0[j]; |
739 |
– |
if (1<<j & i) |
740 |
– |
ck0[j] += s; |
741 |
– |
if (r->rop[j] < ck0[j] - OCTSCALE*s) |
742 |
– |
break; |
743 |
– |
if (r->rop[j] > ck0[j] + (1.0+OCTSCALE)*s) |
744 |
– |
break; |
745 |
– |
} |
746 |
– |
if (j == 3) |
747 |
– |
wsum += sumambient(acol, r, rn, al, |
748 |
– |
at->kid+i, ck0, s); |
749 |
– |
} |
750 |
– |
return(wsum); |
751 |
– |
} |
752 |
– |
|
753 |
– |
|
754 |
– |
static double |
755 |
– |
makeambient( /* make a new ambient value for storage */ |
756 |
– |
COLOR acol, |
757 |
– |
RAY *r, |
758 |
– |
FVECT rn, |
759 |
– |
int al |
760 |
– |
) |
761 |
– |
{ |
762 |
– |
AMBVAL amb; |
763 |
– |
FVECT gp, gd; |
764 |
– |
int i; |
765 |
– |
|
766 |
– |
amb.weight = 1.0; /* compute weight */ |
767 |
– |
for (i = al; i-- > 0; ) |
768 |
– |
amb.weight *= AVGREFL; |
769 |
– |
if (r->rweight < 0.1*amb.weight) /* heuristic override */ |
770 |
– |
amb.weight = 1.25*r->rweight; |
771 |
– |
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
772 |
– |
/* compute ambient */ |
773 |
– |
amb.rad = doambient(acol, r, amb.weight, gp, gd); |
774 |
– |
if (amb.rad <= FTINY) { |
775 |
– |
setcolor(acol, 0.0, 0.0, 0.0); |
776 |
– |
return(0.0); |
777 |
– |
} |
778 |
– |
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
779 |
– |
/* store value */ |
780 |
– |
VCOPY(amb.pos, r->rop); |
781 |
– |
VCOPY(amb.dir, r->ron); |
782 |
– |
amb.lvl = al; |
783 |
– |
copycolor(amb.val, acol); |
784 |
– |
VCOPY(amb.gpos, gp); |
785 |
– |
VCOPY(amb.gdir, gd); |
786 |
– |
/* insert into tree */ |
787 |
– |
avsave(&amb); /* and save to file */ |
788 |
– |
if (rn != r->ron) |
789 |
– |
extambient(acol, &amb, r->rop, rn); /* texture */ |
790 |
– |
return(amb.rad); |
791 |
– |
} |
792 |
– |
|
793 |
– |
|
656 |
|
static void |
795 |
– |
extambient( /* extrapolate value at pv, nv */ |
796 |
– |
COLOR cr, |
797 |
– |
AMBVAL *ap, |
798 |
– |
FVECT pv, |
799 |
– |
FVECT nv |
800 |
– |
) |
801 |
– |
{ |
802 |
– |
FVECT v1; |
803 |
– |
int i; |
804 |
– |
double d; |
805 |
– |
|
806 |
– |
d = 1.0; /* zeroeth order */ |
807 |
– |
/* gradient due to translation */ |
808 |
– |
for (i = 0; i < 3; i++) |
809 |
– |
d += ap->gpos[i]*(pv[i]-ap->pos[i]); |
810 |
– |
/* gradient due to rotation */ |
811 |
– |
VCROSS(v1, ap->dir, nv); |
812 |
– |
d += DOT(ap->gdir, v1); |
813 |
– |
if (d <= 0.0) { |
814 |
– |
setcolor(cr, 0.0, 0.0, 0.0); |
815 |
– |
return; |
816 |
– |
} |
817 |
– |
copycolor(cr, ap->val); |
818 |
– |
scalecolor(cr, d); |
819 |
– |
} |
820 |
– |
|
821 |
– |
|
822 |
– |
static void |
823 |
– |
avinsert( /* insert ambient value in our tree */ |
824 |
– |
AMBVAL *av |
825 |
– |
) |
826 |
– |
{ |
827 |
– |
AMBTREE *at; |
828 |
– |
AMBVAL *ap; |
829 |
– |
AMBVAL avh; |
830 |
– |
FVECT ck0; |
831 |
– |
double s; |
832 |
– |
int branch; |
833 |
– |
int i; |
834 |
– |
|
835 |
– |
if (av->rad <= FTINY) |
836 |
– |
error(CONSISTENCY, "zero ambient radius in avinsert"); |
837 |
– |
at = &atrunk; |
838 |
– |
VCOPY(ck0, thescene.cuorg); |
839 |
– |
s = thescene.cusize; |
840 |
– |
while (s*(OCTSCALE/2) > av->rad*ambacc) { |
841 |
– |
if (at->kid == NULL) |
842 |
– |
if ((at->kid = newambtree()) == NULL) |
843 |
– |
error(SYSTEM, "out of memory in avinsert"); |
844 |
– |
s *= 0.5; |
845 |
– |
branch = 0; |
846 |
– |
for (i = 0; i < 3; i++) |
847 |
– |
if (av->pos[i] > ck0[i] + s) { |
848 |
– |
ck0[i] += s; |
849 |
– |
branch |= 1 << i; |
850 |
– |
} |
851 |
– |
at = at->kid + branch; |
852 |
– |
} |
853 |
– |
avh.next = at->alist; /* order by increasing level */ |
854 |
– |
for (ap = &avh; ap->next != NULL; ap = ap->next) |
855 |
– |
if (ap->next->lvl >= av->lvl) |
856 |
– |
break; |
857 |
– |
av->next = ap->next; |
858 |
– |
ap->next = (AMBVAL*)av; |
859 |
– |
at->alist = avh.next; |
860 |
– |
} |
861 |
– |
|
862 |
– |
#endif /* ! NEWAMB */ |
863 |
– |
|
864 |
– |
/************* FOLLOWING ROUTINES SAME FOR NEW & OLD METHODS ***************/ |
865 |
– |
|
866 |
– |
static void |
657 |
|
initambfile( /* initialize ambient file */ |
658 |
|
int cre8 |
659 |
|
) |
694 |
|
AMBVAL *av |
695 |
|
) |
696 |
|
{ |
697 |
< |
avinsert(avstore(av)); |
697 |
> |
avstore(av); |
698 |
|
if (ambfp == NULL) |
699 |
|
return; |
700 |
|
if (writambval(av, ambfp) < 0) |
709 |
|
|
710 |
|
|
711 |
|
static AMBVAL * |
712 |
< |
avstore( /* allocate memory and store aval */ |
712 |
> |
avstore( /* allocate memory and save aval */ |
713 |
|
AMBVAL *aval |
714 |
|
) |
715 |
|
{ |
727 |
|
avsum += log(d); |
728 |
|
navsum++; |
729 |
|
} |
730 |
+ |
avinsert(av); /* insert in our cache tree */ |
731 |
|
return(av); |
732 |
|
} |
733 |
|
|
754 |
|
} |
755 |
|
atp = atfreelist; |
756 |
|
atfreelist = atp->kid; |
757 |
< |
memset((char *)atp, '\0', 8*sizeof(AMBTREE)); |
757 |
> |
memset(atp, 0, 8*sizeof(AMBTREE)); |
758 |
|
return(atp); |
759 |
|
} |
760 |
|
|
780 |
|
/* transfer values at this node */ |
781 |
|
for (av = at->alist; av != NULL; av = at->alist) { |
782 |
|
at->alist = av->next; |
783 |
+ |
av->next = NULL; |
784 |
|
(*f)(av); |
785 |
|
} |
786 |
|
if (at->kid == NULL) |
858 |
|
{ |
859 |
|
AMBVAL **avlpp; |
860 |
|
|
861 |
< |
avlpp = (AMBVAL **)bsearch((char *)&avaddr, (char *)avlist2, |
862 |
< |
nambvals, sizeof(AMBVAL *), &aposcmp); |
861 |
> |
avlpp = (AMBVAL **)bsearch(&avaddr, avlist2, |
862 |
> |
nambvals, sizeof(AMBVAL *), aposcmp); |
863 |
|
if (avlpp == NULL) |
864 |
|
error(CONSISTENCY, "address not found in avlmemi"); |
865 |
|
return(avlpp - avlist2); |
902 |
|
} |
903 |
|
if (avlist1 == NULL) { /* no time tracking -- rebuild tree? */ |
904 |
|
if (avlist2 != NULL) |
905 |
< |
free((void *)avlist2); |
905 |
> |
free(avlist2); |
906 |
|
if (always) { /* rebuild without sorting */ |
907 |
|
oldatrunk = atrunk; |
908 |
|
atrunk.alist = NULL; |
909 |
|
atrunk.kid = NULL; |
910 |
< |
unloadatree(&oldatrunk, &avinsert); |
910 |
> |
unloadatree(&oldatrunk, avinsert); |
911 |
|
} |
912 |
|
} else { /* sort memory by last access time */ |
913 |
|
/* |
924 |
|
eputs(errmsg); |
925 |
|
#endif |
926 |
|
i_avlist = 0; |
927 |
< |
unloadatree(&atrunk, &av2list); /* empty current tree */ |
927 |
> |
unloadatree(&atrunk, av2list); /* empty current tree */ |
928 |
|
#ifdef DEBUG |
929 |
|
if (i_avlist < nambvals) |
930 |
|
error(CONSISTENCY, "missing ambient values in sortambvals"); |
931 |
|
#endif |
932 |
< |
qsort((char *)avlist1, nambvals, sizeof(struct avl), &alatcmp); |
933 |
< |
qsort((char *)avlist2, nambvals, sizeof(AMBVAL *), &aposcmp); |
932 |
> |
qsort(avlist1, nambvals, sizeof(struct avl), alatcmp); |
933 |
> |
qsort(avlist2, nambvals, sizeof(AMBVAL *), aposcmp); |
934 |
|
for (i = 0; i < nambvals; i++) { |
935 |
|
if (avlist1[i].p == NULL) |
936 |
|
continue; |
946 |
|
avinsert(avlist2[j]); |
947 |
|
avlist1[j].p = NULL; |
948 |
|
} |
949 |
< |
free((void *)avlist1); |
950 |
< |
free((void *)avlist2); |
949 |
> |
free(avlist1); |
950 |
> |
free(avlist2); |
951 |
|
/* compute new sort interval */ |
952 |
|
sortintvl = ambclock - lastsort; |
953 |
|
if (sortintvl >= MAX_SORT_INTVL/2) |
975 |
|
|
976 |
|
if (typ == fls.l_type) /* already called? */ |
977 |
|
return; |
978 |
+ |
|
979 |
|
fls.l_type = typ; |
980 |
< |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) < 0) |
981 |
< |
error(SYSTEM, "cannot (un)lock ambient file"); |
980 |
> |
do |
981 |
> |
if (fcntl(fileno(ambfp), F_SETLKW, &fls) != -1) |
982 |
> |
return; |
983 |
> |
while (errno == EINTR); |
984 |
> |
|
985 |
> |
error(SYSTEM, "cannot (un)lock ambient file"); |
986 |
|
} |
987 |
|
|
988 |
|
|
1001 |
|
if ((flen = lseek(fileno(ambfp), (off_t)0, SEEK_END)) < 0) |
1002 |
|
goto seekerr; |
1003 |
|
if ((n = flen - lastpos) > 0) { /* file has grown */ |
1004 |
< |
if (ambinp == NULL) { /* use duplicate filedes */ |
1005 |
< |
ambinp = fdopen(dup(fileno(ambfp)), "r"); |
1004 |
> |
if (ambinp == NULL) { /* get new file pointer */ |
1005 |
> |
ambinp = fopen(ambfile, "rb"); |
1006 |
|
if (ambinp == NULL) |
1007 |
< |
error(SYSTEM, "fdopen failed in ambsync"); |
1007 |
> |
error(SYSTEM, "fopen failed in ambsync"); |
1008 |
|
} |
1009 |
|
if (fseek(ambinp, lastpos, SEEK_SET) < 0) |
1010 |
|
goto seekerr; |
1016 |
|
error(WARNING, errmsg); |
1017 |
|
break; |
1018 |
|
} |
1019 |
< |
avinsert(avstore(&avs)); |
1019 |
> |
avstore(&avs); |
1020 |
|
n -= AMBVALSIZ; |
1021 |
|
} |
1022 |
< |
lastpos = flen - n; |
1023 |
< |
/*** seek always as safety measure |
1024 |
< |
if (n) ***/ /* alignment */ |
1228 |
< |
if (lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1229 |
< |
goto seekerr; |
1022 |
> |
lastpos = flen - n; /* check alignment */ |
1023 |
> |
if (n && lseek(fileno(ambfp), (off_t)lastpos, SEEK_SET) < 0) |
1024 |
> |
goto seekerr; |
1025 |
|
} |
1026 |
|
n = fflush(ambfp); /* calls write() at last */ |
1027 |
< |
if (n != EOF) |
1233 |
< |
lastpos += (long)nunflshed*AMBVALSIZ; |
1234 |
< |
else if ((lastpos = lseek(fileno(ambfp), (off_t)0, SEEK_CUR)) < 0) |
1235 |
< |
goto seekerr; |
1236 |
< |
|
1027 |
> |
lastpos += (long)nunflshed*AMBVALSIZ; |
1028 |
|
aflock(F_UNLCK); /* release file */ |
1029 |
|
nunflshed = 0; |
1030 |
|
return(n); |
1031 |
|
seekerr: |
1032 |
|
error(SYSTEM, "seek failed in ambsync"); |
1033 |
< |
return -1; /* pro forma return */ |
1033 |
> |
return(EOF); /* pro forma return */ |
1034 |
|
} |
1035 |
|
|
1036 |
|
#else /* ! F_SETLKW */ |