7 |
|
* External symbols declared in source.h |
8 |
|
*/ |
9 |
|
|
10 |
– |
#include "copyright.h" |
11 |
– |
|
10 |
|
#include "ray.h" |
13 |
– |
|
11 |
|
#include "otypes.h" |
12 |
< |
|
16 |
< |
#include "otspecial.h" |
17 |
< |
|
12 |
> |
#include "rtotypes.h" |
13 |
|
#include "source.h" |
19 |
– |
|
14 |
|
#include "random.h" |
15 |
+ |
#include "pmap.h" |
16 |
+ |
#include "pmapsrc.h" |
17 |
|
|
22 |
– |
extern double ssampdist; /* scatter sampling distance */ |
23 |
– |
|
18 |
|
#ifndef MAXSSAMP |
19 |
|
#define MAXSSAMP 16 /* maximum samples per ray */ |
20 |
|
#endif |
39 |
|
static CNTPTR *cntord; /* source ordering in direct() */ |
40 |
|
static int maxcntr = 0; /* size of contribution arrays */ |
41 |
|
|
42 |
+ |
static int cntcmp(const void *p1, const void *p2); |
43 |
|
|
44 |
+ |
|
45 |
|
OBJREC * /* find an object's actual material */ |
46 |
< |
findmaterial(register OBJREC *o) |
46 |
> |
findmaterial(OBJREC *o) |
47 |
|
{ |
48 |
|
while (!ismaterial(o->otype)) { |
53 |
– |
if (ismixture(o->otype)) |
54 |
– |
return(NULL); /* reject mixed materials */ |
49 |
|
if (o->otype == MOD_ALIAS && o->oargs.nsargs) { |
50 |
|
OBJECT aobj; |
51 |
|
OBJREC *ao; |
55 |
|
ao = objptr(aobj); |
56 |
|
if (ismaterial(ao->otype)) |
57 |
|
return(ao); |
58 |
+ |
if (ao->otype == MOD_ALIAS) { |
59 |
+ |
o = ao; |
60 |
+ |
continue; |
61 |
+ |
} |
62 |
|
} |
63 |
|
if (o->omod == OVOID) |
64 |
|
return(NULL); |
65 |
|
o = objptr(o->omod); |
66 |
|
} |
67 |
< |
return(o); |
67 |
> |
return(o); /* mixtures will return NULL */ |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
|
void |
72 |
< |
marksources() /* find and mark source objects */ |
72 |
> |
marksources(void) /* find and mark source objects */ |
73 |
|
{ |
74 |
|
int foundsource = 0; |
75 |
|
int i; |
76 |
< |
register OBJREC *o, *m; |
77 |
< |
register int ns; |
76 |
> |
OBJREC *o, *m; |
77 |
> |
int ns; |
78 |
|
/* initialize dispatch table */ |
79 |
|
initstypes(); |
80 |
|
/* find direct sources */ |
85 |
|
if (!issurface(o->otype) || o->omod == OVOID) |
86 |
|
continue; |
87 |
|
/* find material */ |
88 |
< |
m = findmaterial(o); |
89 |
< |
if (m == NULL || !islight(m->otype)) |
88 |
> |
m = findmaterial(objptr(o->omod)); |
89 |
> |
if (m == NULL) |
90 |
> |
continue; |
91 |
> |
if (m->otype == MAT_CLIP) { |
92 |
> |
markclip(m); /* special case for antimatter */ |
93 |
> |
continue; |
94 |
> |
} |
95 |
> |
if (!islight(m->otype)) |
96 |
|
continue; /* not source modifier */ |
97 |
|
|
98 |
|
if (m->oargs.nfargs != (m->otype == MAT_GLOW ? 4 : |
99 |
|
m->otype == MAT_SPOT ? 7 : 3)) |
100 |
|
objerror(m, USER, "bad # arguments"); |
101 |
|
|
98 |
– |
if (m->otype == MAT_GLOW && |
99 |
– |
o->otype != OBJ_SOURCE && |
100 |
– |
m->oargs.farg[3] <= FTINY) |
101 |
– |
continue; /* don't bother */ |
102 |
|
if (m->oargs.farg[0] <= FTINY && m->oargs.farg[1] <= FTINY && |
103 |
|
m->oargs.farg[2] <= FTINY) |
104 |
|
continue; /* don't bother */ |
105 |
< |
|
105 |
> |
if (m->otype == MAT_GLOW && |
106 |
> |
o->otype != OBJ_SOURCE && |
107 |
> |
m->oargs.farg[3] <= FTINY) { |
108 |
> |
foundsource += (ambounce > 0); |
109 |
> |
continue; /* don't track these */ |
110 |
> |
} |
111 |
|
if (sfun[o->otype].of == NULL || |
112 |
|
sfun[o->otype].of->setsrc == NULL) |
113 |
|
objerror(o, USER, "illegal material"); |
120 |
|
if (m->otype == MAT_GLOW) { |
121 |
|
source[ns].sflags |= SPROX; |
122 |
|
source[ns].sl.prox = m->oargs.farg[3]; |
123 |
< |
if (source[ns].sflags & SDISTANT) |
123 |
> |
if (source[ns].sflags & SDISTANT) { |
124 |
|
source[ns].sflags |= SSKIP; |
125 |
+ |
foundsource += (ambounce > 0); |
126 |
+ |
} |
127 |
|
} else if (m->otype == MAT_SPOT) { |
128 |
|
source[ns].sflags |= SSPOT; |
129 |
|
if ((source[ns].sl.s = makespot(m)) == NULL) |
135 |
|
source[ns].sflags |= SSKIP; |
136 |
|
} |
137 |
|
} |
138 |
< |
#if SHADCACHE |
132 |
< |
source[ns].obscache = NULL; |
133 |
< |
#endif |
134 |
< |
if (!(source[ns].sflags & SSKIP)) |
135 |
< |
foundsource++; |
138 |
> |
foundsource += !(source[ns].sflags & SSKIP); |
139 |
|
} |
140 |
|
if (!foundsource) { |
141 |
|
error(WARNING, "no light sources found"); |
142 |
|
return; |
143 |
|
} |
144 |
< |
markvirtuals(); /* find and add virtual sources */ |
144 |
> |
#if SHADCACHE |
145 |
> |
for (ns = 0; ns < nsources; ns++) /* initialize obstructor cache */ |
146 |
> |
initobscache(ns); |
147 |
> |
#endif |
148 |
> |
/* PMAP: disable virtual sources */ |
149 |
> |
if (!photonMapping) |
150 |
> |
markvirtuals(); /* find and add virtual sources */ |
151 |
> |
|
152 |
|
/* allocate our contribution arrays */ |
153 |
|
maxcntr = nsources + MAXSPART; /* start with this many */ |
154 |
|
srccnt = (CONTRIB *)malloc(maxcntr*sizeof(CONTRIB)); |
162 |
|
|
163 |
|
|
164 |
|
void |
165 |
< |
freesources() /* free all source structures */ |
165 |
> |
freesources(void) /* free all source structures */ |
166 |
|
{ |
167 |
|
if (nsources > 0) { |
168 |
|
#if SHADCACHE |
173 |
|
source = NULL; |
174 |
|
nsources = 0; |
175 |
|
} |
176 |
+ |
markclip(NULL); |
177 |
|
if (maxcntr <= 0) |
178 |
|
return; |
179 |
|
free((void *)srccnt); |
186 |
|
|
187 |
|
int |
188 |
|
srcray( /* send a ray to a source, return domega */ |
189 |
< |
register RAY *sr, /* returned source ray */ |
190 |
< |
RAY *r, /* ray which hit object */ |
191 |
< |
SRCINDEX *si /* source sample index */ |
189 |
> |
RAY *sr, /* returned source ray */ |
190 |
> |
RAY *r, /* ray which hit object */ |
191 |
> |
SRCINDEX *si /* source sample index */ |
192 |
|
) |
193 |
|
{ |
194 |
< |
double d; /* distance to source */ |
195 |
< |
register SRCREC *srcp; |
194 |
> |
double d; /* distance to source */ |
195 |
> |
SRCREC *srcp; |
196 |
|
|
197 |
< |
rayorigin(sr, r, SHADOW, 1.0); /* ignore limits */ |
197 |
> |
rayorigin(sr, SHADOW, r, NULL); /* ignore limits */ |
198 |
|
|
199 |
< |
while ((d = nextssamp(sr, si)) != 0.0) { |
200 |
< |
sr->rsrc = si->sn; /* remember source */ |
201 |
< |
srcp = source + si->sn; |
202 |
< |
if (srcp->sflags & SDISTANT) { |
203 |
< |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) |
204 |
< |
continue; |
205 |
< |
return(1); /* sample OK */ |
206 |
< |
} |
199 |
> |
if (r == NULL) |
200 |
> |
sr->rmax = 0.0; |
201 |
> |
|
202 |
> |
while ((d = nextssamp(sr, si)) != 0.0) { |
203 |
> |
sr->rsrc = si->sn; /* remember source */ |
204 |
> |
srcp = source + si->sn; |
205 |
> |
if (srcp->sflags & SDISTANT) { |
206 |
> |
if (srcp->sflags & SSPOT && spotout(sr, srcp->sl.s)) |
207 |
> |
continue; |
208 |
> |
return(1); /* sample OK */ |
209 |
> |
} |
210 |
|
/* local source */ |
211 |
|
/* check proximity */ |
212 |
< |
if (srcp->sflags & SPROX && d > srcp->sl.prox) |
199 |
< |
continue; |
200 |
< |
/* check angle */ |
201 |
< |
if (srcp->sflags & SSPOT) { |
202 |
< |
if (spotout(sr, srcp->sl.s)) |
212 |
> |
if (srcp->sflags & SPROX && d > srcp->sl.prox) |
213 |
|
continue; |
214 |
+ |
/* check angle */ |
215 |
+ |
if (srcp->sflags & SSPOT) { |
216 |
+ |
if (spotout(sr, srcp->sl.s)) |
217 |
+ |
continue; |
218 |
|
/* adjust solid angle */ |
219 |
< |
si->dom *= d*d; |
220 |
< |
d += srcp->sl.s->flen; |
221 |
< |
si->dom /= d*d; |
219 |
> |
si->dom *= d*d; |
220 |
> |
d += srcp->sl.s->flen; |
221 |
> |
si->dom /= d*d; |
222 |
> |
} |
223 |
> |
return(1); /* sample OK */ |
224 |
|
} |
225 |
< |
return(1); /* sample OK */ |
210 |
< |
} |
211 |
< |
return(0); /* no more samples */ |
225 |
> |
return(0); /* no more samples */ |
226 |
|
} |
227 |
|
|
228 |
|
|
229 |
|
void |
230 |
|
srcvalue( /* punch ray to source and compute value */ |
231 |
< |
register RAY *r |
231 |
> |
RAY *r |
232 |
|
) |
233 |
|
{ |
234 |
< |
register SRCREC *sp; |
234 |
> |
SRCREC *sp; |
235 |
|
|
236 |
|
sp = &source[r->rsrc]; |
237 |
|
if (sp->sflags & SVIRTUAL) { /* virtual source */ |
268 |
|
} |
269 |
|
|
270 |
|
|
271 |
+ |
static int |
272 |
+ |
transillum( /* check if material is transparent illum */ |
273 |
+ |
OBJECT obj |
274 |
+ |
) |
275 |
+ |
{ |
276 |
+ |
OBJREC *m = findmaterial(objptr(obj)); |
277 |
+ |
|
278 |
+ |
if (m == NULL) |
279 |
+ |
return(1); |
280 |
+ |
if (m->otype != MAT_ILLUM) |
281 |
+ |
return(0); |
282 |
+ |
return(!m->oargs.nsargs || !strcmp(m->oargs.sarg[0], VOIDID)); |
283 |
+ |
} |
284 |
+ |
|
285 |
+ |
|
286 |
|
int |
287 |
|
sourcehit( /* check to see if ray hit distant source */ |
288 |
< |
register RAY *r |
288 |
> |
RAY *r |
289 |
|
) |
290 |
|
{ |
291 |
+ |
int glowsrc = -1; |
292 |
+ |
int transrc = -1; |
293 |
|
int first, last; |
294 |
< |
register int i; |
294 |
> |
int i; |
295 |
|
|
296 |
|
if (r->rsrc >= 0) { /* check only one if aimed */ |
297 |
|
first = last = r->rsrc; |
298 |
|
} else { /* otherwise check all */ |
299 |
|
first = 0; last = nsources-1; |
300 |
|
} |
301 |
< |
for (i = first; i <= last; i++) |
302 |
< |
if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT) |
303 |
< |
/* |
304 |
< |
* Check to see if ray is within |
305 |
< |
* solid angle of source. |
306 |
< |
*/ |
307 |
< |
if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir)) |
308 |
< |
<= source[i].ss2) { |
309 |
< |
r->ro = source[i].so; |
310 |
< |
if (!(source[i].sflags & SSKIP)) |
311 |
< |
break; |
312 |
< |
} |
313 |
< |
|
314 |
< |
if (r->ro != NULL) { |
315 |
< |
r->robj = objndx(r->ro); |
316 |
< |
for (i = 0; i < 3; i++) |
317 |
< |
r->ron[i] = -r->rdir[i]; |
318 |
< |
r->rod = 1.0; |
319 |
< |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
320 |
< |
r->uv[0] = r->uv[1] = 0.0; |
321 |
< |
r->rox = NULL; |
322 |
< |
return(1); |
301 |
> |
for (i = first; i <= last; i++) { |
302 |
> |
if ((source[i].sflags & (SDISTANT|SVIRTUAL)) != SDISTANT) |
303 |
> |
continue; |
304 |
> |
/* |
305 |
> |
* Check to see if ray is within |
306 |
> |
* solid angle of source. |
307 |
> |
*/ |
308 |
> |
if (2.*PI*(1. - DOT(source[i].sloc,r->rdir)) > source[i].ss2) |
309 |
> |
continue; |
310 |
> |
/* is it the only possibility? */ |
311 |
> |
if (first == last) { |
312 |
> |
r->ro = source[i].so; |
313 |
> |
break; |
314 |
> |
} |
315 |
> |
/* |
316 |
> |
* If it's a glow or transparent illum, just remember it. |
317 |
> |
*/ |
318 |
> |
if (source[i].sflags & SSKIP) { |
319 |
> |
if (glowsrc < 0) |
320 |
> |
glowsrc = i; |
321 |
> |
continue; |
322 |
> |
} |
323 |
> |
if (transillum(source[i].so->omod)) { |
324 |
> |
if (transrc < 0) |
325 |
> |
transrc = i; |
326 |
> |
continue; |
327 |
> |
} |
328 |
> |
r->ro = source[i].so; /* otherwise, use first hit */ |
329 |
> |
break; |
330 |
|
} |
331 |
< |
return(0); |
332 |
< |
} |
333 |
< |
|
334 |
< |
|
335 |
< |
#if SHADCACHE /* preemptive shadow checking */ |
336 |
< |
#define ABS(x) ((x)>0 ? (x) : -(x)) |
337 |
< |
|
338 |
< |
static void /* find closest blockers to source */ |
339 |
< |
initobscache(SRCREC *srcp) |
340 |
< |
{ |
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); |
331 |
> |
/* |
332 |
> |
* Do we need fallback? |
333 |
> |
*/ |
334 |
> |
if (r->ro == NULL) { |
335 |
> |
if (transrc >= 0 && r->crtype & (AMBIENT|SPECULAR)) |
336 |
> |
return(0); /* avoid overcounting */ |
337 |
> |
if (glowsrc >= 0) |
338 |
> |
r->ro = source[glowsrc].so; |
339 |
> |
else |
340 |
> |
return(0); /* nothing usable */ |
341 |
|
} |
342 |
< |
/* XXX Should cast rays from source */ |
343 |
< |
for (i = cachelen; i--; ) |
344 |
< |
srcp->obscache->obs[i] = OVOID; |
342 |
> |
/* |
343 |
> |
* Make assignments. |
344 |
> |
*/ |
345 |
> |
r->robj = objndx(r->ro); |
346 |
> |
for (i = 0; i < 3; i++) |
347 |
> |
r->ron[i] = -r->rdir[i]; |
348 |
> |
r->rod = 1.0; |
349 |
> |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
350 |
> |
r->uv[0] = r->uv[1] = 0.0; |
351 |
> |
r->rox = NULL; |
352 |
> |
return(1); |
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 |
– |
if (ondx < 0 | ondx >= 4*SHADCACHE*SHADCACHE) |
386 |
– |
return &nobs; /* could happen if ray is outside */ |
387 |
– |
} else if (srcp->sflags & SFLAT) { |
388 |
– |
FVECT sd; |
389 |
– |
RREAL sd0m, sd1m; |
390 |
– |
sd[0] = -DOT(r->rdir, srcp->obscache->p.f.u); |
391 |
– |
sd[1] = -DOT(r->rdir, srcp->obscache->p.f.v); |
392 |
– |
sd[2] = -DOT(r->rdir, srcp->snorm); |
393 |
– |
if (sd[2] < 0) |
394 |
– |
return &noobs; /* shouldn't happen */ |
395 |
– |
sd0m = ABS(sd[0]); |
396 |
– |
sd1m = ABS(sd[1]); |
397 |
– |
if (sd[2] >= sd0m && sd[2] >= sd1m) { |
398 |
– |
ondx = SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * |
399 |
– |
(1. + sd[0]/sd[2])); |
400 |
– |
ondx += (int)(SHADCACHE*(.5-FTINY) * |
401 |
– |
(1. + sd[1]/sd[2])); |
402 |
– |
} else if (sd0m >= sd1m) { |
403 |
– |
ondx = SHADCACHE*SHADCACHE; |
404 |
– |
if (sd[0] < 0) |
405 |
– |
ondx += ((SHADCACHE+1)>>1)*SHADCACHE; |
406 |
– |
ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * |
407 |
– |
sd[2]/sd0m); |
408 |
– |
ondx += (int)(SHADCACHE*(.5-FTINY) * |
409 |
– |
(1. + sd[1]/sd0m)); |
410 |
– |
} else /* sd1m > sd0m */ { |
411 |
– |
ondx = SHADCACHE*SHADCACHE + |
412 |
– |
((SHADCACHE+1)>>1)*SHADCACHE*2; |
413 |
– |
if (sd[1] < 0) |
414 |
– |
ondx += ((SHADCACHE+1)>>1)*SHADCACHE; |
415 |
– |
ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * |
416 |
– |
sd[2]/sd1m); |
417 |
– |
ondx += (int)(SHADCACHE*(.5-FTINY) * |
418 |
– |
(1. + sd[0]/sd1m)); |
419 |
– |
} |
420 |
– |
} else /* spherical distribution */ { |
421 |
– |
int ax, ax1, ax2; |
422 |
– |
RREAL amax = 0; |
423 |
– |
for (ax1 = 3; ax1--; ) |
424 |
– |
if (ABS(r->rdir[ax1]) > amax) { |
425 |
– |
amax = ABS(r->rdir[ax1]); |
426 |
– |
ax = ax1; |
427 |
– |
} |
428 |
– |
if ((ax1 = ax+1) >= 3) ax1 -= 3; |
429 |
– |
if ((ax2 = ax+2) >= 3) ax2 -= 3; |
430 |
– |
ondx = 2*SHADCACHE*SHADCACHE * ax; |
431 |
– |
if (r->rdir[ax] < 0) |
432 |
– |
ondx += SHADCACHE*SHADCACHE; |
433 |
– |
ondx += SHADCACHE*(int)(SHADCACHE*(.5-FTINY) * |
434 |
– |
(1. + r->rdir[ax1]/amax)); |
435 |
– |
ondx += (int)(SHADCACHE*(.5-FTINY) * |
436 |
– |
(1. + r->rdir[ax2]/amax)); |
437 |
– |
} |
438 |
– |
/* return cache pointer */ |
439 |
– |
return(&srcp->obscache->obs[ondx]); |
440 |
– |
} |
441 |
– |
|
442 |
– |
|
443 |
– |
void /* free obstruction cache */ |
444 |
– |
freeobscache(SRCREC *srcp) |
445 |
– |
{ |
446 |
– |
if (srcp->obscache == NULL) |
447 |
– |
return; |
448 |
– |
free((void *)srcp->obscache); |
449 |
– |
srcp->obscache = NULL; |
450 |
– |
} |
451 |
– |
|
452 |
– |
|
453 |
– |
void /* record a source blocker */ |
454 |
– |
srcblocker(register RAY *r) |
455 |
– |
{ |
456 |
– |
OBJREC *m; |
457 |
– |
|
458 |
– |
if (r->robj == OVOID || objptr(r->robj) != r->ro || |
459 |
– |
isvolume(r->ro->otype)) |
460 |
– |
return; /* don't record complex blockers */ |
461 |
– |
m = findmaterial(r->ro); |
462 |
– |
if (m == NULL) |
463 |
– |
return; /* no material?! */ |
464 |
– |
if (!(ofun[m->otype].flags & T_OPAQUE)) |
465 |
– |
return; /* material not a reliable blocker */ |
466 |
– |
|
467 |
– |
*srcobstructp(r) = r->robj; /* else record obstructor */ |
468 |
– |
} |
469 |
– |
|
470 |
– |
|
471 |
– |
int /* check ray against cached blocker */ |
472 |
– |
srcblocked(RAY *r) |
473 |
– |
{ |
474 |
– |
OBJECT obs = *srcobstructp(r); |
475 |
– |
OBJREC *op; |
476 |
– |
|
477 |
– |
if (obs == OVOID) |
478 |
– |
return(0); |
479 |
– |
op = objptr(obs); /* check for intersection */ |
480 |
– |
return ((*ofun[op->otype].funp)(op, r)); |
481 |
– |
} |
482 |
– |
|
483 |
– |
#endif |
484 |
– |
|
485 |
– |
|
356 |
|
static int |
357 |
|
cntcmp( /* contribution compare (descending) */ |
358 |
< |
const void *p1, |
359 |
< |
const void *p2 |
358 |
> |
const void *p1, |
359 |
> |
const void *p2 |
360 |
|
) |
361 |
|
{ |
362 |
< |
register const CNTPTR *sc1 = (const CNTPTR *)p1; |
363 |
< |
register const CNTPTR *sc2 = (const CNTPTR *)p2; |
362 |
> |
const CNTPTR *sc1 = (const CNTPTR *)p1; |
363 |
> |
const CNTPTR *sc2 = (const CNTPTR *)p2; |
364 |
|
|
365 |
|
if (sc1->brt > sc2->brt) |
366 |
|
return(-1); |
372 |
|
|
373 |
|
void |
374 |
|
direct( /* add direct component */ |
375 |
< |
RAY *r, /* ray that hit surface */ |
376 |
< |
void (*f)(), /* direct component coefficient function */ |
377 |
< |
char *p /* data for f */ |
375 |
> |
RAY *r, /* ray that hit surface */ |
376 |
> |
srcdirf_t *f, /* direct component coefficient function */ |
377 |
> |
void *p /* data for f */ |
378 |
|
) |
379 |
|
{ |
380 |
< |
extern void (*trace)(); |
381 |
< |
register int sn; |
512 |
< |
register CONTRIB *scp; |
380 |
> |
int sn; |
381 |
> |
CONTRIB *scp; |
382 |
|
SRCINDEX si; |
383 |
|
int nshadcheck, ncnts; |
384 |
|
int nhits; |
385 |
|
double prob, ourthresh, hwt; |
386 |
|
RAY sr; |
387 |
+ |
|
388 |
+ |
/* PMAP: Factor in direct photons (primarily for debugging/validation) */ |
389 |
+ |
if (directPhotonMapping) { |
390 |
+ |
(*f)(r -> rcol, p, r -> ron, PI); |
391 |
+ |
multDirectPmap(r); |
392 |
+ |
return; |
393 |
+ |
} |
394 |
+ |
|
395 |
|
/* NOTE: srccnt and cntord global so no recursion */ |
396 |
|
if (nsources <= 0) |
397 |
|
return; /* no sources?! */ |
412 |
|
scp->sno = sr.rsrc; |
413 |
|
/* compute coefficient */ |
414 |
|
(*f)(scp->coef, p, sr.rdir, si.dom); |
415 |
< |
cntord[sn].brt = bright(scp->coef); |
415 |
> |
cntord[sn].brt = intens(scp->coef); |
416 |
|
if (cntord[sn].brt <= 0.0) |
417 |
|
continue; |
418 |
|
#if SHADCACHE |
423 |
|
} |
424 |
|
#endif |
425 |
|
VCOPY(scp->dir, sr.rdir); |
426 |
+ |
copycolor(sr.rcoef, scp->coef); |
427 |
|
/* compute potential */ |
428 |
|
sr.revf = srcvalue; |
429 |
|
rayvalue(&sr); |
430 |
+ |
multcolor(sr.rcol, sr.rcoef); |
431 |
|
copycolor(scp->val, sr.rcol); |
432 |
< |
multcolor(scp->val, scp->coef); |
554 |
< |
cntord[sn].brt = bright(scp->val); |
432 |
> |
cntord[sn].brt = bright(sr.rcol); |
433 |
|
} |
434 |
|
/* sort contributions */ |
435 |
|
qsort(cntord, sn, sizeof(CNTPTR), cntcmp); |
436 |
|
{ /* find last */ |
437 |
< |
register int l, m; |
437 |
> |
int l, m; |
438 |
|
|
439 |
|
ncnts = l = sn; |
440 |
|
sn = 0; |
454 |
|
/* compute number to check */ |
455 |
|
nshadcheck = pow((double)ncnts, shadcert) + .5; |
456 |
|
/* modify threshold */ |
457 |
< |
ourthresh = shadthresh / r->rweight; |
457 |
> |
if (ncnts > MINSHADCNT) |
458 |
> |
ourthresh = shadthresh / r->rweight; |
459 |
> |
else |
460 |
> |
ourthresh = 0; |
461 |
|
/* test for shadows */ |
462 |
|
for (nhits = 0, hwt = 0.0, sn = 0; sn < ncnts; |
463 |
|
hwt += (double)source[scp->sno].nhits / |
470 |
|
break; |
471 |
|
scp = srccnt + cntord[sn].sndx; |
472 |
|
/* test for hit */ |
473 |
< |
rayorigin(&sr, r, SHADOW, 1.0); |
473 |
> |
rayorigin(&sr, SHADOW, r, NULL); |
474 |
> |
copycolor(sr.rcoef, scp->coef); |
475 |
|
VCOPY(sr.rdir, scp->dir); |
476 |
|
sr.rsrc = scp->sno; |
477 |
|
/* keep statistics */ |
484 |
|
source[scp->sno].sflags & SFOLLOW )) { |
485 |
|
/* follow entire path */ |
486 |
|
raycont(&sr); |
605 |
– |
rayparticipate(&sr); |
487 |
|
if (trace != NULL) |
488 |
|
(*trace)(&sr); /* trace execution */ |
489 |
|
if (bright(sr.rcol) <= FTINY) { |
490 |
|
#if SHADCACHE |
491 |
|
if ((scp <= srccnt || scp[-1].sno != scp->sno) |
492 |
< |
&& (scp >= srccnt+ncnts || |
492 |
> |
&& (scp >= srccnt+ncnts-1 || |
493 |
|
scp[1].sno != scp->sno)) |
494 |
|
srcblocker(&sr); |
495 |
|
#endif |
496 |
|
continue; /* missed! */ |
497 |
|
} |
498 |
+ |
rayparticipate(&sr); |
499 |
+ |
multcolor(sr.rcol, sr.rcoef); |
500 |
|
copycolor(scp->val, sr.rcol); |
501 |
< |
multcolor(scp->val, scp->coef); |
501 |
> |
} else if (trace != NULL && |
502 |
> |
(source[scp->sno].sflags & (SDISTANT|SVIRTUAL|SFOLLOW)) |
503 |
> |
== (SDISTANT|SFOLLOW) && |
504 |
> |
sourcehit(&sr) && rayshade(&sr, sr.ro->omod)) { |
505 |
> |
(*trace)(&sr); /* trace execution */ |
506 |
> |
/* skip call to rayparticipate() & scp->val update */ |
507 |
|
} |
508 |
|
/* add contribution if hit */ |
509 |
|
addcolor(r->rcol, scp->val); |
525 |
|
scp = srccnt + cntord[sn].sndx; |
526 |
|
prob = hwt * (double)source[scp->sno].nhits / |
527 |
|
(double)source[scp->sno].ntests; |
528 |
< |
if (prob > 1.0) |
529 |
< |
prob = 1.0; |
642 |
< |
scalecolor(scp->val, prob); |
528 |
> |
if (prob < 1.0) |
529 |
> |
scalecolor(scp->val, prob); |
530 |
|
addcolor(r->rcol, scp->val); |
531 |
|
} |
532 |
|
} |
534 |
|
|
535 |
|
void |
536 |
|
srcscatter( /* compute source scattering into ray */ |
537 |
< |
register RAY *r |
537 |
> |
RAY *r |
538 |
|
) |
539 |
|
{ |
540 |
|
int oldsampndx; |
543 |
|
SRCINDEX si; |
544 |
|
double t, d; |
545 |
|
double re, ge, be; |
546 |
< |
COLOR cvext; |
546 |
> |
COLOR cvext, pmapInscatter; |
547 |
|
int i, j; |
548 |
|
|
549 |
+ |
/* PMAP: do unconditional inscattering for volume photons ? */ |
550 |
+ |
/* if (!volumePhotonMapping) */ |
551 |
|
if (r->slights == NULL || r->slights[0] == 0 |
552 |
|
|| r->gecc >= 1.-FTINY || r->rot >= FHUGE) |
553 |
|
return; |
554 |
+ |
|
555 |
|
if (ssampdist <= FTINY || (nsamps = r->rot/ssampdist + .5) < 1) |
556 |
|
nsamps = 1; |
557 |
|
#if MAXSSAMP |
576 |
|
sr.rorg[0] = r->rorg[0] + r->rdir[0]*t; |
577 |
|
sr.rorg[1] = r->rorg[1] + r->rdir[1]*t; |
578 |
|
sr.rorg[2] = r->rorg[2] + r->rdir[2]*t; |
689 |
– |
sr.rmax = 0.; |
579 |
|
initsrcindex(&si); /* sample ray to this source */ |
580 |
|
si.sn = r->slights[i]; |
581 |
|
nopart(&si, &sr); |
582 |
|
if (!srcray(&sr, NULL, &si) || |
583 |
|
sr.rsrc != r->slights[i]) |
584 |
|
continue; /* no path */ |
585 |
+ |
#if SHADCACHE |
586 |
+ |
if (srcblocked(&sr)) /* check shadow cache */ |
587 |
+ |
continue; |
588 |
+ |
#endif |
589 |
|
copycolor(sr.cext, r->cext); |
590 |
|
copycolor(sr.albedo, r->albedo); |
591 |
|
sr.gecc = r->gecc; |
592 |
|
sr.slights = r->slights; |
593 |
|
rayvalue(&sr); /* eval. source ray */ |
594 |
< |
if (bright(sr.rcol) <= FTINY) |
594 |
> |
if (bright(sr.rcol) <= FTINY) { |
595 |
> |
#if SHADCACHE |
596 |
> |
srcblocker(&sr); /* add blocker to cache */ |
597 |
> |
#endif |
598 |
|
continue; |
599 |
+ |
} |
600 |
|
if (r->gecc <= FTINY) /* compute P(theta) */ |
601 |
|
d = 1.; |
602 |
|
else { |
606 |
|
} |
607 |
|
/* other factors */ |
608 |
|
d *= si.dom * r->rot / (4.*PI*nsamps); |
609 |
+ |
scalecolor(sr.rcol, d); |
610 |
+ |
|
611 |
+ |
/* PMAP: Add ambient inscattering from volume photons once only */ |
612 |
+ |
if (volumePhotonMapping && i == 1) { |
613 |
+ |
inscatterVolumePmap(&sr, pmapInscatter); |
614 |
+ |
scalecolor(pmapInscatter, r -> rot / nsamps); |
615 |
+ |
addcolor(sr.rcol, pmapInscatter); |
616 |
+ |
} |
617 |
+ |
|
618 |
|
multcolor(sr.rcol, r->cext); |
619 |
|
multcolor(sr.rcol, r->albedo); |
714 |
– |
scalecolor(sr.rcol, d); |
620 |
|
multcolor(sr.rcol, cvext); |
621 |
|
addcolor(r->rcol, sr.rcol); /* add it in */ |
622 |
|
} |
642 |
|
*/ |
643 |
|
|
644 |
|
static int |
645 |
< |
weaksrcmat(int obj) /* identify material */ |
645 |
> |
weaksrcmat(OBJECT obj) /* identify material */ |
646 |
|
{ |
647 |
< |
register OBJREC *o = objptr(obj); |
647 |
> |
OBJREC *m = findmaterial(objptr(obj)); |
648 |
|
|
649 |
< |
while (!ismaterial(o->otype)) /* find material */ |
650 |
< |
o = objptr(o->omod); |
746 |
< |
return((o->otype==MAT_ILLUM)|(o->otype==MAT_GLOW)); |
649 |
> |
if (m == NULL) return(0); |
650 |
> |
return((m->otype==MAT_ILLUM) | (m->otype==MAT_GLOW)); |
651 |
|
} |
652 |
|
|
653 |
|
#define illumblock(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ |
710 |
|
|
711 |
|
int |
712 |
|
m_light( /* ray hit a light source */ |
713 |
< |
register OBJREC *m, |
714 |
< |
register RAY *r |
713 |
> |
OBJREC *m, |
714 |
> |
RAY *r |
715 |
|
) |
716 |
|
{ |
717 |
|
/* check for over-counting */ |
718 |
< |
if (badcomponent(m, r)) |
718 |
> |
if (badcomponent(m, r)) { |
719 |
> |
setcolor(r->rcoef, 0.0, 0.0, 0.0); |
720 |
|
return(1); |
721 |
< |
if (wrongsource(m, r)) |
721 |
> |
} |
722 |
> |
if (wrongsource(m, r)) { |
723 |
> |
setcolor(r->rcoef, 0.0, 0.0, 0.0); |
724 |
|
return(1); |
725 |
+ |
} |
726 |
|
/* check for passed illum */ |
727 |
|
if (passillum(m, r)) { |
728 |
|
if (m->oargs.nsargs && strcmp(m->oargs.sarg[0], VOIDID)) |
730 |
|
raytrans(r); |
731 |
|
return(1); |
732 |
|
} |
733 |
+ |
/* check for invisibility */ |
734 |
+ |
if (srcignore(m, r)) { |
735 |
+ |
setcolor(r->rcoef, 0.0, 0.0, 0.0); |
736 |
+ |
return(1); |
737 |
+ |
} |
738 |
|
/* otherwise treat as source */ |
739 |
|
/* check for behind */ |
740 |
|
if (r->rod < 0.0) |
828 |
– |
return(1); |
829 |
– |
/* check for invisibility */ |
830 |
– |
if (srcignore(m, r)) |
741 |
|
return(1); |
742 |
|
/* check for outside spot */ |
743 |
|
if (m->otype==MAT_SPOT && spotout(r, makespot(m))) |