| 1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 4 |
|
/* |
| 5 |
|
* Routines to do the actual calculation for mkillum |
| 6 |
|
*/ |
| 7 |
|
|
| 8 |
< |
#include "mkillum.h" |
| 8 |
> |
#include <string.h> |
| 9 |
|
|
| 10 |
+ |
#include "mkillum.h" |
| 11 |
|
#include "face.h" |
| 14 |
– |
|
| 12 |
|
#include "cone.h" |
| 13 |
+ |
#include "source.h" |
| 14 |
|
|
| 17 |
– |
#include "random.h" |
| 15 |
|
|
| 16 |
+ |
COLORV * distarr = NULL; /* distribution array */ |
| 17 |
+ |
int distsiz = 0; |
| 18 |
+ |
COLORV * direct_discount = NULL; /* amount to take off direct */ |
| 19 |
|
|
| 20 |
< |
o_default(ob, il, rt, nm) /* default illum action */ |
| 21 |
< |
OBJREC *ob; |
| 22 |
< |
struct illum_args *il; |
| 23 |
< |
struct rtproc *rt; |
| 24 |
< |
char *nm; |
| 20 |
> |
void |
| 21 |
> |
newdist( /* allocate & clear distribution array */ |
| 22 |
> |
int siz |
| 23 |
> |
) |
| 24 |
|
{ |
| 25 |
+ |
if (siz <= 0) { |
| 26 |
+ |
if (distsiz > 0) |
| 27 |
+ |
free((void *)distarr); |
| 28 |
+ |
distarr = NULL; |
| 29 |
+ |
distsiz = 0; |
| 30 |
+ |
return; |
| 31 |
+ |
} |
| 32 |
+ |
if (distsiz < siz) { |
| 33 |
+ |
if (distsiz > 0) |
| 34 |
+ |
free((void *)distarr); |
| 35 |
+ |
distarr = (COLORV *)malloc(sizeof(COLOR)*siz); |
| 36 |
+ |
if (distarr == NULL) |
| 37 |
+ |
error(SYSTEM, "out of memory in newdist"); |
| 38 |
+ |
distsiz = siz; |
| 39 |
+ |
} |
| 40 |
+ |
memset(distarr, '\0', sizeof(COLOR)*siz); |
| 41 |
+ |
} |
| 42 |
+ |
|
| 43 |
+ |
|
| 44 |
+ |
static void |
| 45 |
+ |
new_discount() /* allocate space for direct contrib. record */ |
| 46 |
+ |
{ |
| 47 |
+ |
if (distsiz <= 0) |
| 48 |
+ |
return; |
| 49 |
+ |
direct_discount = (COLORV *)calloc(distsiz, sizeof(COLOR)); |
| 50 |
+ |
if (direct_discount == NULL) |
| 51 |
+ |
error(SYSTEM, "out of memory in new_discount"); |
| 52 |
+ |
} |
| 53 |
+ |
|
| 54 |
+ |
|
| 55 |
+ |
static void |
| 56 |
+ |
done_discount() /* clear off direct contrib. record */ |
| 57 |
+ |
{ |
| 58 |
+ |
if (direct_discount == NULL) |
| 59 |
+ |
return; |
| 60 |
+ |
free((void *)direct_discount); |
| 61 |
+ |
direct_discount = NULL; |
| 62 |
+ |
} |
| 63 |
+ |
|
| 64 |
+ |
|
| 65 |
+ |
int |
| 66 |
+ |
process_ray( /* process a ray result or report error */ |
| 67 |
+ |
RAY *r, |
| 68 |
+ |
int rv |
| 69 |
+ |
) |
| 70 |
+ |
{ |
| 71 |
+ |
COLORV *colp; |
| 72 |
+ |
|
| 73 |
+ |
if (rv == 0) /* no result ready */ |
| 74 |
+ |
return(0); |
| 75 |
+ |
if (rv < 0) |
| 76 |
+ |
error(USER, "ray tracing process died"); |
| 77 |
+ |
if (r->rno >= distsiz) |
| 78 |
+ |
error(INTERNAL, "bad returned index in process_ray"); |
| 79 |
+ |
multcolor(r->rcol, r->rcoef); /* in case it's a source ray */ |
| 80 |
+ |
colp = &distarr[r->rno * 3]; |
| 81 |
+ |
addcolor(colp, r->rcol); |
| 82 |
+ |
if (r->rsrc >= 0 && /* remember source contrib. */ |
| 83 |
+ |
direct_discount != NULL) { |
| 84 |
+ |
colp = &direct_discount[r->rno * 3]; |
| 85 |
+ |
addcolor(colp, r->rcol); |
| 86 |
+ |
} |
| 87 |
+ |
return(1); |
| 88 |
+ |
} |
| 89 |
+ |
|
| 90 |
+ |
|
| 91 |
+ |
void |
| 92 |
+ |
raysamp( /* queue a ray sample */ |
| 93 |
+ |
int ndx, |
| 94 |
+ |
FVECT org, |
| 95 |
+ |
FVECT dir |
| 96 |
+ |
) |
| 97 |
+ |
{ |
| 98 |
+ |
RAY myRay; |
| 99 |
+ |
int rv; |
| 100 |
+ |
|
| 101 |
+ |
if ((ndx < 0) | (ndx >= distsiz)) |
| 102 |
+ |
error(INTERNAL, "bad index in raysamp"); |
| 103 |
+ |
VCOPY(myRay.rorg, org); |
| 104 |
+ |
VCOPY(myRay.rdir, dir); |
| 105 |
+ |
myRay.rmax = .0; |
| 106 |
+ |
rayorigin(&myRay, PRIMARY, NULL, NULL); |
| 107 |
+ |
myRay.rno = ndx; |
| 108 |
+ |
/* queue ray, check result */ |
| 109 |
+ |
process_ray(&myRay, ray_pqueue(&myRay)); |
| 110 |
+ |
} |
| 111 |
+ |
|
| 112 |
+ |
|
| 113 |
+ |
void |
| 114 |
+ |
srcsamps( /* sample sources from this surface position */ |
| 115 |
+ |
struct illum_args *il, |
| 116 |
+ |
FVECT org, |
| 117 |
+ |
FVECT nrm, |
| 118 |
+ |
MAT4 ixfm |
| 119 |
+ |
) |
| 120 |
+ |
{ |
| 121 |
+ |
int nalt, nazi; |
| 122 |
+ |
SRCINDEX si; |
| 123 |
+ |
RAY sr; |
| 124 |
+ |
FVECT v; |
| 125 |
+ |
double d; |
| 126 |
+ |
int i, j; |
| 127 |
+ |
/* get sampling density */ |
| 128 |
+ |
if (il->sampdens <= 0) { |
| 129 |
+ |
nalt = nazi = 1; |
| 130 |
+ |
} else { |
| 131 |
+ |
i = PI * il->sampdens; |
| 132 |
+ |
nalt = sqrt(i/PI) + .5; |
| 133 |
+ |
nazi = PI*nalt + .5; |
| 134 |
+ |
} |
| 135 |
+ |
initsrcindex(&si); /* loop over (sub)sources */ |
| 136 |
+ |
for ( ; ; ) { |
| 137 |
+ |
VCOPY(sr.rorg, org); /* pick side to shoot from */ |
| 138 |
+ |
if (il->sd != NULL) { |
| 139 |
+ |
int sn = si.sn; |
| 140 |
+ |
if (si.sp+1 >= si.np) ++sn; |
| 141 |
+ |
if (sn >= nsources) break; |
| 142 |
+ |
if (source[sn].sflags & SDISTANT) |
| 143 |
+ |
d = DOT(source[sn].sloc, nrm); |
| 144 |
+ |
else { |
| 145 |
+ |
VSUB(v, source[sn].sloc, org); |
| 146 |
+ |
d = DOT(v, nrm); |
| 147 |
+ |
} |
| 148 |
+ |
} else |
| 149 |
+ |
d = 1.0; /* only transmission */ |
| 150 |
+ |
if (d < 0.0) |
| 151 |
+ |
d = -1.0001*il->thick - 5.*FTINY; |
| 152 |
+ |
else |
| 153 |
+ |
d = 5.*FTINY; |
| 154 |
+ |
for (i = 3; i--; ) |
| 155 |
+ |
sr.rorg[i] += d*nrm[i]; |
| 156 |
+ |
samplendx++; /* increment sample counter */ |
| 157 |
+ |
if (!srcray(&sr, NULL, &si)) |
| 158 |
+ |
break; /* end of sources */ |
| 159 |
+ |
/* index direction */ |
| 160 |
+ |
if (ixfm != NULL) |
| 161 |
+ |
multv3(v, sr.rdir, ixfm); |
| 162 |
+ |
else |
| 163 |
+ |
VCOPY(v, sr.rdir); |
| 164 |
+ |
if (il->sd != NULL) { |
| 165 |
+ |
i = getBSDF_incndx(il->sd, v); |
| 166 |
+ |
if (i < 0) |
| 167 |
+ |
continue; /* must not be important */ |
| 168 |
+ |
sr.rno = i; |
| 169 |
+ |
d = 1.0/getBSDF_incohm(il->sd, i); |
| 170 |
+ |
} else { |
| 171 |
+ |
if (v[2] >= -FTINY) |
| 172 |
+ |
continue; /* only sample transmission */ |
| 173 |
+ |
v[0] = -v[0]; v[1] = -v[1]; v[2] = -v[2]; |
| 174 |
+ |
sr.rno = flatindex(v, nalt, nazi); |
| 175 |
+ |
d = nalt*nazi*(1./PI) * v[2]; |
| 176 |
+ |
} |
| 177 |
+ |
d *= si.dom; /* solid angle correction */ |
| 178 |
+ |
scalecolor(sr.rcoef, d); |
| 179 |
+ |
process_ray(&sr, ray_pqueue(&sr)); |
| 180 |
+ |
} |
| 181 |
+ |
} |
| 182 |
+ |
|
| 183 |
+ |
|
| 184 |
+ |
void |
| 185 |
+ |
rayclean() /* finish all pending rays */ |
| 186 |
+ |
{ |
| 187 |
+ |
RAY myRay; |
| 188 |
+ |
|
| 189 |
+ |
while (process_ray(&myRay, ray_presult(&myRay, 0))) |
| 190 |
+ |
; |
| 191 |
+ |
} |
| 192 |
+ |
|
| 193 |
+ |
|
| 194 |
+ |
static void |
| 195 |
+ |
mkaxes( /* compute u and v to go with n */ |
| 196 |
+ |
FVECT u, |
| 197 |
+ |
FVECT v, |
| 198 |
+ |
FVECT n |
| 199 |
+ |
) |
| 200 |
+ |
{ |
| 201 |
+ |
register int i; |
| 202 |
+ |
|
| 203 |
+ |
v[0] = v[1] = v[2] = 0.0; |
| 204 |
+ |
for (i = 0; i < 3; i++) |
| 205 |
+ |
if (n[i] < 0.6 && n[i] > -0.6) |
| 206 |
+ |
break; |
| 207 |
+ |
v[i] = 1.0; |
| 208 |
+ |
fcross(u, v, n); |
| 209 |
+ |
normalize(u); |
| 210 |
+ |
fcross(v, n, u); |
| 211 |
+ |
} |
| 212 |
+ |
|
| 213 |
+ |
|
| 214 |
+ |
static void |
| 215 |
+ |
rounddir( /* compute uniform spherical direction */ |
| 216 |
+ |
register FVECT dv, |
| 217 |
+ |
double alt, |
| 218 |
+ |
double azi |
| 219 |
+ |
) |
| 220 |
+ |
{ |
| 221 |
+ |
double d1, d2; |
| 222 |
+ |
|
| 223 |
+ |
dv[2] = 1. - 2.*alt; |
| 224 |
+ |
d1 = sqrt(1. - dv[2]*dv[2]); |
| 225 |
+ |
d2 = 2.*PI * azi; |
| 226 |
+ |
dv[0] = d1*cos(d2); |
| 227 |
+ |
dv[1] = d1*sin(d2); |
| 228 |
+ |
} |
| 229 |
+ |
|
| 230 |
+ |
|
| 231 |
+ |
void |
| 232 |
+ |
flatdir( /* compute uniform hemispherical direction */ |
| 233 |
+ |
FVECT dv, |
| 234 |
+ |
double alt, |
| 235 |
+ |
double azi |
| 236 |
+ |
) |
| 237 |
+ |
{ |
| 238 |
+ |
double d1, d2; |
| 239 |
+ |
|
| 240 |
+ |
d1 = sqrt(alt); |
| 241 |
+ |
d2 = 2.*PI * azi; |
| 242 |
+ |
dv[0] = d1*cos(d2); |
| 243 |
+ |
dv[1] = d1*sin(d2); |
| 244 |
+ |
dv[2] = sqrt(1. - alt); |
| 245 |
+ |
} |
| 246 |
+ |
|
| 247 |
+ |
int |
| 248 |
+ |
flatindex( /* compute index for hemispherical direction */ |
| 249 |
+ |
FVECT dv, |
| 250 |
+ |
int nalt, |
| 251 |
+ |
int nazi |
| 252 |
+ |
) |
| 253 |
+ |
{ |
| 254 |
+ |
double d; |
| 255 |
+ |
int i, j; |
| 256 |
+ |
|
| 257 |
+ |
d = 1.0 - dv[2]*dv[2]; |
| 258 |
+ |
i = d*nalt; |
| 259 |
+ |
d = atan2(dv[1], dv[0]) * (0.5/PI); |
| 260 |
+ |
if (d < 0.0) d += 1.0; |
| 261 |
+ |
j = d*nazi + 0.5; |
| 262 |
+ |
if (j >= nazi) j = 0; |
| 263 |
+ |
return(i*nazi + j); |
| 264 |
+ |
} |
| 265 |
+ |
|
| 266 |
+ |
|
| 267 |
+ |
int |
| 268 |
+ |
my_default( /* default illum action */ |
| 269 |
+ |
OBJREC *ob, |
| 270 |
+ |
struct illum_args *il, |
| 271 |
+ |
char *nm |
| 272 |
+ |
) |
| 273 |
+ |
{ |
| 274 |
|
sprintf(errmsg, "(%s): cannot make illum for %s \"%s\"", |
| 275 |
|
nm, ofun[ob->otype].funame, ob->oname); |
| 276 |
|
error(WARNING, errmsg); |
| 277 |
< |
if (!(il->flags & IL_LIGHT)) |
| 278 |
< |
printobj(il->altmat, ob); |
| 277 |
> |
printobj(il->altmat, ob); |
| 278 |
> |
return(1); |
| 279 |
|
} |
| 280 |
|
|
| 281 |
|
|
| 282 |
< |
o_face(ob, il, rt, nm) /* make an illum face */ |
| 283 |
< |
OBJREC *ob; |
| 284 |
< |
struct illum_args *il; |
| 285 |
< |
struct rtproc *rt; |
| 286 |
< |
char *nm; |
| 282 |
> |
int |
| 283 |
> |
my_face( /* make an illum face */ |
| 284 |
> |
OBJREC *ob, |
| 285 |
> |
struct illum_args *il, |
| 286 |
> |
char *nm |
| 287 |
> |
) |
| 288 |
|
{ |
| 289 |
< |
#define MAXMISS (5*n*il->nsamps) |
| 290 |
< |
int dim[3]; |
| 42 |
< |
int n, nalt, nazi, h; |
| 43 |
< |
float *distarr; |
| 289 |
> |
int dim[2]; |
| 290 |
> |
int n, nalt, nazi, alti; |
| 291 |
|
double sp[2], r1, r2; |
| 292 |
+ |
int h; |
| 293 |
|
FVECT dn, org, dir; |
| 294 |
|
FVECT u, v; |
| 295 |
|
double ur[2], vr[2]; |
| 296 |
< |
int nmisses; |
| 297 |
< |
register FACE *fa; |
| 298 |
< |
register int i, j; |
| 296 |
> |
MAT4 xfm; |
| 297 |
> |
int nallow; |
| 298 |
> |
FACE *fa; |
| 299 |
> |
int i, j; |
| 300 |
|
/* get/check arguments */ |
| 301 |
|
fa = getface(ob); |
| 302 |
|
if (fa->area == 0.0) { |
| 303 |
|
freeface(ob); |
| 304 |
< |
o_default(ob, il, rt, nm); |
| 56 |
< |
return; |
| 304 |
> |
return(my_default(ob, il, nm)); |
| 305 |
|
} |
| 306 |
|
/* set up sampling */ |
| 307 |
< |
if (il->sampdens <= 0) |
| 308 |
< |
nalt = nazi = 1; |
| 309 |
< |
else { |
| 310 |
< |
n = PI * il->sampdens; |
| 311 |
< |
nalt = sqrt(n/PI) + .5; |
| 312 |
< |
nazi = PI*nalt + .5; |
| 307 |
> |
if (il->sd != NULL) { |
| 308 |
> |
if (!getBSDF_xfm(xfm, fa->norm, il->udir)) { |
| 309 |
> |
objerror(ob, WARNING, "illegal up direction"); |
| 310 |
> |
freeface(ob); |
| 311 |
> |
return(my_default(ob, il, nm)); |
| 312 |
> |
} |
| 313 |
> |
n = il->sd->ninc; |
| 314 |
> |
} else { |
| 315 |
> |
if (il->sampdens <= 0) { |
| 316 |
> |
nalt = nazi = 1; /* diffuse assumption */ |
| 317 |
> |
} else { |
| 318 |
> |
n = PI * il->sampdens; |
| 319 |
> |
nalt = sqrt(n/PI) + .5; |
| 320 |
> |
nazi = PI*nalt + .5; |
| 321 |
> |
} |
| 322 |
> |
n = nazi*nalt; |
| 323 |
|
} |
| 324 |
< |
n = nalt*nazi; |
| 325 |
< |
distarr = (float *)calloc(n, 3*sizeof(float)); |
| 326 |
< |
if (distarr == NULL) |
| 327 |
< |
error(SYSTEM, "out of memory in o_face"); |
| 328 |
< |
mkaxes(u, v, fa->norm); |
| 324 |
> |
newdist(n); |
| 325 |
> |
/* take first edge >= sqrt(area) */ |
| 326 |
> |
for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) { |
| 327 |
> |
u[0] = VERTEX(fa,i)[0] - VERTEX(fa,j)[0]; |
| 328 |
> |
u[1] = VERTEX(fa,i)[1] - VERTEX(fa,j)[1]; |
| 329 |
> |
u[2] = VERTEX(fa,i)[2] - VERTEX(fa,j)[2]; |
| 330 |
> |
if ((r1 = DOT(u,u)) >= fa->area-FTINY) |
| 331 |
> |
break; |
| 332 |
> |
} |
| 333 |
> |
if (i < fa->nv) { /* got one! -- let's align our axes */ |
| 334 |
> |
r2 = 1.0/sqrt(r1); |
| 335 |
> |
u[0] *= r2; u[1] *= r2; u[2] *= r2; |
| 336 |
> |
fcross(v, fa->norm, u); |
| 337 |
> |
} else /* oh well, we'll just have to wing it */ |
| 338 |
> |
mkaxes(u, v, fa->norm); |
| 339 |
> |
/* now, find limits in (u,v) coordinates */ |
| 340 |
|
ur[0] = vr[0] = FHUGE; |
| 341 |
|
ur[1] = vr[1] = -FHUGE; |
| 342 |
|
for (i = 0; i < fa->nv; i++) { |
| 349 |
|
} |
| 350 |
|
dim[0] = random(); |
| 351 |
|
/* sample polygon */ |
| 352 |
< |
nmisses = 0; |
| 353 |
< |
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
| 85 |
< |
for (dim[2] = 0; dim[2] < nazi; dim[2]++) |
| 352 |
> |
nallow = 5*n*il->nsamps; |
| 353 |
> |
for (dim[1] = 0; dim[1] < n; dim[1]++) |
| 354 |
|
for (i = 0; i < il->nsamps; i++) { |
| 355 |
< |
/* random direction */ |
| 356 |
< |
h = ilhash(dim, 3) + i; |
| 357 |
< |
multisamp(sp, 2, urand(h)); |
| 358 |
< |
r1 = (dim[1] + sp[0])/nalt; |
| 359 |
< |
r2 = (dim[2] + sp[1])/nazi; |
| 360 |
< |
flatdir(dn, r1, r2); |
| 361 |
< |
for (j = 0; j < 3; j++) |
| 362 |
< |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*fa->norm[j]; |
| 363 |
< |
/* random location */ |
| 355 |
> |
/* randomize direction */ |
| 356 |
> |
h = ilhash(dim, 2) + i; |
| 357 |
> |
if (il->sd != NULL) { |
| 358 |
> |
r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm); |
| 359 |
> |
} else { |
| 360 |
> |
multisamp(sp, 2, urand(h)); |
| 361 |
> |
alti = dim[1]/nazi; |
| 362 |
> |
r1 = (alti + sp[0])/nalt; |
| 363 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
| 364 |
> |
flatdir(dn, r1, r2); |
| 365 |
> |
for (j = 0; j < 3; j++) |
| 366 |
> |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - |
| 367 |
> |
dn[2]*fa->norm[j]; |
| 368 |
> |
} |
| 369 |
> |
/* randomize location */ |
| 370 |
|
do { |
| 371 |
< |
multisamp(sp, 2, urand(h+4862+nmisses)); |
| 371 |
> |
multisamp(sp, 2, urand(h+4862+nallow)); |
| 372 |
|
r1 = ur[0] + (ur[1]-ur[0]) * sp[0]; |
| 373 |
|
r2 = vr[0] + (vr[1]-vr[0]) * sp[1]; |
| 374 |
|
for (j = 0; j < 3; j++) |
| 375 |
|
org[j] = r1*u[j] + r2*v[j] |
| 376 |
|
+ fa->offset*fa->norm[j]; |
| 377 |
< |
} while (!inface(org, fa) && nmisses++ < MAXMISS); |
| 378 |
< |
if (nmisses > MAXMISS) { |
| 377 |
> |
} while (!inface(org, fa) && nallow-- > 0); |
| 378 |
> |
if (nallow < 0) { |
| 379 |
|
objerror(ob, WARNING, "bad aspect"); |
| 380 |
< |
rt->nrays = 0; |
| 380 |
> |
rayclean(); |
| 381 |
|
freeface(ob); |
| 382 |
< |
free((char *)distarr); |
| 109 |
< |
o_default(ob, il, rt, nm); |
| 110 |
< |
return; |
| 382 |
> |
return(my_default(ob, il, nm)); |
| 383 |
|
} |
| 384 |
+ |
if (il->sd != NULL && DOT(dir, fa->norm) < -FTINY) |
| 385 |
+ |
r1 = -1.0001*il->thick - 5.*FTINY; |
| 386 |
+ |
else |
| 387 |
+ |
r1 = 5.*FTINY; |
| 388 |
|
for (j = 0; j < 3; j++) |
| 389 |
< |
org[j] += .001*fa->norm[j]; |
| 389 |
> |
org[j] += r1*fa->norm[j]; |
| 390 |
|
/* send sample */ |
| 391 |
< |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); |
| 391 |
> |
raysamp(dim[1], org, dir); |
| 392 |
|
} |
| 393 |
< |
rayflush(rt); |
| 393 |
> |
/* add in direct component? */ |
| 394 |
> |
if (!directvis && (il->flags & IL_LIGHT || il->sd != NULL)) { |
| 395 |
> |
MAT4 ixfm; |
| 396 |
> |
if (il->sd == NULL) { |
| 397 |
> |
for (i = 3; i--; ) { |
| 398 |
> |
ixfm[i][0] = u[i]; |
| 399 |
> |
ixfm[i][1] = v[i]; |
| 400 |
> |
ixfm[i][2] = fa->norm[i]; |
| 401 |
> |
ixfm[i][3] = 0.; |
| 402 |
> |
} |
| 403 |
> |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
| 404 |
> |
ixfm[3][3] = 1.; |
| 405 |
> |
} else { |
| 406 |
> |
if (!invmat4(ixfm, xfm)) |
| 407 |
> |
objerror(ob, INTERNAL, |
| 408 |
> |
"cannot invert BSDF transform"); |
| 409 |
> |
if (!(il->flags & IL_LIGHT)) |
| 410 |
> |
new_discount(); |
| 411 |
> |
} |
| 412 |
> |
dim[0] = random(); |
| 413 |
> |
nallow = 10*il->nsamps; |
| 414 |
> |
for (i = 0; i < il->nsamps; i++) { |
| 415 |
> |
/* randomize location */ |
| 416 |
> |
h = dim[0] + samplendx++; |
| 417 |
> |
do { |
| 418 |
> |
multisamp(sp, 2, urand(h+nallow)); |
| 419 |
> |
r1 = ur[0] + (ur[1]-ur[0]) * sp[0]; |
| 420 |
> |
r2 = vr[0] + (vr[1]-vr[0]) * sp[1]; |
| 421 |
> |
for (j = 0; j < 3; j++) |
| 422 |
> |
org[j] = r1*u[j] + r2*v[j] |
| 423 |
> |
+ fa->offset*fa->norm[j]; |
| 424 |
> |
} while (!inface(org, fa) && nallow-- > 0); |
| 425 |
> |
if (nallow < 0) { |
| 426 |
> |
objerror(ob, WARNING, "bad aspect"); |
| 427 |
> |
rayclean(); |
| 428 |
> |
freeface(ob); |
| 429 |
> |
return(my_default(ob, il, nm)); |
| 430 |
> |
} |
| 431 |
> |
/* sample source rays */ |
| 432 |
> |
srcsamps(il, org, fa->norm, ixfm); |
| 433 |
> |
} |
| 434 |
> |
} |
| 435 |
> |
/* wait for all rays to finish */ |
| 436 |
> |
rayclean(); |
| 437 |
> |
if (il->sd != NULL) { /* run distribution through BSDF */ |
| 438 |
> |
nalt = sqrt(il->sd->nout/PI) + .5; |
| 439 |
> |
nazi = PI*nalt + .5; |
| 440 |
> |
redistribute(il->sd, nalt, nazi, u, v, fa->norm, xfm); |
| 441 |
> |
done_discount(); |
| 442 |
> |
if (!il->sampdens) |
| 443 |
> |
il->sampdens = nalt*nazi/PI + .999; |
| 444 |
> |
} |
| 445 |
|
/* write out the face and its distribution */ |
| 446 |
< |
average(il, distarr, nalt*nazi); |
| 447 |
< |
if (il->sampdens > 0) |
| 448 |
< |
flatout(il, distarr, nalt, nazi, u, v, fa->norm); |
| 449 |
< |
illumout(il, ob); |
| 446 |
> |
if (average(il, distarr, n)) { |
| 447 |
> |
if (il->sampdens > 0) |
| 448 |
> |
flatout(il, distarr, nalt, nazi, u, v, fa->norm); |
| 449 |
> |
illumout(il, ob); |
| 450 |
> |
} else |
| 451 |
> |
printobj(il->altmat, ob); |
| 452 |
|
/* clean up */ |
| 453 |
|
freeface(ob); |
| 454 |
< |
free((char *)distarr); |
| 126 |
< |
#undef MAXMISS |
| 454 |
> |
return(0); |
| 455 |
|
} |
| 456 |
|
|
| 457 |
|
|
| 458 |
< |
o_sphere(ob, il, rt, nm) /* make an illum sphere */ |
| 459 |
< |
register OBJREC *ob; |
| 460 |
< |
struct illum_args *il; |
| 461 |
< |
struct rtproc *rt; |
| 462 |
< |
char *nm; |
| 458 |
> |
int |
| 459 |
> |
my_sphere( /* make an illum sphere */ |
| 460 |
> |
register OBJREC *ob, |
| 461 |
> |
struct illum_args *il, |
| 462 |
> |
char *nm |
| 463 |
> |
) |
| 464 |
|
{ |
| 465 |
|
int dim[3]; |
| 466 |
|
int n, nalt, nazi; |
| 138 |
– |
float *distarr; |
| 467 |
|
double sp[4], r1, r2, r3; |
| 468 |
|
FVECT org, dir; |
| 469 |
|
FVECT u, v; |
| 476 |
|
nalt = nazi = 1; |
| 477 |
|
else { |
| 478 |
|
n = 4.*PI * il->sampdens; |
| 479 |
< |
nalt = sqrt(n/PI) + .5; |
| 480 |
< |
nazi = PI*nalt + .5; |
| 479 |
> |
nalt = sqrt(2./PI*n) + .5; |
| 480 |
> |
nazi = PI/2.*nalt + .5; |
| 481 |
|
} |
| 482 |
+ |
if (il->sd != NULL) |
| 483 |
+ |
objerror(ob, WARNING, "BSDF ignored"); |
| 484 |
|
n = nalt*nazi; |
| 485 |
< |
distarr = (float *)calloc(n, 3*sizeof(float)); |
| 156 |
< |
if (distarr == NULL) |
| 157 |
< |
error(SYSTEM, "out of memory in o_sphere"); |
| 485 |
> |
newdist(n); |
| 486 |
|
dim[0] = random(); |
| 487 |
|
/* sample sphere */ |
| 488 |
|
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
| 492 |
|
multisamp(sp, 4, urand(ilhash(dim,3)+i)); |
| 493 |
|
/* random direction */ |
| 494 |
|
r1 = (dim[1] + sp[0])/nalt; |
| 495 |
< |
r2 = (dim[2] + sp[1])/nazi; |
| 495 |
> |
r2 = (dim[2] + sp[1] - .5)/nazi; |
| 496 |
|
rounddir(dir, r1, r2); |
| 497 |
|
/* random location */ |
| 498 |
|
mkaxes(u, v, dir); /* yuck! */ |
| 507 |
|
dir[j] = -dir[j]; |
| 508 |
|
} |
| 509 |
|
/* send sample */ |
| 510 |
< |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); |
| 510 |
> |
raysamp(dim[1]*nazi+dim[2], org, dir); |
| 511 |
|
} |
| 512 |
< |
rayflush(rt); |
| 512 |
> |
/* wait for all rays to finish */ |
| 513 |
> |
rayclean(); |
| 514 |
|
/* write out the sphere and its distribution */ |
| 515 |
< |
average(il, distarr, nalt*nazi); |
| 516 |
< |
if (il->sampdens > 0) |
| 517 |
< |
roundout(il, distarr, nalt, nazi); |
| 518 |
< |
else |
| 519 |
< |
objerror(ob, WARNING, "diffuse distribution"); |
| 520 |
< |
illumout(il, ob); |
| 515 |
> |
if (average(il, distarr, n)) { |
| 516 |
> |
if (il->sampdens > 0) |
| 517 |
> |
roundout(il, distarr, nalt, nazi); |
| 518 |
> |
else |
| 519 |
> |
objerror(ob, WARNING, "diffuse distribution"); |
| 520 |
> |
illumout(il, ob); |
| 521 |
> |
} else |
| 522 |
> |
printobj(il->altmat, ob); |
| 523 |
|
/* clean up */ |
| 524 |
< |
free((char *)distarr); |
| 524 |
> |
return(1); |
| 525 |
|
} |
| 526 |
|
|
| 527 |
|
|
| 528 |
< |
o_ring(ob, il, rt, nm) /* make an illum ring */ |
| 529 |
< |
OBJREC *ob; |
| 530 |
< |
struct illum_args *il; |
| 531 |
< |
struct rtproc *rt; |
| 532 |
< |
char *nm; |
| 528 |
> |
int |
| 529 |
> |
my_ring( /* make an illum ring */ |
| 530 |
> |
OBJREC *ob, |
| 531 |
> |
struct illum_args *il, |
| 532 |
> |
char *nm |
| 533 |
> |
) |
| 534 |
|
{ |
| 535 |
< |
int dim[3]; |
| 536 |
< |
int n, nalt, nazi; |
| 537 |
< |
float *distarr; |
| 538 |
< |
double sp[4], r1, r2, r3; |
| 535 |
> |
int dim[2]; |
| 536 |
> |
int n, nalt, nazi, alti; |
| 537 |
> |
double sp[2], r1, r2, r3; |
| 538 |
> |
int h; |
| 539 |
|
FVECT dn, org, dir; |
| 540 |
|
FVECT u, v; |
| 541 |
< |
register CONE *co; |
| 542 |
< |
register int i, j; |
| 541 |
> |
MAT4 xfm; |
| 542 |
> |
CONE *co; |
| 543 |
> |
int i, j; |
| 544 |
|
/* get/check arguments */ |
| 545 |
|
co = getcone(ob, 0); |
| 546 |
|
/* set up sampling */ |
| 547 |
< |
if (il->sampdens <= 0) |
| 548 |
< |
nalt = nazi = 1; |
| 549 |
< |
else { |
| 550 |
< |
n = PI * il->sampdens; |
| 551 |
< |
nalt = sqrt(n/PI) + .5; |
| 552 |
< |
nazi = PI*nalt + .5; |
| 547 |
> |
if (il->sd != NULL) { |
| 548 |
> |
if (!getBSDF_xfm(xfm, co->ad, il->udir)) { |
| 549 |
> |
objerror(ob, WARNING, "illegal up direction"); |
| 550 |
> |
freecone(ob); |
| 551 |
> |
return(my_default(ob, il, nm)); |
| 552 |
> |
} |
| 553 |
> |
n = il->sd->ninc; |
| 554 |
> |
} else { |
| 555 |
> |
if (il->sampdens <= 0) { |
| 556 |
> |
nalt = nazi = 1; /* diffuse assumption */ |
| 557 |
> |
} else { |
| 558 |
> |
n = PI * il->sampdens; |
| 559 |
> |
nalt = sqrt(n/PI) + .5; |
| 560 |
> |
nazi = PI*nalt + .5; |
| 561 |
> |
} |
| 562 |
> |
n = nazi*nalt; |
| 563 |
|
} |
| 564 |
< |
n = nalt*nazi; |
| 222 |
< |
distarr = (float *)calloc(n, 3*sizeof(float)); |
| 223 |
< |
if (distarr == NULL) |
| 224 |
< |
error(SYSTEM, "out of memory in o_ring"); |
| 564 |
> |
newdist(n); |
| 565 |
|
mkaxes(u, v, co->ad); |
| 566 |
|
dim[0] = random(); |
| 567 |
|
/* sample disk */ |
| 568 |
< |
for (dim[1] = 0; dim[1] < nalt; dim[1]++) |
| 229 |
< |
for (dim[2] = 0; dim[2] < nazi; dim[2]++) |
| 568 |
> |
for (dim[1] = 0; dim[1] < n; dim[1]++) |
| 569 |
|
for (i = 0; i < il->nsamps; i++) { |
| 570 |
|
/* next sample point */ |
| 571 |
< |
multisamp(sp, 4, urand(ilhash(dim,3)+i)); |
| 572 |
< |
/* random direction */ |
| 573 |
< |
r1 = (dim[1] + sp[0])/nalt; |
| 574 |
< |
r2 = (dim[2] + sp[1])/nalt; |
| 575 |
< |
flatdir(dn, r1, r2); |
| 576 |
< |
for (j = 0; j < 3; j++) |
| 577 |
< |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
| 578 |
< |
/* random location */ |
| 571 |
> |
h = ilhash(dim,2) + i; |
| 572 |
> |
/* randomize direction */ |
| 573 |
> |
if (il->sd != NULL) { |
| 574 |
> |
r_BSDF_incvec(dir, il->sd, dim[1], urand(h), xfm); |
| 575 |
> |
} else { |
| 576 |
> |
multisamp(sp, 2, urand(h)); |
| 577 |
> |
alti = dim[1]/nazi; |
| 578 |
> |
r1 = (alti + sp[0])/nalt; |
| 579 |
> |
r2 = (dim[1] - alti*nazi + sp[1] - .5)/nazi; |
| 580 |
> |
flatdir(dn, r1, r2); |
| 581 |
> |
for (j = 0; j < 3; j++) |
| 582 |
> |
dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j]; |
| 583 |
> |
} |
| 584 |
> |
/* randomize location */ |
| 585 |
> |
multisamp(sp, 2, urand(h+8371)); |
| 586 |
|
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
| 587 |
< |
sp[2]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
| 588 |
< |
r2 = 2.*PI*sp[3]; |
| 587 |
> |
sp[0]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
| 588 |
> |
r2 = 2.*PI*sp[1]; |
| 589 |
|
r1 = r3*cos(r2); |
| 590 |
|
r2 = r3*sin(r2); |
| 591 |
+ |
if (il->sd != NULL && DOT(dir, co->ad) < -FTINY) |
| 592 |
+ |
r3 = -1.0001*il->thick - 5.*FTINY; |
| 593 |
+ |
else |
| 594 |
+ |
r3 = 5.*FTINY; |
| 595 |
|
for (j = 0; j < 3; j++) |
| 596 |
< |
org[j] = CO_P0(co)[j] + r1*u[j] + r1*v[j] + |
| 597 |
< |
.001*co->ad[j]; |
| 248 |
< |
|
| 596 |
> |
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] + |
| 597 |
> |
r3*co->ad[j]; |
| 598 |
|
/* send sample */ |
| 599 |
< |
raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt); |
| 599 |
> |
raysamp(dim[1], org, dir); |
| 600 |
|
} |
| 601 |
< |
rayflush(rt); |
| 601 |
> |
/* add in direct component? */ |
| 602 |
> |
if (!directvis && (il->flags & IL_LIGHT || il->sd != NULL)) { |
| 603 |
> |
MAT4 ixfm; |
| 604 |
> |
if (il->sd == NULL) { |
| 605 |
> |
for (i = 3; i--; ) { |
| 606 |
> |
ixfm[i][0] = u[i]; |
| 607 |
> |
ixfm[i][1] = v[i]; |
| 608 |
> |
ixfm[i][2] = co->ad[i]; |
| 609 |
> |
ixfm[i][3] = 0.; |
| 610 |
> |
} |
| 611 |
> |
ixfm[3][0] = ixfm[3][1] = ixfm[3][2] = 0.; |
| 612 |
> |
ixfm[3][3] = 1.; |
| 613 |
> |
} else { |
| 614 |
> |
if (!invmat4(ixfm, xfm)) |
| 615 |
> |
objerror(ob, INTERNAL, |
| 616 |
> |
"cannot invert BSDF transform"); |
| 617 |
> |
if (!(il->flags & IL_LIGHT)) |
| 618 |
> |
new_discount(); |
| 619 |
> |
} |
| 620 |
> |
dim[0] = random(); |
| 621 |
> |
for (i = 0; i < il->nsamps; i++) { |
| 622 |
> |
/* randomize location */ |
| 623 |
> |
h = dim[0] + samplendx++; |
| 624 |
> |
multisamp(sp, 2, urand(h)); |
| 625 |
> |
r3 = sqrt(CO_R0(co)*CO_R0(co) + |
| 626 |
> |
sp[0]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co))); |
| 627 |
> |
r2 = 2.*PI*sp[1]; |
| 628 |
> |
r1 = r3*cos(r2); |
| 629 |
> |
r2 = r3*sin(r2); |
| 630 |
> |
for (j = 0; j < 3; j++) |
| 631 |
> |
org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j]; |
| 632 |
> |
/* sample source rays */ |
| 633 |
> |
srcsamps(il, org, co->ad, ixfm); |
| 634 |
> |
} |
| 635 |
> |
} |
| 636 |
> |
/* wait for all rays to finish */ |
| 637 |
> |
rayclean(); |
| 638 |
> |
if (il->sd != NULL) { /* run distribution through BSDF */ |
| 639 |
> |
nalt = sqrt(il->sd->nout/PI) + .5; |
| 640 |
> |
nazi = PI*nalt + .5; |
| 641 |
> |
redistribute(il->sd, nalt, nazi, u, v, co->ad, xfm); |
| 642 |
> |
done_discount(); |
| 643 |
> |
if (!il->sampdens) |
| 644 |
> |
il->sampdens = nalt*nazi/PI + .999; |
| 645 |
> |
} |
| 646 |
|
/* write out the ring and its distribution */ |
| 647 |
< |
average(il, distarr, nalt*nazi); |
| 648 |
< |
if (il->sampdens > 0) |
| 649 |
< |
flatout(il, distarr, nalt, nazi, u, v, co->ad); |
| 650 |
< |
illumout(il, ob); |
| 647 |
> |
if (average(il, distarr, n)) { |
| 648 |
> |
if (il->sampdens > 0) |
| 649 |
> |
flatout(il, distarr, nalt, nazi, u, v, co->ad); |
| 650 |
> |
illumout(il, ob); |
| 651 |
> |
} else |
| 652 |
> |
printobj(il->altmat, ob); |
| 653 |
|
/* clean up */ |
| 654 |
|
freecone(ob); |
| 655 |
< |
free((char *)distarr); |
| 261 |
< |
} |
| 262 |
< |
|
| 263 |
< |
|
| 264 |
< |
raysamp(res, org, dir, rt) /* compute a ray sample */ |
| 265 |
< |
float res[3]; |
| 266 |
< |
FVECT org, dir; |
| 267 |
< |
register struct rtproc *rt; |
| 268 |
< |
{ |
| 269 |
< |
register float *fp; |
| 270 |
< |
|
| 271 |
< |
if (rt->nrays == rt->bsiz) |
| 272 |
< |
rayflush(rt); |
| 273 |
< |
rt->dest[rt->nrays] = res; |
| 274 |
< |
fp = rt->buf + 6*rt->nrays++; |
| 275 |
< |
*fp++ = org[0]; *fp++ = org[1]; *fp++ = org[2]; |
| 276 |
< |
*fp++ = dir[0]; *fp++ = dir[1]; *fp = dir[2]; |
| 277 |
< |
} |
| 278 |
< |
|
| 279 |
< |
|
| 280 |
< |
rayflush(rt) /* flush buffered rays */ |
| 281 |
< |
register struct rtproc *rt; |
| 282 |
< |
{ |
| 283 |
< |
register int i; |
| 284 |
< |
|
| 285 |
< |
if (rt->nrays <= 0) |
| 286 |
< |
return; |
| 287 |
< |
bzero(rt->buf+6*rt->nrays, 6*sizeof(float)); |
| 288 |
< |
if ( process(rt->pd, (char *)rt->buf, (char *)rt->buf, |
| 289 |
< |
3*sizeof(float)*rt->nrays, |
| 290 |
< |
6*sizeof(float)*(rt->nrays+1)) < |
| 291 |
< |
3*sizeof(float)*rt->nrays ) |
| 292 |
< |
error(SYSTEM, "error reading from rtrace process"); |
| 293 |
< |
i = rt->nrays; |
| 294 |
< |
while (i--) { |
| 295 |
< |
rt->dest[i][0] += rt->buf[3*i]; |
| 296 |
< |
rt->dest[i][1] += rt->buf[3*i+1]; |
| 297 |
< |
rt->dest[i][2] += rt->buf[3*i+2]; |
| 298 |
< |
} |
| 299 |
< |
rt->nrays = 0; |
| 300 |
< |
} |
| 301 |
< |
|
| 302 |
< |
|
| 303 |
< |
mkaxes(u, v, n) /* compute u and v to go with n */ |
| 304 |
< |
FVECT u, v, n; |
| 305 |
< |
{ |
| 306 |
< |
register int i; |
| 307 |
< |
|
| 308 |
< |
v[0] = v[1] = v[2] = 0.0; |
| 309 |
< |
for (i = 0; i < 3; i++) |
| 310 |
< |
if (n[i] < 0.6 && n[i] > -0.6) |
| 311 |
< |
break; |
| 312 |
< |
v[i] = 1.0; |
| 313 |
< |
fcross(u, v, n); |
| 314 |
< |
normalize(u); |
| 315 |
< |
fcross(v, n, u); |
| 316 |
< |
} |
| 317 |
< |
|
| 318 |
< |
|
| 319 |
< |
rounddir(dv, alt, azi) /* compute uniform spherical direction */ |
| 320 |
< |
register FVECT dv; |
| 321 |
< |
double alt, azi; |
| 322 |
< |
{ |
| 323 |
< |
double d1, d2; |
| 324 |
< |
|
| 325 |
< |
dv[2] = 1. - 2.*alt; |
| 326 |
< |
d1 = sqrt(1. - dv[2]*dv[2]); |
| 327 |
< |
d2 = 2.*PI * azi; |
| 328 |
< |
dv[0] = d1*cos(d2); |
| 329 |
< |
dv[1] = d1*sin(d2); |
| 330 |
< |
} |
| 331 |
< |
|
| 332 |
< |
|
| 333 |
< |
flatdir(dv, alt, azi) /* compute uniform hemispherical direction */ |
| 334 |
< |
register FVECT dv; |
| 335 |
< |
double alt, azi; |
| 336 |
< |
{ |
| 337 |
< |
double d1, d2; |
| 338 |
< |
|
| 339 |
< |
d1 = sqrt(alt); |
| 340 |
< |
d2 = 2.*PI * azi; |
| 341 |
< |
dv[0] = d1*cos(d2); |
| 342 |
< |
dv[1] = d1*sin(d2); |
| 343 |
< |
dv[2] = sqrt(1. - alt); |
| 655 |
> |
return(1); |
| 656 |
|
} |