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 |
– |
|
34 |
– |
#ifndef MAXLOOP |
35 |
– |
#define MAXLOOP 0 /* modifier loop detection */ |
36 |
– |
#endif |
37 |
– |
|
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 |
|
|
41 |
– |
int |
42 |
– |
rayorigin(r, ro, rt, rw) /* start new ray from old one */ |
43 |
– |
register RAY *r, *ro; |
44 |
– |
int rt; |
45 |
– |
double rw; |
46 |
– |
{ |
47 |
– |
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 > .1) |
97 |
> |
r->rweight *= exp(-re); |
98 |
|
} |
99 |
|
rayclear(r); |
100 |
+ |
if (r->crtype & SHADOW) /* shadow commitment */ |
101 |
+ |
return(0); |
102 |
+ |
if (maxdepth <= 0 && rc != NULL) { /* Russian roulette */ |
103 |
+ |
if (minweight <= 0.0) |
104 |
+ |
error(USER, "zero ray weight in Russian roulette"); |
105 |
+ |
if (maxdepth < 0 && r->rlvl > -maxdepth) |
106 |
+ |
return(-1); /* upper reflection limit */ |
107 |
+ |
if (r->rweight >= minweight) |
108 |
+ |
return(0); |
109 |
+ |
if (frandom() < r->rweight/minweight) |
110 |
+ |
return(-1); |
111 |
+ |
rw = minweight/r->rweight; /* promote survivor */ |
112 |
+ |
scalecolor(r->rcoef, rw); |
113 |
+ |
r->rweight = minweight; |
114 |
+ |
return(0); |
115 |
+ |
} |
116 |
|
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
< |
void |
121 |
< |
rayclear(r) /* clear a ray for (re)evaluation */ |
122 |
< |
register RAY *r; |
120 |
> |
extern void |
121 |
> |
rayclear( /* clear a ray for (re)evaluation */ |
122 |
> |
register RAY *r |
123 |
> |
) |
124 |
|
{ |
125 |
|
r->rno = raynum++; |
126 |
|
r->newcset = r->clipset; |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
< |
void |
140 |
< |
raytrace(r) /* trace a ray and compute its value */ |
141 |
< |
RAY *r; |
139 |
> |
extern void |
140 |
> |
raytrace( /* trace a ray and compute its value */ |
141 |
> |
RAY *r |
142 |
> |
) |
143 |
|
{ |
144 |
|
if (localhit(r, &thescene)) |
145 |
|
raycont(r); /* hit local surface, evaluate */ |
149 |
|
} else if (sourcehit(r)) |
150 |
|
rayshade(r, r->ro->omod); /* distant source */ |
151 |
|
|
122 |
– |
rayparticipate(r); /* for participating medium */ |
123 |
– |
|
152 |
|
if (trace != NULL) |
153 |
|
(*trace)(r); /* trace execution */ |
154 |
+ |
|
155 |
+ |
rayparticipate(r); /* for participating medium */ |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
< |
void |
160 |
< |
raycont(r) /* check for clipped object and continue */ |
161 |
< |
register RAY *r; |
159 |
> |
extern void |
160 |
> |
raycont( /* check for clipped object and continue */ |
161 |
> |
register RAY *r |
162 |
> |
) |
163 |
|
{ |
164 |
|
if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || |
165 |
|
!rayshade(r, r->ro->omod)) |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
< |
void |
171 |
< |
raytrans(r) /* transmit ray as is */ |
172 |
< |
register RAY *r; |
170 |
> |
extern void |
171 |
> |
raytrans( /* transmit ray as is */ |
172 |
> |
register RAY *r |
173 |
> |
) |
174 |
|
{ |
175 |
|
RAY tr; |
176 |
|
|
177 |
< |
if (rayorigin(&tr, r, TRANS, 1.0) == 0) { |
177 |
> |
if (rayorigin(&tr, TRANS, r, NULL) == 0) { |
178 |
|
VCOPY(tr.rdir, r->rdir); |
179 |
|
rayvalue(&tr); |
180 |
|
copycolor(r->rcol, tr.rcol); |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
< |
int |
187 |
< |
rayshade(r, mod) /* shade ray r with material mod */ |
188 |
< |
register RAY *r; |
189 |
< |
int mod; |
186 |
> |
extern int |
187 |
> |
rayshade( /* shade ray r with material mod */ |
188 |
> |
register RAY *r, |
189 |
> |
int mod |
190 |
> |
) |
191 |
|
{ |
159 |
– |
int gotmat; |
192 |
|
register OBJREC *m; |
193 |
< |
#if MAXLOOP |
162 |
< |
static int depth = 0; |
163 |
< |
/* check for infinite loop */ |
164 |
< |
if (depth++ >= MAXLOOP) |
165 |
< |
objerror(r->ro, USER, "possible modifier loop"); |
166 |
< |
#endif |
193 |
> |
|
194 |
|
r->rt = r->rot; /* set effective ray length */ |
195 |
< |
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { |
195 |
> |
for ( ; mod != OVOID; mod = m->omod) { |
196 |
|
m = objptr(mod); |
197 |
|
/****** unnecessary test since modifier() is always called |
198 |
|
if (!ismodifier(m->otype)) { |
202 |
|
******/ |
203 |
|
/* hack for irradiance calculation */ |
204 |
|
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) && |
205 |
+ |
m->otype != MAT_CLIP && |
206 |
|
(ofun[m->otype].flags & (T_M|T_X))) { |
207 |
|
if (irr_ignore(m->otype)) { |
180 |
– |
#if MAXLOOP |
181 |
– |
depth--; |
182 |
– |
#endif |
208 |
|
raytrans(r); |
209 |
|
return(1); |
210 |
|
} |
211 |
|
if (!islight(m->otype)) |
212 |
|
m = &Lamb; |
213 |
|
} |
214 |
< |
/* materials call raytexture */ |
215 |
< |
gotmat = (*ofun[m->otype].funp)(m, r); |
214 |
> |
if ((*ofun[m->otype].funp)(m, r)) |
215 |
> |
return(1); /* materials call raytexture() */ |
216 |
|
} |
217 |
< |
#if MAXLOOP |
193 |
< |
depth--; |
194 |
< |
#endif |
195 |
< |
return(gotmat); |
217 |
> |
return(0); /* no material! */ |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
< |
void |
222 |
< |
rayparticipate(r) /* compute ray medium participation */ |
223 |
< |
register RAY *r; |
221 |
> |
extern void |
222 |
> |
rayparticipate( /* compute ray medium participation */ |
223 |
> |
register RAY *r |
224 |
> |
) |
225 |
|
{ |
226 |
|
COLOR ce, ca; |
227 |
|
double re, ge, be; |
236 |
|
ge *= 1. - colval(r->albedo,GRN); |
237 |
|
be *= 1. - colval(r->albedo,BLU); |
238 |
|
} |
239 |
< |
setcolor(ce, re<=0. ? 1. : re>92. ? 0. : exp(-re), |
240 |
< |
ge<=0. ? 1. : ge>92. ? 0. : exp(-ge), |
241 |
< |
be<=0. ? 1. : be>92. ? 0. : exp(-be)); |
242 |
< |
multcolor(r->rcol, ce); /* path absorption */ |
239 |
> |
setcolor(ce, re<=FTINY ? 1. : re>92. ? 0. : exp(-re), |
240 |
> |
ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge), |
241 |
> |
be<=FTINY ? 1. : be>92. ? 0. : exp(-be)); |
242 |
> |
multcolor(r->rcol, ce); /* path extinction */ |
243 |
|
if (r->crtype & SHADOW || intens(r->albedo) <= FTINY) |
244 |
|
return; /* no scattering */ |
245 |
|
setcolor(ca, |
251 |
|
} |
252 |
|
|
253 |
|
|
254 |
< |
void |
255 |
< |
raytexture(r, mod) /* get material modifiers */ |
256 |
< |
RAY *r; |
257 |
< |
OBJECT mod; |
254 |
> |
extern void |
255 |
> |
raytexture( /* get material modifiers */ |
256 |
> |
RAY *r, |
257 |
> |
OBJECT mod |
258 |
> |
) |
259 |
|
{ |
260 |
|
register OBJREC *m; |
237 |
– |
#if MAXLOOP |
238 |
– |
static int depth = 0; |
239 |
– |
/* check for infinite loop */ |
240 |
– |
if (depth++ >= MAXLOOP) |
241 |
– |
objerror(r->ro, USER, "modifier loop"); |
242 |
– |
#endif |
261 |
|
/* execute textures and patterns */ |
262 |
|
for ( ; mod != OVOID; mod = m->omod) { |
263 |
|
m = objptr(mod); |
273 |
|
objerror(r->ro, USER, errmsg); |
274 |
|
} |
275 |
|
} |
258 |
– |
#if MAXLOOP |
259 |
– |
depth--; /* end here */ |
260 |
– |
#endif |
276 |
|
} |
277 |
|
|
278 |
|
|
279 |
< |
int |
280 |
< |
raymixture(r, fore, back, coef) /* mix modifiers */ |
281 |
< |
register RAY *r; |
282 |
< |
OBJECT fore, back; |
283 |
< |
double coef; |
279 |
> |
extern int |
280 |
> |
raymixture( /* mix modifiers */ |
281 |
> |
register RAY *r, |
282 |
> |
OBJECT fore, |
283 |
> |
OBJECT back, |
284 |
> |
double coef |
285 |
> |
) |
286 |
|
{ |
287 |
|
RAY fr, br; |
288 |
|
int foremat, backmat; |
330 |
|
} |
331 |
|
|
332 |
|
|
333 |
< |
double |
334 |
< |
raydist(r, flags) /* compute (cumulative) ray distance */ |
335 |
< |
register RAY *r; |
336 |
< |
register int flags; |
333 |
> |
extern double |
334 |
> |
raydist( /* compute (cumulative) ray distance */ |
335 |
> |
register const RAY *r, |
336 |
> |
register int flags |
337 |
> |
) |
338 |
|
{ |
339 |
|
double sum = 0.0; |
340 |
|
|
346 |
|
} |
347 |
|
|
348 |
|
|
349 |
< |
double |
350 |
< |
raynormal(norm, r) /* compute perturbed normal for ray */ |
351 |
< |
FVECT norm; |
352 |
< |
register RAY *r; |
349 |
> |
extern void |
350 |
> |
raycontrib( /* compute (cumulative) ray contribution */ |
351 |
> |
double rc[3], |
352 |
> |
const RAY *r, |
353 |
> |
int flags |
354 |
> |
) |
355 |
|
{ |
356 |
+ |
double eext[3]; |
357 |
+ |
int i; |
358 |
+ |
|
359 |
+ |
eext[0] = eext[1] = eext[2] = 0.; |
360 |
+ |
rc[0] = rc[1] = rc[2] = 1.; |
361 |
+ |
|
362 |
+ |
while (r != NULL && r->crtype&flags) { |
363 |
+ |
for (i = 3; i--; ) { |
364 |
+ |
rc[i] *= colval(r->rcoef,i); |
365 |
+ |
eext[i] += r->rot * colval(r->cext,i); |
366 |
+ |
} |
367 |
+ |
r = r->parent; |
368 |
+ |
} |
369 |
+ |
for (i = 3; i--; ) |
370 |
+ |
rc[i] *= (eext[i] <= FTINY) ? 1. : |
371 |
+ |
(eext[i] > 300.) ? 0. : exp(-eext[i]); |
372 |
+ |
} |
373 |
+ |
|
374 |
+ |
|
375 |
+ |
extern double |
376 |
+ |
raynormal( /* compute perturbed normal for ray */ |
377 |
+ |
FVECT norm, |
378 |
+ |
register RAY *r |
379 |
+ |
) |
380 |
+ |
{ |
381 |
|
double newdot; |
382 |
|
register int i; |
383 |
|
|
408 |
|
} |
409 |
|
|
410 |
|
|
411 |
< |
void |
412 |
< |
newrayxf(r) /* get new tranformation matrix for ray */ |
413 |
< |
RAY *r; |
411 |
> |
extern void |
412 |
> |
newrayxf( /* get new tranformation matrix for ray */ |
413 |
> |
RAY *r |
414 |
> |
) |
415 |
|
{ |
416 |
|
static struct xfn { |
417 |
|
struct xfn *next; |
418 |
|
FULLXF xf; |
419 |
|
} xfseed = { &xfseed }, *xflast = &xfseed; |
420 |
|
register struct xfn *xp; |
421 |
< |
register RAY *rp; |
421 |
> |
register const RAY *rp; |
422 |
|
|
423 |
|
/* |
424 |
|
* Search for transform in circular list that |
446 |
|
} |
447 |
|
|
448 |
|
|
449 |
< |
void |
450 |
< |
flipsurface(r) /* reverse surface orientation */ |
451 |
< |
register RAY *r; |
449 |
> |
extern void |
450 |
> |
flipsurface( /* reverse surface orientation */ |
451 |
> |
register RAY *r |
452 |
> |
) |
453 |
|
{ |
454 |
|
r->rod = -r->rod; |
455 |
|
r->ron[0] = -r->ron[0]; |
461 |
|
} |
462 |
|
|
463 |
|
|
464 |
< |
void |
465 |
< |
rayhit(oset, r) /* standard ray hit test */ |
466 |
< |
OBJECT *oset; |
467 |
< |
RAY *r; |
464 |
> |
extern void |
465 |
> |
rayhit( /* standard ray hit test */ |
466 |
> |
OBJECT *oset, |
467 |
> |
RAY *r |
468 |
> |
) |
469 |
|
{ |
470 |
|
OBJREC *o; |
471 |
|
int i; |
478 |
|
} |
479 |
|
|
480 |
|
|
481 |
< |
int |
482 |
< |
localhit(r, scene) /* check for hit in the octree */ |
483 |
< |
register RAY *r; |
484 |
< |
register CUBE *scene; |
481 |
> |
extern int |
482 |
> |
localhit( /* check for hit in the octree */ |
483 |
> |
register RAY *r, |
484 |
> |
register CUBE *scene |
485 |
> |
) |
486 |
|
{ |
487 |
|
OBJECT cxset[MAXCSET+1]; /* set of checked objects */ |
488 |
|
FVECT curpos; /* current cube position */ |
542 |
|
|
543 |
|
|
544 |
|
static int |
545 |
< |
raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */ |
546 |
< |
FVECT pos; /* current position, modified herein */ |
547 |
< |
OBJECT *cxs; /* checked objects, modified by checkhit */ |
548 |
< |
int dirf; /* direction indicators to speed tests */ |
549 |
< |
register RAY *r; |
550 |
< |
register CUBE *cu; |
545 |
> |
raymove( /* check for hit as we move */ |
546 |
> |
FVECT pos, /* current position, modified herein */ |
547 |
> |
OBJECT *cxs, /* checked objects, modified by checkhit */ |
548 |
> |
int dirf, /* direction indicators to speed tests */ |
549 |
> |
register RAY *r, |
550 |
> |
register CUBE *cu |
551 |
> |
) |
552 |
|
{ |
553 |
|
int ax; |
554 |
|
double dt, t; |
629 |
|
|
630 |
|
|
631 |
|
static int |
632 |
< |
checkhit(r, cu, cxs) /* check for hit in full cube */ |
633 |
< |
register RAY *r; |
634 |
< |
CUBE *cu; |
635 |
< |
OBJECT *cxs; |
632 |
> |
checkhit( /* check for hit in full cube */ |
633 |
> |
register RAY *r, |
634 |
> |
CUBE *cu, |
635 |
> |
OBJECT *cxs |
636 |
> |
) |
637 |
|
{ |
638 |
|
OBJECT oset[MAXSET+1]; |
639 |
|
|
650 |
|
|
651 |
|
|
652 |
|
static void |
653 |
< |
checkset(os, cs) /* modify checked set and set to check */ |
654 |
< |
register OBJECT *os; /* os' = os - cs */ |
655 |
< |
register OBJECT *cs; /* cs' = cs + os */ |
653 |
> |
checkset( /* modify checked set and set to check */ |
654 |
> |
register OBJECT *os, /* os' = os - cs */ |
655 |
> |
register OBJECT *cs /* cs' = cs + os */ |
656 |
> |
) |
657 |
|
{ |
658 |
|
OBJECT cset[MAXCSET+MAXSET+1]; |
659 |
|
register int i, j; |