ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.45
Committed: Tue Mar 30 16:13:01 2004 UTC (20 years ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6, rad3R6P1
Changes since 2.44: +94 -74 lines
Log Message:
Continued ANSIfication. There are only bits and pieces left now.

File Contents

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