14 |
|
#include "otypes.h" |
15 |
|
#include "otspecial.h" |
16 |
|
#include "random.h" |
17 |
+ |
#include "pmap.h" |
18 |
|
|
19 |
|
#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ |
20 |
|
|
41 |
|
RAY *r, |
42 |
|
int rt, |
43 |
|
const RAY *ro, |
44 |
< |
const COLOR rc |
44 |
> |
const SCOLOR rc |
45 |
|
) |
46 |
|
{ |
47 |
|
double rw, re; |
48 |
|
/* assign coefficient/weight */ |
49 |
|
if (rc == NULL) { |
50 |
|
rw = 1.0; |
51 |
< |
setcolor(r->rcoef, 1., 1., 1.); |
51 |
> |
setscolor(r->rcoef, 1., 1., 1.); |
52 |
|
} else { |
53 |
< |
rw = intens(rc); |
53 |
> |
rw = sintens((COLORV *)rc); |
54 |
> |
if (rw > 1.0) |
55 |
> |
rw = 1.0; /* avoid calculation growth */ |
56 |
|
if (rc != r->rcoef) |
57 |
< |
copycolor(r->rcoef, rc); |
57 |
> |
copyscolor(r->rcoef, rc); |
58 |
|
} |
59 |
|
if ((r->parent = ro) == NULL) { /* primary ray */ |
60 |
|
r->rlvl = 0; |
68 |
|
r->gecc = seccg; |
69 |
|
r->slights = NULL; |
70 |
|
} else { /* spawned ray */ |
71 |
< |
if (ro->rot >= FHUGE) { |
71 |
> |
if (ro->rot >= FHUGE*.99) { |
72 |
|
memset(r, 0, sizeof(RAY)); |
73 |
|
return(-1); /* illegal continuation */ |
74 |
|
} |
75 |
|
r->rlvl = ro->rlvl; |
76 |
+ |
r->rsrc = ro->rsrc; |
77 |
|
if (rt & RAYREFL) { |
78 |
|
r->rlvl++; |
79 |
< |
r->rsrc = -1; |
79 |
> |
if (r->rsrc >= 0) /* malfunctioning material? */ |
80 |
> |
r->rsrc = -1; |
81 |
|
r->clipset = ro->clipset; |
82 |
|
r->rmax = 0.0; |
83 |
|
} else { |
79 |
– |
r->rsrc = ro->rsrc; |
84 |
|
r->clipset = ro->newcset; |
85 |
< |
r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot; |
85 |
> |
r->rmax = (ro->rmax > FTINY)*(ro->rmax - ro->rot); |
86 |
|
} |
87 |
|
r->revf = ro->revf; |
88 |
|
copycolor(r->cext, ro->cext); |
110 |
|
return(-1); |
111 |
|
if (r->crtype & SHADOW) /* shadow commitment */ |
112 |
|
return(0); |
113 |
< |
if (maxdepth <= 0 && rc != NULL) { /* Russian roulette */ |
113 |
> |
/* ambient in photon map? */ |
114 |
> |
if (ro != NULL && ro->crtype & AMBIENT) { |
115 |
> |
if (causticPhotonMapping) |
116 |
> |
return(-1); |
117 |
> |
if (photonMapping && rt != TRANS) |
118 |
> |
return(-1); |
119 |
> |
} |
120 |
> |
if ((maxdepth <= 0) & (rc != NULL)) { /* Russian roulette */ |
121 |
|
if (minweight <= 0.0) |
122 |
|
error(USER, "zero ray weight in Russian roulette"); |
123 |
< |
if (maxdepth < 0 && r->rlvl > -maxdepth) |
123 |
> |
if ((maxdepth < 0) & (r->rlvl > -maxdepth)) |
124 |
|
return(-1); /* upper reflection limit */ |
125 |
|
if (r->rweight >= minweight) |
126 |
|
return(0); |
127 |
|
if (frandom() > r->rweight/minweight) |
128 |
|
return(-1); |
129 |
|
rw = minweight/r->rweight; /* promote survivor */ |
130 |
< |
scalecolor(r->rcoef, rw); |
130 |
> |
scalescolor(r->rcoef, rw); |
131 |
|
r->rweight = minweight; |
132 |
|
return(0); |
133 |
|
} |
134 |
< |
return(r->rweight >= minweight && r->rlvl <= abs(maxdepth) ? 0 : -1); |
134 |
> |
return((r->rweight >= minweight) & (r->rlvl <= abs(maxdepth)) ? 0 : -1); |
135 |
|
} |
136 |
|
|
137 |
|
|
146 |
|
r->robj = OVOID; |
147 |
|
r->ro = NULL; |
148 |
|
r->rox = NULL; |
149 |
< |
r->rt = r->rot = FHUGE; |
149 |
> |
r->rxt = r->rmt = r->rot = FHUGE; |
150 |
> |
VCOPY(r->rop, r->rorg); |
151 |
> |
r->ron[0] = -r->rdir[0]; r->ron[1] = -r->rdir[1]; r->ron[2] = -r->rdir[2]; |
152 |
> |
r->rod = 1.0; |
153 |
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
154 |
+ |
r->rflips = 0; |
155 |
|
r->uv[0] = r->uv[1] = 0.0; |
156 |
< |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
157 |
< |
setcolor(r->rcol, 0.0, 0.0, 0.0); |
156 |
> |
setscolor(r->pcol, 1.0, 1.0, 1.0); |
157 |
> |
scolorblack(r->mcol); |
158 |
> |
scolorblack(r->rcol); |
159 |
|
} |
160 |
|
|
161 |
|
|
197 |
|
{ |
198 |
|
RAY tr; |
199 |
|
|
200 |
< |
if (rayorigin(&tr, TRANS, r, NULL) == 0) { |
201 |
< |
VCOPY(tr.rdir, r->rdir); |
202 |
< |
rayvalue(&tr); |
203 |
< |
copycolor(r->rcol, tr.rcol); |
204 |
< |
r->rt = r->rot + tr.rt; |
200 |
> |
rayorigin(&tr, TRANS, r, NULL); /* always continue */ |
201 |
> |
VCOPY(tr.rdir, r->rdir); |
202 |
> |
rayvalue(&tr); |
203 |
> |
copyscolor(r->mcol, tr.mcol); |
204 |
> |
copyscolor(r->rcol, tr.rcol); |
205 |
> |
r->rmt = r->rot + tr.rmt; |
206 |
> |
r->rxt = r->rot + tr.rxt; |
207 |
> |
} |
208 |
> |
|
209 |
> |
|
210 |
> |
int |
211 |
> |
raytirrad( /* irradiance hack */ |
212 |
> |
OBJREC *m, |
213 |
> |
RAY *r |
214 |
> |
) |
215 |
> |
{ |
216 |
> |
if (m->otype != MAT_CLIP && ismaterial(m->otype)) { |
217 |
> |
if (istransp(m) || isBSDFproxy(m)) { |
218 |
> |
raytrans(r); |
219 |
> |
return(1); |
220 |
> |
} |
221 |
> |
if (!islight(m->otype)) { |
222 |
> |
setscolor(r->pcol, 1.0, 1.0, 1.0); |
223 |
> |
return((*ofun[Lamb.otype].funp)(&Lamb, r)); |
224 |
> |
} |
225 |
|
} |
226 |
+ |
return(0); /* not a qualifying surface */ |
227 |
|
} |
228 |
|
|
229 |
|
|
233 |
|
int mod |
234 |
|
) |
235 |
|
{ |
236 |
+ |
int tst_irrad = do_irrad && !(r->crtype & ~(PRIMARY|TRANS)); |
237 |
|
OBJREC *m; |
238 |
|
|
239 |
< |
r->rt = r->rot; /* set effective ray length */ |
239 |
> |
r->rxt = r->rot; /* preset effective ray length */ |
240 |
|
for ( ; mod != OVOID; mod = m->omod) { |
241 |
|
m = objptr(mod); |
242 |
|
/****** unnecessary test since modifier() is always called |
246 |
|
} |
247 |
|
******/ |
248 |
|
/* hack for irradiance calculation */ |
249 |
< |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) && |
250 |
< |
m->otype != MAT_CLIP && |
251 |
< |
(ofun[m->otype].flags & (T_M|T_X))) { |
214 |
< |
if (irr_ignore(m->otype)) { |
215 |
< |
raytrans(r); |
216 |
< |
return(1); |
217 |
< |
} |
218 |
< |
if (!islight(m->otype)) |
219 |
< |
m = &Lamb; |
220 |
< |
} |
249 |
> |
if (tst_irrad && raytirrad(m, r)) |
250 |
> |
return(1); |
251 |
> |
|
252 |
|
if ((*ofun[m->otype].funp)(m, r)) |
253 |
|
return(1); /* materials call raytexture() */ |
254 |
|
} |
261 |
|
RAY *r |
262 |
|
) |
263 |
|
{ |
264 |
< |
COLOR ce, ca; |
264 |
> |
SCOLOR ce, ca; |
265 |
|
double re, ge, be; |
266 |
|
|
267 |
|
if (intens(r->cext) <= 1./FHUGE) |
274 |
|
ge *= 1. - colval(r->albedo,GRN); |
275 |
|
be *= 1. - colval(r->albedo,BLU); |
276 |
|
} |
277 |
< |
setcolor(ce, re<=FTINY ? 1. : re>92. ? 0. : exp(-re), |
277 |
> |
setscolor(ce, re<=FTINY ? 1. : re>92. ? 0. : exp(-re), |
278 |
|
ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge), |
279 |
|
be<=FTINY ? 1. : be>92. ? 0. : exp(-be)); |
280 |
< |
multcolor(r->rcol, ce); /* path extinction */ |
280 |
> |
smultscolor(r->rcol, ce); /* path extinction */ |
281 |
|
if (r->crtype & SHADOW || intens(r->albedo) <= FTINY) |
282 |
|
return; /* no scattering */ |
283 |
< |
setcolor(ca, |
284 |
< |
colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)), |
285 |
< |
colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)), |
286 |
< |
colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU))); |
287 |
< |
addcolor(r->rcol, ca); /* ambient in scattering */ |
283 |
> |
|
284 |
> |
/* PMAP: indirect inscattering accounted for by volume photons? */ |
285 |
> |
if (!volumePhotonMapping) { |
286 |
> |
setscolor(ca, |
287 |
> |
colval(r->albedo,RED)*colval(ambval,RED)*(1.-scolval(ce,RED)), |
288 |
> |
colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-scolval(ce,GRN)), |
289 |
> |
colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-scolval(ce,BLU))); |
290 |
> |
saddscolor(r->rcol, ca); /* ambient in scattering */ |
291 |
> |
} |
292 |
> |
|
293 |
|
srcscatter(r); /* source in scattering */ |
294 |
|
} |
295 |
|
|
328 |
|
) |
329 |
|
{ |
330 |
|
RAY fr, br; |
331 |
+ |
double mfore, mback; |
332 |
|
int foremat, backmat; |
333 |
|
int i; |
334 |
|
/* bound coefficient */ |
342 |
|
fr = *r; |
343 |
|
if (coef > FTINY) { |
344 |
|
fr.rweight *= coef; |
345 |
< |
scalecolor(fr.rcoef, coef); |
345 |
> |
scalescolor(fr.rcoef, coef); |
346 |
|
foremat = rayshade(&fr, fore); |
347 |
|
} |
348 |
|
/* background */ |
349 |
|
br = *r; |
350 |
|
if (coef < 1.0-FTINY) { |
351 |
|
br.rweight *= 1.0-coef; |
352 |
< |
scalecolor(br.rcoef, 1.0-coef); |
352 |
> |
scalescolor(br.rcoef, 1.0-coef); |
353 |
|
backmat = rayshade(&br, back); |
354 |
|
} |
355 |
|
/* check for transparency */ |
363 |
|
for (i = 0; i < 3; i++) |
364 |
|
r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; |
365 |
|
/* mix pattern colors */ |
366 |
< |
scalecolor(fr.pcol, coef); |
367 |
< |
scalecolor(br.pcol, 1.0-coef); |
368 |
< |
copycolor(r->pcol, fr.pcol); |
369 |
< |
addcolor(r->pcol, br.pcol); |
366 |
> |
scalescolor(fr.pcol, coef); |
367 |
> |
scalescolor(br.pcol, 1.0-coef); |
368 |
> |
copyscolor(r->pcol, fr.pcol); |
369 |
> |
saddscolor(r->pcol, br.pcol); |
370 |
|
/* return value tells if material */ |
371 |
|
if (!foremat & !backmat) |
372 |
|
return(0); |
373 |
|
/* mix returned ray values */ |
374 |
< |
scalecolor(fr.rcol, coef); |
375 |
< |
scalecolor(br.rcol, 1.0-coef); |
376 |
< |
copycolor(r->rcol, fr.rcol); |
377 |
< |
addcolor(r->rcol, br.rcol); |
378 |
< |
r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt; |
374 |
> |
scalescolor(fr.rcol, coef); |
375 |
> |
scalescolor(br.rcol, 1.0-coef); |
376 |
> |
copyscolor(r->rcol, fr.rcol); |
377 |
> |
saddscolor(r->rcol, br.rcol); |
378 |
> |
scalescolor(fr.mcol, coef); |
379 |
> |
scalescolor(br.mcol, 1.0-coef); |
380 |
> |
copyscolor(r->mcol, fr.mcol); |
381 |
> |
saddscolor(r->mcol, br.mcol); |
382 |
> |
mfore = pbright(fr.mcol); mback = pbright(br.mcol); |
383 |
> |
r->rmt = mfore > mback ? fr.rmt : br.rmt; |
384 |
> |
r->rxt = pbright(fr.rcol)-mfore > pbright(br.rcol)-mback ? |
385 |
> |
fr.rxt : br.rxt; |
386 |
|
return(1); |
387 |
|
} |
388 |
|
|
405 |
|
|
406 |
|
void |
407 |
|
raycontrib( /* compute (cumulative) ray contribution */ |
408 |
< |
RREAL rc[3], |
408 |
> |
SCOLOR rc, |
409 |
|
const RAY *r, |
410 |
|
int flags |
411 |
|
) |
412 |
|
{ |
413 |
< |
double eext[3]; |
414 |
< |
int i; |
413 |
> |
static int warnedPM = 0; |
414 |
> |
double re, ge, be; |
415 |
> |
SCOLOR ce; |
416 |
|
|
417 |
< |
eext[0] = eext[1] = eext[2] = 0.; |
418 |
< |
rc[0] = rc[1] = rc[2] = 1.; |
417 |
> |
setscolor(rc, 1., 1., 1.); |
418 |
> |
re = ge = be = 0.; |
419 |
|
|
420 |
|
while (r != NULL && r->crtype&flags) { |
421 |
< |
for (i = 3; i--; ) { |
422 |
< |
rc[i] *= colval(r->rcoef,i); |
423 |
< |
eext[i] += r->rot * colval(r->cext,i); |
421 |
> |
/* include this ray coefficient */ |
422 |
> |
smultscolor(rc, r->rcoef); |
423 |
> |
/* check participating medium */ |
424 |
> |
if (!warnedPM && bright(r->albedo) > FTINY) { |
425 |
> |
error(WARNING, |
426 |
> |
"ray contribution calculation does not support participating media"); |
427 |
> |
warnedPM++; |
428 |
|
} |
429 |
+ |
/* sum PM extinction */ |
430 |
+ |
re += r->rot*colval(r->cext,RED); |
431 |
+ |
ge += r->rot*colval(r->cext,GRN); |
432 |
+ |
be += r->rot*colval(r->cext,BLU); |
433 |
+ |
/* descend the tree */ |
434 |
|
r = r->parent; |
435 |
|
} |
436 |
< |
for (i = 3; i--; ) |
437 |
< |
rc[i] *= (eext[i] <= FTINY) ? 1. : |
438 |
< |
(eext[i] > 92.) ? 0. : exp(-eext[i]); |
436 |
> |
/* cumulative extinction */ |
437 |
> |
setscolor(ce, re<=FTINY ? 1. : re>92. ? 0. : exp(-re), |
438 |
> |
ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge), |
439 |
> |
be<=FTINY ? 1. : be>92. ? 0. : exp(-be)); |
440 |
> |
smultscolor(rc, ce); |
441 |
|
} |
442 |
|
|
443 |
|
|
468 |
|
return(r->rod); |
469 |
|
} |
470 |
|
newdot = -DOT(norm, r->rdir); |
471 |
< |
if ((newdot > 0.0) != (r->rod > 0.0)) { /* fix orientation */ |
471 |
> |
if ((newdot > 0.0) ^ (r->rod > 0.0)) { /* fix orientation */ |
472 |
|
for (i = 0; i < 3; i++) |
473 |
|
norm[i] += 2.0*newdot*r->rdir[i]; |
474 |
|
newdot = -newdot; |
527 |
|
r->pert[0] = -r->pert[0]; |
528 |
|
r->pert[1] = -r->pert[1]; |
529 |
|
r->pert[2] = -r->pert[2]; |
530 |
+ |
r->rflips++; |
531 |
|
} |
532 |
|
|
533 |
+ |
|
534 |
+ |
int |
535 |
+ |
rayreject( /* check if candidate hit is worse than current */ |
536 |
+ |
OBJREC *o, |
537 |
+ |
RAY *r, |
538 |
+ |
double t, |
539 |
+ |
double rod |
540 |
+ |
) |
541 |
+ |
{ |
542 |
+ |
OBJREC *mnew, *mray; |
543 |
+ |
|
544 |
+ |
if ((t <= FTINY) | (t > r->rot + FTINY)) |
545 |
+ |
return(1); |
546 |
+ |
if (t < r->rot - FTINY) /* is new hit significantly closer? */ |
547 |
+ |
return(0); |
548 |
+ |
/* coincident point, so decide... */ |
549 |
+ |
if (o == r->ro) |
550 |
+ |
return(1); /* shouldn't happen */ |
551 |
+ |
if (r->ro == NULL) |
552 |
+ |
return(0); /* ditto */ |
553 |
+ |
mnew = findmaterial(o); |
554 |
+ |
mray = findmaterial(r->ro); /* check material transparencies */ |
555 |
+ |
if (mnew == NULL) { |
556 |
+ |
if (mray != NULL) |
557 |
+ |
return(1); /* old has material, new does not */ |
558 |
+ |
} else if (mray == NULL) { |
559 |
+ |
return(0); /* new has material, old does not */ |
560 |
+ |
} else if (istransp(mnew)) { |
561 |
+ |
if (!istransp(mray)) |
562 |
+ |
return(1); /* new is transparent, old is not */ |
563 |
+ |
} else if (istransp(mray)) { |
564 |
+ |
return(0); /* old is transparent, new is not */ |
565 |
+ |
} |
566 |
+ |
if (rod <= 0) { /* check which side we hit */ |
567 |
+ |
if (r->rod > 0) |
568 |
+ |
return(1); /* old hit front, new did not */ |
569 |
+ |
} else if (r->rod <= 0) { |
570 |
+ |
return(0); /* new hit front, old did not */ |
571 |
+ |
} |
572 |
+ |
/* earlier modifier definition wins tie */ |
573 |
+ |
return (r->ro->omod >= o->omod); |
574 |
+ |
} |
575 |
|
|
576 |
|
void |
577 |
|
rayhit( /* standard ray hit test */ |