| 1 |
greg |
1.1 |
/* Copyright (c) 1994 Regents of the University of California */
|
| 2 |
|
|
|
| 3 |
|
|
#ifndef lint
|
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL";
|
| 5 |
|
|
#endif
|
| 6 |
|
|
|
| 7 |
|
|
/*
|
| 8 |
|
|
* Context handlers
|
| 9 |
|
|
*/
|
| 10 |
|
|
|
| 11 |
|
|
#include <stdio.h>
|
| 12 |
greg |
1.2 |
#include <math.h>
|
| 13 |
greg |
1.1 |
#include <string.h>
|
| 14 |
|
|
#include "parser.h"
|
| 15 |
|
|
#include "lookup.h"
|
| 16 |
|
|
|
| 17 |
|
|
/* default context values */
|
| 18 |
|
|
static C_COLOR c_dfcolor = C_DEFCOLOR;
|
| 19 |
|
|
static C_MATERIAL c_dfmaterial = C_DEFMATERIAL;
|
| 20 |
|
|
static C_VERTEX c_dfvertex = C_DEFVERTEX;
|
| 21 |
|
|
|
| 22 |
|
|
/* the unnamed contexts */
|
| 23 |
|
|
static C_COLOR c_uncolor = C_DEFCOLOR;
|
| 24 |
|
|
static C_MATERIAL c_unmaterial = C_DEFMATERIAL;
|
| 25 |
|
|
static C_VERTEX c_unvertex = C_DEFVERTEX;
|
| 26 |
|
|
|
| 27 |
|
|
/* the current contexts */
|
| 28 |
|
|
C_COLOR *c_ccolor = &c_uncolor;
|
| 29 |
greg |
1.12 |
char *c_ccname = NULL;
|
| 30 |
greg |
1.1 |
C_MATERIAL *c_cmaterial = &c_unmaterial;
|
| 31 |
greg |
1.12 |
char *c_cmname = NULL;
|
| 32 |
greg |
1.1 |
C_VERTEX *c_cvertex = &c_unvertex;
|
| 33 |
greg |
1.12 |
char *c_cvname = NULL;
|
| 34 |
greg |
1.1 |
|
| 35 |
greg |
1.3 |
static LUTAB clr_tab = LU_SINIT(free,free); /* color lookup table */
|
| 36 |
|
|
static LUTAB mat_tab = LU_SINIT(free,free); /* material lookup table */
|
| 37 |
|
|
static LUTAB vtx_tab = LU_SINIT(free,free); /* vertex lookup table */
|
| 38 |
greg |
1.1 |
|
| 39 |
greg |
1.5 |
/* CIE 1931 Standard Observer */
|
| 40 |
|
|
static C_COLOR cie_xf = C_CIEX;
|
| 41 |
|
|
static C_COLOR cie_yf = C_CIEY;
|
| 42 |
|
|
static C_COLOR cie_zf = C_CIEZ;
|
| 43 |
greg |
1.1 |
|
| 44 |
greg |
1.5 |
static int setspectrum();
|
| 45 |
|
|
static void mixcolors();
|
| 46 |
|
|
|
| 47 |
|
|
|
| 48 |
greg |
1.1 |
int
|
| 49 |
|
|
c_hcolor(ac, av) /* handle color entity */
|
| 50 |
|
|
int ac;
|
| 51 |
|
|
register char **av;
|
| 52 |
|
|
{
|
| 53 |
greg |
1.5 |
double w, wsum;
|
| 54 |
|
|
register int i;
|
| 55 |
greg |
1.1 |
register LUENT *lp;
|
| 56 |
|
|
|
| 57 |
|
|
switch (mg_entity(av[0])) {
|
| 58 |
|
|
case MG_E_COLOR: /* get/set color context */
|
| 59 |
greg |
1.4 |
if (ac > 4)
|
| 60 |
|
|
return(MG_EARGC);
|
| 61 |
greg |
1.1 |
if (ac == 1) { /* set unnamed color context */
|
| 62 |
|
|
c_uncolor = c_dfcolor;
|
| 63 |
|
|
c_ccolor = &c_uncolor;
|
| 64 |
greg |
1.12 |
c_ccname = NULL;
|
| 65 |
greg |
1.1 |
return(MG_OK);
|
| 66 |
|
|
}
|
| 67 |
|
|
lp = lu_find(&clr_tab, av[1]); /* lookup context */
|
| 68 |
|
|
if (lp == NULL)
|
| 69 |
|
|
return(MG_EMEM);
|
| 70 |
greg |
1.14 |
c_ccname = lp->key;
|
| 71 |
greg |
1.9 |
c_ccolor = (C_COLOR *)lp->data;
|
| 72 |
greg |
1.1 |
if (ac == 2) { /* reestablish previous context */
|
| 73 |
greg |
1.9 |
if (c_ccolor == NULL)
|
| 74 |
greg |
1.1 |
return(MG_EUNDEF);
|
| 75 |
|
|
return(MG_OK);
|
| 76 |
|
|
}
|
| 77 |
|
|
if (av[2][0] != '=' || av[2][1])
|
| 78 |
|
|
return(MG_ETYPE);
|
| 79 |
greg |
1.9 |
if (c_ccolor == NULL) { /* create new color context */
|
| 80 |
greg |
1.1 |
lp->key = (char *)malloc(strlen(av[1])+1);
|
| 81 |
|
|
if (lp->key == NULL)
|
| 82 |
|
|
return(MG_EMEM);
|
| 83 |
|
|
strcpy(lp->key, av[1]);
|
| 84 |
|
|
lp->data = (char *)malloc(sizeof(C_COLOR));
|
| 85 |
|
|
if (lp->data == NULL)
|
| 86 |
|
|
return(MG_EMEM);
|
| 87 |
greg |
1.14 |
c_ccname = lp->key;
|
| 88 |
greg |
1.9 |
c_ccolor = (C_COLOR *)lp->data;
|
| 89 |
|
|
c_ccolor->clock = 0;
|
| 90 |
greg |
1.1 |
}
|
| 91 |
greg |
1.10 |
i = c_ccolor->clock;
|
| 92 |
greg |
1.1 |
if (ac == 3) { /* use default template */
|
| 93 |
|
|
*c_ccolor = c_dfcolor;
|
| 94 |
greg |
1.9 |
c_ccolor->clock = i + 1;
|
| 95 |
greg |
1.1 |
return(MG_OK);
|
| 96 |
|
|
}
|
| 97 |
|
|
lp = lu_find(&clr_tab, av[3]); /* lookup template */
|
| 98 |
|
|
if (lp == NULL)
|
| 99 |
|
|
return(MG_EMEM);
|
| 100 |
|
|
if (lp->data == NULL)
|
| 101 |
|
|
return(MG_EUNDEF);
|
| 102 |
|
|
*c_ccolor = *(C_COLOR *)lp->data;
|
| 103 |
greg |
1.9 |
c_ccolor->clock = i + 1;
|
| 104 |
greg |
1.1 |
return(MG_OK);
|
| 105 |
|
|
case MG_E_CXY: /* assign CIE XY value */
|
| 106 |
|
|
if (ac != 3)
|
| 107 |
|
|
return(MG_EARGC);
|
| 108 |
|
|
if (!isflt(av[1]) || !isflt(av[2]))
|
| 109 |
|
|
return(MG_ETYPE);
|
| 110 |
|
|
c_ccolor->cx = atof(av[1]);
|
| 111 |
|
|
c_ccolor->cy = atof(av[2]);
|
| 112 |
greg |
1.5 |
c_ccolor->flags = C_CDXY|C_CSXY;
|
| 113 |
greg |
1.1 |
if (c_ccolor->cx < 0. | c_ccolor->cy < 0. |
|
| 114 |
|
|
c_ccolor->cx + c_ccolor->cy > 1.)
|
| 115 |
|
|
return(MG_EILL);
|
| 116 |
greg |
1.8 |
c_ccolor->clock++;
|
| 117 |
greg |
1.1 |
return(MG_OK);
|
| 118 |
greg |
1.5 |
case MG_E_CSPEC: /* assign spectral values */
|
| 119 |
|
|
if (ac < 5)
|
| 120 |
|
|
return(MG_EARGC);
|
| 121 |
|
|
if (!isint(av[1]) || !isint(av[2]))
|
| 122 |
|
|
return(MG_ETYPE);
|
| 123 |
|
|
return(setspectrum(c_ccolor, atoi(av[1]), atoi(av[2]),
|
| 124 |
|
|
ac-3, av+3));
|
| 125 |
|
|
case MG_E_CMIX: /* mix colors */
|
| 126 |
|
|
if (ac < 5 || (ac-1)%2)
|
| 127 |
|
|
return(MG_EARGC);
|
| 128 |
|
|
if (!isflt(av[1]))
|
| 129 |
|
|
return(MG_ETYPE);
|
| 130 |
|
|
wsum = atof(av[1]);
|
| 131 |
|
|
if (wsum < 0.)
|
| 132 |
|
|
return(MG_EILL);
|
| 133 |
|
|
if ((lp = lu_find(&clr_tab, av[2])) == NULL)
|
| 134 |
|
|
return(MG_EMEM);
|
| 135 |
|
|
if (lp->data == NULL)
|
| 136 |
|
|
return(MG_EUNDEF);
|
| 137 |
|
|
*c_ccolor = *(C_COLOR *)lp->data;
|
| 138 |
|
|
for (i = 3; i < ac; i += 2) {
|
| 139 |
|
|
if (!isflt(av[i]))
|
| 140 |
|
|
return(MG_ETYPE);
|
| 141 |
|
|
w = atof(av[i]);
|
| 142 |
|
|
if (w < 0.)
|
| 143 |
|
|
return(MG_EILL);
|
| 144 |
|
|
if ((lp = lu_find(&clr_tab, av[i+1])) == NULL)
|
| 145 |
|
|
return(MG_EMEM);
|
| 146 |
|
|
if (lp->data == NULL)
|
| 147 |
|
|
return(MG_EUNDEF);
|
| 148 |
|
|
mixcolors(c_ccolor, wsum, c_ccolor,
|
| 149 |
|
|
w, (C_COLOR *)lp->data);
|
| 150 |
|
|
wsum += w;
|
| 151 |
|
|
}
|
| 152 |
greg |
1.10 |
c_ccolor->clock++;
|
| 153 |
greg |
1.5 |
return(MG_OK);
|
| 154 |
greg |
1.1 |
}
|
| 155 |
|
|
return(MG_EUNK);
|
| 156 |
|
|
}
|
| 157 |
|
|
|
| 158 |
|
|
|
| 159 |
|
|
int
|
| 160 |
|
|
c_hmaterial(ac, av) /* handle material entity */
|
| 161 |
|
|
int ac;
|
| 162 |
|
|
register char **av;
|
| 163 |
|
|
{
|
| 164 |
greg |
1.9 |
int i;
|
| 165 |
greg |
1.1 |
register LUENT *lp;
|
| 166 |
|
|
|
| 167 |
|
|
switch (mg_entity(av[0])) {
|
| 168 |
|
|
case MG_E_MATERIAL: /* get/set material context */
|
| 169 |
greg |
1.4 |
if (ac > 4)
|
| 170 |
|
|
return(MG_EARGC);
|
| 171 |
greg |
1.1 |
if (ac == 1) { /* set unnamed material context */
|
| 172 |
|
|
c_unmaterial = c_dfmaterial;
|
| 173 |
|
|
c_cmaterial = &c_unmaterial;
|
| 174 |
greg |
1.12 |
c_cmname = NULL;
|
| 175 |
greg |
1.1 |
return(MG_OK);
|
| 176 |
|
|
}
|
| 177 |
|
|
lp = lu_find(&mat_tab, av[1]); /* lookup context */
|
| 178 |
|
|
if (lp == NULL)
|
| 179 |
|
|
return(MG_EMEM);
|
| 180 |
greg |
1.14 |
c_cmname = lp->key;
|
| 181 |
greg |
1.9 |
c_cmaterial = (C_MATERIAL *)lp->data;
|
| 182 |
greg |
1.1 |
if (ac == 2) { /* reestablish previous context */
|
| 183 |
greg |
1.9 |
if (c_cmaterial == NULL)
|
| 184 |
greg |
1.1 |
return(MG_EUNDEF);
|
| 185 |
|
|
return(MG_OK);
|
| 186 |
|
|
}
|
| 187 |
|
|
if (av[2][0] != '=' || av[2][1])
|
| 188 |
|
|
return(MG_ETYPE);
|
| 189 |
greg |
1.9 |
if (c_cmaterial == NULL) { /* create new material */
|
| 190 |
greg |
1.1 |
lp->key = (char *)malloc(strlen(av[1])+1);
|
| 191 |
|
|
if (lp->key == NULL)
|
| 192 |
|
|
return(MG_EMEM);
|
| 193 |
|
|
strcpy(lp->key, av[1]);
|
| 194 |
|
|
lp->data = (char *)malloc(sizeof(C_MATERIAL));
|
| 195 |
|
|
if (lp->data == NULL)
|
| 196 |
|
|
return(MG_EMEM);
|
| 197 |
greg |
1.14 |
c_cmname = lp->key;
|
| 198 |
greg |
1.9 |
c_cmaterial = (C_MATERIAL *)lp->data;
|
| 199 |
|
|
c_cmaterial->clock = 0;
|
| 200 |
greg |
1.1 |
}
|
| 201 |
greg |
1.10 |
i = c_cmaterial->clock;
|
| 202 |
greg |
1.1 |
if (ac == 3) { /* use default template */
|
| 203 |
|
|
*c_cmaterial = c_dfmaterial;
|
| 204 |
greg |
1.9 |
c_cmaterial->clock = i + 1;
|
| 205 |
greg |
1.1 |
return(MG_OK);
|
| 206 |
|
|
}
|
| 207 |
|
|
lp = lu_find(&mat_tab, av[3]); /* lookup template */
|
| 208 |
|
|
if (lp == NULL)
|
| 209 |
|
|
return(MG_EMEM);
|
| 210 |
|
|
if (lp->data == NULL)
|
| 211 |
|
|
return(MG_EUNDEF);
|
| 212 |
|
|
*c_cmaterial = *(C_MATERIAL *)lp->data;
|
| 213 |
greg |
1.9 |
c_cmaterial->clock = i + 1;
|
| 214 |
greg |
1.1 |
return(MG_OK);
|
| 215 |
|
|
case MG_E_RD: /* set diffuse reflectance */
|
| 216 |
|
|
if (ac != 2)
|
| 217 |
|
|
return(MG_EARGC);
|
| 218 |
|
|
if (!isflt(av[1]))
|
| 219 |
|
|
return(MG_ETYPE);
|
| 220 |
|
|
c_cmaterial->rd = atof(av[1]);
|
| 221 |
|
|
if (c_cmaterial->rd < 0. | c_cmaterial->rd > 1.)
|
| 222 |
|
|
return(MG_EILL);
|
| 223 |
|
|
c_cmaterial->rd_c = *c_ccolor;
|
| 224 |
greg |
1.2 |
c_cmaterial->clock++;
|
| 225 |
greg |
1.1 |
return(MG_OK);
|
| 226 |
|
|
case MG_E_ED: /* set diffuse emittance */
|
| 227 |
|
|
if (ac != 2)
|
| 228 |
|
|
return(MG_EARGC);
|
| 229 |
|
|
if (!isflt(av[1]))
|
| 230 |
|
|
return(MG_ETYPE);
|
| 231 |
|
|
c_cmaterial->ed = atof(av[1]);
|
| 232 |
|
|
if (c_cmaterial->ed < 0.)
|
| 233 |
|
|
return(MG_EILL);
|
| 234 |
|
|
c_cmaterial->ed_c = *c_ccolor;
|
| 235 |
greg |
1.2 |
c_cmaterial->clock++;
|
| 236 |
greg |
1.1 |
return(MG_OK);
|
| 237 |
|
|
case MG_E_TD: /* set diffuse transmittance */
|
| 238 |
|
|
if (ac != 2)
|
| 239 |
|
|
return(MG_EARGC);
|
| 240 |
|
|
if (!isflt(av[1]))
|
| 241 |
|
|
return(MG_ETYPE);
|
| 242 |
|
|
c_cmaterial->td = atof(av[1]);
|
| 243 |
|
|
if (c_cmaterial->td < 0. | c_cmaterial->td > 1.)
|
| 244 |
|
|
return(MG_EILL);
|
| 245 |
|
|
c_cmaterial->td_c = *c_ccolor;
|
| 246 |
greg |
1.8 |
c_cmaterial->clock++;
|
| 247 |
greg |
1.1 |
return(MG_OK);
|
| 248 |
|
|
case MG_E_RS: /* set specular reflectance */
|
| 249 |
|
|
if (ac != 3)
|
| 250 |
|
|
return(MG_EARGC);
|
| 251 |
|
|
if (!isflt(av[1]) || !isflt(av[2]))
|
| 252 |
|
|
return(MG_ETYPE);
|
| 253 |
|
|
c_cmaterial->rs = atof(av[1]);
|
| 254 |
|
|
c_cmaterial->rs_a = atof(av[2]);
|
| 255 |
|
|
if (c_cmaterial->rs < 0. | c_cmaterial->rs > 1. |
|
| 256 |
|
|
c_cmaterial->rs_a < 0.)
|
| 257 |
|
|
return(MG_EILL);
|
| 258 |
|
|
c_cmaterial->rs_c = *c_ccolor;
|
| 259 |
greg |
1.2 |
c_cmaterial->clock++;
|
| 260 |
greg |
1.1 |
return(MG_OK);
|
| 261 |
|
|
case MG_E_TS: /* set specular transmittance */
|
| 262 |
|
|
if (ac != 3)
|
| 263 |
|
|
return(MG_EARGC);
|
| 264 |
|
|
if (!isflt(av[1]) || !isflt(av[2]))
|
| 265 |
|
|
return(MG_ETYPE);
|
| 266 |
|
|
c_cmaterial->ts = atof(av[1]);
|
| 267 |
|
|
c_cmaterial->ts_a = atof(av[2]);
|
| 268 |
|
|
if (c_cmaterial->ts < 0. | c_cmaterial->ts > 1. |
|
| 269 |
|
|
c_cmaterial->ts_a < 0.)
|
| 270 |
|
|
return(MG_EILL);
|
| 271 |
|
|
c_cmaterial->ts_c = *c_ccolor;
|
| 272 |
greg |
1.2 |
c_cmaterial->clock++;
|
| 273 |
greg |
1.1 |
return(MG_OK);
|
| 274 |
greg |
1.13 |
case MG_E_SIDES: /* set number of sides */
|
| 275 |
|
|
if (ac != 2)
|
| 276 |
|
|
return(MG_EARGC);
|
| 277 |
|
|
if (!isint(av[1]))
|
| 278 |
|
|
return(MG_ETYPE);
|
| 279 |
|
|
i = atoi(av[1]);
|
| 280 |
|
|
if (i == 1)
|
| 281 |
|
|
c_cmaterial->sided = 1;
|
| 282 |
|
|
else if (i == 2)
|
| 283 |
|
|
c_cmaterial->sided = 0;
|
| 284 |
|
|
else
|
| 285 |
|
|
return(MG_EILL);
|
| 286 |
|
|
c_cmaterial->clock++;
|
| 287 |
|
|
return(MG_OK);
|
| 288 |
greg |
1.1 |
}
|
| 289 |
|
|
return(MG_EUNK);
|
| 290 |
|
|
}
|
| 291 |
|
|
|
| 292 |
|
|
|
| 293 |
|
|
int
|
| 294 |
|
|
c_hvertex(ac, av) /* handle a vertex entity */
|
| 295 |
|
|
int ac;
|
| 296 |
|
|
register char **av;
|
| 297 |
|
|
{
|
| 298 |
greg |
1.9 |
int i;
|
| 299 |
greg |
1.1 |
register LUENT *lp;
|
| 300 |
|
|
|
| 301 |
|
|
switch (mg_entity(av[0])) {
|
| 302 |
|
|
case MG_E_VERTEX: /* get/set vertex context */
|
| 303 |
greg |
1.4 |
if (ac > 4)
|
| 304 |
|
|
return(MG_EARGC);
|
| 305 |
greg |
1.1 |
if (ac == 1) { /* set unnamed vertex context */
|
| 306 |
|
|
c_unvertex = c_dfvertex;
|
| 307 |
|
|
c_cvertex = &c_unvertex;
|
| 308 |
greg |
1.12 |
c_cvname = NULL;
|
| 309 |
greg |
1.1 |
return(MG_OK);
|
| 310 |
|
|
}
|
| 311 |
|
|
lp = lu_find(&vtx_tab, av[1]); /* lookup context */
|
| 312 |
|
|
if (lp == NULL)
|
| 313 |
|
|
return(MG_EMEM);
|
| 314 |
greg |
1.14 |
c_cvname = lp->key;
|
| 315 |
greg |
1.9 |
c_cvertex = (C_VERTEX *)lp->data;
|
| 316 |
greg |
1.1 |
if (ac == 2) { /* reestablish previous context */
|
| 317 |
greg |
1.9 |
if (c_cvertex == NULL)
|
| 318 |
greg |
1.1 |
return(MG_EUNDEF);
|
| 319 |
|
|
return(MG_OK);
|
| 320 |
|
|
}
|
| 321 |
|
|
if (av[2][0] != '=' || av[2][1])
|
| 322 |
|
|
return(MG_ETYPE);
|
| 323 |
greg |
1.9 |
if (c_cvertex == NULL) { /* create new vertex context */
|
| 324 |
greg |
1.1 |
lp->key = (char *)malloc(strlen(av[1])+1);
|
| 325 |
|
|
if (lp->key == NULL)
|
| 326 |
|
|
return(MG_EMEM);
|
| 327 |
|
|
strcpy(lp->key, av[1]);
|
| 328 |
|
|
lp->data = (char *)malloc(sizeof(C_VERTEX));
|
| 329 |
|
|
if (lp->data == NULL)
|
| 330 |
|
|
return(MG_EMEM);
|
| 331 |
greg |
1.14 |
c_cvname = lp->key;
|
| 332 |
greg |
1.9 |
c_cvertex = (C_VERTEX *)lp->data;
|
| 333 |
greg |
1.1 |
}
|
| 334 |
greg |
1.10 |
i = c_cvertex->clock;
|
| 335 |
greg |
1.1 |
if (ac == 3) { /* use default template */
|
| 336 |
|
|
*c_cvertex = c_dfvertex;
|
| 337 |
greg |
1.9 |
c_cvertex->clock = i + 1;
|
| 338 |
greg |
1.1 |
return(MG_OK);
|
| 339 |
|
|
}
|
| 340 |
|
|
lp = lu_find(&vtx_tab, av[3]); /* lookup template */
|
| 341 |
|
|
if (lp == NULL)
|
| 342 |
|
|
return(MG_EMEM);
|
| 343 |
|
|
if (lp->data == NULL)
|
| 344 |
|
|
return(MG_EUNDEF);
|
| 345 |
|
|
*c_cvertex = *(C_VERTEX *)lp->data;
|
| 346 |
greg |
1.9 |
c_cvertex->clock = i + 1;
|
| 347 |
greg |
1.1 |
return(MG_OK);
|
| 348 |
|
|
case MG_E_POINT: /* set point */
|
| 349 |
|
|
if (ac != 4)
|
| 350 |
|
|
return(MG_EARGC);
|
| 351 |
|
|
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3]))
|
| 352 |
|
|
return(MG_ETYPE);
|
| 353 |
|
|
c_cvertex->p[0] = atof(av[1]);
|
| 354 |
|
|
c_cvertex->p[1] = atof(av[2]);
|
| 355 |
|
|
c_cvertex->p[2] = atof(av[3]);
|
| 356 |
greg |
1.8 |
c_cvertex->clock++;
|
| 357 |
greg |
1.1 |
return(MG_OK);
|
| 358 |
|
|
case MG_E_NORMAL: /* set normal */
|
| 359 |
|
|
if (ac != 4)
|
| 360 |
|
|
return(MG_EARGC);
|
| 361 |
|
|
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3]))
|
| 362 |
|
|
return(MG_ETYPE);
|
| 363 |
|
|
c_cvertex->n[0] = atof(av[1]);
|
| 364 |
|
|
c_cvertex->n[1] = atof(av[2]);
|
| 365 |
|
|
c_cvertex->n[2] = atof(av[3]);
|
| 366 |
|
|
(void)normalize(c_cvertex->n);
|
| 367 |
greg |
1.8 |
c_cvertex->clock++;
|
| 368 |
greg |
1.1 |
return(MG_OK);
|
| 369 |
|
|
}
|
| 370 |
|
|
return(MG_EUNK);
|
| 371 |
|
|
}
|
| 372 |
|
|
|
| 373 |
|
|
|
| 374 |
|
|
void
|
| 375 |
|
|
c_clearall() /* empty context tables */
|
| 376 |
|
|
{
|
| 377 |
|
|
c_uncolor = c_dfcolor;
|
| 378 |
|
|
c_ccolor = &c_uncolor;
|
| 379 |
greg |
1.15 |
c_ccname = NULL;
|
| 380 |
greg |
1.3 |
lu_done(&clr_tab);
|
| 381 |
greg |
1.1 |
c_unmaterial = c_dfmaterial;
|
| 382 |
|
|
c_cmaterial = &c_unmaterial;
|
| 383 |
greg |
1.15 |
c_cmname = NULL;
|
| 384 |
greg |
1.3 |
lu_done(&mat_tab);
|
| 385 |
greg |
1.1 |
c_unvertex = c_dfvertex;
|
| 386 |
|
|
c_cvertex = &c_unvertex;
|
| 387 |
greg |
1.15 |
c_cvname = NULL;
|
| 388 |
greg |
1.3 |
lu_done(&vtx_tab);
|
| 389 |
greg |
1.1 |
}
|
| 390 |
|
|
|
| 391 |
|
|
|
| 392 |
greg |
1.11 |
C_MATERIAL *
|
| 393 |
|
|
c_getmaterial(name) /* get a named material */
|
| 394 |
|
|
char *name;
|
| 395 |
|
|
{
|
| 396 |
|
|
register LUENT *lp;
|
| 397 |
|
|
|
| 398 |
|
|
if ((lp = lu_find(&mat_tab, name)) == NULL)
|
| 399 |
|
|
return(NULL);
|
| 400 |
|
|
return((C_MATERIAL *)lp->data);
|
| 401 |
|
|
}
|
| 402 |
|
|
|
| 403 |
|
|
|
| 404 |
greg |
1.1 |
C_VERTEX *
|
| 405 |
|
|
c_getvert(name) /* get a named vertex */
|
| 406 |
|
|
char *name;
|
| 407 |
|
|
{
|
| 408 |
|
|
register LUENT *lp;
|
| 409 |
|
|
|
| 410 |
|
|
if ((lp = lu_find(&vtx_tab, name)) == NULL)
|
| 411 |
|
|
return(NULL);
|
| 412 |
|
|
return((C_VERTEX *)lp->data);
|
| 413 |
greg |
1.5 |
}
|
| 414 |
|
|
|
| 415 |
|
|
|
| 416 |
greg |
1.7 |
C_COLOR *
|
| 417 |
|
|
c_getcolor(name) /* get a named color */
|
| 418 |
|
|
char *name;
|
| 419 |
|
|
{
|
| 420 |
|
|
register LUENT *lp;
|
| 421 |
|
|
|
| 422 |
|
|
if ((lp = lu_find(&clr_tab, name)) == NULL)
|
| 423 |
|
|
return(NULL);
|
| 424 |
|
|
return((C_COLOR *)lp->data);
|
| 425 |
|
|
}
|
| 426 |
|
|
|
| 427 |
|
|
|
| 428 |
greg |
1.5 |
int
|
| 429 |
|
|
c_isgrey(clr) /* check if color is grey */
|
| 430 |
|
|
register C_COLOR *clr;
|
| 431 |
|
|
{
|
| 432 |
greg |
1.8 |
if (!(clr->flags & (C_CSXY|C_CSSPEC)))
|
| 433 |
greg |
1.5 |
return(1); /* no settings == grey */
|
| 434 |
|
|
c_ccvt(clr, C_CSXY);
|
| 435 |
|
|
return(clr->cx >= .323 && clr->cx <= .343 &&
|
| 436 |
|
|
clr->cy >= .323 && clr->cy <= .343);
|
| 437 |
|
|
}
|
| 438 |
|
|
|
| 439 |
|
|
|
| 440 |
|
|
void
|
| 441 |
|
|
c_ccvt(clr, fl) /* convert color representations */
|
| 442 |
|
|
register C_COLOR *clr;
|
| 443 |
|
|
int fl;
|
| 444 |
|
|
{
|
| 445 |
|
|
double x, y, z;
|
| 446 |
|
|
register int i;
|
| 447 |
|
|
|
| 448 |
|
|
if (clr->flags & fl) /* already done */
|
| 449 |
|
|
return;
|
| 450 |
greg |
1.8 |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) /* nothing set! */
|
| 451 |
greg |
1.5 |
*clr = c_dfcolor;
|
| 452 |
|
|
else if (fl & C_CSXY) { /* cspec -> cxy */
|
| 453 |
|
|
x = y = z = 0.;
|
| 454 |
|
|
for (i = 0; i < C_CNSS; i++) {
|
| 455 |
|
|
x += cie_xf.ssamp[i] * clr->ssamp[i];
|
| 456 |
|
|
y += cie_yf.ssamp[i] * clr->ssamp[i];
|
| 457 |
|
|
z += cie_zf.ssamp[i] * clr->ssamp[i];
|
| 458 |
|
|
}
|
| 459 |
|
|
z += x + y;
|
| 460 |
|
|
clr->cx = x / z;
|
| 461 |
|
|
clr->cy = y / z;
|
| 462 |
|
|
clr->flags |= C_CSXY;
|
| 463 |
|
|
} else { /* cxy -> cspec */
|
| 464 |
|
|
z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.;
|
| 465 |
|
|
x = clr->cx * z / cie_xf.ssum;
|
| 466 |
|
|
y = clr->cy * z / cie_yf.ssum;
|
| 467 |
|
|
z = (1. - clr->cx - clr->cy) * z / cie_zf.ssum;
|
| 468 |
|
|
clr->ssum = 0;
|
| 469 |
|
|
for (i = 0; i < C_CNSS; i++)
|
| 470 |
|
|
clr->ssum += clr->ssamp[i] =
|
| 471 |
|
|
x * cie_xf.ssamp[i] +
|
| 472 |
|
|
y * cie_yf.ssamp[i] +
|
| 473 |
|
|
z * cie_zf.ssamp[i] ;
|
| 474 |
|
|
clr->flags |= C_CSSPEC;
|
| 475 |
|
|
}
|
| 476 |
|
|
}
|
| 477 |
|
|
|
| 478 |
|
|
|
| 479 |
|
|
static int
|
| 480 |
|
|
setspectrum(clr, wlmin, wlmax, ac, av) /* convert a spectrum */
|
| 481 |
|
|
register C_COLOR *clr;
|
| 482 |
|
|
int wlmin, wlmax;
|
| 483 |
|
|
int ac;
|
| 484 |
|
|
char **av;
|
| 485 |
|
|
{
|
| 486 |
|
|
double scale;
|
| 487 |
|
|
float *va;
|
| 488 |
|
|
register int i;
|
| 489 |
|
|
int wl, pos;
|
| 490 |
|
|
double wl0, wlstep;
|
| 491 |
|
|
|
| 492 |
|
|
if (wlmin < C_CMINWL || wlmin >= wlmax || wlmax > C_CMAXWL)
|
| 493 |
|
|
return(MG_EILL);
|
| 494 |
|
|
if ((va = (float *)malloc(ac*sizeof(float))) == NULL)
|
| 495 |
|
|
return(MG_EMEM);
|
| 496 |
|
|
scale = 0.; /* get values and maximum */
|
| 497 |
|
|
for (i = 0; i < ac; i++) {
|
| 498 |
|
|
if (!isflt(av[i]))
|
| 499 |
|
|
return(MG_ETYPE);
|
| 500 |
|
|
va[i] = atof(av[i]);
|
| 501 |
|
|
if (va[i] < 0.)
|
| 502 |
|
|
return(MG_EILL);
|
| 503 |
|
|
if (va[i] > scale)
|
| 504 |
|
|
scale = va[i];
|
| 505 |
|
|
}
|
| 506 |
|
|
if (scale == 0.)
|
| 507 |
|
|
return(MG_EILL);
|
| 508 |
|
|
scale = C_CMAXV / scale;
|
| 509 |
|
|
clr->ssum = 0; /* convert to our spacing */
|
| 510 |
|
|
wl0 = wlmin;
|
| 511 |
|
|
wlstep = (double)(wlmax - wlmin)/(ac-1);
|
| 512 |
|
|
pos = 0;
|
| 513 |
|
|
for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI)
|
| 514 |
|
|
if (wl < wlmin || wl > wlmax)
|
| 515 |
|
|
clr->ssamp[i] = 0;
|
| 516 |
|
|
else {
|
| 517 |
|
|
while (wl0 + wlstep < wl+FTINY) {
|
| 518 |
|
|
wl0 += wlstep;
|
| 519 |
|
|
pos++;
|
| 520 |
|
|
}
|
| 521 |
|
|
if (wl+FTINY >= wl0 && wl-FTINY <= wl0)
|
| 522 |
|
|
clr->ssamp[i] = scale*va[pos];
|
| 523 |
|
|
else /* interpolate if necessary */
|
| 524 |
|
|
clr->ssamp[i] = scale / wlstep *
|
| 525 |
|
|
( va[pos]*(wl0+wlstep - wl) +
|
| 526 |
|
|
va[pos+1]*(wl - wl0) );
|
| 527 |
|
|
clr->ssum += clr->ssamp[i];
|
| 528 |
|
|
}
|
| 529 |
|
|
clr->flags = C_CDSPEC|C_CSSPEC;
|
| 530 |
greg |
1.8 |
clr->clock++;
|
| 531 |
greg |
1.5 |
free((MEM_PTR)va);
|
| 532 |
|
|
return(MG_OK);
|
| 533 |
|
|
}
|
| 534 |
|
|
|
| 535 |
|
|
|
| 536 |
|
|
static void
|
| 537 |
|
|
mixcolors(cres, w1, c1, w2, c2) /* mix two colors according to weights given */
|
| 538 |
|
|
register C_COLOR *cres, *c1, *c2;
|
| 539 |
|
|
double w1, w2;
|
| 540 |
|
|
{
|
| 541 |
|
|
double scale;
|
| 542 |
|
|
float cmix[C_CNSS];
|
| 543 |
|
|
register int i;
|
| 544 |
|
|
|
| 545 |
|
|
if ((c1->flags|c2->flags) & C_CDSPEC) { /* spectral mixing */
|
| 546 |
|
|
c_ccvt(c1, C_CSSPEC);
|
| 547 |
|
|
c_ccvt(c2, C_CSSPEC);
|
| 548 |
|
|
w1 /= (double)c1->ssum;
|
| 549 |
|
|
w2 /= (double)c2->ssum;
|
| 550 |
|
|
scale = 0.;
|
| 551 |
|
|
for (i = 0; i < C_CNSS; i++) {
|
| 552 |
|
|
cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i];
|
| 553 |
|
|
if (cmix[i] > scale)
|
| 554 |
|
|
scale = cmix[i];
|
| 555 |
|
|
}
|
| 556 |
|
|
scale = C_CMAXV / scale;
|
| 557 |
|
|
cres->ssum = 0;
|
| 558 |
|
|
for (i = 0; i < C_CNSS; i++)
|
| 559 |
|
|
cres->ssum += cres->ssamp[i] = scale*cmix[i] + .5;
|
| 560 |
|
|
cres->flags = C_CDSPEC|C_CSSPEC;
|
| 561 |
|
|
} else { /* CIE xy mixing */
|
| 562 |
|
|
c_ccvt(c1, C_CSXY);
|
| 563 |
|
|
c_ccvt(c2, C_CSXY);
|
| 564 |
|
|
scale = 1. / (w1/c1->cy + w2/c2->cy);
|
| 565 |
|
|
cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale;
|
| 566 |
|
|
cres->cy = (w1 + w2) * scale;
|
| 567 |
|
|
cres->flags = C_CDXY|C_CSXY;
|
| 568 |
|
|
}
|
| 569 |
greg |
1.1 |
}
|