1 |
– |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* raytrace.c - routines for tracing and shading rays. |
6 |
|
* |
7 |
< |
* 8/7/85 |
7 |
> |
* External symbols declared in ray.h |
8 |
|
*/ |
9 |
|
|
10 |
+ |
#include "copyright.h" |
11 |
+ |
|
12 |
|
#include "ray.h" |
13 |
|
|
15 |
– |
#include "octree.h" |
16 |
– |
|
14 |
|
#include "otypes.h" |
15 |
|
|
16 |
|
#include "otspecial.h" |
17 |
|
|
18 |
|
#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ |
19 |
|
|
23 |
– |
extern CUBE thescene; /* our scene */ |
24 |
– |
extern int maxdepth; /* maximum recursion depth */ |
25 |
– |
extern double minweight; /* minimum ray weight */ |
26 |
– |
extern int do_irrad; /* compute irradiance? */ |
27 |
– |
|
20 |
|
unsigned long raynum = 0; /* next unique ray number */ |
21 |
|
unsigned long nrays = 0; /* number of calls to localhit */ |
22 |
|
|
23 |
< |
static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
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, |
27 |
|
}; /* a Lambertian surface */ |
28 |
|
|
29 |
< |
static int raymove(), checkset(), checkhit(); |
29 |
> |
OBJREC Aftplane; /* aft clipping plane object */ |
30 |
|
|
31 |
< |
#define MAXLOOP 128 /* modifier loop detection */ |
31 |
> |
static int raymove(), checkhit(); |
32 |
> |
static void checkset(); |
33 |
|
|
34 |
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
35 |
|
|
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; |
44 |
+ |
|
45 |
|
if ((r->parent = ro) == NULL) { /* primary ray */ |
46 |
|
r->rlvl = 0; |
47 |
|
r->rweight = rw; |
49 |
|
r->rsrc = -1; |
50 |
|
r->clipset = NULL; |
51 |
|
r->revf = raytrace; |
52 |
+ |
copycolor(r->cext, cextinction); |
53 |
+ |
copycolor(r->albedo, salbedo); |
54 |
+ |
r->gecc = seccg; |
55 |
+ |
r->slights = NULL; |
56 |
|
} else { /* spawned ray */ |
57 |
|
r->rlvl = ro->rlvl; |
58 |
|
if (rt & RAYREFL) { |
59 |
|
r->rlvl++; |
60 |
|
r->rsrc = -1; |
61 |
|
r->clipset = ro->clipset; |
62 |
+ |
r->rmax = 0.0; |
63 |
|
} else { |
64 |
|
r->rsrc = ro->rsrc; |
65 |
|
r->clipset = ro->newcset; |
66 |
+ |
r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot; |
67 |
|
} |
68 |
|
r->revf = ro->revf; |
69 |
< |
r->rweight = ro->rweight * rw; |
69 |
> |
copycolor(r->cext, ro->cext); |
70 |
> |
copycolor(r->albedo, ro->albedo); |
71 |
> |
r->gecc = ro->gecc; |
72 |
> |
r->slights = ro->slights; |
73 |
|
r->crtype = ro->crtype | (r->rtype = rt); |
74 |
|
VCOPY(r->rorg, ro->rop); |
75 |
+ |
r->rweight = ro->rweight * rw; |
76 |
+ |
/* estimate absorption */ |
77 |
+ |
re = colval(ro->cext,RED) < colval(ro->cext,GRN) ? |
78 |
+ |
colval(ro->cext,RED) : colval(ro->cext,GRN); |
79 |
+ |
if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU); |
80 |
+ |
if (re > 0.) |
81 |
+ |
r->rweight *= exp(-re*ro->rot); |
82 |
|
} |
83 |
|
rayclear(r); |
84 |
|
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
+ |
void |
89 |
|
rayclear(r) /* clear a ray for (re)evaluation */ |
90 |
|
register RAY *r; |
91 |
|
{ |
92 |
|
r->rno = raynum++; |
93 |
|
r->newcset = r->clipset; |
94 |
+ |
r->hitf = rayhit; |
95 |
+ |
r->robj = OVOID; |
96 |
|
r->ro = NULL; |
97 |
< |
r->rot = FHUGE; |
97 |
> |
r->rox = NULL; |
98 |
> |
r->rt = r->rot = FHUGE; |
99 |
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
100 |
+ |
r->uv[0] = r->uv[1] = 0.0; |
101 |
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
102 |
|
setcolor(r->rcol, 0.0, 0.0, 0.0); |
86 |
– |
r->rt = 0.0; |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
+ |
void |
107 |
|
raytrace(r) /* trace a ray and compute its value */ |
108 |
|
RAY *r; |
109 |
|
{ |
93 |
– |
extern int (*trace)(); |
94 |
– |
int gotmat; |
95 |
– |
|
110 |
|
if (localhit(r, &thescene)) |
111 |
< |
gotmat = raycont(r); |
112 |
< |
else if (sourcehit(r)) |
113 |
< |
gotmat = rayshade(r, r->ro->omod); |
111 |
> |
raycont(r); /* hit local surface, evaluate */ |
112 |
> |
else if (r->ro == &Aftplane) { |
113 |
> |
r->ro = NULL; /* hit aft clipping plane */ |
114 |
> |
r->rot = FHUGE; |
115 |
> |
} else if (sourcehit(r)) |
116 |
> |
rayshade(r, r->ro->omod); /* distant source */ |
117 |
|
|
118 |
< |
if (!gotmat) |
102 |
< |
objerror(r->ro, USER, "material not found"); |
118 |
> |
rayparticipate(r); /* for participating medium */ |
119 |
|
|
120 |
|
if (trace != NULL) |
121 |
|
(*trace)(r); /* trace execution */ |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
+ |
void |
126 |
|
raycont(r) /* check for clipped object and continue */ |
127 |
|
register RAY *r; |
128 |
|
{ |
129 |
|
if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || |
130 |
< |
r->ro->omod == OVOID) { |
130 |
> |
!rayshade(r, r->ro->omod)) |
131 |
|
raytrans(r); |
115 |
– |
return(1); |
116 |
– |
} |
117 |
– |
return(rayshade(r, r->ro->omod)); |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
+ |
void |
136 |
|
raytrans(r) /* transmit ray as is */ |
137 |
|
register RAY *r; |
138 |
|
{ |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
+ |
int |
151 |
|
rayshade(r, mod) /* shade ray r with material mod */ |
152 |
|
register RAY *r; |
153 |
|
int mod; |
154 |
|
{ |
139 |
– |
static int depth = 0; |
155 |
|
int gotmat; |
156 |
|
register OBJREC *m; |
142 |
– |
/* check for infinite loop */ |
143 |
– |
if (depth++ >= MAXLOOP) |
144 |
– |
objerror(r->ro, USER, "possible modifier loop"); |
157 |
|
r->rt = r->rot; /* set effective ray length */ |
158 |
|
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { |
159 |
|
m = objptr(mod); |
164 |
|
} |
165 |
|
******/ |
166 |
|
/* hack for irradiance calculation */ |
167 |
< |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
167 |
> |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) && |
168 |
> |
m->otype != MAT_CLIP && |
169 |
> |
(ofun[m->otype].flags & (T_M|T_X))) { |
170 |
|
if (irr_ignore(m->otype)) { |
157 |
– |
depth--; |
171 |
|
raytrans(r); |
172 |
< |
return; |
172 |
> |
return(1); |
173 |
|
} |
174 |
|
if (!islight(m->otype)) |
175 |
|
m = &Lamb; |
177 |
|
/* materials call raytexture */ |
178 |
|
gotmat = (*ofun[m->otype].funp)(m, r); |
179 |
|
} |
167 |
– |
depth--; |
180 |
|
return(gotmat); |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
+ |
void |
185 |
+ |
rayparticipate(r) /* compute ray medium participation */ |
186 |
+ |
register RAY *r; |
187 |
+ |
{ |
188 |
+ |
COLOR ce, ca; |
189 |
+ |
double re, ge, be; |
190 |
+ |
|
191 |
+ |
if (intens(r->cext) <= 1./FHUGE) |
192 |
+ |
return; /* no medium */ |
193 |
+ |
re = r->rot*colval(r->cext,RED); |
194 |
+ |
ge = r->rot*colval(r->cext,GRN); |
195 |
+ |
be = r->rot*colval(r->cext,BLU); |
196 |
+ |
if (r->crtype & SHADOW) { /* no scattering for sources */ |
197 |
+ |
re *= 1. - colval(r->albedo,RED); |
198 |
+ |
ge *= 1. - colval(r->albedo,GRN); |
199 |
+ |
be *= 1. - colval(r->albedo,BLU); |
200 |
+ |
} |
201 |
+ |
setcolor(ce, re<=0. ? 1. : re>92. ? 0. : exp(-re), |
202 |
+ |
ge<=0. ? 1. : ge>92. ? 0. : exp(-ge), |
203 |
+ |
be<=0. ? 1. : be>92. ? 0. : exp(-be)); |
204 |
+ |
multcolor(r->rcol, ce); /* path absorption */ |
205 |
+ |
if (r->crtype & SHADOW || intens(r->albedo) <= FTINY) |
206 |
+ |
return; /* no scattering */ |
207 |
+ |
setcolor(ca, |
208 |
+ |
colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)), |
209 |
+ |
colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)), |
210 |
+ |
colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU))); |
211 |
+ |
addcolor(r->rcol, ca); /* ambient in scattering */ |
212 |
+ |
srcscatter(r); /* source in scattering */ |
213 |
+ |
} |
214 |
+ |
|
215 |
+ |
|
216 |
+ |
void |
217 |
|
raytexture(r, mod) /* get material modifiers */ |
218 |
|
RAY *r; |
219 |
< |
int mod; |
219 |
> |
OBJECT mod; |
220 |
|
{ |
176 |
– |
static int depth = 0; |
221 |
|
register OBJREC *m; |
178 |
– |
/* check for infinite loop */ |
179 |
– |
if (depth++ >= MAXLOOP) |
180 |
– |
objerror(r->ro, USER, "modifier loop"); |
222 |
|
/* execute textures and patterns */ |
223 |
|
for ( ; mod != OVOID; mod = m->omod) { |
224 |
|
m = objptr(mod); |
228 |
|
error(USER, errmsg); |
229 |
|
} |
230 |
|
******/ |
231 |
< |
if ((*ofun[m->otype].funp)(m, r)) |
232 |
< |
objerror(r->ro, USER, "conflicting materials"); |
231 |
> |
if ((*ofun[m->otype].funp)(m, r)) { |
232 |
> |
sprintf(errmsg, "conflicting material \"%s\"", |
233 |
> |
m->oname); |
234 |
> |
objerror(r->ro, USER, errmsg); |
235 |
> |
} |
236 |
|
} |
193 |
– |
depth--; /* end here */ |
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
+ |
int |
241 |
|
raymixture(r, fore, back, coef) /* mix modifiers */ |
242 |
|
register RAY *r; |
243 |
|
OBJECT fore, back; |
244 |
|
double coef; |
245 |
|
{ |
246 |
|
RAY fr, br; |
203 |
– |
COLOR ctmp; |
247 |
|
int foremat, backmat; |
248 |
|
register int i; |
249 |
< |
/* clip coefficient */ |
249 |
> |
/* bound coefficient */ |
250 |
|
if (coef > 1.0) |
251 |
|
coef = 1.0; |
252 |
|
else if (coef < 0.0) |
253 |
|
coef = 0.0; |
254 |
+ |
/* compute foreground and background */ |
255 |
+ |
foremat = backmat = 0; |
256 |
|
/* foreground */ |
257 |
< |
copystruct(&fr, r); |
258 |
< |
fr.pert[0] = fr.pert[1] = fr.pert[2] = 0.0; |
214 |
< |
setcolor(fr.pcol, 1.0, 1.0, 1.0); |
215 |
< |
setcolor(fr.rcol, 0.0, 0.0, 0.0); |
216 |
< |
if (fore != OVOID && coef > FTINY) |
257 |
> |
fr = *r; |
258 |
> |
if (coef > FTINY) |
259 |
|
foremat = rayshade(&fr, fore); |
218 |
– |
else |
219 |
– |
foremat = 0; |
260 |
|
/* background */ |
261 |
< |
copystruct(&br, r); |
262 |
< |
br.pert[0] = br.pert[1] = br.pert[2] = 0.0; |
223 |
< |
setcolor(br.pcol, 1.0, 1.0, 1.0); |
224 |
< |
setcolor(br.rcol, 0.0, 0.0, 0.0); |
225 |
< |
if (back != OVOID && coef < 1.0-FTINY) |
261 |
> |
br = *r; |
262 |
> |
if (coef < 1.0-FTINY) |
263 |
|
backmat = rayshade(&br, back); |
264 |
< |
else |
265 |
< |
backmat = foremat; |
266 |
< |
/* check */ |
267 |
< |
if ((backmat==0) != (foremat==0)) |
268 |
< |
objerror(r->ro, USER, "mixing material with non-material"); |
269 |
< |
/* sum perturbations */ |
264 |
> |
/* check for transparency */ |
265 |
> |
if (backmat ^ foremat) { |
266 |
> |
if (backmat && coef > FTINY) |
267 |
> |
raytrans(&fr); |
268 |
> |
else if (foremat && coef < 1.0-FTINY) |
269 |
> |
raytrans(&br); |
270 |
> |
} |
271 |
> |
/* mix perturbations */ |
272 |
|
for (i = 0; i < 3; i++) |
273 |
< |
r->pert[i] += coef*fr.pert[i] + (1.0-coef)*br.pert[i]; |
274 |
< |
/* multiply pattern colors */ |
273 |
> |
r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; |
274 |
> |
/* mix pattern colors */ |
275 |
|
scalecolor(fr.pcol, coef); |
276 |
|
scalecolor(br.pcol, 1.0-coef); |
277 |
< |
copycolor(ctmp, fr.pcol); |
278 |
< |
addcolor(ctmp, br.pcol); |
279 |
< |
multcolor(r->pcol, ctmp); |
280 |
< |
/* sum returned ray values */ |
277 |
> |
copycolor(r->pcol, fr.pcol); |
278 |
> |
addcolor(r->pcol, br.pcol); |
279 |
> |
/* return value tells if material */ |
280 |
> |
if (!foremat & !backmat) |
281 |
> |
return(0); |
282 |
> |
/* mix returned ray values */ |
283 |
|
scalecolor(fr.rcol, coef); |
284 |
|
scalecolor(br.rcol, 1.0-coef); |
285 |
< |
addcolor(r->rcol, fr.rcol); |
285 |
> |
copycolor(r->rcol, fr.rcol); |
286 |
|
addcolor(r->rcol, br.rcol); |
287 |
< |
if (foremat) |
288 |
< |
r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt; |
248 |
< |
/* return value tells if material */ |
249 |
< |
return(foremat); |
287 |
> |
r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt; |
288 |
> |
return(1); |
289 |
|
} |
290 |
|
|
291 |
|
|
292 |
|
double |
293 |
+ |
raydist(r, flags) /* compute (cumulative) ray distance */ |
294 |
+ |
register RAY *r; |
295 |
+ |
register int flags; |
296 |
+ |
{ |
297 |
+ |
double sum = 0.0; |
298 |
+ |
|
299 |
+ |
while (r != NULL && r->crtype&flags) { |
300 |
+ |
sum += r->rot; |
301 |
+ |
r = r->parent; |
302 |
+ |
} |
303 |
+ |
return(sum); |
304 |
+ |
} |
305 |
+ |
|
306 |
+ |
|
307 |
+ |
double |
308 |
|
raynormal(norm, r) /* compute perturbed normal for ray */ |
309 |
|
FVECT norm; |
310 |
|
register RAY *r; |
339 |
|
} |
340 |
|
|
341 |
|
|
342 |
+ |
void |
343 |
|
newrayxf(r) /* get new tranformation matrix for ray */ |
344 |
|
RAY *r; |
345 |
|
{ |
359 |
|
if (rp->rox == &xp->xf) { /* xp in use */ |
360 |
|
xp = xp->next; /* move to next */ |
361 |
|
if (xp == xflast) { /* need new one */ |
362 |
< |
xp = (struct xfn *)bmalloc(sizeof(struct xfn)); |
362 |
> |
xp = (struct xfn *)malloc(sizeof(struct xfn)); |
363 |
|
if (xp == NULL) |
364 |
|
error(SYSTEM, |
365 |
|
"out of memory in newrayxf"); |
376 |
|
} |
377 |
|
|
378 |
|
|
379 |
+ |
void |
380 |
|
flipsurface(r) /* reverse surface orientation */ |
381 |
|
register RAY *r; |
382 |
|
{ |
390 |
|
} |
391 |
|
|
392 |
|
|
393 |
+ |
void |
394 |
+ |
rayhit(oset, r) /* standard ray hit test */ |
395 |
+ |
OBJECT *oset; |
396 |
+ |
RAY *r; |
397 |
+ |
{ |
398 |
+ |
OBJREC *o; |
399 |
+ |
int i; |
400 |
+ |
|
401 |
+ |
for (i = oset[0]; i > 0; i--) { |
402 |
+ |
o = objptr(oset[i]); |
403 |
+ |
if ((*ofun[o->otype].funp)(o, r)) |
404 |
+ |
r->robj = oset[i]; |
405 |
+ |
} |
406 |
+ |
} |
407 |
+ |
|
408 |
+ |
|
409 |
+ |
int |
410 |
|
localhit(r, scene) /* check for hit in the octree */ |
411 |
|
register RAY *r; |
412 |
|
register CUBE *scene; |
428 |
|
} |
429 |
|
if (sflags == 0) |
430 |
|
error(CONSISTENCY, "zero ray direction in localhit"); |
431 |
+ |
/* start off assuming nothing hit */ |
432 |
+ |
if (r->rmax > FTINY) { /* except aft plane if one */ |
433 |
+ |
r->ro = &Aftplane; |
434 |
+ |
r->rot = r->rmax; |
435 |
+ |
for (i = 0; i < 3; i++) |
436 |
+ |
r->rop[i] = r->rorg[i] + r->rot*r->rdir[i]; |
437 |
+ |
} |
438 |
+ |
/* find global cube entrance point */ |
439 |
|
t = 0.0; |
440 |
|
if (!incube(scene, curpos)) { |
441 |
|
/* find distance to entry */ |
453 |
|
t = dt; /* farthest face is the one */ |
454 |
|
} |
455 |
|
t += FTINY; /* fudge to get inside cube */ |
456 |
+ |
if (t >= r->rot) /* clipped already */ |
457 |
+ |
return(0); |
458 |
|
/* advance position */ |
459 |
|
for (i = 0; i < 3; i++) |
460 |
|
curpos[i] += r->rdir[i]*t; |
463 |
|
return(0); |
464 |
|
} |
465 |
|
cxset[0] = 0; |
466 |
< |
return(raymove(curpos, cxset, sflags, r, scene) == RAYHIT); |
466 |
> |
raymove(curpos, cxset, sflags, r, scene); |
467 |
> |
return((r->ro != NULL) & (r->ro != &Aftplane)); |
468 |
|
} |
469 |
|
|
470 |
|
|
519 |
|
} |
520 |
|
/*NOTREACHED*/ |
521 |
|
} |
522 |
< |
if (isfull(cu->cutree) && checkhit(r, cu, cxs)) |
522 |
> |
if (isfull(cu->cutree)) { |
523 |
> |
if (checkhit(r, cu, cxs)) |
524 |
> |
return(RAYHIT); |
525 |
> |
} else if (r->ro == &Aftplane && incube(cu, r->rop)) |
526 |
|
return(RAYHIT); |
527 |
|
/* advance to next cube */ |
528 |
|
if (dirf&0x11) { |
554 |
|
} |
555 |
|
|
556 |
|
|
557 |
< |
static |
557 |
> |
static int |
558 |
|
checkhit(r, cu, cxs) /* check for hit in full cube */ |
559 |
|
register RAY *r; |
560 |
|
CUBE *cu; |
561 |
|
OBJECT *cxs; |
562 |
|
{ |
563 |
|
OBJECT oset[MAXSET+1]; |
477 |
– |
register OBJREC *o; |
478 |
– |
register int i; |
564 |
|
|
565 |
|
objset(oset, cu->cutree); |
566 |
< |
checkset(oset, cxs); /* eliminate double-checking */ |
567 |
< |
for (i = oset[0]; i > 0; i--) { |
568 |
< |
o = objptr(oset[i]); |
569 |
< |
(*ofun[o->otype].funp)(o, r); |
570 |
< |
} |
486 |
< |
if (r->ro == NULL) |
566 |
> |
checkset(oset, cxs); /* avoid double-checking */ |
567 |
> |
|
568 |
> |
(*r->hitf)(oset, r); /* test for hit in set */ |
569 |
> |
|
570 |
> |
if (r->robj == OVOID) |
571 |
|
return(0); /* no scores yet */ |
572 |
|
|
573 |
|
return(incube(cu, r->rop)); /* hit OK if in current cube */ |
574 |
|
} |
575 |
|
|
576 |
|
|
577 |
< |
static |
577 |
> |
static void |
578 |
|
checkset(os, cs) /* modify checked set and set to check */ |
579 |
|
register OBJECT *os; /* os' = os - cs */ |
580 |
|
register OBJECT *cs; /* cs' = cs + os */ |