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
37 #define TOCMAP(x)(unsigned short)((x<0?0:x>1?1:x)*(GLfloat) ((1<<16) - 1))
38 #define TORMAP(x)(unsigned short)((x<0?0:x>1?1:x)*(GLfloat)(REVERSECMAPSIZE-1))
39 #define FROMCMAP(x) (GLfloat)x / (GLfloat)((1<<16) - 1)
41 static struct fb_cmap ColorMap
, OriginalColorMap
;
43 unsigned short RedColorMap
[256], GreenColorMap
[256], BlueColorMap
[256];
45 unsigned char ReverseColorMap
[REVERSECMAPSIZE
]
49 static void FindReverseMap(int r
, int g
, int b
)
52 int i
, shift
= 16 - REVERSECMAPSIZELOG
;
53 unsigned int minv
= -1, mini
= 0;
54 for(i
=0; i
<256; i
++) {
56 val
+= abs(r
-(RedColorMap
[i
]>>shift
));
57 val
+= abs(g
-(GreenColorMap
[i
]>>shift
));
58 val
+= abs(b
-(BlueColorMap
[i
]>>shift
));
64 ReverseColorMap
[r
][g
][b
] = mini
;
67 static void FillItemReverseColorMap(int r
, int g
, int b
)
69 FindReverseMap(r
, g
, b
);
71 FindReverseMap(r
-1, g
, b
);
72 if(r
< REVERSECMAPSIZE
- 1)
73 FindReverseMap(r
+1, g
, b
);
75 FindReverseMap(r
, g
-1, b
);
76 if(g
< REVERSECMAPSIZE
- 1)
77 FindReverseMap(r
, g
+1, b
);
79 FindReverseMap(r
, g
, b
-1);
80 if(b
< REVERSECMAPSIZE
- 1)
81 FindReverseMap(r
, g
, b
+1);
84 static void FillReverseColorMap(void)
87 for(r
= 0; r
< REVERSECMAPSIZE
; r
++)
88 for(g
= 0; g
< REVERSECMAPSIZE
; g
++)
89 for(b
= 0; b
< REVERSECMAPSIZE
; b
++)
90 FindReverseMap(r
, g
, b
);
93 void RestoreColorMap(void)
95 if(FixedInfo
.visual
== FB_VISUAL_TRUECOLOR
)
98 if (ioctl(FrameBufferFD
, FBIOPUTCMAP
, (void *) &ColorMap
) < 0)
99 sprintf(exiterror
, "ioctl(FBIOPUTCMAP) failed!\n");
102 void LoadColorMap(void)
104 if(FixedInfo
.visual
== FB_VISUAL_TRUECOLOR
)
108 ColorMap
.red
= RedColorMap
;
109 ColorMap
.green
= GreenColorMap
;
110 ColorMap
.blue
= BlueColorMap
;
111 ColorMap
.transp
= NULL
;
113 if(DisplayMode
& GLUT_INDEX
) {
116 if (ioctl(FrameBufferFD
, FBIOGETCMAP
, (void *) &ColorMap
) < 0)
117 sprintf(exiterror
, "ioctl(FBIOGETCMAP) failed!\n");
119 FillReverseColorMap();
121 int rcols
= 1 << VarInfo
.red
.length
;
122 int gcols
= 1 << VarInfo
.green
.length
;
123 int bcols
= 1 << VarInfo
.blue
.length
;
127 ColorMap
.len
= gcols
;
129 for (i
= 0; i
< rcols
; i
++)
130 RedColorMap
[i
] = (65536/(rcols
-1)) * i
;
132 for (i
= 0; i
< gcols
; i
++)
133 GreenColorMap
[i
] = (65536/(gcols
-1)) * i
;
135 for (i
= 0; i
< bcols
; i
++)
136 BlueColorMap
[i
] = (65536/(bcols
-1)) * i
;
142 void glutSetColor(int cell
, GLfloat red
, GLfloat green
, GLfloat blue
)
144 if(cell
< 0 || cell
>= 256)
147 RedColorMap
[cell
] = TOCMAP(red
);
148 GreenColorMap
[cell
] = TOCMAP(green
);
149 BlueColorMap
[cell
] = TOCMAP(blue
);
153 FillItemReverseColorMap(TORMAP(red
), TORMAP(green
), TORMAP(blue
));
156 GLfloat
glutGetColor(int cell
, int component
)
158 if(!(DisplayMode
& GLUT_INDEX
))
161 if(cell
< 0 || cell
> 256)
166 return FROMCMAP(RedColorMap
[cell
]);
168 return FROMCMAP(GreenColorMap
[cell
]);
170 return FROMCMAP(BlueColorMap
[cell
]);
175 void glutCopyColormap(int win
)