ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 1.6
Committed: Mon Oct 16 18:57:02 1989 UTC (34 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.5: +1 -1 lines
Log Message:
Reduced preposterous MAXLOOP value

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