1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Support routines for source objects and materials |
9 |
*/ |
10 |
|
11 |
#include "ray.h" |
12 |
|
13 |
#include "otypes.h" |
14 |
|
15 |
#include "source.h" |
16 |
|
17 |
#include "cone.h" |
18 |
|
19 |
#include "face.h" |
20 |
|
21 |
|
22 |
SRCREC *source = NULL; /* our list of sources */ |
23 |
int nsources = 0; /* the number of sources */ |
24 |
|
25 |
SRCFUNC sfun[NUMOTYPE]; /* source dispatch table */ |
26 |
|
27 |
|
28 |
initstypes() /* initialize source dispatch table */ |
29 |
{ |
30 |
extern VSMATERIAL mirror_vs; |
31 |
extern int fsetsrc(), ssetsrc(), sphsetsrc(), rsetsrc(); |
32 |
extern double fgetplaneq(), rgetplaneq(); |
33 |
extern double fgetmaxdisk(), rgetmaxdisk(); |
34 |
static SOBJECT fsobj = {fsetsrc, fgetplaneq, fgetmaxdisk}; |
35 |
static SOBJECT ssobj = {ssetsrc}; |
36 |
static SOBJECT sphsobj = {sphsetsrc}; |
37 |
static SOBJECT rsobj = {rsetsrc, rgetplaneq, rgetmaxdisk}; |
38 |
|
39 |
sfun[MAT_MIRROR].mf = &mirror_vs; |
40 |
sfun[OBJ_FACE].of = &fsobj; |
41 |
sfun[OBJ_SOURCE].of = &ssobj; |
42 |
sfun[OBJ_SPHERE].of = &sphsobj; |
43 |
sfun[OBJ_RING].of = &rsobj; |
44 |
} |
45 |
|
46 |
|
47 |
SRCREC * |
48 |
newsource() /* allocate new source in our array */ |
49 |
{ |
50 |
if (nsources == 0) |
51 |
source = (SRCREC *)malloc(sizeof(SRCREC)); |
52 |
else |
53 |
source = (SRCREC *)realloc((char *)source, |
54 |
(unsigned)(nsources+1)*sizeof(SRCREC)); |
55 |
if (source == NULL) |
56 |
return(NULL); |
57 |
source[nsources].sflags = 0; |
58 |
source[nsources].nhits = 1; |
59 |
source[nsources].ntests = 2; /* initial hit probability = 1/2 */ |
60 |
return(&source[nsources++]); |
61 |
} |
62 |
|
63 |
|
64 |
fsetsrc(src, so) /* set a face as a source */ |
65 |
register SRCREC *src; |
66 |
OBJREC *so; |
67 |
{ |
68 |
register FACE *f; |
69 |
register int i, j; |
70 |
|
71 |
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
72 |
src->so = so; |
73 |
/* get the face */ |
74 |
f = getface(so); |
75 |
/* find the center */ |
76 |
for (j = 0; j < 3; j++) { |
77 |
src->sloc[j] = 0.0; |
78 |
for (i = 0; i < f->nv; i++) |
79 |
src->sloc[j] += VERTEX(f,i)[j]; |
80 |
src->sloc[j] /= (double)f->nv; |
81 |
} |
82 |
if (!inface(src->sloc, f)) |
83 |
objerror(so, USER, "cannot hit center"); |
84 |
src->sflags |= SFLAT; |
85 |
VCOPY(src->snorm, f->norm); |
86 |
src->ss = sqrt(f->area / PI); |
87 |
src->ss2 = f->area; |
88 |
} |
89 |
|
90 |
|
91 |
ssetsrc(src, so) /* set a source as a source */ |
92 |
register SRCREC *src; |
93 |
register OBJREC *so; |
94 |
{ |
95 |
double theta; |
96 |
|
97 |
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
98 |
src->so = so; |
99 |
if (so->oargs.nfargs != 4) |
100 |
objerror(so, USER, "bad arguments"); |
101 |
src->sflags |= SDISTANT; |
102 |
VCOPY(src->sloc, so->oargs.farg); |
103 |
if (normalize(src->sloc) == 0.0) |
104 |
objerror(so, USER, "zero direction"); |
105 |
theta = PI/180.0/2.0 * so->oargs.farg[3]; |
106 |
if (theta <= FTINY) |
107 |
objerror(so, USER, "zero size"); |
108 |
src->ss = theta >= PI/4 ? 1.0 : tan(theta); |
109 |
src->ss2 = 2.0*PI * (1.0 - cos(theta)); |
110 |
} |
111 |
|
112 |
|
113 |
sphsetsrc(src, so) /* set a sphere as a source */ |
114 |
register SRCREC *src; |
115 |
register OBJREC *so; |
116 |
{ |
117 |
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
118 |
src->so = so; |
119 |
if (so->oargs.nfargs != 4) |
120 |
objerror(so, USER, "bad # arguments"); |
121 |
if (so->oargs.farg[3] <= FTINY) |
122 |
objerror(so, USER, "illegal radius"); |
123 |
VCOPY(src->sloc, so->oargs.farg); |
124 |
src->ss = so->oargs.farg[3]; |
125 |
src->ss2 = PI * src->ss * src->ss; |
126 |
} |
127 |
|
128 |
|
129 |
rsetsrc(src, so) /* set a ring (disk) as a source */ |
130 |
register SRCREC *src; |
131 |
OBJREC *so; |
132 |
{ |
133 |
register CONE *co; |
134 |
|
135 |
src->sa.success = 2*AIMREQT-1; /* bitch on second failure */ |
136 |
src->so = so; |
137 |
/* get the ring */ |
138 |
co = getcone(so, 0); |
139 |
VCOPY(src->sloc, CO_P0(co)); |
140 |
if (CO_R0(co) > 0.0) |
141 |
objerror(so, USER, "cannot hit center"); |
142 |
src->sflags |= SFLAT; |
143 |
VCOPY(src->snorm, co->ad); |
144 |
src->ss = CO_R1(co); |
145 |
src->ss2 = PI * src->ss * src->ss; |
146 |
} |
147 |
|
148 |
|
149 |
SPOT * |
150 |
makespot(m) /* make a spotlight */ |
151 |
register OBJREC *m; |
152 |
{ |
153 |
extern double cos(); |
154 |
register SPOT *ns; |
155 |
|
156 |
if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL) |
157 |
return(NULL); |
158 |
ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3])); |
159 |
VCOPY(ns->aim, m->oargs.farg+4); |
160 |
if ((ns->flen = normalize(ns->aim)) == 0.0) |
161 |
objerror(m, USER, "zero focus vector"); |
162 |
return(ns); |
163 |
} |
164 |
|
165 |
|
166 |
double |
167 |
fgetmaxdisk(ocent, op) /* get center and squared radius of face */ |
168 |
FVECT ocent; |
169 |
OBJREC *op; |
170 |
{ |
171 |
double maxrad2; |
172 |
double d2; |
173 |
register int i, j; |
174 |
register FACE *f; |
175 |
|
176 |
f = getface(op); |
177 |
for (i = 0; i < 3; i++) { |
178 |
ocent[i] = 0.; |
179 |
for (j = 0; j < f->nv; j++) |
180 |
ocent[i] += VERTEX(f,j)[i]; |
181 |
ocent[i] /= (double)f->nv; |
182 |
} |
183 |
maxrad2 = 0.; |
184 |
for (j = 0; j < f->nv; j++) { |
185 |
d2 = dist2(VERTEX(f,j), ocent); |
186 |
if (d2 > maxrad2) |
187 |
maxrad2 = d2; |
188 |
} |
189 |
return(maxrad2); |
190 |
} |
191 |
|
192 |
|
193 |
double |
194 |
rgetmaxdisk(ocent, op) /* get center and squared radius of ring */ |
195 |
FVECT ocent; |
196 |
OBJREC *op; |
197 |
{ |
198 |
register CONE *co; |
199 |
|
200 |
co = getcone(op, 0); |
201 |
VCOPY(ocent, CO_P0(co)); |
202 |
return(CO_R1(co)*CO_R1(co)); |
203 |
} |
204 |
|
205 |
|
206 |
double |
207 |
fgetplaneq(nvec, op) /* get plane equation for face */ |
208 |
FVECT nvec; |
209 |
OBJREC *op; |
210 |
{ |
211 |
register FACE *fo; |
212 |
|
213 |
fo = getface(op); |
214 |
VCOPY(nvec, fo->norm); |
215 |
return(fo->offset); |
216 |
} |
217 |
|
218 |
|
219 |
double |
220 |
rgetplaneq(nvec, op) /* get plane equation for ring */ |
221 |
FVECT nvec; |
222 |
OBJREC *op; |
223 |
{ |
224 |
register CONE *co; |
225 |
|
226 |
co = getcone(op, 0); |
227 |
VCOPY(nvec, co->ad); |
228 |
return(DOT(nvec, CO_P0(co))); |
229 |
} |
230 |
|
231 |
|
232 |
sourcehit(r) /* check to see if ray hit distant source */ |
233 |
register RAY *r; |
234 |
{ |
235 |
int first, last; |
236 |
register int i; |
237 |
|
238 |
if (r->rsrc >= 0) { /* check only one if aimed */ |
239 |
first = last = r->rsrc; |
240 |
} else { /* otherwise check all */ |
241 |
first = 0; last = nsources-1; |
242 |
} |
243 |
for (i = first; i <= last; i++) |
244 |
if (source[i].sflags & SDISTANT) |
245 |
/* |
246 |
* Check to see if ray is within |
247 |
* solid angle of source. |
248 |
*/ |
249 |
if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir)) |
250 |
<= source[i].ss2) { |
251 |
r->ro = source[i].so; |
252 |
if (!(source[i].sflags & SSKIP)) |
253 |
break; |
254 |
} |
255 |
|
256 |
if (r->ro != NULL) { |
257 |
for (i = 0; i < 3; i++) |
258 |
r->ron[i] = -r->rdir[i]; |
259 |
r->rod = 1.0; |
260 |
r->rox = NULL; |
261 |
return(1); |
262 |
} |
263 |
return(0); |
264 |
} |
265 |
|
266 |
|
267 |
#define wrongsource(m, r) (m->otype!=MAT_ILLUM && \ |
268 |
r->rsrc>=0 && \ |
269 |
source[r->rsrc].so!=r->ro) |
270 |
|
271 |
#define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \ |
272 |
!(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3])) |
273 |
|
274 |
#define passillum(m, r) (m->otype==MAT_ILLUM && \ |
275 |
!(r->rsrc>=0&&source[r->rsrc].so==r->ro)) |
276 |
|
277 |
|
278 |
m_light(m, r) /* ray hit a light source */ |
279 |
register OBJREC *m; |
280 |
register RAY *r; |
281 |
{ |
282 |
/* check for over-counting */ |
283 |
if (wrongsource(m, r) || badambient(m, r)) |
284 |
return; |
285 |
/* check for passed illum */ |
286 |
if (passillum(m, r)) { |
287 |
|
288 |
if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID)) |
289 |
raytrans(r); |
290 |
else |
291 |
rayshade(r, modifier(m->oargs.sarg[0])); |
292 |
|
293 |
/* otherwise treat as source */ |
294 |
} else { |
295 |
/* check for behind */ |
296 |
if (r->rod < 0.0) |
297 |
return; |
298 |
/* get distribution pattern */ |
299 |
raytexture(r, m->omod); |
300 |
/* get source color */ |
301 |
setcolor(r->rcol, m->oargs.farg[0], |
302 |
m->oargs.farg[1], |
303 |
m->oargs.farg[2]); |
304 |
/* modify value */ |
305 |
multcolor(r->rcol, r->pcol); |
306 |
} |
307 |
} |