ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.1
Committed: Tue Nov 12 17:09:44 1991 UTC (32 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.23: +0 -0 lines
Log Message:
updated revision number for release 2.0

File Contents

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