1 |
< |
/* Copyright (c) 1992 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 |
|
/* |
26 |
|
* Structures used by direct() |
27 |
|
*/ |
72 |
|
o->otype != OBJ_SOURCE && |
73 |
|
m->oargs.farg[3] <= FTINY) |
74 |
|
continue; /* don't bother */ |
75 |
+ |
if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY && |
76 |
+ |
m->oargs.farg[2] <= FTINY) |
77 |
+ |
continue; /* don't bother */ |
78 |
|
|
79 |
|
if (sfun[o->otype].of == NULL || |
80 |
|
sfun[o->otype].of->setsrc == NULL) |
88 |
|
if (m->otype == MAT_GLOW) { |
89 |
|
source[ns].sflags |= SPROX; |
90 |
|
source[ns].sl.prox = m->oargs.farg[3]; |
91 |
< |
if (o->otype == OBJ_SOURCE) |
91 |
> |
if (source[ns].sflags & SDISTANT) |
92 |
|
source[ns].sflags |= SSKIP; |
93 |
|
} else if (m->otype == MAT_SPOT) { |
94 |
|
source[ns].sflags |= SSPOT; |
135 |
|
sr->rsrc = si->sn; /* remember source */ |
136 |
|
srcp = source + si->sn; |
137 |
|
if (srcp->sflags & SDISTANT) { |
138 |
< |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s, 1)) |
138 |
> |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) |
139 |
|
continue; |
140 |
|
return(1); /* sample OK */ |
141 |
|
} |
145 |
|
continue; |
146 |
|
/* check angle */ |
147 |
|
if (srcp->sflags & SSPOT) { |
148 |
< |
if (spotout(sr, srcp->sl.s, 0)) |
148 |
> |
if (spotout(sr, srcp->sl.s)) |
149 |
|
continue; |
150 |
|
/* adjust solid angle */ |
151 |
|
si->dom *= d*d; |
159 |
|
|
160 |
|
|
161 |
|
srcvalue(r) /* punch ray to source and compute value */ |
162 |
< |
RAY *r; |
162 |
> |
register RAY *r; |
163 |
|
{ |
164 |
|
register SRCREC *sp; |
165 |
|
|
168 |
|
/* check intersection */ |
169 |
|
if (!(*ofun[sp->so->otype].funp)(sp->so, r)) |
170 |
|
return; |
171 |
< |
raycont(r); /* compute contribution */ |
171 |
> |
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
172 |
> |
goto nomat; |
173 |
> |
rayparticipate(r); |
174 |
|
return; |
175 |
|
} |
176 |
|
/* compute intersection */ |
178 |
|
(*ofun[sp->so->otype].funp)(sp->so, r)) { |
179 |
|
if (sp->sa.success >= 0) |
180 |
|
sp->sa.success++; |
181 |
< |
raycont(r); /* compute contribution */ |
181 |
> |
if (!rayshade(r, r->ro->omod)) /* compute contribution */ |
182 |
> |
goto nomat; |
183 |
> |
rayparticipate(r); |
184 |
|
return; |
185 |
|
} |
186 |
+ |
/* we missed our mark! */ |
187 |
|
if (sp->sa.success < 0) |
188 |
|
return; /* bitched already */ |
189 |
|
sp->sa.success -= AIMREQT; |
192 |
|
sprintf(errmsg, "aiming failure for light source \"%s\"", |
193 |
|
sp->so->oname); |
194 |
|
error(WARNING, errmsg); /* issue warning */ |
195 |
+ |
return; |
196 |
+ |
nomat: |
197 |
+ |
objerror(r->ro, USER, "material not found"); |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
+ |
sourcehit(r) /* check to see if ray hit distant source */ |
202 |
+ |
register RAY *r; |
203 |
+ |
{ |
204 |
+ |
int first, last; |
205 |
+ |
register int i; |
206 |
+ |
|
207 |
+ |
if (r->rsrc >= 0) { /* check only one if aimed */ |
208 |
+ |
first = last = r->rsrc; |
209 |
+ |
} else { /* otherwise check all */ |
210 |
+ |
first = 0; last = nsources-1; |
211 |
+ |
} |
212 |
+ |
for (i = first; i <= last; i++) |
213 |
+ |
if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT) |
214 |
+ |
/* |
215 |
+ |
* Check to see if ray is within |
216 |
+ |
* solid angle of source. |
217 |
+ |
*/ |
218 |
+ |
if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir)) |
219 |
+ |
<= source[i].ss2) { |
220 |
+ |
r->ro = source[i].so; |
221 |
+ |
if (!(source[i].sflags & SSKIP)) |
222 |
+ |
break; |
223 |
+ |
} |
224 |
+ |
|
225 |
+ |
if (r->ro != NULL) { |
226 |
+ |
for (i = 0; i < 3; i++) |
227 |
+ |
r->ron[i] = -r->rdir[i]; |
228 |
+ |
r->rod = 1.0; |
229 |
+ |
r->rox = NULL; |
230 |
+ |
return(1); |
231 |
+ |
} |
232 |
+ |
return(0); |
233 |
+ |
} |
234 |
+ |
|
235 |
+ |
|
236 |
|
static int |
237 |
|
cntcmp(sc1, sc2) /* contribution compare (descending) */ |
238 |
|
register CNTPTR *sc1, *sc2; |
251 |
|
char *p; /* data for f */ |
252 |
|
{ |
253 |
|
extern int (*trace)(); |
204 |
– |
extern double pow(); |
254 |
|
register int sn; |
255 |
+ |
register CONTRIB *scp; |
256 |
|
SRCINDEX si; |
257 |
|
int nshadcheck, ncnts; |
258 |
|
int nhits; |
274 |
|
error(SYSTEM, "out of memory in direct"); |
275 |
|
} |
276 |
|
cntord[sn].sndx = sn; |
277 |
< |
srccnt[sn].sno = sr.rsrc; |
277 |
> |
scp = srccnt + sn; |
278 |
> |
scp->sno = sr.rsrc; |
279 |
|
/* compute coefficient */ |
280 |
< |
(*f)(srccnt[sn].coef, p, sr.rdir, si.dom); |
281 |
< |
cntord[sn].brt = bright(srccnt[sn].coef); |
280 |
> |
(*f)(scp->coef, p, sr.rdir, si.dom); |
281 |
> |
cntord[sn].brt = bright(scp->coef); |
282 |
|
if (cntord[sn].brt <= 0.0) |
283 |
|
continue; |
284 |
< |
VCOPY(srccnt[sn].dir, sr.rdir); |
284 |
> |
VCOPY(scp->dir, sr.rdir); |
285 |
|
/* compute potential */ |
286 |
|
sr.revf = srcvalue; |
287 |
|
rayvalue(&sr); |
288 |
< |
copycolor(srccnt[sn].val, sr.rcol); |
289 |
< |
multcolor(srccnt[sn].val, srccnt[sn].coef); |
290 |
< |
cntord[sn].brt = bright(srccnt[sn].val); |
288 |
> |
copycolor(scp->val, sr.rcol); |
289 |
> |
multcolor(scp->val, scp->coef); |
290 |
> |
cntord[sn].brt = bright(scp->val); |
291 |
|
} |
292 |
|
/* sort contributions */ |
293 |
|
qsort(cntord, sn, sizeof(CNTPTR), cntcmp); |
314 |
|
/* modify threshold */ |
315 |
|
ourthresh = shadthresh / r->rweight; |
316 |
|
/* test for shadows */ |
317 |
< |
nhits = 0; |
318 |
< |
for (sn = 0; sn < ncnts; sn++) { |
317 |
> |
for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts; |
318 |
> |
hwt += (double)source[scp->sno].nhits / |
319 |
> |
(double)source[scp->sno].ntests, |
320 |
> |
sn++) { |
321 |
|
/* check threshold */ |
322 |
|
if ((sn+nshadcheck>=ncnts ? cntord[sn].brt : |
323 |
|
cntord[sn].brt-cntord[sn+nshadcheck].brt) |
324 |
|
< ourthresh*bright(r->rcol)) |
325 |
|
break; |
326 |
+ |
scp = srccnt + cntord[sn].sndx; |
327 |
|
/* test for hit */ |
328 |
|
rayorigin(&sr, r, SHADOW, 1.0); |
329 |
< |
VCOPY(sr.rdir, srccnt[cntord[sn].sndx].dir); |
330 |
< |
sr.rsrc = srccnt[cntord[sn].sndx].sno; |
331 |
< |
source[sr.rsrc].ntests++; /* keep statistics */ |
329 |
> |
VCOPY(sr.rdir, scp->dir); |
330 |
> |
sr.rsrc = scp->sno; |
331 |
> |
source[scp->sno].ntests++; /* keep statistics */ |
332 |
|
if (localhit(&sr, &thescene) && |
333 |
< |
( sr.ro != source[sr.rsrc].so || |
334 |
< |
source[sr.rsrc].sflags & SFOLLOW )) { |
333 |
> |
( sr.ro != source[scp->sno].so || |
334 |
> |
source[scp->sno].sflags & SFOLLOW )) { |
335 |
|
/* follow entire path */ |
336 |
< |
raycont(&sr); |
336 |
> |
if (!raycont(&sr)) |
337 |
> |
objerror(sr.ro, USER, "material not found"); |
338 |
> |
rayparticipate(&sr); |
339 |
|
if (trace != NULL) |
340 |
|
(*trace)(&sr); /* trace execution */ |
341 |
|
if (bright(sr.rcol) <= FTINY) |
342 |
|
continue; /* missed! */ |
343 |
< |
copycolor(srccnt[cntord[sn].sndx].val, sr.rcol); |
344 |
< |
multcolor(srccnt[cntord[sn].sndx].val, |
289 |
< |
srccnt[cntord[sn].sndx].coef); |
343 |
> |
copycolor(scp->val, sr.rcol); |
344 |
> |
multcolor(scp->val, scp->coef); |
345 |
|
} |
346 |
|
/* add contribution if hit */ |
347 |
< |
addcolor(r->rcol, srccnt[cntord[sn].sndx].val); |
347 |
> |
addcolor(r->rcol, scp->val); |
348 |
|
nhits++; |
349 |
< |
source[sr.rsrc].nhits++; |
349 |
> |
source[scp->sno].nhits++; |
350 |
|
} |
351 |
< |
/* surface hit rate */ |
352 |
< |
if (sn > 0) |
353 |
< |
hwt = (double)nhits / (double)sn; |
351 |
> |
/* source hit rate */ |
352 |
> |
if (hwt > FTINY) |
353 |
> |
hwt = (double)nhits / hwt; |
354 |
|
else |
355 |
|
hwt = 0.5; |
356 |
|
#ifdef DEBUG |
357 |
< |
sprintf(errmsg, "%d tested, %d untested, %f hit rate\n", |
357 |
> |
sprintf(errmsg, "%d tested, %d untested, %f conditional hit rate\n", |
358 |
|
sn, ncnts-sn, hwt); |
359 |
|
eputs(errmsg); |
360 |
|
#endif |
361 |
|
/* add in untested sources */ |
362 |
|
for ( ; sn < ncnts; sn++) { |
363 |
< |
sr.rsrc = srccnt[cntord[sn].sndx].sno; |
364 |
< |
prob = hwt * (double)source[sr.rsrc].nhits / |
365 |
< |
(double)source[sr.rsrc].ntests; |
366 |
< |
scalecolor(srccnt[cntord[sn].sndx].val, prob); |
367 |
< |
addcolor(r->rcol, srccnt[cntord[sn].sndx].val); |
363 |
> |
scp = srccnt + cntord[sn].sndx; |
364 |
> |
prob = hwt * (double)source[scp->sno].nhits / |
365 |
> |
(double)source[scp->sno].ntests; |
366 |
> |
if (prob > 1.0) |
367 |
> |
prob = 1.0; |
368 |
> |
scalecolor(scp->val, prob); |
369 |
> |
addcolor(r->rcol, scp->val); |
370 |
|
} |
371 |
|
} |
372 |
|
|
373 |
|
|
374 |
+ |
srcscatter(r) /* compute source scattering into ray */ |
375 |
+ |
register RAY *r; |
376 |
+ |
{ |
377 |
+ |
int nsamps; |
378 |
+ |
RAY sr; |
379 |
+ |
SRCINDEX si; |
380 |
+ |
double t, lastt, d; |
381 |
+ |
COLOR cumval, ctmp; |
382 |
+ |
int i, j; |
383 |
+ |
|
384 |
+ |
if (r->slights == NULL || r->slights[0] == 0 || r->gecc >= 1.-FTINY) |
385 |
+ |
return; |
386 |
+ |
if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) |
387 |
+ |
nsamps = 1; |
388 |
+ |
initsrcindex(&si); |
389 |
+ |
for (i = r->slights[0]; i > 0; i--) { /* for each source */ |
390 |
+ |
setcolor(cumval, 0., 0., 0.); |
391 |
+ |
lastt = r->rot; |
392 |
+ |
for (j = nsamps; j-- > 0; ) { /* for each sample position */ |
393 |
+ |
samplendx++; |
394 |
+ |
t = r->rot * (j+frandom())/nsamps; |
395 |
+ |
sr.rorg[0] = r->rorg[0] + r->rdir[0]*t; |
396 |
+ |
sr.rorg[1] = r->rorg[1] + r->rdir[1]*t; |
397 |
+ |
sr.rorg[2] = r->rorg[2] + r->rdir[2]*t; |
398 |
+ |
sr.rmax = 0.; |
399 |
+ |
/* sample ray to this source */ |
400 |
+ |
if (si.sp >= si.np-1 || !srcray(&sr, NULL, &si) || |
401 |
+ |
sr.rsrc != r->slights[i]) { |
402 |
+ |
si.sn = r->slights[i]-1; /* reset */ |
403 |
+ |
si.np = 0; |
404 |
+ |
if (!srcray(&sr, NULL, &si) || |
405 |
+ |
sr.rsrc != r->slights[i]) |
406 |
+ |
continue; /* no path */ |
407 |
+ |
} |
408 |
+ |
copycolor(sr.cext, r->cext); |
409 |
+ |
sr.albedo = r->albedo; |
410 |
+ |
sr.gecc = r->gecc; |
411 |
+ |
rayvalue(&sr); /* eval. source ray */ |
412 |
+ |
if (bright(sr.rcol) <= FTINY) |
413 |
+ |
continue; |
414 |
+ |
/* compute fall-off */ |
415 |
+ |
d = lastt - t; |
416 |
+ |
setcolor(ctmp, 1.-d*colval(r->cext,RED), |
417 |
+ |
1.-d*colval(r->cext,GRN), |
418 |
+ |
1.-d*colval(r->cext,BLU)); |
419 |
+ |
multcolor(cumval, ctmp); |
420 |
+ |
lastt = t; |
421 |
+ |
if (r->gecc <= FTINY) /* compute P(theta) */ |
422 |
+ |
d = 1.; |
423 |
+ |
else { |
424 |
+ |
d = DOT(r->rdir, sr.rdir); |
425 |
+ |
d = sqrt(1. + r->gecc*r->gecc - 2.*r->gecc*d); |
426 |
+ |
d = (1. - r->gecc*r->gecc) / (d*d*d); |
427 |
+ |
} |
428 |
+ |
/* other factors */ |
429 |
+ |
d *= si.dom * r->albedo * r->rot / (4.*PI*nsamps); |
430 |
+ |
multcolor(sr.rcol, r->cext); |
431 |
+ |
scalecolor(sr.rcol, d); |
432 |
+ |
addcolor(cumval, sr.rcol); |
433 |
+ |
} |
434 |
+ |
/* final fall-off */ |
435 |
+ |
setcolor(ctmp, 1.-lastt*colval(r->cext,RED), |
436 |
+ |
1.-lastt*colval(r->cext,GRN), |
437 |
+ |
1.-lastt*colval(r->cext,BLU)); |
438 |
+ |
multcolor(cumval, ctmp); |
439 |
+ |
addcolor(r->rcol, cumval); /* sum into ray result */ |
440 |
+ |
} |
441 |
+ |
} |
442 |
+ |
|
443 |
+ |
|
444 |
|
/**************************************************************** |
445 |
|
* The following macros were separated from the m_light() routine |
446 |
|
* because they are very nasty and difficult to understand. |
447 |
|
*/ |
448 |
|
|
449 |
< |
/* wrongillum * |
449 |
> |
/* illumblock * |
450 |
|
* |
451 |
|
* We cannot allow an illum to pass to another illum, because that |
452 |
|
* would almost certainly constitute overcounting. |
453 |
|
* However, we do allow an illum to pass to another illum |
454 |
|
* that is actually going to relay to a virtual light source. |
455 |
+ |
* We also prevent an illum from passing to a glow; this provides a |
456 |
+ |
* convenient mechanism for defining detailed light source |
457 |
+ |
* geometry behind (or inside) an effective radiator. |
458 |
|
*/ |
459 |
|
|
460 |
< |
#define wrongillum(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ |
461 |
< |
objptr(source[r->rsrc].so->omod)->otype==MAT_ILLUM) |
460 |
> |
static int weaksrcmod(obj) int obj; /* efficiency booster function */ |
461 |
> |
{register OBJREC *o = objptr(obj); |
462 |
> |
return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} |
463 |
|
|
464 |
+ |
#define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ |
465 |
+ |
r->rod > 0.0 && \ |
466 |
+ |
weaksrcmod(source[r->rsrc].so->omod)) |
467 |
+ |
|
468 |
|
/* wrongsource * |
469 |
|
* |
470 |
|
* This source is the wrong source (ie. overcounted) if we are |
471 |
|
* aimed to a different source than the one we hit and the one |
472 |
< |
* we hit is not an illum which should be passed. |
472 |
> |
* we hit is not an illum that should be passed. |
473 |
|
*/ |
474 |
|
|
475 |
|
#define wrongsource(m, r) (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \ |
476 |
< |
(m->otype!=MAT_ILLUM || wrongillum(m,r))) |
476 |
> |
(m->otype!=MAT_ILLUM || illumblock(m,r))) |
477 |
|
|
478 |
|
/* distglow * |
479 |
|
* |
480 |
|
* A distant glow is an object that sometimes acts as a light source, |
481 |
|
* but is too far away from the test point to be one in this case. |
482 |
+ |
* (Glows with negative radii should NEVER participate in illumination.) |
483 |
|
*/ |
484 |
|
|
485 |
< |
#define distglow(m, r) (m->otype==MAT_GLOW && \ |
486 |
< |
r->rot > m->oargs.farg[3]) |
485 |
> |
#define distglow(m, r, d) (m->otype==MAT_GLOW && \ |
486 |
> |
m->oargs.farg[3] >= -FTINY && \ |
487 |
> |
d > m->oargs.farg[3]) |
488 |
|
|
489 |
|
/* badcomponent * |
490 |
|
* |
497 |
|
|
498 |
|
#define badcomponent(m, r) (r->crtype&(AMBIENT|SPECULAR) && \ |
499 |
|
!(r->crtype&SHADOW || r->rod < 0.0 || \ |
500 |
< |
distglow(m, r))) |
500 |
> |
/* not 100% correct */ distglow(m, r, r->rot))) |
501 |
|
|
365 |
– |
/* overcount * |
366 |
– |
* |
367 |
– |
* All overcounting possibilities are contained here. |
368 |
– |
*/ |
369 |
– |
|
370 |
– |
#define overcount(m, r) (badcomponent(m,r) || wrongsource(m,r)) |
371 |
– |
|
502 |
|
/* passillum * |
503 |
|
* |
504 |
|
* An illum passes to another material type when we didn't hit it |
512 |
|
|
513 |
|
/* srcignore * |
514 |
|
* |
515 |
< |
* The -di flag renders light sources invisible, and here is the test. |
515 |
> |
* The -dv flag is normally on for sources to be visible. |
516 |
|
*/ |
517 |
|
|
518 |
< |
#define srcignore(m, r) (directinvis && !(r->crtype&SHADOW) && \ |
519 |
< |
!distglow(m, r)) |
518 |
> |
#define srcignore(m, r) !(directvis || r->crtype&SHADOW || \ |
519 |
> |
distglow(m, r, raydist(r,PRIMARY))) |
520 |
|
|
521 |
|
|
522 |
|
m_light(m, r) /* ray hit a light source */ |
524 |
|
register RAY *r; |
525 |
|
{ |
526 |
|
/* check for over-counting */ |
527 |
< |
if (overcount(m, r)) |
528 |
< |
return; |
527 |
> |
if (badcomponent(m, r)) |
528 |
> |
return(1); |
529 |
> |
if (wrongsource(m, r)) |
530 |
> |
return(1); |
531 |
|
/* check for passed illum */ |
532 |
|
if (passillum(m, r)) { |
533 |
< |
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
534 |
< |
raytrans(r); |
535 |
< |
else |
536 |
< |
rayshade(r, modifier(m->oargs.sarg[0])); |
405 |
< |
return; |
533 |
> |
if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) |
534 |
> |
return(rayshade(r, modifier(m->oargs.sarg[0]))); |
535 |
> |
raytrans(r); |
536 |
> |
return(1); |
537 |
|
} |
538 |
|
/* otherwise treat as source */ |
539 |
|
/* check for behind */ |
540 |
|
if (r->rod < 0.0) |
541 |
< |
return; |
541 |
> |
return(1); |
542 |
|
/* check for invisibility */ |
543 |
|
if (srcignore(m, r)) |
544 |
< |
return; |
544 |
> |
return(1); |
545 |
|
/* check for outside spot */ |
546 |
< |
if (m->otype==MAT_SPOT && spotout(r, (SPOT *)m->os, r->rot>=FHUGE)) |
547 |
< |
return; |
546 |
> |
if (m->otype==MAT_SPOT && spotout(r, makespot(m))) |
547 |
> |
return(1); |
548 |
|
/* get distribution pattern */ |
549 |
|
raytexture(r, m->omod); |
550 |
|
/* get source color */ |
553 |
|
m->oargs.farg[2]); |
554 |
|
/* modify value */ |
555 |
|
multcolor(r->rcol, r->pcol); |
556 |
+ |
return(1); |
557 |
|
} |