1 |
– |
/* Copyright (c) 1995 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 "otspecial.h" |
17 |
+ |
|
18 |
|
#include "source.h" |
19 |
|
|
20 |
|
#include "random.h" |
46 |
|
static int maxcntr = 0; /* size of contribution arrays */ |
47 |
|
|
48 |
|
|
49 |
+ |
OBJREC * /* find an object's actual material */ |
50 |
+ |
findmaterial(register OBJREC *o) |
51 |
+ |
{ |
52 |
+ |
while (!ismaterial(o->otype)) { |
53 |
+ |
if (ismixture(o->otype)) |
54 |
+ |
return(NULL); /* reject mixed materials */ |
55 |
+ |
if (o->otype == MOD_ALIAS && o->oargs.nsargs) { |
56 |
+ |
OBJECT aobj; |
57 |
+ |
OBJREC *ao; |
58 |
+ |
aobj = lastmod(objndx(o), o->oargs.sarg[0]); |
59 |
+ |
if (aobj < 0) |
60 |
+ |
objerror(o, USER, "bad reference"); |
61 |
+ |
ao = objptr(aobj); |
62 |
+ |
if (ismaterial(ao->otype)) |
63 |
+ |
return(ao); |
64 |
+ |
} |
65 |
+ |
if (o->omod == OVOID) |
66 |
+ |
return(NULL); |
67 |
+ |
o = objptr(o->omod); |
68 |
+ |
} |
69 |
+ |
return(o); |
70 |
+ |
} |
71 |
+ |
|
72 |
+ |
|
73 |
+ |
void |
74 |
|
marksources() /* find and mark source objects */ |
75 |
|
{ |
76 |
|
int foundsource = 0; |
80 |
|
/* initialize dispatch table */ |
81 |
|
initstypes(); |
82 |
|
/* find direct sources */ |
83 |
< |
for (i = 0; i < nobjects; i++) { |
83 |
> |
for (i = 0; i < nsceneobjs; i++) { |
84 |
|
|
85 |
|
o = objptr(i); |
86 |
|
|
87 |
|
if (!issurface(o->otype) || o->omod == OVOID) |
88 |
|
continue; |
89 |
< |
|
90 |
< |
m = objptr(o->omod); |
91 |
< |
|
92 |
< |
if (!islight(m->otype)) |
69 |
< |
continue; |
89 |
> |
/* find material */ |
90 |
> |
m = findmaterial(o); |
91 |
> |
if (m == NULL || !islight(m->otype)) |
92 |
> |
continue; /* not source modifier */ |
93 |
|
|
94 |
|
if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 : |
95 |
|
m->otype == MAT_SPOT ? 7 : 3)) |
128 |
|
source[ns].sflags |= SSKIP; |
129 |
|
} |
130 |
|
} |
131 |
+ |
#if SHADCACHE |
132 |
+ |
source[ns].obscache = NULL; |
133 |
+ |
#endif |
134 |
|
if (!(source[ns].sflags & SSKIP)) |
135 |
|
foundsource++; |
136 |
|
} |
143 |
|
maxcntr = nsources + MAXSPART; /* start with this many */ |
144 |
|
srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB)); |
145 |
|
cntord = (CNTPTR *)malloc(maxcntr*sizeof(CNTPTR)); |
146 |
< |
if (srccnt == NULL | cntord == NULL) |
146 |
> |
if ((srccnt == NULL) | (cntord == NULL)) |
147 |
|
goto memerr; |
148 |
|
return; |
149 |
|
memerr: |
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
< |
srcray(sr, r, si) /* send a ray to a source, return domega */ |
155 |
< |
register RAY *sr; /* returned source ray */ |
130 |
< |
RAY *r; /* ray which hit object */ |
131 |
< |
SRCINDEX *si; /* source sample index */ |
154 |
> |
void |
155 |
> |
freesources() /* free all source structures */ |
156 |
|
{ |
157 |
+ |
if (nsources > 0) { |
158 |
+ |
#if SHADCACHE |
159 |
+ |
while (nsources--) |
160 |
+ |
freeobscache(&source[nsources]); |
161 |
+ |
#endif |
162 |
+ |
free((void *)source); |
163 |
+ |
source = NULL; |
164 |
+ |
nsources = 0; |
165 |
+ |
} |
166 |
+ |
if (maxcntr <= 0) |
167 |
+ |
return; |
168 |
+ |
free((void *)srccnt); |
169 |
+ |
srccnt = NULL; |
170 |
+ |
free((void *)cntord); |
171 |
+ |
cntord = NULL; |
172 |
+ |
maxcntr = 0; |
173 |
+ |
} |
174 |
+ |
|
175 |
+ |
|
176 |
+ |
int |
177 |
+ |
srcray( /* send a ray to a source, return domega */ |
178 |
+ |
register RAY *sr, /* returned source ray */ |
179 |
+ |
RAY *r, /* ray which hit object */ |
180 |
+ |
SRCINDEX *si /* source sample index */ |
181 |
+ |
) |
182 |
+ |
{ |
183 |
|
double d; /* distance to source */ |
184 |
|
register SRCREC *srcp; |
185 |
|
|
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
< |
srcvalue(r) /* punch ray to source and compute value */ |
216 |
< |
register RAY *r; |
215 |
> |
void |
216 |
> |
srcvalue( /* punch ray to source and compute value */ |
217 |
> |
register RAY *r |
218 |
> |
) |
219 |
|
{ |
220 |
|
register SRCREC *sp; |
221 |
|
|
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
< |
sourcehit(r) /* check to see if ray hit distant source */ |
258 |
< |
register RAY *r; |
257 |
> |
int |
258 |
> |
sourcehit( /* check to see if ray hit distant source */ |
259 |
> |
register RAY *r |
260 |
> |
) |
261 |
|
{ |
262 |
|
int first, last; |
263 |
|
register int i; |
281 |
|
} |
282 |
|
|
283 |
|
if (r->ro != NULL) { |
284 |
+ |
r->robj = objndx(r->ro); |
285 |
|
for (i = 0; i < 3; i++) |
286 |
|
r->ron[i] = -r->rdir[i]; |
287 |
|
r->rod = 1.0; |
288 |
+ |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
289 |
+ |
r->uv[0] = r->uv[1] = 0.0; |
290 |
|
r->rox = NULL; |
291 |
|
return(1); |
292 |
|
} |
294 |
|
} |
295 |
|
|
296 |
|
|
297 |
+ |
#if SHADCACHE /* preemptive shadow checking */ |
298 |
+ |
#define ABS(x) ((x)>0 ? (x) : -(x)) |
299 |
+ |
|
300 |
+ |
static void /* find closest blockers to source */ |
301 |
+ |
initobscache(SRCREC *srcp) |
302 |
+ |
{ |
303 |
+ |
int i; |
304 |
+ |
int cachelen; |
305 |
+ |
|
306 |
+ |
if (srcp->sflags & SDISTANT) |
307 |
+ |
cachelen = 4*SHADCACHE*SHADCACHE; |
308 |
+ |
else if (srcp->sflags & SFLAT) |
309 |
+ |
cachelen = SHADCACHE*SHADCACHE*3 + (SHADCACHE&1)*SHADCACHE*4; |
310 |
+ |
else /* spherical distribution */ |
311 |
+ |
cachelen = SHADCACHE*SHADCACHE*6; |
312 |
+ |
/* allocate cache */ |
313 |
+ |
DCHECK(srcp->obscache != NULL, |
314 |
+ |
CONSISTENCY, "initobscache() called twice"); |
315 |
+ |
srcp->obscache = (OBSCACHE *)malloc(sizeof(OBSCACHE) + |
316 |
+ |
sizeof(OBJECT)*(cachelen-1)); |
317 |
+ |
if (srcp->obscache == NULL) |
318 |
+ |
error(SYSTEM, "out of memory in initobscache()"); |
319 |
+ |
/* set parameters */ |
320 |
+ |
if (srcp->sflags & SDISTANT) { |
321 |
+ |
int ax, ax1, ax2; |
322 |
+ |
RREAL amax = 0; |
323 |
+ |
for (ax1 = 3; ax1--; ) |
324 |
+ |
if (ABS(srcp->sloc[ax1]) > amax) { |
325 |
+ |
amax = ABS(srcp->sloc[ax1]); |
326 |
+ |
ax = ax1; |
327 |
+ |
} |
328 |
+ |
srcp->obscache->p.d.ax = ax; |
329 |
+ |
ax1 = (ax+1)%3; |
330 |
+ |
ax2 = (ax+2)%3; |
331 |
+ |
VCOPY(srcp->obscache->p.d.o, thescene.cuorg); |
332 |
+ |
if (srcp->sloc[ax] > 0) |
333 |
+ |
srcp->obscache->p.d.o[ax] += thescene.cusize; |
334 |
+ |
if (srcp->sloc[ax1] < 0) |
335 |
+ |
srcp->obscache->p.d.o[ax1] += thescene.cusize * |
336 |
+ |
srcp->sloc[ax1] / ABS(srcp->sloc[ax]); |
337 |
+ |
if (srcp->sloc[ax2] < 0) |
338 |
+ |
srcp->obscache->p.d.o[ax2] += thescene.cusize * |
339 |
+ |
srcp->sloc[ax2] / ABS(srcp->sloc[ax]); |
340 |
+ |
srcp->obscache->p.d.e1 = (1.-FTINY) / (thescene.cusize*(1. + |
341 |
+ |
fabs(srcp->sloc[ax1]/srcp->sloc[ax]))); |
342 |
+ |
srcp->obscache->p.d.e2 = (1.-FTINY) / (thescene.cusize*(1. + |
343 |
+ |
fabs(srcp->sloc[ax2]/srcp->sloc[ax]))); |
344 |
+ |
} else if (srcp->sflags & SFLAT) { |
345 |
+ |
VCOPY(srcp->obscache->p.f.u, srcp->ss[SU]); |
346 |
+ |
normalize(srcp->obscache->p.f.u); |
347 |
+ |
fcross(srcp->obscache->p.f.v, |
348 |
+ |
srcp->snorm, srcp->obscache->p.f.u); |
349 |
+ |
} |
350 |
+ |
/* XXX Should cast rays from source */ |
351 |
+ |
for (i = cachelen; i--; ) |
352 |
+ |
srcp->obscache->obs[i] = OVOID; |
353 |
+ |
} |
354 |
+ |
|
355 |
+ |
|
356 |
+ |
static OBJECT * /* return occluder cache entry */ |
357 |
+ |
srcobstructp(register RAY *r) |
358 |
+ |
{ |
359 |
+ |
static OBJECT noobs; |
360 |
+ |
SRCREC *srcp; |
361 |
+ |
int ondx; |
362 |
+ |
|
363 |
+ |
DCHECK(r->rsrc < 0, CONSISTENCY, |
364 |
+ |
"srcobstructp() called with unaimed ray"); |
365 |
+ |
noobs = OVOID; |
366 |
+ |
srcp = &source[r->rsrc]; |
367 |
+ |
if (srcp->obscache == NULL) /* initialize cache */ |
368 |
+ |
initobscache(srcp); |
369 |
+ |
/* compute cache index */ |
370 |
+ |
if (srcp->sflags & SDISTANT) { |
371 |
+ |
int ax, ax1, ax2; |
372 |
+ |
double t; |
373 |
+ |
ax = srcp->obscache->p.d.ax; |
374 |
+ |
if ((ax1 = ax+1) >= 3) ax1 -= 3; |
375 |
+ |
if ((ax2 = ax+2) >= 3) ax2 -= 3; |
376 |
+ |
t = (srcp->obscache->p.d.o[ax] - r->rorg[ax]) / srcp->sloc[ax]; |
377 |
+ |
if (t <= FTINY) |
378 |
+ |
return &noobs; /* could happen if ray is outside */ |
379 |
+ |
ondx = 2*SHADCACHE*(int)(2*SHADCACHE*srcp->obscache->p.d.e1 * |
380 |
+ |
(r->rorg[ax1] + t*srcp->sloc[ax1] - |
381 |
+ |
srcp->obscache->p.d.o[ax1])); |
382 |
+ |
ondx += (int)(2*SHADCACHE*srcp->obscache->p.d.e2 * |
383 |
+ |
(r->rorg[ax2] + t*srcp->sloc[ax2] - |
384 |
+ |
srcp->obscache->p.d.o[ax2])); |
385 |
+ |
} else if (srcp->sflags & SFLAT) { |
386 |
+ |
FVECT sd; |
387 |
+ |
RREAL sd0m, sd1m; |
388 |
+ |
sd[0] = -DOT(r->rdir, srcp->obscache->p.f.u); |
389 |
+ |
sd[1] = -DOT(r->rdir, srcp->obscache->p.f.v); |
390 |
+ |
sd[2] = -DOT(r->rdir, srcp->snorm); |
391 |
+ |
if (sd[2] < 0) |
392 |
+ |
return &noobs; /* shouldn't happen */ |
393 |
+ |
sd0m = ABS(sd[0]); |
394 |
+ |
sd1m = ABS(sd[1]); |
395 |
+ |
if (sd[2] >= sd0m && sd[2] >= sd1m) { |
396 |
+ |
ondx = SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * |
397 |
+ |
(1. + sd[0]/sd[2])); |
398 |
+ |
ondx += (int)(SHADCACHE*(.5-FTINY) * |
399 |
+ |
(1. + sd[1]/sd[2])); |
400 |
+ |
} else if (sd0m >= sd1m) { |
401 |
+ |
ondx = SHADCACHE*SHADCACHE; |
402 |
+ |
if (sd[0] < 0) |
403 |
+ |
ondx += ((SHADCACHE+1)>>1)*SHADCACHE; |
404 |
+ |
ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * |
405 |
+ |
sd[2]/sd0m); |
406 |
+ |
ondx += (int)(SHADCACHE*(.5-FTINY) * |
407 |
+ |
(1. + sd[1]/sd0m)); |
408 |
+ |
} else /* sd1m > sd0m */ { |
409 |
+ |
ondx = SHADCACHE*SHADCACHE + |
410 |
+ |
((SHADCACHE+1)>>1)*SHADCACHE*2; |
411 |
+ |
if (sd[1] < 0) |
412 |
+ |
ondx += ((SHADCACHE+1)>>1)*SHADCACHE; |
413 |
+ |
ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * |
414 |
+ |
sd[2]/sd1m); |
415 |
+ |
ondx += (int)(SHADCACHE*(.5-FTINY) * |
416 |
+ |
(1. + sd[0]/sd1m)); |
417 |
+ |
} |
418 |
+ |
} else /* spherical distribution */ { |
419 |
+ |
int ax, ax1, ax2; |
420 |
+ |
RREAL amax = 0; |
421 |
+ |
for (ax1 = 3; ax1--; ) |
422 |
+ |
if (ABS(r->rdir[ax1]) > amax) { |
423 |
+ |
amax = ABS(r->rdir[ax1]); |
424 |
+ |
ax = ax1; |
425 |
+ |
} |
426 |
+ |
if ((ax1 = ax+1) >= 3) ax1 -= 3; |
427 |
+ |
if ((ax2 = ax+2) >= 3) ax2 -= 3; |
428 |
+ |
ondx = 2*SHADCACHE*SHADCACHE * ax; |
429 |
+ |
if (r->rdir[ax] < 0) |
430 |
+ |
ondx += SHADCACHE*SHADCACHE; |
431 |
+ |
ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * |
432 |
+ |
(1. + r->rdir[ax1]/amax)); |
433 |
+ |
ondx += (int)(SHADCACHE*(.5-FTINY) * |
434 |
+ |
(1. + r->rdir[ax2]/amax)); |
435 |
+ |
} |
436 |
+ |
/* return cache pointer */ |
437 |
+ |
return(&srcp->obscache->obs[ondx]); |
438 |
+ |
} |
439 |
+ |
|
440 |
+ |
|
441 |
+ |
void /* free obstruction cache */ |
442 |
+ |
freeobscache(SRCREC *srcp) |
443 |
+ |
{ |
444 |
+ |
if (srcp->obscache == NULL) |
445 |
+ |
return; |
446 |
+ |
free((void *)srcp->obscache); |
447 |
+ |
srcp->obscache = NULL; |
448 |
+ |
} |
449 |
+ |
|
450 |
+ |
|
451 |
+ |
void /* record a source blocker */ |
452 |
+ |
srcblocker(register RAY *r) |
453 |
+ |
{ |
454 |
+ |
OBJREC *m; |
455 |
+ |
|
456 |
+ |
if (r->robj == OVOID || objptr(r->robj) != r->ro || |
457 |
+ |
isvolume(r->ro->otype)) |
458 |
+ |
return; /* don't record complex blockers */ |
459 |
+ |
m = findmaterial(r->ro); |
460 |
+ |
if (m == NULL) |
461 |
+ |
return; /* no material?! */ |
462 |
+ |
if (!(ofun[m->otype].flags & T_OPAQUE)) |
463 |
+ |
return; /* material not a reliable blocker */ |
464 |
+ |
|
465 |
+ |
*srcobstructp(r) = r->robj; /* else record obstructor */ |
466 |
+ |
} |
467 |
+ |
|
468 |
+ |
|
469 |
+ |
int /* check ray against cached blocker */ |
470 |
+ |
srcblocked(RAY *r) |
471 |
+ |
{ |
472 |
+ |
OBJECT obs = *srcobstructp(r); |
473 |
+ |
OBJREC *op; |
474 |
+ |
|
475 |
+ |
if (obs == OVOID) |
476 |
+ |
return(0); |
477 |
+ |
op = objptr(obs); /* check for intersection */ |
478 |
+ |
return ((*ofun[op->otype].funp)(op, r)); |
479 |
+ |
} |
480 |
+ |
|
481 |
+ |
#endif |
482 |
+ |
|
483 |
+ |
|
484 |
|
static int |
485 |
< |
cntcmp(sc1, sc2) /* contribution compare (descending) */ |
486 |
< |
register CNTPTR *sc1, *sc2; |
485 |
> |
cntcmp( /* contribution compare (descending) */ |
486 |
> |
const void *p1, |
487 |
> |
const void *p2 |
488 |
> |
) |
489 |
|
{ |
490 |
+ |
register const CNTPTR *sc1 = (const CNTPTR *)p1; |
491 |
+ |
register const CNTPTR *sc2 = (const CNTPTR *)p2; |
492 |
+ |
|
493 |
|
if (sc1->brt > sc2->brt) |
494 |
|
return(-1); |
495 |
|
if (sc1->brt < sc2->brt) |
498 |
|
} |
499 |
|
|
500 |
|
|
501 |
< |
direct(r, f, p) /* add direct component */ |
502 |
< |
RAY *r; /* ray that hit surface */ |
503 |
< |
int (*f)(); /* direct component coefficient function */ |
504 |
< |
char *p; /* data for f */ |
501 |
> |
void |
502 |
> |
direct( /* add direct component */ |
503 |
> |
RAY *r, /* ray that hit surface */ |
504 |
> |
void (*f)(), /* direct component coefficient function */ |
505 |
> |
char *p /* data for f */ |
506 |
> |
) |
507 |
|
{ |
508 |
< |
extern int (*trace)(); |
508 |
> |
extern void (*trace)(); |
509 |
|
register int sn; |
510 |
|
register CONTRIB *scp; |
511 |
|
SRCINDEX si; |
521 |
|
for (sn = 0; srcray(&sr, r, &si); sn++) { |
522 |
|
if (sn >= maxcntr) { |
523 |
|
maxcntr = sn + MAXSPART; |
524 |
< |
srccnt = (CONTRIB *)realloc((char *)srccnt, |
524 |
> |
srccnt = (CONTRIB *)realloc((void *)srccnt, |
525 |
|
maxcntr*sizeof(CONTRIB)); |
526 |
< |
cntord = (CNTPTR *)realloc((char *)cntord, |
526 |
> |
cntord = (CNTPTR *)realloc((void *)cntord, |
527 |
|
maxcntr*sizeof(CNTPTR)); |
528 |
< |
if (srccnt == NULL | cntord == NULL) |
528 |
> |
if ((srccnt == NULL) | (cntord == NULL)) |
529 |
|
error(SYSTEM, "out of memory in direct"); |
530 |
|
} |
531 |
|
cntord[sn].sndx = sn; |
536 |
|
cntord[sn].brt = bright(scp->coef); |
537 |
|
if (cntord[sn].brt <= 0.0) |
538 |
|
continue; |
539 |
+ |
#if SHADCACHE |
540 |
+ |
/* check shadow cache */ |
541 |
+ |
if (si.np == 1 && srcblocked(&sr)) { |
542 |
+ |
cntord[sn].brt = 0.0; |
543 |
+ |
continue; |
544 |
+ |
} |
545 |
+ |
#endif |
546 |
|
VCOPY(scp->dir, sr.rdir); |
547 |
|
/* compute potential */ |
548 |
|
sr.revf = srcvalue; |
590 |
|
rayorigin(&sr, r, SHADOW, 1.0); |
591 |
|
VCOPY(sr.rdir, scp->dir); |
592 |
|
sr.rsrc = scp->sno; |
593 |
< |
source[scp->sno].ntests++; /* keep statistics */ |
593 |
> |
/* keep statistics */ |
594 |
> |
if (source[scp->sno].ntests++ > 0xfffffff0) { |
595 |
> |
source[scp->sno].ntests >>= 1; |
596 |
> |
source[scp->sno].nhits >>= 1; |
597 |
> |
} |
598 |
|
if (localhit(&sr, &thescene) && |
599 |
|
( sr.ro != source[scp->sno].so || |
600 |
|
source[scp->sno].sflags & SFOLLOW )) { |
601 |
|
/* follow entire path */ |
602 |
< |
if (!raycont(&sr)) |
341 |
< |
objerror(sr.ro, USER, "material not found"); |
602 |
> |
raycont(&sr); |
603 |
|
rayparticipate(&sr); |
604 |
|
if (trace != NULL) |
605 |
|
(*trace)(&sr); /* trace execution */ |
606 |
< |
if (bright(sr.rcol) <= FTINY) |
606 |
> |
if (bright(sr.rcol) <= FTINY) { |
607 |
> |
#if SHADCACHE |
608 |
> |
if ((scp <= srccnt || scp[-1].sno != scp->sno) |
609 |
> |
&& (scp >= srccnt+ncnts || |
610 |
> |
scp[1].sno != scp->sno)) |
611 |
> |
srcblocker(&sr); |
612 |
> |
#endif |
613 |
|
continue; /* missed! */ |
614 |
+ |
} |
615 |
|
copycolor(scp->val, sr.rcol); |
616 |
|
multcolor(scp->val, scp->coef); |
617 |
|
} |
643 |
|
} |
644 |
|
|
645 |
|
|
646 |
< |
srcscatter(r) /* compute source scattering into ray */ |
647 |
< |
register RAY *r; |
646 |
> |
void |
647 |
> |
srcscatter( /* compute source scattering into ray */ |
648 |
> |
register RAY *r |
649 |
> |
) |
650 |
|
{ |
651 |
|
int oldsampndx; |
652 |
|
int nsamps; |
653 |
|
RAY sr; |
654 |
|
SRCINDEX si; |
655 |
< |
double t, lastt, d; |
656 |
< |
COLOR cumval, ctmp; |
655 |
> |
double t, d; |
656 |
> |
double re, ge, be; |
657 |
> |
COLOR cvext; |
658 |
|
int i, j; |
659 |
|
|
660 |
|
if (r->slights == NULL || r->slights[0] == 0 |
669 |
|
oldsampndx = samplendx; |
670 |
|
samplendx = random()&0x7fff; /* randomize */ |
671 |
|
for (i = r->slights[0]; i > 0; i--) { /* for each source */ |
672 |
< |
setcolor(cumval, 0., 0., 0.); |
402 |
< |
lastt = r->rot; |
403 |
< |
for (j = nsamps; j-- > 0; ) { /* for each sample position */ |
672 |
> |
for (j = 0; j < nsamps; j++) { /* for each sample position */ |
673 |
|
samplendx++; |
674 |
|
t = r->rot * (j+frandom())/nsamps; |
675 |
+ |
/* extinction */ |
676 |
+ |
re = t*colval(r->cext,RED); |
677 |
+ |
ge = t*colval(r->cext,GRN); |
678 |
+ |
be = t*colval(r->cext,BLU); |
679 |
+ |
setcolor(cvext, re > 92. ? 0. : exp(-re), |
680 |
+ |
ge > 92. ? 0. : exp(-ge), |
681 |
+ |
be > 92. ? 0. : exp(-be)); |
682 |
+ |
if (intens(cvext) <= FTINY) |
683 |
+ |
break; /* too far away */ |
684 |
|
sr.rorg[0] = r->rorg[0] + r->rdir[0]*t; |
685 |
|
sr.rorg[1] = r->rorg[1] + r->rdir[1]*t; |
686 |
|
sr.rorg[2] = r->rorg[2] + r->rdir[2]*t; |
692 |
|
sr.rsrc != r->slights[i]) |
693 |
|
continue; /* no path */ |
694 |
|
copycolor(sr.cext, r->cext); |
695 |
< |
sr.albedo = r->albedo; |
695 |
> |
copycolor(sr.albedo, r->albedo); |
696 |
|
sr.gecc = r->gecc; |
697 |
+ |
sr.slights = r->slights; |
698 |
|
rayvalue(&sr); /* eval. source ray */ |
699 |
|
if (bright(sr.rcol) <= FTINY) |
700 |
|
continue; |
422 |
– |
/* compute fall-off */ |
423 |
– |
d = lastt - t; |
424 |
– |
setcolor(ctmp, 1.-d*colval(r->cext,RED), |
425 |
– |
1.-d*colval(r->cext,GRN), |
426 |
– |
1.-d*colval(r->cext,BLU)); |
427 |
– |
multcolor(cumval, ctmp); |
428 |
– |
lastt = t; |
701 |
|
if (r->gecc <= FTINY) /* compute P(theta) */ |
702 |
|
d = 1.; |
703 |
|
else { |
704 |
|
d = DOT(r->rdir, sr.rdir); |
705 |
< |
d = sqrt(1. + r->gecc*r->gecc - 2.*r->gecc*d); |
706 |
< |
d = (1. - r->gecc*r->gecc) / (d*d*d); |
705 |
> |
d = 1. + r->gecc*r->gecc - 2.*r->gecc*d; |
706 |
> |
d = (1. - r->gecc*r->gecc) / (d*sqrt(d)); |
707 |
|
} |
708 |
|
/* other factors */ |
709 |
< |
d *= si.dom * r->albedo * r->rot / (4.*PI*nsamps); |
709 |
> |
d *= si.dom * r->rot / (4.*PI*nsamps); |
710 |
|
multcolor(sr.rcol, r->cext); |
711 |
+ |
multcolor(sr.rcol, r->albedo); |
712 |
|
scalecolor(sr.rcol, d); |
713 |
< |
addcolor(cumval, sr.rcol); |
713 |
> |
multcolor(sr.rcol, cvext); |
714 |
> |
addcolor(r->rcol, sr.rcol); /* add it in */ |
715 |
|
} |
442 |
– |
/* final fall-off */ |
443 |
– |
setcolor(ctmp, 1.-lastt*colval(r->cext,RED), |
444 |
– |
1.-lastt*colval(r->cext,GRN), |
445 |
– |
1.-lastt*colval(r->cext,BLU)); |
446 |
– |
multcolor(cumval, ctmp); |
447 |
– |
addcolor(r->rcol, cumval); /* sum into ray result */ |
716 |
|
} |
717 |
|
samplendx = oldsampndx; |
718 |
|
} |
734 |
|
* geometry behind (or inside) an effective radiator. |
735 |
|
*/ |
736 |
|
|
737 |
< |
static int weaksrcmod(obj) int obj; /* efficiency booster function */ |
738 |
< |
{register OBJREC *o = objptr(obj); |
739 |
< |
return(o->otype==MAT_ILLUM|o->otype==MAT_GLOW);} |
737 |
> |
static int |
738 |
> |
weaksrcmat(int obj) /* identify material */ |
739 |
> |
{ |
740 |
> |
register OBJREC *o = objptr(obj); |
741 |
> |
|
742 |
> |
while (!ismaterial(o->otype)) /* find material */ |
743 |
> |
o = objptr(o->omod); |
744 |
> |
return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW)); |
745 |
> |
} |
746 |
|
|
747 |
|
#define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ |
748 |
|
r->rod > 0.0 && \ |
749 |
< |
weaksrcmod(source[r->rsrc].so->omod)) |
749 |
> |
weaksrcmat(source[r->rsrc].so->omod)) |
750 |
|
|
751 |
|
/* wrongsource * |
752 |
|
* |
802 |
|
distglow(m, r, raydist(r,PRIMARY))) |
803 |
|
|
804 |
|
|
805 |
< |
m_light(m, r) /* ray hit a light source */ |
806 |
< |
register OBJREC *m; |
807 |
< |
register RAY *r; |
805 |
> |
int |
806 |
> |
m_light( /* ray hit a light source */ |
807 |
> |
register OBJREC *m, |
808 |
> |
register RAY *r |
809 |
> |
) |
810 |
|
{ |
811 |
|
/* check for over-counting */ |
812 |
|
if (badcomponent(m, r)) |
816 |
|
/* check for passed illum */ |
817 |
|
if (passillum(m, r)) { |
818 |
|
if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) |
819 |
< |
return(rayshade(r, modifier(m->oargs.sarg[0]))); |
819 |
> |
return(rayshade(r,lastmod(objndx(m),m->oargs.sarg[0]))); |
820 |
|
raytrans(r); |
821 |
|
return(1); |
822 |
|
} |