ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.2
Committed: Mon Jan 25 15:16:59 1993 UTC (31 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +30 -6 lines
Log Message:
modified OBJREC and RAY structures to avoid redundant intersection
testing using sets instead of flags

File Contents

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