ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.56
Committed: Mon Aug 22 21:54:41 2005 UTC (18 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2
Changes since 2.55: +2 -2 lines
Log Message:
Fixed another serious bug where raytrans() wasn't working with RR ray term.

File Contents

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