--- ray/src/rt/ambcomp.c 1992/02/19 12:00:04 2.2 +++ ray/src/rt/ambcomp.c 1998/12/16 18:14:57 2.8 @@ -1,7 +1,7 @@ -/* Copyright (c) 1991 Regents of the University of California */ +/* Copyright (c) 1998 Silicon Graphics, Inc. */ #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static char SCCSid[] = "$SunId$ SGI"; #endif /* @@ -27,9 +27,7 @@ typedef struct { short nt, np; /* number of theta and phi directions */ } AMBHEMI; /* ambient sample hemisphere */ -extern double sin(), cos(), sqrt(); - static int ambcmp(d1, d2) /* decreasing order */ AMBSAMP *d1, *d2; @@ -75,8 +73,8 @@ RAY *r; multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n)); zd = sqrt((dp->t + spt[0])/h->nt); phi = 2.0*PI * (dp->p + spt[1])/h->np; - xd = cos(phi) * zd; - yd = sin(phi) * zd; + xd = tcos(phi) * zd; + yd = tsin(phi) * zd; zd = sqrt(1.0 - zd*zd); for (i = 0; i < 3; i++) ar.rdir[i] = xd*h->ux[i] + @@ -86,8 +84,9 @@ RAY *r; rayvalue(&ar); ndims--; addcolor(dp->v, ar.rcol); - if (ar.rt > FTINY && ar.rt < FHUGE) - dp->r += 1.0/ar.rt; + /* be conservative and use rot */ + if (ar.rot > FTINY && ar.rot < FHUGE) + dp->r += 1.0/ar.rot; /* (re)initialize error */ if (dp->n++) { b2 = bright(dp->v)/dp->n - bright(ar.rcol); @@ -141,14 +140,15 @@ FVECT pg, dg; dp->n = 0; if (divsample(dp, &hemi, r) < 0) goto oopsy; + arad += dp->r; if (div != NULL) dp++; - else { + else addcolor(acol, dp->v); - arad += dp->r; - } } - if (ns > 0) { /* perform super-sampling */ + if (ns > 0 && arad > FTINY && ndivs/arad < minarad) + ns = 0; /* close enough */ + else if (ns > 0) { /* else perform super-sampling */ comperrs(div, &hemi); /* compute errors */ qsort(div, ndivs, sizeof(AMBSAMP), ambcmp); /* sort divs */ /* super-sample */ @@ -170,6 +170,7 @@ FVECT pg, dg; } /* compute returned values */ if (div != NULL) { + arad = 0.0; for (i = ndivs, dp = div; i-- > 0; dp++) { arad += dp->r; if (dp->n > 1) { @@ -207,11 +208,8 @@ FVECT pg, dg; scalecolor(acol, b); if (arad <= FTINY) arad = maxarad; - else { + else arad = (ndivs+ns)/arad; - if (arad > maxarad) - arad = maxarad; - } if (pg != NULL) { /* reduce radius if gradient large */ d = DOT(pg,pg); if (d*arad*arad > 1.0) @@ -225,7 +223,9 @@ FVECT pg, dg; pg[i] *= d; } } - return(arad/sqrt(wt)); + if ((arad /= sqrt(wt)) > maxarad) + arad = maxarad; + return(arad); oopsy: if (div != NULL) free((char *)div); @@ -358,7 +358,7 @@ register AMBHEMI *hp; } mag0 *= 2.0*PI / hp->np; phi = 2.0*PI * (double)j/hp->np; - cosp = cos(phi); sinp = sin(phi); + cosp = tcos(phi); sinp = tsin(phi); xd += mag0*cosp - mag1*sinp; yd += mag0*sinp + mag1*cosp; } @@ -392,8 +392,8 @@ register AMBHEMI *hp; dp += hp->np; } phi = 2.0*PI * (j+.5)/hp->np + PI/2.0; - xd += mag * cos(phi); - yd += mag * sin(phi); + xd += mag * tcos(phi); + yd += mag * tsin(phi); } for (i = 0; i < 3; i++) gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])/(hp->nt*hp->np);