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, |
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 |
> |
* Elliptical radii test based on Hessian |
393 |
|
*/ |
394 |
|
decodedir(uvw[0], av->udir); |
395 |
|
VCROSS(uvw[1], uvw[2], uvw[0]); |
398 |
|
delta_t2 = d*d; |
399 |
|
d = DOT(ck0, uvw[1]) / av->rad[1]; |
400 |
|
delta_t2 += d*d; |
401 |
< |
if (delta_t2 >= ambacc*ambacc) |
401 |
> |
if (delta_t2 >= qambacc*qambacc) |
402 |
|
continue; |
403 |
|
/* |
404 |
< |
* Ray behind test. |
404 |
> |
* Intersection behind test |
405 |
|
*/ |
406 |
|
d = 0.0; |
407 |
|
for (j = 0; j < 3; j++) |
409 |
|
if (d*0.5 < -minarad*ambacc-.001) |
410 |
|
continue; |
411 |
|
/* |
412 |
< |
* Convert to final weight (hat function) |
412 |
> |
* Extrapolate value and compute final weight (hat function) |
413 |
|
*/ |
414 |
+ |
extambient(ct, av, r->rop, rn, uvw); |
415 |
|
d = tfunc(maxangle, sqrt(delta_r2), 0.0) * |
416 |
< |
tfunc(ambacc, sqrt(delta_t2), 0.0); |
406 |
< |
wsum += d; |
407 |
< |
extambient(ct, av, uvw, r->rop, rn); |
416 |
> |
tfunc(qambacc, sqrt(delta_t2), 0.0); |
417 |
|
scalecolor(ct, d); |
418 |
|
addcolor(acol, ct); |
419 |
+ |
wsum += d; |
420 |
|
} |
421 |
|
if (at->kid == NULL) |
422 |
|
return(wsum); |
449 |
|
) |
450 |
|
{ |
451 |
|
AMBVAL amb; |
452 |
< |
FVECT uv[2]; |
452 |
> |
FVECT uvw[3]; |
453 |
|
int i; |
454 |
|
|
455 |
|
amb.weight = 1.0; /* compute weight */ |
459 |
|
amb.weight = 1.25*r->rweight; |
460 |
|
setcolor(acol, AVGREFL, AVGREFL, AVGREFL); |
461 |
|
/* compute ambient */ |
462 |
< |
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 |
< |
} |
462 |
> |
i = doambient(acol, r, amb.weight, uvw, amb.rad, amb.gpos, amb.gdir); |
463 |
|
scalecolor(acol, 1./AVGREFL); /* undo assumed reflectance */ |
464 |
+ |
if (i <= 0 || amb.rad[0] <= FTINY) /* no Hessian or zero radius */ |
465 |
+ |
return(i); |
466 |
|
/* store value */ |
467 |
|
VCOPY(amb.pos, r->rop); |
468 |
|
amb.ndir = encodedir(r->ron); |
469 |
< |
amb.udir = encodedir(uv[0]); |
469 |
> |
amb.udir = encodedir(uvw[0]); |
470 |
|
amb.lvl = al; |
471 |
|
copycolor(amb.val, acol); |
472 |
|
/* insert into tree */ |
473 |
|
avsave(&amb); /* and save to file */ |
474 |
< |
if (rn != r->ron) |
475 |
< |
extambient(acol, &amb, r->rop, rn); /* texture */ |
474 |
> |
if (rn != r->ron) { /* texture */ |
475 |
> |
VCOPY(uvw[2], r->ron); |
476 |
> |
extambient(acol, &amb, r->rop, rn, uvw); |
477 |
> |
} |
478 |
|
return(1); |
479 |
|
} |
480 |
|
|
483 |
|
extambient( /* extrapolate value at pv, nv */ |
484 |
|
COLOR cr, |
485 |
|
AMBVAL *ap, |
475 |
– |
FVECT uvw[3], |
486 |
|
FVECT pv, |
487 |
< |
FVECT nv |
487 |
> |
FVECT nv, |
488 |
> |
FVECT uvw[3] |
489 |
|
) |
490 |
|
{ |
491 |
< |
FVECT v1; |
492 |
< |
int i; |
493 |
< |
double d = 1.0; /* zeroeth order */ |
491 |
> |
static FVECT my_uvw[3]; |
492 |
> |
FVECT v1; |
493 |
> |
int i; |
494 |
> |
double d = 1.0; /* zeroeth order */ |
495 |
|
|
496 |
+ |
if (uvw == NULL) { /* need local coordinates? */ |
497 |
+ |
decodedir(my_uvw[2], ap->ndir); |
498 |
+ |
decodedir(my_uvw[0], ap->udir); |
499 |
+ |
VCROSS(my_uvw[1], my_uvw[2], my_uvw[0]); |
500 |
+ |
uvw = my_uvw; |
501 |
+ |
} |
502 |
|
for (i = 3; i--; ) /* gradient due to translation */ |
503 |
|
d += (pv[i] - ap->pos[i]) * |
504 |
|
(ap->gpos[0]*uvw[0][i] + ap->gpos[1]*uvw[1][i]); |
534 |
|
at = &atrunk; |
535 |
|
VCOPY(ck0, thescene.cuorg); |
536 |
|
s = thescene.cusize; |
537 |
< |
while (s*(OCTSCALE/2) > av->rad[1]*ambacc) { |
537 |
> |
while (s*(OCTSCALE/2) > av->rad[1]*qambacc) { |
538 |
|
if (at->kid == NULL) |
539 |
|
if ((at->kid = newambtree()) == NULL) |
540 |
|
error(SYSTEM, "out of memory in avinsert"); |
559 |
|
|
560 |
|
#else /* ! NEWAMB */ |
561 |
|
|
562 |
+ |
static double sumambient(COLOR acol, RAY *r, FVECT rn, int al, |
563 |
+ |
AMBTREE *at, FVECT c0, double s); |
564 |
+ |
static double makeambient(COLOR acol, RAY *r, FVECT rn, int al); |
565 |
+ |
static void extambient(COLOR cr, AMBVAL *ap, FVECT pv, FVECT nv); |
566 |
|
|
567 |
+ |
|
568 |
|
void |
569 |
|
multambient( /* compute ambient component & multiply by coef. */ |
570 |
|
COLOR aval, |
635 |
|
} |
636 |
|
|
637 |
|
|
638 |
< |
double |
638 |
> |
static double |
639 |
|
sumambient( /* get interpolated ambient value */ |
640 |
|
COLOR acol, |
641 |
|
RAY *r, |
746 |
|
} |
747 |
|
|
748 |
|
|
749 |
< |
double |
749 |
> |
static double |
750 |
|
makeambient( /* make a new ambient value for storage */ |
751 |
|
COLOR acol, |
752 |
|
RAY *r, |
786 |
|
} |
787 |
|
|
788 |
|
|
789 |
< |
void |
789 |
> |
static void |
790 |
|
extambient( /* extrapolate value at pv, nv */ |
791 |
|
COLOR cr, |
792 |
|
AMBVAL *ap, |