ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.23
Committed: Fri Dec 8 18:49:09 1995 UTC (28 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.22: +51 -3 lines
Log Message:
added M_MIST (mist) material and global participating medium

File Contents

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