--- ray/src/rt/ambcomp.c 1998/12/16 18:14:57 2.8 +++ ray/src/rt/ambcomp.c 2005/04/13 23:00:59 2.13 @@ -1,33 +1,21 @@ -/* Copyright (c) 1998 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: ambcomp.c,v 2.13 2005/04/13 23:00:59 greg Exp $"; #endif - /* * Routines to compute "ambient" values using Monte Carlo + * + * Declarations of external symbols in ambient.h */ +#include "copyright.h" + #include "ray.h" #include "ambient.h" #include "random.h" -typedef struct { - short t, p; /* theta, phi indices */ - COLOR v; /* value sum */ - float r; /* 1/distance sum */ - float k; /* variance for this division */ - int n; /* number of subsamples */ -} AMBSAMP; /* ambient sample division */ -typedef struct { - FVECT ux, uy, uz; /* x, y and z axis directions */ - short nt, np; /* number of theta and phi directions */ -} AMBHEMI; /* ambient sample hemisphere */ - - static int ambcmp(d1, d2) /* decreasing order */ AMBSAMP *d1, *d2; @@ -46,12 +34,13 @@ AMBSAMP *d1, *d2; { register int c; - if (c = d1->t - d2->t) + if ( (c = d1->t - d2->t) ) return(c); return(d1->p - d2->p); } +int divsample(dp, h, r) /* sample a division */ register AMBSAMP *dp; AMBHEMI *h; @@ -84,9 +73,9 @@ RAY *r; rayvalue(&ar); ndims--; addcolor(dp->v, ar.rcol); - /* be conservative and use rot */ - if (ar.rot > FTINY && ar.rot < FHUGE) - dp->r += 1.0/ar.rot; + /* use rt to improve gradient calc */ + if (ar.rt > FTINY && ar.rt < FHUGE) + dp->r += 1.0/ar.rt; /* (re)initialize error */ if (dp->n++) { b2 = bright(dp->v)/dp->n - bright(ar.rcol); @@ -153,17 +142,17 @@ FVECT pg, dg; qsort(div, ndivs, sizeof(AMBSAMP), ambcmp); /* sort divs */ /* super-sample */ for (i = ns; i > 0; i--) { - copystruct(&dnew, div); + dnew = *div; if (divsample(&dnew, &hemi, r) < 0) goto oopsy; /* reinsert */ dp = div; j = ndivs < i ? ndivs : i; while (--j > 0 && dnew.k < dp[1].k) { - copystruct(dp, dp+1); + *dp = *(dp+1); dp++; } - copystruct(dp, &dnew); + *dp = dnew; } if (pg != NULL || dg != NULL) /* restore order */ qsort(div, ndivs, sizeof(AMBSAMP), ambnorm); @@ -202,7 +191,7 @@ FVECT pg, dg; for (i = 0; i < 3; i++) dg[i] = 0.0; } - free((char *)div); + free((void *)div); } b = 1.0/ndivs; scalecolor(acol, b); @@ -228,11 +217,12 @@ FVECT pg, dg; return(arad); oopsy: if (div != NULL) - free((char *)div); + free((void *)div); return(0.0); } +void inithemi(hp, r, wt) /* initialize sampling hemisphere */ register AMBHEMI *hp; RAY *r; @@ -240,11 +230,10 @@ double wt; { register int i; /* set number of divisions */ - if (wt < (.25*PI)/ambdiv+FTINY) { - hp->nt = hp->np = 0; - return; /* zero samples */ - } hp->nt = sqrt(ambdiv * wt / PI) + 0.5; + i = ambacc > FTINY ? 3 : 1; /* minimum number of samples */ + if (hp->nt < i) + hp->nt = i; hp->np = PI * hp->nt + 0.5; /* make axes */ VCOPY(hp->uz, r->ron); @@ -261,6 +250,7 @@ double wt; } +void comperrs(da, hp) /* compute initial error estimates */ AMBSAMP *da; /* assumes standard ordering */ register AMBHEMI *hp; @@ -311,6 +301,7 @@ register AMBHEMI *hp; } +void posgradient(gv, da, hp) /* compute position gradient */ FVECT gv; AMBSAMP *da; /* assumes standard ordering */ @@ -367,6 +358,7 @@ register AMBHEMI *hp; } +void dirgradient(gv, da, hp) /* compute direction gradient */ FVECT gv; AMBSAMP *da; /* assumes standard ordering */