10 |
|
#include "copyright.h" |
11 |
|
|
12 |
|
#include "ray.h" |
13 |
< |
|
13 |
> |
#include "source.h" |
14 |
|
#include "otypes.h" |
15 |
– |
|
15 |
|
#include "otspecial.h" |
16 |
+ |
#include "random.h" |
17 |
|
|
18 |
|
#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ |
19 |
|
|
28 |
|
|
29 |
|
OBJREC Aftplane; /* aft clipping plane object */ |
30 |
|
|
31 |
– |
static int raymove(), checkhit(); |
32 |
– |
static void checkset(); |
33 |
– |
|
31 |
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
32 |
|
|
33 |
+ |
static int raymove(FVECT pos, OBJECT *cxs, int dirf, RAY *r, CUBE *cu); |
34 |
+ |
static int checkhit(RAY *r, CUBE *cu, OBJECT *cxs); |
35 |
+ |
static void checkset(OBJECT *os, OBJECT *cs); |
36 |
|
|
37 |
– |
int |
38 |
– |
rayorigin(r, ro, rt, rw) /* start new ray from old one */ |
39 |
– |
register RAY *r, *ro; |
40 |
– |
int rt; |
41 |
– |
double rw; |
42 |
– |
{ |
43 |
– |
double re; |
37 |
|
|
38 |
+ |
extern int |
39 |
+ |
rayorigin( /* start new ray from old one */ |
40 |
+ |
RAY *r, |
41 |
+ |
int rt, |
42 |
+ |
const RAY *ro, |
43 |
+ |
const COLOR rc |
44 |
+ |
) |
45 |
+ |
{ |
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; |
65 |
|
r->gecc = seccg; |
66 |
|
r->slights = NULL; |
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++; |
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 |
|
|
125 |
< |
void |
126 |
< |
rayclear(r) /* clear a ray for (re)evaluation */ |
127 |
< |
register RAY *r; |
125 |
> |
extern void |
126 |
> |
rayclear( /* clear a ray for (re)evaluation */ |
127 |
> |
register RAY *r |
128 |
> |
) |
129 |
|
{ |
130 |
|
r->rno = raynum++; |
131 |
|
r->newcset = r->clipset; |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
< |
void |
145 |
< |
raytrace(r) /* trace a ray and compute its value */ |
146 |
< |
RAY *r; |
144 |
> |
extern void |
145 |
> |
raytrace( /* trace a ray and compute its value */ |
146 |
> |
RAY *r |
147 |
> |
) |
148 |
|
{ |
149 |
|
if (localhit(r, &thescene)) |
150 |
|
raycont(r); /* hit local surface, evaluate */ |
154 |
|
} else if (sourcehit(r)) |
155 |
|
rayshade(r, r->ro->omod); /* distant source */ |
156 |
|
|
118 |
– |
rayparticipate(r); /* for participating medium */ |
119 |
– |
|
157 |
|
if (trace != NULL) |
158 |
|
(*trace)(r); /* trace execution */ |
159 |
+ |
|
160 |
+ |
rayparticipate(r); /* for participating medium */ |
161 |
|
} |
162 |
|
|
163 |
|
|
164 |
< |
void |
165 |
< |
raycont(r) /* check for clipped object and continue */ |
166 |
< |
register RAY *r; |
164 |
> |
extern void |
165 |
> |
raycont( /* check for clipped object and continue */ |
166 |
> |
register RAY *r |
167 |
> |
) |
168 |
|
{ |
169 |
|
if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || |
170 |
|
!rayshade(r, r->ro->omod)) |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
< |
void |
176 |
< |
raytrans(r) /* transmit ray as is */ |
177 |
< |
register RAY *r; |
175 |
> |
extern void |
176 |
> |
raytrans( /* transmit ray as is */ |
177 |
> |
register RAY *r |
178 |
> |
) |
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); |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
< |
int |
192 |
< |
rayshade(r, mod) /* shade ray r with material mod */ |
193 |
< |
register RAY *r; |
194 |
< |
int mod; |
191 |
> |
extern int |
192 |
> |
rayshade( /* shade ray r with material mod */ |
193 |
> |
register RAY *r, |
194 |
> |
int mod |
195 |
> |
) |
196 |
|
{ |
155 |
– |
int gotmat; |
197 |
|
register OBJREC *m; |
198 |
+ |
|
199 |
|
r->rt = r->rot; /* set effective ray length */ |
200 |
< |
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { |
200 |
> |
for ( ; mod != OVOID; mod = m->omod) { |
201 |
|
m = objptr(mod); |
202 |
|
/****** unnecessary test since modifier() is always called |
203 |
|
if (!ismodifier(m->otype)) { |
216 |
|
if (!islight(m->otype)) |
217 |
|
m = &Lamb; |
218 |
|
} |
219 |
< |
/* materials call raytexture */ |
220 |
< |
gotmat = (*ofun[m->otype].funp)(m, r); |
219 |
> |
if ((*ofun[m->otype].funp)(m, r)) |
220 |
> |
return(1); /* materials call raytexture() */ |
221 |
|
} |
222 |
< |
return(gotmat); |
222 |
> |
return(0); /* no material! */ |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
< |
void |
227 |
< |
rayparticipate(r) /* compute ray medium participation */ |
228 |
< |
register RAY *r; |
226 |
> |
extern void |
227 |
> |
rayparticipate( /* compute ray medium participation */ |
228 |
> |
register RAY *r |
229 |
> |
) |
230 |
|
{ |
231 |
|
COLOR ce, ca; |
232 |
|
double re, ge, be; |
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, |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
< |
void |
260 |
< |
raytexture(r, mod) /* get material modifiers */ |
261 |
< |
RAY *r; |
262 |
< |
OBJECT mod; |
259 |
> |
extern void |
260 |
> |
raytexture( /* get material modifiers */ |
261 |
> |
RAY *r, |
262 |
> |
OBJECT mod |
263 |
> |
) |
264 |
|
{ |
265 |
|
register OBJREC *m; |
266 |
|
/* execute textures and patterns */ |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
< |
int |
285 |
< |
raymixture(r, fore, back, coef) /* mix modifiers */ |
286 |
< |
register RAY *r; |
287 |
< |
OBJECT fore, back; |
288 |
< |
double coef; |
284 |
> |
extern int |
285 |
> |
raymixture( /* mix modifiers */ |
286 |
> |
register RAY *r, |
287 |
> |
OBJECT fore, |
288 |
> |
OBJECT back, |
289 |
> |
double coef |
290 |
> |
) |
291 |
|
{ |
292 |
|
RAY fr, br; |
293 |
|
int foremat, backmat; |
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) |
339 |
|
} |
340 |
|
|
341 |
|
|
342 |
< |
double |
343 |
< |
raydist(r, flags) /* compute (cumulative) ray distance */ |
344 |
< |
register RAY *r; |
345 |
< |
register int flags; |
342 |
> |
extern double |
343 |
> |
raydist( /* compute (cumulative) ray distance */ |
344 |
> |
register const RAY *r, |
345 |
> |
register int flags |
346 |
> |
) |
347 |
|
{ |
348 |
|
double sum = 0.0; |
349 |
|
|
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
< |
double |
359 |
< |
raynormal(norm, r) /* compute perturbed normal for ray */ |
360 |
< |
FVECT norm; |
361 |
< |
register RAY *r; |
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, |
387 |
+ |
register RAY *r |
388 |
+ |
) |
389 |
+ |
{ |
390 |
|
double newdot; |
391 |
|
register int i; |
392 |
|
|
417 |
|
} |
418 |
|
|
419 |
|
|
420 |
< |
void |
421 |
< |
newrayxf(r) /* get new tranformation matrix for ray */ |
422 |
< |
RAY *r; |
420 |
> |
extern void |
421 |
> |
newrayxf( /* get new tranformation matrix for ray */ |
422 |
> |
RAY *r |
423 |
> |
) |
424 |
|
{ |
425 |
|
static struct xfn { |
426 |
|
struct xfn *next; |
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 |
455 |
|
} |
456 |
|
|
457 |
|
|
458 |
< |
void |
459 |
< |
flipsurface(r) /* reverse surface orientation */ |
460 |
< |
register RAY *r; |
458 |
> |
extern void |
459 |
> |
flipsurface( /* reverse surface orientation */ |
460 |
> |
register RAY *r |
461 |
> |
) |
462 |
|
{ |
463 |
|
r->rod = -r->rod; |
464 |
|
r->ron[0] = -r->ron[0]; |
470 |
|
} |
471 |
|
|
472 |
|
|
473 |
< |
void |
474 |
< |
rayhit(oset, r) /* standard ray hit test */ |
475 |
< |
OBJECT *oset; |
476 |
< |
RAY *r; |
473 |
> |
extern void |
474 |
> |
rayhit( /* standard ray hit test */ |
475 |
> |
OBJECT *oset, |
476 |
> |
RAY *r |
477 |
> |
) |
478 |
|
{ |
479 |
|
OBJREC *o; |
480 |
|
int i; |
487 |
|
} |
488 |
|
|
489 |
|
|
490 |
< |
int |
491 |
< |
localhit(r, scene) /* check for hit in the octree */ |
492 |
< |
register RAY *r; |
493 |
< |
register CUBE *scene; |
490 |
> |
extern int |
491 |
> |
localhit( /* check for hit in the octree */ |
492 |
> |
register RAY *r, |
493 |
> |
register CUBE *scene |
494 |
> |
) |
495 |
|
{ |
496 |
|
OBJECT cxset[MAXCSET+1]; /* set of checked objects */ |
497 |
|
FVECT curpos; /* current cube position */ |
551 |
|
|
552 |
|
|
553 |
|
static int |
554 |
< |
raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */ |
555 |
< |
FVECT pos; /* current position, modified herein */ |
556 |
< |
OBJECT *cxs; /* checked objects, modified by checkhit */ |
557 |
< |
int dirf; /* direction indicators to speed tests */ |
558 |
< |
register RAY *r; |
559 |
< |
register CUBE *cu; |
554 |
> |
raymove( /* check for hit as we move */ |
555 |
> |
FVECT pos, /* current position, modified herein */ |
556 |
> |
OBJECT *cxs, /* checked objects, modified by checkhit */ |
557 |
> |
int dirf, /* direction indicators to speed tests */ |
558 |
> |
register RAY *r, |
559 |
> |
register CUBE *cu |
560 |
> |
) |
561 |
|
{ |
562 |
|
int ax; |
563 |
|
double dt, t; |
638 |
|
|
639 |
|
|
640 |
|
static int |
641 |
< |
checkhit(r, cu, cxs) /* check for hit in full cube */ |
642 |
< |
register RAY *r; |
643 |
< |
CUBE *cu; |
644 |
< |
OBJECT *cxs; |
641 |
> |
checkhit( /* check for hit in full cube */ |
642 |
> |
register RAY *r, |
643 |
> |
CUBE *cu, |
644 |
> |
OBJECT *cxs |
645 |
> |
) |
646 |
|
{ |
647 |
|
OBJECT oset[MAXSET+1]; |
648 |
|
|
659 |
|
|
660 |
|
|
661 |
|
static void |
662 |
< |
checkset(os, cs) /* modify checked set and set to check */ |
663 |
< |
register OBJECT *os; /* os' = os - cs */ |
664 |
< |
register OBJECT *cs; /* cs' = cs + os */ |
662 |
> |
checkset( /* modify checked set and set to check */ |
663 |
> |
register OBJECT *os, /* os' = os - cs */ |
664 |
> |
register OBJECT *cs /* cs' = cs + os */ |
665 |
> |
) |
666 |
|
{ |
667 |
|
OBJECT cset[MAXCSET+MAXSET+1]; |
668 |
|
register int i, j; |