ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.35
Committed: Tue Feb 25 02:47:23 2003 UTC (21 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.34: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

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