ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/srcsupp.c
(Generate patch)

Comparing ray/src/rt/srcsupp.c (file contents):
Revision 1.12 by greg, Mon Aug 26 10:11:08 1991 UTC vs.
Revision 2.9 by greg, Sat Feb 22 02:07:29 2003 UTC

# Line 1 | Line 1
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 + /* ====================================================================
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 + */
66 +
67   #include  "ray.h"
68  
69   #include  "otypes.h"
# Line 18 | Line 74 | static char SCCSid[] = "$SunId$ LBL";
74  
75   #include  "face.h"
76  
77 + #define SRCINC          4               /* realloc increment for array */
78  
79   SRCREC  *source = NULL;                 /* our list of sources */
80   int  nsources = 0;                      /* the number of sources */
# Line 25 | Line 82 | int  nsources = 0;                     /* the number of sources */
82   SRCFUNC  sfun[NUMOTYPE];                /* source dispatch table */
83  
84  
85 + void
86   initstypes()                    /* initialize source dispatch table */
87   {
88          extern VSMATERIAL  mirror_vs, direct1_vs, direct2_vs;
89 <        extern int  fsetsrc(), ssetsrc(), sphsetsrc(), rsetsrc();
90 <        extern double  fgetplaneq(), rgetplaneq();
91 <        extern double  fgetmaxdisk(), rgetmaxdisk();
92 <        static SOBJECT  fsobj = {fsetsrc, fgetplaneq, fgetmaxdisk};
93 <        static SOBJECT  ssobj = {ssetsrc};
36 <        static SOBJECT  sphsobj = {sphsetsrc};
37 <        static SOBJECT  rsobj = {rsetsrc, rgetplaneq, rgetmaxdisk};
89 >        static SOBJECT  fsobj = {fsetsrc, flatpart, fgetplaneq, fgetmaxdisk};
90 >        static SOBJECT  ssobj = {ssetsrc, nopart};
91 >        static SOBJECT  sphsobj = {sphsetsrc, nopart};
92 >        static SOBJECT  cylsobj = {cylsetsrc, cylpart};
93 >        static SOBJECT  rsobj = {rsetsrc, flatpart, rgetplaneq, rgetmaxdisk};
94  
95          sfun[MAT_MIRROR].mf = &mirror_vs;
96          sfun[MAT_DIRECT1].mf = &direct1_vs;
# Line 42 | Line 98 | initstypes()                   /* initialize source dispatch table */
98          sfun[OBJ_FACE].of = &fsobj;
99          sfun[OBJ_SOURCE].of = &ssobj;
100          sfun[OBJ_SPHERE].of = &sphsobj;
101 +        sfun[OBJ_CYLINDER].of = &cylsobj;
102          sfun[OBJ_RING].of = &rsobj;
103   }
104  
# Line 50 | Line 107 | int
107   newsource()                     /* allocate new source in our array */
108   {
109          if (nsources == 0)
110 <                source = (SRCREC *)malloc(sizeof(SRCREC));
111 <        else
110 >                source = (SRCREC *)malloc(SRCINC*sizeof(SRCREC));
111 >        else if (nsources%SRCINC == 0)
112                  source = (SRCREC *)realloc((char *)source,
113 <                                (unsigned)(nsources+1)*sizeof(SRCREC));
113 >                                (unsigned)(nsources+SRCINC)*sizeof(SRCREC));
114          if (source == NULL)
115                  return(-1);
116          source[nsources].sflags = 0;
# Line 63 | Line 120 | newsource()                    /* allocate new source in our array */
120   }
121  
122  
123 + void
124 + setflatss(src)                          /* set sampling for a flat source */
125 + register SRCREC  *src;
126 + {
127 +        double  mult;
128 +        register int  i;
129 +
130 +        src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0;
131 +        for (i = 0; i < 3; i++)
132 +                if (src->snorm[i] < 0.6 && src->snorm[i] > -0.6)
133 +                        break;
134 +        src->ss[SV][i] = 1.0;
135 +        fcross(src->ss[SU], src->ss[SV], src->snorm);
136 +        mult = .5 * sqrt( src->ss2 / DOT(src->ss[SU],src->ss[SU]) );
137 +        for (i = 0; i < 3; i++)
138 +                src->ss[SU][i] *= mult;
139 +        fcross(src->ss[SV], src->snorm, src->ss[SU]);
140 + }
141 +
142 +
143 + void
144   fsetsrc(src, so)                        /* set a face as a source */
145   register SRCREC  *src;
146   OBJREC  *so;
147   {
148          register FACE  *f;
149          register int  i, j;
150 +        double  d;
151          
152          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
153          src->so = so;
# Line 85 | Line 164 | OBJREC  *so;
164                  objerror(so, USER, "cannot hit center");
165          src->sflags |= SFLAT;
166          VCOPY(src->snorm, f->norm);
88        src->ss = sqrt(f->area / PI);
167          src->ss2 = f->area;
168 +                                                /* find maximum radius */
169 +        src->srad = 0.;
170 +        for (i = 0; i < f->nv; i++) {
171 +                d = dist2(VERTEX(f,i), src->sloc);
172 +                if (d > src->srad)
173 +                        src->srad = d;
174 +        }
175 +        src->srad = sqrt(src->srad);
176 +                                                /* compute size vectors */
177 +        if (f->nv == 4)                         /* parallelogram case */
178 +                for (j = 0; j < 3; j++) {
179 +                        src->ss[SU][j] = .5*(VERTEX(f,1)[j]-VERTEX(f,0)[j]);
180 +                        src->ss[SV][j] = .5*(VERTEX(f,3)[j]-VERTEX(f,0)[j]);
181 +                }
182 +        else
183 +                setflatss(src);
184   }
185  
186  
187 + void
188   ssetsrc(src, so)                        /* set a source as a source */
189   register SRCREC  *src;
190   register OBJREC  *so;
# Line 107 | Line 202 | register OBJREC  *so;
202          theta = PI/180.0/2.0 * so->oargs.farg[3];
203          if (theta <= FTINY)
204                  objerror(so, USER, "zero size");
110        src->ss = theta >= PI/4.0 ? 1.0 : tan(theta);
205          src->ss2 = 2.0*PI * (1.0 - cos(theta));
206 +                                        /* the following is approximate */
207 +        src->srad = sqrt(src->ss2/PI);
208 +        VCOPY(src->snorm, src->sloc);
209 +        setflatss(src);                 /* hey, whatever works */
210 +        src->ss[SW][0] = src->ss[SW][1] = src->ss[SW][2] = 0.0;
211   }
212  
213  
214 + void
215   sphsetsrc(src, so)                      /* set a sphere as a source */
216   register SRCREC  *src;
217   register OBJREC  *so;
218   {
219 +        register int  i;
220 +
221          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
222          src->so = so;
223          if (so->oargs.nfargs != 4)
# Line 123 | Line 225 | register OBJREC  *so;
225          if (so->oargs.farg[3] <= FTINY)
226                  objerror(so, USER, "illegal radius");
227          VCOPY(src->sloc, so->oargs.farg);
228 <        src->ss = so->oargs.farg[3];
229 <        src->ss2 = PI * src->ss * src->ss;
228 >        src->srad = so->oargs.farg[3];
229 >        src->ss2 = PI * src->srad * src->srad;
230 >        for (i = 0; i < 3; i++)
231 >                src->ss[SU][i] = src->ss[SV][i] = src->ss[SW][i] = 0.0;
232 >        for (i = 0; i < 3; i++)
233 >                src->ss[i][i] = .7236 * so->oargs.farg[3];
234   }
235  
236  
237 + void
238   rsetsrc(src, so)                        /* set a ring (disk) as a source */
239   register SRCREC  *src;
240   OBJREC  *so;
# Line 143 | Line 250 | OBJREC  *so;
250                  objerror(so, USER, "cannot hit center");
251          src->sflags |= SFLAT;
252          VCOPY(src->snorm, co->ad);
253 <        src->ss = CO_R1(co);
254 <        src->ss2 = PI * src->ss * src->ss;
253 >        src->srad = CO_R1(co);
254 >        src->ss2 = PI * src->srad * src->srad;
255 >        setflatss(src);
256   }
257  
258  
259 + void
260 + cylsetsrc(src, so)                      /* set a cylinder as a source */
261 + register SRCREC  *src;
262 + OBJREC  *so;
263 + {
264 +        register CONE  *co;
265 +        register int  i;
266 +        
267 +        src->sa.success = 4*AIMREQT-1;          /* bitch on fourth failure */
268 +        src->so = so;
269 +                                                /* get the cylinder */
270 +        co = getcone(so, 0);
271 +        if (CO_R0(co) > .2*co->al)              /* heuristic constraint */
272 +                objerror(so, WARNING, "source aspect too small");
273 +        src->sflags |= SCYL;
274 +        for (i = 0; i < 3; i++)
275 +                src->sloc[i] = .5 * (CO_P1(co)[i] + CO_P0(co)[i]);
276 +        src->srad = .5*co->al;
277 +        src->ss2 = 2.*CO_R0(co)*co->al;
278 +                                                /* set sampling vectors */
279 +        for (i = 0; i < 3; i++)
280 +                src->ss[SU][i] = .5 * co->al * co->ad[i];
281 +        src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0;
282 +        for (i = 0; i < 3; i++)
283 +                if (co->ad[i] < 0.6 && co->ad[i] > -0.6)
284 +                        break;
285 +        src->ss[SV][i] = 1.0;
286 +        fcross(src->ss[SW], src->ss[SV], co->ad);
287 +        normalize(src->ss[SW]);
288 +        for (i = 0; i < 3; i++)
289 +                src->ss[SW][i] *= .8559 * CO_R0(co);
290 +        fcross(src->ss[SV], src->ss[SW], co->ad);
291 + }
292 +
293 +
294   SPOT *
295   makespot(m)                     /* make a spotlight */
296   register OBJREC  *m;
297   {
298          register SPOT  *ns;
299  
300 +        if ((ns = (SPOT *)m->os) != NULL)
301 +                return(ns);
302          if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
303                  return(NULL);
304          ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
305          VCOPY(ns->aim, m->oargs.farg+4);
306          if ((ns->flen = normalize(ns->aim)) == 0.0)
307                  objerror(m, USER, "zero focus vector");
308 +        m->os = (char *)ns;
309          return(ns);
310   }
311  
312  
313 + int
314 + spotout(r, s)                   /* check if we're outside spot region */
315 + register RAY  *r;
316 + register SPOT  *s;
317 + {
318 +        double  d;
319 +        FVECT  vd;
320 +        
321 +        if (s == NULL)
322 +                return(0);
323 +        if (s->flen < -FTINY) {         /* distant source */
324 +                vd[0] = s->aim[0] - r->rorg[0];
325 +                vd[1] = s->aim[1] - r->rorg[1];
326 +                vd[2] = s->aim[2] - r->rorg[2];
327 +                d = DOT(r->rdir,vd);
328 +                /*                      wrong side?
329 +                if (d <= FTINY)
330 +                        return(1);      */
331 +                d = DOT(vd,vd) - d*d;
332 +                if (PI*d > s->siz)
333 +                        return(1);      /* out */
334 +                return(0);      /* OK */
335 +        }
336 +                                        /* local source */
337 +        if (s->siz < 2.0*PI * (1.0 + DOT(s->aim,r->rdir)))
338 +                return(1);      /* out */
339 +        return(0);      /* OK */
340 + }
341 +
342 +
343   double
344   fgetmaxdisk(ocent, op)          /* get center and squared radius of face */
345   FVECT  ocent;
# Line 235 | Line 411 | OBJREC  *op;
411   }
412  
413  
414 + int
415   commonspot(sp1, sp2, org)       /* set sp1 to intersection of sp1 and sp2 */
416   register SPOT  *sp1, *sp2;
417   FVECT  org;
# Line 258 | Line 435 | FVECT  org;
435   }
436  
437  
438 + int
439   commonbeam(sp1, sp2, dir)       /* set sp1 to intersection of sp1 and sp2 */
440   register SPOT  *sp1, *sp2;
441   FVECT  dir;
# Line 282 | Line 460 | FVECT  dir;
460   }
461  
462  
463 + int
464   checkspot(sp, nrm)              /* check spotlight for behind source */
465   register SPOT  *sp;     /* spotlight */
466   FVECT  nrm;             /* source surface normal */
# Line 377 | Line 556 | double  r1s, r2s;              /* radii squared */
556          for (i = 0; i < 3; i++)
557                  cc[i] = c1[i] + l*disp[i];
558          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|SVIRTUAL)) == 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)      (r->rsrc>=0 && \
419                                source[r->rsrc].so!=r->ro && \
420                                (m->otype!=MAT_ILLUM || \
421                        objptr(source[r->rsrc].so->omod)->otype==MAT_ILLUM))
422
423 #define  distglow(m, r)         (m->otype==MAT_GLOW && \
424                                r->rot > m->oargs.farg[3])
425
426 #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
427                                !distglow(m, r))
428
429 #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
430                                !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
431
432 #define  srcignore(m, r)        (directinvis && !(r->crtype&SHADOW) && \
433                                !distglow(m, r))
434
435
436 m_light(m, r)                   /* ray hit a light source */
437 register OBJREC  *m;
438 register RAY  *r;
439 {
440                                                /* check for over-counting */
441        if (wrongsource(m, r) || badambient(m, r))
442                return;
443                                                /* check for passed illum */
444        if (passillum(m, r)) {
445                if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
446                        raytrans(r);
447                else
448                        rayshade(r, modifier(m->oargs.sarg[0]));
449                return;
450        }
451                                        /* otherwise treat as source */
452                                                /* check for behind */
453        if (r->rod < 0.0)
454                return;
455                                                /* check for invisibility */
456        if (srcignore(m, r))
457                return;
458                                                /* get distribution pattern */
459        raytexture(r, m->omod);
460                                                /* get source color */
461        setcolor(r->rcol, m->oargs.farg[0],
462                          m->oargs.farg[1],
463                          m->oargs.farg[2]);
464                                                /* modify value */
465        multcolor(r->rcol, r->pcol);
559   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines