89556ef4a5c75ba73ac009f73d7982d595e1880e
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)
101 ColorMap
.red
= RedColorMap
;
102 ColorMap
.green
= GreenColorMap
;
103 ColorMap
.blue
= BlueColorMap
;
104 ColorMap
.transp
= NULL
;
106 if(DisplayMode
& GLUT_INDEX
) {
109 if (ioctl(FrameBufferFD
, FBIOGETCMAP
, (void *) &ColorMap
) < 0)
110 sprintf(exiterror
, "ioctl(FBIOGETCMAP) failed!\n");
112 FillReverseColorMap();
114 int rcols
= 1 << VarInfo
.red
.length
;
115 int gcols
= 1 << VarInfo
.green
.length
;
116 int bcols
= 1 << VarInfo
.blue
.length
;
120 ColorMap
.len
= gcols
;
122 for (i
= 0; i
< rcols
; i
++)
123 RedColorMap
[i
] = (65536/(rcols
-1)) * i
;
125 for (i
= 0; i
< gcols
; i
++)
126 GreenColorMap
[i
] = (65536/(gcols
-1)) * i
;
128 for (i
= 0; i
< bcols
; i
++)
129 BlueColorMap
[i
] = (65536/(bcols
-1)) * i
;
135 void glutSetColor(int cell
, GLfloat red
, GLfloat green
, GLfloat blue
)
137 if(cell
< 0 || cell
>= 256)
140 RedColorMap
[cell
] = TOCMAP(red
);
141 GreenColorMap
[cell
] = TOCMAP(green
);
142 BlueColorMap
[cell
] = TOCMAP(blue
);
146 FillItemReverseColorMap(TORMAP(red
), TORMAP(green
), TORMAP(blue
));
149 GLfloat
glutGetColor(int cell
, int component
)
151 if(!(DisplayMode
& GLUT_INDEX
))
154 if(cell
< 0 || cell
> 256)
159 return FROMCMAP(RedColorMap
[cell
]);
161 return FROMCMAP(GreenColorMap
[cell
]);
163 return FROMCMAP(BlueColorMap
[cell
]);
168 void glutCopyColormap(int win
)