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"; |
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 |
+ |
int i; |
42 |
|
register OBJREC *o, *m; |
43 |
< |
register int i; |
43 |
> |
register SRCREC *ns; |
44 |
|
|
45 |
|
for (i = 0; i < nobjects; i++) { |
46 |
|
|
51 |
|
|
52 |
|
m = objptr(o->omod); |
53 |
|
|
54 |
< |
if (m->otype != MAT_LIGHT && |
51 |
< |
m->otype != MAT_ILLUM && |
52 |
< |
m->otype != MAT_GLOW && |
53 |
< |
m->otype != MAT_SPOT) |
54 |
> |
if (!islight(m->otype)) |
55 |
|
continue; |
56 |
|
|
57 |
|
if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 : |
63 |
|
m->oargs.farg[3] <= FTINY) |
64 |
|
continue; /* don't bother */ |
65 |
|
|
66 |
< |
if (source == NULL) |
67 |
< |
source = (SRCREC *)malloc(sizeof(SRCREC)); |
67 |
< |
else |
68 |
< |
source = (SRCREC *)realloc((char *)source, |
69 |
< |
(unsigned)(nsources+1)*sizeof(SRCREC)); |
70 |
< |
if (source == NULL) |
71 |
< |
error(SYSTEM, "out of memory in marksources"); |
66 |
> |
if ((ns = newsource()) == NULL) |
67 |
> |
goto memerr; |
68 |
|
|
69 |
< |
newsource(&source[nsources], o); |
69 |
> |
setsource(ns, o); |
70 |
|
|
71 |
|
if (m->otype == MAT_GLOW) { |
72 |
< |
source[nsources].sflags |= SPROX; |
73 |
< |
source[nsources].sl.prox = m->oargs.farg[3]; |
72 |
> |
ns->sflags |= SPROX; |
73 |
> |
ns->sl.prox = m->oargs.farg[3]; |
74 |
|
if (o->otype == OBJ_SOURCE) |
75 |
< |
source[nsources].sflags |= SSKIP; |
75 |
> |
ns->sflags |= SSKIP; |
76 |
|
} else if (m->otype == MAT_SPOT) { |
77 |
< |
source[nsources].sflags |= SSPOT; |
78 |
< |
source[nsources].sl.s = makespot(m); |
77 |
> |
ns->sflags |= SSPOT; |
78 |
> |
if ((ns->sl.s = makespot(m)) == NULL) |
79 |
> |
goto memerr; |
80 |
|
} |
84 |
– |
nsources++; |
81 |
|
} |
82 |
+ |
if (nsources <= 0) { |
83 |
+ |
error(WARNING, "no light sources found"); |
84 |
+ |
return; |
85 |
+ |
} |
86 |
+ |
markvirtuals(); /* find and add virtual sources */ |
87 |
+ |
srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB)); |
88 |
+ |
cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR)); |
89 |
+ |
if (srccnt != NULL && cntord != NULL) |
90 |
+ |
goto memerr; |
91 |
+ |
return; |
92 |
+ |
memerr: |
93 |
+ |
error(SYSTEM, "out of memory in marksources"); |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
< |
newsource(src, so) /* add a source to the array */ |
97 |
> |
SRCREC * |
98 |
> |
newsource() /* allocate new source in our array */ |
99 |
> |
{ |
100 |
> |
if (nsources == 0) |
101 |
> |
source = (SRCREC *)malloc(sizeof(SRCREC)); |
102 |
> |
else |
103 |
> |
source = (SRCREC *)realloc((char *)source, |
104 |
> |
(unsigned)(nsources+1)*sizeof(SRCREC)); |
105 |
> |
if (source == NULL) |
106 |
> |
return(NULL); |
107 |
> |
source[nsources].sflags = 0; |
108 |
> |
source[nsources].nhits = 1; |
109 |
> |
source[nsources].ntests = 2; /* initial hit probability = 1/2 */ |
110 |
> |
return(&source[nsources++]); |
111 |
> |
} |
112 |
> |
|
113 |
> |
|
114 |
> |
setsource(src, so) /* add a source to the array */ |
115 |
|
register SRCREC *src; |
116 |
|
register OBJREC *so; |
117 |
|
{ |
122 |
|
int j; |
123 |
|
register int i; |
124 |
|
|
125 |
< |
src->sflags = 0; |
101 |
< |
src->nhits = 1; src->ntests = 2; /* start probability = 1/2 */ |
125 |
> |
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
126 |
|
src->so = so; |
127 |
|
|
128 |
|
switch (so->otype) { |
152 |
|
src->sloc[j] = 0.0; |
153 |
|
for (i = 0; i < f->nv; i++) |
154 |
|
src->sloc[j] += VERTEX(f,i)[j]; |
155 |
< |
src->sloc[j] /= f->nv; |
155 |
> |
src->sloc[j] /= (double)f->nv; |
156 |
|
} |
157 |
|
if (!inface(src->sloc, f)) |
158 |
|
objerror(so, USER, "cannot hit center"); |
159 |
+ |
src->sflags |= SFLAT; |
160 |
+ |
VCOPY(src->snorm, f->norm); |
161 |
|
src->ss = sqrt(f->area / PI); |
162 |
|
src->ss2 = f->area; |
163 |
|
break; |
167 |
|
VCOPY(src->sloc, CO_P0(co)); |
168 |
|
if (CO_R0(co) > 0.0) |
169 |
|
objerror(so, USER, "cannot hit center"); |
170 |
+ |
src->sflags |= SFLAT; |
171 |
+ |
VCOPY(src->snorm, co->ad); |
172 |
|
src->ss = CO_R1(co); |
173 |
|
src->ss2 = PI * src->ss * src->ss; |
174 |
|
break; |
186 |
|
register SPOT *ns; |
187 |
|
|
188 |
|
if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
189 |
< |
error(SYSTEM, "out of memory in makespot"); |
189 |
> |
return(NULL); |
190 |
|
ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3])); |
191 |
|
VCOPY(ns->aim, m->oargs.farg+4); |
192 |
|
if ((ns->flen = normalize(ns->aim)) == 0.0) |
201 |
|
RAY *r; /* ray which hit object */ |
202 |
|
register int sn; /* source number */ |
203 |
|
{ |
176 |
– |
register double *norm = NULL; /* plane normal */ |
204 |
|
double ddot; /* (distance times) cosine */ |
205 |
|
FVECT vd; |
206 |
|
double d; |
213 |
|
|
214 |
|
sr->rsrc = sn; /* remember source */ |
215 |
|
/* get source direction */ |
216 |
< |
if (source[sn].sflags & SDISTANT) |
216 |
> |
if (source[sn].sflags & SDISTANT) { |
217 |
> |
if (source[sn].sflags & SSPOT) { /* check location */ |
218 |
> |
for (i = 0; i < 3; i++) |
219 |
> |
vd[i] = sr->rorg[i] - source[sn].sl.s->aim[i]; |
220 |
> |
d = DOT(source[sn].sloc,vd); |
221 |
> |
d = DOT(vd,vd) - d*d; |
222 |
> |
if (PI*d > source[sn].sl.s->siz) |
223 |
> |
return(0.0); |
224 |
> |
} |
225 |
|
/* constant direction */ |
226 |
|
VCOPY(sr->rdir, source[sn].sloc); |
227 |
< |
else { /* compute direction */ |
227 |
> |
} else { /* compute direction */ |
228 |
|
for (i = 0; i < 3; i++) |
229 |
|
sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i]; |
230 |
|
|
231 |
< |
if (source[sn].so->otype == OBJ_FACE) |
232 |
< |
norm = getface(source[sn].so)->norm; |
198 |
< |
else if (source[sn].so->otype == OBJ_RING) |
199 |
< |
norm = getcone(source[sn].so,0)->ad; |
200 |
< |
|
201 |
< |
if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY) |
231 |
> |
if (source[sn].sflags & SFLAT && |
232 |
> |
(ddot = -DOT(sr->rdir, source[sn].snorm)) <= FTINY) |
233 |
|
return(0.0); /* behind surface! */ |
234 |
|
} |
235 |
|
if (dstrsrc > FTINY) { |
236 |
|
/* distribute source direction */ |
237 |
< |
for (i = 0; i < 3; i++) |
238 |
< |
vd[i] = dstrsrc * source[sn].ss * (1.0 - 2.0*frandom()); |
239 |
< |
|
240 |
< |
if (norm != NULL) { /* project offset */ |
241 |
< |
d = DOT(vd, norm); |
237 |
> |
dimlist[ndims++] = sn; |
238 |
> |
for (i = 0; i < 3; i++) { |
239 |
> |
dimlist[ndims] = i + 8831; |
240 |
> |
vd[i] = dstrsrc * source[sn].ss * |
241 |
> |
(1.0 - 2.0*urand(ilhash(dimlist,ndims+1)+samplendx)); |
242 |
> |
} |
243 |
> |
ndims--; |
244 |
> |
if (source[sn].sflags & SFLAT) { /* project offset */ |
245 |
> |
d = DOT(vd, source[sn].snorm); |
246 |
|
for (i = 0; i < 3; i++) |
247 |
< |
vd[i] -= d * norm[i]; |
247 |
> |
vd[i] -= d * source[sn].snorm[i]; |
248 |
|
} |
249 |
|
for (i = 0; i < 3; i++) /* offset source direction */ |
250 |
|
sr->rdir[i] += vd[i]; |
261 |
|
/* domega constant */ |
262 |
|
return(source[sn].ss2); |
263 |
|
|
229 |
– |
else { |
264 |
|
/* check proximity */ |
265 |
< |
if (source[sn].sflags & SPROX && |
266 |
< |
d > source[sn].sl.prox) |
267 |
< |
return(0.0); |
268 |
< |
|
269 |
< |
if (norm != NULL) |
270 |
< |
ddot /= d; |
271 |
< |
else |
272 |
< |
ddot = 1.0; |
265 |
> |
if (source[sn].sflags & SPROX && |
266 |
> |
d > source[sn].sl.prox) |
267 |
> |
return(0.0); |
268 |
> |
/* compute dot product */ |
269 |
> |
if (source[sn].sflags & SFLAT) |
270 |
> |
ddot /= d; |
271 |
> |
else |
272 |
> |
ddot = 1.0; |
273 |
|
/* check angle */ |
274 |
< |
if (source[sn].sflags & SSPOT) { |
275 |
< |
if (source[sn].sl.s->siz < 2.0*PI * |
274 |
> |
if (source[sn].sflags & SSPOT) { |
275 |
> |
if (source[sn].sl.s->siz < 2.0*PI * |
276 |
|
(1.0 + DOT(source[sn].sl.s->aim,sr->rdir))) |
277 |
< |
return(0.0); |
278 |
< |
d += source[sn].sl.s->flen; |
245 |
< |
} |
246 |
< |
/* return domega */ |
247 |
< |
return(ddot*source[sn].ss2/(d*d)); |
277 |
> |
return(0.0); |
278 |
> |
d += source[sn].sl.s->flen; /* adjust length */ |
279 |
|
} |
280 |
+ |
/* compute domega */ |
281 |
+ |
return(ddot*source[sn].ss2/(d*d)); |
282 |
|
} |
283 |
|
|
284 |
|
|
310 |
|
for (i = 0; i < 3; i++) |
311 |
|
r->ron[i] = -r->rdir[i]; |
312 |
|
r->rod = 1.0; |
313 |
< |
r->rofs = 1.0; setident4(r->rofx); |
281 |
< |
r->robs = 1.0; setident4(r->robx); |
313 |
> |
r->rox = NULL; |
314 |
|
return(1); |
315 |
|
} |
316 |
|
return(0); |
336 |
|
{ |
337 |
|
extern double pow(); |
338 |
|
register int sn; |
307 |
– |
register CONTRIB *srccnt; |
308 |
– |
register CNTPTR *cntord; |
339 |
|
int nshadcheck, ncnts; |
340 |
< |
double prob, ourthresh, hwt, test2, hit2; |
340 |
> |
int nhits; |
341 |
> |
double dom, prob, ourthresh, hwt; |
342 |
|
RAY sr; |
343 |
< |
|
344 |
< |
srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB)); |
345 |
< |
cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR)); |
315 |
< |
if (srccnt == NULL || cntord == NULL) |
316 |
< |
error(SYSTEM, "out of memory in direct"); |
343 |
> |
/* NOTE: srccnt and cntord global so no recursion */ |
344 |
> |
if (nsources <= 0) |
345 |
> |
return; /* no sources?! */ |
346 |
|
/* compute number to check */ |
347 |
|
nshadcheck = pow((double)nsources, shadcert) + .5; |
348 |
|
/* modify threshold */ |
352 |
|
cntord[sn].sno = sn; |
353 |
|
cntord[sn].brt = 0.0; |
354 |
|
/* get source ray */ |
355 |
< |
if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0) |
355 |
> |
if ((dom = srcray(&sr, r, sn)) == 0.0) |
356 |
|
continue; |
357 |
|
VCOPY(srccnt[sn].dir, sr.rdir); |
358 |
|
/* compute coefficient */ |
359 |
< |
(*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom); |
360 |
< |
cntord[sn].brt = bright(srccnt[sn].val); |
359 |
> |
(*f)(srccnt[sn].coef, p, srccnt[sn].dir, dom); |
360 |
> |
cntord[sn].brt = bright(srccnt[sn].coef); |
361 |
|
if (cntord[sn].brt <= 0.0) |
362 |
|
continue; |
334 |
– |
/* compute intersection */ |
335 |
– |
if (!( source[sn].sflags & SDISTANT ? |
336 |
– |
sourcehit(&sr) : |
337 |
– |
(*ofun[source[sn].so->otype].funp) |
338 |
– |
(source[sn].so, &sr) )) |
339 |
– |
continue; |
363 |
|
/* compute contribution */ |
364 |
< |
rayshade(&sr, sr.ro->omod); |
365 |
< |
multcolor(srccnt[sn].val, sr.rcol); |
364 |
> |
srcvalue(&sr); |
365 |
> |
copycolor(srccnt[sn].val, sr.rcol); |
366 |
> |
multcolor(srccnt[sn].val, srccnt[sn].coef); |
367 |
|
cntord[sn].brt = bright(srccnt[sn].val); |
368 |
|
} |
369 |
|
/* sort contributions */ |
383 |
|
/* accumulate tail */ |
384 |
|
for (sn = ncnts-1; sn > 0; sn--) |
385 |
|
cntord[sn-1].brt += cntord[sn].brt; |
362 |
– |
/* start with prob=.5 */ |
363 |
– |
hit2 = 0.5; test2 = 1.0; |
386 |
|
/* test for shadows */ |
387 |
+ |
nhits = 0; |
388 |
|
for (sn = 0; sn < ncnts; sn++) { |
389 |
|
/* check threshold */ |
390 |
|
if ((sn+nshadcheck>=ncnts ? cntord[sn].brt : |
391 |
< |
cntord[sn].brt-cntord[sn+nshadcheck].brt) < |
392 |
< |
ourthresh*bright(r->rcol)) |
391 |
> |
cntord[sn].brt-cntord[sn+nshadcheck].brt) |
392 |
> |
< ourthresh*bright(r->rcol)) |
393 |
|
break; |
394 |
|
/* get statistics */ |
372 |
– |
hwt = (double)source[cntord[sn].sno].nhits / |
373 |
– |
(double)source[cntord[sn].sno].ntests; |
374 |
– |
test2 += hwt; |
395 |
|
source[cntord[sn].sno].ntests++; |
396 |
|
/* test for hit */ |
397 |
|
rayorigin(&sr, r, SHADOW, 1.0); |
398 |
|
VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir); |
399 |
+ |
sr.rsrc = cntord[sn].sno; |
400 |
|
if (localhit(&sr, &thescene) && |
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); |
384 |
< |
else |
385 |
< |
rayshade(&sr, sr.ro->omod); |
401 |
> |
( sr.ro != source[cntord[sn].sno].so || |
402 |
> |
source[cntord[sn].sno].sflags & SFOLLOW )) { |
403 |
> |
/* follow entire path */ |
404 |
> |
raycont(&sr); |
405 |
|
if (bright(sr.rcol) <= FTINY) |
406 |
|
continue; /* missed! */ |
407 |
< |
(*f)(srccnt[cntord[sn].sno].val, p, |
408 |
< |
srccnt[cntord[sn].sno].dir, |
409 |
< |
srccnt[cntord[sn].sno].dom); |
391 |
< |
multcolor(srccnt[cntord[sn].sno].val, sr.rcol); |
407 |
> |
copycolor(srccnt[cntord[sn].sno].val, sr.rcol); |
408 |
> |
multcolor(srccnt[cntord[sn].sno].val, |
409 |
> |
srccnt[cntord[sn].sno].coef); |
410 |
|
} |
411 |
|
/* add contribution if hit */ |
412 |
|
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
413 |
< |
hit2 += hwt; |
413 |
> |
nhits++; |
414 |
|
source[cntord[sn].sno].nhits++; |
415 |
|
} |
416 |
< |
/* weighted hit rate */ |
417 |
< |
hwt = hit2 / test2; |
418 |
< |
#ifdef notdef |
416 |
> |
/* surface hit rate */ |
417 |
> |
if (sn > 0) |
418 |
> |
hwt = (double)nhits / (double)sn; |
419 |
> |
else |
420 |
> |
hwt = 0.5; |
421 |
> |
#ifdef DEBUG |
422 |
|
sprintf(errmsg, "%d tested, %d untested, %f hit rate\n", |
423 |
|
sn, ncnts-sn, hwt); |
424 |
|
eputs(errmsg); |
430 |
|
scalecolor(srccnt[cntord[sn].sno].val, prob); |
431 |
|
addcolor(r->rcol, srccnt[cntord[sn].sno].val); |
432 |
|
} |
412 |
– |
|
413 |
– |
free(srccnt); |
414 |
– |
free(cntord); |
433 |
|
} |
434 |
|
|
435 |
|
|
436 |
+ |
srcvalue(r) /* punch ray to source and compute value */ |
437 |
+ |
RAY *r; |
438 |
+ |
{ |
439 |
+ |
register SRCREC *sp; |
440 |
+ |
|
441 |
+ |
sp = &source[r->rsrc]; |
442 |
+ |
if (sp->sflags & SVIRTUAL) { /* virtual source */ |
443 |
+ |
RAY nr; |
444 |
+ |
/* check intersection */ |
445 |
+ |
if (!(*ofun[sp->so->otype].funp)(sp->so, r)) |
446 |
+ |
return; |
447 |
+ |
/* relay ray to source */ |
448 |
+ |
vsrcrelay(&nr, r); |
449 |
+ |
srcvalue(&nr); |
450 |
+ |
return; |
451 |
+ |
} |
452 |
+ |
/* compute intersection */ |
453 |
+ |
if (sp->sflags & SDISTANT ? sourcehit(r) : |
454 |
+ |
(*ofun[sp->so->otype].funp)(sp->so, r)) { |
455 |
+ |
if (sp->sa.success >= 0) |
456 |
+ |
sp->sa.success++; |
457 |
+ |
raycont(r); /* compute contribution */ |
458 |
+ |
return; |
459 |
+ |
} |
460 |
+ |
if (sp->sa.success < 0) |
461 |
+ |
return; /* bitched already */ |
462 |
+ |
sp->sa.success -= AIMREQT; |
463 |
+ |
if (sp->sa.success >= 0) |
464 |
+ |
return; /* leniency */ |
465 |
+ |
sprintf(errmsg, "aiming failure for light source \"%s\"", |
466 |
+ |
sp->so->oname); |
467 |
+ |
error(WARNING, errmsg); /* issue warning */ |
468 |
+ |
} |
469 |
+ |
|
470 |
+ |
|
471 |
|
#define wrongsource(m, r) (m->otype!=MAT_ILLUM && \ |
472 |
|
r->rsrc>=0 && \ |
473 |
|
source[r->rsrc].so!=r->ro) |
474 |
|
|
475 |
|
#define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \ |
423 |
– |
!(r->rtype&REFLECTED) && /* hack! */\ |
476 |
|
!(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3])) |
477 |
|
|
478 |
|
#define passillum(m, r) (m->otype==MAT_ILLUM && \ |
509 |
|
multcolor(r->rcol, r->pcol); |
510 |
|
} |
511 |
|
} |
460 |
– |
|
461 |
– |
|
462 |
– |
o_source() {} /* intersection with a source is done elsewhere */ |