ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 1.15
Committed: Wed May 1 11:17:01 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.14: +20 -1 lines
Log Message:
added -i option for irradiance calculation

File Contents

# User Rev Content
1 greg 1.12 /* Copyright (c) 1990 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 1.1 extern CUBE thescene; /* our scene */
22     extern int maxdepth; /* maximum recursion depth */
23     extern double minweight; /* minimum ray weight */
24 greg 1.15 extern int do_irrad; /* compute irradiance? */
25 greg 1.1
26     long nrays = 0L; /* number of rays traced */
27    
28 greg 1.15 static double Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
29     OBJREC Lamb = {
30     OVOID, MAT_PLASTIC, "Lambertian",
31     {0, 5, NULL, Lambfa}, NULL, -1,
32     }; /* a Lambertian surface */
33    
34 greg 1.6 #define MAXLOOP 128 /* modifier loop detection */
35 greg 1.1
36     #define RAYHIT (-1) /* return value for intercepted ray */
37    
38    
39     rayorigin(r, ro, rt, rw) /* start new ray from old one */
40     register RAY *r, *ro;
41     int rt;
42     double rw;
43     {
44     if ((r->parent = ro) == NULL) { /* primary ray */
45     r->rlvl = 0;
46     r->rweight = rw;
47     r->crtype = r->rtype = rt;
48     r->rsrc = -1;
49     r->clipset = NULL;
50     } else { /* spawned ray */
51     r->rlvl = ro->rlvl;
52     if (rt & RAYREFL) {
53     r->rlvl++;
54     r->rsrc = -1;
55     r->clipset = ro->clipset;
56     } else {
57     r->rsrc = ro->rsrc;
58     r->clipset = ro->newcset;
59     }
60     r->rweight = ro->rweight * rw;
61     r->crtype = ro->crtype | (r->rtype = rt);
62     VCOPY(r->rorg, ro->rop);
63     }
64     r->rno = nrays;
65     r->newcset = r->clipset;
66     r->ro = NULL;
67     r->rot = FHUGE;
68     r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
69     setcolor(r->pcol, 1.0, 1.0, 1.0);
70     setcolor(r->rcol, 0.0, 0.0, 0.0);
71 greg 1.10 r->rt = 0.0;
72 greg 1.1 return(r->rlvl <= maxdepth && r->rweight >= minweight ? 0 : -1);
73     }
74    
75    
76     rayvalue(r) /* compute a ray's value */
77 greg 1.8 RAY *r;
78 greg 1.1 {
79     extern int (*trace)();
80    
81 greg 1.15 if (localhit(r, &thescene))
82 greg 1.8 raycont(r);
83 greg 1.15 else if (sourcehit(r))
84     rayshade(r, r->ro->omod);
85 greg 1.1
86     if (trace != NULL)
87     (*trace)(r); /* trace execution */
88     }
89    
90    
91 greg 1.8 raycont(r) /* check for clipped object and continue */
92     register RAY *r;
93     {
94     if (r->clipset != NULL && inset(r->clipset, r->ro->omod))
95     raytrans(r);
96     else
97     rayshade(r, r->ro->omod);
98     }
99    
100    
101 greg 1.1 raytrans(r) /* transmit ray as is */
102 greg 1.8 register RAY *r;
103 greg 1.1 {
104     RAY tr;
105    
106     if (rayorigin(&tr, r, TRANS, 1.0) == 0) {
107     VCOPY(tr.rdir, r->rdir);
108     rayvalue(&tr);
109     copycolor(r->rcol, tr.rcol);
110 greg 1.10 r->rt = r->rot + tr.rt;
111 greg 1.1 }
112     }
113    
114    
115     rayshade(r, mod) /* shade ray r with material mod */
116     register RAY *r;
117     int mod;
118     {
119     static int depth = 0;
120     register OBJREC *m;
121 greg 1.15 /* check for irradiance calc. */
122     if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
123     if (irr_ignore(objptr(mod)->otype))
124     raytrans(r);
125     else
126     (*ofun[Lamb.otype].funp)(&Lamb, r);
127     return;
128     }
129 greg 1.1 /* check for infinite loop */
130     if (depth++ >= MAXLOOP)
131 greg 1.4 objerror(r->ro, USER, "possible modifier loop");
132 greg 1.1 for ( ; mod != OVOID; mod = m->omod) {
133     m = objptr(mod);
134 greg 1.4 /****** unnecessary test since modifier() is always called
135 greg 1.1 if (!ismodifier(m->otype)) {
136     sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
137     error(USER, errmsg);
138     }
139 greg 1.4 ******/
140 greg 1.1 (*ofun[m->otype].funp)(m, r); /* execute function */
141     m->lastrno = r->rno;
142     if (ismaterial(m->otype)) { /* materials call raytexture */
143     depth--;
144     return; /* we're done */
145     }
146     }
147     objerror(r->ro, USER, "material not found");
148     }
149    
150    
151     raytexture(r, mod) /* get material modifiers */
152     RAY *r;
153     int mod;
154     {
155     static int depth = 0;
156     register OBJREC *m;
157     /* check for infinite loop */
158     if (depth++ >= MAXLOOP)
159     objerror(r->ro, USER, "modifier loop");
160     /* execute textures and patterns */
161     for ( ; mod != OVOID; mod = m->omod) {
162     m = objptr(mod);
163     if (!istexture(m->otype)) {
164     sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
165     error(USER, errmsg);
166     }
167     (*ofun[m->otype].funp)(m, r);
168     m->lastrno = r->rno;
169     }
170     depth--; /* end here */
171     }
172    
173    
174     raymixture(r, fore, back, coef) /* mix modifiers */
175     register RAY *r;
176     OBJECT fore, back;
177     double coef;
178     {
179     FVECT curpert, forepert, backpert;
180     COLOR curpcol, forepcol, backpcol;
181     register int i;
182     /* clip coefficient */
183     if (coef > 1.0)
184     coef = 1.0;
185     else if (coef < 0.0)
186     coef = 0.0;
187     /* save current mods */
188     VCOPY(curpert, r->pert);
189     copycolor(curpcol, r->pcol);
190     /* compute new mods */
191     /* foreground */
192     r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
193     setcolor(r->pcol, 1.0, 1.0, 1.0);
194     if (fore != OVOID && coef > FTINY)
195     raytexture(r, fore);
196     VCOPY(forepert, r->pert);
197     copycolor(forepcol, r->pcol);
198     /* background */
199     r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
200     setcolor(r->pcol, 1.0, 1.0, 1.0);
201     if (back != OVOID && coef < 1.0-FTINY)
202     raytexture(r, back);
203     VCOPY(backpert, r->pert);
204     copycolor(backpcol, r->pcol);
205     /* sum perturbations */
206     for (i = 0; i < 3; i++)
207     r->pert[i] = curpert[i] + coef*forepert[i] +
208     (1.0-coef)*backpert[i];
209     /* multiply colors */
210     setcolor(r->pcol, coef*colval(forepcol,RED) +
211     (1.0-coef)*colval(backpcol,RED),
212     coef*colval(forepcol,GRN) +
213     (1.0-coef)*colval(backpcol,GRN),
214     coef*colval(forepcol,BLU) +
215     (1.0-coef)*colval(backpcol,BLU));
216     multcolor(r->pcol, curpcol);
217     }
218    
219    
220     double
221     raynormal(norm, r) /* compute perturbed normal for ray */
222     FVECT norm;
223     register RAY *r;
224     {
225     double newdot;
226     register int i;
227    
228     /* The perturbation is added to the surface normal to obtain
229     * the new normal. If the new normal would affect the surface
230     * orientation wrt. the ray, a correction is made. The method is
231     * still fraught with problems since reflected rays and similar
232     * directions calculated from the surface normal may spawn rays behind
233     * the surface. The only solution is to curb textures at high
234 greg 1.9 * incidence (namely, keep DOT(rdir,pert) < Rdot).
235 greg 1.1 */
236    
237     for (i = 0; i < 3; i++)
238     norm[i] = r->ron[i] + r->pert[i];
239    
240     if (normalize(norm) == 0.0) {
241     objerror(r->ro, WARNING, "illegal normal perturbation");
242     VCOPY(norm, r->ron);
243     return(r->rod);
244     }
245     newdot = -DOT(norm, r->rdir);
246     if ((newdot > 0.0) != (r->rod > 0.0)) { /* fix orientation */
247     for (i = 0; i < 3; i++)
248     norm[i] += 2.0*newdot*r->rdir[i];
249     newdot = -newdot;
250     }
251     return(newdot);
252 greg 1.12 }
253    
254    
255     newrayxf(r) /* get new tranformation matrix for ray */
256     RAY *r;
257     {
258     static struct xfn {
259     struct xfn *next;
260     FULLXF xf;
261     } xfseed = { &xfseed }, *xflast = &xfseed;
262     register struct xfn *xp;
263     register RAY *rp;
264    
265     /*
266     * Search for transform in circular list that
267     * has no associated ray in the tree.
268     */
269     xp = xflast;
270     for (rp = r->parent; rp != NULL; rp = rp->parent)
271     if (rp->rox == &xp->xf) { /* xp in use */
272     xp = xp->next; /* move to next */
273     if (xp == xflast) { /* need new one */
274 greg 1.14 xp = (struct xfn *)bmalloc(sizeof(struct xfn));
275 greg 1.12 if (xp == NULL)
276     error(SYSTEM,
277     "out of memory in newrayxf");
278     /* insert in list */
279     xp->next = xflast->next;
280     xflast->next = xp;
281     break; /* we're done */
282     }
283     rp = r; /* start check over */
284     }
285     /* got it */
286     r->rox = &xp->xf;
287     xflast = xp;
288 greg 1.1 }
289    
290    
291     flipsurface(r) /* reverse surface orientation */
292     register RAY *r;
293     {
294     r->rod = -r->rod;
295     r->ron[0] = -r->ron[0];
296     r->ron[1] = -r->ron[1];
297     r->ron[2] = -r->ron[2];
298     r->pert[0] = -r->pert[0];
299     r->pert[1] = -r->pert[1];
300     r->pert[2] = -r->pert[2];
301     }
302    
303    
304     localhit(r, scene) /* check for hit in the octree */
305     register RAY *r;
306     register CUBE *scene;
307     {
308     FVECT curpos; /* current cube position */
309 greg 1.11 int sflags; /* sign flags */
310 greg 1.1 double t, dt;
311     register int i;
312    
313     nrays++; /* increment trace counter */
314    
315 greg 1.11 sflags = 0;
316 greg 1.1 for (i = 0; i < 3; i++) {
317     curpos[i] = r->rorg[i];
318     if (r->rdir[i] > FTINY)
319 greg 1.11 sflags |= 1 << i;
320 greg 1.1 else if (r->rdir[i] < -FTINY)
321 greg 1.11 sflags |= 0x10 << i;
322 greg 1.1 }
323     t = 0.0;
324     if (!incube(scene, curpos)) {
325     /* find distance to entry */
326     for (i = 0; i < 3; i++) {
327     /* plane in our direction */
328 greg 1.11 if (sflags & 1<<i)
329 greg 1.1 dt = scene->cuorg[i];
330 greg 1.11 else if (sflags & 0x10<<i)
331 greg 1.1 dt = scene->cuorg[i] + scene->cusize;
332     else
333     continue;
334     /* distance to the plane */
335     dt = (dt - r->rorg[i])/r->rdir[i];
336     if (dt > t)
337     t = dt; /* farthest face is the one */
338     }
339     t += FTINY; /* fudge to get inside cube */
340     /* advance position */
341     for (i = 0; i < 3; i++)
342     curpos[i] += r->rdir[i]*t;
343    
344     if (!incube(scene, curpos)) /* non-intersecting ray */
345     return(0);
346     }
347 greg 1.11 return(raymove(curpos, sflags, r, scene) == RAYHIT);
348 greg 1.1 }
349    
350    
351     static int
352 greg 1.11 raymove(pos, dirf, r, cu) /* check for hit as we move */
353 greg 1.1 FVECT pos; /* modified */
354 greg 1.11 int dirf; /* direction indicators to speed tests */
355 greg 1.1 register RAY *r;
356     register CUBE *cu;
357     {
358     int ax;
359     double dt, t;
360    
361     if (istree(cu->cutree)) { /* recurse on subcubes */
362     CUBE cukid;
363 greg 1.11 register int br, sgn;
364 greg 1.1
365     cukid.cusize = cu->cusize * 0.5; /* find subcube */
366     VCOPY(cukid.cuorg, cu->cuorg);
367     br = 0;
368     if (pos[0] >= cukid.cuorg[0]+cukid.cusize) {
369     cukid.cuorg[0] += cukid.cusize;
370     br |= 1;
371     }
372     if (pos[1] >= cukid.cuorg[1]+cukid.cusize) {
373     cukid.cuorg[1] += cukid.cusize;
374     br |= 2;
375     }
376     if (pos[2] >= cukid.cuorg[2]+cukid.cusize) {
377     cukid.cuorg[2] += cukid.cusize;
378     br |= 4;
379     }
380     for ( ; ; ) {
381     cukid.cutree = octkid(cu->cutree, br);
382 greg 1.11 if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT)
383 greg 1.1 return(RAYHIT);
384     sgn = 1 << ax;
385 greg 1.11 if (sgn & dirf) /* positive axis? */
386 greg 1.1 if (sgn & br)
387     return(ax); /* overflow */
388     else {
389     cukid.cuorg[ax] += cukid.cusize;
390     br |= sgn;
391     }
392 greg 1.11 else
393     if (sgn & br) {
394     cukid.cuorg[ax] -= cukid.cusize;
395     br &= ~sgn;
396     } else
397     return(ax); /* underflow */
398 greg 1.1 }
399     /*NOTREACHED*/
400     }
401     if (isfull(cu->cutree) && checkhit(r, cu))
402     return(RAYHIT);
403     /* advance to next cube */
404 greg 1.11 if (dirf&0x11) {
405     dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
406 greg 1.1 t = (dt - pos[0])/r->rdir[0];
407     ax = 0;
408     } else
409     t = FHUGE;
410 greg 1.11 if (dirf&0x22) {
411     dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
412 greg 1.1 dt = (dt - pos[1])/r->rdir[1];
413     if (dt < t) {
414     t = dt;
415     ax = 1;
416     }
417     }
418 greg 1.11 if (dirf&0x44) {
419     dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
420 greg 1.1 dt = (dt - pos[2])/r->rdir[2];
421     if (dt < t) {
422     t = dt;
423     ax = 2;
424     }
425     }
426     pos[0] += r->rdir[0]*t;
427     pos[1] += r->rdir[1]*t;
428     pos[2] += r->rdir[2]*t;
429     return(ax);
430     }
431    
432    
433     static
434     checkhit(r, cu) /* check for hit in full cube */
435     register RAY *r;
436     CUBE *cu;
437     {
438     OBJECT oset[MAXSET+1];
439     register OBJREC *o;
440     register int i;
441    
442     objset(oset, cu->cutree);
443     for (i = oset[0]; i > 0; i--) {
444     o = objptr(oset[i]);
445     if (o->lastrno == r->rno) /* checked already? */
446     continue;
447     (*ofun[o->otype].funp)(o, r);
448     o->lastrno = r->rno;
449     }
450     if (r->ro == NULL)
451     return(0); /* no scores yet */
452    
453     return(incube(cu, r->rop)); /* hit OK if in current cube */
454     }