| 1 |
– |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 4 |
|
/* |
| 5 |
|
* Routines to compute "ambient" values using Monte Carlo |
| 6 |
+ |
* |
| 7 |
+ |
* Declarations of external symbols in ambient.h |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
+ |
#include "copyright.h" |
| 11 |
+ |
|
| 12 |
|
#include "ray.h" |
| 13 |
|
|
| 14 |
|
#include "ambient.h" |
| 15 |
|
|
| 16 |
|
#include "random.h" |
| 17 |
|
|
| 17 |
– |
typedef struct { |
| 18 |
– |
short t, p; /* theta, phi indices */ |
| 19 |
– |
COLOR v; /* value sum */ |
| 20 |
– |
float r; /* 1/distance sum */ |
| 21 |
– |
float k; /* variance for this division */ |
| 22 |
– |
int n; /* number of subsamples */ |
| 23 |
– |
} AMBSAMP; /* ambient sample division */ |
| 18 |
|
|
| 25 |
– |
typedef struct { |
| 26 |
– |
FVECT ux, uy, uz; /* x, y and z axis directions */ |
| 27 |
– |
short nt, np; /* number of theta and phi directions */ |
| 28 |
– |
} AMBHEMI; /* ambient sample hemisphere */ |
| 29 |
– |
|
| 30 |
– |
|
| 19 |
|
static int |
| 20 |
|
ambcmp(d1, d2) /* decreasing order */ |
| 21 |
|
AMBSAMP *d1, *d2; |
| 34 |
|
{ |
| 35 |
|
register int c; |
| 36 |
|
|
| 37 |
< |
if (c = d1->t - d2->t) |
| 37 |
> |
if ( (c = d1->t - d2->t) ) |
| 38 |
|
return(c); |
| 39 |
|
return(d1->p - d2->p); |
| 40 |
|
} |
| 41 |
|
|
| 42 |
|
|
| 43 |
+ |
int |
| 44 |
|
divsample(dp, h, r) /* sample a division */ |
| 45 |
|
register AMBSAMP *dp; |
| 46 |
|
AMBHEMI *h; |
| 73 |
|
rayvalue(&ar); |
| 74 |
|
ndims--; |
| 75 |
|
addcolor(dp->v, ar.rcol); |
| 76 |
< |
/* be conservative and use rot */ |
| 77 |
< |
if (ar.rot > FTINY && ar.rot < FHUGE) |
| 78 |
< |
dp->r += 1.0/ar.rot; |
| 76 |
> |
/* use rt to improve gradient calc */ |
| 77 |
> |
if (ar.rt > FTINY && ar.rt < FHUGE) |
| 78 |
> |
dp->r += 1.0/ar.rt; |
| 79 |
|
/* (re)initialize error */ |
| 80 |
|
if (dp->n++) { |
| 81 |
|
b2 = bright(dp->v)/dp->n - bright(ar.rcol); |
| 142 |
|
qsort(div, ndivs, sizeof(AMBSAMP), ambcmp); /* sort divs */ |
| 143 |
|
/* super-sample */ |
| 144 |
|
for (i = ns; i > 0; i--) { |
| 145 |
< |
copystruct(&dnew, div); |
| 145 |
> |
dnew = *div; |
| 146 |
|
if (divsample(&dnew, &hemi, r) < 0) |
| 147 |
|
goto oopsy; |
| 148 |
|
/* reinsert */ |
| 149 |
|
dp = div; |
| 150 |
|
j = ndivs < i ? ndivs : i; |
| 151 |
|
while (--j > 0 && dnew.k < dp[1].k) { |
| 152 |
< |
copystruct(dp, dp+1); |
| 152 |
> |
*dp = *(dp+1); |
| 153 |
|
dp++; |
| 154 |
|
} |
| 155 |
< |
copystruct(dp, &dnew); |
| 155 |
> |
*dp = dnew; |
| 156 |
|
} |
| 157 |
|
if (pg != NULL || dg != NULL) /* restore order */ |
| 158 |
|
qsort(div, ndivs, sizeof(AMBSAMP), ambnorm); |
| 191 |
|
for (i = 0; i < 3; i++) |
| 192 |
|
dg[i] = 0.0; |
| 193 |
|
} |
| 194 |
< |
free((char *)div); |
| 194 |
> |
free((void *)div); |
| 195 |
|
} |
| 196 |
|
b = 1.0/ndivs; |
| 197 |
|
scalecolor(acol, b); |
| 217 |
|
return(arad); |
| 218 |
|
oopsy: |
| 219 |
|
if (div != NULL) |
| 220 |
< |
free((char *)div); |
| 220 |
> |
free((void *)div); |
| 221 |
|
return(0.0); |
| 222 |
|
} |
| 223 |
|
|
| 224 |
|
|
| 225 |
+ |
void |
| 226 |
|
inithemi(hp, r, wt) /* initialize sampling hemisphere */ |
| 227 |
|
register AMBHEMI *hp; |
| 228 |
|
RAY *r; |
| 251 |
|
} |
| 252 |
|
|
| 253 |
|
|
| 254 |
+ |
void |
| 255 |
|
comperrs(da, hp) /* compute initial error estimates */ |
| 256 |
|
AMBSAMP *da; /* assumes standard ordering */ |
| 257 |
|
register AMBHEMI *hp; |
| 302 |
|
} |
| 303 |
|
|
| 304 |
|
|
| 305 |
+ |
void |
| 306 |
|
posgradient(gv, da, hp) /* compute position gradient */ |
| 307 |
|
FVECT gv; |
| 308 |
|
AMBSAMP *da; /* assumes standard ordering */ |
| 359 |
|
} |
| 360 |
|
|
| 361 |
|
|
| 362 |
+ |
void |
| 363 |
|
dirgradient(gv, da, hp) /* compute direction gradient */ |
| 364 |
|
FVECT gv; |
| 365 |
|
AMBSAMP *da; /* assumes standard ordering */ |