ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 1.22
Committed: Wed Jul 17 12:38:48 1991 UTC (32 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.21: +8 -1 lines
Log Message:
added rayclear() function

File Contents

# Content
1 /* Copyright (c) 1991 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 #include "otspecial.h"
20
21 extern CUBE thescene; /* our scene */
22 extern int maxdepth; /* maximum recursion depth */
23 extern double minweight; /* minimum ray weight */
24 extern int do_irrad; /* compute irradiance? */
25
26 long raynum = 0L; /* next unique ray number */
27 long nrays = 0L; /* number of calls to localhit */
28
29 static double Lambfa[5] = {PI, PI, PI, 0.0, 0.0};
30 OBJREC Lamb = {
31 OVOID, MAT_PLASTIC, "Lambertian",
32 {0, 5, NULL, Lambfa}, NULL, -1,
33 }; /* a Lambertian surface */
34
35 #define MAXLOOP 128 /* modifier loop detection */
36
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 r->revf = raytrace;
52 } 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 r->revf = ro->revf;
63 r->rweight = ro->rweight * rw;
64 r->crtype = ro->crtype | (r->rtype = rt);
65 VCOPY(r->rorg, ro->rop);
66 }
67 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 r->rno = raynum++;
76 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 r->rt = 0.0;
83 }
84
85
86 raytrace(r) /* trace a ray and compute its value */
87 RAY *r;
88 {
89 extern int (*trace)();
90
91 if (localhit(r, &thescene))
92 raycont(r);
93 else if (sourcehit(r))
94 rayshade(r, r->ro->omod);
95
96 if (trace != NULL)
97 (*trace)(r); /* trace execution */
98 }
99
100
101 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 raytrans(r) /* transmit ray as is */
112 register RAY *r;
113 {
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 r->rt = r->rot + tr.rt;
121 }
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 objerror(r->ro, USER, "possible modifier loop");
134 r->rt = r->rot; /* set effective ray length */
135 for ( ; mod != OVOID; mod = m->omod) {
136 m = objptr(mod);
137 /****** unnecessary test since modifier() is always called
138 if (!ismodifier(m->otype)) {
139 sprintf(errmsg, "illegal modifier \"%s\"", m->oname);
140 error(USER, errmsg);
141 }
142 ******/
143 /* 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 if (!islight(m->otype))
151 m = &Lamb;
152 }
153 (*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 * incidence (namely, keep DOT(rdir,pert) < Rdot).
248 */
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 }
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 xp = (struct xfn *)bmalloc(sizeof(struct xfn));
288 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 }
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 int sflags; /* sign flags */
323 double t, dt;
324 register int i;
325
326 nrays++; /* increment trace counter */
327 sflags = 0;
328 for (i = 0; i < 3; i++) {
329 curpos[i] = r->rorg[i];
330 if (r->rdir[i] > FTINY)
331 sflags |= 1 << i;
332 else if (r->rdir[i] < -FTINY)
333 sflags |= 0x10 << i;
334 }
335 if (sflags == 0)
336 error(CONSISTENCY, "zero ray direction in localhit");
337 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 if (sflags & 1<<i)
343 dt = scene->cuorg[i];
344 else if (sflags & 0x10<<i)
345 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 return(raymove(curpos, sflags, r, scene) == RAYHIT);
362 }
363
364
365 static int
366 raymove(pos, dirf, r, cu) /* check for hit as we move */
367 FVECT pos; /* modified */
368 int dirf; /* direction indicators to speed tests */
369 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 register int br, sgn;
378
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 if ((ax = raymove(pos,dirf,r,&cukid)) == RAYHIT)
397 return(RAYHIT);
398 sgn = 1 << ax;
399 if (sgn & dirf) /* positive axis? */
400 if (sgn & br)
401 return(ax); /* overflow */
402 else {
403 cukid.cuorg[ax] += cukid.cusize;
404 br |= sgn;
405 }
406 else
407 if (sgn & br) {
408 cukid.cuorg[ax] -= cukid.cusize;
409 br &= ~sgn;
410 } else
411 return(ax); /* underflow */
412 }
413 /*NOTREACHED*/
414 }
415 if (isfull(cu->cutree) && checkhit(r, cu))
416 return(RAYHIT);
417 /* advance to next cube */
418 if (dirf&0x11) {
419 dt = dirf&1 ? cu->cuorg[0] + cu->cusize : cu->cuorg[0];
420 t = (dt - pos[0])/r->rdir[0];
421 ax = 0;
422 } else
423 t = FHUGE;
424 if (dirf&0x22) {
425 dt = dirf&2 ? cu->cuorg[1] + cu->cusize : cu->cuorg[1];
426 dt = (dt - pos[1])/r->rdir[1];
427 if (dt < t) {
428 t = dt;
429 ax = 1;
430 }
431 }
432 if (dirf&0x44) {
433 dt = dirf&4 ? cu->cuorg[2] + cu->cusize : cu->cuorg[2];
434 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 }