1 |
– |
/* Copyright (c) 1986 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 "ray.h" |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#include "octree.h" |
13 |
< |
|
12 |
> |
#include "ray.h" |
13 |
> |
#include "source.h" |
14 |
|
#include "otypes.h" |
15 |
+ |
#include "otspecial.h" |
16 |
|
|
17 |
< |
extern CUBE thescene; /* our scene */ |
20 |
< |
extern int maxdepth; /* maximum recursion depth */ |
21 |
< |
extern double minweight; /* minimum ray weight */ |
17 |
> |
#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ |
18 |
|
|
19 |
< |
long nrays = 0L; /* number of rays traced */ |
19 |
> |
unsigned long raynum = 0; /* next unique ray number */ |
20 |
> |
unsigned long nrays = 0; /* number of calls to localhit */ |
21 |
|
|
22 |
< |
#define MAXLOOP 32 /* modifier loop detection */ |
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, |
26 |
> |
}; /* a Lambertian surface */ |
27 |
|
|
28 |
+ |
OBJREC Aftplane; /* aft clipping plane object */ |
29 |
+ |
|
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 |
|
|
36 |
< |
rayorigin(r, ro, rt, rw) /* start new ray from old one */ |
37 |
< |
register RAY *r, *ro; |
38 |
< |
int rt; |
39 |
< |
double rw; |
36 |
> |
|
37 |
> |
extern int |
38 |
> |
rayorigin( /* start new ray from old one */ |
39 |
> |
register RAY *r, |
40 |
> |
register RAY *ro, |
41 |
> |
int rt, |
42 |
> |
double rw |
43 |
> |
) |
44 |
|
{ |
45 |
+ |
double re; |
46 |
+ |
|
47 |
|
if ((r->parent = ro) == NULL) { /* primary ray */ |
48 |
|
r->rlvl = 0; |
49 |
|
r->rweight = rw; |
50 |
|
r->crtype = r->rtype = rt; |
51 |
|
r->rsrc = -1; |
52 |
|
r->clipset = NULL; |
53 |
+ |
r->revf = raytrace; |
54 |
+ |
copycolor(r->cext, cextinction); |
55 |
+ |
copycolor(r->albedo, salbedo); |
56 |
+ |
r->gecc = seccg; |
57 |
+ |
r->slights = NULL; |
58 |
|
} else { /* spawned ray */ |
59 |
|
r->rlvl = ro->rlvl; |
60 |
|
if (rt & RAYREFL) { |
61 |
|
r->rlvl++; |
62 |
|
r->rsrc = -1; |
63 |
|
r->clipset = ro->clipset; |
64 |
+ |
r->rmax = 0.0; |
65 |
|
} else { |
66 |
|
r->rsrc = ro->rsrc; |
67 |
|
r->clipset = ro->newcset; |
68 |
+ |
r->rmax = ro->rmax <= FTINY ? 0.0 : ro->rmax - ro->rot; |
69 |
|
} |
70 |
< |
r->rweight = ro->rweight * rw; |
70 |
> |
r->revf = ro->revf; |
71 |
> |
copycolor(r->cext, ro->cext); |
72 |
> |
copycolor(r->albedo, ro->albedo); |
73 |
> |
r->gecc = ro->gecc; |
74 |
> |
r->slights = ro->slights; |
75 |
|
r->crtype = ro->crtype | (r->rtype = rt); |
76 |
|
VCOPY(r->rorg, ro->rop); |
77 |
+ |
r->rweight = ro->rweight * rw; |
78 |
+ |
/* estimate absorption */ |
79 |
+ |
re = colval(ro->cext,RED) < colval(ro->cext,GRN) ? |
80 |
+ |
colval(ro->cext,RED) : colval(ro->cext,GRN); |
81 |
+ |
if (colval(ro->cext,BLU) < re) re = colval(ro->cext,BLU); |
82 |
+ |
if (re > 0.) |
83 |
+ |
r->rweight *= exp(-re*ro->rot); |
84 |
|
} |
85 |
< |
r->rno = nrays; |
85 |
> |
rayclear(r); |
86 |
> |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
87 |
> |
} |
88 |
> |
|
89 |
> |
|
90 |
> |
extern void |
91 |
> |
rayclear( /* clear a ray for (re)evaluation */ |
92 |
> |
register RAY *r |
93 |
> |
) |
94 |
> |
{ |
95 |
> |
r->rno = raynum++; |
96 |
|
r->newcset = r->clipset; |
97 |
+ |
r->hitf = rayhit; |
98 |
+ |
r->robj = OVOID; |
99 |
|
r->ro = NULL; |
100 |
< |
r->rot = FHUGE; |
101 |
< |
r->ros = 1.0; setident4(r->rox); |
100 |
> |
r->rox = NULL; |
101 |
> |
r->rt = r->rot = FHUGE; |
102 |
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
103 |
+ |
r->uv[0] = r->uv[1] = 0.0; |
104 |
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
105 |
|
setcolor(r->rcol, 0.0, 0.0, 0.0); |
63 |
– |
return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1); |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
< |
rayvalue(r) /* compute a ray's value */ |
110 |
< |
register RAY *r; |
109 |
> |
extern void |
110 |
> |
raytrace( /* trace a ray and compute its value */ |
111 |
> |
RAY *r |
112 |
> |
) |
113 |
|
{ |
70 |
– |
extern int (*trace)(); |
71 |
– |
|
114 |
|
if (localhit(r, &thescene)) |
115 |
< |
if (r->clipset != NULL && inset(r->clipset, r->ro->omod)) |
116 |
< |
raytrans(r); /* object is clipped */ |
117 |
< |
else |
118 |
< |
rayshade(r, r->ro->omod); |
119 |
< |
else if (sourcehit(r)) |
120 |
< |
rayshade(r, r->ro->omod); |
115 |
> |
raycont(r); /* hit local surface, evaluate */ |
116 |
> |
else if (r->ro == &Aftplane) { |
117 |
> |
r->ro = NULL; /* hit aft clipping plane */ |
118 |
> |
r->rot = FHUGE; |
119 |
> |
} else if (sourcehit(r)) |
120 |
> |
rayshade(r, r->ro->omod); /* distant source */ |
121 |
|
|
122 |
+ |
rayparticipate(r); /* for participating medium */ |
123 |
+ |
|
124 |
|
if (trace != NULL) |
125 |
|
(*trace)(r); /* trace execution */ |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
< |
raytrans(r) /* transmit ray as is */ |
130 |
< |
RAY *r; |
129 |
> |
extern void |
130 |
> |
raycont( /* check for clipped object and continue */ |
131 |
> |
register RAY *r |
132 |
> |
) |
133 |
|
{ |
134 |
+ |
if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || |
135 |
+ |
!rayshade(r, r->ro->omod)) |
136 |
+ |
raytrans(r); |
137 |
+ |
} |
138 |
+ |
|
139 |
+ |
|
140 |
+ |
extern void |
141 |
+ |
raytrans( /* transmit ray as is */ |
142 |
+ |
register RAY *r |
143 |
+ |
) |
144 |
+ |
{ |
145 |
|
RAY tr; |
146 |
|
|
147 |
|
if (rayorigin(&tr, r, TRANS, 1.0) == 0) { |
148 |
|
VCOPY(tr.rdir, r->rdir); |
149 |
|
rayvalue(&tr); |
150 |
|
copycolor(r->rcol, tr.rcol); |
151 |
+ |
r->rt = r->rot + tr.rt; |
152 |
|
} |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
< |
rayshade(r, mod) /* shade ray r with material mod */ |
157 |
< |
register RAY *r; |
158 |
< |
int mod; |
156 |
> |
extern int |
157 |
> |
rayshade( /* shade ray r with material mod */ |
158 |
> |
register RAY *r, |
159 |
> |
int mod |
160 |
> |
) |
161 |
|
{ |
162 |
< |
static int depth = 0; |
162 |
> |
int gotmat; |
163 |
|
register OBJREC *m; |
164 |
< |
/* check for infinite loop */ |
165 |
< |
if (depth++ >= MAXLOOP) |
106 |
< |
objerror(r->ro, USER, "material loop"); |
107 |
< |
for ( ; mod != OVOID; mod = m->omod) { |
164 |
> |
r->rt = r->rot; /* set effective ray length */ |
165 |
> |
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { |
166 |
|
m = objptr(mod); |
167 |
+ |
/****** unnecessary test since modifier() is always called |
168 |
|
if (!ismodifier(m->otype)) { |
169 |
|
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
170 |
|
error(USER, errmsg); |
171 |
|
} |
172 |
< |
(*ofun[m->otype].funp)(m, r); /* execute function */ |
173 |
< |
m->lastrno = r->rno; |
174 |
< |
if (ismaterial(m->otype)) { /* materials call raytexture */ |
175 |
< |
depth--; |
176 |
< |
return; /* we're done */ |
172 |
> |
******/ |
173 |
> |
/* hack for irradiance calculation */ |
174 |
> |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) && |
175 |
> |
m->otype != MAT_CLIP && |
176 |
> |
(ofun[m->otype].flags & (T_M|T_X))) { |
177 |
> |
if (irr_ignore(m->otype)) { |
178 |
> |
raytrans(r); |
179 |
> |
return(1); |
180 |
> |
} |
181 |
> |
if (!islight(m->otype)) |
182 |
> |
m = &Lamb; |
183 |
|
} |
184 |
+ |
/* materials call raytexture */ |
185 |
+ |
gotmat = (*ofun[m->otype].funp)(m, r); |
186 |
|
} |
187 |
< |
objerror(r->ro, USER, "material not found"); |
187 |
> |
return(gotmat); |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
< |
raytexture(r, mod) /* get material modifiers */ |
192 |
< |
RAY *r; |
193 |
< |
int mod; |
191 |
> |
extern void |
192 |
> |
rayparticipate( /* compute ray medium participation */ |
193 |
> |
register RAY *r |
194 |
> |
) |
195 |
|
{ |
196 |
< |
static int depth = 0; |
196 |
> |
COLOR ce, ca; |
197 |
> |
double re, ge, be; |
198 |
> |
|
199 |
> |
if (intens(r->cext) <= 1./FHUGE) |
200 |
> |
return; /* no medium */ |
201 |
> |
re = r->rot*colval(r->cext,RED); |
202 |
> |
ge = r->rot*colval(r->cext,GRN); |
203 |
> |
be = r->rot*colval(r->cext,BLU); |
204 |
> |
if (r->crtype & SHADOW) { /* no scattering for sources */ |
205 |
> |
re *= 1. - colval(r->albedo,RED); |
206 |
> |
ge *= 1. - colval(r->albedo,GRN); |
207 |
> |
be *= 1. - colval(r->albedo,BLU); |
208 |
> |
} |
209 |
> |
setcolor(ce, re<=0. ? 1. : re>92. ? 0. : exp(-re), |
210 |
> |
ge<=0. ? 1. : ge>92. ? 0. : exp(-ge), |
211 |
> |
be<=0. ? 1. : be>92. ? 0. : exp(-be)); |
212 |
> |
multcolor(r->rcol, ce); /* path absorption */ |
213 |
> |
if (r->crtype & SHADOW || intens(r->albedo) <= FTINY) |
214 |
> |
return; /* no scattering */ |
215 |
> |
setcolor(ca, |
216 |
> |
colval(r->albedo,RED)*colval(ambval,RED)*(1.-colval(ce,RED)), |
217 |
> |
colval(r->albedo,GRN)*colval(ambval,GRN)*(1.-colval(ce,GRN)), |
218 |
> |
colval(r->albedo,BLU)*colval(ambval,BLU)*(1.-colval(ce,BLU))); |
219 |
> |
addcolor(r->rcol, ca); /* ambient in scattering */ |
220 |
> |
srcscatter(r); /* source in scattering */ |
221 |
> |
} |
222 |
> |
|
223 |
> |
|
224 |
> |
extern void |
225 |
> |
raytexture( /* get material modifiers */ |
226 |
> |
RAY *r, |
227 |
> |
OBJECT mod |
228 |
> |
) |
229 |
> |
{ |
230 |
|
register OBJREC *m; |
130 |
– |
/* check for infinite loop */ |
131 |
– |
if (depth++ >= MAXLOOP) |
132 |
– |
objerror(r->ro, USER, "modifier loop"); |
231 |
|
/* execute textures and patterns */ |
232 |
|
for ( ; mod != OVOID; mod = m->omod) { |
233 |
|
m = objptr(mod); |
234 |
< |
if (!istexture(m->otype)) { |
234 |
> |
/****** unnecessary test since modifier() is always called |
235 |
> |
if (!ismodifier(m->otype)) { |
236 |
|
sprintf(errmsg, "illegal modifier \"%s\"", m->oname); |
237 |
|
error(USER, errmsg); |
238 |
|
} |
239 |
< |
(*ofun[m->otype].funp)(m, r); |
240 |
< |
m->lastrno = r->rno; |
239 |
> |
******/ |
240 |
> |
if ((*ofun[m->otype].funp)(m, r)) { |
241 |
> |
sprintf(errmsg, "conflicting material \"%s\"", |
242 |
> |
m->oname); |
243 |
> |
objerror(r->ro, USER, errmsg); |
244 |
> |
} |
245 |
|
} |
143 |
– |
depth--; /* end here */ |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
< |
raymixture(r, fore, back, coef) /* mix modifiers */ |
250 |
< |
register RAY *r; |
251 |
< |
OBJECT fore, back; |
252 |
< |
double coef; |
249 |
> |
extern int |
250 |
> |
raymixture( /* mix modifiers */ |
251 |
> |
register RAY *r, |
252 |
> |
OBJECT fore, |
253 |
> |
OBJECT back, |
254 |
> |
double coef |
255 |
> |
) |
256 |
|
{ |
257 |
< |
FVECT curpert, forepert, backpert; |
258 |
< |
COLOR curpcol, forepcol, backpcol; |
257 |
> |
RAY fr, br; |
258 |
> |
int foremat, backmat; |
259 |
|
register int i; |
260 |
< |
/* clip coefficient */ |
260 |
> |
/* bound coefficient */ |
261 |
|
if (coef > 1.0) |
262 |
|
coef = 1.0; |
263 |
|
else if (coef < 0.0) |
264 |
|
coef = 0.0; |
265 |
< |
/* save current mods */ |
266 |
< |
VCOPY(curpert, r->pert); |
267 |
< |
copycolor(curpcol, r->pcol); |
268 |
< |
/* compute new mods */ |
269 |
< |
/* foreground */ |
270 |
< |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
271 |
< |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
272 |
< |
if (fore != OVOID && coef > FTINY) |
273 |
< |
raytexture(r, fore); |
274 |
< |
VCOPY(forepert, r->pert); |
275 |
< |
copycolor(forepcol, r->pcol); |
276 |
< |
/* background */ |
277 |
< |
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
278 |
< |
setcolor(r->pcol, 1.0, 1.0, 1.0); |
279 |
< |
if (back != OVOID && coef < 1.0-FTINY) |
280 |
< |
raytexture(r, back); |
281 |
< |
VCOPY(backpert, r->pert); |
282 |
< |
copycolor(backpcol, r->pcol); |
178 |
< |
/* sum perturbations */ |
265 |
> |
/* compute foreground and background */ |
266 |
> |
foremat = backmat = 0; |
267 |
> |
/* foreground */ |
268 |
> |
fr = *r; |
269 |
> |
if (coef > FTINY) |
270 |
> |
foremat = rayshade(&fr, fore); |
271 |
> |
/* background */ |
272 |
> |
br = *r; |
273 |
> |
if (coef < 1.0-FTINY) |
274 |
> |
backmat = rayshade(&br, back); |
275 |
> |
/* check for transparency */ |
276 |
> |
if (backmat ^ foremat) { |
277 |
> |
if (backmat && coef > FTINY) |
278 |
> |
raytrans(&fr); |
279 |
> |
else if (foremat && coef < 1.0-FTINY) |
280 |
> |
raytrans(&br); |
281 |
> |
} |
282 |
> |
/* mix perturbations */ |
283 |
|
for (i = 0; i < 3; i++) |
284 |
< |
r->pert[i] = curpert[i] + coef*forepert[i] + |
285 |
< |
(1.0-coef)*backpert[i]; |
286 |
< |
/* multiply colors */ |
287 |
< |
setcolor(r->pcol, coef*colval(forepcol,RED) + |
288 |
< |
(1.0-coef)*colval(backpcol,RED), |
289 |
< |
coef*colval(forepcol,GRN) + |
290 |
< |
(1.0-coef)*colval(backpcol,GRN), |
291 |
< |
coef*colval(forepcol,BLU) + |
292 |
< |
(1.0-coef)*colval(backpcol,BLU)); |
293 |
< |
multcolor(r->pcol, curpcol); |
284 |
> |
r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; |
285 |
> |
/* mix pattern colors */ |
286 |
> |
scalecolor(fr.pcol, coef); |
287 |
> |
scalecolor(br.pcol, 1.0-coef); |
288 |
> |
copycolor(r->pcol, fr.pcol); |
289 |
> |
addcolor(r->pcol, br.pcol); |
290 |
> |
/* return value tells if material */ |
291 |
> |
if (!foremat & !backmat) |
292 |
> |
return(0); |
293 |
> |
/* mix returned ray values */ |
294 |
> |
scalecolor(fr.rcol, coef); |
295 |
> |
scalecolor(br.rcol, 1.0-coef); |
296 |
> |
copycolor(r->rcol, fr.rcol); |
297 |
> |
addcolor(r->rcol, br.rcol); |
298 |
> |
r->rt = bright(fr.rcol) > bright(br.rcol) ? fr.rt : br.rt; |
299 |
> |
return(1); |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
< |
double |
304 |
< |
raynormal(norm, r) /* compute perturbed normal for ray */ |
305 |
< |
FVECT norm; |
306 |
< |
register RAY *r; |
303 |
> |
extern double |
304 |
> |
raydist( /* compute (cumulative) ray distance */ |
305 |
> |
register RAY *r, |
306 |
> |
register int flags |
307 |
> |
) |
308 |
|
{ |
309 |
+ |
double sum = 0.0; |
310 |
+ |
|
311 |
+ |
while (r != NULL && r->crtype&flags) { |
312 |
+ |
sum += r->rot; |
313 |
+ |
r = r->parent; |
314 |
+ |
} |
315 |
+ |
return(sum); |
316 |
+ |
} |
317 |
+ |
|
318 |
+ |
|
319 |
+ |
extern double |
320 |
+ |
raynormal( /* compute perturbed normal for ray */ |
321 |
+ |
FVECT norm, |
322 |
+ |
register RAY *r |
323 |
+ |
) |
324 |
+ |
{ |
325 |
|
double newdot; |
326 |
|
register int i; |
327 |
|
|
331 |
|
* still fraught with problems since reflected rays and similar |
332 |
|
* directions calculated from the surface normal may spawn rays behind |
333 |
|
* the surface. The only solution is to curb textures at high |
334 |
< |
* incidence (Rdot << 1). |
334 |
> |
* incidence (namely, keep DOT(rdir,pert) < Rdot). |
335 |
|
*/ |
336 |
|
|
337 |
|
for (i = 0; i < 3; i++) |
352 |
|
} |
353 |
|
|
354 |
|
|
355 |
< |
flipsurface(r) /* reverse surface orientation */ |
356 |
< |
register RAY *r; |
355 |
> |
extern void |
356 |
> |
newrayxf( /* get new tranformation matrix for ray */ |
357 |
> |
RAY *r |
358 |
> |
) |
359 |
|
{ |
360 |
+ |
static struct xfn { |
361 |
+ |
struct xfn *next; |
362 |
+ |
FULLXF xf; |
363 |
+ |
} xfseed = { &xfseed }, *xflast = &xfseed; |
364 |
+ |
register struct xfn *xp; |
365 |
+ |
register RAY *rp; |
366 |
+ |
|
367 |
+ |
/* |
368 |
+ |
* Search for transform in circular list that |
369 |
+ |
* has no associated ray in the tree. |
370 |
+ |
*/ |
371 |
+ |
xp = xflast; |
372 |
+ |
for (rp = r->parent; rp != NULL; rp = rp->parent) |
373 |
+ |
if (rp->rox == &xp->xf) { /* xp in use */ |
374 |
+ |
xp = xp->next; /* move to next */ |
375 |
+ |
if (xp == xflast) { /* need new one */ |
376 |
+ |
xp = (struct xfn *)malloc(sizeof(struct xfn)); |
377 |
+ |
if (xp == NULL) |
378 |
+ |
error(SYSTEM, |
379 |
+ |
"out of memory in newrayxf"); |
380 |
+ |
/* insert in list */ |
381 |
+ |
xp->next = xflast->next; |
382 |
+ |
xflast->next = xp; |
383 |
+ |
break; /* we're done */ |
384 |
+ |
} |
385 |
+ |
rp = r; /* start check over */ |
386 |
+ |
} |
387 |
+ |
/* got it */ |
388 |
+ |
r->rox = &xp->xf; |
389 |
+ |
xflast = xp; |
390 |
+ |
} |
391 |
+ |
|
392 |
+ |
|
393 |
+ |
extern void |
394 |
+ |
flipsurface( /* reverse surface orientation */ |
395 |
+ |
register RAY *r |
396 |
+ |
) |
397 |
+ |
{ |
398 |
|
r->rod = -r->rod; |
399 |
|
r->ron[0] = -r->ron[0]; |
400 |
|
r->ron[1] = -r->ron[1]; |
405 |
|
} |
406 |
|
|
407 |
|
|
408 |
< |
localhit(r, scene) /* check for hit in the octree */ |
409 |
< |
register RAY *r; |
410 |
< |
register CUBE *scene; |
408 |
> |
extern void |
409 |
> |
rayhit( /* standard ray hit test */ |
410 |
> |
OBJECT *oset, |
411 |
> |
RAY *r |
412 |
> |
) |
413 |
|
{ |
414 |
+ |
OBJREC *o; |
415 |
+ |
int i; |
416 |
+ |
|
417 |
+ |
for (i = oset[0]; i > 0; i--) { |
418 |
+ |
o = objptr(oset[i]); |
419 |
+ |
if ((*ofun[o->otype].funp)(o, r)) |
420 |
+ |
r->robj = oset[i]; |
421 |
+ |
} |
422 |
+ |
} |
423 |
+ |
|
424 |
+ |
|
425 |
+ |
extern int |
426 |
+ |
localhit( /* check for hit in the octree */ |
427 |
+ |
register RAY *r, |
428 |
+ |
register CUBE *scene |
429 |
+ |
) |
430 |
+ |
{ |
431 |
+ |
OBJECT cxset[MAXCSET+1]; /* set of checked objects */ |
432 |
|
FVECT curpos; /* current cube position */ |
433 |
< |
int mpos, mneg; /* sign flags */ |
433 |
> |
int sflags; /* sign flags */ |
434 |
|
double t, dt; |
435 |
|
register int i; |
436 |
|
|
437 |
|
nrays++; /* increment trace counter */ |
438 |
< |
|
252 |
< |
mpos = mneg = 0; |
438 |
> |
sflags = 0; |
439 |
|
for (i = 0; i < 3; i++) { |
440 |
|
curpos[i] = r->rorg[i]; |
441 |
< |
if (r->rdir[i] > FTINY) |
442 |
< |
mpos |= 1 << i; |
443 |
< |
else if (r->rdir[i] < -FTINY) |
444 |
< |
mneg |= 1 << i; |
441 |
> |
if (r->rdir[i] > 1e-7) |
442 |
> |
sflags |= 1 << i; |
443 |
> |
else if (r->rdir[i] < -1e-7) |
444 |
> |
sflags |= 0x10 << i; |
445 |
|
} |
446 |
+ |
if (sflags == 0) |
447 |
+ |
error(CONSISTENCY, "zero ray direction in localhit"); |
448 |
+ |
/* start off assuming nothing hit */ |
449 |
+ |
if (r->rmax > FTINY) { /* except aft plane if one */ |
450 |
+ |
r->ro = &Aftplane; |
451 |
+ |
r->rot = r->rmax; |
452 |
+ |
for (i = 0; i < 3; i++) |
453 |
+ |
r->rop[i] = r->rorg[i] + r->rot*r->rdir[i]; |
454 |
+ |
} |
455 |
+ |
/* find global cube entrance point */ |
456 |
|
t = 0.0; |
457 |
|
if (!incube(scene, curpos)) { |
458 |
|
/* find distance to entry */ |
459 |
|
for (i = 0; i < 3; i++) { |
460 |
|
/* plane in our direction */ |
461 |
< |
if (mpos & 1<<i) |
461 |
> |
if (sflags & 1<<i) |
462 |
|
dt = scene->cuorg[i]; |
463 |
< |
else if (mneg & 1<<i) |
463 |
> |
else if (sflags & 0x10<<i) |
464 |
|
dt = scene->cuorg[i] + scene->cusize; |
465 |
|
else |
466 |
|
continue; |
470 |
|
t = dt; /* farthest face is the one */ |
471 |
|
} |
472 |
|
t += FTINY; /* fudge to get inside cube */ |
473 |
+ |
if (t >= r->rot) /* clipped already */ |
474 |
+ |
return(0); |
475 |
|
/* advance position */ |
476 |
|
for (i = 0; i < 3; i++) |
477 |
|
curpos[i] += r->rdir[i]*t; |
479 |
|
if (!incube(scene, curpos)) /* non-intersecting ray */ |
480 |
|
return(0); |
481 |
|
} |
482 |
< |
return(raymove(curpos, mpos, mneg, r, scene) == RAYHIT); |
482 |
> |
cxset[0] = 0; |
483 |
> |
raymove(curpos, cxset, sflags, r, scene); |
484 |
> |
return((r->ro != NULL) & (r->ro != &Aftplane)); |
485 |
|
} |
486 |
|
|
487 |
|
|
488 |
|
static int |
489 |
< |
raymove(pos, plus, minus, r, cu) /* check for hit as we move */ |
490 |
< |
FVECT pos; /* modified */ |
491 |
< |
int plus, minus; /* direction indicators to speed tests */ |
492 |
< |
register RAY *r; |
493 |
< |
register CUBE *cu; |
489 |
> |
raymove( /* check for hit as we move */ |
490 |
> |
FVECT pos, /* current position, modified herein */ |
491 |
> |
OBJECT *cxs, /* checked objects, modified by checkhit */ |
492 |
> |
int dirf, /* direction indicators to speed tests */ |
493 |
> |
register RAY *r, |
494 |
> |
register CUBE *cu |
495 |
> |
) |
496 |
|
{ |
497 |
|
int ax; |
498 |
|
double dt, t; |
297 |
– |
register int sgn; |
499 |
|
|
500 |
|
if (istree(cu->cutree)) { /* recurse on subcubes */ |
501 |
|
CUBE cukid; |
502 |
< |
register int br; |
502 |
> |
register int br, sgn; |
503 |
|
|
504 |
|
cukid.cusize = cu->cusize * 0.5; /* find subcube */ |
505 |
|
VCOPY(cukid.cuorg, cu->cuorg); |
518 |
|
} |
519 |
|
for ( ; ; ) { |
520 |
|
cukid.cutree = octkid(cu->cutree, br); |
521 |
< |
if ((ax = raymove(pos,plus,minus,r,&cukid)) == RAYHIT) |
521 |
> |
if ((ax = raymove(pos,cxs,dirf,r,&cukid)) == RAYHIT) |
522 |
|
return(RAYHIT); |
523 |
|
sgn = 1 << ax; |
524 |
< |
if (sgn & minus) /* negative axis? */ |
324 |
< |
if (sgn & br) { |
325 |
< |
cukid.cuorg[ax] -= cukid.cusize; |
326 |
< |
br &= ~sgn; |
327 |
< |
} else |
328 |
< |
return(ax); /* underflow */ |
329 |
< |
else |
524 |
> |
if (sgn & dirf) /* positive axis? */ |
525 |
|
if (sgn & br) |
526 |
|
return(ax); /* overflow */ |
527 |
|
else { |
528 |
|
cukid.cuorg[ax] += cukid.cusize; |
529 |
|
br |= sgn; |
530 |
|
} |
531 |
+ |
else |
532 |
+ |
if (sgn & br) { |
533 |
+ |
cukid.cuorg[ax] -= cukid.cusize; |
534 |
+ |
br &= ~sgn; |
535 |
+ |
} else |
536 |
+ |
return(ax); /* underflow */ |
537 |
|
} |
538 |
|
/*NOTREACHED*/ |
539 |
|
} |
540 |
< |
if (isfull(cu->cutree) && checkhit(r, cu)) |
540 |
> |
if (isfull(cu->cutree)) { |
541 |
> |
if (checkhit(r, cu, cxs)) |
542 |
> |
return(RAYHIT); |
543 |
> |
} else if (r->ro == &Aftplane && incube(cu, r->rop)) |
544 |
|
return(RAYHIT); |
545 |
|
/* advance to next cube */ |
546 |
< |
sgn = plus | minus; |
547 |
< |
if (sgn&1) { |
344 |
< |
dt = plus&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; |
546 |
> |
if (dirf&0x11) { |
547 |
> |
dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0]; |
548 |
|
t = (dt - pos[0])/r->rdir[0]; |
549 |
|
ax = 0; |
550 |
|
} else |
551 |
|
t = FHUGE; |
552 |
< |
if (sgn&2) { |
553 |
< |
dt = plus&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; |
552 |
> |
if (dirf&0x22) { |
553 |
> |
dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1]; |
554 |
|
dt = (dt - pos[1])/r->rdir[1]; |
555 |
|
if (dt < t) { |
556 |
|
t = dt; |
557 |
|
ax = 1; |
558 |
|
} |
559 |
|
} |
560 |
< |
if (sgn&4) { |
561 |
< |
dt = plus&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; |
560 |
> |
if (dirf&0x44) { |
561 |
> |
dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2]; |
562 |
|
dt = (dt - pos[2])/r->rdir[2]; |
563 |
|
if (dt < t) { |
564 |
|
t = dt; |
572 |
|
} |
573 |
|
|
574 |
|
|
575 |
< |
static |
576 |
< |
checkhit(r, cu) /* check for hit in full cube */ |
577 |
< |
register RAY *r; |
578 |
< |
CUBE *cu; |
575 |
> |
static int |
576 |
> |
checkhit( /* check for hit in full cube */ |
577 |
> |
register RAY *r, |
578 |
> |
CUBE *cu, |
579 |
> |
OBJECT *cxs |
580 |
> |
) |
581 |
|
{ |
582 |
|
OBJECT oset[MAXSET+1]; |
378 |
– |
register OBJREC *o; |
379 |
– |
register int i; |
583 |
|
|
584 |
|
objset(oset, cu->cutree); |
585 |
< |
for (i = oset[0]; i > 0; i--) { |
586 |
< |
o = objptr(oset[i]); |
587 |
< |
if (o->lastrno == r->rno) /* checked already? */ |
588 |
< |
continue; |
589 |
< |
(*ofun[o->otype].funp)(o, r); |
387 |
< |
o->lastrno = r->rno; |
388 |
< |
} |
389 |
< |
if (r->ro == NULL) |
585 |
> |
checkset(oset, cxs); /* avoid double-checking */ |
586 |
> |
|
587 |
> |
(*r->hitf)(oset, r); /* test for hit in set */ |
588 |
> |
|
589 |
> |
if (r->robj == OVOID) |
590 |
|
return(0); /* no scores yet */ |
591 |
|
|
592 |
|
return(incube(cu, r->rop)); /* hit OK if in current cube */ |
593 |
+ |
} |
594 |
+ |
|
595 |
+ |
|
596 |
+ |
static void |
597 |
+ |
checkset( /* modify checked set and set to check */ |
598 |
+ |
register OBJECT *os, /* os' = os - cs */ |
599 |
+ |
register OBJECT *cs /* cs' = cs + os */ |
600 |
+ |
) |
601 |
+ |
{ |
602 |
+ |
OBJECT cset[MAXCSET+MAXSET+1]; |
603 |
+ |
register int i, j; |
604 |
+ |
int k; |
605 |
+ |
/* copy os in place, cset <- cs */ |
606 |
+ |
cset[0] = 0; |
607 |
+ |
k = 0; |
608 |
+ |
for (i = j = 1; i <= os[0]; i++) { |
609 |
+ |
while (j <= cs[0] && cs[j] < os[i]) |
610 |
+ |
cset[++cset[0]] = cs[j++]; |
611 |
+ |
if (j > cs[0] || os[i] != cs[j]) { /* object to check */ |
612 |
+ |
os[++k] = os[i]; |
613 |
+ |
cset[++cset[0]] = os[i]; |
614 |
+ |
} |
615 |
+ |
} |
616 |
+ |
if (!(os[0] = k)) /* new "to check" set size */ |
617 |
+ |
return; /* special case */ |
618 |
+ |
while (j <= cs[0]) /* get the rest of cs */ |
619 |
+ |
cset[++cset[0]] = cs[j++]; |
620 |
+ |
if (cset[0] > MAXCSET) /* truncate "checked" set if nec. */ |
621 |
+ |
cset[0] = MAXCSET; |
622 |
+ |
/* setcopy(cs, cset); */ /* copy cset back to cs */ |
623 |
+ |
os = cset; |
624 |
+ |
for (i = os[0]; i-- >= 0; ) |
625 |
+ |
*cs++ = *os++; |
626 |
|
} |