ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/raytrace.c
Revision: 1.17
Committed: Fri May 10 08:51:06 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.16: +3 -1 lines
Log Message:
fixed bug introduced by better trans distance comp.

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