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 |
* Support routines for source objects and materials |
9 |
*/ |
10 |
|
11 |
#include "ray.h" |
12 |
|
13 |
#include "otypes.h" |
14 |
|
15 |
#include "source.h" |
16 |
|
17 |
#include "cone.h" |
18 |
|
19 |
#include "face.h" |
20 |
|
21 |
|
22 |
SRCREC *source = NULL; /* our list of sources */ |
23 |
int nsources = 0; /* the number of sources */ |
24 |
|
25 |
SRCFUNC sfun[NUMOTYPE]; /* source dispatch table */ |
26 |
|
27 |
|
28 |
initstypes() /* initialize source dispatch table */ |
29 |
{ |
30 |
extern VSMATERIAL mirror_vs, direct1_vs, direct2_vs; |
31 |
extern int fsetsrc(), ssetsrc(), sphsetsrc(), rsetsrc(); |
32 |
extern double fgetplaneq(), rgetplaneq(); |
33 |
extern double fgetmaxdisk(), rgetmaxdisk(); |
34 |
static SOBJECT fsobj = {fsetsrc, fgetplaneq, fgetmaxdisk}; |
35 |
static SOBJECT ssobj = {ssetsrc}; |
36 |
static SOBJECT sphsobj = {sphsetsrc}; |
37 |
static SOBJECT rsobj = {rsetsrc, rgetplaneq, rgetmaxdisk}; |
38 |
|
39 |
sfun[MAT_MIRROR].mf = &mirror_vs; |
40 |
sfun[MAT_DIRECT1].mf = &direct1_vs; |
41 |
sfun[MAT_DIRECT2].mf = &direct2_vs; |
42 |
sfun[OBJ_FACE].of = &fsobj; |
43 |
sfun[OBJ_SOURCE].of = &ssobj; |
44 |
sfun[OBJ_SPHERE].of = &sphsobj; |
45 |
sfun[OBJ_RING].of = &rsobj; |
46 |
} |
47 |
|
48 |
|
49 |
int |
50 |
newsource() /* allocate new source in our array */ |
51 |
{ |
52 |
if (nsources == 0) |
53 |
source = (SRCREC *)malloc(sizeof(SRCREC)); |
54 |
else |
55 |
source = (SRCREC *)realloc((char *)source, |
56 |
(unsigned)(nsources+1)*sizeof(SRCREC)); |
57 |
if (source == NULL) |
58 |
return(-1); |
59 |
source[nsources].sflags = 0; |
60 |
source[nsources].nhits = 1; |
61 |
source[nsources].ntests = 2; /* initial hit probability = 1/2 */ |
62 |
return(nsources++); |
63 |
} |
64 |
|
65 |
|
66 |
fsetsrc(src, so) /* set a face as a source */ |
67 |
register SRCREC *src; |
68 |
OBJREC *so; |
69 |
{ |
70 |
register FACE *f; |
71 |
register int i, j; |
72 |
|
73 |
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
74 |
src->so = so; |
75 |
/* get the face */ |
76 |
f = getface(so); |
77 |
/* find the center */ |
78 |
for (j = 0; j < 3; j++) { |
79 |
src->sloc[j] = 0.0; |
80 |
for (i = 0; i < f->nv; i++) |
81 |
src->sloc[j] += VERTEX(f,i)[j]; |
82 |
src->sloc[j] /= (double)f->nv; |
83 |
} |
84 |
if (!inface(src->sloc, f)) |
85 |
objerror(so, USER, "cannot hit center"); |
86 |
src->sflags |= SFLAT; |
87 |
VCOPY(src->snorm, f->norm); |
88 |
src->ss = sqrt(f->area / PI); |
89 |
src->ss2 = f->area; |
90 |
} |
91 |
|
92 |
|
93 |
ssetsrc(src, so) /* set a source as a source */ |
94 |
register SRCREC *src; |
95 |
register OBJREC *so; |
96 |
{ |
97 |
double theta; |
98 |
|
99 |
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
100 |
src->so = so; |
101 |
if (so->oargs.nfargs != 4) |
102 |
objerror(so, USER, "bad arguments"); |
103 |
src->sflags |= SDISTANT; |
104 |
VCOPY(src->sloc, so->oargs.farg); |
105 |
if (normalize(src->sloc) == 0.0) |
106 |
objerror(so, USER, "zero direction"); |
107 |
theta = PI/180.0/2.0 * so->oargs.farg[3]; |
108 |
if (theta <= FTINY) |
109 |
objerror(so, USER, "zero size"); |
110 |
src->ss = theta >= PI/4.0 ? 1.0 : tan(theta); |
111 |
src->ss2 = 2.0*PI * (1.0 - cos(theta)); |
112 |
} |
113 |
|
114 |
|
115 |
sphsetsrc(src, so) /* set a sphere as a source */ |
116 |
register SRCREC *src; |
117 |
register OBJREC *so; |
118 |
{ |
119 |
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
120 |
src->so = so; |
121 |
if (so->oargs.nfargs != 4) |
122 |
objerror(so, USER, "bad # arguments"); |
123 |
if (so->oargs.farg[3] <= FTINY) |
124 |
objerror(so, USER, "illegal radius"); |
125 |
VCOPY(src->sloc, so->oargs.farg); |
126 |
src->ss = so->oargs.farg[3]; |
127 |
src->ss2 = PI * src->ss * src->ss; |
128 |
} |
129 |
|
130 |
|
131 |
rsetsrc(src, so) /* set a ring (disk) as a source */ |
132 |
register SRCREC *src; |
133 |
OBJREC *so; |
134 |
{ |
135 |
register CONE *co; |
136 |
|
137 |
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
138 |
src->so = so; |
139 |
/* get the ring */ |
140 |
co = getcone(so, 0); |
141 |
VCOPY(src->sloc, CO_P0(co)); |
142 |
if (CO_R0(co) > 0.0) |
143 |
objerror(so, USER, "cannot hit center"); |
144 |
src->sflags |= SFLAT; |
145 |
VCOPY(src->snorm, co->ad); |
146 |
src->ss = CO_R1(co); |
147 |
src->ss2 = PI * src->ss * src->ss; |
148 |
} |
149 |
|
150 |
|
151 |
SPOT * |
152 |
makespot(m) /* make a spotlight */ |
153 |
register OBJREC *m; |
154 |
{ |
155 |
register SPOT *ns; |
156 |
|
157 |
if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
158 |
return(NULL); |
159 |
ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3])); |
160 |
VCOPY(ns->aim, m->oargs.farg+4); |
161 |
if ((ns->flen = normalize(ns->aim)) == 0.0) |
162 |
objerror(m, USER, "zero focus vector"); |
163 |
return(ns); |
164 |
} |
165 |
|
166 |
|
167 |
double |
168 |
fgetmaxdisk(ocent, op) /* get center and squared radius of face */ |
169 |
FVECT ocent; |
170 |
OBJREC *op; |
171 |
{ |
172 |
double maxrad2; |
173 |
double d; |
174 |
register int i, j; |
175 |
register FACE *f; |
176 |
|
177 |
f = getface(op); |
178 |
if (f->area == 0.) |
179 |
return(0.); |
180 |
for (i = 0; i < 3; i++) { |
181 |
ocent[i] = 0.; |
182 |
for (j = 0; j < f->nv; j++) |
183 |
ocent[i] += VERTEX(f,j)[i]; |
184 |
ocent[i] /= (double)f->nv; |
185 |
} |
186 |
d = DOT(ocent,f->norm); |
187 |
for (i = 0; i < 3; i++) |
188 |
ocent[i] += (f->offset - d)*f->norm[i]; |
189 |
maxrad2 = 0.; |
190 |
for (j = 0; j < f->nv; j++) { |
191 |
d = dist2(VERTEX(f,j), ocent); |
192 |
if (d > maxrad2) |
193 |
maxrad2 = d; |
194 |
} |
195 |
return(maxrad2); |
196 |
} |
197 |
|
198 |
|
199 |
double |
200 |
rgetmaxdisk(ocent, op) /* get center and squared radius of ring */ |
201 |
FVECT ocent; |
202 |
OBJREC *op; |
203 |
{ |
204 |
register CONE *co; |
205 |
|
206 |
co = getcone(op, 0); |
207 |
VCOPY(ocent, CO_P0(co)); |
208 |
return(CO_R1(co)*CO_R1(co)); |
209 |
} |
210 |
|
211 |
|
212 |
double |
213 |
fgetplaneq(nvec, op) /* get plane equation for face */ |
214 |
FVECT nvec; |
215 |
OBJREC *op; |
216 |
{ |
217 |
register FACE *fo; |
218 |
|
219 |
fo = getface(op); |
220 |
VCOPY(nvec, fo->norm); |
221 |
return(fo->offset); |
222 |
} |
223 |
|
224 |
|
225 |
double |
226 |
rgetplaneq(nvec, op) /* get plane equation for ring */ |
227 |
FVECT nvec; |
228 |
OBJREC *op; |
229 |
{ |
230 |
register CONE *co; |
231 |
|
232 |
co = getcone(op, 0); |
233 |
VCOPY(nvec, co->ad); |
234 |
return(DOT(nvec, CO_P0(co))); |
235 |
} |
236 |
|
237 |
|
238 |
commonspot(sp1, sp2, org) /* set sp1 to intersection of sp1 and sp2 */ |
239 |
register SPOT *sp1, *sp2; |
240 |
FVECT org; |
241 |
{ |
242 |
FVECT cent; |
243 |
double rad2, cos1, cos2; |
244 |
|
245 |
cos1 = 1. - sp1->siz/(2.*PI); |
246 |
cos2 = 1. - sp2->siz/(2.*PI); |
247 |
if (sp2->siz >= 2.*PI-FTINY) /* BIG, just check overlap */ |
248 |
return(DOT(sp1->aim,sp2->aim) >= cos1*cos2 - |
249 |
sqrt((1.-cos1*cos1)*(1.-cos2*cos2))); |
250 |
/* compute and check disks */ |
251 |
rad2 = intercircle(cent, sp1->aim, sp2->aim, |
252 |
1./(cos1*cos1) - 1., 1./(cos2*cos2) - 1.); |
253 |
if (rad2 <= FTINY || normalize(cent) == 0.) |
254 |
return(0); |
255 |
VCOPY(sp1->aim, cent); |
256 |
sp1->siz = 2.*PI*(1. - 1./sqrt(1.+rad2)); |
257 |
return(1); |
258 |
} |
259 |
|
260 |
|
261 |
commonbeam(sp1, sp2, dir) /* set sp1 to intersection of sp1 and sp2 */ |
262 |
register SPOT *sp1, *sp2; |
263 |
FVECT dir; |
264 |
{ |
265 |
FVECT cent, c1, c2; |
266 |
double rad2, d; |
267 |
register int i; |
268 |
/* move centers to common plane */ |
269 |
d = DOT(sp1->aim, dir); |
270 |
for (i = 0; i < 3; i++) |
271 |
c1[i] = sp1->aim[i] - d*dir[i]; |
272 |
d = DOT(sp2->aim, dir); |
273 |
for (i = 0; i < 3; i++) |
274 |
c2[i] = sp2->aim[i] - d*dir[i]; |
275 |
/* compute overlap */ |
276 |
rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI); |
277 |
if (rad2 <= FTINY) |
278 |
return(0); |
279 |
VCOPY(sp1->aim, cent); |
280 |
sp1->siz = PI*rad2; |
281 |
return(1); |
282 |
} |
283 |
|
284 |
|
285 |
checkspot(sp, nrm) /* check spotlight for behind source */ |
286 |
register SPOT *sp; /* spotlight */ |
287 |
FVECT nrm; /* source surface normal */ |
288 |
{ |
289 |
double d, d1; |
290 |
|
291 |
d = DOT(sp->aim, nrm); |
292 |
if (d > FTINY) /* center in front? */ |
293 |
return(1); |
294 |
/* else check horizon */ |
295 |
d1 = 1. - sp->siz/(2.*PI); |
296 |
return(1.-FTINY-d*d < d1*d1); |
297 |
} |
298 |
|
299 |
|
300 |
double |
301 |
spotdisk(oc, op, sp, pos) /* intersect spot with object op */ |
302 |
FVECT oc; |
303 |
OBJREC *op; |
304 |
register SPOT *sp; |
305 |
FVECT pos; |
306 |
{ |
307 |
FVECT onorm; |
308 |
double offs, d, dist; |
309 |
register int i; |
310 |
|
311 |
offs = getplaneq(onorm, op); |
312 |
d = -DOT(onorm, sp->aim); |
313 |
if (d >= -FTINY && d <= FTINY) |
314 |
return(0.); |
315 |
dist = (DOT(pos, onorm) - offs)/d; |
316 |
if (dist < 0.) |
317 |
return(0.); |
318 |
for (i = 0; i < 3; i++) |
319 |
oc[i] = pos[i] + dist*sp->aim[i]; |
320 |
return(sp->siz*dist*dist/PI/(d*d)); |
321 |
} |
322 |
|
323 |
|
324 |
double |
325 |
beamdisk(oc, op, sp, dir) /* intersect beam with object op */ |
326 |
FVECT oc; |
327 |
OBJREC *op; |
328 |
register SPOT *sp; |
329 |
FVECT dir; |
330 |
{ |
331 |
FVECT onorm; |
332 |
double offs, d, dist; |
333 |
register int i; |
334 |
|
335 |
offs = getplaneq(onorm, op); |
336 |
d = -DOT(onorm, dir); |
337 |
if (d >= -FTINY && d <= FTINY) |
338 |
return(0.); |
339 |
dist = (DOT(sp->aim, onorm) - offs)/d; |
340 |
for (i = 0; i < 3; i++) |
341 |
oc[i] = sp->aim[i] + dist*dir[i]; |
342 |
return(sp->siz/PI/(d*d)); |
343 |
} |
344 |
|
345 |
|
346 |
double |
347 |
intercircle(cc, c1, c2, r1s, r2s) /* intersect two circles */ |
348 |
FVECT cc; /* midpoint (return value) */ |
349 |
FVECT c1, c2; /* circle centers */ |
350 |
double r1s, r2s; /* radii squared */ |
351 |
{ |
352 |
double a2, d2, l; |
353 |
FVECT disp; |
354 |
register int i; |
355 |
|
356 |
for (i = 0; i < 3; i++) |
357 |
disp[i] = c2[i] - c1[i]; |
358 |
d2 = DOT(disp,disp); |
359 |
/* circle within overlap? */ |
360 |
if (r1s < r2s) { |
361 |
if (r2s >= r1s + d2) { |
362 |
VCOPY(cc, c1); |
363 |
return(r1s); |
364 |
} |
365 |
} else { |
366 |
if (r1s >= r2s + d2) { |
367 |
VCOPY(cc, c2); |
368 |
return(r2s); |
369 |
} |
370 |
} |
371 |
a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2); |
372 |
/* no overlap? */ |
373 |
if (a2 <= 0.) |
374 |
return(0.); |
375 |
/* overlap, compute center */ |
376 |
l = sqrt((r1s - a2)/d2); |
377 |
for (i = 0; i < 3; i++) |
378 |
cc[i] = c1[i] + l*disp[i]; |
379 |
return(a2); |
380 |
} |
381 |
|
382 |
|
383 |
sourcehit(r) /* check to see if ray hit distant source */ |
384 |
register RAY *r; |
385 |
{ |
386 |
int first, last; |
387 |
register int i; |
388 |
|
389 |
if (r->rsrc >= 0) { /* check only one if aimed */ |
390 |
first = last = r->rsrc; |
391 |
} else { /* otherwise check all */ |
392 |
first = 0; last = nsources-1; |
393 |
} |
394 |
for (i = first; i <= last; i++) |
395 |
if (source[i].sflags & SDISTANT) |
396 |
/* |
397 |
* Check to see if ray is within |
398 |
* solid angle of source. |
399 |
*/ |
400 |
if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir)) |
401 |
<= source[i].ss2) { |
402 |
r->ro = source[i].so; |
403 |
if (!(source[i].sflags & SSKIP)) |
404 |
break; |
405 |
} |
406 |
|
407 |
if (r->ro != NULL) { |
408 |
for (i = 0; i < 3; i++) |
409 |
r->ron[i] = -r->rdir[i]; |
410 |
r->rod = 1.0; |
411 |
r->rox = NULL; |
412 |
return(1); |
413 |
} |
414 |
return(0); |
415 |
} |
416 |
|
417 |
|
418 |
#define wrongsource(m, r) (m->otype!=MAT_ILLUM && \ |
419 |
r->rsrc>=0 && \ |
420 |
source[r->rsrc].so!=r->ro) |
421 |
|
422 |
#define distglow(m, r) (m->otype==MAT_GLOW && \ |
423 |
r->rot > m->oargs.farg[3]) |
424 |
|
425 |
#define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \ |
426 |
!distglow(m, r)) |
427 |
|
428 |
#define passillum(m, r) (m->otype==MAT_ILLUM && \ |
429 |
!(r->rsrc>=0&&source[r->rsrc].so==r->ro)) |
430 |
|
431 |
#define srcignore(m, r) (directinvis && !(r->crtype&SHADOW) && \ |
432 |
!distglow(m, r)) |
433 |
|
434 |
|
435 |
m_light(m, r) /* ray hit a light source */ |
436 |
register OBJREC *m; |
437 |
register RAY *r; |
438 |
{ |
439 |
/* check for over-counting */ |
440 |
if (wrongsource(m, r) || badambient(m, r)) |
441 |
return; |
442 |
/* check for passed illum */ |
443 |
if (passillum(m, r)) { |
444 |
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
445 |
raytrans(r); |
446 |
else |
447 |
rayshade(r, modifier(m->oargs.sarg[0])); |
448 |
return; |
449 |
} |
450 |
/* otherwise treat as source */ |
451 |
/* check for behind */ |
452 |
if (r->rod < 0.0) |
453 |
return; |
454 |
/* check for invisibility */ |
455 |
if (srcignore(m, r)) |
456 |
return; |
457 |
/* get distribution pattern */ |
458 |
raytexture(r, m->omod); |
459 |
/* get source color */ |
460 |
setcolor(r->rcol, m->oargs.farg[0], |
461 |
m->oargs.farg[1], |
462 |
m->oargs.farg[2]); |
463 |
/* modify value */ |
464 |
multcolor(r->rcol, r->pcol); |
465 |
} |