10 |
|
#include "copyright.h" |
11 |
|
|
12 |
|
#include "ray.h" |
13 |
< |
|
13 |
> |
#include "source.h" |
14 |
|
#include "otypes.h" |
15 |
– |
|
15 |
|
#include "otspecial.h" |
16 |
|
|
17 |
|
#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ |
19 |
|
unsigned long raynum = 0; /* next unique ray number */ |
20 |
|
unsigned long nrays = 0; /* number of calls to localhit */ |
21 |
|
|
22 |
< |
static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
22 |
> |
static RREAL Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
23 |
|
OBJREC Lamb = { |
24 |
|
OVOID, MAT_PLASTIC, "Lambertian", |
25 |
|
{0, 5, NULL, Lambfa}, NULL, |
27 |
|
|
28 |
|
OBJREC Aftplane; /* aft clipping plane object */ |
29 |
|
|
31 |
– |
static int raymove(), checkhit(); |
32 |
– |
static void checkset(); |
33 |
– |
|
34 |
– |
#ifndef MAXLOOP |
35 |
– |
#define MAXLOOP 0 /* modifier loop detection */ |
36 |
– |
#endif |
37 |
– |
|
30 |
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
31 |
|
|
32 |
+ |
static int raymove(FVECT pos, OBJECT *cxs, int dirf, RAY *r, CUBE *cu); |
33 |
+ |
static int checkhit(RAY *r, CUBE *cu, OBJECT *cxs); |
34 |
+ |
static void checkset(OBJECT *os, OBJECT *cs); |
35 |
|
|
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; |
36 |
|
|
37 |
+ |
extern int |
38 |
+ |
rayorigin( /* start new ray from old one */ |
39 |
+ |
RAY *r, |
40 |
+ |
int rt, |
41 |
+ |
const RAY *ro, |
42 |
+ |
const COLOR rc |
43 |
+ |
) |
44 |
+ |
{ |
45 |
+ |
double rw, re; |
46 |
+ |
/* assign coefficient/weight */ |
47 |
+ |
if (rc == NULL) { |
48 |
+ |
rw = 1.0; |
49 |
+ |
setcolor(r->rcoef, 1., 1., 1.); |
50 |
+ |
} else { |
51 |
+ |
rw = intens(rc); |
52 |
+ |
if (rc != r->rcoef) |
53 |
+ |
copycolor(r->rcoef, rc); |
54 |
+ |
} |
55 |
|
if ((r->parent = ro) == NULL) { /* primary ray */ |
56 |
|
r->rlvl = 0; |
57 |
|
r->rweight = rw; |
58 |
|
r->crtype = r->rtype = rt; |
59 |
|
r->rsrc = -1; |
60 |
|
r->clipset = NULL; |
55 |
– |
r->revf = raytrace; |
61 |
|
copycolor(r->cext, cextinction); |
62 |
|
copycolor(r->albedo, salbedo); |
63 |
|
r->gecc = seccg; |
64 |
|
r->slights = NULL; |
65 |
|
} else { /* spawned ray */ |
66 |
+ |
if (ro->rot >= FHUGE) { |
67 |
+ |
memset(r, 0, sizeof(RAY)); |
68 |
+ |
return(-1); /* illegal continuation */ |
69 |
+ |
} |
70 |
|
r->rlvl = ro->rlvl; |
71 |
|
if (rt & RAYREFL) { |
72 |
|
r->rlvl++; |
78 |
|
r->clipset = ro->newcset; |
79 |
|
r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot; |
80 |
|
} |
72 |
– |
r->revf = ro->revf; |
81 |
|
copycolor(r->cext, ro->cext); |
82 |
|
copycolor(r->albedo, ro->albedo); |
83 |
|
r->gecc = ro->gecc; |
85 |
|
r->crtype = ro->crtype | (r->rtype = rt); |
86 |
|
VCOPY(r->rorg, ro->rop); |
87 |
|
r->rweight = ro->rweight * rw; |
88 |
< |
/* estimate absorption */ |
88 |
> |
/* estimate extinction */ |
89 |
|
re = colval(ro->cext,RED) < colval(ro->cext,GRN) ? |
90 |
|
colval(ro->cext,RED) : colval(ro->cext,GRN); |
91 |
|
if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU); |
92 |
< |
if (re > 0.) |
93 |
< |
r->rweight *= exp(-re*ro->rot); |
92 |
> |
re *= ro->rot; |
93 |
> |
if (re > .1) |
94 |
> |
r->rweight *= exp(-re); |
95 |
|
} |
96 |
|
rayclear(r); |
97 |
|
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
< |
void |
102 |
< |
rayclear(r) /* clear a ray for (re)evaluation */ |
103 |
< |
register RAY *r; |
101 |
> |
extern void |
102 |
> |
rayclear( /* clear a ray for (re)evaluation */ |
103 |
> |
register RAY *r |
104 |
> |
) |
105 |
|
{ |
106 |
|
r->rno = raynum++; |
107 |
|
r->newcset = r->clipset; |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
< |
void |
121 |
< |
raytrace(r) /* trace a ray and compute its value */ |
122 |
< |
RAY *r; |
120 |
> |
extern void |
121 |
> |
rayvalue( /* trace a ray and compute its value */ |
122 |
> |
RAY *r |
123 |
> |
) |
124 |
|
{ |
125 |
|
if (localhit(r, &thescene)) |
126 |
|
raycont(r); /* hit local surface, evaluate */ |
130 |
|
} else if (sourcehit(r)) |
131 |
|
rayshade(r, r->ro->omod); /* distant source */ |
132 |
|
|
122 |
– |
rayparticipate(r); /* for participating medium */ |
123 |
– |
|
133 |
|
if (trace != NULL) |
134 |
|
(*trace)(r); /* trace execution */ |
135 |
+ |
|
136 |
+ |
rayparticipate(r); /* for participating medium */ |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
< |
void |
141 |
< |
raycont(r) /* check for clipped object and continue */ |
142 |
< |
register RAY *r; |
140 |
> |
extern void |
141 |
> |
raycont( /* check for clipped object and continue */ |
142 |
> |
register RAY *r |
143 |
> |
) |
144 |
|
{ |
145 |
|
if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || |
146 |
|
!rayshade(r, r->ro->omod)) |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
< |
void |
152 |
< |
raytrans(r) /* transmit ray as is */ |
153 |
< |
register RAY *r; |
151 |
> |
extern void |
152 |
> |
raytrans( /* transmit ray as is */ |
153 |
> |
register RAY *r |
154 |
> |
) |
155 |
|
{ |
156 |
|
RAY tr; |
157 |
|
|
158 |
< |
if (rayorigin(&tr, r, TRANS, 1.0) == 0) { |
158 |
> |
if (rayorigin(&tr, TRANS, r, NULL) == 0) { |
159 |
|
VCOPY(tr.rdir, r->rdir); |
160 |
|
rayvalue(&tr); |
161 |
|
copycolor(r->rcol, tr.rcol); |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
< |
int |
168 |
< |
rayshade(r, mod) /* shade ray r with material mod */ |
169 |
< |
register RAY *r; |
170 |
< |
int mod; |
167 |
> |
extern int |
168 |
> |
rayshade( /* shade ray r with material mod */ |
169 |
> |
register RAY *r, |
170 |
> |
int mod |
171 |
> |
) |
172 |
|
{ |
159 |
– |
int gotmat; |
173 |
|
register OBJREC *m; |
174 |
< |
#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 |
174 |
> |
|
175 |
|
r->rt = r->rot; /* set effective ray length */ |
176 |
< |
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { |
176 |
> |
for ( ; mod != OVOID; mod = m->omod) { |
177 |
|
m = objptr(mod); |
178 |
|
/****** unnecessary test since modifier() is always called |
179 |
|
if (!ismodifier(m->otype)) { |
182 |
|
} |
183 |
|
******/ |
184 |
|
/* hack for irradiance calculation */ |
185 |
< |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
185 |
> |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) && |
186 |
> |
m->otype != MAT_CLIP && |
187 |
> |
(ofun[m->otype].flags & (T_M|T_X))) { |
188 |
|
if (irr_ignore(m->otype)) { |
179 |
– |
#if MAXLOOP |
180 |
– |
depth--; |
181 |
– |
#endif |
189 |
|
raytrans(r); |
190 |
|
return(1); |
191 |
|
} |
192 |
|
if (!islight(m->otype)) |
193 |
|
m = &Lamb; |
194 |
|
} |
195 |
< |
/* materials call raytexture */ |
196 |
< |
gotmat = (*ofun[m->otype].funp)(m, r); |
195 |
> |
if ((*ofun[m->otype].funp)(m, r)) |
196 |
> |
return(1); /* materials call raytexture() */ |
197 |
|
} |
198 |
< |
#if MAXLOOP |
192 |
< |
depth--; |
193 |
< |
#endif |
194 |
< |
return(gotmat); |
198 |
> |
return(0); /* no material! */ |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
< |
void |
203 |
< |
rayparticipate(r) /* compute ray medium participation */ |
204 |
< |
register RAY *r; |
202 |
> |
extern void |
203 |
> |
rayparticipate( /* compute ray medium participation */ |
204 |
> |
register RAY *r |
205 |
> |
) |
206 |
|
{ |
207 |
|
COLOR ce, ca; |
208 |
|
double re, ge, be; |
217 |
|
ge *= 1. - colval(r->albedo,GRN); |
218 |
|
be *= 1. - colval(r->albedo,BLU); |
219 |
|
} |
220 |
< |
setcolor(ce, re<=0. ? 1. : re>92. ? 0. : exp(-re), |
221 |
< |
ge<=0. ? 1. : ge>92. ? 0. : exp(-ge), |
222 |
< |
be<=0. ? 1. : be>92. ? 0. : exp(-be)); |
223 |
< |
multcolor(r->rcol, ce); /* path absorption */ |
220 |
> |
setcolor(ce, re<=FTINY ? 1. : re>92. ? 0. : exp(-re), |
221 |
> |
ge<=FTINY ? 1. : ge>92. ? 0. : exp(-ge), |
222 |
> |
be<=FTINY ? 1. : be>92. ? 0. : exp(-be)); |
223 |
> |
multcolor(r->rcol, ce); /* path extinction */ |
224 |
|
if (r->crtype & SHADOW || intens(r->albedo) <= FTINY) |
225 |
|
return; /* no scattering */ |
226 |
|
setcolor(ca, |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
< |
raytexture(r, mod) /* get material modifiers */ |
236 |
< |
RAY *r; |
237 |
< |
int mod; |
235 |
> |
extern void |
236 |
> |
raytexture( /* get material modifiers */ |
237 |
> |
RAY *r, |
238 |
> |
OBJECT mod |
239 |
> |
) |
240 |
|
{ |
241 |
|
register OBJREC *m; |
235 |
– |
#if MAXLOOP |
236 |
– |
static int depth = 0; |
237 |
– |
/* check for infinite loop */ |
238 |
– |
if (depth++ >= MAXLOOP) |
239 |
– |
objerror(r->ro, USER, "modifier loop"); |
240 |
– |
#endif |
242 |
|
/* execute textures and patterns */ |
243 |
|
for ( ; mod != OVOID; mod = m->omod) { |
244 |
|
m = objptr(mod); |
254 |
|
objerror(r->ro, USER, errmsg); |
255 |
|
} |
256 |
|
} |
256 |
– |
#if MAXLOOP |
257 |
– |
depth--; /* end here */ |
258 |
– |
#endif |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
< |
int |
261 |
< |
raymixture(r, fore, back, coef) /* mix modifiers */ |
262 |
< |
register RAY *r; |
263 |
< |
OBJECT fore, back; |
264 |
< |
double coef; |
260 |
> |
extern int |
261 |
> |
raymixture( /* mix modifiers */ |
262 |
> |
register RAY *r, |
263 |
> |
OBJECT fore, |
264 |
> |
OBJECT back, |
265 |
> |
double coef |
266 |
> |
) |
267 |
|
{ |
268 |
|
RAY fr, br; |
269 |
|
int foremat, backmat; |
276 |
|
/* compute foreground and background */ |
277 |
|
foremat = backmat = 0; |
278 |
|
/* foreground */ |
279 |
< |
copystruct(&fr, r); |
279 |
> |
fr = *r; |
280 |
|
if (coef > FTINY) |
281 |
|
foremat = rayshade(&fr, fore); |
282 |
|
/* background */ |
283 |
< |
copystruct(&br, r); |
283 |
> |
br = *r; |
284 |
|
if (coef < 1.0-FTINY) |
285 |
|
backmat = rayshade(&br, back); |
286 |
|
/* check for transparency */ |
287 |
< |
if (backmat ^ foremat) |
287 |
> |
if (backmat ^ foremat) { |
288 |
|
if (backmat && coef > FTINY) |
289 |
|
raytrans(&fr); |
290 |
|
else if (foremat && coef < 1.0-FTINY) |
291 |
|
raytrans(&br); |
292 |
+ |
} |
293 |
|
/* mix perturbations */ |
294 |
|
for (i = 0; i < 3; i++) |
295 |
|
r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; |
311 |
|
} |
312 |
|
|
313 |
|
|
314 |
< |
double |
315 |
< |
raydist(r, flags) /* compute (cumulative) ray distance */ |
316 |
< |
register RAY *r; |
317 |
< |
register int flags; |
314 |
> |
extern double |
315 |
> |
raydist( /* compute (cumulative) ray distance */ |
316 |
> |
register const RAY *r, |
317 |
> |
register int flags |
318 |
> |
) |
319 |
|
{ |
320 |
|
double sum = 0.0; |
321 |
|
|
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
< |
double |
331 |
< |
raynormal(norm, r) /* compute perturbed normal for ray */ |
332 |
< |
FVECT norm; |
333 |
< |
register RAY *r; |
330 |
> |
extern void |
331 |
> |
raycontrib( /* compute (cumulative) ray contribution */ |
332 |
> |
COLOR rc, |
333 |
> |
const RAY *r, |
334 |
> |
int flags |
335 |
> |
) |
336 |
|
{ |
337 |
+ |
COLOR eext, ext1; |
338 |
+ |
|
339 |
+ |
setcolor(eext, 0., 0., 0.); |
340 |
+ |
setcolor(rc, 1., 1., 1.); |
341 |
+ |
|
342 |
+ |
while (r != NULL && r->crtype&flags) { |
343 |
+ |
multcolor(rc, r->rcoef); |
344 |
+ |
copycolor(ext1, r->cext); |
345 |
+ |
scalecolor(ext1, r->rot); |
346 |
+ |
addcolor(eext, ext1); |
347 |
+ |
r = r->parent; |
348 |
+ |
} |
349 |
+ |
if (intens(eext) > FTINY) { |
350 |
+ |
setcolor(ext1, exp(-colval(eext,RED)), |
351 |
+ |
exp(-colval(eext,GRN)), |
352 |
+ |
exp(-colval(eext,BLU))); |
353 |
+ |
multcolor(rc, ext1); |
354 |
+ |
} |
355 |
+ |
} |
356 |
+ |
|
357 |
+ |
|
358 |
+ |
extern double |
359 |
+ |
raynormal( /* compute perturbed normal for ray */ |
360 |
+ |
FVECT norm, |
361 |
+ |
register RAY *r |
362 |
+ |
) |
363 |
+ |
{ |
364 |
|
double newdot; |
365 |
|
register int i; |
366 |
|
|
391 |
|
} |
392 |
|
|
393 |
|
|
394 |
< |
void |
395 |
< |
newrayxf(r) /* get new tranformation matrix for ray */ |
396 |
< |
RAY *r; |
394 |
> |
extern void |
395 |
> |
newrayxf( /* get new tranformation matrix for ray */ |
396 |
> |
RAY *r |
397 |
> |
) |
398 |
|
{ |
399 |
|
static struct xfn { |
400 |
|
struct xfn *next; |
401 |
|
FULLXF xf; |
402 |
|
} xfseed = { &xfseed }, *xflast = &xfseed; |
403 |
|
register struct xfn *xp; |
404 |
< |
register RAY *rp; |
404 |
> |
register const RAY *rp; |
405 |
|
|
406 |
|
/* |
407 |
|
* Search for transform in circular list that |
429 |
|
} |
430 |
|
|
431 |
|
|
432 |
< |
void |
433 |
< |
flipsurface(r) /* reverse surface orientation */ |
434 |
< |
register RAY *r; |
432 |
> |
extern void |
433 |
> |
flipsurface( /* reverse surface orientation */ |
434 |
> |
register RAY *r |
435 |
> |
) |
436 |
|
{ |
437 |
|
r->rod = -r->rod; |
438 |
|
r->ron[0] = -r->ron[0]; |
444 |
|
} |
445 |
|
|
446 |
|
|
447 |
< |
void |
448 |
< |
rayhit(oset, r) /* standard ray hit test */ |
449 |
< |
OBJECT *oset; |
450 |
< |
RAY *r; |
447 |
> |
extern void |
448 |
> |
rayhit( /* standard ray hit test */ |
449 |
> |
OBJECT *oset, |
450 |
> |
RAY *r |
451 |
> |
) |
452 |
|
{ |
453 |
|
OBJREC *o; |
454 |
|
int i; |
461 |
|
} |
462 |
|
|
463 |
|
|
464 |
< |
int |
465 |
< |
localhit(r, scene) /* check for hit in the octree */ |
466 |
< |
register RAY *r; |
467 |
< |
register CUBE *scene; |
464 |
> |
extern int |
465 |
> |
localhit( /* check for hit in the octree */ |
466 |
> |
register RAY *r, |
467 |
> |
register CUBE *scene |
468 |
> |
) |
469 |
|
{ |
470 |
|
OBJECT cxset[MAXCSET+1]; /* set of checked objects */ |
471 |
|
FVECT curpos; /* current cube position */ |
520 |
|
} |
521 |
|
cxset[0] = 0; |
522 |
|
raymove(curpos, cxset, sflags, r, scene); |
523 |
< |
return(r->ro != NULL & r->ro != &Aftplane); |
523 |
> |
return((r->ro != NULL) & (r->ro != &Aftplane)); |
524 |
|
} |
525 |
|
|
526 |
|
|
527 |
|
static int |
528 |
< |
raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */ |
529 |
< |
FVECT pos; /* current position, modified herein */ |
530 |
< |
OBJECT *cxs; /* checked objects, modified by checkhit */ |
531 |
< |
int dirf; /* direction indicators to speed tests */ |
532 |
< |
register RAY *r; |
533 |
< |
register CUBE *cu; |
528 |
> |
raymove( /* check for hit as we move */ |
529 |
> |
FVECT pos, /* current position, modified herein */ |
530 |
> |
OBJECT *cxs, /* checked objects, modified by checkhit */ |
531 |
> |
int dirf, /* direction indicators to speed tests */ |
532 |
> |
register RAY *r, |
533 |
> |
register CUBE *cu |
534 |
> |
) |
535 |
|
{ |
536 |
|
int ax; |
537 |
|
double dt, t; |
612 |
|
|
613 |
|
|
614 |
|
static int |
615 |
< |
checkhit(r, cu, cxs) /* check for hit in full cube */ |
616 |
< |
register RAY *r; |
617 |
< |
CUBE *cu; |
618 |
< |
OBJECT *cxs; |
615 |
> |
checkhit( /* check for hit in full cube */ |
616 |
> |
register RAY *r, |
617 |
> |
CUBE *cu, |
618 |
> |
OBJECT *cxs |
619 |
> |
) |
620 |
|
{ |
621 |
|
OBJECT oset[MAXSET+1]; |
622 |
|
|
633 |
|
|
634 |
|
|
635 |
|
static void |
636 |
< |
checkset(os, cs) /* modify checked set and set to check */ |
637 |
< |
register OBJECT *os; /* os' = os - cs */ |
638 |
< |
register OBJECT *cs; /* cs' = cs + os */ |
636 |
> |
checkset( /* modify checked set and set to check */ |
637 |
> |
register OBJECT *os, /* os' = os - cs */ |
638 |
> |
register OBJECT *cs /* cs' = cs + os */ |
639 |
> |
) |
640 |
|
{ |
641 |
|
OBJECT cset[MAXCSET+MAXSET+1]; |
642 |
|
register int i, j; |