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

Comparing ray/src/rt/source.c (file contents):
Revision 1.32 by greg, Fri Jun 14 14:58:49 1991 UTC vs.
Revision 1.35 by greg, Thu Jun 20 13:43:29 1991 UTC

# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "octree.h"
16  
17 #include  "source.h"
18
17   #include  "otypes.h"
18  
19 < #include  "cone.h"
19 > #include  "source.h"
20  
23 #include  "face.h"
24
21   #include  "random.h"
22  
23 + /*
24 + * Structures used by direct()
25 + */
26  
27 < extern double  dstrsrc;                 /* source distribution amount */
28 < extern double  shadthresh;              /* relative shadow threshold */
29 < extern double  shadcert;                /* shadow testing certainty */
27 > typedef struct {
28 >        FVECT  dir;             /* source direction */
29 >        COLOR  coef;            /* material coefficient */
30 >        COLOR  val;             /* contribution */
31 > }  CONTRIB;             /* direct contribution */
32  
33 < SRCREC  *source = NULL;                 /* our list of sources */
34 < int  nsources = 0;                      /* the number of sources */
33 > typedef struct {
34 >        int  sno;               /* source number */
35 >        float  brt;             /* brightness (for comparison) */
36 > }  CNTPTR;              /* contribution pointer */
37  
38   static CONTRIB  *srccnt;                /* source contributions in direct() */
39   static CNTPTR  *cntord;                 /* source ordering in direct() */
# Line 38 | Line 41 | static CNTPTR  *cntord;                        /* source ordering in direct
41  
42   marksources()                   /* find and mark source objects */
43   {
44 +        int  i;
45          register OBJREC  *o, *m;
46 <        register int  i;
47 <
46 >        register SRCREC  *ns;
47 >                                        /* initialize dispatch table */
48 >        initstypes();
49 >                                        /* find direct sources */
50          for (i = 0; i < nobjects; i++) {
51          
52                  o = objptr(i);
53  
54 <                if (o->omod == OVOID)
54 >                if (!issurface(o->otype) || o->omod == OVOID)
55                          continue;
56  
57                  m = objptr(o->omod);
# Line 62 | Line 68 | marksources()                  /* find and mark source objects */
68                                  m->oargs.farg[3] <= FTINY)
69                          continue;                       /* don't bother */
70  
71 <                if (source == NULL)
72 <                        source = (SRCREC *)malloc(sizeof(SRCREC));
73 <                else
74 <                        source = (SRCREC *)realloc((char *)source,
75 <                                        (unsigned)(nsources+1)*sizeof(SRCREC));
70 <                if (source == NULL)
71 >                if (sfun[o->otype].of == NULL ||
72 >                                sfun[o->otype].of->setsrc == NULL)
73 >                        objerror(o, USER, "illegal material");
74 >
75 >                if ((ns = newsource()) == NULL)
76                          goto memerr;
77  
78 <                newsource(&source[nsources], o);
78 >                (*sfun[o->otype].of->setsrc)(ns, o);
79  
80                  if (m->otype == MAT_GLOW) {
81 <                        source[nsources].sflags |= SPROX;
82 <                        source[nsources].sl.prox = m->oargs.farg[3];
81 >                        ns->sflags |= SPROX;
82 >                        ns->sl.prox = m->oargs.farg[3];
83                          if (o->otype == OBJ_SOURCE)
84 <                                source[nsources].sflags |= SSKIP;
84 >                                ns->sflags |= SSKIP;
85                  } else if (m->otype == MAT_SPOT) {
86 <                        source[nsources].sflags |= SSPOT;
87 <                        source[nsources].sl.s = makespot(m);
86 >                        ns->sflags |= SSPOT;
87 >                        if ((ns->sl.s = makespot(m)) == NULL)
88 >                                goto memerr;
89                  }
84                nsources++;
90          }
91          if (nsources <= 0) {
92                  error(WARNING, "no light sources found");
93                  return;
94          }
95 +        markvirtuals();                 /* find and add virtual sources */
96          srccnt = (CONTRIB *)malloc(nsources*sizeof(CONTRIB));
97          cntord = (CNTPTR *)malloc(nsources*sizeof(CNTPTR));
98          if (srccnt != NULL && cntord != NULL)
99 <                return;
100 <        /* fall through */
99 >                goto memerr;
100 >        return;
101   memerr:
102          error(SYSTEM, "out of memory in marksources");
103   }
104  
105  
100 newsource(src, so)                      /* add a source to the array */
101 register SRCREC  *src;
102 register OBJREC  *so;
103 {
104        double  cos(), tan(), sqrt();
105        double  theta;
106        FACE  *f;
107        CONE  *co;
108        int  j;
109        register int  i;
110        
111        src->sflags = 0;
112        src->aimsuccess = 2*AIMREQT-1;          /* bitch on second failure */
113        src->nhits = 1; src->ntests = 2;        /* start probability = 1/2 */
114        src->so = so;
115
116        switch (so->otype) {
117        case OBJ_SOURCE:
118                if (so->oargs.nfargs != 4)
119                        objerror(so, USER, "bad arguments");
120                src->sflags |= SDISTANT;
121                VCOPY(src->sloc, so->oargs.farg);
122                if (normalize(src->sloc) == 0.0)
123                        objerror(so, USER, "zero direction");
124                theta = PI/180.0/2.0 * so->oargs.farg[3];
125                if (theta <= FTINY)
126                        objerror(so, USER, "zero size");
127                src->ss = theta >= PI/4 ? 1.0 : tan(theta);
128                src->ss2 = 2.0*PI * (1.0 - cos(theta));
129                break;
130        case OBJ_SPHERE:
131                VCOPY(src->sloc, so->oargs.farg);
132                src->ss = so->oargs.farg[3];
133                src->ss2 = PI * src->ss * src->ss;
134                break;
135        case OBJ_FACE:
136                                                /* get the face */
137                f = getface(so);
138                                                /* find the center */
139                for (j = 0; j < 3; j++) {
140                        src->sloc[j] = 0.0;
141                        for (i = 0; i < f->nv; i++)
142                                src->sloc[j] += VERTEX(f,i)[j];
143                        src->sloc[j] /= f->nv;
144                }
145                if (!inface(src->sloc, f))
146                        objerror(so, USER, "cannot hit center");
147                src->ss = sqrt(f->area / PI);
148                src->ss2 = f->area;
149                break;
150        case OBJ_RING:
151                                                /* get the ring */
152                co = getcone(so, 0);
153                VCOPY(src->sloc, CO_P0(co));
154                if (CO_R0(co) > 0.0)
155                        objerror(so, USER, "cannot hit center");
156                src->ss = CO_R1(co);
157                src->ss2 = PI * src->ss * src->ss;
158                break;
159        default:
160                objerror(so, USER, "illegal material");
161        }
162 }
163
164
165 SPOT *
166 makespot(m)                     /* make a spotlight */
167 register OBJREC  *m;
168 {
169        extern double  cos();
170        register SPOT  *ns;
171
172        if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
173                error(SYSTEM, "out of memory in makespot");
174        ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
175        VCOPY(ns->aim, m->oargs.farg+4);
176        if ((ns->flen = normalize(ns->aim)) == 0.0)
177                objerror(m, USER, "zero focus vector");
178        return(ns);
179 }
180
181
106   double
107   srcray(sr, r, sn)               /* send a ray to a source, return domega */
108   register RAY  *sr;              /* returned source ray */
109   RAY  *r;                        /* ray which hit object */
110   register int  sn;               /* source number */
111   {
188        register double  *norm = NULL;  /* plane normal */
112          double  ddot;                   /* (distance times) cosine */
113          FVECT  vd;
114          double  d;
# Line 198 | Line 121 | register int  sn;              /* source number */
121  
122          sr->rsrc = sn;                          /* remember source */
123                                                  /* get source direction */
124 <        if (source[sn].sflags & SDISTANT)
124 >        if (source[sn].sflags & SDISTANT) {
125 >                if (source[sn].sflags & SSPOT) {        /* check location */
126 >                        for (i = 0; i < 3; i++)
127 >                                vd[i] = sr->rorg[i] - source[sn].sl.s->aim[i];
128 >                        d = DOT(source[sn].sloc,vd);
129 >                        d = DOT(vd,vd) - d*d;
130 >                        if (PI*d > source[sn].sl.s->siz)
131 >                                return(0.0);
132 >                }
133                                                  /* constant direction */
134                  VCOPY(sr->rdir, source[sn].sloc);
135 <        else {                                  /* compute direction */
135 >        } else {                                /* compute direction */
136                  for (i = 0; i < 3; i++)
137                          sr->rdir[i] = source[sn].sloc[i] - sr->rorg[i];
138  
139 <                if (source[sn].so->otype == OBJ_FACE)
140 <                        norm = getface(source[sn].so)->norm;
210 <                else if (source[sn].so->otype == OBJ_RING)
211 <                        norm = getcone(source[sn].so,0)->ad;
212 <
213 <                if (norm != NULL && (ddot = -DOT(sr->rdir, norm)) <= FTINY)
139 >                if (source[sn].sflags & SFLAT &&
140 >                        (ddot = -DOT(sr->rdir, source[sn].snorm)) <= FTINY)
141                          return(0.0);            /* behind surface! */
142          }
143          if (dstrsrc > FTINY) {
# Line 222 | Line 149 | register int  sn;              /* source number */
149                  (1.0 - 2.0*urand(ilhash(dimlist,ndims+1)+samplendx));
150                  }
151                  ndims--;
152 <                if (norm != NULL) {             /* project offset */
153 <                        d = DOT(vd, norm);
152 >                if (source[sn].sflags & SFLAT) {        /* project offset */
153 >                        d = DOT(vd, source[sn].snorm);
154                          for (i = 0; i < 3; i++)
155 <                                vd[i] -= d * norm[i];
155 >                                vd[i] -= d * source[sn].snorm[i];
156                  }
157                  for (i = 0; i < 3; i++)         /* offset source direction */
158                          sr->rdir[i] += vd[i];
# Line 247 | Line 174 | register int  sn;              /* source number */
174                          d > source[sn].sl.prox)
175                  return(0.0);
176                                                  /* compute dot product */
177 <        if (norm != NULL)
177 >        if (source[sn].sflags & SFLAT)
178                  ddot /= d;
179          else
180                  ddot = 1.0;
# Line 263 | Line 190 | register int  sn;              /* source number */
190   }
191  
192  
193 < sourcehit(r)                    /* check to see if ray hit distant source */
194 < register RAY  *r;
193 > srcvalue(r)                     /* punch ray to source and compute value */
194 > RAY  *r;
195   {
196 <        int  first, last;
270 <        register int  i;
196 >        register SRCREC  *sp;
197  
198 <        if (r->rsrc >= 0) {             /* check only one if aimed */
199 <                first = last = r->rsrc;
200 <        } else {                        /* otherwise check all */
201 <                first = 0; last = nsources-1;
198 >        sp = &source[r->rsrc];
199 >        if (sp->sflags & SVIRTUAL) {    /* virtual source */
200 >                                        /* check intersection */
201 >                if (!(*ofun[sp->so->otype].funp)(sp->so, r))
202 >                        return;
203 >                raycont(r);             /* compute contribution */
204 >                return;
205          }
206 <        for (i = first; i <= last; i++)
207 <                if (source[i].sflags & SDISTANT)
208 <                        /*
209 <                         * Check to see if ray is within
210 <                         * solid angle of source.
211 <                         */
212 <                        if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
284 <                                        <= source[i].ss2) {
285 <                                r->ro = source[i].so;
286 <                                if (!(source[i].sflags & SSKIP))
287 <                                        break;
288 <                        }
289 <
290 <        if (r->ro != NULL) {
291 <                for (i = 0; i < 3; i++)
292 <                        r->ron[i] = -r->rdir[i];
293 <                r->rod = 1.0;
294 <                r->rox = NULL;
295 <                return(1);
206 >                                        /* compute intersection */
207 >        if (sp->sflags & SDISTANT ? sourcehit(r) :
208 >                        (*ofun[sp->so->otype].funp)(sp->so, r)) {
209 >                if (sp->sa.success >= 0)
210 >                        sp->sa.success++;
211 >                raycont(r);             /* compute contribution */
212 >                return;
213          }
214 <        return(0);
214 >        if (sp->sa.success < 0)
215 >                return;                 /* bitched already */
216 >        sp->sa.success -= AIMREQT;
217 >        if (sp->sa.success >= 0)
218 >                return;                 /* leniency */
219 >        sprintf(errmsg, "aiming failure for light source \"%s\"",
220 >                        sp->so->oname);
221 >        error(WARNING, errmsg);         /* issue warning */
222   }
223  
224  
# Line 319 | Line 243 | char  *p;                      /* data for f */
243          register int  sn;
244          int  nshadcheck, ncnts;
245          int  nhits;
246 <        double  prob, ourthresh, hwt;
246 >        double  dom, prob, ourthresh, hwt;
247          RAY  sr;
248                          /* NOTE: srccnt and cntord global so no recursion */
249          if (nsources <= 0)
# Line 333 | Line 257 | char  *p;                      /* data for f */
257                  cntord[sn].sno = sn;
258                  cntord[sn].brt = 0.0;
259                                                  /* get source ray */
260 <                if ((srccnt[sn].dom = srcray(&sr, r, sn)) == 0.0)
260 >                if ((dom = srcray(&sr, r, sn)) == 0.0)
261                          continue;
262                  VCOPY(srccnt[sn].dir, sr.rdir);
263                                                  /* compute coefficient */
264 <                (*f)(srccnt[sn].val, p, srccnt[sn].dir, srccnt[sn].dom);
265 <                cntord[sn].brt = bright(srccnt[sn].val);
264 >                (*f)(srccnt[sn].coef, p, srccnt[sn].dir, dom);
265 >                cntord[sn].brt = bright(srccnt[sn].coef);
266                  if (cntord[sn].brt <= 0.0)
267                          continue;
268 <                                                /* compute intersection */
269 <                if (source[sn].sflags & SDISTANT ?
270 <                                sourcehit(&sr) :
271 <                                (*ofun[source[sn].so->otype].funp)
272 <                                (source[sn].so, &sr)) {
349 <                        if (source[sn].aimsuccess >= 0)
350 <                                source[sn].aimsuccess++;
351 <                } else {
352 <                        cntord[sn].brt = 0.0;
353 <                        if (source[sn].aimsuccess < 0)
354 <                                continue;       /* bitched already */
355 <                        source[sn].aimsuccess -= AIMREQT;
356 <                        if (source[sn].aimsuccess >= 0)
357 <                                continue;       /* leniency */
358 <                        sprintf(errmsg,
359 <                                "aiming failure for light source \"%s\"",
360 <                                        source[sn].so->oname);
361 <                        error(WARNING, errmsg);
362 <                        continue;
363 <                }
364 <                                                /* compute contribution */
365 <                raycont(&sr);
366 <                multcolor(srccnt[sn].val, sr.rcol);
268 >                                                /* compute potential */
269 >                sr.revf = srcvalue;
270 >                rayvalue(&sr);
271 >                copycolor(srccnt[sn].val, sr.rcol);
272 >                multcolor(srccnt[sn].val, srccnt[sn].coef);
273                  cntord[sn].brt = bright(srccnt[sn].val);
274          }
275                                                  /* sort contributions */
# Line 398 | Line 304 | char  *p;                      /* data for f */
304                  VCOPY(sr.rdir, srccnt[cntord[sn].sno].dir);
305                  sr.rsrc = cntord[sn].sno;
306                  if (localhit(&sr, &thescene) &&
307 <                                sr.ro != source[cntord[sn].sno].so) {
308 <                                                /* check for transmission */
307 >                                ( sr.ro != source[cntord[sn].sno].so ||
308 >                                source[cntord[sn].sno].sflags & SFOLLOW )) {
309 >                                                /* follow entire path */
310                          raycont(&sr);
311                          if (bright(sr.rcol) <= FTINY)
312                                  continue;       /* missed! */
313 <                        (*f)(srccnt[cntord[sn].sno].val, p,
314 <                                        srccnt[cntord[sn].sno].dir,
315 <                                        srccnt[cntord[sn].sno].dom);
409 <                        multcolor(srccnt[cntord[sn].sno].val, sr.rcol);
313 >                        copycolor(srccnt[cntord[sn].sno].val, sr.rcol);
314 >                        multcolor(srccnt[cntord[sn].sno].val,
315 >                                        srccnt[cntord[sn].sno].coef);
316                  }
317                                                  /* add contribution if hit */
318                  addcolor(r->rcol, srccnt[cntord[sn].sno].val);
# Line 429 | Line 335 | char  *p;                      /* data for f */
335                                  (double)source[cntord[sn].sno].ntests;
336                  scalecolor(srccnt[cntord[sn].sno].val, prob);
337                  addcolor(r->rcol, srccnt[cntord[sn].sno].val);
432        }
433 }
434
435
436 #define  wrongsource(m, r)      (m->otype!=MAT_ILLUM && \
437                                r->rsrc>=0 && \
438                                source[r->rsrc].so!=r->ro)
439
440 #define  badambient(m, r)       ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
441                                !(r->rtype&REFLECTED) &&        /* hack! */\
442                                !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
443
444 #define  passillum(m, r)        (m->otype==MAT_ILLUM && \
445                                !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
446
447
448 m_light(m, r)                   /* ray hit a light source */
449 register OBJREC  *m;
450 register RAY  *r;
451 {
452                                                /* check for over-counting */
453        if (wrongsource(m, r) || badambient(m, r))
454                return;
455                                                /* check for passed illum */
456        if (passillum(m, r)) {
457
458                if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
459                        raytrans(r);
460                else
461                        rayshade(r, modifier(m->oargs.sarg[0]));
462
463                                                /* otherwise treat as source */
464        } else {
465                                                /* check for behind */
466                if (r->rod < 0.0)
467                        return;
468                                                /* get distribution pattern */
469                raytexture(r, m->omod);
470                                                /* get source color */
471                setcolor(r->rcol, m->oargs.farg[0],
472                                  m->oargs.farg[1],
473                                  m->oargs.farg[2]);
474                                                /* modify value */
475                multcolor(r->rcol, r->pcol);
338          }
339   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines