ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 1.19
Committed: Fri Jun 14 10:34:26 1991 UTC (32 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.18: +1 -0 lines
Log Message:
changed initialization of effective ray length

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