| 1 |
– |
/* Copyright (c) 1993 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 |
|
* source.c - routines dealing with illumination sources. |
| 6 |
|
* |
| 7 |
< |
* 8/20/85 |
| 7 |
> |
* External symbols declared in source.h |
| 8 |
|
*/ |
| 9 |
|
|
| 10 |
+ |
#include "copyright.h" |
| 11 |
+ |
|
| 12 |
|
#include "ray.h" |
| 13 |
|
|
| 15 |
– |
#include "octree.h" |
| 16 |
– |
|
| 14 |
|
#include "otypes.h" |
| 15 |
|
|
| 16 |
|
#include "source.h" |
| 17 |
|
|
| 18 |
+ |
#include "random.h" |
| 19 |
+ |
|
| 20 |
+ |
extern double ssampdist; /* scatter sampling distance */ |
| 21 |
+ |
|
| 22 |
+ |
#ifndef MAXSSAMP |
| 23 |
+ |
#define MAXSSAMP 16 /* maximum samples per ray */ |
| 24 |
+ |
#endif |
| 25 |
+ |
|
| 26 |
|
/* |
| 27 |
|
* Structures used by direct() |
| 28 |
|
*/ |
| 44 |
|
static int maxcntr = 0; /* size of contribution arrays */ |
| 45 |
|
|
| 46 |
|
|
| 47 |
+ |
void |
| 48 |
|
marksources() /* find and mark source objects */ |
| 49 |
|
{ |
| 50 |
|
int foundsource = 0; |
| 54 |
|
/* initialize dispatch table */ |
| 55 |
|
initstypes(); |
| 56 |
|
/* find direct sources */ |
| 57 |
< |
for (i = 0; i < nobjects; i++) { |
| 57 |
> |
for (i = 0; i < nsceneobjs; i++) { |
| 58 |
|
|
| 59 |
|
o = objptr(i); |
| 60 |
|
|
| 61 |
|
if (!issurface(o->otype) || o->omod == OVOID) |
| 62 |
|
continue; |
| 63 |
< |
|
| 63 |
> |
/* find material */ |
| 64 |
|
m = objptr(o->omod); |
| 65 |
< |
|
| 66 |
< |
if (!islight(m->otype)) |
| 67 |
< |
continue; |
| 65 |
> |
while (!ismaterial(m->otype)) |
| 66 |
> |
if (ismixture(m->otype) || m->omod == OVOID) { |
| 67 |
> |
m = NULL; |
| 68 |
> |
break; |
| 69 |
> |
} else |
| 70 |
> |
m = objptr(m->omod); |
| 71 |
> |
if (m == NULL || !islight(m->otype)) |
| 72 |
> |
continue; /* not source modifier */ |
| 73 |
|
|
| 74 |
|
if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 : |
| 75 |
|
m->otype == MAT_SPOT ? 7 : 3)) |
| 79 |
|
o->otype != OBJ_SOURCE && |
| 80 |
|
m->oargs.farg[3] <= FTINY) |
| 81 |
|
continue; /* don't bother */ |
| 82 |
+ |
if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY && |
| 83 |
+ |
m->oargs.farg[2] <= FTINY) |
| 84 |
+ |
continue; /* don't bother */ |
| 85 |
|
|
| 86 |
|
if (sfun[o->otype].of == NULL || |
| 87 |
|
sfun[o->otype].of->setsrc == NULL) |
| 120 |
|
maxcntr = nsources + MAXSPART; /* start with this many */ |
| 121 |
|
srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB)); |
| 122 |
|
cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR)); |
| 123 |
< |
if (srccnt == NULL | cntord == NULL) |
| 123 |
> |
if ((srccnt == NULL) | (cntord == NULL)) |
| 124 |
|
goto memerr; |
| 125 |
|
return; |
| 126 |
|
memerr: |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
|
| 131 |
+ |
void |
| 132 |
+ |
freesources() /* free all source structures */ |
| 133 |
+ |
{ |
| 134 |
+ |
if (nsources > 0) { |
| 135 |
+ |
free((void *)source); |
| 136 |
+ |
source = NULL; |
| 137 |
+ |
nsources = 0; |
| 138 |
+ |
} |
| 139 |
+ |
if (maxcntr <= 0) |
| 140 |
+ |
return; |
| 141 |
+ |
free((void *)srccnt); |
| 142 |
+ |
srccnt = NULL; |
| 143 |
+ |
free((void *)cntord); |
| 144 |
+ |
cntord = NULL; |
| 145 |
+ |
maxcntr = 0; |
| 146 |
+ |
} |
| 147 |
+ |
|
| 148 |
+ |
|
| 149 |
+ |
int |
| 150 |
|
srcray(sr, r, si) /* send a ray to a source, return domega */ |
| 151 |
|
register RAY *sr; /* returned source ray */ |
| 152 |
|
RAY *r; /* ray which hit object */ |
| 161 |
|
sr->rsrc = si->sn; /* remember source */ |
| 162 |
|
srcp = source + si->sn; |
| 163 |
|
if (srcp->sflags & SDISTANT) { |
| 164 |
< |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s, 1)) |
| 164 |
> |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) |
| 165 |
|
continue; |
| 166 |
|
return(1); /* sample OK */ |
| 167 |
|
} |
| 171 |
|
continue; |
| 172 |
|
/* check angle */ |
| 173 |
|
if (srcp->sflags & SSPOT) { |
| 174 |
< |
if (spotout(sr, srcp->sl.s, 0)) |
| 174 |
> |
if (spotout(sr, srcp->sl.s)) |
| 175 |
|
continue; |
| 176 |
|
/* adjust solid angle */ |
| 177 |
|
si->dom *= d*d; |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
|
| 187 |
+ |
void |
| 188 |
|
srcvalue(r) /* punch ray to source and compute value */ |
| 189 |
< |
RAY *r; |
| 189 |
> |
register RAY *r; |
| 190 |
|
{ |
| 191 |
|
register SRCREC *sp; |
| 192 |
|
|
| 195 |
|
/* check intersection */ |
| 196 |
|
if (!(*ofun[sp->so->otype].funp)(sp->so, r)) |
| 197 |
|
return; |
| 198 |
< |
raycont(r); /* compute contribution */ |
| 198 |
> |
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
| 199 |
> |
goto nomat; |
| 200 |
> |
rayparticipate(r); |
| 201 |
|
return; |
| 202 |
|
} |
| 203 |
|
/* compute intersection */ |
| 205 |
|
(*ofun[sp->so->otype].funp)(sp->so, r)) { |
| 206 |
|
if (sp->sa.success >= 0) |
| 207 |
|
sp->sa.success++; |
| 208 |
< |
raycont(r); /* compute contribution */ |
| 208 |
> |
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
| 209 |
> |
goto nomat; |
| 210 |
> |
rayparticipate(r); |
| 211 |
|
return; |
| 212 |
|
} |
| 213 |
+ |
/* we missed our mark! */ |
| 214 |
|
if (sp->sa.success < 0) |
| 215 |
|
return; /* bitched already */ |
| 216 |
|
sp->sa.success -= AIMREQT; |
| 219 |
|
sprintf(errmsg, "aiming failure for light source \"%s\"", |
| 220 |
|
sp->so->oname); |
| 221 |
|
error(WARNING, errmsg); /* issue warning */ |
| 222 |
+ |
return; |
| 223 |
+ |
nomat: |
| 224 |
+ |
objerror(r->ro, USER, "material not found"); |
| 225 |
|
} |
| 226 |
|
|
| 227 |
|
|
| 228 |
+ |
int |
| 229 |
|
sourcehit(r) /* check to see if ray hit distant source */ |
| 230 |
|
register RAY *r; |
| 231 |
|
{ |
| 251 |
|
} |
| 252 |
|
|
| 253 |
|
if (r->ro != NULL) { |
| 254 |
+ |
r->robj = objndx(r->ro); |
| 255 |
|
for (i = 0; i < 3; i++) |
| 256 |
|
r->ron[i] = -r->rdir[i]; |
| 257 |
|
r->rod = 1.0; |
| 258 |
+ |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
| 259 |
+ |
r->uv[0] = r->uv[1] = 0.0; |
| 260 |
|
r->rox = NULL; |
| 261 |
|
return(1); |
| 262 |
|
} |
| 276 |
|
} |
| 277 |
|
|
| 278 |
|
|
| 279 |
+ |
void |
| 280 |
|
direct(r, f, p) /* add direct component */ |
| 281 |
|
RAY *r; /* ray that hit surface */ |
| 282 |
< |
int (*f)(); /* direct component coefficient function */ |
| 282 |
> |
void (*f)(); /* direct component coefficient function */ |
| 283 |
|
char *p; /* data for f */ |
| 284 |
|
{ |
| 285 |
< |
extern int (*trace)(); |
| 285 |
> |
extern void (*trace)(); |
| 286 |
|
register int sn; |
| 287 |
|
register CONTRIB *scp; |
| 288 |
|
SRCINDEX si; |
| 298 |
|
for (sn = 0; srcray(&sr, r, &si); sn++) { |
| 299 |
|
if (sn >= maxcntr) { |
| 300 |
|
maxcntr = sn + MAXSPART; |
| 301 |
< |
srccnt = (CONTRIB *)realloc((char *)srccnt, |
| 301 |
> |
srccnt = (CONTRIB *)realloc((void *)srccnt, |
| 302 |
|
maxcntr*sizeof(CONTRIB)); |
| 303 |
< |
cntord = (CNTPTR *)realloc((char *)cntord, |
| 303 |
> |
cntord = (CNTPTR *)realloc((void *)cntord, |
| 304 |
|
maxcntr*sizeof(CNTPTR)); |
| 305 |
< |
if (srccnt == NULL | cntord == NULL) |
| 305 |
> |
if ((srccnt == NULL) | (cntord == NULL)) |
| 306 |
|
error(SYSTEM, "out of memory in direct"); |
| 307 |
|
} |
| 308 |
|
cntord[sn].sndx = sn; |
| 360 |
|
rayorigin(&sr, r, SHADOW, 1.0); |
| 361 |
|
VCOPY(sr.rdir, scp->dir); |
| 362 |
|
sr.rsrc = scp->sno; |
| 363 |
< |
source[scp->sno].ntests++; /* keep statistics */ |
| 363 |
> |
/* keep statistics */ |
| 364 |
> |
if (source[scp->sno].ntests++ > 0xfffffff0) { |
| 365 |
> |
source[scp->sno].ntests >>= 1; |
| 366 |
> |
source[scp->sno].nhits >>= 1; |
| 367 |
> |
} |
| 368 |
|
if (localhit(&sr, &thescene) && |
| 369 |
|
( sr.ro != source[scp->sno].so || |
| 370 |
|
source[scp->sno].sflags & SFOLLOW )) { |
| 371 |
|
/* follow entire path */ |
| 372 |
|
raycont(&sr); |
| 373 |
+ |
rayparticipate(&sr); |
| 374 |
|
if (trace != NULL) |
| 375 |
|
(*trace)(&sr); /* trace execution */ |
| 376 |
|
if (bright(sr.rcol) <= FTINY) |
| 406 |
|
} |
| 407 |
|
|
| 408 |
|
|
| 409 |
+ |
void |
| 410 |
+ |
srcscatter(r) /* compute source scattering into ray */ |
| 411 |
+ |
register RAY *r; |
| 412 |
+ |
{ |
| 413 |
+ |
int oldsampndx; |
| 414 |
+ |
int nsamps; |
| 415 |
+ |
RAY sr; |
| 416 |
+ |
SRCINDEX si; |
| 417 |
+ |
double t, d; |
| 418 |
+ |
double re, ge, be; |
| 419 |
+ |
COLOR cvext; |
| 420 |
+ |
int i, j; |
| 421 |
+ |
|
| 422 |
+ |
if (r->slights == NULL || r->slights[0] == 0 |
| 423 |
+ |
|| r->gecc >= 1.-FTINY || r->rot >= FHUGE) |
| 424 |
+ |
return; |
| 425 |
+ |
if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) |
| 426 |
+ |
nsamps = 1; |
| 427 |
+ |
#if MAXSSAMP |
| 428 |
+ |
else if (nsamps > MAXSSAMP) |
| 429 |
+ |
nsamps = MAXSSAMP; |
| 430 |
+ |
#endif |
| 431 |
+ |
oldsampndx = samplendx; |
| 432 |
+ |
samplendx = random()&0x7fff; /* randomize */ |
| 433 |
+ |
for (i = r->slights[0]; i > 0; i--) { /* for each source */ |
| 434 |
+ |
for (j = 0; j < nsamps; j++) { /* for each sample position */ |
| 435 |
+ |
samplendx++; |
| 436 |
+ |
t = r->rot * (j+frandom())/nsamps; |
| 437 |
+ |
/* extinction */ |
| 438 |
+ |
re = t*colval(r->cext,RED); |
| 439 |
+ |
ge = t*colval(r->cext,GRN); |
| 440 |
+ |
be = t*colval(r->cext,BLU); |
| 441 |
+ |
setcolor(cvext, re > 92. ? 0. : exp(-re), |
| 442 |
+ |
ge > 92. ? 0. : exp(-ge), |
| 443 |
+ |
be > 92. ? 0. : exp(-be)); |
| 444 |
+ |
if (intens(cvext) <= FTINY) |
| 445 |
+ |
break; /* too far away */ |
| 446 |
+ |
sr.rorg[0] = r->rorg[0] + r->rdir[0]*t; |
| 447 |
+ |
sr.rorg[1] = r->rorg[1] + r->rdir[1]*t; |
| 448 |
+ |
sr.rorg[2] = r->rorg[2] + r->rdir[2]*t; |
| 449 |
+ |
sr.rmax = 0.; |
| 450 |
+ |
initsrcindex(&si); /* sample ray to this source */ |
| 451 |
+ |
si.sn = r->slights[i]; |
| 452 |
+ |
nopart(&si, &sr); |
| 453 |
+ |
if (!srcray(&sr, NULL, &si) || |
| 454 |
+ |
sr.rsrc != r->slights[i]) |
| 455 |
+ |
continue; /* no path */ |
| 456 |
+ |
copycolor(sr.cext, r->cext); |
| 457 |
+ |
copycolor(sr.albedo, r->albedo); |
| 458 |
+ |
sr.gecc = r->gecc; |
| 459 |
+ |
sr.slights = r->slights; |
| 460 |
+ |
rayvalue(&sr); /* eval. source ray */ |
| 461 |
+ |
if (bright(sr.rcol) <= FTINY) |
| 462 |
+ |
continue; |
| 463 |
+ |
if (r->gecc <= FTINY) /* compute P(theta) */ |
| 464 |
+ |
d = 1.; |
| 465 |
+ |
else { |
| 466 |
+ |
d = DOT(r->rdir, sr.rdir); |
| 467 |
+ |
d = 1. + r->gecc*r->gecc - 2.*r->gecc*d; |
| 468 |
+ |
d = (1. - r->gecc*r->gecc) / (d*sqrt(d)); |
| 469 |
+ |
} |
| 470 |
+ |
/* other factors */ |
| 471 |
+ |
d *= si.dom * r->rot / (4.*PI*nsamps); |
| 472 |
+ |
multcolor(sr.rcol, r->cext); |
| 473 |
+ |
multcolor(sr.rcol, r->albedo); |
| 474 |
+ |
scalecolor(sr.rcol, d); |
| 475 |
+ |
multcolor(sr.rcol, cvext); |
| 476 |
+ |
addcolor(r->rcol, sr.rcol); /* add it in */ |
| 477 |
+ |
} |
| 478 |
+ |
} |
| 479 |
+ |
samplendx = oldsampndx; |
| 480 |
+ |
} |
| 481 |
+ |
|
| 482 |
+ |
|
| 483 |
|
/**************************************************************** |
| 484 |
|
* The following macros were separated from the m_light() routine |
| 485 |
|
* because they are very nasty and difficult to understand. |
| 496 |
|
* geometry behind (or inside) an effective radiator. |
| 497 |
|
*/ |
| 498 |
|
|
| 499 |
< |
static int weaksrcmod(obj) int obj; /* efficiency booster function */ |
| 500 |
< |
{register OBJREC *o = objptr(obj); |
| 501 |
< |
return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} |
| 499 |
> |
static int |
| 500 |
> |
weaksrcmat(int obj) /* identify material */ |
| 501 |
> |
{ |
| 502 |
> |
register OBJREC *o = objptr(obj); |
| 503 |
> |
|
| 504 |
> |
while (!ismaterial(o->otype)) /* find material */ |
| 505 |
> |
o = objptr(o->omod); |
| 506 |
> |
return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW)); |
| 507 |
> |
} |
| 508 |
|
|
| 509 |
|
#define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ |
| 510 |
|
r->rod > 0.0 && \ |
| 511 |
< |
weaksrcmod(source[r->rsrc].so->omod)) |
| 511 |
> |
weaksrcmat(source[r->rsrc].so->omod)) |
| 512 |
|
|
| 513 |
|
/* wrongsource * |
| 514 |
|
* |
| 527 |
|
* (Glows with negative radii should NEVER participate in illumination.) |
| 528 |
|
*/ |
| 529 |
|
|
| 530 |
< |
#define distglow(m, r) (m->otype==MAT_GLOW && \ |
| 530 |
> |
#define distglow(m, r, d) (m->otype==MAT_GLOW && \ |
| 531 |
|
m->oargs.farg[3] >= -FTINY && \ |
| 532 |
< |
r->rot > m->oargs.farg[3]) |
| 532 |
> |
d > m->oargs.farg[3]) |
| 533 |
|
|
| 534 |
|
/* badcomponent * |
| 535 |
|
* |
| 542 |
|
|
| 543 |
|
#define badcomponent(m, r) (r->crtype&(AMBIENT|SPECULAR) && \ |
| 544 |
|
!(r->crtype&SHADOW || r->rod < 0.0 || \ |
| 545 |
< |
distglow(m, r))) |
| 545 |
> |
/* not 100% correct */ distglow(m, r, r->rot))) |
| 546 |
|
|
| 547 |
|
/* passillum * |
| 548 |
|
* |
| 560 |
|
* The -dv flag is normally on for sources to be visible. |
| 561 |
|
*/ |
| 562 |
|
|
| 563 |
< |
#define srcignore(m, r) (!directvis && !(r->crtype&SHADOW) && \ |
| 564 |
< |
!distglow(m, r)) |
| 563 |
> |
#define srcignore(m, r) !(directvis || r->crtype&SHADOW || \ |
| 564 |
> |
distglow(m, r, raydist(r,PRIMARY))) |
| 565 |
|
|
| 566 |
|
|
| 567 |
+ |
int |
| 568 |
|
m_light(m, r) /* ray hit a light source */ |
| 569 |
|
register OBJREC *m; |
| 570 |
|
register RAY *r; |
| 571 |
|
{ |
| 572 |
|
/* check for over-counting */ |
| 573 |
|
if (badcomponent(m, r)) |
| 574 |
< |
return; |
| 575 |
< |
if (wrongsource(m,r)) |
| 576 |
< |
return; |
| 574 |
> |
return(1); |
| 575 |
> |
if (wrongsource(m, r)) |
| 576 |
> |
return(1); |
| 577 |
|
/* check for passed illum */ |
| 578 |
|
if (passillum(m, r)) { |
| 579 |
< |
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
| 580 |
< |
raytrans(r); |
| 581 |
< |
else |
| 582 |
< |
rayshade(r, modifier(m->oargs.sarg[0])); |
| 450 |
< |
return; |
| 579 |
> |
if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) |
| 580 |
> |
return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0]))); |
| 581 |
> |
raytrans(r); |
| 582 |
> |
return(1); |
| 583 |
|
} |
| 584 |
|
/* otherwise treat as source */ |
| 585 |
|
/* check for behind */ |
| 586 |
|
if (r->rod < 0.0) |
| 587 |
< |
return; |
| 587 |
> |
return(1); |
| 588 |
|
/* check for invisibility */ |
| 589 |
|
if (srcignore(m, r)) |
| 590 |
< |
return; |
| 590 |
> |
return(1); |
| 591 |
|
/* check for outside spot */ |
| 592 |
< |
if (m->otype==MAT_SPOT && spotout(r, makespot(m), r->rot>=FHUGE)) |
| 593 |
< |
return; |
| 592 |
> |
if (m->otype==MAT_SPOT && spotout(r, makespot(m))) |
| 593 |
> |
return(1); |
| 594 |
|
/* get distribution pattern */ |
| 595 |
|
raytexture(r, m->omod); |
| 596 |
|
/* get source color */ |
| 599 |
|
m->oargs.farg[2]); |
| 600 |
|
/* modify value */ |
| 601 |
|
multcolor(r->rcol, r->pcol); |
| 602 |
+ |
return(1); |
| 603 |
|
} |