ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 1.21
Committed: Thu Jun 20 13:29:32 1991 UTC (32 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.20: +5 -3 lines
Log Message:
added revf member to ray structure for dynamic evaluation

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