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_r albedo_g albedo_b [gecc]]] |
37 |
* |
38 |
* The primaries indicate medium extinction per unit length (absorption |
39 |
* 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 |
* 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 |
#ifndef MAXSLIST |
48 |
#define MAXSLIST 32 /* maximum sources to check */ |
49 |
#endif |
50 |
|
51 |
#define RELAYDELIM '>' /* relay delimiter character */ |
52 |
|
53 |
extern COLOR cextinction; /* global coefficient of extinction */ |
54 |
extern COLOR salbedo; /* global scattering albedo */ |
55 |
extern double seccg; /* global scattering eccentricity */ |
56 |
|
57 |
|
58 |
static int |
59 |
inslist(sl, n) /* return index of source n if it's in list sl */ |
60 |
register int *sl; |
61 |
register int n; |
62 |
{ |
63 |
register int i; |
64 |
|
65 |
for (i = sl[0]; i > 0; i--) |
66 |
if (sl[i] == n) |
67 |
return(i); |
68 |
return(0); |
69 |
} |
70 |
|
71 |
|
72 |
static int |
73 |
srcmatch(sp, id) /* check for an id match on a light source */ |
74 |
register SRCREC *sp; |
75 |
register char *id; |
76 |
{ |
77 |
extern char *index(); |
78 |
register char *cp; |
79 |
/* check for relay sources */ |
80 |
while ((cp = index(id, RELAYDELIM)) != NULL) { |
81 |
if (!(sp->sflags & SVIRTUAL) || sp->so == NULL) |
82 |
return(0); |
83 |
if (strncmp(id, sp->so->oname, cp-id) || sp->so->oname[cp-id]) |
84 |
return(0); |
85 |
id = cp + 1; /* relay to next */ |
86 |
sp = source + sp->sa.sv.sn; |
87 |
} |
88 |
if (sp->sflags & SVIRTUAL || sp->so == NULL) |
89 |
return(0); |
90 |
return(!strcmp(id, sp->so->oname)); |
91 |
} |
92 |
|
93 |
|
94 |
static |
95 |
add2slist(r, sl) /* add source list to ray's */ |
96 |
register RAY *r; |
97 |
register int *sl; |
98 |
{ |
99 |
static int slspare[MAXSLIST+1]; /* in case of emergence */ |
100 |
register int i; |
101 |
|
102 |
if (sl == NULL || sl[0] == 0) /* nothing to add */ |
103 |
return; |
104 |
if (r->slights == NULL) |
105 |
(r->slights = slspare)[0] = 0; /* just once per ray path */ |
106 |
for (i = sl[0]; i > 0; i--) |
107 |
if (!inslist(r->slights, sl[i])) { |
108 |
if (r->slights[0] >= MAXSLIST) |
109 |
error(USER, "scattering source list overflow"); |
110 |
r->slights[++r->slights[0]] = sl[i]; |
111 |
} |
112 |
} |
113 |
|
114 |
|
115 |
m_mist(m, r) /* process a ray entering or leaving some mist */ |
116 |
OBJREC *m; |
117 |
register RAY *r; |
118 |
{ |
119 |
RAY p; |
120 |
int *myslist = NULL; |
121 |
int newslist[MAXSLIST+1]; |
122 |
COLOR mext; |
123 |
double re, ge, be; |
124 |
register int i, j; |
125 |
/* check arguments */ |
126 |
if (m->oargs.nfargs > 7) |
127 |
objerror(m, USER, "bad arguments"); |
128 |
/* get source indices */ |
129 |
if (m->oargs.nsargs > 0 && (myslist = (int *)m->os) == NULL) { |
130 |
if (m->oargs.nsargs > MAXSLIST) |
131 |
objerror(m, USER, "too many sources in list"); |
132 |
myslist = (int *)malloc((m->oargs.nsargs+1)*sizeof(int)); |
133 |
if (myslist == NULL) |
134 |
goto memerr; |
135 |
myslist[0] = 0; /* size is first in list */ |
136 |
for (j = 0; j < m->oargs.nsargs; j++) { |
137 |
i = nsources; /* look up each source id */ |
138 |
while (i--) |
139 |
if (srcmatch(source+i, m->oargs.sarg[j])) |
140 |
break; |
141 |
if (i < 0) { |
142 |
sprintf(errmsg, "unknown source \"%s\"", |
143 |
m->oargs.sarg[j]); |
144 |
objerror(m, WARNING, errmsg); |
145 |
} else if (inslist(myslist, i)) { |
146 |
sprintf(errmsg, "duplicate source \"%s\"", |
147 |
m->oargs.sarg[j]); |
148 |
objerror(m, WARNING, errmsg); |
149 |
} else |
150 |
myslist[++myslist[0]] = i; |
151 |
} |
152 |
m->os = (char *)myslist; |
153 |
} |
154 |
if (m->oargs.nfargs > 2) { /* compute extinction */ |
155 |
setcolor(mext, m->oargs.farg[0], m->oargs.farg[1], |
156 |
m->oargs.farg[2]); |
157 |
raytexture(r, m->omod); /* get modifiers */ |
158 |
multcolor(mext, r->pcol); |
159 |
} else |
160 |
setcolor(mext, 0., 0., 0.); |
161 |
/* start transmitted ray */ |
162 |
if (rayorigin(&p, r, TRANS, 1.) < 0) |
163 |
return(1); |
164 |
VCOPY(p.rdir, r->rdir); |
165 |
p.slights = newslist; |
166 |
if (r->slights != NULL) /* copy old list if one */ |
167 |
for (j = r->slights[0]; j >= 0; j--) |
168 |
p.slights[j] = r->slights[j]; |
169 |
else |
170 |
p.slights[0] = 0; |
171 |
if (r->rod > 0.) { /* entering ray */ |
172 |
addcolor(p.cext, mext); |
173 |
if (m->oargs.nfargs > 5) |
174 |
setcolor(p.albedo, m->oargs.farg[3], |
175 |
m->oargs.farg[4], m->oargs.farg[5]); |
176 |
if (m->oargs.nfargs > 6) |
177 |
p.gecc = m->oargs.farg[6]; |
178 |
add2slist(&p, myslist); /* add to list */ |
179 |
} else { /* leaving ray */ |
180 |
if (myslist != NULL) { /* delete from list */ |
181 |
for (j = myslist[0]; j > 0; j--) |
182 |
if (i = inslist(p.slights, myslist[j])) |
183 |
p.slights[i] = -1; |
184 |
for (i = 0, j = 1; j <= p.slights[0]; j++) |
185 |
if (p.slights[j] != -1) |
186 |
p.slights[++i] = p.slights[j]; |
187 |
if (p.slights[0] - i < myslist[0]) { /* fix old */ |
188 |
addcolor(r->cext, mext); |
189 |
if (m->oargs.nfargs > 5) |
190 |
setcolor(r->albedo, m->oargs.farg[3], |
191 |
m->oargs.farg[4], m->oargs.farg[5]); |
192 |
if (m->oargs.nfargs > 6) |
193 |
r->gecc = m->oargs.farg[6]; |
194 |
add2slist(r, myslist); |
195 |
} |
196 |
p.slights[0] = i; |
197 |
} |
198 |
if ((re = colval(r->cext,RED) - colval(mext,RED)) < |
199 |
colval(cextinction,RED)) |
200 |
re = colval(cextinction,RED); |
201 |
if ((ge = colval(r->cext,GRN) - colval(mext,GRN)) < |
202 |
colval(cextinction,GRN)) |
203 |
ge = colval(cextinction,GRN); |
204 |
if ((be = colval(r->cext,BLU) - colval(mext,BLU)) < |
205 |
colval(cextinction,BLU)) |
206 |
be = colval(cextinction,BLU); |
207 |
setcolor(p.cext, re, ge, be); |
208 |
if (m->oargs.nfargs > 5) |
209 |
copycolor(p.albedo, salbedo); |
210 |
if (m->oargs.nfargs > 6) |
211 |
p.gecc = seccg; |
212 |
} |
213 |
rayvalue(&p); /* calls rayparticipate() */ |
214 |
copycolor(r->rcol, p.rcol); /* return value */ |
215 |
r->rt = r->rot + p.rt; |
216 |
return(1); |
217 |
memerr: |
218 |
error(SYSTEM, "out of memory in m_mist"); |
219 |
} |