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.13 by greg, Fri Sep 13 13:44:10 1991 UTC vs.
Revision 2.7 by greg, Thu Aug 24 20:56:08 1995 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1995 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include  "face.h"
20  
21 + #define SRCINC          4               /* realloc increment for array */
22  
23   SRCREC  *source = NULL;                 /* our list of sources */
24   int  nsources = 0;                      /* the number of sources */
# Line 28 | Line 29 | SRCFUNC  sfun[NUMOTYPE];               /* source dispatch table */
29   initstypes()                    /* initialize source dispatch table */
30   {
31          extern VSMATERIAL  mirror_vs, direct1_vs, direct2_vs;
32 <        extern int  fsetsrc(), ssetsrc(), sphsetsrc(), rsetsrc();
32 >        extern int  fsetsrc(), ssetsrc(), sphsetsrc(), cylsetsrc(), rsetsrc();
33 >        extern int  nopart(), flatpart(), cylpart();
34          extern double  fgetplaneq(), rgetplaneq();
35          extern double  fgetmaxdisk(), rgetmaxdisk();
36 <        static SOBJECT  fsobj = {fsetsrc, fgetplaneq, fgetmaxdisk};
37 <        static SOBJECT  ssobj = {ssetsrc};
38 <        static SOBJECT  sphsobj = {sphsetsrc};
39 <        static SOBJECT  rsobj = {rsetsrc, rgetplaneq, rgetmaxdisk};
36 >        static SOBJECT  fsobj = {fsetsrc, flatpart, fgetplaneq, fgetmaxdisk};
37 >        static SOBJECT  ssobj = {ssetsrc, nopart};
38 >        static SOBJECT  sphsobj = {sphsetsrc, nopart};
39 >        static SOBJECT  cylsobj = {cylsetsrc, cylpart};
40 >        static SOBJECT  rsobj = {rsetsrc, flatpart, rgetplaneq, rgetmaxdisk};
41  
42          sfun[MAT_MIRROR].mf = &mirror_vs;
43          sfun[MAT_DIRECT1].mf = &direct1_vs;
# Line 42 | Line 45 | initstypes()                   /* initialize source dispatch table */
45          sfun[OBJ_FACE].of = &fsobj;
46          sfun[OBJ_SOURCE].of = &ssobj;
47          sfun[OBJ_SPHERE].of = &sphsobj;
48 +        sfun[OBJ_CYLINDER].of = &cylsobj;
49          sfun[OBJ_RING].of = &rsobj;
50   }
51  
# Line 49 | Line 53 | initstypes()                   /* initialize source dispatch table */
53   int
54   newsource()                     /* allocate new source in our array */
55   {
52 #define SRCINC  4
56          if (nsources == 0)
57                  source = (SRCREC *)malloc(SRCINC*sizeof(SRCREC));
58          else if (nsources%SRCINC == 0)
# Line 61 | Line 64 | newsource()                    /* allocate new source in our array */
64          source[nsources].nhits = 1;
65          source[nsources].ntests = 2;    /* initial hit probability = 1/2 */
66          return(nsources++);
64 #undef SRCINC
67   }
68  
69  
70 + setflatss(src)                          /* set sampling for a flat source */
71 + register SRCREC  *src;
72 + {
73 +        double  mult;
74 +        register int  i;
75 +
76 +        src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0;
77 +        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++)
84 +                src->ss[SU][i] *= mult;
85 +        fcross(src->ss[SV], src->snorm, src->ss[SU]);
86 + }
87 +
88 +
89   fsetsrc(src, so)                        /* set a face as a source */
90   register SRCREC  *src;
91   OBJREC  *so;
92   {
93          register FACE  *f;
94          register int  i, j;
95 +        double  d;
96          
97          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
98          src->so = so;
# Line 87 | Line 109 | OBJREC  *so;
109                  objerror(so, USER, "cannot hit center");
110          src->sflags |= SFLAT;
111          VCOPY(src->snorm, f->norm);
90        src->ss = sqrt(f->area / PI);
112          src->ss2 = f->area;
113 +                                                /* find maximum radius */
114 +        src->srad = 0.;
115 +        for (i = 0; i < f->nv; i++) {
116 +                d = dist2(VERTEX(f,i), src->sloc);
117 +                if (d > src->srad)
118 +                        src->srad = d;
119 +        }
120 +        src->srad = sqrt(src->srad);
121 +                                                /* compute size vectors */
122 +        if (f->nv == 4)                         /* parallelogram case */
123 +                for (j = 0; j < 3; j++) {
124 +                        src->ss[SU][j] = .5*(VERTEX(f,1)[j]-VERTEX(f,0)[j]);
125 +                        src->ss[SV][j] = .5*(VERTEX(f,3)[j]-VERTEX(f,0)[j]);
126 +                }
127 +        else
128 +                setflatss(src);
129   }
130  
131  
# Line 109 | Line 146 | register OBJREC  *so;
146          theta = PI/180.0/2.0 * so->oargs.farg[3];
147          if (theta <= FTINY)
148                  objerror(so, USER, "zero size");
112        src->ss = theta >= PI/4.0 ? 1.0 : tan(theta);
149          src->ss2 = 2.0*PI * (1.0 - cos(theta));
150 +                                        /* the following is approximate */
151 +        src->srad = sqrt(src->ss2/PI);
152 +        VCOPY(src->snorm, src->sloc);
153 +        setflatss(src);                 /* hey, whatever works */
154 +        src->ss[SW][0] = src->ss[SW][1] = src->ss[SW][2] = 0.0;
155   }
156  
157  
# Line 118 | Line 159 | sphsetsrc(src, so)                     /* set a sphere as a source */
159   register SRCREC  *src;
160   register OBJREC  *so;
161   {
162 +        register int  i;
163 +
164          src->sa.success = 2*AIMREQT-1;          /* bitch on second failure */
165          src->so = so;
166          if (so->oargs.nfargs != 4)
# Line 125 | Line 168 | register OBJREC  *so;
168          if (so->oargs.farg[3] <= FTINY)
169                  objerror(so, USER, "illegal radius");
170          VCOPY(src->sloc, so->oargs.farg);
171 <        src->ss = so->oargs.farg[3];
172 <        src->ss2 = PI * src->ss * src->ss;
171 >        src->srad = so->oargs.farg[3];
172 >        src->ss2 = PI * src->srad * src->srad;
173 >        for (i = 0; i < 3; i++)
174 >                src->ss[SU][i] = src->ss[SV][i] = src->ss[SW][i] = 0.0;
175 >        for (i = 0; i < 3; i++)
176 >                src->ss[i][i] = .7236 * so->oargs.farg[3];
177   }
178  
179  
# Line 145 | Line 192 | OBJREC  *so;
192                  objerror(so, USER, "cannot hit center");
193          src->sflags |= SFLAT;
194          VCOPY(src->snorm, co->ad);
195 <        src->ss = CO_R1(co);
196 <        src->ss2 = PI * src->ss * src->ss;
195 >        src->srad = CO_R1(co);
196 >        src->ss2 = PI * src->srad * src->srad;
197 >        setflatss(src);
198   }
199  
200  
201 + cylsetsrc(src, so)                      /* set a cylinder as a source */
202 + register SRCREC  *src;
203 + OBJREC  *so;
204 + {
205 +        register CONE  *co;
206 +        register int  i;
207 +        
208 +        src->sa.success = 4*AIMREQT-1;          /* bitch on fourth failure */
209 +        src->so = so;
210 +                                                /* get the cylinder */
211 +        co = getcone(so, 0);
212 +        if (CO_R0(co) > .2*co->al)              /* heuristic constraint */
213 +                objerror(so, WARNING, "source aspect too small");
214 +        src->sflags |= SCYL;
215 +        for (i = 0; i < 3; i++)
216 +                src->sloc[i] = .5 * (CO_P1(co)[i] + CO_P0(co)[i]);
217 +        src->srad = .5*co->al;
218 +        src->ss2 = 2.*CO_R0(co)*co->al;
219 +                                                /* set sampling vectors */
220 +        for (i = 0; i < 3; i++)
221 +                src->ss[SU][i] = .5 * co->al * co->ad[i];
222 +        src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0;
223 +        for (i = 0; i < 3; i++)
224 +                if (co->ad[i] < 0.6 && co->ad[i] > -0.6)
225 +                        break;
226 +        src->ss[SV][i] = 1.0;
227 +        fcross(src->ss[SW], src->ss[SV], co->ad);
228 +        normalize(src->ss[SW]);
229 +        for (i = 0; i < 3; i++)
230 +                src->ss[SW][i] *= .8559 * CO_R0(co);
231 +        fcross(src->ss[SV], src->ss[SW], co->ad);
232 + }
233 +
234 +
235   SPOT *
236   makespot(m)                     /* make a spotlight */
237   register OBJREC  *m;
238   {
239          register SPOT  *ns;
240  
241 +        if ((ns = (SPOT *)m->os) != NULL)
242 +                return(ns);
243          if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
244                  return(NULL);
245          ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
246          VCOPY(ns->aim, m->oargs.farg+4);
247          if ((ns->flen = normalize(ns->aim)) == 0.0)
248                  objerror(m, USER, "zero focus vector");
249 +        m->os = (char *)ns;
250          return(ns);
251   }
252  
253  
254 + spotout(r, s, dist)             /* check if we're outside spot region */
255 + register RAY  *r;
256 + register SPOT  *s;
257 + int  dist;
258 + {
259 +        double  d;
260 +        FVECT  vd;
261 +        
262 +        if (s == NULL)
263 +                return(0);
264 +        if (dist) {                     /* distant source */
265 +                vd[0] = s->aim[0] - r->rorg[0];
266 +                vd[1] = s->aim[1] - r->rorg[1];
267 +                vd[2] = s->aim[2] - r->rorg[2];
268 +                d = DOT(r->rdir,vd);
269 +                /*                      wrong side?
270 +                if (d <= FTINY)
271 +                        return(1);      */
272 +                d = DOT(vd,vd) - d*d;
273 +                if (PI*d > s->siz)
274 +                        return(1);      /* out */
275 +                return(0);      /* OK */
276 +        }
277 +                                        /* local source */
278 +        if (s->siz < 2.0*PI * (1.0 + DOT(s->aim,r->rdir)))
279 +                return(1);      /* out */
280 +        return(0);      /* OK */
281 + }
282 +
283 +
284   double
285   fgetmaxdisk(ocent, op)          /* get center and squared radius of face */
286   FVECT  ocent;
# Line 379 | Line 494 | double  r1s, r2s;              /* radii squared */
494          for (i = 0; i < 3; i++)
495                  cc[i] = c1[i] + l*disp[i];
496          return(a2);
382 }
383
384
385 sourcehit(r)                    /* check to see if ray hit distant source */
386 register RAY  *r;
387 {
388        int  first, last;
389        register int  i;
390
391        if (r->rsrc >= 0) {             /* check only one if aimed */
392                first = last = r->rsrc;
393        } else {                        /* otherwise check all */
394                first = 0; last = nsources-1;
395        }
396        for (i = first; i <= last; i++)
397                if ((source[i].sflags & (SDISTANT|SVIRTUAL)) == SDISTANT)
398                        /*
399                         * Check to see if ray is within
400                         * solid angle of source.
401                         */
402                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
403                                        <= source[i].ss2) {
404                                r->ro = source[i].so;
405                                if (!(source[i].sflags & SSKIP))
406                                        break;
407                        }
408
409        if (r->ro != NULL) {
410                for (i = 0; i < 3; i++)
411                        r->ron[i] = -r->rdir[i];
412                r->rod = 1.0;
413                r->rox = NULL;
414                return(1);
415        }
416        return(0);
417 }
418
419
420 #define  wrongsource(m, r)      (r->rsrc>=0 && \
421                                source[r->rsrc].so!=r->ro && \
422                                (m->otype!=MAT_ILLUM || \
423                        objptr(source[r->rsrc].so->omod)->otype==MAT_ILLUM))
424
425 #define  distglow(m, r)         (m->otype==MAT_GLOW && \
426                                r->rot > m->oargs.farg[3])
427
428 #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
429                                !distglow(m, r))
430
431 #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
432                                !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
433
434 #define  srcignore(m, r)        (directinvis && !(r->crtype&SHADOW) && \
435                                !distglow(m, r))
436
437
438 m_light(m, r)                   /* ray hit a light source */
439 register OBJREC  *m;
440 register RAY  *r;
441 {
442                                                /* check for over-counting */
443        if (wrongsource(m, r) || badambient(m, r))
444                return;
445                                                /* check for passed illum */
446        if (passillum(m, r)) {
447                if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
448                        raytrans(r);
449                else
450                        rayshade(r, modifier(m->oargs.sarg[0]));
451                return;
452        }
453                                        /* otherwise treat as source */
454                                                /* check for behind */
455        if (r->rod < 0.0)
456                return;
457                                                /* check for invisibility */
458        if (srcignore(m, r))
459                return;
460                                                /* get distribution pattern */
461        raytexture(r, m->omod);
462                                                /* get source color */
463        setcolor(r->rcol, m->oargs.farg[0],
464                          m->oargs.farg[1],
465                          m->oargs.farg[2]);
466                                                /* modify value */
467        multcolor(r->rcol, r->pcol);
497   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines