51 |
|
#define MAX_SORT_INTVL (SORT_INTVL<<6) |
52 |
|
#endif |
53 |
|
|
54 |
+ |
|
55 |
+ |
static double qambacc = 0.; /* ambient accuracy to the 1/4 power */ |
56 |
|
static double avsum = 0.; /* computed ambient value sum (log) */ |
57 |
|
static unsigned int navsum = 0; /* number of values in avsum */ |
58 |
|
static unsigned int nambvals = 0; /* total number of indirect values */ |
129 |
|
double newa |
130 |
|
) |
131 |
|
{ |
132 |
< |
double ambdiff; |
133 |
< |
|
134 |
< |
if (newa < 0.0) |
135 |
< |
newa = 0.0; |
136 |
< |
ambdiff = fabs(newa - ambacc); |
137 |
< |
if (ambdiff >= .01 && (ambacc = newa) > FTINY && nambvals > 0) |
138 |
< |
sortambvals(1); /* rebuild tree */ |
132 |
> |
double olda = qambacc*qambacc*qambacc*qambacc; |
133 |
> |
|
134 |
> |
newa *= (newa > 0); |
135 |
> |
if (fabs(newa - olda) >= .05*(newa + olda)) { |
136 |
> |
qambacc = sqrt(sqrt(ambacc = newa)); |
137 |
> |
if (nambvals > 0) |
138 |
> |
sortambvals(1); /* rebuild tree */ |
139 |
> |
} |
140 |
|
} |
141 |
|
|
142 |
|
|
266 |
|
|
267 |
|
#ifdef NEWAMB |
268 |
|
|
269 |
< |
#define tfunc(lwr, x, upr) (((x)-(lwr)/((upr)-(lwr))) |
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 |
|
|
277 |
|
void |
278 |
|
multambient( /* compute ambient component & multiply by coef. */ |
323 |
|
ok = makeambient(acol, r, nrm, rdepth-1); |
324 |
|
rdepth--; |
325 |
|
if (ok) { |
326 |
< |
multcolor(aval, acol); /* got new value */ |
326 |
> |
multcolor(aval, acol); /* computed new value */ |
327 |
|
return; |
328 |
|
} |
329 |
|
dumbamb: /* return global value */ |
346 |
|
|
347 |
|
|
348 |
|
double |
349 |
< |
sumambient( /* get interpolated ambient value */ |
349 |
> |
sumambient( /* get interpolated ambient value */ |
350 |
|
COLOR acol, |
351 |
|
RAY *r, |
352 |
|
FVECT rn, |
355 |
|
FVECT c0, |
356 |
|
double s |
357 |
|
) |
358 |
< |
{ /* initial limit is ambacc radians */ |
359 |
< |
const double maxangle = (ambacc-PI/2.)*pow(r->rweight,0.13) + PI/2.; |
358 |
> |
{ /* initial limit is 10 degrees plus ambacc radians */ |
359 |
> |
const double minangle = 10.0 * PI/180.; |
360 |
> |
const double maxangle = (minangle+ambacc-PI/2.)*pow(r->rweight,0.13) |
361 |
> |
+ PI/2.; |
362 |
|
double wsum = 0.0; |
363 |
|
FVECT ck0; |
364 |
|
int i, j; |
372 |
|
if (tracktime) |
373 |
|
av->latick = ambclock; |
374 |
|
/* |
375 |
< |
* Ambient level test. |
375 |
> |
* Ambient level test |
376 |
|
*/ |
377 |
|
if (av->lvl > al) /* list sorted, so this works */ |
378 |
|
break; |
379 |
|
if (av->weight < 0.9*r->rweight) |
380 |
|
continue; |
381 |
|
/* |
382 |
< |
* Direction test using unperturbed normal. |
382 |
> |
* Direction test using unperturbed normal |
383 |
|
*/ |
384 |
|
decodedir(uvw[2], av->ndir); |
385 |
|
d = DOT(uvw[2], r->ron); |
389 |
|
if (delta_r2 >= maxangle*maxangle) |
390 |
|
continue; |
391 |
|
/* |
392 |
< |
* Ambient radius test. |
392 |
> |
* Modified ray behind test |
393 |
|
*/ |
394 |
+ |
VSUB(ck0, av->pos, r->rop); |
395 |
+ |
d = DOT(ck0, uvw[2]); |
396 |
+ |
if (d < -minarad*qambacc-.001) |
397 |
+ |
continue; |
398 |
+ |
d /= av->rad[0]; |
399 |
+ |
delta_t2 = d*d; |
400 |
+ |
if (delta_t2 >= qambacc*qambacc) |
401 |
+ |
continue; |
402 |
+ |
/* |
403 |
+ |
* Elliptical radii test based on Hessian |
404 |
+ |
*/ |
405 |
|
decodedir(uvw[0], av->udir); |
406 |
|
VCROSS(uvw[1], uvw[2], uvw[0]); |
386 |
– |
VSUB(ck0, av->pos, r->rop); |
407 |
|
d = DOT(ck0, uvw[0]) / av->rad[0]; |
408 |
< |
delta_t2 = d*d; |
408 |
> |
delta_t2 += d*d; |
409 |
|
d = DOT(ck0, uvw[1]) / av->rad[1]; |
410 |
|
delta_t2 += d*d; |
411 |
< |
if (delta_t2 >= ambacc*ambacc) |
411 |
> |
if (delta_t2 >= qambacc*qambacc) |
412 |
|
continue; |
413 |
|
/* |
414 |
< |
* Ray behind test. |
414 |
> |
* Extrapolate value and compute final weight (hat function) |
415 |
|
*/ |
416 |
< |
d = 0.0; |
397 |
< |
for (j = 0; j < 3; j++) |
398 |
< |
d += (r->rop[j] - av->pos[j])*(uvw[2][j] + r->ron[j]); |
399 |
< |
if (d*0.5 < -minarad*ambacc-.001) |
400 |
< |
continue; |
401 |
< |
/* |
402 |
< |
* Convert to final weight (hat function) |
403 |
< |
*/ |
416 |
> |
extambient(ct, av, r->rop, rn, uvw); |
417 |
|
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
418 |
< |
tfunc(ambacc, sqrt(delta_t2), 0.0); |
406 |
< |
wsum += d; |
407 |
< |
extambient(ct, av, uvw, r->rop, rn); |
418 |
> |
tfunc(qambacc, sqrt(delta_t2), 0.0); |
419 |
|
scalecolor(ct, d); |
420 |
|
addcolor(acol, ct); |
421 |
+ |
wsum += d; |
422 |
|
} |
423 |
|
if (at->kid == NULL) |
424 |
|
return(wsum); |
451 |
|
) |
452 |
|
{ |
453 |
|
AMBVAL amb; |
454 |
< |
FVECT uv[2]; |
454 |
> |
FVECT uvw[3]; |
455 |
|
int i; |
456 |
|
|
457 |
|
amb.weight = 1.0; /* compute weight */ |
461 |
|
amb.weight = 1.25*r->rweight; |
462 |
|
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
463 |
|
/* compute ambient */ |
464 |
< |
if (!doambient(acol, r, amb.weight, uv, amb.rad, amb.gpos, amb.gdir)) { |
453 |
< |
setcolor(acol, 0.0, 0.0, 0.0); |
454 |
< |
return(0); |
455 |
< |
} |
464 |
> |
i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir); |
465 |
|
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
466 |
+ |
if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */ |
467 |
+ |
return(i); |
468 |
|
/* store value */ |
469 |
|
VCOPY(amb.pos, r->rop); |
470 |
|
amb.ndir = encodedir(r->ron); |
471 |
< |
amb.udir = encodedir(uv[0]); |
471 |
> |
amb.udir = encodedir(uvw[0]); |
472 |
|
amb.lvl = al; |
473 |
|
copycolor(amb.val, acol); |
474 |
|
/* insert into tree */ |
475 |
|
avsave(&amb); /* and save to file */ |
476 |
< |
if (rn != r->ron) |
477 |
< |
extambient(acol, &amb, r->rop, rn); /* texture */ |
476 |
> |
if (rn != r->ron) { /* texture */ |
477 |
> |
VCOPY(uvw[2], r->ron); |
478 |
> |
extambient(acol, &amb, r->rop, rn, uvw); |
479 |
> |
} |
480 |
|
return(1); |
481 |
|
} |
482 |
|
|
485 |
|
extambient( /* extrapolate value at pv, nv */ |
486 |
|
COLOR cr, |
487 |
|
AMBVAL *ap, |
475 |
– |
FVECT uvw[3], |
488 |
|
FVECT pv, |
489 |
< |
FVECT nv |
489 |
> |
FVECT nv, |
490 |
> |
FVECT uvw[3] |
491 |
|
) |
492 |
|
{ |
493 |
< |
FVECT v1; |
494 |
< |
int i; |
495 |
< |
double d = 1.0; /* zeroeth order */ |
493 |
> |
static FVECT my_uvw[3]; |
494 |
> |
FVECT v1; |
495 |
> |
int i; |
496 |
> |
double d = 1.0; /* zeroeth order */ |
497 |
|
|
498 |
+ |
if (uvw == NULL) { /* need local coordinates? */ |
499 |
+ |
decodedir(my_uvw[2], ap->ndir); |
500 |
+ |
decodedir(my_uvw[0], ap->udir); |
501 |
+ |
VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]); |
502 |
+ |
uvw = my_uvw; |
503 |
+ |
} |
504 |
|
for (i = 3; i--; ) /* gradient due to translation */ |
505 |
|
d += (pv[i] - ap->pos[i]) * |
506 |
|
(ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]); |
536 |
|
at = &atrunk; |
537 |
|
VCOPY(ck0, thescene.cuorg); |
538 |
|
s = thescene.cusize; |
539 |
< |
while (s*(OCTSCALE/2) > av->rad[1]*ambacc) { |
539 |
> |
while (s*(OCTSCALE/2) > av->rad[1]*qambacc) { |
540 |
|
if (at->kid == NULL) |
541 |
|
if ((at->kid = newambtree()) == NULL) |
542 |
|
error(SYSTEM, "out of memory in avinsert"); |
561 |
|
|
562 |
|
#else /* ! NEWAMB */ |
563 |
|
|
564 |
+ |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
565 |
+ |
AMBTREE *at, FVECT c0, double s); |
566 |
+ |
static double makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
567 |
+ |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv); |
568 |
|
|
569 |
+ |
|
570 |
|
void |
571 |
|
multambient( /* compute ambient component & multiply by coef. */ |
572 |
|
COLOR aval, |
637 |
|
} |
638 |
|
|
639 |
|
|
640 |
< |
double |
640 |
> |
static double |
641 |
|
sumambient( /* get interpolated ambient value */ |
642 |
|
COLOR acol, |
643 |
|
RAY *r, |
748 |
|
} |
749 |
|
|
750 |
|
|
751 |
< |
double |
751 |
> |
static double |
752 |
|
makeambient( /* make a new ambient value for storage */ |
753 |
|
COLOR acol, |
754 |
|
RAY *r, |
788 |
|
} |
789 |
|
|
790 |
|
|
791 |
< |
void |
791 |
> |
static void |
792 |
|
extambient( /* extrapolate value at pv, nv */ |
793 |
|
COLOR cr, |
794 |
|
AMBVAL *ap, |