1 |
– |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Context handlers |
6 |
|
*/ |
7 |
|
|
8 |
|
#include <stdio.h> |
9 |
+ |
#include <math.h> |
10 |
+ |
#include <stdlib.h> |
11 |
|
#include <string.h> |
12 |
|
#include "parser.h" |
13 |
|
#include "lookup.h" |
24 |
|
|
25 |
|
/* the current contexts */ |
26 |
|
C_COLOR *c_ccolor = &c_uncolor; |
27 |
+ |
char *c_ccname = NULL; |
28 |
|
C_MATERIAL *c_cmaterial = &c_unmaterial; |
29 |
+ |
char *c_cmname = NULL; |
30 |
|
C_VERTEX *c_cvertex = &c_unvertex; |
31 |
+ |
char *c_cvname = NULL; |
32 |
|
|
33 |
< |
static LUTAB clr_tab; /* color lookup table */ |
34 |
< |
static LUTAB mat_tab; /* material lookup table */ |
35 |
< |
static LUTAB vtx_tab; /* vertex lookup table */ |
33 |
> |
static LUTAB clr_tab = LU_SINIT(free,free); /* color lookup table */ |
34 |
> |
static LUTAB mat_tab = LU_SINIT(free,free); /* material lookup table */ |
35 |
> |
static LUTAB vtx_tab = LU_SINIT(free,free); /* vertex lookup table */ |
36 |
|
|
37 |
+ |
/* CIE 1931 Standard Observer curves */ |
38 |
+ |
static C_COLOR cie_xf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
39 |
+ |
{14,42,143,435,1344,2839,3483,3362,2908,1954,956, |
40 |
+ |
320,49,93,633,1655,2904,4334,5945,7621,9163,10263, |
41 |
+ |
10622,10026,8544,6424,4479,2835,1649,874,468,227, |
42 |
+ |
114,58,29,14,7,3,2,1,0}, 106836L, .467, .368, 362.230 |
43 |
+ |
}; |
44 |
+ |
static C_COLOR cie_yf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
45 |
+ |
{0,1,4,12,40,116,230,380,600,910,1390,2080,3230, |
46 |
+ |
5030,7100,8620,9540,9950,9950,9520,8700,7570,6310, |
47 |
+ |
5030,3810,2650,1750,1070,610,320,170,82,41,21,10, |
48 |
+ |
5,2,1,1,0,0}, 106856L, .398, .542, 493.525 |
49 |
+ |
}; |
50 |
+ |
static C_COLOR cie_zf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF, |
51 |
+ |
{65,201,679,2074,6456,13856,17471,17721,16692, |
52 |
+ |
12876,8130,4652,2720,1582,782,422,203,87,39,21,17, |
53 |
+ |
11,8,3,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, |
54 |
+ |
106770L, .147, .077, 54.363 |
55 |
+ |
}; |
56 |
+ |
/* Derived CIE 1931 Primaries (imaginary) */ |
57 |
+ |
static C_COLOR cie_xp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY, |
58 |
+ |
{-174,-198,-195,-197,-202,-213,-235,-272,-333, |
59 |
+ |
-444,-688,-1232,-2393,-4497,-6876,-6758,-5256, |
60 |
+ |
-3100,-815,1320,3200,4782,5998,6861,7408,7754, |
61 |
+ |
7980,8120,8199,8240,8271,8292,8309,8283,8469, |
62 |
+ |
8336,8336,8336,8336,8336,8336}, |
63 |
+ |
127424L, 1., .0, |
64 |
+ |
}; |
65 |
+ |
static C_COLOR cie_yp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY, |
66 |
+ |
{-451,-431,-431,-430,-427,-417,-399,-366,-312, |
67 |
+ |
-204,57,691,2142,4990,8810,9871,9122,7321,5145, |
68 |
+ |
3023,1123,-473,-1704,-2572,-3127,-3474,-3704, |
69 |
+ |
-3846,-3927,-3968,-3999,-4021,-4038,-4012,-4201, |
70 |
+ |
-4066,-4066,-4066,-4066,-4066,-4066}, |
71 |
+ |
-23035L, .0, 1., |
72 |
+ |
}; |
73 |
+ |
static C_COLOR cie_zp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY, |
74 |
+ |
{4051,4054,4052,4053,4054,4056,4059,4064,4071, |
75 |
+ |
4074,4056,3967,3677,2933,1492,313,-440,-795, |
76 |
+ |
-904,-918,-898,-884,-869,-863,-855,-855,-851, |
77 |
+ |
-848,-847,-846,-846,-846,-845,-846,-843,-845, |
78 |
+ |
-845,-845,-845,-845,-845}, |
79 |
+ |
36057L, .0, .0, |
80 |
+ |
}; |
81 |
|
|
82 |
+ |
static int setspectrum(); |
83 |
+ |
static int setbbtemp(); |
84 |
+ |
static void mixcolors(); |
85 |
+ |
|
86 |
+ |
|
87 |
|
int |
88 |
|
c_hcolor(ac, av) /* handle color entity */ |
89 |
|
int ac; |
90 |
|
register char **av; |
91 |
|
{ |
92 |
+ |
double w, wsum; |
93 |
+ |
register int i; |
94 |
|
register LUENT *lp; |
95 |
|
|
96 |
|
switch (mg_entity(av[0])) { |
97 |
|
case MG_E_COLOR: /* get/set color context */ |
98 |
+ |
if (ac > 4) |
99 |
+ |
return(MG_EARGC); |
100 |
|
if (ac == 1) { /* set unnamed color context */ |
101 |
|
c_uncolor = c_dfcolor; |
102 |
|
c_ccolor = &c_uncolor; |
103 |
+ |
c_ccname = NULL; |
104 |
|
return(MG_OK); |
105 |
|
} |
106 |
+ |
if (!isname(av[1])) |
107 |
+ |
return(MG_EILL); |
108 |
|
lp = lu_find(&clr_tab, av[1]); /* lookup context */ |
109 |
|
if (lp == NULL) |
110 |
|
return(MG_EMEM); |
111 |
+ |
c_ccname = lp->key; |
112 |
+ |
c_ccolor = (C_COLOR *)lp->data; |
113 |
|
if (ac == 2) { /* reestablish previous context */ |
114 |
< |
if (lp->data == NULL) |
114 |
> |
if (c_ccolor == NULL) |
115 |
|
return(MG_EUNDEF); |
56 |
– |
c_ccolor = (C_COLOR *)lp->data; |
116 |
|
return(MG_OK); |
117 |
|
} |
118 |
|
if (av[2][0] != '=' || av[2][1]) |
119 |
|
return(MG_ETYPE); |
120 |
< |
if (lp->key == NULL) { /* create new color context */ |
120 |
> |
if (c_ccolor == NULL) { /* create new color context */ |
121 |
|
lp->key = (char *)malloc(strlen(av[1])+1); |
122 |
|
if (lp->key == NULL) |
123 |
|
return(MG_EMEM); |
125 |
|
lp->data = (char *)malloc(sizeof(C_COLOR)); |
126 |
|
if (lp->data == NULL) |
127 |
|
return(MG_EMEM); |
128 |
+ |
c_ccname = lp->key; |
129 |
+ |
c_ccolor = (C_COLOR *)lp->data; |
130 |
+ |
c_ccolor->clock = 0; |
131 |
+ |
c_ccolor->client_data = NULL; |
132 |
|
} |
133 |
< |
c_ccolor = (C_COLOR *)lp->data; |
133 |
> |
i = c_ccolor->clock; |
134 |
|
if (ac == 3) { /* use default template */ |
135 |
|
*c_ccolor = c_dfcolor; |
136 |
+ |
c_ccolor->clock = i + 1; |
137 |
|
return(MG_OK); |
138 |
|
} |
139 |
|
lp = lu_find(&clr_tab, av[3]); /* lookup template */ |
142 |
|
if (lp->data == NULL) |
143 |
|
return(MG_EUNDEF); |
144 |
|
*c_ccolor = *(C_COLOR *)lp->data; |
145 |
< |
if (ac > 4) |
82 |
< |
return(MG_EARGC); |
145 |
> |
c_ccolor->clock = i + 1; |
146 |
|
return(MG_OK); |
147 |
|
case MG_E_CXY: /* assign CIE XY value */ |
148 |
|
if (ac != 3) |
149 |
|
return(MG_EARGC); |
150 |
< |
if (!isflt(av[1]) || !isflt(av[2])) |
150 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
151 |
|
return(MG_ETYPE); |
152 |
|
c_ccolor->cx = atof(av[1]); |
153 |
|
c_ccolor->cy = atof(av[2]); |
154 |
+ |
c_ccolor->flags = C_CDXY|C_CSXY; |
155 |
|
if (c_ccolor->cx < 0. | c_ccolor->cy < 0. | |
156 |
|
c_ccolor->cx + c_ccolor->cy > 1.) |
157 |
|
return(MG_EILL); |
158 |
+ |
c_ccolor->clock++; |
159 |
|
return(MG_OK); |
160 |
+ |
case MG_E_CSPEC: /* assign spectral values */ |
161 |
+ |
if (ac < 5) |
162 |
+ |
return(MG_EARGC); |
163 |
+ |
if (!isflt(av[1]) | !isflt(av[2])) |
164 |
+ |
return(MG_ETYPE); |
165 |
+ |
return(setspectrum(c_ccolor, atof(av[1]), atof(av[2]), |
166 |
+ |
ac-3, av+3)); |
167 |
+ |
case MG_E_CCT: /* assign black body spectrum */ |
168 |
+ |
if (ac != 2) |
169 |
+ |
return(MG_EARGC); |
170 |
+ |
if (!isflt(av[1])) |
171 |
+ |
return(MG_ETYPE); |
172 |
+ |
return(setbbtemp(c_ccolor, atof(av[1]))); |
173 |
+ |
case MG_E_CMIX: /* mix colors */ |
174 |
+ |
if (ac < 5 || (ac-1)%2) |
175 |
+ |
return(MG_EARGC); |
176 |
+ |
if (!isflt(av[1])) |
177 |
+ |
return(MG_ETYPE); |
178 |
+ |
wsum = atof(av[1]); |
179 |
+ |
if ((lp = lu_find(&clr_tab, av[2])) == NULL) |
180 |
+ |
return(MG_EMEM); |
181 |
+ |
if (lp->data == NULL) |
182 |
+ |
return(MG_EUNDEF); |
183 |
+ |
*c_ccolor = *(C_COLOR *)lp->data; |
184 |
+ |
for (i = 3; i < ac; i += 2) { |
185 |
+ |
if (!isflt(av[i])) |
186 |
+ |
return(MG_ETYPE); |
187 |
+ |
w = atof(av[i]); |
188 |
+ |
if ((lp = lu_find(&clr_tab, av[i+1])) == NULL) |
189 |
+ |
return(MG_EMEM); |
190 |
+ |
if (lp->data == NULL) |
191 |
+ |
return(MG_EUNDEF); |
192 |
+ |
mixcolors(c_ccolor, wsum, c_ccolor, |
193 |
+ |
w, (C_COLOR *)lp->data); |
194 |
+ |
wsum += w; |
195 |
+ |
} |
196 |
+ |
if (wsum <= 0.) |
197 |
+ |
return(MG_EILL); |
198 |
+ |
c_ccolor->clock++; |
199 |
+ |
return(MG_OK); |
200 |
|
} |
201 |
|
return(MG_EUNK); |
202 |
|
} |
207 |
|
int ac; |
208 |
|
register char **av; |
209 |
|
{ |
210 |
+ |
int i; |
211 |
|
register LUENT *lp; |
212 |
|
|
213 |
|
switch (mg_entity(av[0])) { |
214 |
|
case MG_E_MATERIAL: /* get/set material context */ |
215 |
+ |
if (ac > 4) |
216 |
+ |
return(MG_EARGC); |
217 |
|
if (ac == 1) { /* set unnamed material context */ |
218 |
|
c_unmaterial = c_dfmaterial; |
219 |
|
c_cmaterial = &c_unmaterial; |
220 |
+ |
c_cmname = NULL; |
221 |
|
return(MG_OK); |
222 |
|
} |
223 |
+ |
if (!isname(av[1])) |
224 |
+ |
return(MG_EILL); |
225 |
|
lp = lu_find(&mat_tab, av[1]); /* lookup context */ |
226 |
|
if (lp == NULL) |
227 |
|
return(MG_EMEM); |
228 |
+ |
c_cmname = lp->key; |
229 |
+ |
c_cmaterial = (C_MATERIAL *)lp->data; |
230 |
|
if (ac == 2) { /* reestablish previous context */ |
231 |
< |
if (lp->data == NULL) |
231 |
> |
if (c_cmaterial == NULL) |
232 |
|
return(MG_EUNDEF); |
120 |
– |
c_cmaterial = (C_MATERIAL *)lp->data; |
233 |
|
return(MG_OK); |
234 |
|
} |
235 |
|
if (av[2][0] != '=' || av[2][1]) |
236 |
|
return(MG_ETYPE); |
237 |
< |
if (lp->key == NULL) { /* create new material context */ |
237 |
> |
if (c_cmaterial == NULL) { /* create new material */ |
238 |
|
lp->key = (char *)malloc(strlen(av[1])+1); |
239 |
|
if (lp->key == NULL) |
240 |
|
return(MG_EMEM); |
242 |
|
lp->data = (char *)malloc(sizeof(C_MATERIAL)); |
243 |
|
if (lp->data == NULL) |
244 |
|
return(MG_EMEM); |
245 |
+ |
c_cmname = lp->key; |
246 |
+ |
c_cmaterial = (C_MATERIAL *)lp->data; |
247 |
+ |
c_cmaterial->clock = 0; |
248 |
+ |
c_cmaterial->client_data = NULL; |
249 |
|
} |
250 |
< |
c_cmaterial = (C_MATERIAL *)lp->data; |
250 |
> |
i = c_cmaterial->clock; |
251 |
|
if (ac == 3) { /* use default template */ |
252 |
|
*c_cmaterial = c_dfmaterial; |
253 |
+ |
c_cmaterial->clock = i + 1; |
254 |
|
return(MG_OK); |
255 |
|
} |
256 |
|
lp = lu_find(&mat_tab, av[3]); /* lookup template */ |
259 |
|
if (lp->data == NULL) |
260 |
|
return(MG_EUNDEF); |
261 |
|
*c_cmaterial = *(C_MATERIAL *)lp->data; |
262 |
< |
if (ac > 4) |
262 |
> |
c_cmaterial->clock = i + 1; |
263 |
> |
return(MG_OK); |
264 |
> |
case MG_E_IR: /* set index of refraction */ |
265 |
> |
if (ac != 3) |
266 |
|
return(MG_EARGC); |
267 |
+ |
if (!isflt(av[1]) | !isflt(av[2])) |
268 |
+ |
return(MG_ETYPE); |
269 |
+ |
c_cmaterial->nr = atof(av[1]); |
270 |
+ |
c_cmaterial->ni = atof(av[2]); |
271 |
+ |
if (c_cmaterial->nr <= FTINY) |
272 |
+ |
return(MG_EILL); |
273 |
+ |
c_cmaterial->clock++; |
274 |
|
return(MG_OK); |
275 |
|
case MG_E_RD: /* set diffuse reflectance */ |
276 |
|
if (ac != 2) |
281 |
|
if (c_cmaterial->rd < 0. | c_cmaterial->rd > 1.) |
282 |
|
return(MG_EILL); |
283 |
|
c_cmaterial->rd_c = *c_ccolor; |
284 |
+ |
c_cmaterial->clock++; |
285 |
|
return(MG_OK); |
286 |
|
case MG_E_ED: /* set diffuse emittance */ |
287 |
|
if (ac != 2) |
292 |
|
if (c_cmaterial->ed < 0.) |
293 |
|
return(MG_EILL); |
294 |
|
c_cmaterial->ed_c = *c_ccolor; |
295 |
+ |
c_cmaterial->clock++; |
296 |
|
return(MG_OK); |
297 |
|
case MG_E_TD: /* set diffuse transmittance */ |
298 |
|
if (ac != 2) |
303 |
|
if (c_cmaterial->td < 0. | c_cmaterial->td > 1.) |
304 |
|
return(MG_EILL); |
305 |
|
c_cmaterial->td_c = *c_ccolor; |
306 |
+ |
c_cmaterial->clock++; |
307 |
|
return(MG_OK); |
308 |
|
case MG_E_RS: /* set specular reflectance */ |
309 |
|
if (ac != 3) |
310 |
|
return(MG_EARGC); |
311 |
< |
if (!isflt(av[1]) || !isflt(av[2])) |
311 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
312 |
|
return(MG_ETYPE); |
313 |
|
c_cmaterial->rs = atof(av[1]); |
314 |
|
c_cmaterial->rs_a = atof(av[2]); |
316 |
|
c_cmaterial->rs_a < 0.) |
317 |
|
return(MG_EILL); |
318 |
|
c_cmaterial->rs_c = *c_ccolor; |
319 |
+ |
c_cmaterial->clock++; |
320 |
|
return(MG_OK); |
321 |
|
case MG_E_TS: /* set specular transmittance */ |
322 |
|
if (ac != 3) |
323 |
|
return(MG_EARGC); |
324 |
< |
if (!isflt(av[1]) || !isflt(av[2])) |
324 |
> |
if (!isflt(av[1]) | !isflt(av[2])) |
325 |
|
return(MG_ETYPE); |
326 |
|
c_cmaterial->ts = atof(av[1]); |
327 |
|
c_cmaterial->ts_a = atof(av[2]); |
329 |
|
c_cmaterial->ts_a < 0.) |
330 |
|
return(MG_EILL); |
331 |
|
c_cmaterial->ts_c = *c_ccolor; |
332 |
+ |
c_cmaterial->clock++; |
333 |
|
return(MG_OK); |
334 |
+ |
case MG_E_SIDES: /* set number of sides */ |
335 |
+ |
if (ac != 2) |
336 |
+ |
return(MG_EARGC); |
337 |
+ |
if (!isint(av[1])) |
338 |
+ |
return(MG_ETYPE); |
339 |
+ |
i = atoi(av[1]); |
340 |
+ |
if (i == 1) |
341 |
+ |
c_cmaterial->sided = 1; |
342 |
+ |
else if (i == 2) |
343 |
+ |
c_cmaterial->sided = 0; |
344 |
+ |
else |
345 |
+ |
return(MG_EILL); |
346 |
+ |
c_cmaterial->clock++; |
347 |
+ |
return(MG_OK); |
348 |
|
} |
349 |
|
return(MG_EUNK); |
350 |
|
} |
355 |
|
int ac; |
356 |
|
register char **av; |
357 |
|
{ |
358 |
+ |
int i; |
359 |
|
register LUENT *lp; |
360 |
|
|
361 |
|
switch (mg_entity(av[0])) { |
362 |
|
case MG_E_VERTEX: /* get/set vertex context */ |
363 |
+ |
if (ac > 4) |
364 |
+ |
return(MG_EARGC); |
365 |
|
if (ac == 1) { /* set unnamed vertex context */ |
366 |
|
c_unvertex = c_dfvertex; |
367 |
|
c_cvertex = &c_unvertex; |
368 |
+ |
c_cvname = NULL; |
369 |
|
return(MG_OK); |
370 |
|
} |
371 |
+ |
if (!isname(av[1])) |
372 |
+ |
return(MG_EILL); |
373 |
|
lp = lu_find(&vtx_tab, av[1]); /* lookup context */ |
374 |
|
if (lp == NULL) |
375 |
|
return(MG_EMEM); |
376 |
+ |
c_cvname = lp->key; |
377 |
+ |
c_cvertex = (C_VERTEX *)lp->data; |
378 |
|
if (ac == 2) { /* reestablish previous context */ |
379 |
< |
if (lp->data == NULL) |
379 |
> |
if (c_cvertex == NULL) |
380 |
|
return(MG_EUNDEF); |
227 |
– |
c_cvertex = (C_VERTEX *)lp->data; |
381 |
|
return(MG_OK); |
382 |
|
} |
383 |
|
if (av[2][0] != '=' || av[2][1]) |
384 |
|
return(MG_ETYPE); |
385 |
< |
if (lp->key == NULL) { /* create new vertex context */ |
385 |
> |
if (c_cvertex == NULL) { /* create new vertex context */ |
386 |
|
lp->key = (char *)malloc(strlen(av[1])+1); |
387 |
|
if (lp->key == NULL) |
388 |
|
return(MG_EMEM); |
390 |
|
lp->data = (char *)malloc(sizeof(C_VERTEX)); |
391 |
|
if (lp->data == NULL) |
392 |
|
return(MG_EMEM); |
393 |
+ |
c_cvname = lp->key; |
394 |
+ |
c_cvertex = (C_VERTEX *)lp->data; |
395 |
+ |
c_cvertex->clock = 0; |
396 |
+ |
c_cvertex->client_data = NULL; |
397 |
|
} |
398 |
< |
c_cvertex = (C_VERTEX *)lp->data; |
398 |
> |
i = c_cvertex->clock; |
399 |
|
if (ac == 3) { /* use default template */ |
400 |
|
*c_cvertex = c_dfvertex; |
401 |
+ |
c_cvertex->clock = i + 1; |
402 |
|
return(MG_OK); |
403 |
|
} |
404 |
|
lp = lu_find(&vtx_tab, av[3]); /* lookup template */ |
407 |
|
if (lp->data == NULL) |
408 |
|
return(MG_EUNDEF); |
409 |
|
*c_cvertex = *(C_VERTEX *)lp->data; |
410 |
< |
if (ac > 4) |
253 |
< |
return(MG_EARGC); |
410 |
> |
c_cvertex->clock = i + 1; |
411 |
|
return(MG_OK); |
412 |
|
case MG_E_POINT: /* set point */ |
413 |
|
if (ac != 4) |
414 |
|
return(MG_EARGC); |
415 |
< |
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3])) |
415 |
> |
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
416 |
|
return(MG_ETYPE); |
417 |
|
c_cvertex->p[0] = atof(av[1]); |
418 |
|
c_cvertex->p[1] = atof(av[2]); |
419 |
|
c_cvertex->p[2] = atof(av[3]); |
420 |
+ |
c_cvertex->clock++; |
421 |
|
return(MG_OK); |
422 |
|
case MG_E_NORMAL: /* set normal */ |
423 |
|
if (ac != 4) |
424 |
|
return(MG_EARGC); |
425 |
< |
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3])) |
425 |
> |
if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3])) |
426 |
|
return(MG_ETYPE); |
427 |
|
c_cvertex->n[0] = atof(av[1]); |
428 |
|
c_cvertex->n[1] = atof(av[2]); |
429 |
|
c_cvertex->n[2] = atof(av[3]); |
430 |
|
(void)normalize(c_cvertex->n); |
431 |
+ |
c_cvertex->clock++; |
432 |
|
return(MG_OK); |
433 |
|
} |
434 |
|
return(MG_EUNK); |
435 |
|
} |
436 |
|
|
437 |
|
|
279 |
– |
static void |
280 |
– |
freectx(lp) /* free a context table entry */ |
281 |
– |
register LUENT *lp; |
282 |
– |
{ |
283 |
– |
free((MEM_PTR)lp->key); |
284 |
– |
free((MEM_PTR)lp->data); |
285 |
– |
} |
286 |
– |
|
287 |
– |
|
438 |
|
void |
439 |
|
c_clearall() /* empty context tables */ |
440 |
|
{ |
441 |
|
c_uncolor = c_dfcolor; |
442 |
|
c_ccolor = &c_uncolor; |
443 |
< |
lu_done(&clr_tab, freectx); |
443 |
> |
c_ccname = NULL; |
444 |
> |
lu_done(&clr_tab); |
445 |
|
c_unmaterial = c_dfmaterial; |
446 |
|
c_cmaterial = &c_unmaterial; |
447 |
< |
lu_done(&mat_tab, freectx); |
447 |
> |
c_cmname = NULL; |
448 |
> |
lu_done(&mat_tab); |
449 |
|
c_unvertex = c_dfvertex; |
450 |
|
c_cvertex = &c_unvertex; |
451 |
< |
lu_done(&vtx_tab, freectx); |
451 |
> |
c_cvname = NULL; |
452 |
> |
lu_done(&vtx_tab); |
453 |
|
} |
454 |
|
|
455 |
|
|
456 |
+ |
C_MATERIAL * |
457 |
+ |
c_getmaterial(name) /* get a named material */ |
458 |
+ |
char *name; |
459 |
+ |
{ |
460 |
+ |
register LUENT *lp; |
461 |
+ |
|
462 |
+ |
if ((lp = lu_find(&mat_tab, name)) == NULL) |
463 |
+ |
return(NULL); |
464 |
+ |
return((C_MATERIAL *)lp->data); |
465 |
+ |
} |
466 |
+ |
|
467 |
+ |
|
468 |
|
C_VERTEX * |
469 |
|
c_getvert(name) /* get a named vertex */ |
470 |
|
char *name; |
475 |
|
return(NULL); |
476 |
|
return((C_VERTEX *)lp->data); |
477 |
|
} |
478 |
+ |
|
479 |
+ |
|
480 |
+ |
C_COLOR * |
481 |
+ |
c_getcolor(name) /* get a named color */ |
482 |
+ |
char *name; |
483 |
+ |
{ |
484 |
+ |
register LUENT *lp; |
485 |
+ |
|
486 |
+ |
if ((lp = lu_find(&clr_tab, name)) == NULL) |
487 |
+ |
return(NULL); |
488 |
+ |
return((C_COLOR *)lp->data); |
489 |
+ |
} |
490 |
+ |
|
491 |
+ |
|
492 |
+ |
int |
493 |
+ |
c_isgrey(clr) /* check if color is grey */ |
494 |
+ |
register C_COLOR *clr; |
495 |
+ |
{ |
496 |
+ |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) |
497 |
+ |
return(1); /* no settings == grey */ |
498 |
+ |
c_ccvt(clr, C_CSXY); |
499 |
+ |
return(clr->cx >= .323 && clr->cx <= .343 && |
500 |
+ |
clr->cy >= .323 && clr->cy <= .343); |
501 |
+ |
} |
502 |
+ |
|
503 |
+ |
|
504 |
+ |
void |
505 |
+ |
c_ccvt(clr, fl) /* convert color representations */ |
506 |
+ |
register C_COLOR *clr; |
507 |
+ |
int fl; |
508 |
+ |
{ |
509 |
+ |
double x, y, z; |
510 |
+ |
register int i; |
511 |
+ |
|
512 |
+ |
fl &= ~clr->flags; /* ignore what's done */ |
513 |
+ |
if (!fl) /* everything's done! */ |
514 |
+ |
return; |
515 |
+ |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) /* nothing set! */ |
516 |
+ |
*clr = c_dfcolor; |
517 |
+ |
if (fl & C_CSXY) { /* cspec -> cxy */ |
518 |
+ |
x = y = z = 0.; |
519 |
+ |
for (i = 0; i < C_CNSS; i++) { |
520 |
+ |
x += cie_xf.ssamp[i] * clr->ssamp[i]; |
521 |
+ |
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
522 |
+ |
z += cie_zf.ssamp[i] * clr->ssamp[i]; |
523 |
+ |
} |
524 |
+ |
x /= (double)cie_xf.ssum; |
525 |
+ |
y /= (double)cie_yf.ssum; |
526 |
+ |
z /= (double)cie_zf.ssum; |
527 |
+ |
z += x + y; |
528 |
+ |
clr->cx = x / z; |
529 |
+ |
clr->cy = y / z; |
530 |
+ |
clr->flags |= C_CSXY; |
531 |
+ |
} else if (fl & C_CSSPEC) { /* cxy -> cspec */ |
532 |
+ |
x = clr->cx; |
533 |
+ |
y = clr->cy; |
534 |
+ |
z = 1. - x - y; |
535 |
+ |
clr->ssum = 0; |
536 |
+ |
for (i = 0; i < C_CNSS; i++) { |
537 |
+ |
clr->ssamp[i] = x*cie_xp.ssamp[i] + y*cie_yp.ssamp[i] |
538 |
+ |
+ z*cie_zp.ssamp[i] + .5; |
539 |
+ |
if (clr->ssamp[i] < 0) /* out of gamut! */ |
540 |
+ |
clr->ssamp[i] = 0; |
541 |
+ |
else |
542 |
+ |
clr->ssum += clr->ssamp[i]; |
543 |
+ |
} |
544 |
+ |
clr->flags |= C_CSSPEC; |
545 |
+ |
} |
546 |
+ |
if (fl & C_CSEFF) { /* compute efficacy */ |
547 |
+ |
if (clr->flags & C_CSSPEC) { /* from spectrum */ |
548 |
+ |
y = 0.; |
549 |
+ |
for (i = 0; i < C_CNSS; i++) |
550 |
+ |
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
551 |
+ |
clr->eff = C_CLPWM * y / clr->ssum; |
552 |
+ |
} else /* clr->flags & C_CSXY */ { /* from (x,y) */ |
553 |
+ |
clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff + |
554 |
+ |
(1. - clr->cx - clr->cy)*cie_zf.eff; |
555 |
+ |
} |
556 |
+ |
clr->flags |= C_CSEFF; |
557 |
+ |
} |
558 |
+ |
} |
559 |
+ |
|
560 |
+ |
|
561 |
+ |
static int |
562 |
+ |
setspectrum(clr, wlmin, wlmax, ac, av) /* convert a spectrum */ |
563 |
+ |
register C_COLOR *clr; |
564 |
+ |
double wlmin, wlmax; |
565 |
+ |
int ac; |
566 |
+ |
char **av; |
567 |
+ |
{ |
568 |
+ |
double scale; |
569 |
+ |
float va[C_CNSS]; |
570 |
+ |
register int i, pos; |
571 |
+ |
int n, imax; |
572 |
+ |
int wl; |
573 |
+ |
double wl0, wlstep; |
574 |
+ |
double boxpos, boxstep; |
575 |
+ |
/* check bounds */ |
576 |
+ |
if (wlmax <= C_CMINWL | wlmax <= wlmin | wlmin >= C_CMAXWL) |
577 |
+ |
return(MG_EILL); |
578 |
+ |
wlstep = (wlmax - wlmin)/(ac-1); |
579 |
+ |
while (wlmin < C_CMINWL) { |
580 |
+ |
wlmin += wlstep; |
581 |
+ |
ac--; av++; |
582 |
+ |
} |
583 |
+ |
while (wlmax > C_CMAXWL) { |
584 |
+ |
wlmax -= wlstep; |
585 |
+ |
ac--; |
586 |
+ |
} |
587 |
+ |
imax = ac; /* box filter if necessary */ |
588 |
+ |
boxpos = 0; |
589 |
+ |
boxstep = 1; |
590 |
+ |
if (wlstep < C_CWLI) { |
591 |
+ |
imax = (wlmax - wlmin)/C_CWLI + (1-FTINY); |
592 |
+ |
boxpos = (wlmin - C_CMINWL)/C_CWLI; |
593 |
+ |
boxstep = wlstep/C_CWLI; |
594 |
+ |
wlstep = C_CWLI; |
595 |
+ |
} |
596 |
+ |
scale = 0.; /* get values and maximum */ |
597 |
+ |
pos = 0; |
598 |
+ |
for (i = 0; i < imax; i++) { |
599 |
+ |
va[i] = 0.; n = 0; |
600 |
+ |
while (boxpos < i+.5 && pos < ac) { |
601 |
+ |
if (!isflt(av[pos])) |
602 |
+ |
return(MG_ETYPE); |
603 |
+ |
va[i] += atof(av[pos++]); |
604 |
+ |
n++; |
605 |
+ |
boxpos += boxstep; |
606 |
+ |
} |
607 |
+ |
if (n > 1) |
608 |
+ |
va[i] /= (double)n; |
609 |
+ |
if (va[i] > scale) |
610 |
+ |
scale = va[i]; |
611 |
+ |
else if (va[i] < -scale) |
612 |
+ |
scale = -va[i]; |
613 |
+ |
} |
614 |
+ |
if (scale <= FTINY) |
615 |
+ |
return(MG_EILL); |
616 |
+ |
scale = C_CMAXV / scale; |
617 |
+ |
clr->ssum = 0; /* convert to our spacing */ |
618 |
+ |
wl0 = wlmin; |
619 |
+ |
pos = 0; |
620 |
+ |
for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI) |
621 |
+ |
if (wl < wlmin | wl > wlmax) |
622 |
+ |
clr->ssamp[i] = 0; |
623 |
+ |
else { |
624 |
+ |
while (wl0 + wlstep < wl+FTINY) { |
625 |
+ |
wl0 += wlstep; |
626 |
+ |
pos++; |
627 |
+ |
} |
628 |
+ |
if (wl+FTINY >= wl0 & wl-FTINY <= wl0) |
629 |
+ |
clr->ssamp[i] = scale*va[pos] + .5; |
630 |
+ |
else /* interpolate if necessary */ |
631 |
+ |
clr->ssamp[i] = .5 + scale / wlstep * |
632 |
+ |
( va[pos]*(wl0+wlstep - wl) + |
633 |
+ |
va[pos+1]*(wl - wl0) ); |
634 |
+ |
clr->ssum += clr->ssamp[i]; |
635 |
+ |
} |
636 |
+ |
clr->flags = C_CDSPEC|C_CSSPEC; |
637 |
+ |
clr->clock++; |
638 |
+ |
return(MG_OK); |
639 |
+ |
} |
640 |
+ |
|
641 |
+ |
|
642 |
+ |
static void |
643 |
+ |
mixcolors(cres, w1, c1, w2, c2) /* mix two colors according to weights given */ |
644 |
+ |
register C_COLOR *cres, *c1, *c2; |
645 |
+ |
double w1, w2; |
646 |
+ |
{ |
647 |
+ |
double scale; |
648 |
+ |
float cmix[C_CNSS]; |
649 |
+ |
register int i; |
650 |
+ |
|
651 |
+ |
if ((c1->flags|c2->flags) & C_CDSPEC) { /* spectral mixing */ |
652 |
+ |
c_ccvt(c1, C_CSSPEC|C_CSEFF); |
653 |
+ |
c_ccvt(c2, C_CSSPEC|C_CSEFF); |
654 |
+ |
w1 /= c1->eff*c1->ssum; |
655 |
+ |
w2 /= c2->eff*c2->ssum; |
656 |
+ |
scale = 0.; |
657 |
+ |
for (i = 0; i < C_CNSS; i++) { |
658 |
+ |
cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i]; |
659 |
+ |
if (cmix[i] > scale) |
660 |
+ |
scale = cmix[i]; |
661 |
+ |
} |
662 |
+ |
scale = C_CMAXV / scale; |
663 |
+ |
cres->ssum = 0; |
664 |
+ |
for (i = 0; i < C_CNSS; i++) |
665 |
+ |
cres->ssum += cres->ssamp[i] = scale*cmix[i] + .5; |
666 |
+ |
cres->flags = C_CDSPEC|C_CSSPEC; |
667 |
+ |
} else { /* CIE xy mixing */ |
668 |
+ |
c_ccvt(c1, C_CSXY); |
669 |
+ |
c_ccvt(c2, C_CSXY); |
670 |
+ |
scale = w1/c1->cy + w2/c2->cy; |
671 |
+ |
if (scale == 0.) |
672 |
+ |
return; |
673 |
+ |
scale = 1. / scale; |
674 |
+ |
cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale; |
675 |
+ |
cres->cy = (w1 + w2) * scale; |
676 |
+ |
cres->flags = C_CDXY|C_CSXY; |
677 |
+ |
} |
678 |
+ |
} |
679 |
+ |
|
680 |
+ |
|
681 |
+ |
#define C1 3.741832e-16 /* W-m^2 */ |
682 |
+ |
#define C2 1.4388e-2 /* m-K */ |
683 |
+ |
|
684 |
+ |
#define bbsp(l,t) (C1/((l)*(l)*(l)*(l)*(l)*(exp(C2/((t)*(l)))-1.))) |
685 |
+ |
#define bblm(t) (C2/5./(t)) |
686 |
+ |
|
687 |
+ |
static int |
688 |
+ |
setbbtemp(clr, tk) /* set black body spectrum */ |
689 |
+ |
register C_COLOR *clr; |
690 |
+ |
double tk; |
691 |
+ |
{ |
692 |
+ |
double sf, wl; |
693 |
+ |
register int i; |
694 |
+ |
|
695 |
+ |
if (tk < 1000) |
696 |
+ |
return(MG_EILL); |
697 |
+ |
wl = bblm(tk); /* scalefactor based on peak */ |
698 |
+ |
if (wl < C_CMINWL*1e-9) |
699 |
+ |
wl = C_CMINWL*1e-9; |
700 |
+ |
else if (wl > C_CMAXWL*1e-9) |
701 |
+ |
wl = C_CMAXWL*1e-9; |
702 |
+ |
sf = C_CMAXV/bbsp(wl,tk); |
703 |
+ |
clr->ssum = 0; |
704 |
+ |
for (i = 0; i < C_CNSS; i++) { |
705 |
+ |
wl = (C_CMINWL + i*C_CWLI)*1e-9; |
706 |
+ |
clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5; |
707 |
+ |
} |
708 |
+ |
clr->flags = C_CDSPEC|C_CSSPEC; |
709 |
+ |
clr->clock++; |
710 |
+ |
return(MG_OK); |
711 |
+ |
} |
712 |
+ |
|
713 |
+ |
#undef C1 |
714 |
+ |
#undef C2 |
715 |
+ |
#undef bbsp |
716 |
+ |
#undef bblm |