ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.5
Committed: Mon Mar 8 12:37:30 1993 UTC (31 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +2 -0 lines
Log Message:
portability fixes (removed gcc warnings)

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