ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 1.11
Committed: Sat Aug 18 04:25:59 1990 UTC (33 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.10: +24 -26 lines
Log Message:
simplified direction flag in raymove

File Contents

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