ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/srcsupp.c
Revision: 1.3
Committed: Fri Jun 21 13:23:05 1991 UTC (32 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +2 -0 lines
Log Message:
added test for zero area face in fgetdisk()

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     extern double cos();
154     register SPOT *ns;
155    
156     if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
157     return(NULL);
158     ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
159     VCOPY(ns->aim, m->oargs.farg+4);
160     if ((ns->flen = normalize(ns->aim)) == 0.0)
161     objerror(m, USER, "zero focus vector");
162     return(ns);
163     }
164    
165    
166     double
167     fgetmaxdisk(ocent, op) /* get center and squared radius of face */
168     FVECT ocent;
169     OBJREC *op;
170     {
171     double maxrad2;
172     double d2;
173     register int i, j;
174     register FACE *f;
175    
176     f = getface(op);
177     for (i = 0; i < 3; i++) {
178     ocent[i] = 0.;
179     for (j = 0; j < f->nv; j++)
180     ocent[i] += VERTEX(f,j)[i];
181     ocent[i] /= (double)f->nv;
182     }
183 greg 1.3 if (f->area == 0.)
184     return(0.);
185 greg 1.1 maxrad2 = 0.;
186     for (j = 0; j < f->nv; j++) {
187     d2 = dist2(VERTEX(f,j), ocent);
188     if (d2 > maxrad2)
189     maxrad2 = d2;
190     }
191     return(maxrad2);
192     }
193    
194    
195     double
196     rgetmaxdisk(ocent, op) /* get center and squared radius of ring */
197     FVECT ocent;
198     OBJREC *op;
199     {
200     register CONE *co;
201    
202     co = getcone(op, 0);
203     VCOPY(ocent, CO_P0(co));
204     return(CO_R1(co)*CO_R1(co));
205     }
206    
207    
208     double
209     fgetplaneq(nvec, op) /* get plane equation for face */
210     FVECT nvec;
211     OBJREC *op;
212     {
213     register FACE *fo;
214    
215     fo = getface(op);
216     VCOPY(nvec, fo->norm);
217     return(fo->offset);
218     }
219    
220    
221     double
222     rgetplaneq(nvec, op) /* get plane equation for ring */
223     FVECT nvec;
224     OBJREC *op;
225     {
226     register CONE *co;
227    
228     co = getcone(op, 0);
229     VCOPY(nvec, co->ad);
230     return(DOT(nvec, CO_P0(co)));
231     }
232    
233    
234     sourcehit(r) /* check to see if ray hit distant source */
235     register RAY *r;
236     {
237     int first, last;
238     register int i;
239    
240     if (r->rsrc >= 0) { /* check only one if aimed */
241     first = last = r->rsrc;
242     } else { /* otherwise check all */
243     first = 0; last = nsources-1;
244     }
245     for (i = first; i <= last; i++)
246     if (source[i].sflags & SDISTANT)
247     /*
248     * Check to see if ray is within
249     * solid angle of source.
250     */
251     if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
252     <= source[i].ss2) {
253     r->ro = source[i].so;
254     if (!(source[i].sflags & SSKIP))
255     break;
256     }
257    
258     if (r->ro != NULL) {
259     for (i = 0; i < 3; i++)
260     r->ron[i] = -r->rdir[i];
261     r->rod = 1.0;
262     r->rox = NULL;
263     return(1);
264     }
265     return(0);
266     }
267    
268    
269     #define wrongsource(m, r) (m->otype!=MAT_ILLUM && \
270     r->rsrc>=0 && \
271     source[r->rsrc].so!=r->ro)
272    
273     #define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
274     !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
275    
276     #define passillum(m, r) (m->otype==MAT_ILLUM && \
277     !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
278    
279    
280     m_light(m, r) /* ray hit a light source */
281     register OBJREC *m;
282     register RAY *r;
283     {
284     /* check for over-counting */
285     if (wrongsource(m, r) || badambient(m, r))
286     return;
287     /* check for passed illum */
288     if (passillum(m, r)) {
289    
290     if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
291     raytrans(r);
292     else
293     rayshade(r, modifier(m->oargs.sarg[0]));
294    
295     /* otherwise treat as source */
296     } else {
297     /* check for behind */
298     if (r->rod < 0.0)
299     return;
300     /* get distribution pattern */
301     raytexture(r, m->omod);
302     /* get source color */
303     setcolor(r->rcol, m->oargs.farg[0],
304     m->oargs.farg[1],
305     m->oargs.farg[2]);
306     /* modify value */
307     multcolor(r->rcol, r->pcol);
308     }
309     }