ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/srcsupp.c
Revision: 1.4
Committed: Fri Jun 21 16:44:01 1991 UTC (32 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.3: +99 -1 lines
Log Message:
moved some routines here from virtuals.c

File Contents

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