1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1990 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
27 |
|
|
28 |
|
extern double dstrsrc; /* source distribution amount */ |
29 |
|
extern double shadthresh; /* relative shadow threshold */ |
30 |
+ |
extern double shadcert; /* shadow testing certainty */ |
31 |
|
|
32 |
|
SRCREC *source = NULL; /* our list of sources */ |
33 |
|
int nsources = 0; /* the number of sources */ |
34 |
|
|
35 |
+ |
static CONTRIB *srccnt; /* source contributions in direct() */ |
36 |
+ |
static CNTPTR *cntord; /* source ordering in direct() */ |
37 |
|
|
38 |
+ |
|
39 |
|
marksources() /* find and mark source objects */ |
40 |
|
{ |
41 |
|
register OBJREC *o, *m; |
50 |
|
|
51 |
|
m = objptr(o->omod); |
52 |
|
|
53 |
< |
if (m->otype != MAT_LIGHT && |
50 |
< |
m->otype != MAT_ILLUM && |
51 |
< |
m->otype != MAT_GLOW) |
53 |
> |
if (!islight(m->otype)) |
54 |
|
continue; |
55 |
|
|
56 |
< |
if (m->oargs.nfargs != 3) |
56 |
> |
if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 : |
57 |
> |
m->otype == MAT_SPOT ? 7 : 3)) |
58 |
|
objerror(m, USER, "bad # arguments"); |
59 |
|
|
60 |
< |
if (m->otype == MAT_GLOW && o->otype != OBJ_SOURCE) |
60 |
> |
if (m->otype == MAT_GLOW && |
61 |
> |
o->otype != OBJ_SOURCE && |
62 |
> |
m->oargs.farg[3] <= FTINY) |
63 |
|
continue; /* don't bother */ |
64 |
|
|
65 |
|
if (source == NULL) |
68 |
|
source = (SRCREC *)realloc((char *)source, |
69 |
|
(unsigned)(nsources+1)*sizeof(SRCREC)); |
70 |
|
if (source == NULL) |
71 |
< |
error(SYSTEM, "out of memory in marksources"); |
71 |
> |
goto memerr; |
72 |
|
|
73 |
|
newsource(&source[nsources], o); |
74 |
|
|
75 |
< |
if (m->otype == MAT_GLOW) |
76 |
< |
source[nsources].sflags |= SSKIP; |
77 |
< |
|
75 |
> |
if (m->otype == MAT_GLOW) { |
76 |
> |
source[nsources].sflags |= SPROX; |
77 |
> |
source[nsources].sl.prox = m->oargs.farg[3]; |
78 |
> |
if (o->otype == OBJ_SOURCE) |
79 |
> |
source[nsources].sflags |= SSKIP; |
80 |
> |
} else if (m->otype == MAT_SPOT) { |
81 |
> |
source[nsources].sflags |= SSPOT; |
82 |
> |
source[nsources].sl.s = makespot(m); |
83 |
> |
} |
84 |
|
nsources++; |
85 |
|
} |
86 |
+ |
if (nsources <= 0) { |
87 |
+ |
error(WARNING, "no light sources found"); |
88 |
+ |
return; |
89 |
+ |
} |
90 |
+ |
srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB)); |
91 |
+ |
cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR)); |
92 |
+ |
if (srccnt != NULL && cntord != NULL) |
93 |
+ |
return; |
94 |
+ |
/* fall through */ |
95 |
+ |
memerr: |
96 |
+ |
error(SYSTEM, "out of memory in marksources"); |
97 |
|
} |
98 |
|
|
99 |
|
|
109 |
|
register int i; |
110 |
|
|
111 |
|
src->sflags = 0; |
112 |
< |
src->nhits = src->ntests = 1; /* start with hit probability = 1 */ |
112 |
> |
src->aimsuccess = 2*AIMREQT-1; /* bitch on second failure */ |
113 |
> |
src->nhits = 1; src->ntests = 2; /* start probability = 1/2 */ |
114 |
|
src->so = so; |
115 |
|
|
116 |
|
switch (so->otype) { |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
+ |
SPOT * |
166 |
+ |
makespot(m) /* make a spotlight */ |
167 |
+ |
register OBJREC *m; |
168 |
+ |
{ |
169 |
+ |
extern double cos(); |
170 |
+ |
register SPOT *ns; |
171 |
+ |
|
172 |
+ |
if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
173 |
+ |
error(SYSTEM, "out of memory in makespot"); |
174 |
+ |
ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3])); |
175 |
+ |
VCOPY(ns->aim, m->oargs.farg+4); |
176 |
+ |
if ((ns->flen = normalize(ns->aim)) == 0.0) |
177 |
+ |
objerror(m, USER, "zero focus vector"); |
178 |
+ |
return(ns); |
179 |
+ |
} |
180 |
+ |
|
181 |
+ |
|
182 |
|
double |
183 |
|
srcray(sr, r, sn) /* send a ray to a source, return domega */ |
184 |
|
register RAY *sr; /* returned source ray */ |
215 |
|
} |
216 |
|
if (dstrsrc > FTINY) { |
217 |
|
/* distribute source direction */ |
218 |
< |
for (i = 0; i < 3; i++) |
219 |
< |
vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom()); |
220 |
< |
|
218 |
> |
dimlist[ndims++] = sn; |
219 |
> |
for (i = 0; i < 3; i++) { |
220 |
> |
dimlist[ndims] = i + 8831; |
221 |
> |
vd[i] = dstrsrc * source[sn].ss * |
222 |
> |
(1.0 - 2.0*urand(ilhash(dimlist,ndims+1)+samplendx)); |
223 |
> |
} |
224 |
> |
ndims--; |
225 |
|
if (norm != NULL) { /* project offset */ |
226 |
|
d = DOT(vd, norm); |
227 |
|
for (i = 0; i < 3; i++) |
242 |
|
/* domega constant */ |
243 |
|
return(source[sn].ss2); |
244 |
|
|
245 |
< |
else { |
246 |
< |
|
247 |
< |
if (norm != NULL) |
248 |
< |
ddot /= d; |
249 |
< |
else |
250 |
< |
ddot = 1.0; |
251 |
< |
/* return domega */ |
252 |
< |
return(ddot*source[sn].ss2/(d*d)); |
245 |
> |
/* check proximity */ |
246 |
> |
if (source[sn].sflags & SPROX && |
247 |
> |
d > source[sn].sl.prox) |
248 |
> |
return(0.0); |
249 |
> |
/* compute dot product */ |
250 |
> |
if (norm != NULL) |
251 |
> |
ddot /= d; |
252 |
> |
else |
253 |
> |
ddot = 1.0; |
254 |
> |
/* check angle */ |
255 |
> |
if (source[sn].sflags & SSPOT) { |
256 |
> |
if (source[sn].sl.s->siz < 2.0*PI * |
257 |
> |
(1.0 + DOT(source[sn].sl.s->aim,sr->rdir))) |
258 |
> |
return(0.0); |
259 |
> |
d += source[sn].sl.s->flen; /* adjust length */ |
260 |
|
} |
261 |
+ |
/* compute domega */ |
262 |
+ |
return(ddot*source[sn].ss2/(d*d)); |
263 |
|
} |
264 |
|
|
265 |
|
|
291 |
|
for (i = 0; i < 3; i++) |
292 |
|
r->ron[i] = -r->rdir[i]; |
293 |
|
r->rod = 1.0; |
294 |
< |
r->rofs = 1.0; setident4(r->rofx); |
242 |
< |
r->robs = 1.0; setident4(r->robx); |
294 |
> |
r->rox = NULL; |
295 |
|
return(1); |
296 |
|
} |
297 |
|
return(0); |
300 |
|
|
301 |
|
static int |
302 |
|
cntcmp(sc1, sc2) /* contribution compare (descending) */ |
303 |
< |
register CONTRIB *sc1, *sc2; |
303 |
> |
register CNTPTR *sc1, *sc2; |
304 |
|
{ |
305 |
|
if (sc1->brt > sc2->brt) |
306 |
|
return(-1); |
315 |
|
int (*f)(); /* direct component coefficient function */ |
316 |
|
char *p; /* data for f */ |
317 |
|
{ |
318 |
+ |
extern double pow(); |
319 |
|
register int sn; |
320 |
< |
register CONTRIB *srccnt; |
321 |
< |
double dtmp, hwt, test2, hit2; |
320 |
> |
int nshadcheck, ncnts; |
321 |
> |
int nhits; |
322 |
> |
double prob, ourthresh, hwt; |
323 |
|
RAY sr; |
324 |
< |
|
325 |
< |
if ((srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB))) == NULL) |
326 |
< |
error(SYSTEM, "out of memory in direct"); |
324 |
> |
/* NOTE: srccnt and cntord global so no recursion */ |
325 |
> |
if (nsources <= 0) |
326 |
> |
return; /* no sources?! */ |
327 |
> |
/* compute number to check */ |
328 |
> |
nshadcheck = pow((double)nsources, shadcert) + .5; |
329 |
> |
/* modify threshold */ |
330 |
> |
ourthresh = shadthresh / r->rweight; |
331 |
|
/* potential contributions */ |
332 |
|
for (sn = 0; sn < nsources; sn++) { |
333 |
< |
srccnt[sn].sno = sn; |
334 |
< |
setcolor(srccnt[sn].val, 0.0, 0.0, 0.0); |
333 |
> |
cntord[sn].sno = sn; |
334 |
> |
cntord[sn].brt = 0.0; |
335 |
|
/* get source ray */ |
336 |
|
if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0) |
337 |
|
continue; |
338 |
|
VCOPY(srccnt[sn].dir, sr.rdir); |
339 |
|
/* compute coefficient */ |
340 |
|
(*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom); |
341 |
< |
srccnt[sn].brt = intens(srccnt[sn].val); |
342 |
< |
if (srccnt[sn].brt <= FTINY) |
341 |
> |
cntord[sn].brt = bright(srccnt[sn].val); |
342 |
> |
if (cntord[sn].brt <= 0.0) |
343 |
|
continue; |
344 |
|
/* compute intersection */ |
345 |
< |
if (!( source[sn].sflags & SDISTANT ? |
345 |
> |
if (source[sn].sflags & SDISTANT ? |
346 |
|
sourcehit(&sr) : |
347 |
|
(*ofun[source[sn].so->otype].funp) |
348 |
< |
(source[sn].so, &sr) )) |
348 |
> |
(source[sn].so, &sr)) { |
349 |
> |
if (source[sn].aimsuccess >= 0) |
350 |
> |
source[sn].aimsuccess++; |
351 |
> |
} else { |
352 |
> |
cntord[sn].brt = 0.0; |
353 |
> |
if (source[sn].aimsuccess < 0) |
354 |
> |
continue; /* bitched already */ |
355 |
> |
source[sn].aimsuccess -= AIMREQT; |
356 |
> |
if (source[sn].aimsuccess >= 0) |
357 |
> |
continue; /* leniency */ |
358 |
> |
sprintf(errmsg, |
359 |
> |
"aiming failure for light source \"%s\"", |
360 |
> |
source[sn].so->oname); |
361 |
> |
error(WARNING, errmsg); |
362 |
|
continue; |
363 |
+ |
} |
364 |
|
/* compute contribution */ |
365 |
< |
rayshade(&sr, sr.ro->omod); |
365 |
> |
raycont(&sr); |
366 |
|
multcolor(srccnt[sn].val, sr.rcol); |
367 |
< |
srccnt[sn].brt = intens(srccnt[sn].val); |
367 |
> |
cntord[sn].brt = bright(srccnt[sn].val); |
368 |
|
} |
369 |
|
/* sort contributions */ |
370 |
< |
qsort(srccnt, nsources, sizeof(CONTRIB), cntcmp); |
371 |
< |
hit2 = test2 = 0.0; |
370 |
> |
qsort(cntord, nsources, sizeof(CNTPTR), cntcmp); |
371 |
> |
{ /* find last */ |
372 |
> |
register int l, m; |
373 |
> |
|
374 |
> |
sn = 0; ncnts = l = nsources; |
375 |
> |
while ((m = (sn + ncnts) >> 1) != l) { |
376 |
> |
if (cntord[m].brt > 0.0) |
377 |
> |
sn = m; |
378 |
> |
else |
379 |
> |
ncnts = m; |
380 |
> |
l = m; |
381 |
> |
} |
382 |
> |
} |
383 |
> |
/* accumulate tail */ |
384 |
> |
for (sn = ncnts-1; sn > 0; sn--) |
385 |
> |
cntord[sn-1].brt += cntord[sn].brt; |
386 |
|
/* test for shadows */ |
387 |
< |
for (sn = 0; sn < nsources; sn++) { |
387 |
> |
nhits = 0; |
388 |
> |
for (sn = 0; sn < ncnts; sn++) { |
389 |
|
/* check threshold */ |
390 |
< |
if (srccnt[sn].brt <= shadthresh*intens(r->rcol)/r->rweight) |
390 |
> |
if ((sn+nshadcheck>=ncnts ? cntord[sn].brt : |
391 |
> |
cntord[sn].brt-cntord[sn+nshadcheck].brt) |
392 |
> |
< ourthresh*bright(r->rcol)) |
393 |
|
break; |
394 |
|
/* get statistics */ |
395 |
< |
hwt = (double)source[srccnt[sn].sno].nhits / |
307 |
< |
(double)source[srccnt[sn].sno].ntests; |
308 |
< |
test2 += hwt; |
309 |
< |
source[srccnt[sn].sno].ntests++; |
395 |
> |
source[cntord[sn].sno].ntests++; |
396 |
|
/* test for hit */ |
397 |
|
rayorigin(&sr, r, SHADOW, 1.0); |
398 |
< |
VCOPY(sr.rdir, srccnt[sn].dir); |
398 |
> |
VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir); |
399 |
> |
sr.rsrc = cntord[sn].sno; |
400 |
|
if (localhit(&sr, &thescene) && |
401 |
< |
sr.ro != source[srccnt[sn].sno].so) { |
401 |
> |
sr.ro != source[cntord[sn].sno].so) { |
402 |
|
/* check for transmission */ |
403 |
< |
if (sr.clipset != NULL && inset(sr.clipset,sr.ro->omod)) |
404 |
< |
raytrans(&sr); /* object is clipped */ |
318 |
< |
else |
319 |
< |
rayshade(&sr, sr.ro->omod); |
320 |
< |
if (intens(sr.rcol) <= FTINY) |
403 |
> |
raycont(&sr); |
404 |
> |
if (bright(sr.rcol) <= FTINY) |
405 |
|
continue; /* missed! */ |
406 |
< |
(*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom); |
407 |
< |
multcolor(srccnt[sn].val, sr.rcol); |
406 |
> |
(*f)(srccnt[cntord[sn].sno].val, p, |
407 |
> |
srccnt[cntord[sn].sno].dir, |
408 |
> |
srccnt[cntord[sn].sno].dom); |
409 |
> |
multcolor(srccnt[cntord[sn].sno].val, sr.rcol); |
410 |
|
} |
411 |
|
/* add contribution if hit */ |
412 |
< |
addcolor(r->rcol, srccnt[sn].val); |
413 |
< |
hit2 += hwt; |
414 |
< |
source[srccnt[sn].sno].nhits++; |
412 |
> |
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
413 |
> |
nhits++; |
414 |
> |
source[cntord[sn].sno].nhits++; |
415 |
|
} |
416 |
< |
if (test2 > FTINY) /* weighted hit rate */ |
417 |
< |
hwt = hit2 / test2; |
416 |
> |
/* surface hit rate */ |
417 |
> |
if (sn > 0) |
418 |
> |
hwt = (double)nhits / (double)sn; |
419 |
|
else |
420 |
< |
hwt = 0.0; |
420 |
> |
hwt = 0.5; |
421 |
|
#ifdef DEBUG |
422 |
< |
fprintf(stderr, "%d tested, %f hit rate\n", sn, hwt); |
422 |
> |
sprintf(errmsg, "%d tested, %d untested, %f hit rate\n", |
423 |
> |
sn, ncnts-sn, hwt); |
424 |
> |
eputs(errmsg); |
425 |
|
#endif |
426 |
|
/* add in untested sources */ |
427 |
< |
for ( ; sn < nsources; sn++) { |
428 |
< |
if (srccnt[sn].brt <= 0.0) |
429 |
< |
break; |
430 |
< |
dtmp = hwt * (double)source[srccnt[sn].sno].nhits / |
431 |
< |
(double)source[srccnt[sn].sno].ntests; |
343 |
< |
scalecolor(srccnt[sn].val, dtmp); |
344 |
< |
addcolor(r->rcol, srccnt[sn].val); |
427 |
> |
for ( ; sn < ncnts; sn++) { |
428 |
> |
prob = hwt * (double)source[cntord[sn].sno].nhits / |
429 |
> |
(double)source[cntord[sn].sno].ntests; |
430 |
> |
scalecolor(srccnt[cntord[sn].sno].val, prob); |
431 |
> |
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
432 |
|
} |
346 |
– |
|
347 |
– |
free(srccnt); |
433 |
|
} |
434 |
|
|
435 |
|
|
438 |
|
source[r->rsrc].so!=r->ro) |
439 |
|
|
440 |
|
#define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \ |
441 |
< |
!(r->rtype&REFLECTED)) /* hack! */ |
441 |
> |
!(r->rtype&REFLECTED) && /* hack! */\ |
442 |
> |
!(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3])) |
443 |
|
|
444 |
|
#define passillum(m, r) (m->otype==MAT_ILLUM && \ |
445 |
|
!(r->rsrc>=0&&source[r->rsrc].so==r->ro)) |
449 |
|
register OBJREC *m; |
450 |
|
register RAY *r; |
451 |
|
{ |
366 |
– |
/* check for behind */ |
367 |
– |
if (r->rod < 0.0) |
368 |
– |
return; |
452 |
|
/* check for over-counting */ |
453 |
|
if (wrongsource(m, r) || badambient(m, r)) |
454 |
|
return; |
462 |
|
|
463 |
|
/* otherwise treat as source */ |
464 |
|
} else { |
465 |
+ |
/* check for behind */ |
466 |
+ |
if (r->rod < 0.0) |
467 |
+ |
return; |
468 |
|
/* get distribution pattern */ |
469 |
|
raytexture(r, m->omod); |
470 |
|
/* get source color */ |
473 |
|
m->oargs.farg[2]); |
474 |
|
/* modify value */ |
475 |
|
multcolor(r->rcol, r->pcol); |
476 |
+ |
/* assign distance */ |
477 |
+ |
r->rt = r->rot; |
478 |
|
} |
479 |
|
} |
392 |
– |
|
393 |
– |
|
394 |
– |
o_source() {} /* intersection with a source is done elsewhere */ |