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 */ |
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 */ |
137 |
> |
if (ambdiff >= .01 && (ambacc = newa) > FTINY) { |
138 |
> |
qambacc = sqrt(sqrt(ambacc)); |
139 |
> |
if (nambvals > 0) |
140 |
> |
sortambvals(1); /* rebuild tree */ |
141 |
> |
} |
142 |
|
} |
143 |
|
|
144 |
|
|
152 |
|
ambdone(); |
153 |
|
/* init ambient limits */ |
154 |
|
setambres(ambres); |
155 |
< |
setambacc(ambacc); |
155 |
> |
qambacc = sqrt(sqrt(ambacc *= (ambacc > FTINY))); |
156 |
|
if (ambfile == NULL || !ambfile[0]) |
157 |
|
return; |
158 |
|
if (ambacc <= FTINY) { |
268 |
|
|
269 |
|
#ifdef NEWAMB |
270 |
|
|
271 |
< |
#define tfunc(lwr, x, upr) (((x)-(lwr)/((upr)-(lwr))) |
271 |
> |
#define tfunc(lwr, x, upr) (((x)-(lwr))/((upr)-(lwr))) |
272 |
|
|
273 |
+ |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
274 |
+ |
AMBTREE *at, FVECT c0, double s); |
275 |
+ |
static int makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
276 |
+ |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv, |
277 |
+ |
FVECT uvw[3]); |
278 |
|
|
279 |
|
void |
280 |
|
multambient( /* compute ambient component & multiply by coef. */ |
325 |
|
ok = makeambient(acol, r, nrm, rdepth-1); |
326 |
|
rdepth--; |
327 |
|
if (ok) { |
328 |
< |
multcolor(aval, acol); /* got new value */ |
328 |
> |
multcolor(aval, acol); /* computed new value */ |
329 |
|
return; |
330 |
|
} |
331 |
|
dumbamb: /* return global value */ |
348 |
|
|
349 |
|
|
350 |
|
double |
351 |
< |
sumambient( /* get interpolated ambient value */ |
351 |
> |
sumambient( /* get interpolated ambient value */ |
352 |
|
COLOR acol, |
353 |
|
RAY *r, |
354 |
|
FVECT rn, |
357 |
|
FVECT c0, |
358 |
|
double s |
359 |
|
) |
360 |
< |
{ /* initial limit is ambacc radians */ |
361 |
< |
const double maxangle = (ambacc-PI/2.)*pow(r->rweight,0.13) + PI/2.; |
360 |
> |
{ /* initial limit is 10 degrees plus ambacc radians */ |
361 |
> |
const double minangle = 10.0 * PI/180.; |
362 |
> |
const double maxangle = (minangle+ambacc-PI/2.)*pow(r->rweight,0.13) |
363 |
> |
+ PI/2.; |
364 |
|
double wsum = 0.0; |
365 |
|
FVECT ck0; |
366 |
|
int i, j; |
374 |
|
if (tracktime) |
375 |
|
av->latick = ambclock; |
376 |
|
/* |
377 |
< |
* Ambient level test. |
377 |
> |
* Ambient level test |
378 |
|
*/ |
379 |
|
if (av->lvl > al) /* list sorted, so this works */ |
380 |
|
break; |
381 |
|
if (av->weight < 0.9*r->rweight) |
382 |
|
continue; |
383 |
|
/* |
384 |
< |
* Direction test using unperturbed normal. |
384 |
> |
* Direction test using unperturbed normal |
385 |
|
*/ |
386 |
|
decodedir(uvw[2], av->ndir); |
387 |
|
d = DOT(uvw[2], r->ron); |
391 |
|
if (delta_r2 >= maxangle*maxangle) |
392 |
|
continue; |
393 |
|
/* |
394 |
< |
* Ambient radius test. |
394 |
> |
* Modified ray behind test |
395 |
|
*/ |
396 |
+ |
VSUB(ck0, av->pos, r->rop); |
397 |
+ |
d = DOT(ck0, uvw[2]); |
398 |
+ |
if (d < -minarad*qambacc-.001) |
399 |
+ |
continue; |
400 |
+ |
d /= av->rad[0]; |
401 |
+ |
delta_t2 = d*d; |
402 |
+ |
if (delta_t2 >= qambacc*qambacc) |
403 |
+ |
continue; |
404 |
+ |
/* |
405 |
+ |
* Elliptical radii test based on Hessian |
406 |
+ |
*/ |
407 |
|
decodedir(uvw[0], av->udir); |
408 |
|
VCROSS(uvw[1], uvw[2], uvw[0]); |
386 |
– |
VSUB(ck0, av->pos, r->rop); |
409 |
|
d = DOT(ck0, uvw[0]) / av->rad[0]; |
410 |
< |
delta_t2 = d*d; |
410 |
> |
delta_t2 += d*d; |
411 |
|
d = DOT(ck0, uvw[1]) / av->rad[1]; |
412 |
|
delta_t2 += d*d; |
413 |
< |
if (delta_t2 >= ambacc*ambacc) |
413 |
> |
if (delta_t2 >= qambacc*qambacc) |
414 |
|
continue; |
415 |
|
/* |
416 |
< |
* Ray behind test. |
416 |
> |
* Extrapolate value and compute final weight (hat function) |
417 |
|
*/ |
418 |
< |
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 |
< |
*/ |
418 |
> |
extambient(ct, av, r->rop, rn, uvw); |
419 |
|
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
420 |
< |
tfunc(ambacc, sqrt(delta_t2), 0.0); |
406 |
< |
wsum += d; |
407 |
< |
extambient(ct, av, uvw, r->rop, rn); |
420 |
> |
tfunc(qambacc, sqrt(delta_t2), 0.0); |
421 |
|
scalecolor(ct, d); |
422 |
|
addcolor(acol, ct); |
423 |
+ |
wsum += d; |
424 |
|
} |
425 |
|
if (at->kid == NULL) |
426 |
|
return(wsum); |
453 |
|
) |
454 |
|
{ |
455 |
|
AMBVAL amb; |
456 |
< |
FVECT uv[2]; |
456 |
> |
FVECT uvw[3]; |
457 |
|
int i; |
458 |
|
|
459 |
|
amb.weight = 1.0; /* compute weight */ |
463 |
|
amb.weight = 1.25*r->rweight; |
464 |
|
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
465 |
|
/* compute ambient */ |
466 |
< |
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 |
< |
} |
466 |
> |
i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir); |
467 |
|
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
468 |
+ |
if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */ |
469 |
+ |
return(i); |
470 |
|
/* store value */ |
471 |
|
VCOPY(amb.pos, r->rop); |
472 |
|
amb.ndir = encodedir(r->ron); |
473 |
< |
amb.udir = encodedir(uv[0]); |
473 |
> |
amb.udir = encodedir(uvw[0]); |
474 |
|
amb.lvl = al; |
475 |
|
copycolor(amb.val, acol); |
476 |
|
/* insert into tree */ |
477 |
|
avsave(&amb); /* and save to file */ |
478 |
< |
if (rn != r->ron) |
479 |
< |
extambient(acol, &amb, r->rop, rn); /* texture */ |
478 |
> |
if (rn != r->ron) { /* texture */ |
479 |
> |
VCOPY(uvw[2], r->ron); |
480 |
> |
extambient(acol, &amb, r->rop, rn, uvw); |
481 |
> |
} |
482 |
|
return(1); |
483 |
|
} |
484 |
|
|
487 |
|
extambient( /* extrapolate value at pv, nv */ |
488 |
|
COLOR cr, |
489 |
|
AMBVAL *ap, |
475 |
– |
FVECT uvw[3], |
490 |
|
FVECT pv, |
491 |
< |
FVECT nv |
491 |
> |
FVECT nv, |
492 |
> |
FVECT uvw[3] |
493 |
|
) |
494 |
|
{ |
495 |
< |
FVECT v1; |
496 |
< |
int i; |
497 |
< |
double d = 1.0; /* zeroeth order */ |
495 |
> |
static FVECT my_uvw[3]; |
496 |
> |
FVECT v1; |
497 |
> |
int i; |
498 |
> |
double d = 1.0; /* zeroeth order */ |
499 |
|
|
500 |
+ |
if (uvw == NULL) { /* need local coordinates? */ |
501 |
+ |
decodedir(my_uvw[2], ap->ndir); |
502 |
+ |
decodedir(my_uvw[0], ap->udir); |
503 |
+ |
VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]); |
504 |
+ |
uvw = my_uvw; |
505 |
+ |
} |
506 |
|
for (i = 3; i--; ) /* gradient due to translation */ |
507 |
|
d += (pv[i] - ap->pos[i]) * |
508 |
|
(ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]); |
538 |
|
at = &atrunk; |
539 |
|
VCOPY(ck0, thescene.cuorg); |
540 |
|
s = thescene.cusize; |
541 |
< |
while (s*(OCTSCALE/2) > av->rad[1]*ambacc) { |
541 |
> |
while (s*(OCTSCALE/2) > av->rad[1]*qambacc) { |
542 |
|
if (at->kid == NULL) |
543 |
|
if ((at->kid = newambtree()) == NULL) |
544 |
|
error(SYSTEM, "out of memory in avinsert"); |
563 |
|
|
564 |
|
#else /* ! NEWAMB */ |
565 |
|
|
566 |
+ |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
567 |
+ |
AMBTREE *at, FVECT c0, double s); |
568 |
+ |
static double makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
569 |
+ |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv); |
570 |
|
|
571 |
+ |
|
572 |
|
void |
573 |
|
multambient( /* compute ambient component & multiply by coef. */ |
574 |
|
COLOR aval, |
639 |
|
} |
640 |
|
|
641 |
|
|
642 |
< |
double |
642 |
> |
static double |
643 |
|
sumambient( /* get interpolated ambient value */ |
644 |
|
COLOR acol, |
645 |
|
RAY *r, |
750 |
|
} |
751 |
|
|
752 |
|
|
753 |
< |
double |
753 |
> |
static double |
754 |
|
makeambient( /* make a new ambient value for storage */ |
755 |
|
COLOR acol, |
756 |
|
RAY *r, |
790 |
|
} |
791 |
|
|
792 |
|
|
793 |
< |
void |
793 |
> |
static void |
794 |
|
extambient( /* extrapolate value at pv, nv */ |
795 |
|
COLOR cr, |
796 |
|
AMBVAL *ap, |