1 |
< |
/* Copyright (c) 1993 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
18 |
|
|
19 |
|
#include "source.h" |
20 |
|
|
21 |
+ |
#include "random.h" |
22 |
+ |
|
23 |
+ |
extern double ssampdist; /* scatter sampling distance */ |
24 |
+ |
|
25 |
+ |
#ifndef MAXSSAMP |
26 |
+ |
#define MAXSSAMP 16 /* maximum samples per ray */ |
27 |
+ |
#endif |
28 |
+ |
|
29 |
|
/* |
30 |
|
* Structures used by direct() |
31 |
|
*/ |
76 |
|
o->otype != OBJ_SOURCE && |
77 |
|
m->oargs.farg[3] <= FTINY) |
78 |
|
continue; /* don't bother */ |
79 |
+ |
if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY && |
80 |
+ |
m->oargs.farg[2] <= FTINY) |
81 |
+ |
continue; /* don't bother */ |
82 |
|
|
83 |
|
if (sfun[o->otype].of == NULL || |
84 |
|
sfun[o->otype].of->setsrc == NULL) |
139 |
|
sr->rsrc = si->sn; /* remember source */ |
140 |
|
srcp = source + si->sn; |
141 |
|
if (srcp->sflags & SDISTANT) { |
142 |
< |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s, 1)) |
142 |
> |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) |
143 |
|
continue; |
144 |
|
return(1); /* sample OK */ |
145 |
|
} |
149 |
|
continue; |
150 |
|
/* check angle */ |
151 |
|
if (srcp->sflags & SSPOT) { |
152 |
< |
if (spotout(sr, srcp->sl.s, 0)) |
152 |
> |
if (spotout(sr, srcp->sl.s)) |
153 |
|
continue; |
154 |
|
/* adjust solid angle */ |
155 |
|
si->dom *= d*d; |
163 |
|
|
164 |
|
|
165 |
|
srcvalue(r) /* punch ray to source and compute value */ |
166 |
< |
RAY *r; |
166 |
> |
register RAY *r; |
167 |
|
{ |
168 |
|
register SRCREC *sp; |
169 |
|
|
172 |
|
/* check intersection */ |
173 |
|
if (!(*ofun[sp->so->otype].funp)(sp->so, r)) |
174 |
|
return; |
175 |
< |
raycont(r); /* compute contribution */ |
175 |
> |
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
176 |
> |
goto nomat; |
177 |
> |
rayparticipate(r); |
178 |
|
return; |
179 |
|
} |
180 |
|
/* compute intersection */ |
182 |
|
(*ofun[sp->so->otype].funp)(sp->so, r)) { |
183 |
|
if (sp->sa.success >= 0) |
184 |
|
sp->sa.success++; |
185 |
< |
raycont(r); /* compute contribution */ |
185 |
> |
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
186 |
> |
goto nomat; |
187 |
> |
rayparticipate(r); |
188 |
|
return; |
189 |
|
} |
190 |
+ |
/* we missed our mark! */ |
191 |
|
if (sp->sa.success < 0) |
192 |
|
return; /* bitched already */ |
193 |
|
sp->sa.success -= AIMREQT; |
196 |
|
sprintf(errmsg, "aiming failure for light source \"%s\"", |
197 |
|
sp->so->oname); |
198 |
|
error(WARNING, errmsg); /* issue warning */ |
199 |
+ |
return; |
200 |
+ |
nomat: |
201 |
+ |
objerror(r->ro, USER, "material not found"); |
202 |
|
} |
203 |
|
|
204 |
|
|
318 |
|
/* modify threshold */ |
319 |
|
ourthresh = shadthresh / r->rweight; |
320 |
|
/* test for shadows */ |
321 |
< |
nhits = 0; |
322 |
< |
for (sn = 0; sn < ncnts; sn++) { |
321 |
> |
for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts; |
322 |
> |
hwt += (double)source[scp->sno].nhits / |
323 |
> |
(double)source[scp->sno].ntests, |
324 |
> |
sn++) { |
325 |
|
/* check threshold */ |
326 |
|
if ((sn+nshadcheck>=ncnts ? cntord[sn].brt : |
327 |
|
cntord[sn].brt-cntord[sn+nshadcheck].brt) |
338 |
|
source[scp->sno].sflags & SFOLLOW )) { |
339 |
|
/* follow entire path */ |
340 |
|
raycont(&sr); |
341 |
+ |
rayparticipate(&sr); |
342 |
|
if (trace != NULL) |
343 |
|
(*trace)(&sr); /* trace execution */ |
344 |
|
if (bright(sr.rcol) <= FTINY) |
351 |
|
nhits++; |
352 |
|
source[scp->sno].nhits++; |
353 |
|
} |
354 |
< |
/* surface hit rate */ |
355 |
< |
if (sn > 0) |
356 |
< |
hwt = (double)nhits / (double)sn; |
354 |
> |
/* source hit rate */ |
355 |
> |
if (hwt > FTINY) |
356 |
> |
hwt = (double)nhits / hwt; |
357 |
|
else |
358 |
|
hwt = 0.5; |
359 |
|
#ifdef DEBUG |
360 |
< |
sprintf(errmsg, "%d tested, %d untested, %f hit rate\n", |
360 |
> |
sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n", |
361 |
|
sn, ncnts-sn, hwt); |
362 |
|
eputs(errmsg); |
363 |
|
#endif |
366 |
|
scp = srccnt + cntord[sn].sndx; |
367 |
|
prob = hwt * (double)source[scp->sno].nhits / |
368 |
|
(double)source[scp->sno].ntests; |
369 |
+ |
if (prob > 1.0) |
370 |
+ |
prob = 1.0; |
371 |
|
scalecolor(scp->val, prob); |
372 |
|
addcolor(r->rcol, scp->val); |
373 |
|
} |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
+ |
srcscatter(r) /* compute source scattering into ray */ |
378 |
+ |
register RAY *r; |
379 |
+ |
{ |
380 |
+ |
int oldsampndx; |
381 |
+ |
int nsamps; |
382 |
+ |
RAY sr; |
383 |
+ |
SRCINDEX si; |
384 |
+ |
double t, d; |
385 |
+ |
double re, ge, be; |
386 |
+ |
COLOR cvext; |
387 |
+ |
int i, j; |
388 |
+ |
|
389 |
+ |
if (r->slights == NULL || r->slights[0] == 0 |
390 |
+ |
|| r->gecc >= 1.-FTINY || r->rot >= FHUGE) |
391 |
+ |
return; |
392 |
+ |
if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) |
393 |
+ |
nsamps = 1; |
394 |
+ |
#if MAXSSAMP |
395 |
+ |
else if (nsamps > MAXSSAMP) |
396 |
+ |
nsamps = MAXSSAMP; |
397 |
+ |
#endif |
398 |
+ |
oldsampndx = samplendx; |
399 |
+ |
samplendx = random()&0x7fff; /* randomize */ |
400 |
+ |
for (i = r->slights[0]; i > 0; i--) { /* for each source */ |
401 |
+ |
for (j = 0; j < nsamps; j++) { /* for each sample position */ |
402 |
+ |
samplendx++; |
403 |
+ |
t = r->rot * (j+frandom())/nsamps; |
404 |
+ |
/* extinction */ |
405 |
+ |
re = t*colval(r->cext,RED); |
406 |
+ |
ge = t*colval(r->cext,GRN); |
407 |
+ |
be = t*colval(r->cext,BLU); |
408 |
+ |
setcolor(cvext, re > 92. ? 0. : exp(-re), |
409 |
+ |
ge > 92. ? 0. : exp(-ge), |
410 |
+ |
be > 92. ? 0. : exp(-be)); |
411 |
+ |
if (intens(cvext) <= FTINY) |
412 |
+ |
break; /* too far away */ |
413 |
+ |
sr.rorg[0] = r->rorg[0] + r->rdir[0]*t; |
414 |
+ |
sr.rorg[1] = r->rorg[1] + r->rdir[1]*t; |
415 |
+ |
sr.rorg[2] = r->rorg[2] + r->rdir[2]*t; |
416 |
+ |
sr.rmax = 0.; |
417 |
+ |
initsrcindex(&si); /* sample ray to this source */ |
418 |
+ |
si.sn = r->slights[i]; |
419 |
+ |
nopart(&si, &sr); |
420 |
+ |
if (!srcray(&sr, NULL, &si) || |
421 |
+ |
sr.rsrc != r->slights[i]) |
422 |
+ |
continue; /* no path */ |
423 |
+ |
copycolor(sr.cext, r->cext); |
424 |
+ |
copycolor(sr.albedo, r->albedo); |
425 |
+ |
sr.gecc = r->gecc; |
426 |
+ |
sr.slights = r->slights; |
427 |
+ |
rayvalue(&sr); /* eval. source ray */ |
428 |
+ |
if (bright(sr.rcol) <= FTINY) |
429 |
+ |
continue; |
430 |
+ |
if (r->gecc <= FTINY) /* compute P(theta) */ |
431 |
+ |
d = 1.; |
432 |
+ |
else { |
433 |
+ |
d = DOT(r->rdir, sr.rdir); |
434 |
+ |
d = 1. + r->gecc*r->gecc - 2.*r->gecc*d; |
435 |
+ |
d = (1. - r->gecc*r->gecc) / (d*sqrt(d)); |
436 |
+ |
} |
437 |
+ |
/* other factors */ |
438 |
+ |
d *= si.dom * r->rot / (4.*PI*nsamps); |
439 |
+ |
multcolor(sr.rcol, r->cext); |
440 |
+ |
multcolor(sr.rcol, r->albedo); |
441 |
+ |
scalecolor(sr.rcol, d); |
442 |
+ |
multcolor(sr.rcol, cvext); |
443 |
+ |
addcolor(r->rcol, sr.rcol); /* add it in */ |
444 |
+ |
} |
445 |
+ |
} |
446 |
+ |
samplendx = oldsampndx; |
447 |
+ |
} |
448 |
+ |
|
449 |
+ |
|
450 |
|
/**************************************************************** |
451 |
|
* The following macros were separated from the m_light() routine |
452 |
|
* because they are very nasty and difficult to understand. |
468 |
|
return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} |
469 |
|
|
470 |
|
#define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ |
471 |
+ |
r->rod > 0.0 && \ |
472 |
|
weaksrcmod(source[r->rsrc].so->omod)) |
473 |
|
|
474 |
|
/* wrongsource * |
488 |
|
* (Glows with negative radii should NEVER participate in illumination.) |
489 |
|
*/ |
490 |
|
|
491 |
< |
#define distglow(m, r) (m->otype==MAT_GLOW && \ |
491 |
> |
#define distglow(m, r, d) (m->otype==MAT_GLOW && \ |
492 |
|
m->oargs.farg[3] >= -FTINY && \ |
493 |
< |
r->rot > m->oargs.farg[3]) |
493 |
> |
d > m->oargs.farg[3]) |
494 |
|
|
495 |
|
/* badcomponent * |
496 |
|
* |
503 |
|
|
504 |
|
#define badcomponent(m, r) (r->crtype&(AMBIENT|SPECULAR) && \ |
505 |
|
!(r->crtype&SHADOW || r->rod < 0.0 || \ |
506 |
< |
distglow(m, r))) |
506 |
> |
/* not 100% correct */ distglow(m, r, r->rot))) |
507 |
|
|
410 |
– |
/* overcount * |
411 |
– |
* |
412 |
– |
* All overcounting possibilities are contained here. |
413 |
– |
*/ |
414 |
– |
|
415 |
– |
#define overcount(m, r) (badcomponent(m,r) || wrongsource(m,r)) |
416 |
– |
|
508 |
|
/* passillum * |
509 |
|
* |
510 |
|
* An illum passes to another material type when we didn't hit it |
521 |
|
* The -dv flag is normally on for sources to be visible. |
522 |
|
*/ |
523 |
|
|
524 |
< |
#define srcignore(m, r) (!directvis && !(r->crtype&SHADOW) && \ |
525 |
< |
!distglow(m, r)) |
524 |
> |
#define srcignore(m, r) !(directvis || r->crtype&SHADOW || \ |
525 |
> |
distglow(m, r, raydist(r,PRIMARY))) |
526 |
|
|
527 |
|
|
528 |
|
m_light(m, r) /* ray hit a light source */ |
530 |
|
register RAY *r; |
531 |
|
{ |
532 |
|
/* check for over-counting */ |
533 |
< |
if (overcount(m, r)) |
534 |
< |
return; |
533 |
> |
if (badcomponent(m, r)) |
534 |
> |
return(1); |
535 |
> |
if (wrongsource(m, r)) |
536 |
> |
return(1); |
537 |
|
/* check for passed illum */ |
538 |
|
if (passillum(m, r)) { |
539 |
< |
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
540 |
< |
raytrans(r); |
541 |
< |
else |
542 |
< |
rayshade(r, modifier(m->oargs.sarg[0])); |
450 |
< |
return; |
539 |
> |
if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) |
540 |
> |
return(rayshade(r, modifier(m->oargs.sarg[0]))); |
541 |
> |
raytrans(r); |
542 |
> |
return(1); |
543 |
|
} |
544 |
|
/* otherwise treat as source */ |
545 |
|
/* check for behind */ |
546 |
|
if (r->rod < 0.0) |
547 |
< |
return; |
547 |
> |
return(1); |
548 |
|
/* check for invisibility */ |
549 |
|
if (srcignore(m, r)) |
550 |
< |
return; |
550 |
> |
return(1); |
551 |
|
/* check for outside spot */ |
552 |
< |
if (m->otype==MAT_SPOT && spotout(r, makespot(m), r->rot>=FHUGE)) |
553 |
< |
return; |
552 |
> |
if (m->otype==MAT_SPOT && spotout(r, makespot(m))) |
553 |
> |
return(1); |
554 |
|
/* get distribution pattern */ |
555 |
|
raytexture(r, m->omod); |
556 |
|
/* get source color */ |
559 |
|
m->oargs.farg[2]); |
560 |
|
/* modify value */ |
561 |
|
multcolor(r->rcol, r->pcol); |
562 |
+ |
return(1); |
563 |
|
} |