1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Support routines for source objects and materials |
6 |
+ |
* |
7 |
+ |
* External symbols declared in source.h |
8 |
|
*/ |
9 |
|
|
10 |
+ |
#include "copyright.h" |
11 |
+ |
|
12 |
|
#include "ray.h" |
13 |
|
|
14 |
|
#include "otypes.h" |
19 |
|
|
20 |
|
#include "face.h" |
21 |
|
|
22 |
< |
#define SRCINC 4 /* realloc increment for array */ |
22 |
> |
#define SRCINC 32 /* realloc increment for array */ |
23 |
|
|
24 |
|
SRCREC *source = NULL; /* our list of sources */ |
25 |
|
int nsources = 0; /* the number of sources */ |
27 |
|
SRCFUNC sfun[NUMOTYPE]; /* source dispatch table */ |
28 |
|
|
29 |
|
|
30 |
< |
initstypes() /* initialize source dispatch table */ |
30 |
> |
void |
31 |
> |
initstypes(void) /* initialize source dispatch table */ |
32 |
|
{ |
33 |
|
extern VSMATERIAL mirror_vs, direct1_vs, direct2_vs; |
32 |
– |
extern int fsetsrc(), ssetsrc(), sphsetsrc(), cylsetsrc(), rsetsrc(); |
33 |
– |
extern int nopart(), flatpart(), cylpart(); |
34 |
– |
extern double fgetplaneq(), rgetplaneq(); |
35 |
– |
extern double fgetmaxdisk(), rgetmaxdisk(); |
34 |
|
static SOBJECT fsobj = {fsetsrc, flatpart, fgetplaneq, fgetmaxdisk}; |
35 |
|
static SOBJECT ssobj = {ssetsrc, nopart}; |
36 |
|
static SOBJECT sphsobj = {sphsetsrc, nopart}; |
49 |
|
|
50 |
|
|
51 |
|
int |
52 |
< |
newsource() /* allocate new source in our array */ |
52 |
> |
newsource(void) /* allocate new source in our array */ |
53 |
|
{ |
54 |
|
if (nsources == 0) |
55 |
|
source = (SRCREC *)malloc(SRCINC*sizeof(SRCREC)); |
56 |
|
else if (nsources%SRCINC == 0) |
57 |
< |
source = (SRCREC *)realloc((char *)source, |
57 |
> |
source = (SRCREC *)realloc((void *)source, |
58 |
|
(unsigned)(nsources+SRCINC)*sizeof(SRCREC)); |
59 |
|
if (source == NULL) |
60 |
|
return(-1); |
61 |
|
source[nsources].sflags = 0; |
62 |
|
source[nsources].nhits = 1; |
63 |
< |
source[nsources].ntests = 2; /* initial hit probability = 1/2 */ |
63 |
> |
source[nsources].ntests = 2; /* initial hit probability = 50% */ |
64 |
> |
#if SHADCACHE |
65 |
> |
source[nsources].obscache = NULL; |
66 |
> |
#endif |
67 |
|
return(nsources++); |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
< |
setflatss(src) /* set sampling for a flat source */ |
72 |
< |
register SRCREC *src; |
71 |
> |
void |
72 |
> |
setflatss( /* set sampling for a flat source */ |
73 |
> |
SRCREC *src |
74 |
> |
) |
75 |
|
{ |
76 |
|
double mult; |
77 |
< |
register int i; |
77 |
> |
int i; |
78 |
|
|
79 |
< |
src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0; |
79 |
> |
getperpendicular(src->ss[SU], src->snorm, rand_samp); |
80 |
> |
mult = .5 * sqrt( src->ss2 ); |
81 |
|
for (i = 0; i < 3; i++) |
78 |
– |
if (src->snorm[i] < 0.6 && src->snorm[i] > -0.6) |
79 |
– |
break; |
80 |
– |
src->ss[SV][i] = 1.0; |
81 |
– |
fcross(src->ss[SU], src->ss[SV], src->snorm); |
82 |
– |
mult = .5 * sqrt( src->ss2 / DOT(src->ss[SU],src->ss[SU]) ); |
83 |
– |
for (i = 0; i < 3; i++) |
82 |
|
src->ss[SU][i] *= mult; |
83 |
|
fcross(src->ss[SV], src->snorm, src->ss[SU]); |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
< |
fsetsrc(src, so) /* set a face as a source */ |
88 |
< |
register SRCREC *src; |
89 |
< |
OBJREC *so; |
87 |
> |
void |
88 |
> |
fsetsrc( /* set a face as a source */ |
89 |
> |
SRCREC *src, |
90 |
> |
OBJREC *so |
91 |
> |
) |
92 |
|
{ |
93 |
< |
register FACE *f; |
94 |
< |
register int i, j; |
93 |
> |
FACE *f; |
94 |
> |
int i, j; |
95 |
|
double d; |
96 |
|
|
97 |
|
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
98 |
|
src->so = so; |
99 |
|
/* get the face */ |
100 |
|
f = getface(so); |
101 |
+ |
if (f->area == 0.) |
102 |
+ |
objerror(so, USER, "zero source area"); |
103 |
|
/* find the center */ |
104 |
|
for (j = 0; j < 3; j++) { |
105 |
|
src->sloc[j] = 0.0; |
108 |
|
src->sloc[j] /= (double)f->nv; |
109 |
|
} |
110 |
|
if (!inface(src->sloc, f)) |
111 |
< |
objerror(so, USER, "cannot hit center"); |
111 |
> |
objerror(so, USER, "cannot hit source center"); |
112 |
|
src->sflags |= SFLAT; |
113 |
|
VCOPY(src->snorm, f->norm); |
114 |
|
src->ss2 = f->area; |
121 |
|
} |
122 |
|
src->srad = sqrt(src->srad); |
123 |
|
/* compute size vectors */ |
124 |
< |
if (f->nv == 4 || (f->nv == 5 && /* parallelogram case */ |
123 |
< |
dist2(VERTEX(f,0),VERTEX(f,4)) <= FTINY*FTINY)) |
124 |
> |
if (f->nv == 4) { /* parallelogram case */ |
125 |
|
for (j = 0; j < 3; j++) { |
126 |
|
src->ss[SU][j] = .5*(VERTEX(f,1)[j]-VERTEX(f,0)[j]); |
127 |
|
src->ss[SV][j] = .5*(VERTEX(f,3)[j]-VERTEX(f,0)[j]); |
128 |
|
} |
129 |
< |
else |
130 |
< |
setflatss(src); |
129 |
> |
} else if (f->nv == 3) { /* triangle case */ |
130 |
> |
int near0 = 2; |
131 |
> |
double dmin = dist2line(src->sloc, VERTEX(f,2), VERTEX(f,0)); |
132 |
> |
for (i = 0; i < 2; i++) { |
133 |
> |
double d2 = dist2line(src->sloc, VERTEX(f,i), VERTEX(f,i+1)); |
134 |
> |
if (d2 >= dmin) |
135 |
> |
continue; |
136 |
> |
near0 = i; |
137 |
> |
dmin = d2; /* radius = min distance */ |
138 |
> |
} |
139 |
> |
if (dmin < .08*f->area) |
140 |
> |
objerror(so, WARNING, "triangular source with poor aspect"); |
141 |
> |
i = (near0 + 1) % 3; |
142 |
> |
for (j = 0; j < 3; j++) |
143 |
> |
src->ss[SU][j] = VERTEX(f,i)[j] - VERTEX(f,near0)[j]; |
144 |
> |
normalize(src->ss[SU]); |
145 |
> |
dmin = sqrt(dmin); |
146 |
> |
for (j = 0; j < 3; j++) |
147 |
> |
src->ss[SU][j] *= dmin; |
148 |
> |
fcross(src->ss[SV], f->norm, src->ss[SU]); |
149 |
> |
} else |
150 |
> |
setflatss(src); /* hope for convex! */ |
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
< |
ssetsrc(src, so) /* set a source as a source */ |
155 |
< |
register SRCREC *src; |
156 |
< |
register OBJREC *so; |
154 |
> |
void |
155 |
> |
ssetsrc( /* set a source as a source */ |
156 |
> |
SRCREC *src, |
157 |
> |
OBJREC *so |
158 |
> |
) |
159 |
|
{ |
160 |
|
double theta; |
161 |
|
|
163 |
|
src->so = so; |
164 |
|
if (so->oargs.nfargs != 4) |
165 |
|
objerror(so, USER, "bad arguments"); |
166 |
< |
src->sflags |= SDISTANT; |
166 |
> |
src->sflags |= (SDISTANT|SCIR); |
167 |
|
VCOPY(src->sloc, so->oargs.farg); |
168 |
|
if (normalize(src->sloc) == 0.0) |
169 |
|
objerror(so, USER, "zero direction"); |
175 |
|
src->srad = sqrt(src->ss2/PI); |
176 |
|
VCOPY(src->snorm, src->sloc); |
177 |
|
setflatss(src); /* hey, whatever works */ |
155 |
– |
src->ss[SW][0] = src->ss[SW][1] = src->ss[SW][2] = 0.0; |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
< |
sphsetsrc(src, so) /* set a sphere as a source */ |
182 |
< |
register SRCREC *src; |
183 |
< |
register OBJREC *so; |
181 |
> |
void |
182 |
> |
sphsetsrc( /* set a sphere as a source */ |
183 |
> |
SRCREC *src, |
184 |
> |
OBJREC *so |
185 |
> |
) |
186 |
|
{ |
187 |
< |
register int i; |
187 |
> |
int i; |
188 |
|
|
189 |
|
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
190 |
|
src->so = so; |
191 |
|
if (so->oargs.nfargs != 4) |
192 |
|
objerror(so, USER, "bad # arguments"); |
193 |
|
if (so->oargs.farg[3] <= FTINY) |
194 |
< |
objerror(so, USER, "illegal radius"); |
194 |
> |
objerror(so, USER, "illegal source radius"); |
195 |
> |
src->sflags |= SCIR; |
196 |
|
VCOPY(src->sloc, so->oargs.farg); |
197 |
|
src->srad = so->oargs.farg[3]; |
198 |
|
src->ss2 = PI * src->srad * src->srad; |
199 |
+ |
memset(src->ss, 0, sizeof(src->ss)); |
200 |
|
for (i = 0; i < 3; i++) |
201 |
< |
src->ss[SU][i] = src->ss[SV][i] = src->ss[SW][i] = 0.0; |
176 |
< |
for (i = 0; i < 3; i++) |
177 |
< |
src->ss[i][i] = .7236 * so->oargs.farg[3]; |
201 |
> |
src->ss[i][i] = 0.7236 * so->oargs.farg[3]; |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
< |
rsetsrc(src, so) /* set a ring (disk) as a source */ |
206 |
< |
register SRCREC *src; |
207 |
< |
OBJREC *so; |
205 |
> |
void |
206 |
> |
rsetsrc( /* set a ring (disk) as a source */ |
207 |
> |
SRCREC *src, |
208 |
> |
OBJREC *so |
209 |
> |
) |
210 |
|
{ |
211 |
< |
register CONE *co; |
211 |
> |
CONE *co; |
212 |
|
|
213 |
|
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
214 |
|
src->so = so; |
215 |
|
/* get the ring */ |
216 |
|
co = getcone(so, 0); |
217 |
+ |
if (co == NULL) |
218 |
+ |
objerror(so, USER, "illegal source"); |
219 |
+ |
if (CO_R1(co) <= FTINY) |
220 |
+ |
objerror(so, USER, "illegal source radius"); |
221 |
|
VCOPY(src->sloc, CO_P0(co)); |
222 |
|
if (CO_R0(co) > 0.0) |
223 |
< |
objerror(so, USER, "cannot hit center"); |
224 |
< |
src->sflags |= SFLAT; |
223 |
> |
objerror(so, USER, "cannot hit source center"); |
224 |
> |
src->sflags |= (SFLAT|SCIR); |
225 |
|
VCOPY(src->snorm, co->ad); |
226 |
|
src->srad = CO_R1(co); |
227 |
|
src->ss2 = PI * src->srad * src->srad; |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
< |
cylsetsrc(src, so) /* set a cylinder as a source */ |
233 |
< |
register SRCREC *src; |
234 |
< |
OBJREC *so; |
232 |
> |
void |
233 |
> |
cylsetsrc( /* set a cylinder as a source */ |
234 |
> |
SRCREC *src, |
235 |
> |
OBJREC *so |
236 |
> |
) |
237 |
|
{ |
238 |
< |
register CONE *co; |
239 |
< |
register int i; |
238 |
> |
CONE *co; |
239 |
> |
int i; |
240 |
|
|
241 |
|
src->sa.success = 4*AIMREQT-1; /* bitch on fourth failure */ |
242 |
|
src->so = so; |
243 |
|
/* get the cylinder */ |
244 |
|
co = getcone(so, 0); |
245 |
+ |
if (co == NULL) |
246 |
+ |
objerror(so, USER, "illegal source"); |
247 |
+ |
if (CO_R0(co) <= FTINY) |
248 |
+ |
objerror(so, USER, "illegal source radius"); |
249 |
|
if (CO_R0(co) > .2*co->al) /* heuristic constraint */ |
250 |
|
objerror(so, WARNING, "source aspect too small"); |
251 |
|
src->sflags |= SCYL; |
256 |
|
/* set sampling vectors */ |
257 |
|
for (i = 0; i < 3; i++) |
258 |
|
src->ss[SU][i] = .5 * co->al * co->ad[i]; |
259 |
< |
src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0; |
259 |
> |
getperpendicular(src->ss[SW], co->ad, rand_samp); |
260 |
|
for (i = 0; i < 3; i++) |
225 |
– |
if (co->ad[i] < 0.6 && co->ad[i] > -0.6) |
226 |
– |
break; |
227 |
– |
src->ss[SV][i] = 1.0; |
228 |
– |
fcross(src->ss[SW], src->ss[SV], co->ad); |
229 |
– |
normalize(src->ss[SW]); |
230 |
– |
for (i = 0; i < 3; i++) |
261 |
|
src->ss[SW][i] *= .8559 * CO_R0(co); |
262 |
|
fcross(src->ss[SV], src->ss[SW], co->ad); |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
|
SPOT * |
267 |
< |
makespot(m) /* make a spotlight */ |
268 |
< |
register OBJREC *m; |
267 |
> |
makespot( /* make a spotlight */ |
268 |
> |
OBJREC *m |
269 |
> |
) |
270 |
|
{ |
271 |
< |
register SPOT *ns; |
271 |
> |
SPOT *ns; |
272 |
|
|
273 |
+ |
if ((ns = (SPOT *)m->os) != NULL) |
274 |
+ |
return(ns); |
275 |
|
if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
276 |
|
return(NULL); |
277 |
+ |
if (m->oargs.farg[3] <= FTINY) |
278 |
+ |
objerror(m, USER, "zero angle"); |
279 |
|
ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3])); |
280 |
|
VCOPY(ns->aim, m->oargs.farg+4); |
281 |
|
if ((ns->flen = normalize(ns->aim)) == 0.0) |
282 |
|
objerror(m, USER, "zero focus vector"); |
283 |
+ |
m->os = (char *)ns; |
284 |
|
return(ns); |
285 |
|
} |
286 |
|
|
287 |
|
|
288 |
+ |
int |
289 |
+ |
spotout( /* check if we're outside spot region */ |
290 |
+ |
RAY *r, |
291 |
+ |
SPOT *s |
292 |
+ |
) |
293 |
+ |
{ |
294 |
+ |
double d; |
295 |
+ |
FVECT vd; |
296 |
+ |
|
297 |
+ |
if (s == NULL) |
298 |
+ |
return(0); |
299 |
+ |
if (s->flen < -FTINY) { /* distant source */ |
300 |
+ |
vd[0] = s->aim[0] - r->rorg[0]; |
301 |
+ |
vd[1] = s->aim[1] - r->rorg[1]; |
302 |
+ |
vd[2] = s->aim[2] - r->rorg[2]; |
303 |
+ |
d = DOT(r->rdir,vd); |
304 |
+ |
/* wrong side? |
305 |
+ |
if (d <= FTINY) |
306 |
+ |
return(1); */ |
307 |
+ |
d = DOT(vd,vd) - d*d; |
308 |
+ |
if (PI*d > s->siz) |
309 |
+ |
return(1); /* out */ |
310 |
+ |
return(0); /* OK */ |
311 |
+ |
} |
312 |
+ |
/* local source */ |
313 |
+ |
if (s->siz < 2.0*PI * (1.0 + DOT(s->aim,r->rdir))) |
314 |
+ |
return(1); /* out */ |
315 |
+ |
return(0); /* OK */ |
316 |
+ |
} |
317 |
+ |
|
318 |
+ |
|
319 |
|
double |
320 |
< |
fgetmaxdisk(ocent, op) /* get center and squared radius of face */ |
321 |
< |
FVECT ocent; |
322 |
< |
OBJREC *op; |
320 |
> |
fgetmaxdisk( /* get center and squared radius of face */ |
321 |
> |
FVECT ocent, |
322 |
> |
OBJREC *op |
323 |
> |
) |
324 |
|
{ |
325 |
|
double maxrad2; |
326 |
|
double d; |
327 |
< |
register int i, j; |
328 |
< |
register FACE *f; |
327 |
> |
int i, j; |
328 |
> |
FACE *f; |
329 |
|
|
330 |
|
f = getface(op); |
331 |
|
if (f->area == 0.) |
350 |
|
|
351 |
|
|
352 |
|
double |
353 |
< |
rgetmaxdisk(ocent, op) /* get center and squared radius of ring */ |
354 |
< |
FVECT ocent; |
355 |
< |
OBJREC *op; |
353 |
> |
rgetmaxdisk( /* get center and squared radius of ring */ |
354 |
> |
FVECT ocent, |
355 |
> |
OBJREC *op |
356 |
> |
) |
357 |
|
{ |
358 |
< |
register CONE *co; |
358 |
> |
CONE *co; |
359 |
|
|
360 |
|
co = getcone(op, 0); |
361 |
+ |
if (co == NULL) |
362 |
+ |
return(0.); |
363 |
|
VCOPY(ocent, CO_P0(co)); |
364 |
|
return(CO_R1(co)*CO_R1(co)); |
365 |
|
} |
366 |
|
|
367 |
|
|
368 |
|
double |
369 |
< |
fgetplaneq(nvec, op) /* get plane equation for face */ |
370 |
< |
FVECT nvec; |
371 |
< |
OBJREC *op; |
369 |
> |
fgetplaneq( /* get plane equation for face */ |
370 |
> |
FVECT nvec, |
371 |
> |
OBJREC *op |
372 |
> |
) |
373 |
|
{ |
374 |
< |
register FACE *fo; |
374 |
> |
FACE *fo; |
375 |
|
|
376 |
|
fo = getface(op); |
377 |
|
VCOPY(nvec, fo->norm); |
380 |
|
|
381 |
|
|
382 |
|
double |
383 |
< |
rgetplaneq(nvec, op) /* get plane equation for ring */ |
384 |
< |
FVECT nvec; |
385 |
< |
OBJREC *op; |
383 |
> |
rgetplaneq( /* get plane equation for ring */ |
384 |
> |
FVECT nvec, |
385 |
> |
OBJREC *op |
386 |
> |
) |
387 |
|
{ |
388 |
< |
register CONE *co; |
388 |
> |
CONE *co; |
389 |
|
|
390 |
|
co = getcone(op, 0); |
391 |
+ |
if (co == NULL) { |
392 |
+ |
memset(nvec, 0, sizeof(FVECT)); |
393 |
+ |
return(0.); |
394 |
+ |
} |
395 |
|
VCOPY(nvec, co->ad); |
396 |
|
return(DOT(nvec, CO_P0(co))); |
397 |
|
} |
398 |
|
|
399 |
|
|
400 |
< |
commonspot(sp1, sp2, org) /* set sp1 to intersection of sp1 and sp2 */ |
401 |
< |
register SPOT *sp1, *sp2; |
402 |
< |
FVECT org; |
400 |
> |
int |
401 |
> |
commonspot( /* set sp1 to intersection of sp1 and sp2 */ |
402 |
> |
SPOT *sp1, |
403 |
> |
SPOT *sp2, |
404 |
> |
FVECT org |
405 |
> |
) |
406 |
|
{ |
407 |
|
FVECT cent; |
408 |
|
double rad2, cos1, cos2; |
423 |
|
} |
424 |
|
|
425 |
|
|
426 |
< |
commonbeam(sp1, sp2, dir) /* set sp1 to intersection of sp1 and sp2 */ |
427 |
< |
register SPOT *sp1, *sp2; |
428 |
< |
FVECT dir; |
426 |
> |
int |
427 |
> |
commonbeam( /* set sp1 to intersection of sp1 and sp2 */ |
428 |
> |
SPOT *sp1, |
429 |
> |
SPOT *sp2, |
430 |
> |
FVECT dir |
431 |
> |
) |
432 |
|
{ |
433 |
|
FVECT cent, c1, c2; |
434 |
|
double rad2, d; |
352 |
– |
register int i; |
435 |
|
/* move centers to common plane */ |
436 |
|
d = DOT(sp1->aim, dir); |
437 |
< |
for (i = 0; i < 3; i++) |
356 |
< |
c1[i] = sp1->aim[i] - d*dir[i]; |
437 |
> |
VSUM(c1, sp1->aim, dir, -d); |
438 |
|
d = DOT(sp2->aim, dir); |
439 |
< |
for (i = 0; i < 3; i++) |
359 |
< |
c2[i] = sp2->aim[i] - d*dir[i]; |
439 |
> |
VSUM(c2, sp2->aim, dir, -d); |
440 |
|
/* compute overlap */ |
441 |
|
rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI); |
442 |
|
if (rad2 <= FTINY) |
447 |
|
} |
448 |
|
|
449 |
|
|
450 |
< |
checkspot(sp, nrm) /* check spotlight for behind source */ |
451 |
< |
register SPOT *sp; /* spotlight */ |
452 |
< |
FVECT nrm; /* source surface normal */ |
450 |
> |
int |
451 |
> |
checkspot( /* check spotlight for behind source */ |
452 |
> |
SPOT *sp, /* spotlight */ |
453 |
> |
FVECT nrm /* source surface normal */ |
454 |
> |
) |
455 |
|
{ |
456 |
|
double d, d1; |
457 |
|
|
465 |
|
|
466 |
|
|
467 |
|
double |
468 |
< |
spotdisk(oc, op, sp, pos) /* intersect spot with object op */ |
469 |
< |
FVECT oc; |
470 |
< |
OBJREC *op; |
471 |
< |
register SPOT *sp; |
472 |
< |
FVECT pos; |
468 |
> |
spotdisk( /* intersect spot with object op */ |
469 |
> |
FVECT oc, |
470 |
> |
OBJREC *op, |
471 |
> |
SPOT *sp, |
472 |
> |
FVECT pos |
473 |
> |
) |
474 |
|
{ |
475 |
|
FVECT onorm; |
476 |
|
double offs, d, dist; |
394 |
– |
register int i; |
477 |
|
|
478 |
|
offs = getplaneq(onorm, op); |
479 |
|
d = -DOT(onorm, sp->aim); |
482 |
|
dist = (DOT(pos, onorm) - offs)/d; |
483 |
|
if (dist < 0.) |
484 |
|
return(0.); |
485 |
< |
for (i = 0; i < 3; i++) |
404 |
< |
oc[i] = pos[i] + dist*sp->aim[i]; |
485 |
> |
VSUM(oc, pos, sp->aim, dist); |
486 |
|
return(sp->siz*dist*dist/PI/(d*d)); |
487 |
|
} |
488 |
|
|
489 |
|
|
490 |
|
double |
491 |
< |
beamdisk(oc, op, sp, dir) /* intersect beam with object op */ |
492 |
< |
FVECT oc; |
493 |
< |
OBJREC *op; |
494 |
< |
register SPOT *sp; |
495 |
< |
FVECT dir; |
491 |
> |
beamdisk( /* intersect beam with object op */ |
492 |
> |
FVECT oc, |
493 |
> |
OBJREC *op, |
494 |
> |
SPOT *sp, |
495 |
> |
FVECT dir |
496 |
> |
) |
497 |
|
{ |
498 |
|
FVECT onorm; |
499 |
|
double offs, d, dist; |
418 |
– |
register int i; |
500 |
|
|
501 |
|
offs = getplaneq(onorm, op); |
502 |
|
d = -DOT(onorm, dir); |
503 |
|
if (d >= -FTINY && d <= FTINY) |
504 |
|
return(0.); |
505 |
|
dist = (DOT(sp->aim, onorm) - offs)/d; |
506 |
< |
for (i = 0; i < 3; i++) |
426 |
< |
oc[i] = sp->aim[i] + dist*dir[i]; |
506 |
> |
VSUM(oc, sp->aim, dir, dist); |
507 |
|
return(sp->siz/PI/(d*d)); |
508 |
|
} |
509 |
|
|
510 |
|
|
511 |
|
double |
512 |
< |
intercircle(cc, c1, c2, r1s, r2s) /* intersect two circles */ |
513 |
< |
FVECT cc; /* midpoint (return value) */ |
514 |
< |
FVECT c1, c2; /* circle centers */ |
515 |
< |
double r1s, r2s; /* radii squared */ |
512 |
> |
intercircle( /* intersect two circles */ |
513 |
> |
FVECT cc, /* midpoint (return value) */ |
514 |
> |
FVECT c1, /* circle centers */ |
515 |
> |
FVECT c2, |
516 |
> |
double r1s, /* radii squared */ |
517 |
> |
double r2s |
518 |
> |
) |
519 |
|
{ |
520 |
|
double a2, d2, l; |
521 |
|
FVECT disp; |
439 |
– |
register int i; |
522 |
|
|
523 |
< |
for (i = 0; i < 3; i++) |
442 |
< |
disp[i] = c2[i] - c1[i]; |
523 |
> |
VSUB(disp, c2, c1); |
524 |
|
d2 = DOT(disp,disp); |
525 |
|
/* circle within overlap? */ |
526 |
|
if (r1s < r2s) { |
540 |
|
return(0.); |
541 |
|
/* overlap, compute center */ |
542 |
|
l = sqrt((r1s - a2)/d2); |
543 |
< |
for (i = 0; i < 3; i++) |
463 |
< |
cc[i] = c1[i] + l*disp[i]; |
543 |
> |
VSUM(cc, c1, disp, l); |
544 |
|
return(a2); |
465 |
– |
} |
466 |
– |
|
467 |
– |
|
468 |
– |
sourcehit(r) /* check to see if ray hit distant source */ |
469 |
– |
register RAY *r; |
470 |
– |
{ |
471 |
– |
int first, last; |
472 |
– |
register int i; |
473 |
– |
|
474 |
– |
if (r->rsrc >= 0) { /* check only one if aimed */ |
475 |
– |
first = last = r->rsrc; |
476 |
– |
} else { /* otherwise check all */ |
477 |
– |
first = 0; last = nsources-1; |
478 |
– |
} |
479 |
– |
for (i = first; i <= last; i++) |
480 |
– |
if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT) |
481 |
– |
/* |
482 |
– |
* Check to see if ray is within |
483 |
– |
* solid angle of source. |
484 |
– |
*/ |
485 |
– |
if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir)) |
486 |
– |
<= source[i].ss2) { |
487 |
– |
r->ro = source[i].so; |
488 |
– |
if (!(source[i].sflags & SSKIP)) |
489 |
– |
break; |
490 |
– |
} |
491 |
– |
|
492 |
– |
if (r->ro != NULL) { |
493 |
– |
for (i = 0; i < 3; i++) |
494 |
– |
r->ron[i] = -r->rdir[i]; |
495 |
– |
r->rod = 1.0; |
496 |
– |
r->rox = NULL; |
497 |
– |
return(1); |
498 |
– |
} |
499 |
– |
return(0); |
500 |
– |
} |
501 |
– |
|
502 |
– |
|
503 |
– |
/**************************************************************** |
504 |
– |
* The following macros were separated from the m_light() routine |
505 |
– |
* because they are very nasty and difficult to understand. |
506 |
– |
*/ |
507 |
– |
|
508 |
– |
/* wrongillum * |
509 |
– |
* |
510 |
– |
* We cannot allow an illum to pass to another illum, because that |
511 |
– |
* would almost certainly constitute overcounting. |
512 |
– |
* However, we do allow an illum to pass to another illum |
513 |
– |
* that is actually going to relay to a virtual light source. |
514 |
– |
*/ |
515 |
– |
|
516 |
– |
#define wrongillum(m, r) (!(source[r->rsrc].sflags&SVIRTUAL) && \ |
517 |
– |
objptr(source[r->rsrc].so->omod)->otype==MAT_ILLUM) |
518 |
– |
|
519 |
– |
/* wrongsource * |
520 |
– |
* |
521 |
– |
* This source is the wrong source (ie. overcounted) if we are |
522 |
– |
* aimed to a different source than the one we hit and the one |
523 |
– |
* we hit is not an illum which should be passed. |
524 |
– |
*/ |
525 |
– |
|
526 |
– |
#define wrongsource(m, r) (r->rsrc>=0 && source[r->rsrc].so!=r->ro && \ |
527 |
– |
(m->otype!=MAT_ILLUM || wrongillum(m,r))) |
528 |
– |
|
529 |
– |
/* distglow * |
530 |
– |
* |
531 |
– |
* A distant glow is an object that sometimes acts as a light source, |
532 |
– |
* but is too far away from the test point to be one in this case. |
533 |
– |
*/ |
534 |
– |
|
535 |
– |
#define distglow(m, r) (m->otype==MAT_GLOW && \ |
536 |
– |
r->rot > m->oargs.farg[3]) |
537 |
– |
|
538 |
– |
/* badambient * |
539 |
– |
* |
540 |
– |
* We must avoid including counting light sources in the ambient calculation, |
541 |
– |
* since the direct component is handled separately. Therefore, any |
542 |
– |
* ambient ray which hits an active light source must be discarded. |
543 |
– |
*/ |
544 |
– |
|
545 |
– |
#define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \ |
546 |
– |
!distglow(m, r)) |
547 |
– |
|
548 |
– |
/* passillum * |
549 |
– |
* |
550 |
– |
* An illum passes to another material type when we didn't hit it |
551 |
– |
* on purpose (as part of a direct calculation), or it is relaying |
552 |
– |
* a virtual light source. |
553 |
– |
*/ |
554 |
– |
|
555 |
– |
#define passillum(m, r) (m->otype==MAT_ILLUM && \ |
556 |
– |
(r->rsrc<0 || source[r->rsrc].so!=r->ro || \ |
557 |
– |
source[r->rsrc].sflags&SVIRTUAL)) |
558 |
– |
|
559 |
– |
/* srcignore * |
560 |
– |
* |
561 |
– |
* The -di flag renders light sources invisible, and here is the test. |
562 |
– |
*/ |
563 |
– |
|
564 |
– |
#define srcignore(m, r) (directinvis && !(r->crtype&SHADOW) && \ |
565 |
– |
!distglow(m, r)) |
566 |
– |
|
567 |
– |
|
568 |
– |
m_light(m, r) /* ray hit a light source */ |
569 |
– |
register OBJREC *m; |
570 |
– |
register RAY *r; |
571 |
– |
{ |
572 |
– |
/* check for over-counting */ |
573 |
– |
if (wrongsource(m, r) || badambient(m, r)) |
574 |
– |
return; |
575 |
– |
/* check for passed illum */ |
576 |
– |
if (passillum(m, r)) { |
577 |
– |
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
578 |
– |
raytrans(r); |
579 |
– |
else |
580 |
– |
rayshade(r, modifier(m->oargs.sarg[0])); |
581 |
– |
return; |
582 |
– |
} |
583 |
– |
/* otherwise treat as source */ |
584 |
– |
/* check for behind */ |
585 |
– |
if (r->rod < 0.0) |
586 |
– |
return; |
587 |
– |
/* check for invisibility */ |
588 |
– |
if (srcignore(m, r)) |
589 |
– |
return; |
590 |
– |
/* get distribution pattern */ |
591 |
– |
raytexture(r, m->omod); |
592 |
– |
/* get source color */ |
593 |
– |
setcolor(r->rcol, m->oargs.farg[0], |
594 |
– |
m->oargs.farg[1], |
595 |
– |
m->oargs.farg[2]); |
596 |
– |
/* modify value */ |
597 |
– |
multcolor(r->rcol, r->pcol); |
545 |
|
} |