ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.28
Committed: Fri Mar 7 16:58:54 1997 UTC (27 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.27: +3 -1 lines
Log Message:
added object number to ray structure for instance tracking

File Contents

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