ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.43
Committed: Tue Sep 9 03:28:43 2003 UTC (20 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.42: +2 -1 lines
Log Message:
Bug fix for -i option using antimatter.

File Contents

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