29 |
|
C_MATERIAL *c_cmaterial = &c_unmaterial; |
30 |
|
C_VERTEX *c_cvertex = &c_unvertex; |
31 |
|
|
32 |
< |
static LUTAB clr_tab; /* color lookup table */ |
33 |
< |
static LUTAB mat_tab; /* material lookup table */ |
34 |
< |
static LUTAB vtx_tab; /* vertex lookup table */ |
32 |
> |
static LUTAB clr_tab = LU_SINIT(free,free); /* color lookup table */ |
33 |
> |
static LUTAB mat_tab = LU_SINIT(free,free); /* material lookup table */ |
34 |
> |
static LUTAB vtx_tab = LU_SINIT(free,free); /* vertex lookup table */ |
35 |
|
|
36 |
+ |
/* CIE 1931 Standard Observer */ |
37 |
+ |
static C_COLOR cie_xf = C_CIEX; |
38 |
+ |
static C_COLOR cie_yf = C_CIEY; |
39 |
+ |
static C_COLOR cie_zf = C_CIEZ; |
40 |
|
|
41 |
+ |
static int setspectrum(); |
42 |
+ |
static void mixcolors(); |
43 |
+ |
|
44 |
+ |
|
45 |
|
int |
46 |
|
c_hcolor(ac, av) /* handle color entity */ |
47 |
|
int ac; |
48 |
|
register char **av; |
49 |
|
{ |
50 |
+ |
double w, wsum; |
51 |
+ |
register int i; |
52 |
|
register LUENT *lp; |
53 |
|
|
54 |
|
switch (mg_entity(av[0])) { |
55 |
|
case MG_E_COLOR: /* get/set color context */ |
56 |
+ |
if (ac > 4) |
57 |
+ |
return(MG_EARGC); |
58 |
|
if (ac == 1) { /* set unnamed color context */ |
59 |
|
c_uncolor = c_dfcolor; |
60 |
|
c_ccolor = &c_uncolor; |
63 |
|
lp = lu_find(&clr_tab, av[1]); /* lookup context */ |
64 |
|
if (lp == NULL) |
65 |
|
return(MG_EMEM); |
66 |
+ |
c_ccolor = (C_COLOR *)lp->data; |
67 |
|
if (ac == 2) { /* reestablish previous context */ |
68 |
< |
if (lp->data == NULL) |
68 |
> |
if (c_ccolor == NULL) |
69 |
|
return(MG_EUNDEF); |
57 |
– |
c_ccolor = (C_COLOR *)lp->data; |
70 |
|
return(MG_OK); |
71 |
|
} |
72 |
|
if (av[2][0] != '=' || av[2][1]) |
73 |
|
return(MG_ETYPE); |
74 |
< |
if (lp->key == NULL) { /* create new color context */ |
74 |
> |
if (c_ccolor == NULL) { /* create new color context */ |
75 |
|
lp->key = (char *)malloc(strlen(av[1])+1); |
76 |
|
if (lp->key == NULL) |
77 |
|
return(MG_EMEM); |
79 |
|
lp->data = (char *)malloc(sizeof(C_COLOR)); |
80 |
|
if (lp->data == NULL) |
81 |
|
return(MG_EMEM); |
82 |
+ |
c_ccolor = (C_COLOR *)lp->data; |
83 |
+ |
c_ccolor->clock = 0; |
84 |
|
} |
71 |
– |
c_ccolor = (C_COLOR *)lp->data; |
85 |
|
if (ac == 3) { /* use default template */ |
86 |
+ |
i = c_ccolor->clock; |
87 |
|
*c_ccolor = c_dfcolor; |
88 |
+ |
c_ccolor->name = av[1]; |
89 |
+ |
c_ccolor->clock = i + 1; |
90 |
|
return(MG_OK); |
91 |
|
} |
92 |
|
lp = lu_find(&clr_tab, av[3]); /* lookup template */ |
94 |
|
return(MG_EMEM); |
95 |
|
if (lp->data == NULL) |
96 |
|
return(MG_EUNDEF); |
97 |
+ |
i = c_ccolor->clock; |
98 |
|
*c_ccolor = *(C_COLOR *)lp->data; |
99 |
< |
if (ac > 4) |
100 |
< |
return(MG_EARGC); |
99 |
> |
c_ccolor->name = av[1]; |
100 |
> |
c_ccolor->clock = i + 1; |
101 |
|
return(MG_OK); |
102 |
|
case MG_E_CXY: /* assign CIE XY value */ |
103 |
|
if (ac != 3) |
106 |
|
return(MG_ETYPE); |
107 |
|
c_ccolor->cx = atof(av[1]); |
108 |
|
c_ccolor->cy = atof(av[2]); |
109 |
+ |
c_ccolor->flags = C_CDXY|C_CSXY; |
110 |
|
if (c_ccolor->cx < 0. | c_ccolor->cy < 0. | |
111 |
|
c_ccolor->cx + c_ccolor->cy > 1.) |
112 |
|
return(MG_EILL); |
113 |
+ |
c_ccolor->clock++; |
114 |
|
return(MG_OK); |
115 |
+ |
case MG_E_CSPEC: /* assign spectral values */ |
116 |
+ |
if (ac < 5) |
117 |
+ |
return(MG_EARGC); |
118 |
+ |
if (!isint(av[1]) || !isint(av[2])) |
119 |
+ |
return(MG_ETYPE); |
120 |
+ |
return(setspectrum(c_ccolor, atoi(av[1]), atoi(av[2]), |
121 |
+ |
ac-3, av+3)); |
122 |
+ |
case MG_E_CMIX: /* mix colors */ |
123 |
+ |
if (ac < 5 || (ac-1)%2) |
124 |
+ |
return(MG_EARGC); |
125 |
+ |
if (!isflt(av[1])) |
126 |
+ |
return(MG_ETYPE); |
127 |
+ |
wsum = atof(av[1]); |
128 |
+ |
if (wsum < 0.) |
129 |
+ |
return(MG_EILL); |
130 |
+ |
if ((lp = lu_find(&clr_tab, av[2])) == NULL) |
131 |
+ |
return(MG_EMEM); |
132 |
+ |
if (lp->data == NULL) |
133 |
+ |
return(MG_EUNDEF); |
134 |
+ |
*c_ccolor = *(C_COLOR *)lp->data; |
135 |
+ |
for (i = 3; i < ac; i += 2) { |
136 |
+ |
if (!isflt(av[i])) |
137 |
+ |
return(MG_ETYPE); |
138 |
+ |
w = atof(av[i]); |
139 |
+ |
if (w < 0.) |
140 |
+ |
return(MG_EILL); |
141 |
+ |
if ((lp = lu_find(&clr_tab, av[i+1])) == NULL) |
142 |
+ |
return(MG_EMEM); |
143 |
+ |
if (lp->data == NULL) |
144 |
+ |
return(MG_EUNDEF); |
145 |
+ |
mixcolors(c_ccolor, wsum, c_ccolor, |
146 |
+ |
w, (C_COLOR *)lp->data); |
147 |
+ |
wsum += w; |
148 |
+ |
} |
149 |
+ |
return(MG_OK); |
150 |
|
} |
151 |
|
return(MG_EUNK); |
152 |
|
} |
157 |
|
int ac; |
158 |
|
register char **av; |
159 |
|
{ |
160 |
+ |
int i; |
161 |
|
register LUENT *lp; |
162 |
|
|
163 |
|
switch (mg_entity(av[0])) { |
164 |
|
case MG_E_MATERIAL: /* get/set material context */ |
165 |
+ |
if (ac > 4) |
166 |
+ |
return(MG_EARGC); |
167 |
|
if (ac == 1) { /* set unnamed material context */ |
168 |
|
c_unmaterial = c_dfmaterial; |
169 |
|
c_cmaterial = &c_unmaterial; |
172 |
|
lp = lu_find(&mat_tab, av[1]); /* lookup context */ |
173 |
|
if (lp == NULL) |
174 |
|
return(MG_EMEM); |
175 |
+ |
c_cmaterial = (C_MATERIAL *)lp->data; |
176 |
|
if (ac == 2) { /* reestablish previous context */ |
177 |
< |
if (lp->data == NULL) |
177 |
> |
if (c_cmaterial == NULL) |
178 |
|
return(MG_EUNDEF); |
121 |
– |
c_cmaterial = (C_MATERIAL *)lp->data; |
179 |
|
return(MG_OK); |
180 |
|
} |
181 |
|
if (av[2][0] != '=' || av[2][1]) |
182 |
|
return(MG_ETYPE); |
183 |
< |
if (lp->key == NULL) { /* create new material context */ |
183 |
> |
if (c_cmaterial == NULL) { /* create new material */ |
184 |
|
lp->key = (char *)malloc(strlen(av[1])+1); |
185 |
|
if (lp->key == NULL) |
186 |
|
return(MG_EMEM); |
188 |
|
lp->data = (char *)malloc(sizeof(C_MATERIAL)); |
189 |
|
if (lp->data == NULL) |
190 |
|
return(MG_EMEM); |
191 |
+ |
c_cmaterial = (C_MATERIAL *)lp->data; |
192 |
+ |
c_cmaterial->clock = 0; |
193 |
|
} |
135 |
– |
c_cmaterial = (C_MATERIAL *)lp->data; |
194 |
|
if (ac == 3) { /* use default template */ |
195 |
+ |
i = c_cmaterial->clock; |
196 |
|
*c_cmaterial = c_dfmaterial; |
197 |
< |
c_cmaterial->name = lp->key; |
197 |
> |
c_cmaterial->name = av[1]; |
198 |
> |
c_cmaterial->clock = i + 1; |
199 |
|
return(MG_OK); |
200 |
|
} |
201 |
|
lp = lu_find(&mat_tab, av[3]); /* lookup template */ |
203 |
|
return(MG_EMEM); |
204 |
|
if (lp->data == NULL) |
205 |
|
return(MG_EUNDEF); |
206 |
+ |
i = c_cmaterial->clock; |
207 |
|
*c_cmaterial = *(C_MATERIAL *)lp->data; |
208 |
< |
c_cmaterial->name = lp->key; |
209 |
< |
c_cmaterial->clock = 1; |
149 |
< |
if (ac > 4) |
150 |
< |
return(MG_EARGC); |
208 |
> |
c_cmaterial->name = av[1]; |
209 |
> |
c_cmaterial->clock = i + 1; |
210 |
|
return(MG_OK); |
211 |
|
case MG_E_RD: /* set diffuse reflectance */ |
212 |
|
if (ac != 2) |
239 |
|
if (c_cmaterial->td < 0. | c_cmaterial->td > 1.) |
240 |
|
return(MG_EILL); |
241 |
|
c_cmaterial->td_c = *c_ccolor; |
242 |
+ |
c_cmaterial->clock++; |
243 |
|
return(MG_OK); |
244 |
|
case MG_E_RS: /* set specular reflectance */ |
245 |
|
if (ac != 3) |
277 |
|
int ac; |
278 |
|
register char **av; |
279 |
|
{ |
280 |
+ |
int i; |
281 |
|
register LUENT *lp; |
282 |
|
|
283 |
|
switch (mg_entity(av[0])) { |
284 |
|
case MG_E_VERTEX: /* get/set vertex context */ |
285 |
+ |
if (ac > 4) |
286 |
+ |
return(MG_EARGC); |
287 |
|
if (ac == 1) { /* set unnamed vertex context */ |
288 |
|
c_unvertex = c_dfvertex; |
289 |
|
c_cvertex = &c_unvertex; |
292 |
|
lp = lu_find(&vtx_tab, av[1]); /* lookup context */ |
293 |
|
if (lp == NULL) |
294 |
|
return(MG_EMEM); |
295 |
+ |
c_cvertex = (C_VERTEX *)lp->data; |
296 |
|
if (ac == 2) { /* reestablish previous context */ |
297 |
< |
if (lp->data == NULL) |
297 |
> |
if (c_cvertex == NULL) |
298 |
|
return(MG_EUNDEF); |
235 |
– |
c_cvertex = (C_VERTEX *)lp->data; |
299 |
|
return(MG_OK); |
300 |
|
} |
301 |
|
if (av[2][0] != '=' || av[2][1]) |
302 |
|
return(MG_ETYPE); |
303 |
< |
if (lp->key == NULL) { /* create new vertex context */ |
303 |
> |
if (c_cvertex == NULL) { /* create new vertex context */ |
304 |
|
lp->key = (char *)malloc(strlen(av[1])+1); |
305 |
|
if (lp->key == NULL) |
306 |
|
return(MG_EMEM); |
308 |
|
lp->data = (char *)malloc(sizeof(C_VERTEX)); |
309 |
|
if (lp->data == NULL) |
310 |
|
return(MG_EMEM); |
311 |
+ |
c_cvertex = (C_VERTEX *)lp->data; |
312 |
|
} |
249 |
– |
c_cvertex = (C_VERTEX *)lp->data; |
313 |
|
if (ac == 3) { /* use default template */ |
314 |
+ |
i = c_cvertex->clock; |
315 |
|
*c_cvertex = c_dfvertex; |
316 |
+ |
c_cvertex->name = av[1]; |
317 |
+ |
c_cvertex->clock = i + 1; |
318 |
|
return(MG_OK); |
319 |
|
} |
320 |
|
lp = lu_find(&vtx_tab, av[3]); /* lookup template */ |
322 |
|
return(MG_EMEM); |
323 |
|
if (lp->data == NULL) |
324 |
|
return(MG_EUNDEF); |
325 |
+ |
i = c_cvertex->clock; |
326 |
|
*c_cvertex = *(C_VERTEX *)lp->data; |
327 |
< |
if (ac > 4) |
328 |
< |
return(MG_EARGC); |
327 |
> |
c_cvertex->name = av[1]; |
328 |
> |
c_cvertex->clock = i + 1; |
329 |
|
return(MG_OK); |
330 |
|
case MG_E_POINT: /* set point */ |
331 |
|
if (ac != 4) |
335 |
|
c_cvertex->p[0] = atof(av[1]); |
336 |
|
c_cvertex->p[1] = atof(av[2]); |
337 |
|
c_cvertex->p[2] = atof(av[3]); |
338 |
+ |
c_cvertex->clock++; |
339 |
|
return(MG_OK); |
340 |
|
case MG_E_NORMAL: /* set normal */ |
341 |
|
if (ac != 4) |
346 |
|
c_cvertex->n[1] = atof(av[2]); |
347 |
|
c_cvertex->n[2] = atof(av[3]); |
348 |
|
(void)normalize(c_cvertex->n); |
349 |
+ |
c_cvertex->clock++; |
350 |
|
return(MG_OK); |
351 |
|
} |
352 |
|
return(MG_EUNK); |
353 |
|
} |
354 |
|
|
355 |
|
|
287 |
– |
static void |
288 |
– |
freectx(lp) /* free a context table entry */ |
289 |
– |
register LUENT *lp; |
290 |
– |
{ |
291 |
– |
free((MEM_PTR)lp->key); |
292 |
– |
free((MEM_PTR)lp->data); |
293 |
– |
} |
294 |
– |
|
295 |
– |
|
356 |
|
void |
357 |
|
c_clearall() /* empty context tables */ |
358 |
|
{ |
359 |
|
c_uncolor = c_dfcolor; |
360 |
|
c_ccolor = &c_uncolor; |
361 |
< |
lu_done(&clr_tab, freectx); |
361 |
> |
lu_done(&clr_tab); |
362 |
|
c_unmaterial = c_dfmaterial; |
363 |
|
c_cmaterial = &c_unmaterial; |
364 |
< |
lu_done(&mat_tab, freectx); |
364 |
> |
lu_done(&mat_tab); |
365 |
|
c_unvertex = c_dfvertex; |
366 |
|
c_cvertex = &c_unvertex; |
367 |
< |
lu_done(&vtx_tab, freectx); |
367 |
> |
lu_done(&vtx_tab); |
368 |
|
} |
369 |
|
|
370 |
|
|
377 |
|
if ((lp = lu_find(&vtx_tab, name)) == NULL) |
378 |
|
return(NULL); |
379 |
|
return((C_VERTEX *)lp->data); |
380 |
+ |
} |
381 |
+ |
|
382 |
+ |
|
383 |
+ |
C_COLOR * |
384 |
+ |
c_getcolor(name) /* get a named color */ |
385 |
+ |
char *name; |
386 |
+ |
{ |
387 |
+ |
register LUENT *lp; |
388 |
+ |
|
389 |
+ |
if ((lp = lu_find(&clr_tab, name)) == NULL) |
390 |
+ |
return(NULL); |
391 |
+ |
return((C_COLOR *)lp->data); |
392 |
+ |
} |
393 |
+ |
|
394 |
+ |
|
395 |
+ |
int |
396 |
+ |
c_isgrey(clr) /* check if color is grey */ |
397 |
+ |
register C_COLOR *clr; |
398 |
+ |
{ |
399 |
+ |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) |
400 |
+ |
return(1); /* no settings == grey */ |
401 |
+ |
c_ccvt(clr, C_CSXY); |
402 |
+ |
return(clr->cx >= .323 && clr->cx <= .343 && |
403 |
+ |
clr->cy >= .323 && clr->cy <= .343); |
404 |
+ |
} |
405 |
+ |
|
406 |
+ |
|
407 |
+ |
void |
408 |
+ |
c_ccvt(clr, fl) /* convert color representations */ |
409 |
+ |
register C_COLOR *clr; |
410 |
+ |
int fl; |
411 |
+ |
{ |
412 |
+ |
double x, y, z; |
413 |
+ |
register int i; |
414 |
+ |
|
415 |
+ |
if (clr->flags & fl) /* already done */ |
416 |
+ |
return; |
417 |
+ |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) /* nothing set! */ |
418 |
+ |
*clr = c_dfcolor; |
419 |
+ |
else if (fl & C_CSXY) { /* cspec -> cxy */ |
420 |
+ |
x = y = z = 0.; |
421 |
+ |
for (i = 0; i < C_CNSS; i++) { |
422 |
+ |
x += cie_xf.ssamp[i] * clr->ssamp[i]; |
423 |
+ |
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
424 |
+ |
z += cie_zf.ssamp[i] * clr->ssamp[i]; |
425 |
+ |
} |
426 |
+ |
z += x + y; |
427 |
+ |
clr->cx = x / z; |
428 |
+ |
clr->cy = y / z; |
429 |
+ |
clr->flags |= C_CSXY; |
430 |
+ |
} else { /* cxy -> cspec */ |
431 |
+ |
z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.; |
432 |
+ |
x = clr->cx * z / cie_xf.ssum; |
433 |
+ |
y = clr->cy * z / cie_yf.ssum; |
434 |
+ |
z = (1. - clr->cx - clr->cy) * z / cie_zf.ssum; |
435 |
+ |
clr->ssum = 0; |
436 |
+ |
for (i = 0; i < C_CNSS; i++) |
437 |
+ |
clr->ssum += clr->ssamp[i] = |
438 |
+ |
x * cie_xf.ssamp[i] + |
439 |
+ |
y * cie_yf.ssamp[i] + |
440 |
+ |
z * cie_zf.ssamp[i] ; |
441 |
+ |
clr->flags |= C_CSSPEC; |
442 |
+ |
} |
443 |
+ |
} |
444 |
+ |
|
445 |
+ |
|
446 |
+ |
static int |
447 |
+ |
setspectrum(clr, wlmin, wlmax, ac, av) /* convert a spectrum */ |
448 |
+ |
register C_COLOR *clr; |
449 |
+ |
int wlmin, wlmax; |
450 |
+ |
int ac; |
451 |
+ |
char **av; |
452 |
+ |
{ |
453 |
+ |
double scale; |
454 |
+ |
float *va; |
455 |
+ |
register int i; |
456 |
+ |
int wl, pos; |
457 |
+ |
double wl0, wlstep; |
458 |
+ |
|
459 |
+ |
if (wlmin < C_CMINWL || wlmin >= wlmax || wlmax > C_CMAXWL) |
460 |
+ |
return(MG_EILL); |
461 |
+ |
if ((va = (float *)malloc(ac*sizeof(float))) == NULL) |
462 |
+ |
return(MG_EMEM); |
463 |
+ |
scale = 0.; /* get values and maximum */ |
464 |
+ |
for (i = 0; i < ac; i++) { |
465 |
+ |
if (!isflt(av[i])) |
466 |
+ |
return(MG_ETYPE); |
467 |
+ |
va[i] = atof(av[i]); |
468 |
+ |
if (va[i] < 0.) |
469 |
+ |
return(MG_EILL); |
470 |
+ |
if (va[i] > scale) |
471 |
+ |
scale = va[i]; |
472 |
+ |
} |
473 |
+ |
if (scale == 0.) |
474 |
+ |
return(MG_EILL); |
475 |
+ |
scale = C_CMAXV / scale; |
476 |
+ |
clr->ssum = 0; /* convert to our spacing */ |
477 |
+ |
wl0 = wlmin; |
478 |
+ |
wlstep = (double)(wlmax - wlmin)/(ac-1); |
479 |
+ |
pos = 0; |
480 |
+ |
for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI) |
481 |
+ |
if (wl < wlmin || wl > wlmax) |
482 |
+ |
clr->ssamp[i] = 0; |
483 |
+ |
else { |
484 |
+ |
while (wl0 + wlstep < wl+FTINY) { |
485 |
+ |
wl0 += wlstep; |
486 |
+ |
pos++; |
487 |
+ |
} |
488 |
+ |
if (wl+FTINY >= wl0 && wl-FTINY <= wl0) |
489 |
+ |
clr->ssamp[i] = scale*va[pos]; |
490 |
+ |
else /* interpolate if necessary */ |
491 |
+ |
clr->ssamp[i] = scale / wlstep * |
492 |
+ |
( va[pos]*(wl0+wlstep - wl) + |
493 |
+ |
va[pos+1]*(wl - wl0) ); |
494 |
+ |
clr->ssum += clr->ssamp[i]; |
495 |
+ |
} |
496 |
+ |
clr->flags = C_CDSPEC|C_CSSPEC; |
497 |
+ |
clr->clock++; |
498 |
+ |
free((MEM_PTR)va); |
499 |
+ |
return(MG_OK); |
500 |
+ |
} |
501 |
+ |
|
502 |
+ |
|
503 |
+ |
static void |
504 |
+ |
mixcolors(cres, w1, c1, w2, c2) /* mix two colors according to weights given */ |
505 |
+ |
register C_COLOR *cres, *c1, *c2; |
506 |
+ |
double w1, w2; |
507 |
+ |
{ |
508 |
+ |
double scale; |
509 |
+ |
float cmix[C_CNSS]; |
510 |
+ |
register int i; |
511 |
+ |
|
512 |
+ |
if ((c1->flags|c2->flags) & C_CDSPEC) { /* spectral mixing */ |
513 |
+ |
c_ccvt(c1, C_CSSPEC); |
514 |
+ |
c_ccvt(c2, C_CSSPEC); |
515 |
+ |
w1 /= (double)c1->ssum; |
516 |
+ |
w2 /= (double)c2->ssum; |
517 |
+ |
scale = 0.; |
518 |
+ |
for (i = 0; i < C_CNSS; i++) { |
519 |
+ |
cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i]; |
520 |
+ |
if (cmix[i] > scale) |
521 |
+ |
scale = cmix[i]; |
522 |
+ |
} |
523 |
+ |
scale = C_CMAXV / scale; |
524 |
+ |
cres->ssum = 0; |
525 |
+ |
for (i = 0; i < C_CNSS; i++) |
526 |
+ |
cres->ssum += cres->ssamp[i] = scale*cmix[i] + .5; |
527 |
+ |
cres->flags = C_CDSPEC|C_CSSPEC; |
528 |
+ |
} else { /* CIE xy mixing */ |
529 |
+ |
c_ccvt(c1, C_CSXY); |
530 |
+ |
c_ccvt(c2, C_CSXY); |
531 |
+ |
scale = 1. / (w1/c1->cy + w2/c2->cy); |
532 |
+ |
cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale; |
533 |
+ |
cres->cy = (w1 + w2) * scale; |
534 |
+ |
cres->flags = C_CDXY|C_CSXY; |
535 |
+ |
} |
536 |
+ |
cres->clock++; /* record the change */ |
537 |
|
} |