| 38 |
|
|
| 39 |
|
marksources() /* find and mark source objects */ |
| 40 |
|
{ |
| 41 |
+ |
int i; |
| 42 |
|
register OBJREC *o, *m; |
| 43 |
< |
register int i; |
| 43 |
> |
register SRCREC *ns; |
| 44 |
|
|
| 45 |
|
for (i = 0; i < nobjects; i++) { |
| 46 |
|
|
| 63 |
|
m->oargs.farg[3] <= FTINY) |
| 64 |
|
continue; /* don't bother */ |
| 65 |
|
|
| 66 |
< |
if (source == NULL) |
| 66 |
< |
source = (SRCREC *)malloc(sizeof(SRCREC)); |
| 67 |
< |
else |
| 68 |
< |
source = (SRCREC *)realloc((char *)source, |
| 69 |
< |
(unsigned)(nsources+1)*sizeof(SRCREC)); |
| 70 |
< |
if (source == NULL) |
| 66 |
> |
if ((ns = newsource()) == NULL) |
| 67 |
|
goto memerr; |
| 68 |
|
|
| 69 |
< |
newsource(&source[nsources], o); |
| 69 |
> |
setsource(ns, o); |
| 70 |
|
|
| 71 |
|
if (m->otype == MAT_GLOW) { |
| 72 |
< |
source[nsources].sflags |= SPROX; |
| 73 |
< |
source[nsources].sl.prox = m->oargs.farg[3]; |
| 72 |
> |
ns->sflags |= SPROX; |
| 73 |
> |
ns->sl.prox = m->oargs.farg[3]; |
| 74 |
|
if (o->otype == OBJ_SOURCE) |
| 75 |
< |
source[nsources].sflags |= SSKIP; |
| 75 |
> |
ns->sflags |= SSKIP; |
| 76 |
|
} else if (m->otype == MAT_SPOT) { |
| 77 |
< |
source[nsources].sflags |= SSPOT; |
| 78 |
< |
source[nsources].sl.s = makespot(m); |
| 77 |
> |
ns->sflags |= SSPOT; |
| 78 |
> |
if ((ns->sl.s = makespot(m)) == NULL) |
| 79 |
> |
goto memerr; |
| 80 |
|
} |
| 84 |
– |
nsources++; |
| 81 |
|
} |
| 82 |
|
if (nsources <= 0) { |
| 83 |
|
error(WARNING, "no light sources found"); |
| 84 |
|
return; |
| 85 |
|
} |
| 86 |
+ |
markvirtuals(); /* find and add virtual sources */ |
| 87 |
|
srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB)); |
| 88 |
|
cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR)); |
| 89 |
|
if (srccnt != NULL && cntord != NULL) |
| 90 |
< |
return; |
| 91 |
< |
/* fall through */ |
| 90 |
> |
goto memerr; |
| 91 |
> |
return; |
| 92 |
|
memerr: |
| 93 |
|
error(SYSTEM, "out of memory in marksources"); |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
|
| 97 |
< |
newsource(src, so) /* add a source to the array */ |
| 97 |
> |
SRCREC * |
| 98 |
> |
newsource() /* allocate new source in our array */ |
| 99 |
> |
{ |
| 100 |
> |
if (nsources == 0) |
| 101 |
> |
source = (SRCREC *)malloc(sizeof(SRCREC)); |
| 102 |
> |
else |
| 103 |
> |
source = (SRCREC *)realloc((char *)source, |
| 104 |
> |
(unsigned)(nsources+1)*sizeof(SRCREC)); |
| 105 |
> |
if (source == NULL) |
| 106 |
> |
return(NULL); |
| 107 |
> |
source[nsources].sflags = 0; |
| 108 |
> |
source[nsources].nhits = 1; |
| 109 |
> |
source[nsources].ntests = 2; /* initial hit probability = 1/2 */ |
| 110 |
> |
return(&source[nsources++]); |
| 111 |
> |
} |
| 112 |
> |
|
| 113 |
> |
|
| 114 |
> |
setsource(src, so) /* add a source to the array */ |
| 115 |
|
register SRCREC *src; |
| 116 |
|
register OBJREC *so; |
| 117 |
|
{ |
| 122 |
|
int j; |
| 123 |
|
register int i; |
| 124 |
|
|
| 125 |
< |
src->sflags = 0; |
| 112 |
< |
src->aimsuccess = 2*AIMREQT-1; /* bitch on second failure */ |
| 113 |
< |
src->nhits = 1; src->ntests = 2; /* start probability = 1/2 */ |
| 125 |
> |
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
| 126 |
|
src->so = so; |
| 127 |
|
|
| 128 |
|
switch (so->otype) { |
| 152 |
|
src->sloc[j] = 0.0; |
| 153 |
|
for (i = 0; i < f->nv; i++) |
| 154 |
|
src->sloc[j] += VERTEX(f,i)[j]; |
| 155 |
< |
src->sloc[j] /= f->nv; |
| 155 |
> |
src->sloc[j] /= (double)f->nv; |
| 156 |
|
} |
| 157 |
|
if (!inface(src->sloc, f)) |
| 158 |
|
objerror(so, USER, "cannot hit center"); |
| 159 |
+ |
src->sflags |= SFLAT; |
| 160 |
+ |
VCOPY(src->snorm, f->norm); |
| 161 |
|
src->ss = sqrt(f->area / PI); |
| 162 |
|
src->ss2 = f->area; |
| 163 |
|
break; |
| 167 |
|
VCOPY(src->sloc, CO_P0(co)); |
| 168 |
|
if (CO_R0(co) > 0.0) |
| 169 |
|
objerror(so, USER, "cannot hit center"); |
| 170 |
+ |
src->sflags |= SFLAT; |
| 171 |
+ |
VCOPY(src->snorm, co->ad); |
| 172 |
|
src->ss = CO_R1(co); |
| 173 |
|
src->ss2 = PI * src->ss * src->ss; |
| 174 |
|
break; |
| 186 |
|
register SPOT *ns; |
| 187 |
|
|
| 188 |
|
if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
| 189 |
< |
error(SYSTEM, "out of memory in makespot"); |
| 189 |
> |
return(NULL); |
| 190 |
|
ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3])); |
| 191 |
|
VCOPY(ns->aim, m->oargs.farg+4); |
| 192 |
|
if ((ns->flen = normalize(ns->aim)) == 0.0) |
| 201 |
|
RAY *r; /* ray which hit object */ |
| 202 |
|
register int sn; /* source number */ |
| 203 |
|
{ |
| 188 |
– |
register double *norm = NULL; /* plane normal */ |
| 204 |
|
double ddot; /* (distance times) cosine */ |
| 205 |
|
FVECT vd; |
| 206 |
|
double d; |
| 213 |
|
|
| 214 |
|
sr->rsrc = sn; /* remember source */ |
| 215 |
|
/* get source direction */ |
| 216 |
< |
if (source[sn].sflags & SDISTANT) |
| 216 |
> |
if (source[sn].sflags & SDISTANT) { |
| 217 |
> |
if (source[sn].sflags & SSPOT) { /* check location */ |
| 218 |
> |
for (i = 0; i < 3; i++) |
| 219 |
> |
vd[i] = sr->rorg[i] - source[sn].sl.s->aim[i]; |
| 220 |
> |
d = DOT(source[sn].sloc,vd); |
| 221 |
> |
d = DOT(vd,vd) - d*d; |
| 222 |
> |
if (PI*d > source[sn].sl.s->siz) |
| 223 |
> |
return(0.0); |
| 224 |
> |
} |
| 225 |
|
/* constant direction */ |
| 226 |
|
VCOPY(sr->rdir, source[sn].sloc); |
| 227 |
< |
else { /* compute direction */ |
| 227 |
> |
} else { /* compute direction */ |
| 228 |
|
for (i = 0; i < 3; i++) |
| 229 |
|
sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i]; |
| 230 |
|
|
| 231 |
< |
if (source[sn].so->otype == OBJ_FACE) |
| 232 |
< |
norm = getface(source[sn].so)->norm; |
| 210 |
< |
else if (source[sn].so->otype == OBJ_RING) |
| 211 |
< |
norm = getcone(source[sn].so,0)->ad; |
| 212 |
< |
|
| 213 |
< |
if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY) |
| 231 |
> |
if (source[sn].sflags & SFLAT && |
| 232 |
> |
(ddot = -DOT(sr->rdir, source[sn].snorm)) <= FTINY) |
| 233 |
|
return(0.0); /* behind surface! */ |
| 234 |
|
} |
| 235 |
|
if (dstrsrc > FTINY) { |
| 241 |
|
(1.0 - 2.0*urand(ilhash(dimlist,ndims+1)+samplendx)); |
| 242 |
|
} |
| 243 |
|
ndims--; |
| 244 |
< |
if (norm != NULL) { /* project offset */ |
| 245 |
< |
d = DOT(vd, norm); |
| 244 |
> |
if (source[sn].sflags & SFLAT) { /* project offset */ |
| 245 |
> |
d = DOT(vd, source[sn].snorm); |
| 246 |
|
for (i = 0; i < 3; i++) |
| 247 |
< |
vd[i] -= d * norm[i]; |
| 247 |
> |
vd[i] -= d * source[sn].snorm[i]; |
| 248 |
|
} |
| 249 |
|
for (i = 0; i < 3; i++) /* offset source direction */ |
| 250 |
|
sr->rdir[i] += vd[i]; |
| 266 |
|
d > source[sn].sl.prox) |
| 267 |
|
return(0.0); |
| 268 |
|
/* compute dot product */ |
| 269 |
< |
if (norm != NULL) |
| 269 |
> |
if (source[sn].sflags & SFLAT) |
| 270 |
|
ddot /= d; |
| 271 |
|
else |
| 272 |
|
ddot = 1.0; |
| 360 |
|
cntord[sn].brt = bright(srccnt[sn].val); |
| 361 |
|
if (cntord[sn].brt <= 0.0) |
| 362 |
|
continue; |
| 344 |
– |
/* compute intersection */ |
| 345 |
– |
if (source[sn].sflags & SDISTANT ? |
| 346 |
– |
sourcehit(&sr) : |
| 347 |
– |
(*ofun[source[sn].so->otype].funp) |
| 348 |
– |
(source[sn].so, &sr)) { |
| 349 |
– |
if (source[sn].aimsuccess >= 0) |
| 350 |
– |
source[sn].aimsuccess++; |
| 351 |
– |
} else { |
| 352 |
– |
cntord[sn].brt = 0.0; |
| 353 |
– |
if (source[sn].aimsuccess < 0) |
| 354 |
– |
continue; /* bitched already */ |
| 355 |
– |
source[sn].aimsuccess -= AIMREQT; |
| 356 |
– |
if (source[sn].aimsuccess >= 0) |
| 357 |
– |
continue; /* leniency */ |
| 358 |
– |
sprintf(errmsg, |
| 359 |
– |
"aiming failure for light source \"%s\"", |
| 360 |
– |
source[sn].so->oname); |
| 361 |
– |
error(WARNING, errmsg); |
| 362 |
– |
continue; |
| 363 |
– |
} |
| 363 |
|
/* compute contribution */ |
| 364 |
< |
raycont(&sr); |
| 364 |
> |
srcvalue(&sr); |
| 365 |
|
multcolor(srccnt[sn].val, sr.rcol); |
| 366 |
|
cntord[sn].brt = bright(srccnt[sn].val); |
| 367 |
|
} |
| 397 |
|
VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir); |
| 398 |
|
sr.rsrc = cntord[sn].sno; |
| 399 |
|
if (localhit(&sr, &thescene) && |
| 400 |
< |
sr.ro != source[cntord[sn].sno].so) { |
| 401 |
< |
/* check for transmission */ |
| 400 |
> |
( sr.ro != source[cntord[sn].sno].so || |
| 401 |
> |
source[cntord[sn].sno].sflags & SFOLLOW )) { |
| 402 |
> |
/* follow entire path */ |
| 403 |
|
raycont(&sr); |
| 404 |
|
if (bright(sr.rcol) <= FTINY) |
| 405 |
|
continue; /* missed! */ |
| 433 |
|
} |
| 434 |
|
|
| 435 |
|
|
| 436 |
+ |
srcvalue(r) /* punch ray to source and compute value */ |
| 437 |
+ |
RAY *r; |
| 438 |
+ |
{ |
| 439 |
+ |
register SRCREC *sp; |
| 440 |
+ |
|
| 441 |
+ |
sp = &source[r->rsrc]; |
| 442 |
+ |
if (sp->sflags & SVIRTUAL) { /* virtual source */ |
| 443 |
+ |
RAY nr; |
| 444 |
+ |
/* check intersection */ |
| 445 |
+ |
if (!(*ofun[sp->so->otype].funp)(sp->so, r)) |
| 446 |
+ |
return; |
| 447 |
+ |
/* relay ray to source */ |
| 448 |
+ |
vsrcrelay(&nr, r); |
| 449 |
+ |
srcvalue(&nr); |
| 450 |
+ |
return; |
| 451 |
+ |
} |
| 452 |
+ |
/* compute intersection */ |
| 453 |
+ |
if (sp->sflags & SDISTANT ? sourcehit(r) : |
| 454 |
+ |
(*ofun[sp->so->otype].funp)(sp->so, r)) { |
| 455 |
+ |
if (sp->sa.success >= 0) |
| 456 |
+ |
sp->sa.success++; |
| 457 |
+ |
raycont(r); /* compute contribution */ |
| 458 |
+ |
return; |
| 459 |
+ |
} |
| 460 |
+ |
if (sp->sa.success < 0) |
| 461 |
+ |
return; /* bitched already */ |
| 462 |
+ |
sp->sa.success -= AIMREQT; |
| 463 |
+ |
if (sp->sa.success >= 0) |
| 464 |
+ |
return; /* leniency */ |
| 465 |
+ |
sprintf(errmsg, "aiming failure for light source \"%s\"", |
| 466 |
+ |
sp->so->oname); |
| 467 |
+ |
error(WARNING, errmsg); /* issue warning */ |
| 468 |
+ |
} |
| 469 |
+ |
|
| 470 |
+ |
|
| 471 |
|
#define wrongsource(m, r) (m->otype!=MAT_ILLUM && \ |
| 472 |
|
r->rsrc>=0 && \ |
| 473 |
|
source[r->rsrc].so!=r->ro) |
| 474 |
|
|
| 475 |
|
#define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \ |
| 441 |
– |
!(r->rtype&REFLECTED) && /* hack! */\ |
| 476 |
|
!(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3])) |
| 477 |
|
|
| 478 |
|
#define passillum(m, r) (m->otype==MAT_ILLUM && \ |