| 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; |
| 74 |
|
o->otype != OBJ_SOURCE && |
| 75 |
|
m->oargs.farg[3] <= FTINY) |
| 76 |
|
continue; /* don't bother */ |
| 77 |
+ |
if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY && |
| 78 |
+ |
m->oargs.farg[2] <= FTINY) |
| 79 |
+ |
continue; /* don't bother */ |
| 80 |
|
|
| 81 |
|
if (sfun[o->otype].of == NULL || |
| 82 |
|
sfun[o->otype].of->setsrc == NULL) |
| 123 |
|
} |
| 124 |
|
|
| 125 |
|
|
| 126 |
+ |
void |
| 127 |
+ |
freesources() /* free all source structures */ |
| 128 |
+ |
{ |
| 129 |
+ |
if (nsources > 0) { |
| 130 |
+ |
free((void *)source); |
| 131 |
+ |
source = NULL; |
| 132 |
+ |
nsources = 0; |
| 133 |
+ |
} |
| 134 |
+ |
if (maxcntr <= 0) |
| 135 |
+ |
return; |
| 136 |
+ |
free((void *)srccnt); |
| 137 |
+ |
srccnt = NULL; |
| 138 |
+ |
free((void *)cntord); |
| 139 |
+ |
cntord = NULL; |
| 140 |
+ |
maxcntr = 0; |
| 141 |
+ |
} |
| 142 |
+ |
|
| 143 |
+ |
|
| 144 |
+ |
int |
| 145 |
|
srcray(sr, r, si) /* send a ray to a source, return domega */ |
| 146 |
|
register RAY *sr; /* returned source ray */ |
| 147 |
|
RAY *r; /* ray which hit object */ |
| 156 |
|
sr->rsrc = si->sn; /* remember source */ |
| 157 |
|
srcp = source + si->sn; |
| 158 |
|
if (srcp->sflags & SDISTANT) { |
| 159 |
< |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s, 1)) |
| 159 |
> |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) |
| 160 |
|
continue; |
| 161 |
|
return(1); /* sample OK */ |
| 162 |
|
} |
| 166 |
|
continue; |
| 167 |
|
/* check angle */ |
| 168 |
|
if (srcp->sflags & SSPOT) { |
| 169 |
< |
if (spotout(sr, srcp->sl.s, 0)) |
| 169 |
> |
if (spotout(sr, srcp->sl.s)) |
| 170 |
|
continue; |
| 171 |
|
/* adjust solid angle */ |
| 172 |
|
si->dom *= d*d; |
| 179 |
|
} |
| 180 |
|
|
| 181 |
|
|
| 182 |
+ |
void |
| 183 |
|
srcvalue(r) /* punch ray to source and compute value */ |
| 184 |
< |
RAY *r; |
| 184 |
> |
register RAY *r; |
| 185 |
|
{ |
| 186 |
|
register SRCREC *sp; |
| 187 |
|
|
| 190 |
|
/* check intersection */ |
| 191 |
|
if (!(*ofun[sp->so->otype].funp)(sp->so, r)) |
| 192 |
|
return; |
| 193 |
< |
raycont(r); /* compute contribution */ |
| 193 |
> |
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
| 194 |
> |
goto nomat; |
| 195 |
> |
rayparticipate(r); |
| 196 |
|
return; |
| 197 |
|
} |
| 198 |
|
/* compute intersection */ |
| 200 |
|
(*ofun[sp->so->otype].funp)(sp->so, r)) { |
| 201 |
|
if (sp->sa.success >= 0) |
| 202 |
|
sp->sa.success++; |
| 203 |
< |
raycont(r); /* compute contribution */ |
| 203 |
> |
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
| 204 |
> |
goto nomat; |
| 205 |
> |
rayparticipate(r); |
| 206 |
|
return; |
| 207 |
|
} |
| 208 |
+ |
/* we missed our mark! */ |
| 209 |
|
if (sp->sa.success < 0) |
| 210 |
|
return; /* bitched already */ |
| 211 |
|
sp->sa.success -= AIMREQT; |
| 214 |
|
sprintf(errmsg, "aiming failure for light source \"%s\"", |
| 215 |
|
sp->so->oname); |
| 216 |
|
error(WARNING, errmsg); /* issue warning */ |
| 217 |
+ |
return; |
| 218 |
+ |
nomat: |
| 219 |
+ |
objerror(r->ro, USER, "material not found"); |
| 220 |
|
} |
| 221 |
|
|
| 222 |
|
|
| 223 |
+ |
int |
| 224 |
|
sourcehit(r) /* check to see if ray hit distant source */ |
| 225 |
|
register RAY *r; |
| 226 |
|
{ |
| 246 |
|
} |
| 247 |
|
|
| 248 |
|
if (r->ro != NULL) { |
| 249 |
+ |
r->robj = objndx(r->ro); |
| 250 |
|
for (i = 0; i < 3; i++) |
| 251 |
|
r->ron[i] = -r->rdir[i]; |
| 252 |
|
r->rod = 1.0; |
| 269 |
|
} |
| 270 |
|
|
| 271 |
|
|
| 272 |
+ |
void |
| 273 |
|
direct(r, f, p) /* add direct component */ |
| 274 |
|
RAY *r; /* ray that hit surface */ |
| 275 |
< |
int (*f)(); /* direct component coefficient function */ |
| 275 |
> |
void (*f)(); /* direct component coefficient function */ |
| 276 |
|
char *p; /* data for f */ |
| 277 |
|
{ |
| 278 |
< |
extern int (*trace)(); |
| 278 |
> |
extern void (*trace)(); |
| 279 |
|
register int sn; |
| 280 |
|
register CONTRIB *scp; |
| 281 |
|
SRCINDEX si; |
| 359 |
|
source[scp->sno].sflags & SFOLLOW )) { |
| 360 |
|
/* follow entire path */ |
| 361 |
|
raycont(&sr); |
| 362 |
+ |
rayparticipate(&sr); |
| 363 |
|
if (trace != NULL) |
| 364 |
|
(*trace)(&sr); /* trace execution */ |
| 365 |
|
if (bright(sr.rcol) <= FTINY) |
| 395 |
|
} |
| 396 |
|
|
| 397 |
|
|
| 398 |
+ |
void |
| 399 |
+ |
srcscatter(r) /* compute source scattering into ray */ |
| 400 |
+ |
register RAY *r; |
| 401 |
+ |
{ |
| 402 |
+ |
int oldsampndx; |
| 403 |
+ |
int nsamps; |
| 404 |
+ |
RAY sr; |
| 405 |
+ |
SRCINDEX si; |
| 406 |
+ |
double t, d; |
| 407 |
+ |
double re, ge, be; |
| 408 |
+ |
COLOR cvext; |
| 409 |
+ |
int i, j; |
| 410 |
+ |
|
| 411 |
+ |
if (r->slights == NULL || r->slights[0] == 0 |
| 412 |
+ |
|| r->gecc >= 1.-FTINY || r->rot >= FHUGE) |
| 413 |
+ |
return; |
| 414 |
+ |
if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) |
| 415 |
+ |
nsamps = 1; |
| 416 |
+ |
#if MAXSSAMP |
| 417 |
+ |
else if (nsamps > MAXSSAMP) |
| 418 |
+ |
nsamps = MAXSSAMP; |
| 419 |
+ |
#endif |
| 420 |
+ |
oldsampndx = samplendx; |
| 421 |
+ |
samplendx = random()&0x7fff; /* randomize */ |
| 422 |
+ |
for (i = r->slights[0]; i > 0; i--) { /* for each source */ |
| 423 |
+ |
for (j = 0; j < nsamps; j++) { /* for each sample position */ |
| 424 |
+ |
samplendx++; |
| 425 |
+ |
t = r->rot * (j+frandom())/nsamps; |
| 426 |
+ |
/* extinction */ |
| 427 |
+ |
re = t*colval(r->cext,RED); |
| 428 |
+ |
ge = t*colval(r->cext,GRN); |
| 429 |
+ |
be = t*colval(r->cext,BLU); |
| 430 |
+ |
setcolor(cvext, re > 92. ? 0. : exp(-re), |
| 431 |
+ |
ge > 92. ? 0. : exp(-ge), |
| 432 |
+ |
be > 92. ? 0. : exp(-be)); |
| 433 |
+ |
if (intens(cvext) <= FTINY) |
| 434 |
+ |
break; /* too far away */ |
| 435 |
+ |
sr.rorg[0] = r->rorg[0] + r->rdir[0]*t; |
| 436 |
+ |
sr.rorg[1] = r->rorg[1] + r->rdir[1]*t; |
| 437 |
+ |
sr.rorg[2] = r->rorg[2] + r->rdir[2]*t; |
| 438 |
+ |
sr.rmax = 0.; |
| 439 |
+ |
initsrcindex(&si); /* sample ray to this source */ |
| 440 |
+ |
si.sn = r->slights[i]; |
| 441 |
+ |
nopart(&si, &sr); |
| 442 |
+ |
if (!srcray(&sr, NULL, &si) || |
| 443 |
+ |
sr.rsrc != r->slights[i]) |
| 444 |
+ |
continue; /* no path */ |
| 445 |
+ |
copycolor(sr.cext, r->cext); |
| 446 |
+ |
copycolor(sr.albedo, r->albedo); |
| 447 |
+ |
sr.gecc = r->gecc; |
| 448 |
+ |
sr.slights = r->slights; |
| 449 |
+ |
rayvalue(&sr); /* eval. source ray */ |
| 450 |
+ |
if (bright(sr.rcol) <= FTINY) |
| 451 |
+ |
continue; |
| 452 |
+ |
if (r->gecc <= FTINY) /* compute P(theta) */ |
| 453 |
+ |
d = 1.; |
| 454 |
+ |
else { |
| 455 |
+ |
d = DOT(r->rdir, sr.rdir); |
| 456 |
+ |
d = 1. + r->gecc*r->gecc - 2.*r->gecc*d; |
| 457 |
+ |
d = (1. - r->gecc*r->gecc) / (d*sqrt(d)); |
| 458 |
+ |
} |
| 459 |
+ |
/* other factors */ |
| 460 |
+ |
d *= si.dom * r->rot / (4.*PI*nsamps); |
| 461 |
+ |
multcolor(sr.rcol, r->cext); |
| 462 |
+ |
multcolor(sr.rcol, r->albedo); |
| 463 |
+ |
scalecolor(sr.rcol, d); |
| 464 |
+ |
multcolor(sr.rcol, cvext); |
| 465 |
+ |
addcolor(r->rcol, sr.rcol); /* add it in */ |
| 466 |
+ |
} |
| 467 |
+ |
} |
| 468 |
+ |
samplendx = oldsampndx; |
| 469 |
+ |
} |
| 470 |
+ |
|
| 471 |
+ |
|
| 472 |
|
/**************************************************************** |
| 473 |
|
* The following macros were separated from the m_light() routine |
| 474 |
|
* because they are very nasty and difficult to understand. |
| 510 |
|
* (Glows with negative radii should NEVER participate in illumination.) |
| 511 |
|
*/ |
| 512 |
|
|
| 513 |
< |
#define distglow(m, r) (m->otype==MAT_GLOW && \ |
| 513 |
> |
#define distglow(m, r, d) (m->otype==MAT_GLOW && \ |
| 514 |
|
m->oargs.farg[3] >= -FTINY && \ |
| 515 |
< |
r->rot > m->oargs.farg[3]) |
| 515 |
> |
d > m->oargs.farg[3]) |
| 516 |
|
|
| 517 |
|
/* badcomponent * |
| 518 |
|
* |
| 525 |
|
|
| 526 |
|
#define badcomponent(m, r) (r->crtype&(AMBIENT|SPECULAR) && \ |
| 527 |
|
!(r->crtype&SHADOW || r->rod < 0.0 || \ |
| 528 |
< |
distglow(m, r))) |
| 528 |
> |
/* not 100% correct */ distglow(m, r, r->rot))) |
| 529 |
|
|
| 530 |
|
/* passillum * |
| 531 |
|
* |
| 543 |
|
* The -dv flag is normally on for sources to be visible. |
| 544 |
|
*/ |
| 545 |
|
|
| 546 |
< |
#define srcignore(m, r) (!directvis && !(r->crtype&SHADOW) && \ |
| 547 |
< |
!distglow(m, r)) |
| 546 |
> |
#define srcignore(m, r) !(directvis || r->crtype&SHADOW || \ |
| 547 |
> |
distglow(m, r, raydist(r,PRIMARY))) |
| 548 |
|
|
| 549 |
|
|
| 550 |
+ |
int |
| 551 |
|
m_light(m, r) /* ray hit a light source */ |
| 552 |
|
register OBJREC *m; |
| 553 |
|
register RAY *r; |
| 555 |
|
/* check for over-counting */ |
| 556 |
|
if (badcomponent(m, r)) |
| 557 |
|
return(1); |
| 558 |
< |
if (wrongsource(m,r)) |
| 558 |
> |
if (wrongsource(m, r)) |
| 559 |
|
return(1); |
| 560 |
|
/* check for passed illum */ |
| 561 |
|
if (passillum(m, r)) { |
| 562 |
|
if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) |
| 563 |
< |
return(rayshade(r, modifier(m->oargs.sarg[0]))); |
| 563 |
> |
return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0]))); |
| 564 |
|
raytrans(r); |
| 565 |
|
return(1); |
| 566 |
|
} |
| 572 |
|
if (srcignore(m, r)) |
| 573 |
|
return(1); |
| 574 |
|
/* check for outside spot */ |
| 575 |
< |
if (m->otype==MAT_SPOT && spotout(r, makespot(m), r->rot>=FHUGE)) |
| 575 |
> |
if (m->otype==MAT_SPOT && spotout(r, makespot(m))) |
| 576 |
|
return(1); |
| 577 |
|
/* get distribution pattern */ |
| 578 |
|
raytexture(r, m->omod); |