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 |
|
|
20 |
< |
unsigned long raynum = 0; /* next unique ray number */ |
21 |
< |
unsigned long nrays = 0; /* number of calls to localhit */ |
20 |
> |
RNUMBER raynum = 0; /* next unique ray number */ |
21 |
> |
RNUMBER nrays = 0; /* number of calls to localhit */ |
22 |
|
|
23 |
|
static RREAL Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
24 |
|
OBJREC Lamb = { |
25 |
|
OVOID, MAT_PLASTIC, "Lambertian", |
26 |
< |
{0, 5, NULL, Lambfa}, NULL, |
26 |
> |
{NULL, Lambfa, 0, 5}, NULL |
27 |
|
}; /* a Lambertian surface */ |
28 |
|
|
29 |
|
OBJREC Aftplane; /* aft clipping plane object */ |
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 |
> |
} |
103 |
|
} |
104 |
|
rayclear(r); |
105 |
< |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
105 |
> |
if (r->rweight <= 0.0) /* check for expiration */ |
106 |
> |
return(-1); |
107 |
> |
if (r->crtype & SHADOW) /* shadow commitment */ |
108 |
> |
return(0); |
109 |
> |
if (maxdepth <= 0 && rc != NULL) { /* Russian roulette */ |
110 |
> |
if (minweight <= 0.0) |
111 |
> |
error(USER, "zero ray weight in Russian roulette"); |
112 |
> |
if (maxdepth < 0 && r->rlvl > -maxdepth) |
113 |
> |
return(-1); /* upper reflection limit */ |
114 |
> |
if (r->rweight >= minweight) |
115 |
> |
return(0); |
116 |
> |
if (frandom() > r->rweight/minweight) |
117 |
> |
return(-1); |
118 |
> |
rw = minweight/r->rweight; /* promote survivor */ |
119 |
> |
scalecolor(r->rcoef, rw); |
120 |
> |
r->rweight = minweight; |
121 |
> |
return(0); |
122 |
> |
} |
123 |
> |
return(r->rlvl <= abs(maxdepth) && r->rweight >= minweight ? 0 : -1); |
124 |
|
} |
125 |
|
|
126 |
|
|
144 |
|
|
145 |
|
|
146 |
|
extern void |
147 |
< |
rayvalue( /* trace a ray and compute its value */ |
147 |
> |
raytrace( /* trace a ray and compute its value */ |
148 |
|
RAY *r |
149 |
|
) |
150 |
|
{ |
156 |
|
} else if (sourcehit(r)) |
157 |
|
rayshade(r, r->ro->omod); /* distant source */ |
158 |
|
|
123 |
– |
rayparticipate(r); /* for participating medium */ |
124 |
– |
|
159 |
|
if (trace != NULL) |
160 |
|
(*trace)(r); /* trace execution */ |
161 |
+ |
|
162 |
+ |
rayparticipate(r); /* for participating medium */ |
163 |
|
} |
164 |
|
|
165 |
|
|
181 |
|
{ |
182 |
|
RAY tr; |
183 |
|
|
184 |
< |
if (rayorigin(&tr, r, TRANS, 1.0) == 0) { |
184 |
> |
if (rayorigin(&tr, TRANS, r, NULL) == 0) { |
185 |
|
VCOPY(tr.rdir, r->rdir); |
186 |
|
rayvalue(&tr); |
187 |
|
copycolor(r->rcol, tr.rcol); |
243 |
|
ge *= 1. - colval(r->albedo,GRN); |
244 |
|
be *= 1. - colval(r->albedo,BLU); |
245 |
|
} |
246 |
< |
setcolor(ce, re<=0. ? 1. : re>92. ? 0. : exp(-re), |
247 |
< |
ge<=0. ? 1. : ge>92. ? 0. : exp(-ge), |
248 |
< |
be<=0. ? 1. : be>92. ? 0. : exp(-be)); |
249 |
< |
multcolor(r->rcol, ce); /* path absorption */ |
246 |
> |
setcolor(ce, re<=FTINY ? 1. : re>92. ? 0. : exp(-re), |
247 |
> |
ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge), |
248 |
> |
be<=FTINY ? 1. : be>92. ? 0. : exp(-be)); |
249 |
> |
multcolor(r->rcol, ce); /* path extinction */ |
250 |
|
if (r->crtype & SHADOW || intens(r->albedo) <= FTINY) |
251 |
|
return; /* no scattering */ |
252 |
|
setcolor(ca, |
303 |
|
foremat = backmat = 0; |
304 |
|
/* foreground */ |
305 |
|
fr = *r; |
306 |
< |
if (coef > FTINY) |
306 |
> |
if (coef > FTINY) { |
307 |
> |
fr.rweight *= coef; |
308 |
> |
scalecolor(fr.rcoef, coef); |
309 |
|
foremat = rayshade(&fr, fore); |
310 |
+ |
} |
311 |
|
/* background */ |
312 |
|
br = *r; |
313 |
< |
if (coef < 1.0-FTINY) |
313 |
> |
if (coef < 1.0-FTINY) { |
314 |
> |
br.rweight *= 1.0-coef; |
315 |
> |
scalecolor(br.rcoef, 1.0-coef); |
316 |
|
backmat = rayshade(&br, back); |
317 |
+ |
} |
318 |
|
/* check for transparency */ |
319 |
|
if (backmat ^ foremat) { |
320 |
|
if (backmat && coef > FTINY) |
345 |
|
|
346 |
|
extern double |
347 |
|
raydist( /* compute (cumulative) ray distance */ |
348 |
< |
register RAY *r, |
348 |
> |
register const RAY *r, |
349 |
|
register int flags |
350 |
|
) |
351 |
|
{ |
359 |
|
} |
360 |
|
|
361 |
|
|
362 |
+ |
extern void |
363 |
+ |
raycontrib( /* compute (cumulative) ray contribution */ |
364 |
+ |
double rc[3], |
365 |
+ |
const RAY *r, |
366 |
+ |
int flags |
367 |
+ |
) |
368 |
+ |
{ |
369 |
+ |
double eext[3]; |
370 |
+ |
int i; |
371 |
+ |
|
372 |
+ |
eext[0] = eext[1] = eext[2] = 0.; |
373 |
+ |
rc[0] = rc[1] = rc[2] = 1.; |
374 |
+ |
|
375 |
+ |
while (r != NULL && r->crtype&flags) { |
376 |
+ |
for (i = 3; i--; ) { |
377 |
+ |
rc[i] *= colval(r->rcoef,i); |
378 |
+ |
eext[i] += r->rot * colval(r->cext,i); |
379 |
+ |
} |
380 |
+ |
r = r->parent; |
381 |
+ |
} |
382 |
+ |
for (i = 3; i--; ) |
383 |
+ |
rc[i] *= (eext[i] <= FTINY) ? 1. : |
384 |
+ |
(eext[i] > 92.) ? 0. : exp(-eext[i]); |
385 |
+ |
} |
386 |
+ |
|
387 |
+ |
|
388 |
|
extern double |
389 |
|
raynormal( /* compute perturbed normal for ray */ |
390 |
|
FVECT norm, |
431 |
|
FULLXF xf; |
432 |
|
} xfseed = { &xfseed }, *xflast = &xfseed; |
433 |
|
register struct xfn *xp; |
434 |
< |
register RAY *rp; |
434 |
> |
register const RAY *rp; |
435 |
|
|
436 |
|
/* |
437 |
|
* Search for transform in circular list that |
512 |
|
else if (r->rdir[i] < -1e-7) |
513 |
|
sflags |= 0x10 << i; |
514 |
|
} |
515 |
< |
if (sflags == 0) |
516 |
< |
error(CONSISTENCY, "zero ray direction in localhit"); |
515 |
> |
if (!sflags) { |
516 |
> |
error(WARNING, "zero ray direction in localhit"); |
517 |
> |
return(0); |
518 |
> |
} |
519 |
|
/* start off assuming nothing hit */ |
520 |
|
if (r->rmax > FTINY) { /* except aft plane if one */ |
521 |
|
r->ro = &Aftplane; |