ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/m_mist.c
Revision: 2.5
Committed: Thu Mar 21 10:24:59 1996 UTC (28 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +12 -7 lines
Log Message:
minor bug check fix in assigning leaving extinction coefficient

File Contents

# User Rev Content
1 greg 2.1 /* Copyright (c) 1995 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Mist volumetric material.
9     */
10    
11     #include "ray.h"
12    
13     #include "source.h"
14    
15     /*
16     * A mist volume is used to specify a region in the scene where a certain
17     * light source (or sources) is going to contribute to scattering. The
18     * material can add to the existing global medium, and override any ray
19     * settings for scattering albedo and eccentricity. Overlapping mist
20     * regions should agree w.r.t. albedo and eccentricity, and
21     * should have disjoint source lists.
22     *
23     * A pattern, if used, should compute the line integral of extinction,
24     * and will modify the first three arguments directly. This will tend
25     * to invalidate results when there are other objects intersected within
26     * the mist region.
27     *
28     * The string arguments for MAT_MIST are the identifiers for the important
29     * light sources, which will be looked up in the source array. The last
30     * source found matching a name is the one used. A relayed light source
31     * may be indicated by the relay surface name, followed by a '>' character,
32     * followed by the relayed source name (which may be another relay).
33     *
34     * Up to five real arguments may be given for MAT_MIST:
35     *
36     * [ext_r ext_g ext_b [albedo [gecc]]]
37     *
38     * The primaries indicate medium extinction per unit length (absorption
39 greg 2.5 * plus scattering), which is added to the global extinction coefficient, set
40     * by the -me option. The albedo is the ratio of scattering to extinction,
41 greg 2.1 * and is set globally by the -ma option (salbedo) and overridden here.
42     * The Heyney-Greenstein eccentricity parameter (-mg seccg) indicates how much
43     * scattering favors the forward direction. A value of 0 means isotropic
44     * scattering. A value approaching 1 indicates strong forward scattering.
45     */
46    
47     #define RELAYDELIM '>' /* relay delimiter character */
48    
49     extern COLOR cextinction; /* global coefficient of extinction */
50     extern double salbedo; /* global scattering albedo */
51     extern double seccg; /* global scattering eccentricity */
52    
53    
54     static int
55     inslist(sl, n) /* return index of source n if it's in list sl */
56     register int *sl;
57     register int n;
58     {
59     register int i;
60    
61     for (i = sl[0]; i > 0; i--)
62     if (sl[i] == n)
63     return(i);
64     return(0);
65     }
66    
67    
68     static int
69 greg 2.2 srcmatch(sp, id) /* check for an id match on a light source */
70     register SRCREC *sp;
71     register char *id;
72 greg 2.1 {
73     extern char *index();
74     register char *cp;
75 greg 2.2 /* check for relay sources */
76     while ((cp = index(id, RELAYDELIM)) != NULL) {
77     if (!(sp->sflags & SVIRTUAL) || sp->so == NULL)
78 greg 2.1 return(0);
79 greg 2.2 if (strncmp(id, sp->so->oname, cp-id) || sp->so->oname[cp-id])
80 greg 2.1 return(0);
81 greg 2.2 id = cp + 1; /* relay to next */
82     sp = source + sp->sa.sv.sn;
83 greg 2.1 }
84 greg 2.2 if (sp->sflags & SVIRTUAL || sp->so == NULL)
85 greg 2.1 return(0);
86 greg 2.2 return(!strcmp(id, sp->so->oname));
87 greg 2.1 }
88    
89    
90 greg 2.2 static
91     add2slist(r, sl) /* add source list to ray's */
92     register RAY *r;
93     register int *sl;
94     {
95     static int slspare[MAXSLIST+1]; /* in case of emergence */
96     register int i;
97    
98     if (sl == NULL || sl[0] == 0) /* nothing to add */
99     return;
100     if (r->slights == NULL)
101     (r->slights = slspare)[0] = 0; /* just once per ray path */
102     for (i = sl[0]; i > 0; i--)
103     if (!inslist(r->slights, sl[i])) {
104     if (r->slights[0] >= MAXSLIST)
105     error(USER, "scattering source list overflow");
106     r->slights[++r->slights[0]] = sl[i];
107     }
108     }
109    
110    
111 greg 2.1 m_mist(m, r) /* process a ray entering or leaving some mist */
112     OBJREC *m;
113     register RAY *r;
114     {
115     RAY p;
116     int *myslist = NULL;
117     int newslist[MAXSLIST+1];
118     COLOR mext;
119     double re, ge, be;
120     register int i, j;
121     /* check arguments */
122     if (m->oargs.nfargs > 5)
123     objerror(m, USER, "bad arguments");
124     /* get source indices */
125 greg 2.4 if (m->oargs.nsargs > 0 && (myslist = (int *)m->os) == NULL) {
126 greg 2.2 if (m->oargs.nsargs > MAXSLIST)
127     objerror(m, USER, "too many sources in list");
128 greg 2.1 myslist = (int *)malloc((m->oargs.nsargs+1)*sizeof(int));
129     if (myslist == NULL)
130     goto memerr;
131 greg 2.2 myslist[0] = 0; /* size is first in list */
132     for (j = 0; j < m->oargs.nsargs; j++) {
133 greg 2.1 i = nsources; /* look up each source id */
134     while (i--)
135 greg 2.2 if (srcmatch(source+i, m->oargs.sarg[j]))
136 greg 2.1 break;
137     if (i < 0) {
138     sprintf(errmsg, "unknown source \"%s\"",
139 greg 2.2 m->oargs.sarg[j]);
140 greg 2.1 objerror(m, WARNING, errmsg);
141 greg 2.2 } else if (inslist(myslist, i)) {
142     sprintf(errmsg, "duplicate source \"%s\"",
143     m->oargs.sarg[j]);
144     objerror(m, WARNING, errmsg);
145 greg 2.1 } else
146 greg 2.2 myslist[++myslist[0]] = i;
147 greg 2.1 }
148     m->os = (char *)myslist;
149     }
150     if (m->oargs.nfargs > 2) { /* compute extinction */
151     setcolor(mext, m->oargs.farg[0], m->oargs.farg[1],
152     m->oargs.farg[2]);
153     raytexture(r, m->omod); /* get modifiers */
154     multcolor(mext, r->pcol);
155     } else
156     setcolor(mext, 0., 0., 0.);
157     /* start transmitted ray */
158     if (rayorigin(&p, r, TRANS, 1.) < 0)
159     return(1);
160     VCOPY(p.rdir, r->rdir);
161     p.slights = newslist;
162 greg 2.2 if (r->slights != NULL) /* copy old list if one */
163 greg 2.1 for (j = r->slights[0]; j >= 0; j--)
164     p.slights[j] = r->slights[j];
165     else
166     p.slights[0] = 0;
167     if (r->rod > 0.) { /* entering ray */
168     addcolor(p.cext, mext);
169     if (m->oargs.nfargs > 3)
170     p.albedo = m->oargs.farg[3];
171     if (m->oargs.nfargs > 4)
172     p.gecc = m->oargs.farg[4];
173 greg 2.2 add2slist(&p, myslist); /* add to list */
174     } else { /* leaving ray */
175     if (myslist != NULL) { /* delete from list */
176 greg 2.1 for (j = myslist[0]; j > 0; j--)
177 greg 2.2 if (i = inslist(p.slights, myslist[j]))
178     p.slights[i] = -1;
179     for (i = 0, j = 1; j <= p.slights[0]; j++)
180     if (p.slights[j] != -1)
181     p.slights[++i] = p.slights[j];
182     if (p.slights[0] - i < myslist[0]) { /* fix old */
183     addcolor(r->cext, mext);
184     if (m->oargs.nfargs > 3)
185     r->albedo = m->oargs.farg[3];
186     if (m->oargs.nfargs > 4)
187     r->gecc = m->oargs.farg[4];
188     add2slist(r, myslist);
189     }
190     p.slights[0] = i;
191     }
192 greg 2.5 if ((re = colval(r->cext,RED) - colval(mext,RED)) <
193     colval(cextinction,RED))
194     re = colval(cextinction,RED);
195     if ((ge = colval(r->cext,GRN) - colval(mext,GRN)) <
196     colval(cextinction,GRN))
197     ge = colval(cextinction,GRN);
198     if ((be = colval(r->cext,BLU) - colval(mext,BLU)) <
199     colval(cextinction,BLU))
200     be = colval(cextinction,BLU);
201     setcolor(p.cext, re, ge, be);
202 greg 2.1 if (m->oargs.nfargs > 3)
203     p.albedo = salbedo;
204     if (m->oargs.nfargs > 4)
205     p.gecc = seccg;
206     }
207     rayvalue(&p); /* calls rayparticipate() */
208     copycolor(r->rcol, p.rcol); /* return value */
209     r->rt = r->rot + p.rt;
210     return(1);
211     memerr:
212     error(SYSTEM, "out of memory in m_mist");
213     }