ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 2.22
Committed: Wed Dec 6 12:07:50 1995 UTC (28 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.21: +2 -3 lines
Log Message:
changed aft clipping plane to work through transparent surfaces

File Contents

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