2 * Mesa 3-D graphics library
4 * Copyright (C) 1995-2006 Brian Paul
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Library for glut using mesa fbdev driver
24 * Written by Sean D'Epagnier (c) 2006
36 #define TOCMAP(x)(unsigned short)((x<0?0:x>1?1:x)*(GLfloat) ((1<<16) - 1))
37 #define TORMAP(x)(unsigned short)((x<0?0:x>1?1:x)*(GLfloat)(REVERSECMAPSIZE-1))
38 #define FROMCMAP(x) (GLfloat)x / (GLfloat)((1<<16) - 1)
40 static struct fb_cmap ColorMap
;
42 unsigned short RedColorMap
[256], GreenColorMap
[256], BlueColorMap
[256];
44 unsigned char ReverseColorMap
[REVERSECMAPSIZE
]
48 static void FindReverseMap(int r
, int g
, int b
)
51 int i
, shift
= 16 - REVERSECMAPSIZELOG
;
52 unsigned int minv
= -1, mini
= 0;
53 for(i
=0; i
<256; i
++) {
55 val
+= abs(r
-(RedColorMap
[i
]>>shift
));
56 val
+= abs(g
-(GreenColorMap
[i
]>>shift
));
57 val
+= abs(b
-(BlueColorMap
[i
]>>shift
));
63 ReverseColorMap
[r
][g
][b
] = mini
;
66 static void FillItemReverseColorMap(int r
, int g
, int b
)
68 FindReverseMap(r
, g
, b
);
70 FindReverseMap(r
-1, g
, b
);
71 if(r
< REVERSECMAPSIZE
- 1)
72 FindReverseMap(r
+1, g
, b
);
74 FindReverseMap(r
, g
-1, b
);
75 if(g
< REVERSECMAPSIZE
- 1)
76 FindReverseMap(r
, g
+1, b
);
78 FindReverseMap(r
, g
, b
-1);
79 if(b
< REVERSECMAPSIZE
- 1)
80 FindReverseMap(r
, g
, b
+1);
83 static void FillReverseColorMap(void)
86 for(r
= 0; r
< REVERSECMAPSIZE
; r
++)
87 for(g
= 0; g
< REVERSECMAPSIZE
; g
++)
88 for(b
= 0; b
< REVERSECMAPSIZE
; b
++)
89 FindReverseMap(r
, g
, b
);
92 void RestoreColorMap(void)
94 if (ioctl(FrameBufferFD
, FBIOPUTCMAP
, (void *) &ColorMap
) < 0)
95 sprintf(exiterror
, "ioctl(FBIOPUTCMAP) failed!\n");
98 void LoadColorMap(void)
100 /* we're assuming 256 entries here */
105 ColorMap
.red
= RedColorMap
;
106 ColorMap
.green
= GreenColorMap
;
107 ColorMap
.blue
= BlueColorMap
;
108 ColorMap
.transp
= NULL
;
110 if (ioctl(FrameBufferFD
, FBIOGETCMAP
, (void *) &ColorMap
) < 0)
111 sprintf(exiterror
, "ioctl(FBIOGETCMAP) failed!\n");
113 switch(VarInfo
.bits_per_pixel
) {
116 RedColorMap
[i
] = GreenColorMap
[i
] = BlueColorMap
[i
] = i
*65535/31;
120 RedColorMap
[i
] = BlueColorMap
[i
] = i
*65535/31;
122 GreenColorMap
[i
] = i
*65535/63;
127 RedColorMap
[i
] = GreenColorMap
[i
] = BlueColorMap
[i
] = i
*257;
133 if(DisplayMode
& GLUT_INDEX
)
134 FillReverseColorMap();
137 void glutSetColor(int cell
, GLfloat red
, GLfloat green
, GLfloat blue
)
139 if(cell
< 0 || cell
>= 256)
142 RedColorMap
[cell
] = TOCMAP(red
);
143 GreenColorMap
[cell
] = TOCMAP(green
);
144 BlueColorMap
[cell
] = TOCMAP(blue
);
148 FillItemReverseColorMap(TORMAP(red
), TORMAP(green
), TORMAP(blue
));
151 GLfloat
glutGetColor(int cell
, int component
)
153 if(!(DisplayMode
& GLUT_INDEX
))
156 if(cell
< 0 || cell
> 256)
161 return FROMCMAP(RedColorMap
[cell
]);
163 return FROMCMAP(GreenColorMap
[cell
]);
165 return FROMCMAP(BlueColorMap
[cell
]);
170 void glutCopyColormap(int win
)