13 |
|
#include "source.h" |
14 |
|
#include "otypes.h" |
15 |
|
#include "otspecial.h" |
16 |
+ |
#include "random.h" |
17 |
|
|
18 |
|
#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ |
19 |
|
|
37 |
|
|
38 |
|
extern int |
39 |
|
rayorigin( /* start new ray from old one */ |
40 |
< |
register RAY *r, |
40 |
< |
register RAY *ro, |
40 |
> |
RAY *r, |
41 |
|
int rt, |
42 |
< |
double rw |
42 |
> |
const RAY *ro, |
43 |
> |
const COLOR rc |
44 |
|
) |
45 |
|
{ |
46 |
< |
double re; |
47 |
< |
|
46 |
> |
double rw, re; |
47 |
> |
/* assign coefficient/weight */ |
48 |
> |
if (rc == NULL) { |
49 |
> |
rw = 1.0; |
50 |
> |
setcolor(r->rcoef, 1., 1., 1.); |
51 |
> |
} else { |
52 |
> |
rw = intens(rc); |
53 |
> |
if (rc != r->rcoef) |
54 |
> |
copycolor(r->rcoef, rc); |
55 |
> |
} |
56 |
|
if ((r->parent = ro) == NULL) { /* primary ray */ |
57 |
|
r->rlvl = 0; |
58 |
|
r->rweight = rw; |
59 |
|
r->crtype = r->rtype = rt; |
60 |
|
r->rsrc = -1; |
61 |
|
r->clipset = NULL; |
62 |
+ |
r->revf = raytrace; |
63 |
|
copycolor(r->cext, cextinction); |
64 |
|
copycolor(r->albedo, salbedo); |
65 |
|
r->gecc = seccg; |
66 |
|
r->slights = NULL; |
57 |
– |
} else if (ro->rot >= FHUGE) { /* illegal continuation */ |
58 |
– |
memset(r, 0, sizeof(RAY)); |
59 |
– |
return(-1); |
67 |
|
} else { /* spawned ray */ |
68 |
+ |
if (ro->rot >= FHUGE) { |
69 |
+ |
memset(r, 0, sizeof(RAY)); |
70 |
+ |
return(-1); /* illegal continuation */ |
71 |
+ |
} |
72 |
|
r->rlvl = ro->rlvl; |
73 |
|
if (rt & RAYREFL) { |
74 |
|
r->rlvl++; |
80 |
|
r->clipset = ro->newcset; |
81 |
|
r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot; |
82 |
|
} |
83 |
+ |
r->revf = ro->revf; |
84 |
|
copycolor(r->cext, ro->cext); |
85 |
|
copycolor(r->albedo, ro->albedo); |
86 |
|
r->gecc = ro->gecc; |
88 |
|
r->crtype = ro->crtype | (r->rtype = rt); |
89 |
|
VCOPY(r->rorg, ro->rop); |
90 |
|
r->rweight = ro->rweight * rw; |
91 |
< |
/* estimate absorption */ |
91 |
> |
/* estimate extinction */ |
92 |
|
re = colval(ro->cext,RED) < colval(ro->cext,GRN) ? |
93 |
|
colval(ro->cext,RED) : colval(ro->cext,GRN); |
94 |
|
if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU); |
95 |
< |
if (re > 0.) |
96 |
< |
r->rweight *= exp(-re*ro->rot); |
95 |
> |
re *= ro->rot; |
96 |
> |
if (re > 0.1) |
97 |
> |
if (re > 92.) |
98 |
> |
r->rweight = 0.0; |
99 |
> |
else |
100 |
> |
r->rweight *= exp(-re); |
101 |
|
} |
102 |
|
rayclear(r); |
103 |
< |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
103 |
> |
if (r->rweight <= 0.0) /* check for expiration */ |
104 |
> |
return(-1); |
105 |
> |
if (r->crtype & SHADOW) /* shadow commitment */ |
106 |
> |
return(0); |
107 |
> |
if (maxdepth <= 0 && rc != NULL) { /* Russian roulette */ |
108 |
> |
if (minweight <= 0.0) |
109 |
> |
error(USER, "zero ray weight in Russian roulette"); |
110 |
> |
if (maxdepth < 0 && r->rlvl > -maxdepth) |
111 |
> |
return(-1); /* upper reflection limit */ |
112 |
> |
if (r->rweight >= minweight) |
113 |
> |
return(0); |
114 |
> |
if (frandom() > r->rweight/minweight) |
115 |
> |
return(-1); |
116 |
> |
rw = minweight/r->rweight; /* promote survivor */ |
117 |
> |
scalecolor(r->rcoef, rw); |
118 |
> |
r->rweight = minweight; |
119 |
> |
return(0); |
120 |
> |
} |
121 |
> |
return(r->rlvl <= abs(maxdepth) && r->rweight >= minweight ? 0 : -1); |
122 |
|
} |
123 |
|
|
124 |
|
|
142 |
|
|
143 |
|
|
144 |
|
extern void |
145 |
< |
rayvalue( /* trace a ray and compute its value */ |
145 |
> |
raytrace( /* trace a ray and compute its value */ |
146 |
|
RAY *r |
147 |
|
) |
148 |
|
{ |
154 |
|
} else if (sourcehit(r)) |
155 |
|
rayshade(r, r->ro->omod); /* distant source */ |
156 |
|
|
123 |
– |
rayparticipate(r); /* for participating medium */ |
124 |
– |
|
157 |
|
if (trace != NULL) |
158 |
|
(*trace)(r); /* trace execution */ |
159 |
+ |
|
160 |
+ |
rayparticipate(r); /* for participating medium */ |
161 |
|
} |
162 |
|
|
163 |
|
|
179 |
|
{ |
180 |
|
RAY tr; |
181 |
|
|
182 |
< |
if (rayorigin(&tr, r, TRANS, 1.0) == 0) { |
182 |
> |
if (rayorigin(&tr, TRANS, r, NULL) == 0) { |
183 |
|
VCOPY(tr.rdir, r->rdir); |
184 |
|
rayvalue(&tr); |
185 |
|
copycolor(r->rcol, tr.rcol); |
241 |
|
ge *= 1. - colval(r->albedo,GRN); |
242 |
|
be *= 1. - colval(r->albedo,BLU); |
243 |
|
} |
244 |
< |
setcolor(ce, re<=0. ? 1. : re>92. ? 0. : exp(-re), |
245 |
< |
ge<=0. ? 1. : ge>92. ? 0. : exp(-ge), |
246 |
< |
be<=0. ? 1. : be>92. ? 0. : exp(-be)); |
247 |
< |
multcolor(r->rcol, ce); /* path absorption */ |
244 |
> |
setcolor(ce, re<=FTINY ? 1. : re>92. ? 0. : exp(-re), |
245 |
> |
ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge), |
246 |
> |
be<=FTINY ? 1. : be>92. ? 0. : exp(-be)); |
247 |
> |
multcolor(r->rcol, ce); /* path extinction */ |
248 |
|
if (r->crtype & SHADOW || intens(r->albedo) <= FTINY) |
249 |
|
return; /* no scattering */ |
250 |
|
setcolor(ca, |
301 |
|
foremat = backmat = 0; |
302 |
|
/* foreground */ |
303 |
|
fr = *r; |
304 |
< |
if (coef > FTINY) |
304 |
> |
if (coef > FTINY) { |
305 |
> |
scalecolor(fr.rcoef, coef); |
306 |
|
foremat = rayshade(&fr, fore); |
307 |
+ |
} |
308 |
|
/* background */ |
309 |
|
br = *r; |
310 |
< |
if (coef < 1.0-FTINY) |
310 |
> |
if (coef < 1.0-FTINY) { |
311 |
> |
scalecolor(br.rcoef, 1.0-coef); |
312 |
|
backmat = rayshade(&br, back); |
313 |
+ |
} |
314 |
|
/* check for transparency */ |
315 |
|
if (backmat ^ foremat) { |
316 |
|
if (backmat && coef > FTINY) |
341 |
|
|
342 |
|
extern double |
343 |
|
raydist( /* compute (cumulative) ray distance */ |
344 |
< |
register RAY *r, |
344 |
> |
register const RAY *r, |
345 |
|
register int flags |
346 |
|
) |
347 |
|
{ |
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
+ |
extern void |
359 |
+ |
raycontrib( /* compute (cumulative) ray contribution */ |
360 |
+ |
double rc[3], |
361 |
+ |
const RAY *r, |
362 |
+ |
int flags |
363 |
+ |
) |
364 |
+ |
{ |
365 |
+ |
double eext[3]; |
366 |
+ |
int i; |
367 |
+ |
|
368 |
+ |
eext[0] = eext[1] = eext[2] = 0.; |
369 |
+ |
rc[0] = rc[1] = rc[2] = 1.; |
370 |
+ |
|
371 |
+ |
while (r != NULL && r->crtype&flags) { |
372 |
+ |
for (i = 3; i--; ) { |
373 |
+ |
rc[i] *= colval(r->rcoef,i); |
374 |
+ |
eext[i] += r->rot * colval(r->cext,i); |
375 |
+ |
} |
376 |
+ |
r = r->parent; |
377 |
+ |
} |
378 |
+ |
for (i = 3; i--; ) |
379 |
+ |
rc[i] *= (eext[i] <= FTINY) ? 1. : |
380 |
+ |
(eext[i] > 92.) ? 0. : exp(-eext[i]); |
381 |
+ |
} |
382 |
+ |
|
383 |
+ |
|
384 |
|
extern double |
385 |
|
raynormal( /* compute perturbed normal for ray */ |
386 |
|
FVECT norm, |
427 |
|
FULLXF xf; |
428 |
|
} xfseed = { &xfseed }, *xflast = &xfseed; |
429 |
|
register struct xfn *xp; |
430 |
< |
register RAY *rp; |
430 |
> |
register const RAY *rp; |
431 |
|
|
432 |
|
/* |
433 |
|
* Search for transform in circular list that |