ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.26
Committed: Wed Apr 17 14:06:35 1996 UTC (28 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.25: +16 -14 lines
Log Message:
changed albedo to 3-color parameter

File Contents

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