1 |
greg |
1.17 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
greg |
1.1 |
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* raytrace.c - routines for tracing and shading rays. |
9 |
|
|
* |
10 |
|
|
* 8/7/85 |
11 |
|
|
*/ |
12 |
|
|
|
13 |
|
|
#include "ray.h" |
14 |
|
|
|
15 |
|
|
#include "octree.h" |
16 |
|
|
|
17 |
|
|
#include "otypes.h" |
18 |
|
|
|
19 |
greg |
1.15 |
#include "otspecial.h" |
20 |
|
|
|
21 |
greg |
1.1 |
extern CUBE thescene; /* our scene */ |
22 |
|
|
extern int maxdepth; /* maximum recursion depth */ |
23 |
|
|
extern double minweight; /* minimum ray weight */ |
24 |
greg |
1.15 |
extern int do_irrad; /* compute irradiance? */ |
25 |
greg |
1.1 |
|
26 |
greg |
1.20 |
long raynum = 0L; /* next unique ray number */ |
27 |
|
|
long nrays = 0L; /* number of calls to rayvalue */ |
28 |
greg |
1.1 |
|
29 |
greg |
1.15 |
static double Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
30 |
|
|
OBJREC Lamb = { |
31 |
|
|
OVOID, MAT_PLASTIC, "Lambertian", |
32 |
|
|
{0, 5, NULL, Lambfa}, NULL, -1, |
33 |
|
|
}; /* a Lambertian surface */ |
34 |
|
|
|
35 |
greg |
1.6 |
#define MAXLOOP 128 /* modifier loop detection */ |
36 |
greg |
1.1 |
|
37 |
|
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
rayorigin(r, ro, rt, rw) /* start new ray from old one */ |
41 |
|
|
register RAY *r, *ro; |
42 |
|
|
int rt; |
43 |
|
|
double rw; |
44 |
|
|
{ |
45 |
|
|
if ((r->parent = ro) == NULL) { /* primary ray */ |
46 |
|
|
r->rlvl = 0; |
47 |
|
|
r->rweight = rw; |
48 |
|
|
r->crtype = r->rtype = rt; |
49 |
|
|
r->rsrc = -1; |
50 |
|
|
r->clipset = NULL; |
51 |
|
|
} else { /* spawned ray */ |
52 |
|
|
r->rlvl = ro->rlvl; |
53 |
|
|
if (rt & RAYREFL) { |
54 |
|
|
r->rlvl++; |
55 |
|
|
r->rsrc = -1; |
56 |
|
|
r->clipset = ro->clipset; |
57 |
|
|
} else { |
58 |
|
|
r->rsrc = ro->rsrc; |
59 |
|
|
r->clipset = ro->newcset; |
60 |
|
|
} |
61 |
|
|
r->rweight = ro->rweight * rw; |
62 |
|
|
r->crtype = ro->crtype | (r->rtype = rt); |
63 |
|
|
VCOPY(r->rorg, ro->rop); |
64 |
|
|
} |
65 |
greg |
1.20 |
r->rno = raynum++; |
66 |
greg |
1.1 |
r->newcset = r->clipset; |
67 |
|
|
r->ro = NULL; |
68 |
|
|
r->rot = FHUGE; |
69 |
|
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
70 |
|
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
71 |
|
|
setcolor(r->rcol, 0.0, 0.0, 0.0); |
72 |
greg |
1.10 |
r->rt = 0.0; |
73 |
greg |
1.1 |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
|
77 |
|
|
rayvalue(r) /* compute a ray's value */ |
78 |
greg |
1.8 |
RAY *r; |
79 |
greg |
1.1 |
{ |
80 |
|
|
extern int (*trace)(); |
81 |
|
|
|
82 |
greg |
1.20 |
nrays++; /* increment trace counter */ |
83 |
greg |
1.15 |
if (localhit(r, &thescene)) |
84 |
greg |
1.8 |
raycont(r); |
85 |
greg |
1.15 |
else if (sourcehit(r)) |
86 |
|
|
rayshade(r, r->ro->omod); |
87 |
greg |
1.1 |
|
88 |
|
|
if (trace != NULL) |
89 |
|
|
(*trace)(r); /* trace execution */ |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
|
93 |
greg |
1.8 |
raycont(r) /* check for clipped object and continue */ |
94 |
|
|
register RAY *r; |
95 |
|
|
{ |
96 |
|
|
if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) |
97 |
|
|
raytrans(r); |
98 |
|
|
else |
99 |
|
|
rayshade(r, r->ro->omod); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
|
103 |
greg |
1.1 |
raytrans(r) /* transmit ray as is */ |
104 |
greg |
1.8 |
register RAY *r; |
105 |
greg |
1.1 |
{ |
106 |
|
|
RAY tr; |
107 |
|
|
|
108 |
|
|
if (rayorigin(&tr, r, TRANS, 1.0) == 0) { |
109 |
|
|
VCOPY(tr.rdir, r->rdir); |
110 |
|
|
rayvalue(&tr); |
111 |
|
|
copycolor(r->rcol, tr.rcol); |
112 |
greg |
1.10 |
r->rt = r->rot + tr.rt; |
113 |
greg |
1.1 |
} |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
|
117 |
|
|
rayshade(r, mod) /* shade ray r with material mod */ |
118 |
|
|
register RAY *r; |
119 |
|
|
int mod; |
120 |
|
|
{ |
121 |
|
|
static int depth = 0; |
122 |
|
|
register OBJREC *m; |
123 |
|
|
/* check for infinite loop */ |
124 |
|
|
if (depth++ >= MAXLOOP) |
125 |
greg |
1.4 |
objerror(r->ro, USER, "possible modifier loop"); |
126 |
greg |
1.19 |
r->rt = r->rot; /* set effective ray length */ |
127 |
greg |
1.1 |
for ( ; mod != OVOID; mod = m->omod) { |
128 |
|
|
m = objptr(mod); |
129 |
greg |
1.4 |
/****** unnecessary test since modifier() is always called |
130 |
greg |
1.1 |
if (!ismodifier(m->otype)) { |
131 |
|
|
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
132 |
|
|
error(USER, errmsg); |
133 |
|
|
} |
134 |
greg |
1.4 |
******/ |
135 |
greg |
1.16 |
/* hack for irradiance calculation */ |
136 |
|
|
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
137 |
|
|
if (irr_ignore(m->otype)) { |
138 |
|
|
depth--; |
139 |
|
|
raytrans(r); |
140 |
|
|
return; |
141 |
|
|
} |
142 |
greg |
1.18 |
if (!islight(m->otype)) |
143 |
greg |
1.16 |
m = &Lamb; |
144 |
|
|
} |
145 |
greg |
1.1 |
(*ofun[m->otype].funp)(m, r); /* execute function */ |
146 |
|
|
m->lastrno = r->rno; |
147 |
|
|
if (ismaterial(m->otype)) { /* materials call raytexture */ |
148 |
|
|
depth--; |
149 |
|
|
return; /* we're done */ |
150 |
|
|
} |
151 |
|
|
} |
152 |
|
|
objerror(r->ro, USER, "material not found"); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
|
156 |
|
|
raytexture(r, mod) /* get material modifiers */ |
157 |
|
|
RAY *r; |
158 |
|
|
int mod; |
159 |
|
|
{ |
160 |
|
|
static int depth = 0; |
161 |
|
|
register OBJREC *m; |
162 |
|
|
/* check for infinite loop */ |
163 |
|
|
if (depth++ >= MAXLOOP) |
164 |
|
|
objerror(r->ro, USER, "modifier loop"); |
165 |
|
|
/* execute textures and patterns */ |
166 |
|
|
for ( ; mod != OVOID; mod = m->omod) { |
167 |
|
|
m = objptr(mod); |
168 |
|
|
if (!istexture(m->otype)) { |
169 |
|
|
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
170 |
|
|
error(USER, errmsg); |
171 |
|
|
} |
172 |
|
|
(*ofun[m->otype].funp)(m, r); |
173 |
|
|
m->lastrno = r->rno; |
174 |
|
|
} |
175 |
|
|
depth--; /* end here */ |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
|
179 |
|
|
raymixture(r, fore, back, coef) /* mix modifiers */ |
180 |
|
|
register RAY *r; |
181 |
|
|
OBJECT fore, back; |
182 |
|
|
double coef; |
183 |
|
|
{ |
184 |
|
|
FVECT curpert, forepert, backpert; |
185 |
|
|
COLOR curpcol, forepcol, backpcol; |
186 |
|
|
register int i; |
187 |
|
|
/* clip coefficient */ |
188 |
|
|
if (coef > 1.0) |
189 |
|
|
coef = 1.0; |
190 |
|
|
else if (coef < 0.0) |
191 |
|
|
coef = 0.0; |
192 |
|
|
/* save current mods */ |
193 |
|
|
VCOPY(curpert, r->pert); |
194 |
|
|
copycolor(curpcol, r->pcol); |
195 |
|
|
/* compute new mods */ |
196 |
|
|
/* foreground */ |
197 |
|
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
198 |
|
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
199 |
|
|
if (fore != OVOID && coef > FTINY) |
200 |
|
|
raytexture(r, fore); |
201 |
|
|
VCOPY(forepert, r->pert); |
202 |
|
|
copycolor(forepcol, r->pcol); |
203 |
|
|
/* background */ |
204 |
|
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
205 |
|
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
206 |
|
|
if (back != OVOID && coef < 1.0-FTINY) |
207 |
|
|
raytexture(r, back); |
208 |
|
|
VCOPY(backpert, r->pert); |
209 |
|
|
copycolor(backpcol, r->pcol); |
210 |
|
|
/* sum perturbations */ |
211 |
|
|
for (i = 0; i < 3; i++) |
212 |
|
|
r->pert[i] = curpert[i] + coef*forepert[i] + |
213 |
|
|
(1.0-coef)*backpert[i]; |
214 |
|
|
/* multiply colors */ |
215 |
|
|
setcolor(r->pcol, coef*colval(forepcol,RED) + |
216 |
|
|
(1.0-coef)*colval(backpcol,RED), |
217 |
|
|
coef*colval(forepcol,GRN) + |
218 |
|
|
(1.0-coef)*colval(backpcol,GRN), |
219 |
|
|
coef*colval(forepcol,BLU) + |
220 |
|
|
(1.0-coef)*colval(backpcol,BLU)); |
221 |
|
|
multcolor(r->pcol, curpcol); |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
|
225 |
|
|
double |
226 |
|
|
raynormal(norm, r) /* compute perturbed normal for ray */ |
227 |
|
|
FVECT norm; |
228 |
|
|
register RAY *r; |
229 |
|
|
{ |
230 |
|
|
double newdot; |
231 |
|
|
register int i; |
232 |
|
|
|
233 |
|
|
/* The perturbation is added to the surface normal to obtain |
234 |
|
|
* the new normal. If the new normal would affect the surface |
235 |
|
|
* orientation wrt. the ray, a correction is made. The method is |
236 |
|
|
* still fraught with problems since reflected rays and similar |
237 |
|
|
* directions calculated from the surface normal may spawn rays behind |
238 |
|
|
* the surface. The only solution is to curb textures at high |
239 |
greg |
1.9 |
* incidence (namely, keep DOT(rdir,pert) < Rdot). |
240 |
greg |
1.1 |
*/ |
241 |
|
|
|
242 |
|
|
for (i = 0; i < 3; i++) |
243 |
|
|
norm[i] = r->ron[i] + r->pert[i]; |
244 |
|
|
|
245 |
|
|
if (normalize(norm) == 0.0) { |
246 |
|
|
objerror(r->ro, WARNING, "illegal normal perturbation"); |
247 |
|
|
VCOPY(norm, r->ron); |
248 |
|
|
return(r->rod); |
249 |
|
|
} |
250 |
|
|
newdot = -DOT(norm, r->rdir); |
251 |
|
|
if ((newdot > 0.0) != (r->rod > 0.0)) { /* fix orientation */ |
252 |
|
|
for (i = 0; i < 3; i++) |
253 |
|
|
norm[i] += 2.0*newdot*r->rdir[i]; |
254 |
|
|
newdot = -newdot; |
255 |
|
|
} |
256 |
|
|
return(newdot); |
257 |
greg |
1.12 |
} |
258 |
|
|
|
259 |
|
|
|
260 |
|
|
newrayxf(r) /* get new tranformation matrix for ray */ |
261 |
|
|
RAY *r; |
262 |
|
|
{ |
263 |
|
|
static struct xfn { |
264 |
|
|
struct xfn *next; |
265 |
|
|
FULLXF xf; |
266 |
|
|
} xfseed = { &xfseed }, *xflast = &xfseed; |
267 |
|
|
register struct xfn *xp; |
268 |
|
|
register RAY *rp; |
269 |
|
|
|
270 |
|
|
/* |
271 |
|
|
* Search for transform in circular list that |
272 |
|
|
* has no associated ray in the tree. |
273 |
|
|
*/ |
274 |
|
|
xp = xflast; |
275 |
|
|
for (rp = r->parent; rp != NULL; rp = rp->parent) |
276 |
|
|
if (rp->rox == &xp->xf) { /* xp in use */ |
277 |
|
|
xp = xp->next; /* move to next */ |
278 |
|
|
if (xp == xflast) { /* need new one */ |
279 |
greg |
1.14 |
xp = (struct xfn *)bmalloc(sizeof(struct xfn)); |
280 |
greg |
1.12 |
if (xp == NULL) |
281 |
|
|
error(SYSTEM, |
282 |
|
|
"out of memory in newrayxf"); |
283 |
|
|
/* insert in list */ |
284 |
|
|
xp->next = xflast->next; |
285 |
|
|
xflast->next = xp; |
286 |
|
|
break; /* we're done */ |
287 |
|
|
} |
288 |
|
|
rp = r; /* start check over */ |
289 |
|
|
} |
290 |
|
|
/* got it */ |
291 |
|
|
r->rox = &xp->xf; |
292 |
|
|
xflast = xp; |
293 |
greg |
1.1 |
} |
294 |
|
|
|
295 |
|
|
|
296 |
|
|
flipsurface(r) /* reverse surface orientation */ |
297 |
|
|
register RAY *r; |
298 |
|
|
{ |
299 |
|
|
r->rod = -r->rod; |
300 |
|
|
r->ron[0] = -r->ron[0]; |
301 |
|
|
r->ron[1] = -r->ron[1]; |
302 |
|
|
r->ron[2] = -r->ron[2]; |
303 |
|
|
r->pert[0] = -r->pert[0]; |
304 |
|
|
r->pert[1] = -r->pert[1]; |
305 |
|
|
r->pert[2] = -r->pert[2]; |
306 |
|
|
} |
307 |
|
|
|
308 |
|
|
|
309 |
|
|
localhit(r, scene) /* check for hit in the octree */ |
310 |
|
|
register RAY *r; |
311 |
|
|
register CUBE *scene; |
312 |
|
|
{ |
313 |
|
|
FVECT curpos; /* current cube position */ |
314 |
greg |
1.11 |
int sflags; /* sign flags */ |
315 |
greg |
1.1 |
double t, dt; |
316 |
|
|
register int i; |
317 |
|
|
|
318 |
greg |
1.11 |
sflags = 0; |
319 |
greg |
1.1 |
for (i = 0; i < 3; i++) { |
320 |
|
|
curpos[i] = r->rorg[i]; |
321 |
|
|
if (r->rdir[i] > FTINY) |
322 |
greg |
1.11 |
sflags |= 1 << i; |
323 |
greg |
1.1 |
else if (r->rdir[i] < -FTINY) |
324 |
greg |
1.11 |
sflags |= 0x10 << i; |
325 |
greg |
1.1 |
} |
326 |
greg |
1.17 |
if (sflags == 0) |
327 |
|
|
error(CONSISTENCY, "zero ray direction in localhit"); |
328 |
greg |
1.1 |
t = 0.0; |
329 |
|
|
if (!incube(scene, curpos)) { |
330 |
|
|
/* find distance to entry */ |
331 |
|
|
for (i = 0; i < 3; i++) { |
332 |
|
|
/* plane in our direction */ |
333 |
greg |
1.11 |
if (sflags & 1<<i) |
334 |
greg |
1.1 |
dt = scene->cuorg[i]; |
335 |
greg |
1.11 |
else if (sflags & 0x10<<i) |
336 |
greg |
1.1 |
dt = scene->cuorg[i] + scene->cusize; |
337 |
|
|
else |
338 |
|
|
continue; |
339 |
|
|
/* distance to the plane */ |
340 |
|
|
dt = (dt - r->rorg[i])/r->rdir[i]; |
341 |
|
|
if (dt > t) |
342 |
|
|
t = dt; /* farthest face is the one */ |
343 |
|
|
} |
344 |
|
|
t += FTINY; /* fudge to get inside cube */ |
345 |
|
|
/* advance position */ |
346 |
|
|
for (i = 0; i < 3; i++) |
347 |
|
|
curpos[i] += r->rdir[i]*t; |
348 |
|
|
|
349 |
|
|
if (!incube(scene, curpos)) /* non-intersecting ray */ |
350 |
|
|
return(0); |
351 |
|
|
} |
352 |
greg |
1.11 |
return(raymove(curpos, sflags, r, scene) == RAYHIT); |
353 |
greg |
1.1 |
} |
354 |
|
|
|
355 |
|
|
|
356 |
|
|
static int |
357 |
greg |
1.11 |
raymove(pos, dirf, r, cu) /* check for hit as we move */ |
358 |
greg |
1.1 |
FVECT pos; /* modified */ |
359 |
greg |
1.11 |
int dirf; /* direction indicators to speed tests */ |
360 |
greg |
1.1 |
register RAY *r; |
361 |
|
|
register CUBE *cu; |
362 |
|
|
{ |
363 |
|
|
int ax; |
364 |
|
|
double dt, t; |
365 |
|
|
|
366 |
|
|
if (istree(cu->cutree)) { /* recurse on subcubes */ |
367 |
|
|
CUBE cukid; |
368 |
greg |
1.11 |
register int br, sgn; |
369 |
greg |
1.1 |
|
370 |
|
|
cukid.cusize = cu->cusize * 0.5; /* find subcube */ |
371 |
|
|
VCOPY(cukid.cuorg, cu->cuorg); |
372 |
|
|
br = 0; |
373 |
|
|
if (pos[0] >= cukid.cuorg[0]+cukid.cusize) { |
374 |
|
|
cukid.cuorg[0] += cukid.cusize; |
375 |
|
|
br |= 1; |
376 |
|
|
} |
377 |
|
|
if (pos[1] >= cukid.cuorg[1]+cukid.cusize) { |
378 |
|
|
cukid.cuorg[1] += cukid.cusize; |
379 |
|
|
br |= 2; |
380 |
|
|
} |
381 |
|
|
if (pos[2] >= cukid.cuorg[2]+cukid.cusize) { |
382 |
|
|
cukid.cuorg[2] += cukid.cusize; |
383 |
|
|
br |= 4; |
384 |
|
|
} |
385 |
|
|
for ( ; ; ) { |
386 |
|
|
cukid.cutree = octkid(cu->cutree, br); |
387 |
greg |
1.11 |
if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT) |
388 |
greg |
1.1 |
return(RAYHIT); |
389 |
|
|
sgn = 1 << ax; |
390 |
greg |
1.11 |
if (sgn & dirf) /* positive axis? */ |
391 |
greg |
1.1 |
if (sgn & br) |
392 |
|
|
return(ax); /* overflow */ |
393 |
|
|
else { |
394 |
|
|
cukid.cuorg[ax] += cukid.cusize; |
395 |
|
|
br |= sgn; |
396 |
|
|
} |
397 |
greg |
1.11 |
else |
398 |
|
|
if (sgn & br) { |
399 |
|
|
cukid.cuorg[ax] -= cukid.cusize; |
400 |
|
|
br &= ~sgn; |
401 |
|
|
} else |
402 |
|
|
return(ax); /* underflow */ |
403 |
greg |
1.1 |
} |
404 |
|
|
/*NOTREACHED*/ |
405 |
|
|
} |
406 |
|
|
if (isfull(cu->cutree) && checkhit(r, cu)) |
407 |
|
|
return(RAYHIT); |
408 |
|
|
/* advance to next cube */ |
409 |
greg |
1.11 |
if (dirf&0x11) { |
410 |
|
|
dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; |
411 |
greg |
1.1 |
t = (dt - pos[0])/r->rdir[0]; |
412 |
|
|
ax = 0; |
413 |
|
|
} else |
414 |
|
|
t = FHUGE; |
415 |
greg |
1.11 |
if (dirf&0x22) { |
416 |
|
|
dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; |
417 |
greg |
1.1 |
dt = (dt - pos[1])/r->rdir[1]; |
418 |
|
|
if (dt < t) { |
419 |
|
|
t = dt; |
420 |
|
|
ax = 1; |
421 |
|
|
} |
422 |
|
|
} |
423 |
greg |
1.11 |
if (dirf&0x44) { |
424 |
|
|
dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; |
425 |
greg |
1.1 |
dt = (dt - pos[2])/r->rdir[2]; |
426 |
|
|
if (dt < t) { |
427 |
|
|
t = dt; |
428 |
|
|
ax = 2; |
429 |
|
|
} |
430 |
|
|
} |
431 |
|
|
pos[0] += r->rdir[0]*t; |
432 |
|
|
pos[1] += r->rdir[1]*t; |
433 |
|
|
pos[2] += r->rdir[2]*t; |
434 |
|
|
return(ax); |
435 |
|
|
} |
436 |
|
|
|
437 |
|
|
|
438 |
|
|
static |
439 |
|
|
checkhit(r, cu) /* check for hit in full cube */ |
440 |
|
|
register RAY *r; |
441 |
|
|
CUBE *cu; |
442 |
|
|
{ |
443 |
|
|
OBJECT oset[MAXSET+1]; |
444 |
|
|
register OBJREC *o; |
445 |
|
|
register int i; |
446 |
|
|
|
447 |
|
|
objset(oset, cu->cutree); |
448 |
|
|
for (i = oset[0]; i > 0; i--) { |
449 |
|
|
o = objptr(oset[i]); |
450 |
|
|
if (o->lastrno == r->rno) /* checked already? */ |
451 |
|
|
continue; |
452 |
|
|
(*ofun[o->otype].funp)(o, r); |
453 |
|
|
o->lastrno = r->rno; |
454 |
|
|
} |
455 |
|
|
if (r->ro == NULL) |
456 |
|
|
return(0); /* no scores yet */ |
457 |
|
|
|
458 |
|
|
return(incube(cu, r->rop)); /* hit OK if in current cube */ |
459 |
|
|
} |