21 |
|
#include "ambient.h" |
22 |
|
#include "random.h" |
23 |
|
|
24 |
– |
#ifndef OLDAMB |
25 |
– |
|
24 |
|
extern void SDsquare2disk(double ds[2], double seedx, double seedy); |
25 |
|
|
26 |
|
typedef struct { |
27 |
|
COLOR v; /* hemisphere sample value */ |
28 |
< |
float d; /* reciprocal distance (1/rt) */ |
28 |
> |
float d; /* reciprocal distance */ |
29 |
|
FVECT p; /* intersection point */ |
30 |
|
} AMBSAMP; /* sample value */ |
31 |
|
|
129 |
|
dimlist[ndims++] = AI(hp,i,j) + 90171; |
130 |
|
rayvalue(&ar); /* evaluate ray */ |
131 |
|
ndims--; |
132 |
< |
if (ar.rt <= FTINY) |
132 |
> |
zd = raydistance(&ar); |
133 |
> |
if (zd <= FTINY) |
134 |
|
return(0); /* should never happen */ |
135 |
|
multcolor(ar.rcol, ar.rcoef); /* apply coefficient */ |
136 |
< |
if (ar.rt*ap->d < 1.0) /* new/closer distance? */ |
137 |
< |
ap->d = 1.0/ar.rt; |
136 |
> |
if (zd*ap->d < 1.0) /* new/closer distance? */ |
137 |
> |
ap->d = 1.0/zd; |
138 |
|
if (!n) { /* record first vertex & value */ |
139 |
< |
if (ar.rt > 10.0*thescene.cusize + 1000.) |
140 |
< |
ar.rt = 10.0*thescene.cusize + 1000.; |
141 |
< |
VSUM(ap->p, ar.rorg, ar.rdir, ar.rt); |
139 |
> |
if (zd > 10.0*thescene.cusize + 1000.) |
140 |
> |
zd = 10.0*thescene.cusize + 1000.; |
141 |
> |
VSUM(ap->p, ar.rorg, ar.rdir, zd); |
142 |
|
copycolor(ap->v, ar.rcol); |
143 |
|
} else { /* else update recorded value */ |
144 |
|
hp->acol[RED] -= colval(ap->v,RED); |
291 |
|
hp->sampOK *= -1; /* soft failure */ |
292 |
|
return(hp); |
293 |
|
} |
294 |
+ |
if (hp->sampOK < 64) |
295 |
+ |
return(hp); /* insufficient for super-sampling */ |
296 |
|
n = ambssamp*wt + 0.5; |
297 |
|
if (n > 8) { /* perform super-sampling? */ |
298 |
|
ambsupersamp(hp, n); |
776 |
|
return(1); |
777 |
|
} |
778 |
|
|
778 |
– |
|
779 |
– |
#else /* ! NEWAMB */ |
780 |
– |
|
781 |
– |
|
782 |
– |
void |
783 |
– |
inithemi( /* initialize sampling hemisphere */ |
784 |
– |
AMBHEMI *hp, |
785 |
– |
COLOR ac, |
786 |
– |
RAY *r, |
787 |
– |
double wt |
788 |
– |
) |
789 |
– |
{ |
790 |
– |
double d; |
791 |
– |
int i; |
792 |
– |
/* set number of divisions */ |
793 |
– |
if (ambacc <= FTINY && |
794 |
– |
wt > (d = 0.8*intens(ac)*r->rweight/(ambdiv*minweight))) |
795 |
– |
wt = d; /* avoid ray termination */ |
796 |
– |
hp->nt = sqrt(ambdiv * wt / PI) + 0.5; |
797 |
– |
i = ambacc > FTINY ? 3 : 1; /* minimum number of samples */ |
798 |
– |
if (hp->nt < i) |
799 |
– |
hp->nt = i; |
800 |
– |
hp->np = PI * hp->nt + 0.5; |
801 |
– |
/* set number of super-samples */ |
802 |
– |
hp->ns = ambssamp * wt + 0.5; |
803 |
– |
/* assign coefficient */ |
804 |
– |
copycolor(hp->acoef, ac); |
805 |
– |
d = 1.0/(hp->nt*hp->np); |
806 |
– |
scalecolor(hp->acoef, d); |
807 |
– |
/* make axes */ |
808 |
– |
VCOPY(hp->uz, r->ron); |
809 |
– |
hp->uy[0] = hp->uy[1] = hp->uy[2] = 0.0; |
810 |
– |
for (i = 0; i < 3; i++) |
811 |
– |
if (hp->uz[i] < 0.6 && hp->uz[i] > -0.6) |
812 |
– |
break; |
813 |
– |
if (i >= 3) |
814 |
– |
error(CONSISTENCY, "bad ray direction in inithemi"); |
815 |
– |
hp->uy[i] = 1.0; |
816 |
– |
fcross(hp->ux, hp->uy, hp->uz); |
817 |
– |
normalize(hp->ux); |
818 |
– |
fcross(hp->uy, hp->uz, hp->ux); |
819 |
– |
} |
820 |
– |
|
821 |
– |
|
822 |
– |
int |
823 |
– |
divsample( /* sample a division */ |
824 |
– |
AMBSAMP *dp, |
825 |
– |
AMBHEMI *h, |
826 |
– |
RAY *r |
827 |
– |
) |
828 |
– |
{ |
829 |
– |
RAY ar; |
830 |
– |
int hlist[3]; |
831 |
– |
double spt[2]; |
832 |
– |
double xd, yd, zd; |
833 |
– |
double b2; |
834 |
– |
double phi; |
835 |
– |
int i; |
836 |
– |
/* ambient coefficient for weight */ |
837 |
– |
if (ambacc > FTINY) |
838 |
– |
setcolor(ar.rcoef, AVGREFL, AVGREFL, AVGREFL); |
839 |
– |
else |
840 |
– |
copycolor(ar.rcoef, h->acoef); |
841 |
– |
if (rayorigin(&ar, AMBIENT, r, ar.rcoef) < 0) |
842 |
– |
return(-1); |
843 |
– |
if (ambacc > FTINY) { |
844 |
– |
multcolor(ar.rcoef, h->acoef); |
845 |
– |
scalecolor(ar.rcoef, 1./AVGREFL); |
846 |
– |
} |
847 |
– |
hlist[0] = r->rno; |
848 |
– |
hlist[1] = dp->t; |
849 |
– |
hlist[2] = dp->p; |
850 |
– |
multisamp(spt, 2, urand(ilhash(hlist,3)+dp->n)); |
851 |
– |
zd = sqrt((dp->t + spt[0])/h->nt); |
852 |
– |
phi = 2.0*PI * (dp->p + spt[1])/h->np; |
853 |
– |
xd = tcos(phi) * zd; |
854 |
– |
yd = tsin(phi) * zd; |
855 |
– |
zd = sqrt(1.0 - zd*zd); |
856 |
– |
for (i = 0; i < 3; i++) |
857 |
– |
ar.rdir[i] = xd*h->ux[i] + |
858 |
– |
yd*h->uy[i] + |
859 |
– |
zd*h->uz[i]; |
860 |
– |
checknorm(ar.rdir); |
861 |
– |
dimlist[ndims++] = dp->t*h->np + dp->p + 90171; |
862 |
– |
rayvalue(&ar); |
863 |
– |
ndims--; |
864 |
– |
multcolor(ar.rcol, ar.rcoef); /* apply coefficient */ |
865 |
– |
addcolor(dp->v, ar.rcol); |
866 |
– |
/* use rt to improve gradient calc */ |
867 |
– |
if (ar.rt > FTINY && ar.rt < FHUGE) |
868 |
– |
dp->r += 1.0/ar.rt; |
869 |
– |
/* (re)initialize error */ |
870 |
– |
if (dp->n++) { |
871 |
– |
b2 = bright(dp->v)/dp->n - bright(ar.rcol); |
872 |
– |
b2 = b2*b2 + dp->k*((dp->n-1)*(dp->n-1)); |
873 |
– |
dp->k = b2/(dp->n*dp->n); |
874 |
– |
} else |
875 |
– |
dp->k = 0.0; |
876 |
– |
return(0); |
877 |
– |
} |
878 |
– |
|
879 |
– |
|
880 |
– |
static int |
881 |
– |
ambcmp( /* decreasing order */ |
882 |
– |
const void *p1, |
883 |
– |
const void *p2 |
884 |
– |
) |
885 |
– |
{ |
886 |
– |
const AMBSAMP *d1 = (const AMBSAMP *)p1; |
887 |
– |
const AMBSAMP *d2 = (const AMBSAMP *)p2; |
888 |
– |
|
889 |
– |
if (d1->k < d2->k) |
890 |
– |
return(1); |
891 |
– |
if (d1->k > d2->k) |
892 |
– |
return(-1); |
893 |
– |
return(0); |
894 |
– |
} |
895 |
– |
|
896 |
– |
|
897 |
– |
static int |
898 |
– |
ambnorm( /* standard order */ |
899 |
– |
const void *p1, |
900 |
– |
const void *p2 |
901 |
– |
) |
902 |
– |
{ |
903 |
– |
const AMBSAMP *d1 = (const AMBSAMP *)p1; |
904 |
– |
const AMBSAMP *d2 = (const AMBSAMP *)p2; |
905 |
– |
int c; |
906 |
– |
|
907 |
– |
if ( (c = d1->t - d2->t) ) |
908 |
– |
return(c); |
909 |
– |
return(d1->p - d2->p); |
910 |
– |
} |
911 |
– |
|
912 |
– |
|
913 |
– |
double |
914 |
– |
doambient( /* compute ambient component */ |
915 |
– |
COLOR rcol, |
916 |
– |
RAY *r, |
917 |
– |
double wt, |
918 |
– |
FVECT pg, |
919 |
– |
FVECT dg |
920 |
– |
) |
921 |
– |
{ |
922 |
– |
double b, d=0; |
923 |
– |
AMBHEMI hemi; |
924 |
– |
AMBSAMP *div; |
925 |
– |
AMBSAMP dnew; |
926 |
– |
double acol[3]; |
927 |
– |
AMBSAMP *dp; |
928 |
– |
double arad; |
929 |
– |
int divcnt; |
930 |
– |
int i, j; |
931 |
– |
/* initialize hemisphere */ |
932 |
– |
inithemi(&hemi, rcol, r, wt); |
933 |
– |
divcnt = hemi.nt * hemi.np; |
934 |
– |
/* initialize */ |
935 |
– |
if (pg != NULL) |
936 |
– |
pg[0] = pg[1] = pg[2] = 0.0; |
937 |
– |
if (dg != NULL) |
938 |
– |
dg[0] = dg[1] = dg[2] = 0.0; |
939 |
– |
setcolor(rcol, 0.0, 0.0, 0.0); |
940 |
– |
if (divcnt == 0) |
941 |
– |
return(0.0); |
942 |
– |
/* allocate super-samples */ |
943 |
– |
if (hemi.ns > 0 || pg != NULL || dg != NULL) { |
944 |
– |
div = (AMBSAMP *)malloc(divcnt*sizeof(AMBSAMP)); |
945 |
– |
if (div == NULL) |
946 |
– |
error(SYSTEM, "out of memory in doambient"); |
947 |
– |
} else |
948 |
– |
div = NULL; |
949 |
– |
/* sample the divisions */ |
950 |
– |
arad = 0.0; |
951 |
– |
acol[0] = acol[1] = acol[2] = 0.0; |
952 |
– |
if ((dp = div) == NULL) |
953 |
– |
dp = &dnew; |
954 |
– |
divcnt = 0; |
955 |
– |
for (i = 0; i < hemi.nt; i++) |
956 |
– |
for (j = 0; j < hemi.np; j++) { |
957 |
– |
dp->t = i; dp->p = j; |
958 |
– |
setcolor(dp->v, 0.0, 0.0, 0.0); |
959 |
– |
dp->r = 0.0; |
960 |
– |
dp->n = 0; |
961 |
– |
if (divsample(dp, &hemi, r) < 0) { |
962 |
– |
if (div != NULL) |
963 |
– |
dp++; |
964 |
– |
continue; |
965 |
– |
} |
966 |
– |
arad += dp->r; |
967 |
– |
divcnt++; |
968 |
– |
if (div != NULL) |
969 |
– |
dp++; |
970 |
– |
else |
971 |
– |
addcolor(acol, dp->v); |
972 |
– |
} |
973 |
– |
if (!divcnt) { |
974 |
– |
if (div != NULL) |
975 |
– |
free((void *)div); |
976 |
– |
return(0.0); /* no samples taken */ |
977 |
– |
} |
978 |
– |
if (divcnt < hemi.nt*hemi.np) { |
979 |
– |
pg = dg = NULL; /* incomplete sampling */ |
980 |
– |
hemi.ns = 0; |
981 |
– |
} else if (arad > FTINY && divcnt/arad < minarad) { |
982 |
– |
hemi.ns = 0; /* close enough */ |
983 |
– |
} else if (hemi.ns > 0) { /* else perform super-sampling? */ |
984 |
– |
comperrs(div, &hemi); /* compute errors */ |
985 |
– |
qsort(div, divcnt, sizeof(AMBSAMP), ambcmp); /* sort divs */ |
986 |
– |
/* super-sample */ |
987 |
– |
for (i = hemi.ns; i > 0; i--) { |
988 |
– |
dnew = *div; |
989 |
– |
if (divsample(&dnew, &hemi, r) < 0) { |
990 |
– |
dp++; |
991 |
– |
continue; |
992 |
– |
} |
993 |
– |
dp = div; /* reinsert */ |
994 |
– |
j = divcnt < i ? divcnt : i; |
995 |
– |
while (--j > 0 && dnew.k < dp[1].k) { |
996 |
– |
*dp = *(dp+1); |
997 |
– |
dp++; |
998 |
– |
} |
999 |
– |
*dp = dnew; |
1000 |
– |
} |
1001 |
– |
if (pg != NULL || dg != NULL) /* restore order */ |
1002 |
– |
qsort(div, divcnt, sizeof(AMBSAMP), ambnorm); |
1003 |
– |
} |
1004 |
– |
/* compute returned values */ |
1005 |
– |
if (div != NULL) { |
1006 |
– |
arad = 0.0; /* note: divcnt may be < nt*np */ |
1007 |
– |
for (i = hemi.nt*hemi.np, dp = div; i-- > 0; dp++) { |
1008 |
– |
arad += dp->r; |
1009 |
– |
if (dp->n > 1) { |
1010 |
– |
b = 1.0/dp->n; |
1011 |
– |
scalecolor(dp->v, b); |
1012 |
– |
dp->r *= b; |
1013 |
– |
dp->n = 1; |
1014 |
– |
} |
1015 |
– |
addcolor(acol, dp->v); |
1016 |
– |
} |
1017 |
– |
b = bright(acol); |
1018 |
– |
if (b > FTINY) { |
1019 |
– |
b = 1.0/b; /* compute & normalize gradient(s) */ |
1020 |
– |
if (pg != NULL) { |
1021 |
– |
posgradient(pg, div, &hemi); |
1022 |
– |
for (i = 0; i < 3; i++) |
1023 |
– |
pg[i] *= b; |
1024 |
– |
} |
1025 |
– |
if (dg != NULL) { |
1026 |
– |
dirgradient(dg, div, &hemi); |
1027 |
– |
for (i = 0; i < 3; i++) |
1028 |
– |
dg[i] *= b; |
1029 |
– |
} |
1030 |
– |
} |
1031 |
– |
free((void *)div); |
1032 |
– |
} |
1033 |
– |
copycolor(rcol, acol); |
1034 |
– |
if (arad <= FTINY) |
1035 |
– |
arad = maxarad; |
1036 |
– |
else |
1037 |
– |
arad = (divcnt+hemi.ns)/arad; |
1038 |
– |
if (pg != NULL) { /* reduce radius if gradient large */ |
1039 |
– |
d = DOT(pg,pg); |
1040 |
– |
if (d*arad*arad > 1.0) |
1041 |
– |
arad = 1.0/sqrt(d); |
1042 |
– |
} |
1043 |
– |
if (arad < minarad) { |
1044 |
– |
arad = minarad; |
1045 |
– |
if (pg != NULL && d*arad*arad > 1.0) { /* cap gradient */ |
1046 |
– |
d = 1.0/arad/sqrt(d); |
1047 |
– |
for (i = 0; i < 3; i++) |
1048 |
– |
pg[i] *= d; |
1049 |
– |
} |
1050 |
– |
} |
1051 |
– |
if ((arad /= sqrt(wt)) > maxarad) |
1052 |
– |
arad = maxarad; |
1053 |
– |
return(arad); |
1054 |
– |
} |
1055 |
– |
|
1056 |
– |
|
1057 |
– |
void |
1058 |
– |
comperrs( /* compute initial error estimates */ |
1059 |
– |
AMBSAMP *da, /* assumes standard ordering */ |
1060 |
– |
AMBHEMI *hp |
1061 |
– |
) |
1062 |
– |
{ |
1063 |
– |
double b, b2; |
1064 |
– |
int i, j; |
1065 |
– |
AMBSAMP *dp; |
1066 |
– |
/* sum differences from neighbors */ |
1067 |
– |
dp = da; |
1068 |
– |
for (i = 0; i < hp->nt; i++) |
1069 |
– |
for (j = 0; j < hp->np; j++) { |
1070 |
– |
#ifdef DEBUG |
1071 |
– |
if (dp->t != i || dp->p != j) |
1072 |
– |
error(CONSISTENCY, |
1073 |
– |
"division order in comperrs"); |
1074 |
– |
#endif |
1075 |
– |
b = bright(dp[0].v); |
1076 |
– |
if (i > 0) { /* from above */ |
1077 |
– |
b2 = bright(dp[-hp->np].v) - b; |
1078 |
– |
b2 *= b2 * 0.25; |
1079 |
– |
dp[0].k += b2; |
1080 |
– |
dp[-hp->np].k += b2; |
1081 |
– |
} |
1082 |
– |
if (j > 0) { /* from behind */ |
1083 |
– |
b2 = bright(dp[-1].v) - b; |
1084 |
– |
b2 *= b2 * 0.25; |
1085 |
– |
dp[0].k += b2; |
1086 |
– |
dp[-1].k += b2; |
1087 |
– |
} else { /* around */ |
1088 |
– |
b2 = bright(dp[hp->np-1].v) - b; |
1089 |
– |
b2 *= b2 * 0.25; |
1090 |
– |
dp[0].k += b2; |
1091 |
– |
dp[hp->np-1].k += b2; |
1092 |
– |
} |
1093 |
– |
dp++; |
1094 |
– |
} |
1095 |
– |
/* divide by number of neighbors */ |
1096 |
– |
dp = da; |
1097 |
– |
for (j = 0; j < hp->np; j++) /* top row */ |
1098 |
– |
(dp++)->k *= 1.0/3.0; |
1099 |
– |
if (hp->nt < 2) |
1100 |
– |
return; |
1101 |
– |
for (i = 1; i < hp->nt-1; i++) /* central region */ |
1102 |
– |
for (j = 0; j < hp->np; j++) |
1103 |
– |
(dp++)->k *= 0.25; |
1104 |
– |
for (j = 0; j < hp->np; j++) /* bottom row */ |
1105 |
– |
(dp++)->k *= 1.0/3.0; |
1106 |
– |
} |
1107 |
– |
|
1108 |
– |
|
1109 |
– |
void |
1110 |
– |
posgradient( /* compute position gradient */ |
1111 |
– |
FVECT gv, |
1112 |
– |
AMBSAMP *da, /* assumes standard ordering */ |
1113 |
– |
AMBHEMI *hp |
1114 |
– |
) |
1115 |
– |
{ |
1116 |
– |
int i, j; |
1117 |
– |
double nextsine, lastsine, b, d; |
1118 |
– |
double mag0, mag1; |
1119 |
– |
double phi, cosp, sinp, xd, yd; |
1120 |
– |
AMBSAMP *dp; |
1121 |
– |
|
1122 |
– |
xd = yd = 0.0; |
1123 |
– |
for (j = 0; j < hp->np; j++) { |
1124 |
– |
dp = da + j; |
1125 |
– |
mag0 = mag1 = 0.0; |
1126 |
– |
lastsine = 0.0; |
1127 |
– |
for (i = 0; i < hp->nt; i++) { |
1128 |
– |
#ifdef DEBUG |
1129 |
– |
if (dp->t != i || dp->p != j) |
1130 |
– |
error(CONSISTENCY, |
1131 |
– |
"division order in posgradient"); |
1132 |
– |
#endif |
1133 |
– |
b = bright(dp->v); |
1134 |
– |
if (i > 0) { |
1135 |
– |
d = dp[-hp->np].r; |
1136 |
– |
if (dp[0].r > d) d = dp[0].r; |
1137 |
– |
/* sin(t)*cos(t)^2 */ |
1138 |
– |
d *= lastsine * (1.0 - (double)i/hp->nt); |
1139 |
– |
mag0 += d*(b - bright(dp[-hp->np].v)); |
1140 |
– |
} |
1141 |
– |
nextsine = sqrt((double)(i+1)/hp->nt); |
1142 |
– |
if (j > 0) { |
1143 |
– |
d = dp[-1].r; |
1144 |
– |
if (dp[0].r > d) d = dp[0].r; |
1145 |
– |
mag1 += d * (nextsine - lastsine) * |
1146 |
– |
(b - bright(dp[-1].v)); |
1147 |
– |
} else { |
1148 |
– |
d = dp[hp->np-1].r; |
1149 |
– |
if (dp[0].r > d) d = dp[0].r; |
1150 |
– |
mag1 += d * (nextsine - lastsine) * |
1151 |
– |
(b - bright(dp[hp->np-1].v)); |
1152 |
– |
} |
1153 |
– |
dp += hp->np; |
1154 |
– |
lastsine = nextsine; |
1155 |
– |
} |
1156 |
– |
mag0 *= 2.0*PI / hp->np; |
1157 |
– |
phi = 2.0*PI * (double)j/hp->np; |
1158 |
– |
cosp = tcos(phi); sinp = tsin(phi); |
1159 |
– |
xd += mag0*cosp - mag1*sinp; |
1160 |
– |
yd += mag0*sinp + mag1*cosp; |
1161 |
– |
} |
1162 |
– |
for (i = 0; i < 3; i++) |
1163 |
– |
gv[i] = (xd*hp->ux[i] + yd*hp->uy[i])*(hp->nt*hp->np)/PI; |
1164 |
– |
} |
1165 |
– |
|
1166 |
– |
|
1167 |
– |
void |
1168 |
– |
dirgradient( /* compute direction gradient */ |
1169 |
– |
FVECT gv, |
1170 |
– |
AMBSAMP *da, /* assumes standard ordering */ |
1171 |
– |
AMBHEMI *hp |
1172 |
– |
) |
1173 |
– |
{ |
1174 |
– |
int i, j; |
1175 |
– |
double mag; |
1176 |
– |
double phi, xd, yd; |
1177 |
– |
AMBSAMP *dp; |
1178 |
– |
|
1179 |
– |
xd = yd = 0.0; |
1180 |
– |
for (j = 0; j < hp->np; j++) { |
1181 |
– |
dp = da + j; |
1182 |
– |
mag = 0.0; |
1183 |
– |
for (i = 0; i < hp->nt; i++) { |
1184 |
– |
#ifdef DEBUG |
1185 |
– |
if (dp->t != i || dp->p != j) |
1186 |
– |
error(CONSISTENCY, |
1187 |
– |
"division order in dirgradient"); |
1188 |
– |
#endif |
1189 |
– |
/* tan(t) */ |
1190 |
– |
mag += bright(dp->v)/sqrt(hp->nt/(i+.5) - 1.0); |
1191 |
– |
dp += hp->np; |
1192 |
– |
} |
1193 |
– |
phi = 2.0*PI * (j+.5)/hp->np + PI/2.0; |
1194 |
– |
xd += mag * tcos(phi); |
1195 |
– |
yd += mag * tsin(phi); |
1196 |
– |
} |
1197 |
– |
for (i = 0; i < 3; i++) |
1198 |
– |
gv[i] = xd*hp->ux[i] + yd*hp->uy[i]; |
1199 |
– |
} |
1200 |
– |
|
1201 |
– |
#endif /* ! NEWAMB */ |