ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 1.16
Committed: Thu May 2 11:58:20 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.15: +10 -8 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     /* check for infinite loop */
122     if (depth++ >= MAXLOOP)
123 greg 1.4 objerror(r->ro, USER, "possible modifier loop");
124 greg 1.1 for ( ; mod != OVOID; mod = m->omod) {
125     m = objptr(mod);
126 greg 1.4 /****** unnecessary test since modifier() is always called
127 greg 1.1 if (!ismodifier(m->otype)) {
128     sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
129     error(USER, errmsg);
130     }
131 greg 1.4 ******/
132 greg 1.16 /* hack for irradiance calculation */
133     if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) {
134     if (irr_ignore(m->otype)) {
135     depth--;
136     raytrans(r);
137     return;
138     }
139     if (m->otype != MAT_ILLUM)
140     m = &Lamb;
141     }
142 greg 1.1 (*ofun[m->otype].funp)(m, r); /* execute function */
143     m->lastrno = r->rno;
144     if (ismaterial(m->otype)) { /* materials call raytexture */
145     depth--;
146     return; /* we're done */
147     }
148     }
149     objerror(r->ro, USER, "material not found");
150     }
151    
152    
153     raytexture(r, mod) /* get material modifiers */
154     RAY *r;
155     int mod;
156     {
157     static int depth = 0;
158     register OBJREC *m;
159     /* check for infinite loop */
160     if (depth++ >= MAXLOOP)
161     objerror(r->ro, USER, "modifier loop");
162     /* execute textures and patterns */
163     for ( ; mod != OVOID; mod = m->omod) {
164     m = objptr(mod);
165     if (!istexture(m->otype)) {
166     sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
167     error(USER, errmsg);
168     }
169     (*ofun[m->otype].funp)(m, r);
170     m->lastrno = r->rno;
171     }
172     depth--; /* end here */
173     }
174    
175    
176     raymixture(r, fore, back, coef) /* mix modifiers */
177     register RAY *r;
178     OBJECT fore, back;
179     double coef;
180     {
181     FVECT curpert, forepert, backpert;
182     COLOR curpcol, forepcol, backpcol;
183     register int i;
184     /* clip coefficient */
185     if (coef > 1.0)
186     coef = 1.0;
187     else if (coef < 0.0)
188     coef = 0.0;
189     /* save current mods */
190     VCOPY(curpert, r->pert);
191     copycolor(curpcol, r->pcol);
192     /* compute new mods */
193     /* foreground */
194     r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
195     setcolor(r->pcol, 1.0, 1.0, 1.0);
196     if (fore != OVOID && coef > FTINY)
197     raytexture(r, fore);
198     VCOPY(forepert, r->pert);
199     copycolor(forepcol, r->pcol);
200     /* background */
201     r->pert[0] = r->pert[1] = r->pert[2] = 0.0;
202     setcolor(r->pcol, 1.0, 1.0, 1.0);
203     if (back != OVOID && coef < 1.0-FTINY)
204     raytexture(r, back);
205     VCOPY(backpert, r->pert);
206     copycolor(backpcol, r->pcol);
207     /* sum perturbations */
208     for (i = 0; i < 3; i++)
209     r->pert[i] = curpert[i] + coef*forepert[i] +
210     (1.0-coef)*backpert[i];
211     /* multiply colors */
212     setcolor(r->pcol, coef*colval(forepcol,RED) +
213     (1.0-coef)*colval(backpcol,RED),
214     coef*colval(forepcol,GRN) +
215     (1.0-coef)*colval(backpcol,GRN),
216     coef*colval(forepcol,BLU) +
217     (1.0-coef)*colval(backpcol,BLU));
218     multcolor(r->pcol, curpcol);
219     }
220    
221    
222     double
223     raynormal(norm, r) /* compute perturbed normal for ray */
224     FVECT norm;
225     register RAY *r;
226     {
227     double newdot;
228     register int i;
229    
230     /* The perturbation is added to the surface normal to obtain
231     * the new normal. If the new normal would affect the surface
232     * orientation wrt. the ray, a correction is made. The method is
233     * still fraught with problems since reflected rays and similar
234     * directions calculated from the surface normal may spawn rays behind
235     * the surface. The only solution is to curb textures at high
236 greg 1.9 * incidence (namely, keep DOT(rdir,pert) < Rdot).
237 greg 1.1 */
238    
239     for (i = 0; i < 3; i++)
240     norm[i] = r->ron[i] + r->pert[i];
241    
242     if (normalize(norm) == 0.0) {
243     objerror(r->ro, WARNING, "illegal normal perturbation");
244     VCOPY(norm, r->ron);
245     return(r->rod);
246     }
247     newdot = -DOT(norm, r->rdir);
248     if ((newdot > 0.0) != (r->rod > 0.0)) { /* fix orientation */
249     for (i = 0; i < 3; i++)
250     norm[i] += 2.0*newdot*r->rdir[i];
251     newdot = -newdot;
252     }
253     return(newdot);
254 greg 1.12 }
255    
256    
257     newrayxf(r) /* get new tranformation matrix for ray */
258     RAY *r;
259     {
260     static struct xfn {
261     struct xfn *next;
262     FULLXF xf;
263     } xfseed = { &xfseed }, *xflast = &xfseed;
264     register struct xfn *xp;
265     register RAY *rp;
266    
267     /*
268     * Search for transform in circular list that
269     * has no associated ray in the tree.
270     */
271     xp = xflast;
272     for (rp = r->parent; rp != NULL; rp = rp->parent)
273     if (rp->rox == &xp->xf) { /* xp in use */
274     xp = xp->next; /* move to next */
275     if (xp == xflast) { /* need new one */
276 greg 1.14 xp = (struct xfn *)bmalloc(sizeof(struct xfn));
277 greg 1.12 if (xp == NULL)
278     error(SYSTEM,
279     "out of memory in newrayxf");
280     /* insert in list */
281     xp->next = xflast->next;
282     xflast->next = xp;
283     break; /* we're done */
284     }
285     rp = r; /* start check over */
286     }
287     /* got it */
288     r->rox = &xp->xf;
289     xflast = xp;
290 greg 1.1 }
291    
292    
293     flipsurface(r) /* reverse surface orientation */
294     register RAY *r;
295     {
296     r->rod = -r->rod;
297     r->ron[0] = -r->ron[0];
298     r->ron[1] = -r->ron[1];
299     r->ron[2] = -r->ron[2];
300     r->pert[0] = -r->pert[0];
301     r->pert[1] = -r->pert[1];
302     r->pert[2] = -r->pert[2];
303     }
304    
305    
306     localhit(r, scene) /* check for hit in the octree */
307     register RAY *r;
308     register CUBE *scene;
309     {
310     FVECT curpos; /* current cube position */
311 greg 1.11 int sflags; /* sign flags */
312 greg 1.1 double t, dt;
313     register int i;
314    
315     nrays++; /* increment trace counter */
316    
317 greg 1.11 sflags = 0;
318 greg 1.1 for (i = 0; i < 3; i++) {
319     curpos[i] = r->rorg[i];
320     if (r->rdir[i] > FTINY)
321 greg 1.11 sflags |= 1 << i;
322 greg 1.1 else if (r->rdir[i] < -FTINY)
323 greg 1.11 sflags |= 0x10 << i;
324 greg 1.1 }
325     t = 0.0;
326     if (!incube(scene, curpos)) {
327     /* find distance to entry */
328     for (i = 0; i < 3; i++) {
329     /* plane in our direction */
330 greg 1.11 if (sflags & 1<<i)
331 greg 1.1 dt = scene->cuorg[i];
332 greg 1.11 else if (sflags & 0x10<<i)
333 greg 1.1 dt = scene->cuorg[i] + scene->cusize;
334     else
335     continue;
336     /* distance to the plane */
337     dt = (dt - r->rorg[i])/r->rdir[i];
338     if (dt > t)
339     t = dt; /* farthest face is the one */
340     }
341     t += FTINY; /* fudge to get inside cube */
342     /* advance position */
343     for (i = 0; i < 3; i++)
344     curpos[i] += r->rdir[i]*t;
345    
346     if (!incube(scene, curpos)) /* non-intersecting ray */
347     return(0);
348     }
349 greg 1.11 return(raymove(curpos, sflags, r, scene) == RAYHIT);
350 greg 1.1 }
351    
352    
353     static int
354 greg 1.11 raymove(pos, dirf, r, cu) /* check for hit as we move */
355 greg 1.1 FVECT pos; /* modified */
356 greg 1.11 int dirf; /* direction indicators to speed tests */
357 greg 1.1 register RAY *r;
358     register CUBE *cu;
359     {
360     int ax;
361     double dt, t;
362    
363     if (istree(cu->cutree)) { /* recurse on subcubes */
364     CUBE cukid;
365 greg 1.11 register int br, sgn;
366 greg 1.1
367     cukid.cusize = cu->cusize * 0.5; /* find subcube */
368     VCOPY(cukid.cuorg, cu->cuorg);
369     br = 0;
370     if (pos[0] >= cukid.cuorg[0]+cukid.cusize) {
371     cukid.cuorg[0] += cukid.cusize;
372     br |= 1;
373     }
374     if (pos[1] >= cukid.cuorg[1]+cukid.cusize) {
375     cukid.cuorg[1] += cukid.cusize;
376     br |= 2;
377     }
378     if (pos[2] >= cukid.cuorg[2]+cukid.cusize) {
379     cukid.cuorg[2] += cukid.cusize;
380     br |= 4;
381     }
382     for ( ; ; ) {
383     cukid.cutree = octkid(cu->cutree, br);
384 greg 1.11 if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT)
385 greg 1.1 return(RAYHIT);
386     sgn = 1 << ax;
387 greg 1.11 if (sgn & dirf) /* positive axis? */
388 greg 1.1 if (sgn & br)
389     return(ax); /* overflow */
390     else {
391     cukid.cuorg[ax] += cukid.cusize;
392     br |= sgn;
393     }
394 greg 1.11 else
395     if (sgn & br) {
396     cukid.cuorg[ax] -= cukid.cusize;
397     br &= ~sgn;
398     } else
399     return(ax); /* underflow */
400 greg 1.1 }
401     /*NOTREACHED*/
402     }
403     if (isfull(cu->cutree) && checkhit(r, cu))
404     return(RAYHIT);
405     /* advance to next cube */
406 greg 1.11 if (dirf&0x11) {
407     dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
408 greg 1.1 t = (dt - pos[0])/r->rdir[0];
409     ax = 0;
410     } else
411     t = FHUGE;
412 greg 1.11 if (dirf&0x22) {
413     dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
414 greg 1.1 dt = (dt - pos[1])/r->rdir[1];
415     if (dt < t) {
416     t = dt;
417     ax = 1;
418     }
419     }
420 greg 1.11 if (dirf&0x44) {
421     dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
422 greg 1.1 dt = (dt - pos[2])/r->rdir[2];
423     if (dt < t) {
424     t = dt;
425     ax = 2;
426     }
427     }
428     pos[0] += r->rdir[0]*t;
429     pos[1] += r->rdir[1]*t;
430     pos[2] += r->rdir[2]*t;
431     return(ax);
432     }
433    
434    
435     static
436     checkhit(r, cu) /* check for hit in full cube */
437     register RAY *r;
438     CUBE *cu;
439     {
440     OBJECT oset[MAXSET+1];
441     register OBJREC *o;
442     register int i;
443    
444     objset(oset, cu->cutree);
445     for (i = oset[0]; i > 0; i--) {
446     o = objptr(oset[i]);
447     if (o->lastrno == r->rno) /* checked already? */
448     continue;
449     (*ofun[o->otype].funp)(o, r);
450     o->lastrno = r->rno;
451     }
452     if (r->ro == NULL)
453     return(0); /* no scores yet */
454    
455     return(incube(cu, r->rop)); /* hit OK if in current cube */
456     }