1 |
|
#ifndef lint |
2 |
< |
static const char RCSid[] = "$Id$"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
|
/* |
5 |
|
* raytrace.c - routines for tracing and shading rays. |
7 |
|
* External symbols declared in ray.h |
8 |
|
*/ |
9 |
|
|
10 |
< |
/* ==================================================================== |
11 |
< |
* The Radiance Software License, Version 1.0 |
12 |
< |
* |
13 |
< |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
14 |
< |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
15 |
< |
* |
16 |
< |
* Redistribution and use in source and binary forms, with or without |
17 |
< |
* modification, are permitted provided that the following conditions |
18 |
< |
* are met: |
19 |
< |
* |
20 |
< |
* 1. Redistributions of source code must retain the above copyright |
21 |
< |
* notice, this list of conditions and the following disclaimer. |
22 |
< |
* |
23 |
< |
* 2. Redistributions in binary form must reproduce the above copyright |
24 |
< |
* notice, this list of conditions and the following disclaimer in |
25 |
< |
* the documentation and/or other materials provided with the |
26 |
< |
* distribution. |
27 |
< |
* |
28 |
< |
* 3. The end-user documentation included with the redistribution, |
29 |
< |
* if any, must include the following acknowledgment: |
30 |
< |
* "This product includes Radiance software |
31 |
< |
* (http://radsite.lbl.gov/) |
32 |
< |
* developed by the Lawrence Berkeley National Laboratory |
33 |
< |
* (http://www.lbl.gov/)." |
34 |
< |
* Alternately, this acknowledgment may appear in the software itself, |
35 |
< |
* if and wherever such third-party acknowledgments normally appear. |
36 |
< |
* |
37 |
< |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
38 |
< |
* and "The Regents of the University of California" must |
39 |
< |
* not be used to endorse or promote products derived from this |
40 |
< |
* software without prior written permission. For written |
41 |
< |
* permission, please contact [email protected]. |
42 |
< |
* |
43 |
< |
* 5. Products derived from this software may not be called "Radiance", |
44 |
< |
* nor may "Radiance" appear in their name, without prior written |
45 |
< |
* permission of Lawrence Berkeley National Laboratory. |
46 |
< |
* |
47 |
< |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
48 |
< |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
49 |
< |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
50 |
< |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
51 |
< |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
52 |
< |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
53 |
< |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
54 |
< |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
55 |
< |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
56 |
< |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
57 |
< |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
58 |
< |
* SUCH DAMAGE. |
59 |
< |
* ==================================================================== |
60 |
< |
* |
61 |
< |
* This software consists of voluntary contributions made by many |
62 |
< |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
63 |
< |
* information on Lawrence Berkeley National Laboratory, please see |
64 |
< |
* <http://www.lbl.gov/>. |
65 |
< |
*/ |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
|
#include "ray.h" |
13 |
< |
|
13 |
> |
#include "source.h" |
14 |
|
#include "otypes.h" |
70 |
– |
|
15 |
|
#include "otspecial.h" |
16 |
|
|
17 |
|
#define MAXCSET ((MAXSET+1)*2-1) /* maximum check set size */ |
19 |
|
unsigned long raynum = 0; /* next unique ray number */ |
20 |
|
unsigned long nrays = 0; /* number of calls to localhit */ |
21 |
|
|
22 |
< |
static FLOAT Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
22 |
> |
static RREAL Lambfa[5] = {PI, PI, PI, 0.0, 0.0}; |
23 |
|
OBJREC Lamb = { |
24 |
|
OVOID, MAT_PLASTIC, "Lambertian", |
25 |
|
{0, 5, NULL, Lambfa}, NULL, |
27 |
|
|
28 |
|
OBJREC Aftplane; /* aft clipping plane object */ |
29 |
|
|
86 |
– |
static int raymove(), checkhit(); |
87 |
– |
static void checkset(); |
88 |
– |
|
89 |
– |
#ifndef MAXLOOP |
90 |
– |
#define MAXLOOP 0 /* modifier loop detection */ |
91 |
– |
#endif |
92 |
– |
|
30 |
|
#define RAYHIT (-1) /* return value for intercepted ray */ |
31 |
|
|
32 |
+ |
static int raymove(FVECT pos, OBJECT *cxs, int dirf, RAY *r, CUBE *cu); |
33 |
+ |
static int checkhit(RAY *r, CUBE *cu, OBJECT *cxs); |
34 |
+ |
static void checkset(OBJECT *os, OBJECT *cs); |
35 |
|
|
36 |
< |
int |
37 |
< |
rayorigin(r, ro, rt, rw) /* start new ray from old one */ |
38 |
< |
register RAY *r, *ro; |
39 |
< |
int rt; |
40 |
< |
double rw; |
36 |
> |
|
37 |
> |
extern int |
38 |
> |
rayorigin( /* start new ray from old one */ |
39 |
> |
register RAY *r, |
40 |
> |
register RAY *ro, |
41 |
> |
int rt, |
42 |
> |
double rw |
43 |
> |
) |
44 |
|
{ |
45 |
|
double re; |
46 |
|
|
55 |
|
copycolor(r->albedo, salbedo); |
56 |
|
r->gecc = seccg; |
57 |
|
r->slights = NULL; |
58 |
+ |
} else if (ro->rot >= FHUGE) { /* illegal continuation */ |
59 |
+ |
memset(r, 0, sizeof(RAY)); |
60 |
+ |
return(-1); |
61 |
|
} else { /* spawned ray */ |
62 |
|
r->rlvl = ro->rlvl; |
63 |
|
if (rt & RAYREFL) { |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
< |
void |
94 |
< |
rayclear(r) /* clear a ray for (re)evaluation */ |
95 |
< |
register RAY *r; |
93 |
> |
extern void |
94 |
> |
rayclear( /* clear a ray for (re)evaluation */ |
95 |
> |
register RAY *r |
96 |
> |
) |
97 |
|
{ |
98 |
|
r->rno = raynum++; |
99 |
|
r->newcset = r->clipset; |
100 |
+ |
r->hitf = rayhit; |
101 |
|
r->robj = OVOID; |
102 |
|
r->ro = NULL; |
103 |
|
r->rox = NULL; |
104 |
|
r->rt = r->rot = FHUGE; |
105 |
|
r->pert[0] = r->pert[1] = r->pert[2] = 0.0; |
106 |
+ |
r->uv[0] = r->uv[1] = 0.0; |
107 |
|
setcolor(r->pcol, 1.0, 1.0, 1.0); |
108 |
|
setcolor(r->rcol, 0.0, 0.0, 0.0); |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
< |
void |
113 |
< |
raytrace(r) /* trace a ray and compute its value */ |
114 |
< |
RAY *r; |
112 |
> |
extern void |
113 |
> |
raytrace( /* trace a ray and compute its value */ |
114 |
> |
RAY *r |
115 |
> |
) |
116 |
|
{ |
117 |
|
if (localhit(r, &thescene)) |
118 |
|
raycont(r); /* hit local surface, evaluate */ |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
< |
void |
133 |
< |
raycont(r) /* check for clipped object and continue */ |
134 |
< |
register RAY *r; |
132 |
> |
extern void |
133 |
> |
raycont( /* check for clipped object and continue */ |
134 |
> |
register RAY *r |
135 |
> |
) |
136 |
|
{ |
137 |
|
if ((r->clipset != NULL && inset(r->clipset, r->ro->omod)) || |
138 |
|
!rayshade(r, r->ro->omod)) |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
< |
void |
144 |
< |
raytrans(r) /* transmit ray as is */ |
145 |
< |
register RAY *r; |
143 |
> |
extern void |
144 |
> |
raytrans( /* transmit ray as is */ |
145 |
> |
register RAY *r |
146 |
> |
) |
147 |
|
{ |
148 |
|
RAY tr; |
149 |
|
|
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
< |
int |
160 |
< |
rayshade(r, mod) /* shade ray r with material mod */ |
161 |
< |
register RAY *r; |
162 |
< |
int mod; |
159 |
> |
extern int |
160 |
> |
rayshade( /* shade ray r with material mod */ |
161 |
> |
register RAY *r, |
162 |
> |
int mod |
163 |
> |
) |
164 |
|
{ |
212 |
– |
int gotmat; |
165 |
|
register OBJREC *m; |
166 |
< |
#if MAXLOOP |
215 |
< |
static int depth = 0; |
216 |
< |
/* check for infinite loop */ |
217 |
< |
if (depth++ >= MAXLOOP) |
218 |
< |
objerror(r->ro, USER, "possible modifier loop"); |
219 |
< |
#endif |
166 |
> |
|
167 |
|
r->rt = r->rot; /* set effective ray length */ |
168 |
< |
for (gotmat = 0; !gotmat && mod != OVOID; mod = m->omod) { |
168 |
> |
for ( ; mod != OVOID; mod = m->omod) { |
169 |
|
m = objptr(mod); |
170 |
|
/****** unnecessary test since modifier() is always called |
171 |
|
if (!ismodifier(m->otype)) { |
174 |
|
} |
175 |
|
******/ |
176 |
|
/* hack for irradiance calculation */ |
177 |
< |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS))) { |
177 |
> |
if (do_irrad && !(r->crtype & ~(PRIMARY|TRANS)) && |
178 |
> |
m->otype != MAT_CLIP && |
179 |
> |
(ofun[m->otype].flags & (T_M|T_X))) { |
180 |
|
if (irr_ignore(m->otype)) { |
232 |
– |
#if MAXLOOP |
233 |
– |
depth--; |
234 |
– |
#endif |
181 |
|
raytrans(r); |
182 |
|
return(1); |
183 |
|
} |
184 |
|
if (!islight(m->otype)) |
185 |
|
m = &Lamb; |
186 |
|
} |
187 |
< |
/* materials call raytexture */ |
188 |
< |
gotmat = (*ofun[m->otype].funp)(m, r); |
187 |
> |
if ((*ofun[m->otype].funp)(m, r)) |
188 |
> |
return(1); /* materials call raytexture() */ |
189 |
|
} |
190 |
< |
#if MAXLOOP |
245 |
< |
depth--; |
246 |
< |
#endif |
247 |
< |
return(gotmat); |
190 |
> |
return(0); /* no material! */ |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
< |
void |
195 |
< |
rayparticipate(r) /* compute ray medium participation */ |
196 |
< |
register RAY *r; |
194 |
> |
extern void |
195 |
> |
rayparticipate( /* compute ray medium participation */ |
196 |
> |
register RAY *r |
197 |
> |
) |
198 |
|
{ |
199 |
|
COLOR ce, ca; |
200 |
|
double re, ge, be; |
224 |
|
} |
225 |
|
|
226 |
|
|
227 |
< |
raytexture(r, mod) /* get material modifiers */ |
228 |
< |
RAY *r; |
229 |
< |
int mod; |
227 |
> |
extern void |
228 |
> |
raytexture( /* get material modifiers */ |
229 |
> |
RAY *r, |
230 |
> |
OBJECT mod |
231 |
> |
) |
232 |
|
{ |
233 |
|
register OBJREC *m; |
288 |
– |
#if MAXLOOP |
289 |
– |
static int depth = 0; |
290 |
– |
/* check for infinite loop */ |
291 |
– |
if (depth++ >= MAXLOOP) |
292 |
– |
objerror(r->ro, USER, "modifier loop"); |
293 |
– |
#endif |
234 |
|
/* execute textures and patterns */ |
235 |
|
for ( ; mod != OVOID; mod = m->omod) { |
236 |
|
m = objptr(mod); |
246 |
|
objerror(r->ro, USER, errmsg); |
247 |
|
} |
248 |
|
} |
309 |
– |
#if MAXLOOP |
310 |
– |
depth--; /* end here */ |
311 |
– |
#endif |
249 |
|
} |
250 |
|
|
251 |
|
|
252 |
< |
int |
253 |
< |
raymixture(r, fore, back, coef) /* mix modifiers */ |
254 |
< |
register RAY *r; |
255 |
< |
OBJECT fore, back; |
256 |
< |
double coef; |
252 |
> |
extern int |
253 |
> |
raymixture( /* mix modifiers */ |
254 |
> |
register RAY *r, |
255 |
> |
OBJECT fore, |
256 |
> |
OBJECT back, |
257 |
> |
double coef |
258 |
> |
) |
259 |
|
{ |
260 |
|
RAY fr, br; |
261 |
|
int foremat, backmat; |
268 |
|
/* compute foreground and background */ |
269 |
|
foremat = backmat = 0; |
270 |
|
/* foreground */ |
271 |
< |
copystruct(&fr, r); |
271 |
> |
fr = *r; |
272 |
|
if (coef > FTINY) |
273 |
|
foremat = rayshade(&fr, fore); |
274 |
|
/* background */ |
275 |
< |
copystruct(&br, r); |
275 |
> |
br = *r; |
276 |
|
if (coef < 1.0-FTINY) |
277 |
|
backmat = rayshade(&br, back); |
278 |
|
/* check for transparency */ |
279 |
< |
if (backmat ^ foremat) |
279 |
> |
if (backmat ^ foremat) { |
280 |
|
if (backmat && coef > FTINY) |
281 |
|
raytrans(&fr); |
282 |
|
else if (foremat && coef < 1.0-FTINY) |
283 |
|
raytrans(&br); |
284 |
+ |
} |
285 |
|
/* mix perturbations */ |
286 |
|
for (i = 0; i < 3; i++) |
287 |
|
r->pert[i] = coef*fr.pert[i] + (1.0-coef)*br.pert[i]; |
303 |
|
} |
304 |
|
|
305 |
|
|
306 |
< |
double |
307 |
< |
raydist(r, flags) /* compute (cumulative) ray distance */ |
308 |
< |
register RAY *r; |
309 |
< |
register int flags; |
306 |
> |
extern double |
307 |
> |
raydist( /* compute (cumulative) ray distance */ |
308 |
> |
register RAY *r, |
309 |
> |
register int flags |
310 |
> |
) |
311 |
|
{ |
312 |
|
double sum = 0.0; |
313 |
|
|
319 |
|
} |
320 |
|
|
321 |
|
|
322 |
< |
double |
323 |
< |
raynormal(norm, r) /* compute perturbed normal for ray */ |
324 |
< |
FVECT norm; |
325 |
< |
register RAY *r; |
322 |
> |
extern double |
323 |
> |
raynormal( /* compute perturbed normal for ray */ |
324 |
> |
FVECT norm, |
325 |
> |
register RAY *r |
326 |
> |
) |
327 |
|
{ |
328 |
|
double newdot; |
329 |
|
register int i; |
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
< |
void |
359 |
< |
newrayxf(r) /* get new tranformation matrix for ray */ |
360 |
< |
RAY *r; |
358 |
> |
extern void |
359 |
> |
newrayxf( /* get new tranformation matrix for ray */ |
360 |
> |
RAY *r |
361 |
> |
) |
362 |
|
{ |
363 |
|
static struct xfn { |
364 |
|
struct xfn *next; |
393 |
|
} |
394 |
|
|
395 |
|
|
396 |
< |
void |
397 |
< |
flipsurface(r) /* reverse surface orientation */ |
398 |
< |
register RAY *r; |
396 |
> |
extern void |
397 |
> |
flipsurface( /* reverse surface orientation */ |
398 |
> |
register RAY *r |
399 |
> |
) |
400 |
|
{ |
401 |
|
r->rod = -r->rod; |
402 |
|
r->ron[0] = -r->ron[0]; |
408 |
|
} |
409 |
|
|
410 |
|
|
411 |
< |
int |
412 |
< |
localhit(r, scene) /* check for hit in the octree */ |
413 |
< |
register RAY *r; |
414 |
< |
register CUBE *scene; |
411 |
> |
extern void |
412 |
> |
rayhit( /* standard ray hit test */ |
413 |
> |
OBJECT *oset, |
414 |
> |
RAY *r |
415 |
> |
) |
416 |
|
{ |
417 |
+ |
OBJREC *o; |
418 |
+ |
int i; |
419 |
+ |
|
420 |
+ |
for (i = oset[0]; i > 0; i--) { |
421 |
+ |
o = objptr(oset[i]); |
422 |
+ |
if ((*ofun[o->otype].funp)(o, r)) |
423 |
+ |
r->robj = oset[i]; |
424 |
+ |
} |
425 |
+ |
} |
426 |
+ |
|
427 |
+ |
|
428 |
+ |
extern int |
429 |
+ |
localhit( /* check for hit in the octree */ |
430 |
+ |
register RAY *r, |
431 |
+ |
register CUBE *scene |
432 |
+ |
) |
433 |
+ |
{ |
434 |
|
OBJECT cxset[MAXCSET+1]; /* set of checked objects */ |
435 |
|
FVECT curpos; /* current cube position */ |
436 |
|
int sflags; /* sign flags */ |
484 |
|
} |
485 |
|
cxset[0] = 0; |
486 |
|
raymove(curpos, cxset, sflags, r, scene); |
487 |
< |
return(r->ro != NULL & r->ro != &Aftplane); |
487 |
> |
return((r->ro != NULL) & (r->ro != &Aftplane)); |
488 |
|
} |
489 |
|
|
490 |
|
|
491 |
|
static int |
492 |
< |
raymove(pos, cxs, dirf, r, cu) /* check for hit as we move */ |
493 |
< |
FVECT pos; /* current position, modified herein */ |
494 |
< |
OBJECT *cxs; /* checked objects, modified by checkhit */ |
495 |
< |
int dirf; /* direction indicators to speed tests */ |
496 |
< |
register RAY *r; |
497 |
< |
register CUBE *cu; |
492 |
> |
raymove( /* check for hit as we move */ |
493 |
> |
FVECT pos, /* current position, modified herein */ |
494 |
> |
OBJECT *cxs, /* checked objects, modified by checkhit */ |
495 |
> |
int dirf, /* direction indicators to speed tests */ |
496 |
> |
register RAY *r, |
497 |
> |
register CUBE *cu |
498 |
> |
) |
499 |
|
{ |
500 |
|
int ax; |
501 |
|
double dt, t; |
576 |
|
|
577 |
|
|
578 |
|
static int |
579 |
< |
checkhit(r, cu, cxs) /* check for hit in full cube */ |
580 |
< |
register RAY *r; |
581 |
< |
CUBE *cu; |
582 |
< |
OBJECT *cxs; |
579 |
> |
checkhit( /* check for hit in full cube */ |
580 |
> |
register RAY *r, |
581 |
> |
CUBE *cu, |
582 |
> |
OBJECT *cxs |
583 |
> |
) |
584 |
|
{ |
585 |
|
OBJECT oset[MAXSET+1]; |
622 |
– |
register OBJREC *o; |
623 |
– |
register int i; |
586 |
|
|
587 |
|
objset(oset, cu->cutree); |
588 |
< |
checkset(oset, cxs); /* eliminate double-checking */ |
589 |
< |
for (i = oset[0]; i > 0; i--) { |
590 |
< |
o = objptr(oset[i]); |
591 |
< |
if ((*ofun[o->otype].funp)(o, r)) |
592 |
< |
r->robj = oset[i]; |
631 |
< |
} |
632 |
< |
if (r->ro == NULL) |
588 |
> |
checkset(oset, cxs); /* avoid double-checking */ |
589 |
> |
|
590 |
> |
(*r->hitf)(oset, r); /* test for hit in set */ |
591 |
> |
|
592 |
> |
if (r->robj == OVOID) |
593 |
|
return(0); /* no scores yet */ |
594 |
|
|
595 |
|
return(incube(cu, r->rop)); /* hit OK if in current cube */ |
597 |
|
|
598 |
|
|
599 |
|
static void |
600 |
< |
checkset(os, cs) /* modify checked set and set to check */ |
601 |
< |
register OBJECT *os; /* os' = os - cs */ |
602 |
< |
register OBJECT *cs; /* cs' = cs + os */ |
600 |
> |
checkset( /* modify checked set and set to check */ |
601 |
> |
register OBJECT *os, /* os' = os - cs */ |
602 |
> |
register OBJECT *cs /* cs' = cs + os */ |
603 |
> |
) |
604 |
|
{ |
605 |
|
OBJECT cset[MAXCSET+MAXSET+1]; |
606 |
|
register int i, j; |